Re-land BUCK build for pytorch mobile (#77612)

see https://github.com/pytorch/pytorch/pull/76480
fixed most lint errors
Pull Request resolved: https://github.com/pytorch/pytorch/pull/77612
Approved by: https://github.com/kit1980
This commit is contained in:
Linbin Yu
2022-05-17 00:30:11 +00:00
committed by PyTorch MergeBot
parent 0975174652
commit 1f8049566f
23 changed files with 3742 additions and 4 deletions

15
.buckconfig.oss Normal file
View File

@ -0,0 +1,15 @@
[buildfile]
name = BUILD.buck
[repositories]
bazel_skylib = third_party/bazel-skylib/
[download]
in_build = true
[cxx]
cxxflags = -std=c++17
should_remap_host_platform = true
[project]
default_flavors_mode=all

View File

@ -63,7 +63,54 @@ jobs:
wget https://github.com/facebook/buck/releases/download/v2021.01.12.01/buck.2021.01.12.01_all.deb
sudo apt install ./buck.2021.01.12.01_all.deb
- name: Build Buck target
- name: Download third party libraries and generate wrappers
run: |
echo -e "[buildfile]\n name = BUILD.buck\n" > .buckconfig
buck build --keep-going //third_party:
sh scripts/buck_setup.sh
- name: Build glog
run: |
buck build third_party:glog
- name: Build C10
run: |
buck build c10:c10
- name: Build cpuinfo
run: |
buck build third_party:cpuinfo
- name: Build pthreadpool
run: |
buck build third_party:pthreadpool
- name: Build XNNPACK
run: |
buck build third_party:XNNPACK
- name: Build QNNPACK
run: |
buck build aten/src/ATen/native/quantized/cpu/qnnpack/... --keep-going
- name: Build aten_cpu
run: |
buck build :aten_cpu
- name: Build torch_mobile_core
run: |
buck build :torch_mobile_core
- name: Build torch_mobile_all_ops
run: |
buck build :torch_mobile_all_ops
- name: Build mobile benchmark
run: |
buck build :ptmobile_benchmark
- name: Run lite interpreter model
run: |
buck run :ptmobile_benchmark -- --model=ios/TestApp/models/mobilenet_v2.ptl --input_dims=1,3,224,224 --input_type=float
- name: Build everything
run: |
buck build //... --keep-going

4
.gitignore vendored
View File

@ -325,3 +325,7 @@ pr.diff
.lsp-buck-out/
.lsp.buckd/
buck-out/
# Downloaded libraries
third_party/ruy/
third_party/glog/

View File

@ -144,6 +144,10 @@ include_patterns = [
'torch/utils/benchmark/utils/timer.py',
'torch/utils/benchmark/utils/valgrind_wrapper/**/*.py',
]
exclude_patterns = [
# (linbinyu) copied from internal repo
'tools/code_analyzer/gen_operators_yaml.py',
]
command = [
'python3',
'tools/linter/adapters/mypy_linter.py',

620
BUILD.buck Normal file
View File

@ -0,0 +1,620 @@
load("//tools/build_defs:glob_defs.bzl", "subdir_glob")
load("//tools/build_defs:fb_xplat_genrule.bzl", "fb_xplat_genrule")
load(
"//tools:build_variables.bzl",
"aten_cpu_source_list",
"aten_native_source_list",
"core_sources_common",
"jit_core_headers",
"jit_core_sources",
"libtorch_profiler_sources",
)
load(
"//:pt_defs.oss.bzl",
"USED_PT_BACKENDS",
"build_aten_cpu",
"gen_aten_files",
"gen_aten_libtorch_files",
"get_aten_codegen_extra_params",
"get_pt_compiler_flags",
"get_pt_preprocessor_flags",
"pt_operator_library",
"get_pt_ops_deps",
"aten_ufunc_generated_all_cpu_sources",
"TEMPLATE_SOURCE_LIST",
)
cxx_library(
name = "pthreadpool",
srcs = ['caffe2/utils/threadpool/pthreadpool.cc', 'caffe2/utils/threadpool/pthreadpool_impl.cc', 'caffe2/utils/threadpool/pthreadpool-cpp.cc', 'caffe2/utils/threadpool/thread_pool_guard.cpp', 'caffe2/utils/threadpool/ThreadPool.cc'],
deps = [':caffe2_headers', '//third_party:cpuinfo', '//third_party:glog', '//c10:c10', '//third_party:FXdiv'],
exported_deps = ['//third_party:pthreadpool'],
compiler_flags = ['-Wno-unused-function'],
preferred_linkage = "static",
exported_headers = subdir_glob([("", "caffe2/utils/threadpool/*.h")]),
exported_preprocessor_flags = ['-DUSE_PTHREADPOOL'],
header_namespace = "",
headers = [],
link_whole = True,
platform_preprocessor_flags = [['windows', ['-D_WINDOWS', '-D_WIN32', '-DWIN32', '-DNOMINMAX', '-D_CRT_SECURE_NO_WARNINGS', '-D_USE_MATH_DEFINES']], ['windows.*64$', ['-D_WIN64']]],
visibility = ['PUBLIC'],
)
cxx_library(
name = "caffe2_headers",
deps = ['//c10:c10'],
exported_headers = subdir_glob(
[
("", "caffe2/**/*.h"),
("", "binaries/**/*.h"),
("modules", "**/*.h"),
("aten/src", "ATen/core/**/*.h"),
],
exclude = [
"caffe2/fb/**/*.h",
"caffe2/mobile/contrib/libopencl-stub/**/*.h",
"caffe2/mobile/contrib/libvulkan-stub/**/*.h",
"caffe2/mobile/contrib/nnapi/**/*.h",
"caffe2/mobile/fb/binary/**/*.h",
"caffe2/mobile/fb/snpe_so/**/*.h",
"caffe2/mobile/fb/boltnn/bolt_lib/include/**/*.h",
"caffe2/mobile/contrib/snpe/**/*.h",
"caffe2/mobile/fb/qpl/jni/QuickPerformanceLogger.h",
"caffe2/share/fb/x3d/ldi/*.h",
"**/*.pb.h",
],
),
compiler_flags = ['-Os', '-fexceptions', '-frtti', '-Wno-shadow', '-Wno-unknown-pragmas', '-Wno-unused-variable', '-Wno-sign-compare', '-Icaffe2', '-Imodules', '-DEIGEN_NO_DEBUG', '-DCAFFE2_USE_LITE_PROTO', '-DCAFFE2_USE_GOOGLE_GLOG', '-DCAFFE2_RNN_NO_TEXT_FORMAT', '-DGEMMLOWP_ALLOW_SLOW_SCALAR_FALLBACK=1', '-DCAFFE2_IS_XPLAT_BUILD', '-DSTRIP_ERROR_MESSAGES', '-DUSE_INTERNAL_PTHREADPOOL_IMPL', '-DCAFFE2_USE_HPTT'],
preferred_linkage = "static",
platform_preprocessor_flags = [['windows', ['-D_WINDOWS', '-D_WIN32', '-DWIN32', '-DNOMINMAX', '-D_CRT_SECURE_NO_WARNINGS', '-D_USE_MATH_DEFINES']], ['windows.*64$', ['-D_WIN64']]],
preprocessor_flags = ['-DUSE_INTERNAL_PTHREADPOOL_IMPL'],
visibility = ['PUBLIC'],
)
cxx_library(
name = "common_core",
srcs = ['caffe2/core/common.cc'],
deps = [':caffe2_headers', '//c10:c10'],
exported_deps = [],
compiler_flags = ['-frtti', '-Os', '-Wno-unknown-pragmas', '-Wno-write-strings', '-Wno-unused-variable', '-Wno-unused-function', '-Wno-deprecated-declarations', '-Wno-shadow', '-Wno-global-constructors', '-Wno-missing-prototypes', '-std=gnu++17'],
preferred_linkage = "static",
header_namespace = "caffe2",
headers = [],
link_whole = True,
platform_preprocessor_flags = [['windows', ['-D_WINDOWS', '-D_WIN32', '-DWIN32', '-DNOMINMAX', '-D_CRT_SECURE_NO_WARNINGS', '-D_USE_MATH_DEFINES']], ['windows.*64$', ['-D_WIN64']]],
visibility = ['PUBLIC'],
)
cxx_library(
name = "th_header",
header_namespace = "",
exported_headers = subdir_glob([
# TH
("aten/src", "TH/*.h"),
("aten/src", "TH/*.hpp"),
("aten/src", "TH/generic/*.h"),
("aten/src", "TH/generic/*.hpp"),
("aten/src", "TH/generic/simd/*.h"),
("aten/src", "TH/vector/*.h"),
("aten/src", "TH/generic/*.c"),
("aten/src", "TH/generic/*.cpp"),
("aten/src/TH", "*.h"), # for #include <THGenerateFloatTypes.h>
# THNN
("aten/src", "THNN/*.h"),
("aten/src", "THNN/generic/*.h"),
("aten/src", "THNN/generic/*.c"),
]),
)
cxx_library(
name = "aten_header",
header_namespace = "",
exported_headers = subdir_glob([
# ATen Core
("aten/src", "ATen/core/**/*.h"),
("aten/src", "ATen/ops/*.h"),
# ATen Base
("aten/src", "ATen/*.h"),
("aten/src", "ATen/cpu/**/*.h"),
("aten/src", "ATen/detail/*.h"),
("aten/src", "ATen/quantized/*.h"),
("aten/src", "ATen/vulkan/*.h"),
("aten/src", "ATen/metal/*.h"),
("aten/src", "ATen/mps/*.h"),
("aten/src", "ATen/nnapi/*.h"),
# ATen Native
("aten/src", "ATen/native/*.h"),
("aten/src", "ATen/native/ao_sparse/quantized/cpu/*.h"),
("aten/src", "ATen/native/cpu/**/*.h"),
("aten/src", "ATen/native/sparse/*.h"),
("aten/src", "ATen/native/mps/*.h"),
("aten/src", "ATen/native/nested/*.h"),
("aten/src", "ATen/native/quantized/*.h"),
("aten/src", "ATen/native/quantized/cpu/*.h"),
("aten/src", "ATen/native/transformers/*.h"),
("aten/src", "ATen/native/ufunc/*.h"),
("aten/src", "ATen/native/utils/*.h"),
("aten/src", "ATen/native/vulkan/ops/*.h"),
("aten/src", "ATen/native/xnnpack/*.h"),
# Remove the following after modifying codegen for mobile.
("aten/src", "ATen/mkl/*.h"),
("aten/src", "ATen/native/mkl/*.h"),
("aten/src", "ATen/native/mkldnn/*.h"),
], exclude = ["aten/src/ATen/Config.h"]),
visibility = ["PUBLIC"],
)
cxx_library(
name = "jit_core_headers",
header_namespace = "",
exported_headers = subdir_glob([("", x) for x in jit_core_headers]),
)
cxx_library(
name = "generated_aten_config_header",
header_namespace = "ATen",
exported_headers = {
"Config.h": ":generate_aten_config[Config.h]",
},
)
cxx_library(
name = "torch_mobile_headers",
header_namespace = "",
exported_headers = subdir_glob(
[
("", "torch/csrc/jit/mobile/*.h"),
],
),
visibility = ["PUBLIC"],
)
fb_xplat_genrule(
name = "generate_aten_config",
srcs = [
"aten/src/ATen/Config.h.in",
],
cmd = " ".join([
"sed",
"-e 's/@AT_MKLDNN_ENABLED@/ATEN_MKLDNN_ENABLED_FBXPLAT/g'",
"-e 's/@AT_MKL_ENABLED@/ATEN_MKL_ENABLED_FBXPLAT/g'",
"-e 's/@AT_MKL_SEQUENTIAL@/ATEN_MKL_SEQUENTIAL_FBXPLAT/g'",
"-e 's/@AT_FFTW_ENABLED@/0/g'",
"-e 's/@AT_POCKETFFT_ENABLED@/0/g'",
"-e 's/@AT_NNPACK_ENABLED@/ATEN_NNPACK_ENABLED_FBXPLAT/g'",
"-e 's/@CAFFE2_STATIC_LINK_CUDA_INT@/CAFFE2_STATIC_LINK_CUDA_FBXPLAT/g'",
"-e 's/@AT_BUILD_WITH_BLAS@/USE_BLAS_FBXPLAT/g'",
"-e 's/@AT_PARALLEL_OPENMP@/AT_PARALLEL_OPENMP_FBXPLAT/g'",
"-e 's/@AT_PARALLEL_NATIVE@/AT_PARALLEL_NATIVE_FBXPLAT/g'",
"-e 's/@AT_PARALLEL_NATIVE_TBB@/AT_PARALLEL_NATIVE_TBB_FBXPLAT/g'",
"-e 's/@AT_BUILD_WITH_LAPACK@/USE_LAPACK_FBXPLAT/g'",
"-e 's/@AT_BLAS_F2C@/AT_BLAS_F2C_FBXPLAT/g'",
"-e 's/@AT_BLAS_USE_CBLAS_DOT@/AT_BLAS_USE_CBLAS_DOT_FBXPLAT/g'",
"aten/src/ATen/Config.h.in > $OUT/Config.h"
]),
outs = {
"Config.h": ["Config.h"],
},
default_outs = ["."],
)
gen_aten_files(
name = "gen_aten",
extra_flags = get_aten_codegen_extra_params(USED_PT_BACKENDS),
visibility = ["PUBLIC"],
)
ATEN_EXPORTED_HEADERS = {
"CPUFunctions.h": ":gen_aten[CPUFunctions.h]",
"CPUFunctions_inl.h": ":gen_aten[CPUFunctions_inl.h]",
"CompositeExplicitAutogradFunctions.h": ":gen_aten[CompositeExplicitAutogradFunctions.h]",
"CompositeExplicitAutogradFunctions_inl.h": ":gen_aten[CompositeExplicitAutogradFunctions_inl.h]",
"CompositeImplicitAutogradFunctions.h": ":gen_aten[CompositeImplicitAutogradFunctions.h]",
"CompositeImplicitAutogradFunctions_inl.h": ":gen_aten[CompositeImplicitAutogradFunctions_inl.h]",
"FunctionalInverses.h": ":gen_aten[FunctionalInverses.h]",
"Functions.h": ":gen_aten[Functions.h]",
"MethodOperators.h": ":gen_aten[MethodOperators.h]",
"NativeFunctions.h": ":gen_aten[NativeFunctions.h]",
"NativeMetaFunctions.h": ":gen_aten[NativeMetaFunctions.h]",
"Operators.h": ":gen_aten[Operators.h]",
"RedispatchFunctions.h": ":gen_aten[RedispatchFunctions.h]",
"core/TensorBody.h": ":gen_aten[core/TensorBody.h]",
"core/aten_interned_strings.h": ":gen_aten[core/aten_interned_strings.h]",
}
cxx_library(
name = "generated_aten_headers_cpu",
header_namespace = "ATen",
exported_headers = ATEN_EXPORTED_HEADERS,
)
filegroup(
name = "aten_src_path",
srcs = [
"aten/src/ATen/native/native_functions.yaml",
"aten/src/ATen/native/tags.yaml",
] + glob(["aten/src/ATen/templates/*"]),
visibility = [
"PUBLIC",
],
)
build_aten_cpu(
name = "aten_cpu",
srcs = jit_core_sources +
aten_cpu_source_list + [
# Generated
":gen_aten[Functions.cpp]",
":gen_aten[Operators_0.cpp]",
":gen_aten[Operators_1.cpp]",
":gen_aten[Operators_2.cpp]",
":gen_aten[Operators_3.cpp]",
":gen_aten[Operators_4.cpp]",
":gen_aten[core/ATenOpList.cpp]",
":gen_aten[core/TensorMethods.cpp]",
] + [
# Needed by ATen/native/EmbeddingBag.cpp
"caffe2/perfkernels/embedding_lookup_idx.cc",
],
)
gen_aten_libtorch_files(name = "gen_aten_libtorch")
GENERATED_AUTOGRAD_H = {
"Functions.h": ":gen_aten_libtorch[autograd/generated/Functions.h]",
"VariableType.h": ":gen_aten_libtorch[autograd/generated/VariableType.h]",
"variable_factories.h": ":gen_aten_libtorch[autograd/generated/variable_factories.h]",
# Don't build python bindings on mobile.
#"python_functions.h",
}
cxx_library(
name = "generated-autograd-headers",
header_namespace = "torch/csrc/autograd/generated",
exported_headers = GENERATED_AUTOGRAD_H,
visibility = ["PUBLIC"],
)
cxx_library(
name = "torch_mobile_observer",
srcs = [
"torch/csrc/jit/mobile/observer.cpp",
#"torch/fb/observers/MobileObserverUtil.cpp",
],
header_namespace = "",
exported_headers = subdir_glob(
[
("", "torch/csrc/jit/mobile/observer.h"),
#("", "torch/fb/observers/ObserverUtil.h"),
#("", "torch/fb/observers/MobileObserverUtil.h"),
],
),
visibility = ["PUBLIC"],
deps = [
"//c10:c10",
],
)
python_library(
name = "aten_code_template",
srcs = subdir_glob([
("aten", "src/ATen/code_template.py"),
]),
base_module = "",
visibility = ["PUBLIC"],
)
fb_xplat_genrule(
name = "generate-version-header",
srcs = [
"torch/csrc/api/include/torch/version.h.in",
"version.txt",
],
cmd = "$(exe //tools/setup_helpers:gen-version-header) " + " ".join([
"--template-path",
"torch/csrc/api/include/torch/version.h.in",
"--version-path",
"version.txt",
"--output-path",
"$OUT/version.h",
]),
outs = {
"version.h": ["version.h"],
},
default_outs = ["."],
)
cxx_library(
name = "generated-version-header",
header_namespace = "torch",
exported_headers = {
"version.h": ":generate-version-header[version.h]",
},
)
cxx_library(
name = "torch_headers",
header_namespace = "",
exported_headers = subdir_glob(
[
("torch/csrc/api/include", "torch/**/*.h"),
("", "torch/csrc/**/*.h"),
("", "torch/csrc/generic/*.cpp"),
("", "torch/script.h"),
("", "torch/library.h"),
("", "torch/custom_class.h"),
("", "torch/custom_class_detail.h"),
# Add again due to namespace difference from aten_header.
("", "aten/src/ATen/*.h"),
("", "aten/src/ATen/quantized/*.h"),
],
exclude = [
# Don't need on mobile.
"torch/csrc/Exceptions.h",
"torch/csrc/python_headers.h",
"torch/csrc/utils/auto_gil.h",
"torch/csrc/jit/serialization/mobile_bytecode_generated.h",
"torch/csrc/api/include/torch/version.h",
],
),
visibility = ["PUBLIC"],
deps = [
":generated-version-header",
],
)
cxx_library(
name = "torch_common",
srcs = core_sources_common,
compiler_flags = get_pt_compiler_flags(),
exported_preprocessor_flags = get_pt_preprocessor_flags(),
link_whole = True,
visibility = ["PUBLIC"],
deps = [
":aten_cpu",
":generated-autograd-headers",
":torch_headers",
"//third_party:glog",
"//c10:c10",
],
)
cxx_library(
name = "torch_mobile_deserialize_common",
srcs = [
"torch/csrc/jit/mobile/parse_bytecode.cpp",
"torch/csrc/jit/mobile/parse_operators.cpp",
"torch/csrc/jit/mobile/upgrader_mobile.cpp",
"torch/csrc/jit/serialization/import_read.cpp",
"torch/csrc/jit/serialization/unpickler.cpp",
],
header_namespace = "",
exported_headers = [
"torch/csrc/jit/serialization/import_read.h",
"torch/csrc/jit/serialization/unpickler.h",
],
compiler_flags = get_pt_compiler_flags(),
link_whole = True,
linker_flags = [
"-Wl,--no-as-needed",
],
visibility = ["PUBLIC"],
exported_deps = [
":aten_cpu",
":caffe2_headers",
":caffe2_serialize",
":torch_common",
":torch_headers",
":torch_mobile_headers",
":torch_mobile_module",
":torch_mobile_observer",
"//third_party:glog",
"//c10:c10",
],
)
cxx_library(
name = "caffe2_serialize",
srcs = [
"caffe2/serialize/file_adapter.cc",
"caffe2/serialize/inline_container.cc",
"caffe2/serialize/istream_adapter.cc",
"caffe2/serialize/read_adapter_interface.cc",
],
visibility = ["PUBLIC"],
deps = [
":caffe2_headers",
"//third_party:glog",
"//c10:c10",
"//third_party:miniz",
],
)
cxx_library(
name = "torch_mobile_deserialize",
srcs = [
"torch/csrc/jit/mobile/import.cpp",
],
header_namespace = "",
exported_headers = [
"torch/csrc/jit/mobile/import.h",
],
compiler_flags = get_pt_compiler_flags(),
link_whole = True,
linker_flags = [
"-Wl,--no-as-needed",
],
visibility = ["PUBLIC"],
exported_deps = [
":aten_cpu",
":caffe2_headers",
":caffe2_serialize",
":torch_common",
":torch_headers",
":torch_mobile_headers",
":torch_mobile_module",
":torch_mobile_observer",
"//third_party:glog",
"//c10:c10",
":torch_mobile_deserialize_common",
],
)
cxx_library(
name = "torch_mobile_module",
srcs = [
"torch/csrc/jit/mobile/function.cpp",
"torch/csrc/jit/mobile/interpreter.cpp",
"torch/csrc/jit/mobile/module.cpp",
],
header_namespace = "",
exported_headers = [],
compiler_flags = get_pt_compiler_flags(),
link_whole = True,
linker_flags = [
"-Wl,--no-as-needed",
],
visibility = ["PUBLIC"],
exported_deps = [
":aten_cpu",
":caffe2_headers",
":torch_common",
":torch_headers",
":torch_mobile_headers",
":torch_mobile_observer",
"//third_party:glog",
"//c10:c10",
],
)
cxx_library(
name = "torch_mobile_core",
srcs = [],
header_namespace = "",
exported_headers = [],
compiler_flags = get_pt_compiler_flags(),
exported_preprocessor_flags = get_pt_preprocessor_flags(),
link_whole = True,
linker_flags = [
"-Wl,--no-as-needed",
# "-ldl",
],
visibility = ["PUBLIC"],
deps = [
":generated-autograd-headers",
":torch_mobile_observer",
":torch_mobile_headers",
],
exported_deps = [
":aten_cpu",
":torch_common",
":torch_mobile_deserialize",
],
)
pt_operator_library(
name = "torch_mobile_ops_full_dev",
check_decl = False,
include_all_operators = True,
)
cxx_library(
name = "torch_mobile_all_ops",
visibility = ["PUBLIC"],
deps = get_pt_ops_deps(
name = "pt_ops_full",
train = False,
deps = [
":torch_mobile_ops_full_dev",
],
enable_flatbuffer = False,
),
)
python_library(
name = "gen_oplist_lib",
srcs = subdir_glob([
("tools/code_analyzer", "gen_oplist.py"),
("tools/code_analyzer", "gen_op_registration_allowlist.py"),
]),
base_module = "",
deps = [
"//third_party:pyyaml",
"//tools/lite_interpreter:gen_selected_mobile_ops_header",
"//torchgen:torchgen",
],
)
python_binary(
name = "gen_oplist",
main_module = "gen_oplist",
visibility = ["PUBLIC"],
deps = [
":gen_oplist_lib",
],
)
python_library(
name = "gen_operators_yaml_lib",
srcs = subdir_glob([
("tools/code_analyzer", "gen_operators_yaml.py"),
("tools/code_analyzer", "gen_op_registration_allowlist.py"),
]),
base_module = "",
deps = [
"//third_party:pyyaml",
"//torchgen:torchgen",
],
)
python_binary(
name = "gen_operators_yaml",
main_module = "gen_operators_yaml",
visibility = ["PUBLIC"],
deps = [
":gen_operators_yaml_lib",
],
)
cxx_binary(
name = 'ptmobile_benchmark',
srcs = [
'binaries/speed_benchmark_torch.cc',
],
compiler_flags = [
"-fexceptions",
"-frtti",
"-Wno-deprecated-declarations",
],
preprocessor_flags = [
"-DBUILD_LITE_INTERPRETER",
],
platform_linker_flags = [
(
"^linux.*$",
[
"-Wl,--no-as-needed",
"-ldl",
"-pthread",
],
),
],
deps = [
":torch_mobile_core",
":torch_mobile_all_ops",
"//c10:c10",
],
)
filegroup(
name = "templated_selective_build_srcs",
# NB: no glob here, there are generated targets in this list!
srcs = glob(TEMPLATE_SOURCE_LIST) + aten_ufunc_generated_all_cpu_sources(":gen_aten[{}]"),
visibility = [
"PUBLIC",
],
)

View File

@ -0,0 +1,143 @@
load("//tools/build_defs:glob_defs.bzl", "subdir_glob")
cxx_library(
name = "pytorch_qnnpack",
srcs = ['src/add.c', 'src/average-pooling.c', 'src/channel-shuffle.c', 'src/clamp.c', 'src/conv-prepack.cc', 'src/conv-run.cc', 'src/convolution.c', 'src/deconv-run.cc', 'src/deconvolution.c', 'src/fc-dynamic-run.cc', 'src/fc-prepack.cc', 'src/fc-run.cc', 'src/fully-connected.c', 'src/fully-connected-sparse.c', 'src/global-average-pooling.c', 'src/hardsigmoid.c', 'src/hardswish.c', 'src/indirection.c', 'src/init.c', 'src/leaky-relu.c', 'src/max-pooling.c', 'src/operator-delete.c', 'src/operator-run.c', 'src/pack_block_sparse.cc', 'src/sigmoid.c', 'src/softargmax.c', 'src/tanh.c'],
deps = [':qnnp_interface', ':ukernels_asm', ':ukernels_neon', ':ukernels_psimd', ':ukernels_scalar', ':ukernels_sse2', ':ukernels_sse41', ':ukernels_ssse3', '//third_party:cpuinfo', '//third_party:FP16', '//third_party:FXdiv'],
exported_deps = ['//third_party:cpuinfo'],
compiler_flags = ['-O2', '-DPYTORCH_QNNPACK_RUNTIME_QUANTIZATION', '-Wno-deprecated-declarations'],
preferred_linkage = "static",
exported_headers = subdir_glob([("src", "qnnpack/*.h"),("include", "*.h"),]),
header_namespace = "",
headers = subdir_glob([("src", "**/*.c"), ("src", "q8gemm_sparse/*.h"), ("src", "qnnpack/*.h"), ("src", "requantization/*.h")]),
link_whole = False,
platform_compiler_flags = [['armv7', ['-mfpu=neon']], ['^android-armv7$', ['-marm', '-mfloat-abi=softfp']]],
platform_preprocessor_flags = [['windows', ['-D_WINDOWS', '-D_WIN32', '-DWIN32', '-DNOMINMAX', '-D_CRT_SECURE_NO_WARNINGS', '-D_USE_MATH_DEFINES']], ['windows.*64$', ['-D_WIN64']]],
visibility = ['PUBLIC'],
)
cxx_library(
name = "ukernels_ssse3",
srcs = ['wrappers/requantization/gemmlowp-ssse3.c', 'wrappers/requantization/precise-ssse3.c', 'wrappers/requantization/q31-ssse3.c'],
deps = [':qnnp_interface', '//third_party:cpuinfo', '//third_party:FP16', '//third_party:FXdiv'],
exported_deps = [],
compiler_flags = ['-O3', '-ffast-math', '-Wno-error=unused-variable', '-Wno-shadow', '-DPYTORCH_QNNPACK_RUNTIME_QUANTIZATION'],
preferred_linkage = "static",
header_namespace = "",
headers = subdir_glob([("src", "**/*.c"), ("src", "q8gemm_sparse/*.h"), ("src", "qnnpack/*.h"), ("src", "requantization/*.h")]),
link_whole = False,
platform_compiler_flags = [['86', ['-mssse3', '-mno-sse4']], ['osmeta', ['-mosmeta-no-restrict-sse']]],
platform_preprocessor_flags = [['windows', ['-D_WINDOWS', '-D_WIN32', '-DWIN32', '-DNOMINMAX', '-D_CRT_SECURE_NO_WARNINGS', '-D_USE_MATH_DEFINES']], ['windows.*64$', ['-D_WIN64']]],
visibility = ['PUBLIC'],
)
cxx_library(
name = "ukernels_psimd",
srcs = ['src/requantization/fp32-psimd.c', 'src/requantization/precise-psimd.c', 'src/sgemm/6x8-psimd.c'],
deps = [':qnnp_interface', '//third_party:cpuinfo', '//third_party:FP16', '//third_party:FXdiv', '//third_party:psimd'],
exported_deps = [],
compiler_flags = ['-O3', '-ffast-math', '-DPYTORCH_QNNPACK_RUNTIME_QUANTIZATION'],
preferred_linkage = "static",
header_namespace = "",
headers = subdir_glob([("src", "**/*.c"), ("src", "q8gemm_sparse/*.h"), ("src", "qnnpack/*.h"), ("src", "requantization/*.h")]),
link_whole = False,
platform_compiler_flags = [['armv7', ['-mfpu=neon']], ['^android-armv7$', ['-marm', '-mfloat-abi=softfp']]],
platform_preprocessor_flags = [['windows', ['-D_WINDOWS', '-D_WIN32', '-DWIN32', '-DNOMINMAX', '-D_CRT_SECURE_NO_WARNINGS', '-D_USE_MATH_DEFINES']], ['windows.*64$', ['-D_WIN64']]],
visibility = ['PUBLIC'],
)
cxx_library(
name = "ukernels_scalar",
srcs = ['src/requantization/fp32-scalar.c', 'src/requantization/gemmlowp-scalar.c', 'src/requantization/precise-scalar.c', 'src/requantization/q31-scalar.c', 'src/u8lut32norm/scalar.c', 'src/x8lut/scalar.c'],
deps = [':qnnp_interface', '//third_party:cpuinfo', '//third_party:FP16', '//third_party:FXdiv'],
exported_deps = [],
compiler_flags = ['-O2', '-DPYTORCH_QNNPACK_RUNTIME_QUANTIZATION'],
preferred_linkage = "static",
header_namespace = "",
headers = subdir_glob([("src", "**/*.c"), ("src", "q8gemm_sparse/*.h"), ("src", "qnnpack/*.h"), ("src", "requantization/*.h")]),
link_whole = False,
platform_preprocessor_flags = [['windows', ['-D_WINDOWS', '-D_WIN32', '-DWIN32', '-DNOMINMAX', '-D_CRT_SECURE_NO_WARNINGS', '-D_USE_MATH_DEFINES']], ['windows.*64$', ['-D_WIN64']]],
visibility = ['PUBLIC'],
)
cxx_library(
name = "ukernels_asm",
srcs = ['wrappers/dummy.c', 'wrappers/hgemm/8x8-aarch32-neonfp16arith.S', 'wrappers/q8conv/4x8-aarch32-neon.S', 'wrappers/q8dwconv/up8x9-aarch32-neon.S', 'wrappers/q8dwconv/up8x9-aarch32-neon-per-channel.S', 'wrappers/q8gemm/4x8-aarch32-neon.S', 'wrappers/q8gemm/4x8-dq-aarch32-neon.S', 'wrappers/q8gemm/4x8c2-xzp-aarch32-neon.S', 'wrappers/q8gemm_sparse/4x4-packA-aarch32-neon.S', 'wrappers/q8gemm_sparse/4x8c1x4-dq-packedA-aarch32-neon.S', 'wrappers/q8gemm_sparse/4x8c8x1-dq-packedA-aarch32-neon.S', 'wrappers/q8gemm_sparse/8x4-packA-aarch64-neon.S', 'wrappers/q8gemm_sparse/8x8c1x4-dq-packedA-aarch64-neon.S', 'wrappers/q8gemm_sparse/8x8c8x1-dq-packedA-aarch64-neon.S', 'wrappers/q8conv/8x8-aarch64-neon.S', 'wrappers/q8gemm/8x8-aarch64-neon.S', 'wrappers/q8gemm/8x8-dq-aarch64-neon.S'],
deps = [],
exported_deps = [],
compiler_flags = ['-DPYTORCH_QNNPACK_RUNTIME_QUANTIZATION'],
preferred_linkage = "static",
header_namespace = "",
headers = subdir_glob([("src", "**/*.c"), ("src", "q8gemm_sparse/*.h"), ("src", "qnnpack/*.h"), ("src", "requantization/*.h")]),
link_whole = False,
platform_compiler_flags = [['^iphoneos-armv7$', ['-mfpu=neon-vfpv4']], ['osmeta', ['-mfpu=neon-vfpv4']]],
platform_preprocessor_flags = [['android', ['-D__ELF__=1']], ['tizen', ['-D__ELF__=1']], ['windows', ['-D_WINDOWS', '-D_WIN32', '-DWIN32', '-DNOMINMAX', '-D_CRT_SECURE_NO_WARNINGS', '-D_USE_MATH_DEFINES']], ['windows.*64$', ['-D_WIN64']]],
visibility = ['PUBLIC'],
)
cxx_library(
name = "ukernels_sse41",
srcs = ['wrappers/requantization/gemmlowp-sse4.c', 'wrappers/requantization/precise-sse4.c', 'wrappers/requantization/q31-sse4.c'],
deps = [':qnnp_interface', '//third_party:cpuinfo', '//third_party:FP16', '//third_party:FXdiv'],
exported_deps = [],
compiler_flags = ['-O3', '-ffast-math', '-Wno-error=unused-variable', '-Wno-shadow', '-DPYTORCH_QNNPACK_RUNTIME_QUANTIZATION'],
preferred_linkage = "static",
header_namespace = "",
headers = subdir_glob([("src", "**/*.c"), ("src", "q8gemm_sparse/*.h"), ("src", "qnnpack/*.h"), ("src", "requantization/*.h")]),
link_whole = False,
platform_compiler_flags = [['86', ['-msse4.1', '-mno-sse4.2']], ['osmeta', ['-mosmeta-no-restrict-sse']]],
platform_preprocessor_flags = [['windows', ['-D_WINDOWS', '-D_WIN32', '-DWIN32', '-DNOMINMAX', '-D_CRT_SECURE_NO_WARNINGS', '-D_USE_MATH_DEFINES']], ['windows.*64$', ['-D_WIN64']]],
visibility = ['PUBLIC'],
)
cxx_library(
name = "ukernels_neon",
srcs = ['wrappers/q8avgpool/mp8x9p8q-neon.c', 'wrappers/q8avgpool/up8x9-neon.c', 'wrappers/q8avgpool/up8xm-neon.c', 'wrappers/q8conv/4x8-neon.c', 'wrappers/q8conv/8x8-neon.c', 'wrappers/q8dwconv/mp8x25-neon.c', 'wrappers/q8dwconv/mp8x25-neon-per-channel.c', 'wrappers/q8dwconv/mp8x27-neon.c', 'wrappers/q8dwconv/up8x9-neon.c', 'wrappers/q8dwconv/up8x9-neon-per-channel.c', 'wrappers/q8gavgpool/mp8x7p7q-neon.c', 'wrappers/q8gavgpool/up8x7-neon.c', 'wrappers/q8gavgpool/up8xm-neon.c', 'wrappers/q8gemm/4x-sumrows-neon.c', 'wrappers/q8gemm/4x8-dq-neon.c', 'wrappers/q8gemm/4x8-neon.c', 'wrappers/q8gemm/4x8c2-xzp-neon.c', 'wrappers/q8gemm/6x4-neon.c', 'wrappers/q8gemm/8x8-neon.c', 'wrappers/q8vadd/neon.c', 'wrappers/requantization/fp32-neon.c', 'wrappers/requantization/gemmlowp-neon.c', 'wrappers/requantization/precise-neon.c', 'wrappers/requantization/q31-neon.c', 'wrappers/sgemm/5x8-neon.c', 'wrappers/sgemm/6x8-neon.c', 'wrappers/u8clamp/neon.c', 'wrappers/u8maxpool/16x9p8q-neon.c', 'wrappers/u8maxpool/sub16-neon.c', 'wrappers/u8rmax/neon.c', 'wrappers/x8zip/x2-neon.c', 'wrappers/x8zip/x3-neon.c', 'wrappers/x8zip/x4-neon.c', 'wrappers/x8zip/xm-neon.c'],
deps = [':qnnp_interface', '//third_party:cpuinfo', '//third_party:FP16', '//third_party:FXdiv'],
exported_deps = [],
compiler_flags = ['-O3', '-ffast-math', '-Wno-error=unused-variable', '-Wno-shadow', '-DPYTORCH_QNNPACK_RUNTIME_QUANTIZATION'],
preferred_linkage = "static",
header_namespace = "",
headers = subdir_glob([("src", "**/*.c"), ("src", "q8gemm_sparse/*.h"), ("src", "qnnpack/*.h"), ("src", "requantization/*.h")]),
link_whole = False,
platform_compiler_flags = [['armv7', ['-mfpu=neon']], ['^android-armv7$', ['-marm', '-mfloat-abi=softfp']]],
platform_preprocessor_flags = [['windows', ['-D_WINDOWS', '-D_WIN32', '-DWIN32', '-DNOMINMAX', '-D_CRT_SECURE_NO_WARNINGS', '-D_USE_MATH_DEFINES']], ['windows.*64$', ['-D_WIN64']]],
visibility = ['PUBLIC'],
)
cxx_library(
name = "ukernels_sse2",
srcs = ['wrappers/q8avgpool/mp8x9p8q-sse2.c', 'wrappers/q8avgpool/up8x9-sse2.c', 'wrappers/q8avgpool/up8xm-sse2.c', 'wrappers/q8conv/4x4c2-sse2.c', 'wrappers/q8dwconv/mp8x25-sse2.c', 'wrappers/q8dwconv/mp8x25-sse2-per-channel.c', 'wrappers/q8dwconv/mp8x27-sse2.c', 'wrappers/q8dwconv/up8x9-sse2.c', 'wrappers/q8dwconv/up8x9-sse2-per-channel.c', 'wrappers/q8gavgpool/mp8x7p7q-sse2.c', 'wrappers/q8gavgpool/up8x7-sse2.c', 'wrappers/q8gavgpool/up8xm-sse2.c', 'wrappers/q8gemm/2x4c8-sse2.c', 'wrappers/q8gemm/4x4c2-dq-sse2.c', 'wrappers/q8gemm/4x4c2-sse2.c', 'wrappers/q8gemm_sparse/8x4c1x4-packed-sse2.c', 'wrappers/q8vadd/sse2.c', 'wrappers/requantization/fp32-sse2.c', 'wrappers/requantization/gemmlowp-sse2.c', 'wrappers/requantization/precise-sse2.c', 'wrappers/requantization/q31-sse2.c', 'wrappers/u8clamp/sse2.c', 'wrappers/u8maxpool/16x9p8q-sse2.c', 'wrappers/u8maxpool/sub16-sse2.c', 'wrappers/u8rmax/sse2.c', 'wrappers/x8zip/x2-sse2.c', 'wrappers/x8zip/x3-sse2.c', 'wrappers/x8zip/x4-sse2.c', 'wrappers/x8zip/xm-sse2.c'],
deps = [':qnnp_interface', '//third_party:cpuinfo', '//third_party:FP16', '//third_party:FXdiv'],
exported_deps = [],
compiler_flags = ['-O3', '-ffast-math', '-Wno-error=unused-variable', '-Wno-shadow', '-DPYTORCH_QNNPACK_RUNTIME_QUANTIZATION'],
preferred_linkage = "static",
header_namespace = "",
headers = subdir_glob([("src", "**/*.c"), ("src", "q8gemm_sparse/*.h"), ("src", "qnnpack/*.h"), ("src", "requantization/*.h")]),
link_whole = False,
platform_compiler_flags = [['86', ['-msse2', '-mno-sse3']]],
platform_preprocessor_flags = [['windows', ['-D_WINDOWS', '-D_WIN32', '-DWIN32', '-DNOMINMAX', '-D_CRT_SECURE_NO_WARNINGS', '-D_USE_MATH_DEFINES']], ['windows.*64$', ['-D_WIN64']]],
visibility = ['PUBLIC'],
)
cxx_library(
name = "qnnp_interface",
srcs = [],
deps = ['//third_party:pthreadpool_header'],
exported_deps = [],
compiler_flags = ['-DPYTORCH_QNNPACK_RUNTIME_QUANTIZATION'],
preferred_linkage = "static",
header_namespace = "",
headers = subdir_glob([("src", "**/*.c"), ("src", "q8gemm_sparse/*.h"), ("src", "qnnpack/*.h"), ("src", "requantization/*.h")]),
link_whole = False,
platform_preprocessor_flags = [['windows', ['-D_WINDOWS', '-D_WIN32', '-DWIN32', '-DNOMINMAX', '-D_CRT_SECURE_NO_WARNINGS', '-D_USE_MATH_DEFINES']], ['windows.*64$', ['-D_WIN64']]],
visibility = ['PUBLIC'],
)

50
c10/BUILD.buck Normal file
View File

@ -0,0 +1,50 @@
load("//tools/build_defs:glob_defs.bzl", "subdir_glob")
cxx_library(
name = "c10",
srcs = glob(
["**/*.cpp"],
exclude = [
"test/**/*.cpp",
"benchmark/**/*.cpp",
"cuda/**/*.cpp",
],
),
deps = [
"//third_party:fmt",
"//third_party:glog",
],
exported_deps = [],
compiler_flags = [
"-Werror",
"-Wno-global-constructors",
"-DDISABLE_NAMEDTENSOR",
"-DSUPPORTS_BACKTRACE=0"
],
exported_headers = subdir_glob(
[
("", "**/*.h"),
],
exclude = [
"test/**/*.h",
"benchmark/**/*.h",
"cuda/**/*.h",
],
),
exported_linker_flags = [],
exported_preprocessor_flags = [
'-DC10_USING_CUSTOM_GENERATED_MACROS',
'-DC10_USE_GLOG',
'-DC10_USE_MINIMAL_GLOG',
'-DC10_DISABLE_NUMA',
'-DC10_MOBILE',
'-fexceptions',
'-Wno-global-constructors'
],
header_namespace = "c10",
link_whole = True,
platform_preprocessor_flags = [['windows', ['-D_WINDOWS', '-D_WIN32', '-DWIN32', '-DNOMINMAX', '-D_CRT_SECURE_NO_WARNINGS', '-D_USE_MATH_DEFINES']], ['windows.*64$', ['-D_WIN64']]],
preprocessor_flags = ['-DC10_BUILD_MAIN_LIB'],
reexport_all_header_dependencies = True,
visibility = ['PUBLIC'],
)

809
pt_defs.oss.bzl Normal file
View File

@ -0,0 +1,809 @@
load("@bazel_skylib//lib:paths.bzl", "paths")
load(
"//tools:build_variables.bzl",
"aten_native_source_list",
)
load(
"//tools:ufunc_defs.bzl",
"aten_ufunc_generated_cpu_kernel_sources",
"aten_ufunc_generated_cpu_sources",
"aten_ufunc_generated_cuda_sources",
)
load("//tools/build_defs:fb_xplat_genrule.bzl", "fb_xplat_genrule")
load("//tools/build_defs:type_defs.bzl", "is_list", "is_string")
USED_PT_BACKENDS = [
"CPU",
"QuantizedCPU",
"SparseCPU", # brings ~20 kb size regression
]
# This needs to be kept in sync with https://github.com/pytorch/pytorch/blob/release/1.9/torchgen/gen.py#L892
PT_BACKEND_HEADERS = [
"CPU",
"CUDA",
"CompositeExplicitAutograd",
"CompositeImplicitAutograd",
"Meta",
]
PT_BASE_OPS = [
"aten::_coalesced_",
"aten::_copy_from",
"aten::_empty_affine_quantized",
"aten::_empty_per_channel_affine_quantized",
"aten::_indices",
"aten::_nnz",
"aten::_values",
"aten::add",
"aten::add_",
"aten::arange",
"aten::as_strided",
"aten::as_strided_",
"aten::cat",
"aten::clone",
"aten::coalesce",
"aten::contiguous",
"aten::copy_",
"aten::copy_sparse_to_sparse_",
"aten::dense_dim",
"aten::dequantize",
"aten::div",
"aten::div_",
"aten::empty",
"aten::empty_like",
"aten::empty_strided",
"aten::empty.memory_format",
"aten::eq",
"aten::equal",
"aten::expand",
"aten::fill_",
"aten::is_coalesced",
"aten::is_complex",
"aten::is_floating_point",
"aten::is_leaf",
"aten::is_nonzero",
"aten::item",
"aten::max",
"aten::min",
"aten::mul",
"aten::mul_",
"aten::narrow",
"aten::ne",
"aten::permute",
"aten::q_per_channel_axis",
"aten::q_per_channel_scales",
"aten::q_per_channel_zero_points",
"aten::q_scale",
"aten::q_zero_point",
"aten::qscheme",
"aten::quantize_per_tensor",
"aten::reshape",
"aten::_reshape_alias",
"aten::resize_",
"aten::resize_as_",
"aten::scalar_tensor",
"aten::select",
"aten::set_",
"aten::size",
"aten::slice",
"aten::sparse_dim",
"aten::sparse_resize_and_clear_",
"aten::squeeze",
"aten::squeeze_",
"aten::stride",
"aten::sub",
"aten::sub_",
"aten::sum",
"aten::t",
"aten::to",
"aten::_to_copy",
"aten::unsqueeze",
"aten::view",
"aten::zero_",
"aten::zeros",
"aten::zeros_like",
]
def get_aten_compiler_flags():
return ATEN_COMPILER_FLAGS
def get_generate_code_bin_outs():
return {
"autograd/generated/ADInplaceOrViewTypeEverything.cpp": ["autograd/generated/ADInplaceOrViewTypeEverything.cpp"],
"autograd/generated/ADInplaceOrViewType_0.cpp": ["autograd/generated/ADInplaceOrViewType_0.cpp"],
"autograd/generated/ADInplaceOrViewType_1.cpp": ["autograd/generated/ADInplaceOrViewType_1.cpp"],
"autograd/generated/Functions.cpp": ["autograd/generated/Functions.cpp"],
"autograd/generated/Functions.h": ["autograd/generated/Functions.h"],
"autograd/generated/TraceTypeEverything.cpp": ["autograd/generated/TraceTypeEverything.cpp"],
"autograd/generated/TraceType_0.cpp": ["autograd/generated/TraceType_0.cpp"],
"autograd/generated/TraceType_1.cpp": ["autograd/generated/TraceType_1.cpp"],
"autograd/generated/TraceType_2.cpp": ["autograd/generated/TraceType_2.cpp"],
"autograd/generated/TraceType_3.cpp": ["autograd/generated/TraceType_3.cpp"],
"autograd/generated/TraceType_4.cpp": ["autograd/generated/TraceType_4.cpp"],
"autograd/generated/VariableType.h": ["autograd/generated/VariableType.h"],
"autograd/generated/VariableTypeEverything.cpp": ["autograd/generated/VariableTypeEverything.cpp"],
"autograd/generated/VariableType_0.cpp": ["autograd/generated/VariableType_0.cpp"],
"autograd/generated/VariableType_1.cpp": ["autograd/generated/VariableType_1.cpp"],
"autograd/generated/VariableType_2.cpp": ["autograd/generated/VariableType_2.cpp"],
"autograd/generated/VariableType_3.cpp": ["autograd/generated/VariableType_3.cpp"],
"autograd/generated/VariableType_4.cpp": ["autograd/generated/VariableType_4.cpp"],
"autograd/generated/variable_factories.h": ["autograd/generated/variable_factories.h"],
}
ATEN_COMPILER_FLAGS = [
"-fexceptions",
"-frtti",
"-fPIC",
"-Os",
"-Wno-absolute-value",
"-Wno-deprecated-declarations",
"-Wno-macro-redefined",
"-Wno-tautological-constant-out-of-range-compare",
"-Wno-unknown-pragmas",
"-Wno-unknown-warning-option",
"-Wno-unused-function",
"-Wno-unused-variable",
"-Wno-pass-failed",
"-Wno-shadow",
]
PT_COMPILER_FLAGS = [
"-frtti",
"-Os",
"-Wno-unknown-pragmas",
"-Wno-write-strings",
"-Wno-unused-variable",
"-Wno-unused-function",
"-Wno-deprecated-declarations",
"-Wno-shadow",
"-Wno-global-constructors",
"-Wno-missing-prototypes",
"-std=gnu++17", # to accommodate Eigen
]
def get_template_source_dict():
ret = {}
for file_path in TEMPLATE_SOURCE_LIST:
path_prefix = paths.dirname(file_path)
if path_prefix not in ret:
ret[path_prefix] = []
ret[path_prefix].append(file_path)
return ret
def get_gen_oplist_outs():
return {
#"SupportedMobileModelsRegistration.cpp": [
# "SupportedMobileModelsRegistration.cpp",
#],
"selected_mobile_ops.h": [
"selected_mobile_ops.h",
],
"selected_operators.yaml": [
"selected_operators.yaml",
],
}
def get_pt_compiler_flags():
return PT_COMPILER_FLAGS
def get_aten_preprocessor_flags():
# read_config is not allowed outside of function in Starlark
ATEN_PREPROCESSOR_FLAGS = [
"-DC10_MOBILE",
"-DCPU_CAPABILITY_DEFAULT",
"-DCPU_CAPABILITY=DEFAULT",
"-DCAFFE2_USE_LITE_PROTO",
"-DATEN_CUDNN_ENABLED_FBXPLAT=0",
"-DATEN_MKLDNN_ENABLED_FBXPLAT=0",
"-DATEN_NNPACK_ENABLED_FBXPLAT=0",
"-DATEN_MKL_ENABLED_FBXPLAT=0",
"-DATEN_MKL_SEQUENTIAL_FBXPLAT=0",
"-DUSE_PYTORCH_METAL",
"-DUSE_PYTORCH_QNNPACK",
"-DUSE_XNNPACK",
"-DNO_EXPORT",
"-DPYTORCH_QNNPACK_RUNTIME_QUANTIZATION",
"-DAT_PARALLEL_OPENMP_FBXPLAT=0",
"-DAT_PARALLEL_NATIVE_FBXPLAT=1",
"-DAT_PARALLEL_NATIVE_TBB_FBXPLAT=0",
"-DUSE_LAPACK_FBXPLAT=0",
"-DAT_BLAS_F2C_FBXPLAT=0",
"-DAT_BLAS_USE_CBLAS_DOT_FBXPLAT=0",
"-DUSE_RUY_QMATMUL", # need third_party:ruy
]
# if get_disable_per_op_profiling():
ATEN_PREPROCESSOR_FLAGS.append("-DPYTORCH_DISABLE_PER_OP_PROFILING")
return ATEN_PREPROCESSOR_FLAGS
TEMPLATE_SOURCE_LIST = [
"torch/csrc/jit/runtime/register_prim_ops.cpp",
"torch/csrc/jit/runtime/register_special_ops.cpp",
] + aten_native_source_list
# For selective build, we can lump the CPU and CPU kernel sources altogether
# because there is only ever one vectorization variant that is compiled
def aten_ufunc_generated_all_cpu_sources(gencode_pattern = "{}"):
return (
aten_ufunc_generated_cpu_sources(gencode_pattern) +
aten_ufunc_generated_cpu_kernel_sources(gencode_pattern)
)
def get_template_registration_files_outs():
outs = {}
for file_path in TEMPLATE_SOURCE_LIST:
outs[file_path] = [file_path]
for base_name in aten_ufunc_generated_all_cpu_sources():
file_path = "aten/src/ATen/{}".format(base_name)
outs[file_path] = [file_path]
return outs
def get_pt_preprocessor_flags():
# read_config is not allowed outside of function in Starlark
PT_PREPROCESSOR_FLAGS = [
"-D_THP_CORE",
"-DC10_MOBILE",
"-DUSE_SCALARS",
"-DNO_CUDNN_DESTROY_HANDLE",
"-DNO_EXPORT",
"-DBUILD_CAFFE2",
]
return PT_PREPROCESSOR_FLAGS
def is_arvr_mode():
return False
def get_build_from_deps_query():
build_from_query = native.read_config("pt", "build_from_deps_query", "1")
return bool(int(build_from_query))
def get_enable_lightweight_dispatch():
enable_lightweight_dispatch = native.read_config("pt", "enable_lightweight_dispatch", "0")
return bool(int(enable_lightweight_dispatch))
def get_static_dispatch_backend():
static_dispatch_backend = native.read_config("pt", "static_dispatch_backend", None)
if static_dispatch_backend == None:
return []
return static_dispatch_backend.split(";")
def get_aten_codegen_extra_params(backends):
if get_build_from_deps_query():
extra_params = {
"force_schema_registration": True,
}
static_backends = get_static_dispatch_backend()
if static_backends:
extra_params["static_dispatch_backend"] = static_backends
extra_params["enabled_backends"] = static_backends
else:
extra_params["enabled_backends"] = backends
return extra_params
else:
return {}
def gen_aten_files(
name,
extra_flags = {},
visibility = [],
compatible_with = []):
extra_params = []
force_schema_registration = extra_flags.get("force_schema_registration", False)
op_registration_allowlist = extra_flags.get("op_registration_allowlist", None)
op_selection_yaml_path = extra_flags.get("op_selection_yaml_path", None)
enabled_backends = extra_flags.get("enabled_backends", None)
static_dispatch_backend = extra_flags.get("static_dispatch_backend", None)
if force_schema_registration:
extra_params.append("--force_schema_registration")
if op_registration_allowlist != None and is_string(op_registration_allowlist):
extra_params.append("--op_registration_whitelist")
extra_params.append(op_registration_allowlist)
if op_selection_yaml_path != None and is_string(op_selection_yaml_path):
extra_params.append("--op_selection_yaml_path")
extra_params.append(op_selection_yaml_path)
if enabled_backends != None and is_list(enabled_backends):
extra_params.append("--backend_whitelist")
extra_params.extend(enabled_backends)
if get_enable_lightweight_dispatch():
extra_params.append("--skip_dispatcher_op_registration")
if static_dispatch_backend:
extra_params.append("--static_dispatch_backend")
extra_params.extend(static_dispatch_backend)
backends = static_dispatch_backend
else:
backends = enabled_backends
fb_xplat_genrule(
name = name,
default_outs = ["."],
outs = get_aten_generated_files(backends),
cmd = "$(exe //torchgen:gen) " + " ".join([
"--source-path $(location //:aten_src_path)/aten/src/ATen",
"--install_dir $OUT",
] + extra_params),
visibility = visibility,
compatible_with = compatible_with,
)
def get_aten_generated_files(enabled_backends):
# NB: RegisterMeta counts as an optionally enabled backend,
# and is intentionally omitted from here
src_files = [
"RegisterBackendSelect.cpp",
"RegisterCompositeImplicitAutograd.cpp",
"RegisterCompositeExplicitAutograd.cpp",
"CompositeViewCopyKernels.cpp",
"RegisterSchema.cpp",
"Declarations.yaml",
"Functions.cpp",
"Functions.h",
"RedispatchFunctions.h",
"NativeFunctions.h",
"NativeMetaFunctions.h",
"MethodOperators.h",
"FunctionalInverses.h",
"Operators.h",
"Operators_0.cpp",
"Operators_1.cpp",
"Operators_2.cpp",
"Operators_3.cpp",
"Operators_4.cpp",
"CompositeImplicitAutogradFunctions.h",
"CompositeImplicitAutogradFunctions_inl.h",
"CompositeExplicitAutogradFunctions.h",
"CompositeExplicitAutogradFunctions_inl.h",
"core/ATenOpList.cpp",
"core/TensorBody.h",
"core/TensorMethods.cpp",
"core/aten_interned_strings.h",
] + get_aten_derived_type_srcs(enabled_backends)
# This is tiresome. A better strategy would be to unconditionally
# generate these files, and then only actually COMPILE them depended
# on the generated set. C'est la vie...
if "CPU" in enabled_backends:
src_files.extend(aten_ufunc_generated_cpu_sources())
src_files.extend(aten_ufunc_generated_cpu_kernel_sources())
if "CUDA" in enabled_backends:
# Cannot unconditionally include this, because in the Edge selective
# build CUDA is not enabled and thus the ufunc codegen for CUDA gets
# skipped
src_files.extend(aten_ufunc_generated_cuda_sources())
res = {}
for file_name in src_files:
res[file_name] = [file_name]
return res
def get_template_registration_file_rules(rule_name):
rules = []
for file_path in TEMPLATE_SOURCE_LIST:
rules.append(":{}[{}]".format(rule_name, file_path))
for file_path in aten_ufunc_generated_all_cpu_sources():
rules.append(":{}[aten/src/ATen/{}]".format(rule_name, file_path))
return rules
# Originally, there were two sets of codes in caffe2:aten_cpu, native codes and non-native.
# Now we have only non-naitve sources in aten_cpu. However, there are some aten related
# tests that may require both native and non-native codes. This rule is used to generate
# both aten_cpu and aten_native_cpu. They are using the same compilation setups.
def build_aten_cpu(name, srcs, deps = []):
cxx_library(
name = name,
srcs = srcs,
header_namespace = "",
compiler_flags = get_pt_compiler_flags(),
exported_preprocessor_flags = get_aten_preprocessor_flags(),
link_whole = True,
linker_flags = ["-Wl,--no-as-needed", "-ldl"],
visibility = ["PUBLIC"],
deps = [
"//third_party:cpuinfo",
"//third_party:glog",
"//third_party:XNNPACK",
#"//third_party/linker_lib:omp",
],
exported_deps = [
"//third_party:fmt",
"//aten/src/ATen/native/quantized/cpu/qnnpack:pytorch_qnnpack",
"//c10:c10",
":aten_header",
":caffe2_headers",
":common_core",
":generated_aten_config_header",
":generated_aten_headers_cpu",
":jit_core_headers",
":pthreadpool",
":th_header",
"//third_party:ruy_lib",
],
)
######### selective build #########
def get_pt_ops_deps(name, deps, train = False, enforce_traced_op_list = False, enable_flatbuffer = False, **kwargs):
if not get_build_from_deps_query():
return deps
pt_operator_registry(
name,
deps,
train = train,
enforce_traced_op_list = enforce_traced_op_list,
enable_flatbuffer = enable_flatbuffer,
**kwargs
)
return deps + [":" + name]
# pt_operator_registry is the method that defines the fb_xplat_cxx_library that contains
# code for all selected PyTorch Operators and kernel functions. This also includes
# operator registration into the dispatcher.
#
# template_select: bool: Indicates if template based selective build is enabled.
#
# enforce_traced_op_list: bool: Enforces that only new-style operator
# lists based on the all_mobile_model_configs.yaml file and tracing based selective
# build are used in this library.
#
# train: bool: Build this library for training (True) or inference only (False).
# If built for training, codegen for VariableType is also included.
#
# pt_allow_forced_schema_registration: Manually disables forced schema registration when set to false, Default is true.
# Only does anything when train=True and the app requires full jit then force_schema_registration needs to occur.
# As Federated Learning migrates to lite interpreter
# we can slowly turn off forced schema registration as it is useless space and floods the compatibility api
#
def pt_operator_registry(
name,
deps = [],
train = False,
labels = [],
env = [],
template_select = True,
enforce_traced_op_list = False,
pt_allow_forced_schema_registration = True,
enable_flatbuffer = False,
**kwargs):
compatible_with = kwargs.get("compatible_with", [])
code_gen_files = pt_operator_query_codegen(name, deps = deps, train = train, enforce_traced_op_list = enforce_traced_op_list, pt_allow_forced_schema_registration = pt_allow_forced_schema_registration, compatible_with = compatible_with)
code_gen_srcs = code_gen_files["srcs"]
lib_deps = [
":aten_cpu",
":torch_mobile_core",
"//c10:c10",
"//third_party:glog",
]
#if train:
# lib_deps = lib_deps + ["fbsource//xplat/caffe2:torch_mobile_train"]
exported_preprocessor_flags = get_aten_preprocessor_flags()
exported_preprocessor_flags += kwargs.pop("exported_preprocessor_flags", [])
if template_select:
# In addition to the
# original code-gen select, this option further filter more operators based on
# compile-time calculation. Examples include prim ops and any other ops that were
# not filtered out before. The purpose of this option is to reduce the production
# size further. However, it may have less flexibility, especially for tests from
# python, where the used operator list is not explicitly generated. If the tests
# are for functionality but not for size, and it's difficult to maintain an explicit
# operator list, it's suggested to turn this option off.
exported_preprocessor_flags.append("-DTEMPLATE_SELECTIVE_BUILD")
kwargs.pop("exported_headers", [])
cxx_library(
name = name,
srcs = code_gen_srcs,
linker_flags = [
"-Wl,--no-as-needed",
"-ldl",
],
link_whole = True,
soname = "libtorch-code-gen.$(ext)",
compiler_flags = get_aten_compiler_flags(),
platform_compiler_flags = get_cpukernel_avx2_flags(),
platform_deps = get_cpukernel_avx2_deps(),
header_namespace = "ATen",
exported_headers = code_gen_files["headers"],
exported_preprocessor_flags = exported_preprocessor_flags,
headers = kwargs.pop("headers", []),
deps = lib_deps + [
"//third_party:XNNPACK",
],
**kwargs
)
def get_aten_derived_type_src_rules(aten_rule_name, enabled_backends):
return [
":{}[{}]".format(aten_rule_name, "Register" + backend + ".cpp")
for backend in enabled_backends
]
def get_aten_selective_cpp_rules(aten_rule_name, enabled_backends):
return [
":{}[{}]".format(aten_rule_name, f)
for f in ["RegisterCompositeImplicitAutograd.cpp", "RegisterCompositeExplicitAutograd.cpp", "RegisterSchema.cpp", "RegisterBackendSelect.cpp", "CompositeViewCopyKernels.cpp"]
] + get_aten_derived_type_src_rules(aten_rule_name, enabled_backends)
def get_aten_derived_type_srcs(enabled_backends):
return [
"Register" + derived_type + ".cpp"
for derived_type in enabled_backends
] + [
derived_type + "Functions.h"
for derived_type in enabled_backends
if derived_type in PT_BACKEND_HEADERS or derived_type in get_static_dispatch_backend()
] + [
derived_type + "Functions_inl.h"
for derived_type in enabled_backends
if derived_type in PT_BACKEND_HEADERS or derived_type in get_static_dispatch_backend()
]
def pt_operator_query_codegen(name, deps = [], train = False, enforce_traced_op_list = False, pt_allow_forced_schema_registration = True, compatible_with = []):
oplist_dir_name = name + "_pt_oplist"
# @lint-ignore BUCKLINT
fb_xplat_genrule(
name = oplist_dir_name,
cmd = ("$(exe //:gen_oplist) " +
"--model_file_list_path $(@query_outputs 'attrfilter(labels, pt_operator_library, deps(set({deps})))') " +
("" if enforce_traced_op_list else "--allow_include_all_overloads ") +
"--output_dir $OUT ").format(deps = " ".join(["\"{}\"".format(d) for d in deps])),
outs = get_gen_oplist_outs(),
default_outs = ["."],
compatible_with = compatible_with,
)
# Aten files
aten_genrule = name + "_aten"
extra_flags = {
"enabled_backends": USED_PT_BACKENDS,
"op_selection_yaml_path": "$(location :{}[selected_operators.yaml])".format(oplist_dir_name),
}
if train and pt_allow_forced_schema_registration:
extra_flags["force_schema_registration"] = True
# if get_enable_lightweight_dispatch():
# unboxing_genrule = name + "_unboxing"
# gen_aten_unboxing_files(
# unboxing_genrule,
# extra_flags = extra_flags,
# )
static_dispatch_backend = get_static_dispatch_backend()
if static_dispatch_backend:
extra_flags["static_dispatch_backend"] = static_dispatch_backend
gen_aten_files(
aten_genrule,
extra_flags = extra_flags,
compatible_with = compatible_with,
)
# unboxing_wrappers files
extra_params = [
"--operators_yaml_path",
"$(location :" + oplist_dir_name + "[selected_operators.yaml])",
]
unboxing_and_autograd_genrule = name + "_unboxing_and_autograd"
gen_aten_libtorch_files(unboxing_and_autograd_genrule, extra_params, compatible_with)
# Template runtime files (prim ops, etc)
template_registration_genrule = name + "_template_registration"
copy_template_registration_files(template_registration_genrule)
srcs = get_aten_selective_cpp_rules(
aten_genrule,
static_dispatch_backend if static_dispatch_backend else USED_PT_BACKENDS,
) + get_template_registration_file_rules(
template_registration_genrule,
) + ([
":{}[autograd/generated/VariableType_0.cpp]".format(unboxing_and_autograd_genrule),
":{}[autograd/generated/VariableType_1.cpp]".format(unboxing_and_autograd_genrule),
":{}[autograd/generated/VariableType_2.cpp]".format(unboxing_and_autograd_genrule),
":{}[autograd/generated/VariableType_3.cpp]".format(unboxing_and_autograd_genrule),
":{}[autograd/generated/VariableType_4.cpp]".format(unboxing_and_autograd_genrule),
":{}[autograd/generated/ADInplaceOrViewType_0.cpp]".format(unboxing_and_autograd_genrule),
":{}[autograd/generated/ADInplaceOrViewType_1.cpp]".format(unboxing_and_autograd_genrule),
] if train else []) + ([
#":{}[SupportedMobileModelsRegistration.cpp]".format(oplist_dir_name),
])
headers = {
"selected_mobile_ops.h": ":{}[selected_mobile_ops.h]".format(oplist_dir_name),
}
# if get_enable_lightweight_dispatch():
# srcs.extend([
# ":{}[UnboxingFunctions_0.cpp]".format(unboxing_genrule),
# ":{}[UnboxingFunctions_1.cpp]".format(unboxing_genrule),
# ":{}[UnboxingFunctions_2.cpp]".format(unboxing_genrule),
# ":{}[UnboxingFunctions_3.cpp]".format(unboxing_genrule),
# ":{}[UnboxingFunctions_4.cpp]".format(unboxing_genrule),
# ":{}[RegisterCodegenUnboxedKernels_0.cpp]".format(unboxing_genrule),
# ":{}[RegisterCodegenUnboxedKernels_1.cpp]".format(unboxing_genrule),
# ":{}[RegisterCodegenUnboxedKernels_2.cpp]".format(unboxing_genrule),
# ":{}[RegisterCodegenUnboxedKernels_3.cpp]".format(unboxing_genrule),
# ":{}[RegisterCodegenUnboxedKernels_4.cpp]".format(unboxing_genrule),
# ":{}[RegisterCodegenUnboxedKernels_5.cpp]".format(unboxing_genrule),
# ":{}[RegisterCodegenUnboxedKernels_6.cpp]".format(unboxing_genrule),
# ":{}[RegisterCodegenUnboxedKernels_7.cpp]".format(unboxing_genrule),
# ":{}[RegisterCodegenUnboxedKernels_8.cpp]".format(unboxing_genrule),
# ":{}[RegisterCodegenUnboxedKernels_9.cpp]".format(unboxing_genrule),
# ])
# headers["UnboxingFunctions.h"] = ":{}[UnboxingFunctions.h]".format(unboxing_genrule)
return {"headers": headers, "srcs": srcs}
def gen_aten_libtorch_files(name, extra_params = [], compatible_with = []):
fb_xplat_genrule(
name = name,
outs = get_generate_code_bin_outs(),
default_outs = ["."],
cmd = "mkdir -p tools && " +
"$(exe //tools/setup_helpers:generate_code_bin) " + " ".join(
# Mobile build only needs libtorch - skip python bindings for now, except
# for ovrsource, which needs Python bindings.
(["--subset libtorch"] if not is_arvr_mode() else []) + [
"--native-functions-path $(location :aten_src_path)/aten/src/ATen/native/native_functions.yaml",
"--tags-path $(location :aten_src_path)/aten/src/ATen/native/tags.yaml", # todo D35992309
"--install_dir $OUT",
] + extra_params,
),
cmd_exe = "@powershell -Command New-Item -Path tools -ItemType Directory -Force; " +
"$(exe //tools/setup_helpers:generate_code_bin) " + " ".join(
# Mobile build only needs libtorch - skip python bindings for now, except
# for ovrsource, which needs Python bindings.
(["--subset libtorch"] if not is_arvr_mode() else []) + [
"--native-functions-path $(location :aten_src_path)/aten/src/ATen/native/native_functions.yaml",
"--tags-path $(location :aten_src_path)/aten/src/ATen/native/tags.yaml",
"--install_dir $OUT",
] + extra_params,
),
compatible_with = compatible_with,
)
def copy_template_registration_files(name):
cmd = []
cmd_exe = []
template_source_dict = get_template_source_dict()
# Ideally, we would run one copy command for a single source directory along
# with all its child directories, but it's somewhat hard to know if a directory
# is a child of another just bu looking at the metadata (directory relative
# path) that we currently have since 1 directory could look like a parent of
# another and yet come from a different filegroup() rule.
#
for (path_prefix, file_paths) in template_source_dict.items():
cmd.append("mkdir -p $OUT/{}".format(path_prefix))
cmd_exe.append("md $OUT/{}".format(path_prefix))
# Adding *.cpp is a workaround to prevent cp from thrown an error when it
# encounters a directory (since -r was not specified). If files with an
# extension other than .cpp need to be copied, then the command below
# will not work and will need to be updated.
#
cmd.append("cp -f {0}/{1}/*.cpp $OUT/{1}/".format("$(location :templated_selective_build_srcs)", path_prefix))
cmd_exe.append("robocopy /E {0}/{1} $OUT/{1}".format("$(location :templated_selective_build_srcs)", path_prefix))
cmd.append("mkdir -p $OUT/aten/src/ATen")
cmd_exe.append("md $OUT/aten/src/ATen")
# NB: CUDA is skipped here because this is selective build and CUDA is not
# supported for selective build
for ufunc_file in aten_ufunc_generated_all_cpu_sources("$(location :gen_aten[{}])"):
cmd.append("cp -f " + ufunc_file + " $OUT/aten/src/ATen")
cmd_exe.append("copy " + ufunc_file + " $OUT/aten/src/ATen")
fb_xplat_genrule(
name = name,
cmd = " && ".join(cmd),
cmd_exe = "@powershell -Command " + ("; ".join(cmd_exe)),
outs = get_template_registration_files_outs(),
default_outs = ["."],
)
def pt_operator_library(
name,
ops = [],
exported_deps = [],
check_decl = True,
train = False,
model = None,
include_all_operators = False,
**kwargs):
model_name = name
if get_build_from_deps_query():
ops = [op.strip() for op in ops]
# If ops are specified, then we are in static selective build mode, so we append
# base ops to this list to avoid additional special case logic in subsequent code.
if len(ops) > 0:
ops.extend(PT_BASE_OPS)
visibility = kwargs.pop("visibility", ["PUBLIC"])
fb_xplat_genrule(
name = name,
out = "model_operators.yaml",
cmd = (
"$(exe :gen_operators_yaml) " +
"{optionally_root_ops} " +
"{optionally_training_root_ops} " +
"--rule_name {rule_name} " +
"--output_path \"${{OUT}}\" " +
"--model_name {model_name} " +
"--dep_graph_yaml_path pytorch_op_deps.yaml " +
"--models_yaml_path all_mobile_model_configs.yaml " +
#"{optionally_model_versions} " +
#"{optionally_model_assets} " +
#"{optionally_model_traced_backends} " +
"{optionally_include_all_operators}"
).format(
rule_name = name,
model_name = model_name,
optionally_root_ops = "--root_ops " + (",".join(ops)) if len(ops) > 0 else "",
optionally_training_root_ops = "--training_root_ops " + (",".join(ops)) if len(ops) > 0 and train else "",
#optionally_model_versions = "--model_versions " + (",".join(model_versions)) if model_versions != None else "",
#optionally_model_assets = "--model_assets " + (",".join(model_assets)) if model_assets != None else "",
#optionally_model_traced_backends = "--model_traced_backends " + (",".join(model_traced_backends)) if model_traced_backends != None else "",
optionally_include_all_operators = "--include_all_operators " if include_all_operators else "",
),
labels = ["pt_operator_library"], # for pt_operator_query_codegen query
visibility = visibility,
**kwargs
)
else:
if check_decl:
pass
# ensure_ops_are_declared(ops)
cxx_library(
name = name,
compiler_flags = get_pt_compiler_flags(),
cxx_platform_compiler_flags = get_cpukernel_avx2_flags(),
exported_deps = exported_deps,
**kwargs
)
def compose_platform_setting_list(settings):
"""Settings object:
os/cpu pair: should be valid key, or at most one part can be wildcard.
flags: the values added to the compiler flags
"""
result = []
for setting in settings:
result = result.append([
"^{}-{}$".format(setting["os"], setting["cpu"]),
setting["flags"],
])
return result
def get_cpukernel_avx2_flags():
# flags = compose_platform_setting_list([
# {
# "cpu": "x86_64",
# "flags": ["-DHAVE_AVX2_CPU_DEFINITION"],
# "os": "macosx",
# },
# ]) if build_cpukernel_avx2() else []
return []
def build_cpukernel_avx2():
return not is_arvr_mode()
def get_cpukernel_avx2_deps():
# flags = compose_platform_setting_list([
# {
# "cpu": "x86_64",
# "flags": ["fbsource//xplat/caffe2:cpukernel_avx2"],
# "os": "macosx",
# },
# ]) if build_cpukernel_avx2() else []
return []

29
scripts/buck_setup.sh Normal file
View File

@ -0,0 +1,29 @@
#!/bin/bash
printf "\n[Creating .buckconfig]\n"
cp .buckconfig.oss .buckconfig
cd third_party || return
printf "\n[Generating wrappers for cpuionfo]\n"
python3 generate-cpuinfo-wrappers.py
printf "\n[Generating wrappers for xnnpack]\n"
python3 generate-xnnpack-wrappers.py
# bazel-skylib
printf "\n[Downloading bazel-skylib-1.0.2]\n"
curl -L -o /tmp/bazel-skylib-1.0.2.tar.gz https://github.com/bazelbuild/bazel-skylib/releases/download/1.0.2/bazel-skylib-1.0.2.tar.gz
mkdir bazel-skylib
tar -xf /tmp/bazel-skylib-1.0.2.tar.gz -C bazel-skylib/
# glog
printf "\n[Downloading glog-0.4.0]\n"
curl -L -o /tmp/glog-0.4.0.tar.gz https://github.com/google/glog/archive/v0.4.0.tar.gz
tar -xf /tmp/glog-0.4.0.tar.gz -C /tmp/
mv /tmp/glog-0.4.0/ glog/
# ruy
printf "\n[Downloading ruy]\n"
curl -L -o /tmp/ruy.zip https://github.com/google/ruy/archive/a09683b8da7164b9c5704f88aef2dc65aa583e5d.zip
unzip -q /tmp/ruy.zip -d /tmp/
mv /tmp/ruy-a09683b8da7164b9c5704f88aef2dc65aa583e5d ruy/

209
third_party/BUILD.buck vendored
View File

@ -1,3 +1,10 @@
load("//third_party:glog.buck.bzl", "define_glog")
load("//third_party:xnnpack.buck.bzl", "define_xnnpack")
define_glog()
define_xnnpack()
cxx_library(
name = "fmt",
srcs = ['fmt/src/format.cc'],
@ -6,9 +13,209 @@ cxx_library(
preferred_linkage = "static",
exported_preprocessor_flags = ['-DFMT_EXCEPTIONS=0'],
header_namespace = "third_party/fmt",
reexport_all_header_dependencies = True,
public_system_include_directories = ['fmt/include'],
raw_headers = glob(["fmt/include/fmt/*.h"]),
soname = "libthird-party_fmt_fmt.$(ext)",
visibility = ['PUBLIC'],
)
cxx_library(
name = "pthreadpool",
srcs = ['pthreadpool/src/legacy-api.c', 'pthreadpool/src/memory.c', 'pthreadpool/src/portable-api.c', 'pthreadpool/src/pthreads.c'],
deps = [
":FXdiv",
":pthreadpool_header",
],
compiler_flags = [
"-w",
"-Os",
"-fstack-protector-strong",
"-fno-delete-null-pointer-checks"
],
headers = {
'threadpool-atomics.h': 'pthreadpool/src/threadpool-atomics.h',
'threadpool-common.h': 'pthreadpool/src/threadpool-common.h',
'threadpool-object.h': 'pthreadpool/src/threadpool-object.h',
'threadpool-utils.h': 'pthreadpool/src/threadpool-utils.h',
},
header_namespace = "",
preferred_linkage = "static",
link_whole = False,
platform_preprocessor_flags = [['windows', ['-D_WINDOWS', '-D_WIN32', '-DWIN32', '-DNOMINMAX', '-D_CRT_SECURE_NO_WARNINGS', '-D_USE_MATH_DEFINES']], ['windows.*64$', ['-D_WIN64']]],
preprocessor_flags = ['-DPTHREADPOOL_USE_FUTEX=0', '-DPTHREADPOOL_USE_GCD=0'],
reexport_all_header_dependencies = True,
visibility = ['PUBLIC'],
)
cxx_library(
name = "pthreadpool_header",
header_namespace = "",
exported_headers = {
"pthreadpool.h": "pthreadpool/include/pthreadpool.h",
},
reexport_all_header_dependencies = True,
visibility = ["PUBLIC"],
)
cxx_library(
name = "FXdiv",
header_namespace = "",
exported_headers = {
"fxdiv.h": "FXdiv/include/fxdiv.h",
},
reexport_all_header_dependencies = True,
visibility = ["PUBLIC"],
)
cxx_library(
name = "psimd",
header_namespace = "",
exported_headers = {
"psimd.h": "psimd/include/psimd.h",
},
preferred_linkage = "static",
visibility = ["PUBLIC"],
)
cxx_library(
name = "cpuinfo",
srcs = [
"cpuinfo/wrappers/api.c",
"cpuinfo/wrappers/arm/android/properties.c",
"cpuinfo/wrappers/arm/cache.c",
"cpuinfo/wrappers/arm/linux/aarch32-isa.c",
"cpuinfo/wrappers/arm/linux/aarch64-isa.c",
"cpuinfo/wrappers/arm/linux/chipset.c",
"cpuinfo/wrappers/arm/linux/clusters.c",
"cpuinfo/wrappers/arm/linux/cpuinfo.c",
"cpuinfo/wrappers/arm/linux/hwcap.c",
"cpuinfo/wrappers/arm/linux/init.c",
"cpuinfo/wrappers/arm/linux/midr.c",
"cpuinfo/wrappers/arm/mach/init.c",
"cpuinfo/wrappers/arm/uarch.c",
"cpuinfo/wrappers/cache.c",
"cpuinfo/wrappers/init.c",
"cpuinfo/wrappers/linux/cpulist.c",
"cpuinfo/wrappers/linux/multiline.c",
"cpuinfo/wrappers/linux/processors.c",
"cpuinfo/wrappers/linux/smallfile.c",
"cpuinfo/wrappers/mach/topology.c",
"cpuinfo/wrappers/x86/cache/descriptor.c",
"cpuinfo/wrappers/x86/cache/deterministic.c",
"cpuinfo/wrappers/x86/cache/init.c",
"cpuinfo/wrappers/x86/info.c",
"cpuinfo/wrappers/x86/init.c",
"cpuinfo/wrappers/x86/isa.c",
"cpuinfo/wrappers/x86/linux/cpuinfo.c",
"cpuinfo/wrappers/x86/linux/init.c",
"cpuinfo/wrappers/x86/mach/init.c",
"cpuinfo/wrappers/x86/name.c",
"cpuinfo/wrappers/x86/topology.c",
"cpuinfo/wrappers/x86/uarch.c",
"cpuinfo/wrappers/x86/vendor.c",
"cpuinfo/wrappers/x86/windows/init.c",
],
include_directories = ["cpuinfo/src"],
public_include_directories = ["cpuinfo/include"],
raw_headers = glob([
"cpuinfo/src/**/*.h",
"cpuinfo/src/**/*.c",
]),
preferred_linkage = "static",
preprocessor_flags = [
"-DCPUINFO_LOG_LEVEL=2",
"-D_GNU_SOURCE=1",
],
visibility = ["PUBLIC"],
deps = [
":clog",
],
)
cxx_library(
name = "clog",
srcs = [
"cpuinfo/deps/clog/src/clog.c",
],
raw_headers = glob([
"cpuinfo/deps/clog/include/*.h",
]),
public_include_directories = [
"cpuinfo/deps/clog/include/",
],
force_static = True,
visibility = ["PUBLIC"],
)
cxx_library(
name = "FP16",
raw_headers = glob([
"FP16/include/*.h",
]),
public_include_directories = [
"FP16/include/",
],
force_static = True,
visibility = ["PUBLIC"],
)
cxx_library(
name = "miniz",
srcs = ["miniz-2.0.8/miniz.c"],
header_namespace = "",
exported_headers = {"miniz.h": "miniz-2.0.8/miniz.h"},
exported_preprocessor_flags = [
"-DMINIZ_DISABLE_ZIP_READER_CRC32_CHECKS",
],
visibility = ["PUBLIC"],
)
remote_file(
name = "typing-extensions-download",
url = "https://files.pythonhosted.org/packages/75/e1/932e06004039dd670c9d5e1df0cd606bf46e29a28e65d5bb28e894ea29c9/typing_extensions-4.2.0-py3-none-any.whl",
sha1 = "ff0849420e94f425818bff5d0f25e3cdfaba8601",
out = "typing_extensions-4.2.0-py3-none-any.whl",
)
prebuilt_python_library(
name = "typing-extensions",
binary_src = ":typing-extensions-download",
visibility = ["PUBLIC"],
deps = [":typing-extensions-download"],
)
remote_file(
name = "pyyaml-download",
url = "https://files.pythonhosted.org/packages/12/fc/a4d5a7554e0067677823f7265cb3ae22aed8a238560b5133b58cda252dad/PyYAML-6.0-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl",
sha1 = "11aa9c5fe2d890b6a73212beadc7c8a4265ebc39",
out = "pyyaml.whl",
)
prebuilt_python_library(
name = "pyyaml",
binary_src = ":pyyaml-download",
visibility = ["PUBLIC"],
deps = [":pyyaml-download"],
)
cxx_library(
name = "ruy_lib",
srcs = glob(
["ruy/**/*.cc"],
exclude = [
"ruy/ruy/test_*.cc",
"ruy/ruy/*_test.cc",
"ruy/example/*.cc",
"ruy/ruy/profiler/test.cc",
"ruy/ruy/benchmark.cc",
],
),
compiler_flags = ["-Os"],
preferred_linkage = "static",
public_include_directories = ["ruy"],
raw_headers = glob(["ruy/**/*.h"]),
visibility = [
"PUBLIC",
],
)

View File

@ -0,0 +1,93 @@
#!/usr/bin/env python3
from __future__ import print_function
import os
CPUINFO_SOURCES = {
None: [
"init.c",
"api.c",
"cache.c",
],
"defined(__linux__)": [
"linux/multiline.c",
"linux/cpulist.c",
"linux/mockfile.c",
"linux/smallfile.c",
"linux/processors.c",
],
"defined(__MACH__) && defined(__APPLE__)": [
"mach/topology.c",
],
"defined(__i386__) || defined(__i686__) || defined(__x86_64__) || defined(_WIN32)": [
"x86/cache/init.c",
"x86/cache/deterministic.c",
"x86/cache/descriptor.c",
"x86/info.c",
"x86/mockcpuid.c",
"x86/isa.c",
"x86/topology.c",
"x86/name.c",
"x86/init.c",
"x86/uarch.c",
"x86/vendor.c",
],
"(defined(__i386__) || defined(__i686__) || defined(__x86_64__)) && defined(__linux__)": [
"x86/linux/init.c",
"x86/linux/cpuinfo.c",
],
"(defined(__i386__) || defined(__i686__) || defined(__x86_64__)) && defined(__MACH__) && defined(__APPLE__)": [
"x86/mach/init.c",
],
"defined(_WIN32)": [
"x86/windows/init.c",
],
"(defined(__arm__) || defined(__aarch64__)) && defined(__linux__)": [
"arm/linux/cpuinfo.c",
"arm/linux/hwcap.c",
"arm/linux/init.c",
"arm/linux/clusters.c",
"arm/linux/midr.c",
"arm/linux/chipset.c",
"arm/tlb.c",
"arm/uarch.c",
"arm/cache.c",
],
"defined(__arm__) && defined(__linux__)": [
"arm/linux/aarch32-isa.c",
],
"defined(__aarch64__) && defined(__linux__)": [
"arm/linux/aarch64-isa.c",
],
"(defined(__arm__) || defined(__aarch64__)) && defined(__ANDROID__)": [
"arm/android/properties.c",
],
"(defined(__arm__) || defined(__aarch64__)) && defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE": [
"arm/mach/init.c",
],
}
if __name__ == "__main__":
for condition, filenames in CPUINFO_SOURCES.items():
for filename in filenames:
filepath = os.path.join("cpuinfo/wrappers", filename)
if not os.path.exists(os.path.dirname(filepath)):
print(filepath)
os.makedirs(os.path.dirname(filepath))
with open(filepath, "w") as wrapper:
print("/* Auto-generated by generate-wrappers.py script. Do not modify */", file=wrapper)
print(file=wrapper)
print("#ifdef __APPLE__", file=wrapper)
print("\t#include <TargetConditionals.h>", file=wrapper)
print("#endif /* __APPLE__ */", file=wrapper)
print(file=wrapper)
if not condition:
print("#include <%s>" % filename, file=wrapper)
else:
# Include source file only if condition is satisfied
print("#if %s" % condition, file=wrapper)
print("#include <%s>" % filename, file=wrapper)
print("#endif /* %s */" % condition, file=wrapper)

104
third_party/generate-xnnpack-wrappers.py vendored Normal file
View File

@ -0,0 +1,104 @@
#!/usr/bin/env python3
from __future__ import print_function
import collections
import os
BANNER = "Auto-generated by generate-wrappers.py script. Do not modify"
WRAPPER_SRC_NAMES = {
"PROD_SCALAR_PORTABLE_MICROKERNEL_SRCS": None,
"PROD_SCALAR_AARCH32_MICROKERNEL_SRCS" : "defined(__arm__)",
"PROD_NEON_MICROKERNEL_SRCS": "defined(__arm__) || defined(__aarch64__)",
"PROD_NEONFP16_MICROKERNEL_SRCS": "defined(__arm__) || defined(__aarch64__)",
"PROD_NEONFMA_MICROKERNEL_SRCS": "defined(__arm__) || defined(__aarch64__)",
"PROD_AARCH64_NEON_MICROKERNEL_SRCS": "defined(__aarch64__)",
"PROD_NEONV8_MICROKERNEL_SRCS": "defined(__arm__) || defined(__aarch64__)",
"PROD_AARCH64_NEONFP16ARITH_MICROKERNEL_SRCS": "defined(__aarch64__)",
"PROD_NEONDOT_MICROKERNEL_SRCS": "defined(__arm__) || defined(__aarch64__)",
"PROD_SSE_MICROKERNEL_SRCS": "defined(__i386__) || defined(__i686__) || defined(__x86_64__)",
"PROD_SSE2_MICROKERNEL_SRCS": "defined(__i386__) || defined(__i686__) || defined(__x86_64__)",
"PROD_SSSE3_MICROKERNEL_SRCS": "defined(__i386__) || defined(__i686__) || defined(__x86_64__)",
"PROD_SSE41_MICROKERNEL_SRCS": "defined(__i386__) || defined(__i686__) || defined(__x86_64__)",
"PROD_AVX_MICROKERNEL_SRCS": "defined(__i386__) || defined(__i686__) || defined(__x86_64__)",
"PROD_F16C_MICROKERNEL_SRCS": "defined(__i386__) || defined(__i686__) || defined(__x86_64__)",
"PROD_XOP_MICROKERNEL_SRCS": "defined(__i386__) || defined(__i686__) || defined(__x86_64__)",
"PROD_FMA3_MICROKERNEL_SRCS": "defined(__i386__) || defined(__i686__) || defined(__x86_64__)",
"PROD_AVX2_MICROKERNEL_SRCS": "defined(__i386__) || defined(__i686__) || defined(__x86_64__)",
"PROD_AVX512F_MICROKERNEL_SRCS": "defined(__i386__) || defined(__i686__) || defined(__x86_64__)",
"PROD_AVX512SKX_MICROKERNEL_SRCS": "defined(__i386__) || defined(__i686__) || defined(__x86_64__)",
"AARCH32_ASM_MICROKERNEL_SRCS": "defined(__arm__)",
"AARCH64_ASM_MICROKERNEL_SRCS": "defined(__aarch64__)",
}
SRC_NAMES = [
"OPERATOR_SRCS",
"SUBGRAPH_SRCS",
"LOGGING_SRCS",
"HOT_SRCS",
"TABLE_SRCS",
"JIT_SRCS",
"JIT_AARCH32_SRCS",
"JIT_AARCH64_SRCS",
"PROD_SCALAR_PORTABLE_MICROKERNEL_SRCS",
"PROD_SSE_MICROKERNEL_SRCS",
"PROD_SSE2_MICROKERNEL_SRCS",
"PROD_SSSE3_MICROKERNEL_SRCS",
"PROD_SSE41_MICROKERNEL_SRCS",
"PROD_AVX_MICROKERNEL_SRCS",
"PROD_F16C_MICROKERNEL_SRCS",
"PROD_XOP_MICROKERNEL_SRCS",
"PROD_FMA3_MICROKERNEL_SRCS",
"PROD_AVX2_MICROKERNEL_SRCS",
"PROD_AVX512F_MICROKERNEL_SRCS",
"PROD_AVX512SKX_MICROKERNEL_SRCS",
]
def update_sources():
sources = collections.defaultdict(list)
with open("./XNNPACK/CMakeLists.txt") as cmake:
lines = cmake.readlines()
i = 0
while i < len(lines):
line = lines[i]
if line.startswith("SET") and line.split('(')[1].strip(' \t\n\r') in set(WRAPPER_SRC_NAMES.keys()) | set(SRC_NAMES):
name = line.split('(')[1].strip(' \t\n\r')
i += 1
while i < len(lines) and len(lines[i]) > 0 and ')' not in lines[i]:
# remove "src/" at the beginning, remove whitespaces and newline
value = lines[i].strip(' \t\n\r')
sources[name].append(value[4:])
i += 1
if i < len(lines) and len(lines[i]) > 4:
# remove "src/" at the beginning, possibly ')' at the end
value = lines[i].strip(' \t\n\r)')
sources[name].append(value[4:])
else:
i += 1
print(sources)
return sources
if __name__ == "__main__":
xnnpack_sources = collections.defaultdict(list)
sources = update_sources()
for name in WRAPPER_SRC_NAMES:
xnnpack_sources[WRAPPER_SRC_NAMES[name]].extend(sources[name])
for condition, filenames in xnnpack_sources.items():
for filename in filenames:
filepath = os.path.join("XNNPACK/wrappers", filename)
if not os.path.isdir(os.path.dirname(filepath)):
os.makedirs(os.path.dirname(filepath))
with open(filepath, "w") as wrapper:
print("/* {} */".format(BANNER), file=wrapper)
print(file=wrapper)
# Architecture- or platform-dependent preprocessor flags can be
# defined here. Note: platform_preprocessor_flags can't be used
# because they are ignored by arc focus & buck project.
if condition is None:
print("#include <%s>" % filename, file=wrapper)
else:
# Include source file only if condition is satisfied
print("#if %s" % condition, file=wrapper)
print("#include <%s>" % filename, file=wrapper)
print("#endif /* %s */" % condition, file=wrapper)

97
third_party/glog.buck.bzl vendored Normal file
View File

@ -0,0 +1,97 @@
GLOG_CONFIG_HEADERS = [
"vlog_is_on.h",
"stl_logging.h",
"raw_logging.h",
"logging.h",
]
GLOG_SED_COMMAND = " ".join([
"sed",
"-e 's/@ac_cv_cxx_using_operator@/1/g'",
"-e 's/@ac_cv_have_unistd_h@/1/g'",
"-e 's/@ac_cv_have_stdint_h@/1/g'",
"-e 's/@ac_cv_have_systypes_h@/1/g'",
"-e 's/@ac_cv_have_libgflags@/0/g'",
"-e 's/@ac_cv_have_uint16_t@/1/g'",
"-e 's/@ac_cv_have___builtin_expect@/1/g'",
"-e 's/@ac_cv_have_.*@/0/g'",
"-e 's/@ac_google_start_namespace@/namespace google {/g'",
"-e 's/@ac_google_end_namespace@/}/g'",
"-e 's/@ac_google_namespace@/google/g'",
"-e 's/@ac_cv___attribute___noinline@/__attribute__((noinline))/g'",
"-e 's/@ac_cv___attribute___noreturn@/__attribute__((noreturn))/g'",
"-e 's/@ac_cv___attribute___printf_4_5@/__attribute__((__format__ (__printf__, 4, 5)))/g'",
])
def define_glog():
cxx_library(
name = "glog",
srcs = [
"glog/src/demangle.cc",
"glog/src/vlog_is_on.cc",
"glog/src/symbolize.cc",
"glog/src/raw_logging.cc",
"glog/src/logging.cc",
"glog/src/signalhandler.cc",
"glog/src/utilities.cc",
],
exported_headers = [":glog_{}".format(header) for header in GLOG_CONFIG_HEADERS],
header_namespace = "glog",
compiler_flags = [
"-Wno-sign-compare",
"-Wno-unused-function",
"-Wno-unused-local-typedefs",
"-Wno-unused-variable",
"-Wno-deprecated-declarations",
],
preferred_linkage = "static",
exported_linker_flags = [],
exported_preprocessor_flags = [
"-DGLOG_NO_ABBREVIATED_SEVERITIES",
"-DGLOG_STL_LOGGING_FOR_UNORDERED",
"-DGOOGLE_GLOG_DLL_DECL=",
"-DGOOGLE_NAMESPACE=google",
# this is required for buck build
"-DGLOG_BAZEL_BUILD",
"-DHAVE_PTHREAD",
# Allows src/logging.cc to determine the host name.
"-DHAVE_SYS_UTSNAME_H",
# For src/utilities.cc.
"-DHAVE_SYS_SYSCALL_H",
"-DHAVE_SYS_TIME_H",
"-DHAVE_STDINT_H",
"-DHAVE_STRING_H",
# Enable dumping stacktrace upon sigaction.
"-DHAVE_SIGACTION",
# For logging.cc.
"-DHAVE_PREAD",
"-DHAVE___ATTRIBUTE__",
],
deps = [":glog_config"],
soname = "libglog.$(ext)",
visibility = ["PUBLIC"],
)
cxx_library(
name = "glog_config",
header_namespace = "",
exported_headers = {
"config.h": ":glog_config.h",
"glog/log_severity.h": "glog/src/glog/log_severity.h",
},
)
genrule(
name = "glog_config.h",
srcs = ["glog/src/config.h.cmake.in"],
out = "config.h",
cmd = "awk '{ gsub(/^#cmakedefine/, \"//cmakedefine\"); print; }' $SRCS > $OUT",
)
for header in GLOG_CONFIG_HEADERS:
genrule(
name = "glog_{}".format(header),
out = header,
srcs = ["glog/src/glog/{}.in".format(header)],
cmd = "{} $SRCS > $OUT".format(GLOG_SED_COMMAND),
)

586
third_party/xnnpack.buck.bzl vendored Normal file

File diff suppressed because one or more lines are too long

34
tools/autograd/BUILD.buck Normal file
View File

@ -0,0 +1,34 @@
python_library(
name = "autograd",
srcs = glob(
["*.py"],
),
base_module = "tools.autograd",
resources = [
"deprecated.yaml",
"derivatives.yaml",
"templates/ADInplaceOrViewType.cpp",
"templates/Functions.cpp",
"templates/Functions.h",
"templates/TraceType.cpp",
"templates/VariableType.cpp",
"templates/VariableType.h",
"templates/annotated_fn_args.py.in",
"templates/python_fft_functions.cpp",
"templates/python_functions.cpp",
"templates/python_functions.h",
"templates/python_linalg_functions.cpp",
"templates/python_nn_functions.cpp",
"templates/python_return_types.cpp",
"templates/python_sparse_functions.cpp",
"templates/python_special_functions.cpp",
"templates/python_torch_functions.cpp",
"templates/python_variable_methods.cpp",
"templates/variable_factories.h",
],
visibility = ["PUBLIC"],
deps = [
"//third_party:pyyaml",
"//torchgen:torchgen",
],
)

View File

@ -0,0 +1,5 @@
def fb_xplat_genrule(default_outs = ["."], **kwargs):
genrule(
# default_outs=default_outs, # only needed for internal BUCK
**kwargs
)

View File

@ -0,0 +1,89 @@
"""Provides utility macros for working with globs."""
load("@bazel_skylib//lib:paths.bzl", "paths")
def subdir_glob(glob_specs, exclude = None, prefix = ""):
"""Returns a dict of sub-directory relative paths to full paths.
The subdir_glob() function is useful for defining header maps for C/C++
libraries which should be relative the given sub-directory.
Given a list of tuples, the form of (relative-sub-directory, glob-pattern),
it returns a dict of sub-directory relative paths to full paths.
Please refer to native.glob() for explanations and examples of the pattern.
Args:
glob_specs: The array of tuples in form of
(relative-sub-directory, glob-pattern inside relative-sub-directory).
type: List[Tuple[str, str]]
exclude: A list of patterns to identify files that should be removed
from the set specified by the first argument. Defaults to [].
type: Optional[List[str]]
prefix: If is not None, prepends it to each key in the dictionary.
Defaults to None.
type: Optional[str]
Returns:
A dict of sub-directory relative paths to full paths.
"""
if exclude == None:
exclude = []
results = []
for dirpath, glob_pattern in glob_specs:
results.append(
_single_subdir_glob(dirpath, glob_pattern, exclude, prefix),
)
return _merge_maps(*results)
def _merge_maps(*file_maps):
result = {}
for file_map in file_maps:
for key in file_map:
if key in result and result[key] != file_map[key]:
fail(
"Conflicting files in file search paths. " +
"\"%s\" maps to both \"%s\" and \"%s\"." %
(key, result[key], file_map[key]),
)
result[key] = file_map[key]
return result
def _single_subdir_glob(dirpath, glob_pattern, exclude = None, prefix = None):
if exclude == None:
exclude = []
results = {}
files = native.glob([paths.join(dirpath, glob_pattern)], exclude = exclude)
for f in files:
if dirpath:
key = f[len(dirpath) + 1:]
else:
key = f
if prefix:
key = paths.join(prefix, key)
results[key] = f
return results
# Using a flat list will trigger build errors on Android.
# cxx_library will generate an apple_library on iOS, a cxx_library on Android.
# Those rules have different behaviors. Using a map will make the behavior consistent.
#
def glob_private_headers(glob_patterns, exclude = []):
result = {}
headers = native.glob(glob_patterns, exclude = exclude)
for header in headers:
result[paths.basename(header)] = header
return result
def glob(include, exclude = (), **kwargs):
buildfile = native.read_config("buildfile", "name", "BUCK")
subpkgs = [
target[:-len(buildfile)] + "**/*"
for target in native.glob(["*/**/" + buildfile])
]
return native.glob(include, exclude = list(exclude) + subpkgs, **kwargs)

View File

@ -0,0 +1,128 @@
"""Provides macros for queries type information."""
_SELECT_TYPE = type(select({"DEFAULT": []}))
def is_select(thing):
return type(thing) == _SELECT_TYPE
def is_unicode(arg):
"""Checks if provided instance has a unicode type.
Args:
arg: An instance to check. type: Any
Returns:
True for unicode instances, False otherwise. rtype: bool
"""
return hasattr(arg, "encode")
_STRING_TYPE = type("")
def is_string(arg):
"""Checks if provided instance has a string type.
Args:
arg: An instance to check. type: Any
Returns:
True for string instances, False otherwise. rtype: bool
"""
return type(arg) == _STRING_TYPE
_LIST_TYPE = type([])
def is_list(arg):
"""Checks if provided instance has a list type.
Args:
arg: An instance to check. type: Any
Returns:
True for list instances, False otherwise. rtype: bool
"""
return type(arg) == _LIST_TYPE
_DICT_TYPE = type({})
def is_dict(arg):
"""Checks if provided instance has a dict type.
Args:
arg: An instance to check. type: Any
Returns:
True for dict instances, False otherwise. rtype: bool
"""
return type(arg) == _DICT_TYPE
_TUPLE_TYPE = type(())
def is_tuple(arg):
"""Checks if provided instance has a tuple type.
Args:
arg: An instance to check. type: Any
Returns:
True for tuple instances, False otherwise. rtype: bool
"""
return type(arg) == _TUPLE_TYPE
def is_collection(arg):
"""Checks if provided instance is a collection subtype.
This will either be a dict, list, or tuple.
"""
return is_dict(arg) or is_list(arg) or is_tuple(arg)
_BOOL_TYPE = type(True)
def is_bool(arg):
"""Checks if provided instance is a boolean value.
Args:
arg: An instance ot check. type: Any
Returns:
True for boolean values, False otherwise. rtype: bool
"""
return type(arg) == _BOOL_TYPE
_NUMBER_TYPE = type(1)
def is_number(arg):
"""Checks if provided instance is a number value.
Args:
arg: An instance ot check. type: Any
Returns:
True for number values, False otherwise. rtype: bool
"""
return type(arg) == _NUMBER_TYPE
_STRUCT_TYPE = type(struct()) # Starlark returns the same type for all structs
def is_struct(arg):
"""Checks if provided instance is a struct value.
Args:
arg: An instance ot check. type: Any
Returns:
True for struct values, False otherwise. rtype: bool
"""
return type(arg) == _STRUCT_TYPE
type_utils = struct(
is_bool = is_bool,
is_number = is_number,
is_string = is_string,
is_unicode = is_unicode,
is_list = is_list,
is_dict = is_dict,
is_tuple = is_tuple,
is_collection = is_collection,
is_select = is_select,
is_struct = is_struct,
)

View File

@ -0,0 +1,591 @@
#!/usr/bin/env python3
import argparse
import json
import sys
from typing import List, Optional, Dict, Any
import yaml
from gen_op_registration_allowlist import (
canonical_name,
gen_transitive_closure,
load_op_dep_graph,
)
from torchgen.selective_build.operator import (
SelectiveBuildOperator,
merge_operator_dicts,
)
from torchgen.selective_build.selector import merge_kernel_metadata
# Generate YAML file containing the operators used for a specific PyTorch model.
# ------------------------------------------------------------------------------
#
# This binary is responsible for generating the model_operators.yaml file for
# each model from a pt_operator_library() BUCK macro invocation.
#
# Output YAML file format:
# ------------------------
#
# <BEGIN FILE CONTENTS>
# include_all_non_op_selectives: False
# include_all_operators: False
# debug_info:
# - model1@v100
# - model2@v50
# operators:
# aten::add:
# is_root_operator: Yes
# is_used_for_training: Yes
# include_all_overloads: No
# debug_info:
# - model1@v100
# - model2@v50
# aten::add.int:
# is_root_operator: No
# is_used_for_training: No
# include_all_overloads: Yes
# kernel_metadata:
# add_kernel:
# - Int8
# - UInt32
# sub_kernel:
# - Int16
# - Float
# <END FILE CONTENTS>
#
# There are a few main inputs to this application
# -----------------------------------------------
#
# 1. Inference Root Operators (--root_ops): Root operators (called directly
# from TorchScript) used by inference use-cases.
#
# 2. Training Root Operators (--training_root_ops): Root operators used
# by training use-cases. Currently, this list is the list of all operators
# used by training, and not just the root operators. All Training ops are
# also considered for inference, so these are merged into inference ops.
#
# 3. Operator Depencency Graph (--dep_graph_yaml_path): A path to the
# operator dependency graph used to determine which operators depend on
# which other operators for correct functioning. This is used for
# generating the transitive closure of all the operators used by the
# model based on the root operators when static selective build is used.
# For tracing based selective build, we don't need to perform this
# transitive cloure.
#
# 4. Model Metadata (--model_name, --model_versions, --model_assets,
# --model_backends): Self-descriptive. These are used to tell this
# script which model operator lists to fetch from the Unified Model
# Build Metadata YAML file.
#
# 5. Unified Model YAML file (--models_yaml_path): A path to the Unified
# model YAML operator list file. This yaml file contains (for each
# model/version/asset/backend) the set of used root and traced
# operators. This is used to extract the actual set of operators
# needed to be included in the build.
#
def canonical_opnames(opnames: List[str]) -> List[str]:
return [canonical_name(opname) for opname in opnames]
def make_filter_from_options(
model_name: str,
model_versions: List[str],
model_assets: Optional[List[str]],
model_backends: Optional[List[str]],
):
def is_model_included(model_info):
model = model_info["model"]
if model["name"] != model_name:
return False
if str(model["version"]) not in model_versions:
return False
if model_assets is not None and model["asset"] not in model_assets:
return False
# TODO: Handle backend later
return True
return is_model_included
# Returns if a the specified rule is a new or old style pt_operator_library
def is_new_style_rule(model_name: str, model_versions: Optional[List[str]]):
return model_name is not None and model_versions is not None
# Verifies that specified model_name, and all specified versions and assets
# appear in at least one model yaml. Throws if verification is failed,
# returns None on success
def verify_all_specified_present(
model_assets: Optional[List[str]],
model_versions: List[str],
selected_models_yaml: List[Dict[str, Any]],
rule_name: str,
model_name: str,
new_style_rule: bool,
):
def find_missing_items(model_items, key, selected_models_yaml):
missing_items = []
if not new_style_rule or not model_items:
return missing_items
for item in model_items:
found = False
for model in selected_models_yaml:
if str(model["model"][key]) == item:
found = True
if not found:
missing_items.append(item)
return missing_items
missing_assets = find_missing_items(model_assets, "asset", selected_models_yaml)
missing_versions = find_missing_items(
model_versions, "version", selected_models_yaml
)
if len(missing_versions) > 0 or len(missing_assets) > 0: # at least one is missing
name_warning = ""
if len(selected_models_yaml) == 0:
name_warning = (
"WARNING: 0 yaml's were found for target rule. This could be because the "
+ "provided model name: {name} is incorrect. Please check that field as well as "
+ "the assets and versions."
).format(name=model_name)
raise RuntimeError(
(
"Error: From the pt_operator_library rule for Rule: {name}, at least one entry for the "
+ "following fields was expected -- Model: {model_name} Expected Assets: {expected_assets}, Expected Versions: "
+ "{expected_versions}. {name_warning} In all_mobile_models.yaml either no assets were on one of the "
+ "specified versions, one of the specified assets was not present on any of the specified "
+ "versions, or both. Assets not found: {missing_assets}, Versions not found: {missing_versions} "
+ "For questions please ask in https://fb.workplace.com/groups/2148543255442743/"
).format(
name=rule_name,
model_name=model_name,
expected_versions=model_versions,
expected_assets=model_assets
if model_assets
else "<All model assets present on specified versions>",
name_warning=name_warning,
missing_versions=missing_versions
if len(missing_versions) > 0
else "<All specified versions had at least one asset>",
missing_assets=missing_assets
if len(missing_assets) > 0
else "<All specified assets are present on at least 1 version>",
)
)
# Uses the selected models configs and then combines them into one dictionary,
# formats them as a string, and places the string into output as a top level debug_info
def create_debug_info_from_selected_models(
output: Dict[str, object],
selected_models: List[dict],
new_style_rule: bool,
):
model_dict = {
"asset_info": {}, # maps asset name -> dict of asset metadata like hashes
"is_new_style_rule": new_style_rule,
}
for model in selected_models:
model_info = model["model"]
asset = model_info["asset"]
hash = model_info["md5_hash"]
asset_info = model_dict["asset_info"].setdefault(asset, {})
asset_info.setdefault("md5_hash", []).append(hash)
# Will later be used in gen_oplist to generate the model/version/asset checking
output["debug_info"] = [json.dumps(model_dict)]
def fill_output(output: Dict[str, object], options: object):
"""Populate the output dict with the information required to serialize
the YAML file used for selective build.
"""
dept_graph = load_op_dep_graph(options.dep_graph_yaml_path)
model_versions = (
options.model_versions.split(",") if options.model_versions is not None else []
)
model_assets = (
options.model_assets.split(",") if options.model_assets is not None else None
)
with open(options.models_yaml_path, "rb") as models_yaml_file:
all_models_yaml = yaml.safe_load(models_yaml_file) or []
model_filter_func = make_filter_from_options(
options.model_name, model_versions, model_assets, options.model_backends
)
selected_models_yaml = list(filter(model_filter_func, all_models_yaml))
verify_all_specified_present(
model_assets=model_assets,
model_versions=model_versions,
selected_models_yaml=selected_models_yaml,
rule_name=options.rule_name,
model_name=options.model_name,
new_style_rule=is_new_style_rule(options.model_name, options.model_versions),
)
create_debug_info_from_selected_models(
output,
selected_models_yaml,
is_new_style_rule(options.model_name, options.model_versions),
)
# initialize variables for static build from the pt_operator_library rule
if options.root_ops is not None:
static_root_ops = set(filter(lambda x: len(x) > 0, options.root_ops.split(",")))
else:
static_root_ops = set()
static_training_root_ops = set(
filter(
lambda x: len(x) > 0,
(options.training_root_ops or "").split(","),
)
)
if len(static_training_root_ops) > 0:
static_root_ops = static_root_ops | static_training_root_ops
# end if
root_ops_unexpand = set()
traced_ops = set()
training_root_ops_unexpand = set()
traced_training_ops = set()
all_kernel_metadata = []
all_custom_classes = set()
all_build_features = set()
# Go through each yaml file and retrieve operator information.
for model_info in selected_models_yaml:
if "traced_operators" not in model_info:
# If this YAML file doesn't specify any traced operators, then it is using
# the static analysis selective build approach of finding transitively
# used operators, and we should update root_ops with the set of root
# operators, all of whose overloads must be included. In addition, these
# root_ops will be further expanded using the transitive closure of
# operator dependencies.
static_root_ops = static_root_ops | set(model_info["root_operators"])
else:
# If this YAML file specifies traced operators, then it is using
# the tracing based selective build approach of finding used
# operators, and we should update root_ops_unexpand with the set of root
# operators whose overloads don't need to be included. In addition, these
# root_ops_unexpand will NOT be further expanded. If the train flag is
# set then the ops will be used for training, so we put them in a separate
# set
if model_info["train"]:
training_root_ops_unexpand = training_root_ops_unexpand | set(
model_info["root_operators"]
)
traced_training_ops = traced_training_ops | set(
model_info["traced_operators"]
)
else:
root_ops_unexpand = root_ops_unexpand | set(
model_info["root_operators"]
)
traced_ops = traced_ops | set(model_info["traced_operators"])
if "kernel_metadata" in model_info:
all_kernel_metadata.append(model_info["kernel_metadata"])
if "custom_classes" in model_info:
all_custom_classes = all_custom_classes | set(model_info["custom_classes"])
if "build_features" in model_info:
all_build_features = all_build_features | set(model_info["build_features"])
# This following section on transitive closure is relevant to static build only
canonical_root_ops = canonical_opnames(static_root_ops)
# If no canonical_root_ops exist, don't compute the transitive closure
# otherwise, we will include __BASE__ and __ROOT__ ops and mark them as required
# for inference.
if len(canonical_root_ops) > 0:
closure_op_list = gen_transitive_closure(dept_graph, canonical_root_ops)
else:
closure_op_list = set()
canonical_training_root_ops = canonical_opnames(static_training_root_ops)
# If no canonical_training_root_ops exist, don't compute the transitive closure
# otherwise, we will include __BASE__ and __ROOT__ ops and mark them as required
# for training.
if len(canonical_training_root_ops) > 0:
closure_training_op_list = gen_transitive_closure(
dept_graph, canonical_training_root_ops, train=True
)
else:
closure_training_op_list = set()
# bucketed_ops holds sets of operators that correspond to specific semantic buckets. For
# example:
#
# 1. Root Operators not used for training w/o full overload inclusion
# 2. Root Operators not used for training w/ full overload inclusion
# 3. Root Operators used for training w/o full overload inclusion
# 4. Root Operators used for training w/ full overload inclusion
# 5. Non-root Operators not used for training w/o full overload inclusion
# etc...
#
# Basically for each of the 3 boolean conditional, there are 2
# options (True/False).
#
bucketed_ops = []
# START STATIC BUILD OPS
static_root_ops_bucket = {}
for op_name in static_root_ops:
op = SelectiveBuildOperator.from_yaml_dict(
op_name,
{
"is_root_operator": True,
"is_used_for_training": False,
"include_all_overloads": True,
"debug_info": [options.model_name],
},
)
static_root_ops_bucket[op_name] = op
bucketed_ops.append(static_root_ops_bucket)
closure_ops_bucket = {}
for op_name in closure_op_list:
op = SelectiveBuildOperator.from_yaml_dict(
op_name,
{
"is_root_operator": False,
"is_used_for_training": False,
"include_all_overloads": True,
"debug_info": [options.model_name],
},
)
closure_ops_bucket[op_name] = op
bucketed_ops.append(closure_ops_bucket)
static_training_root_ops_bucket = {}
for op_name in static_training_root_ops:
op = SelectiveBuildOperator.from_yaml_dict(
op_name,
{
"is_root_operator": True,
"is_used_for_training": True,
"include_all_overloads": True,
"debug_info": [options.model_name],
},
)
static_training_root_ops_bucket[op_name] = op
bucketed_ops.append(static_training_root_ops_bucket)
closure_training_ops_bucket = {}
for op_name in closure_training_op_list:
op = SelectiveBuildOperator.from_yaml_dict(
op_name,
{
"is_root_operator": False,
"is_used_for_training": True,
"include_all_overloads": True,
"debug_info": [options.model_name],
},
)
closure_training_ops_bucket[op_name] = op
bucketed_ops.append(closure_training_ops_bucket)
# END STATIC BUILD OPS
# START TRACING BASED BUILD OPS
root_ops_unexpand_bucket = {}
for op_name in root_ops_unexpand:
op = SelectiveBuildOperator.from_yaml_dict(
op_name,
{
"is_root_operator": True,
"is_used_for_training": False,
"include_all_overloads": False,
"debug_info": [options.model_name],
},
)
root_ops_unexpand_bucket[op_name] = op
bucketed_ops.append(root_ops_unexpand_bucket)
traced_ops_bucket = {}
for op_name in traced_ops:
op = SelectiveBuildOperator.from_yaml_dict(
op_name,
{
"is_root_operator": False,
"is_used_for_training": False,
"include_all_overloads": False,
"debug_info": [options.model_name],
},
)
traced_ops_bucket[op_name] = op
bucketed_ops.append(traced_ops_bucket)
training_root_ops_unexpand_bucket = {}
for op_name in training_root_ops_unexpand:
op = SelectiveBuildOperator.from_yaml_dict(
op_name,
{
"is_root_operator": True,
"is_used_for_training": True,
"include_all_overloads": False,
"debug_info": [options.model_name],
},
)
training_root_ops_unexpand_bucket[op_name] = op
bucketed_ops.append(training_root_ops_unexpand_bucket)
traced_training_ops_bucket = {}
for op_name in traced_training_ops:
op = SelectiveBuildOperator.from_yaml_dict(
op_name,
{
"is_root_operator": False,
"is_used_for_training": True,
"include_all_overloads": False,
"debug_info": [options.model_name],
},
)
traced_training_ops_bucket[op_name] = op
bucketed_ops.append(traced_training_ops_bucket)
# END TRACING BASED BUILD OPS
# Merge dictionaries together to remove op duplication
operators: Dict[str, SelectiveBuildOperator] = {}
for ops_dict in bucketed_ops:
operators = merge_operator_dicts(operators, ops_dict)
# Loop over all operators, and if any of the them specifies that
# all overloads need to be included, then set include_all_non_op_selectives
# to True, since it indicates that this operator list came from something
# other than a traced operator list.
include_all_non_op_selectives = False
for (op_name, op_info) in operators.items():
include_all_non_op_selectives = (
include_all_non_op_selectives or op_info.include_all_overloads
)
operators_as_dict = {}
for (k, v) in operators.items():
operators_as_dict[k] = v.to_dict()
output["operators"] = operators_as_dict
output["custom_classes"] = all_custom_classes
output["build_features"] = all_build_features
output["include_all_non_op_selectives"] = include_all_non_op_selectives
if len(all_kernel_metadata) > 0:
kernel_metadata = {}
for kt in all_kernel_metadata:
kernel_metadata = merge_kernel_metadata(kernel_metadata, kt)
output["kernel_metadata"] = kernel_metadata
def get_parser_options(parser: argparse.ArgumentParser) -> argparse.Namespace:
parser.add_argument(
"--root_ops",
help="A comma separated list of root operators used by the model",
required=False,
)
parser.add_argument(
"--training_root_ops",
help="A comma separated list of root operators used for training",
required=False,
)
parser.add_argument(
"--output_path",
help="The location of the output yaml file.",
required=True,
)
parser.add_argument(
"--dep_graph_yaml_path",
type=str,
help="A path to the Operator Dependency Graph YAML file.",
required=True,
)
parser.add_argument(
"--model_name",
type=str,
help="The name of the model that uses the specified root operators.",
required=True,
)
parser.add_argument(
"--model_versions",
type=str,
help="A comma separated list of model versions.",
required=False,
)
parser.add_argument(
"--model_assets",
type=str,
help="A comma separate list of model asset names (if absent, defaults to all assets for this model).",
required=False,
)
parser.add_argument(
"--model_backends",
type=str,
default="CPU",
help="A comma separated list of model backends.",
required=False,
)
parser.add_argument(
"--models_yaml_path",
type=str,
help="The path to where the unified Mobile Model Config YAML resides.",
required=True,
)
parser.add_argument(
"--include_all_operators",
action="store_true",
default=False,
help="Set this flag to request inclusion of all opeators (i.e. build is not selective).",
required=False,
)
parser.add_argument(
"--rule_name",
type=str,
help="The name of pt_operator_library rule resulting in this generation",
required=True,
)
options = parser.parse_args()
return options
def main(argv) -> None:
parser = argparse.ArgumentParser(description="Generate used operators YAML")
options = get_parser_options(parser)
model_dict = {
"model_name": options.model_name,
"asset_info": {},
"is_new_style_rule": False,
}
output = {
"debug_info": [json.dumps(model_dict)],
}
if options.include_all_operators:
output["include_all_operators"] = True
output["operators"] = {}
output["kernel_metadata"] = {}
else:
fill_output(output, options)
with open(options.output_path, "wb") as out_file:
out_file.write(
yaml.safe_dump(
output,
default_flow_style=False,
).encode("utf-8")
)
if __name__ == "__main__":
sys.exit(main(sys.argv))

13
tools/jit/BUILD.buck Normal file
View File

@ -0,0 +1,13 @@
python_library(
name = "jit",
srcs = glob([
"*.py",
"templates/*",
]),
base_module = "tools.jit",
visibility = ["PUBLIC"],
deps = [
"//:aten_code_template",
"//torchgen:torchgen",
],
)

View File

@ -0,0 +1,6 @@
python_library(
name = "gen_selected_mobile_ops_header",
srcs = ["gen_selected_mobile_ops_header.py"],
base_module = "tools.lite_interpreter",
visibility = ["PUBLIC"],
)

View File

@ -0,0 +1,41 @@
python_library(
name = "generate_code",
srcs = [
"generate_code.py",
],
base_module = "tools.setup_helpers",
deps = [
"//tools/autograd:autograd",
"//tools/jit:jit",
"//torchgen:torchgen",
],
)
python_binary(
name = "generate_code_bin",
main_module = "tools.setup_helpers.generate_code",
visibility = ["PUBLIC"],
# package_style = "inplace",
zip_safe = False,
deps = [
":generate_code",
],
)
python_library(
name = "gen-version-header-lib",
srcs = [
"gen_version_header.py",
],
base_module = "tools.setup_helpers",
deps = [],
)
python_binary(
name = "gen-version-header",
main_module = "tools.setup_helpers.gen_version_header",
visibility = ["PUBLIC"],
deps = [
":gen-version-header-lib",
],
)

23
torchgen/BUILD.buck Normal file
View File

@ -0,0 +1,23 @@
python_library(
name = "torchgen",
srcs = glob(
["**/*.py"],
),
base_module = "torchgen",
visibility = ["PUBLIC"],
deps = [
"//third_party:pyyaml",
"//third_party:typing-extensions",
],
)
python_binary(
name = "gen",
main_module = "torchgen.gen",
visibility = [
"PUBLIC",
],
deps = [
":torchgen",
],
)