├── .gitignore ├── LICENSE ├── README.md └── old ├── CMakeLists.txt ├── FindOpenBLAS.cmake ├── FindvecLib.cmake └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | CMakeLists.txt.user 2 | CMakeCache.txt 3 | CMakeFiles 4 | CMakeScripts 5 | Testing 6 | Makefile 7 | cmake_install.cmake 8 | install_manifest.txt 9 | compile_commands.json 10 | CTestTestfile.cmake 11 | _deps 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Creative Commons Legal Code 2 | 3 | CC0 1.0 Universal 4 | 5 | CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE 6 | LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN 7 | ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS 8 | INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES 9 | REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS 10 | PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM 11 | THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED 12 | HEREUNDER. 13 | 14 | Statement of Purpose 15 | 16 | The laws of most jurisdictions throughout the world automatically confer 17 | exclusive Copyright and Related Rights (defined below) upon the creator 18 | and subsequent owner(s) (each and all, an "owner") of an original work of 19 | authorship and/or a database (each, a "Work"). 20 | 21 | Certain owners wish to permanently relinquish those rights to a Work for 22 | the purpose of contributing to a commons of creative, cultural and 23 | scientific works ("Commons") that the public can reliably and without fear 24 | of later claims of infringement build upon, modify, incorporate in other 25 | works, reuse and redistribute as freely as possible in any form whatsoever 26 | and for any purposes, including without limitation commercial purposes. 27 | These owners may contribute to the Commons to promote the ideal of a free 28 | culture and the further production of creative, cultural and scientific 29 | works, or to gain reputation or greater distribution for their Work in 30 | part through the use and efforts of others. 31 | 32 | For these and/or other purposes and motivations, and without any 33 | expectation of additional consideration or compensation, the person 34 | associating CC0 with a Work (the "Affirmer"), to the extent that he or she 35 | is an owner of Copyright and Related Rights in the Work, voluntarily 36 | elects to apply CC0 to the Work and publicly distribute the Work under its 37 | terms, with knowledge of his or her Copyright and Related Rights in the 38 | Work and the meaning and intended legal effect of CC0 on those rights. 39 | 40 | 1. Copyright and Related Rights. A Work made available under CC0 may be 41 | protected by copyright and related or neighboring rights ("Copyright and 42 | Related Rights"). Copyright and Related Rights include, but are not 43 | limited to, the following: 44 | 45 | i. the right to reproduce, adapt, distribute, perform, display, 46 | communicate, and translate a Work; 47 | ii. moral rights retained by the original author(s) and/or performer(s); 48 | iii. publicity and privacy rights pertaining to a person's image or 49 | likeness depicted in a Work; 50 | iv. rights protecting against unfair competition in regards to a Work, 51 | subject to the limitations in paragraph 4(a), below; 52 | v. rights protecting the extraction, dissemination, use and reuse of data 53 | in a Work; 54 | vi. database rights (such as those arising under Directive 96/9/EC of the 55 | European Parliament and of the Council of 11 March 1996 on the legal 56 | protection of databases, and under any national implementation 57 | thereof, including any amended or successor version of such 58 | directive); and 59 | vii. other similar, equivalent or corresponding rights throughout the 60 | world based on applicable law or treaty, and any national 61 | implementations thereof. 62 | 63 | 2. Waiver. To the greatest extent permitted by, but not in contravention 64 | of, applicable law, Affirmer hereby overtly, fully, permanently, 65 | irrevocably and unconditionally waives, abandons, and surrenders all of 66 | Affirmer's Copyright and Related Rights and associated claims and causes 67 | of action, whether now known or unknown (including existing as well as 68 | future claims and causes of action), in the Work (i) in all territories 69 | worldwide, (ii) for the maximum duration provided by applicable law or 70 | treaty (including future time extensions), (iii) in any current or future 71 | medium and for any number of copies, and (iv) for any purpose whatsoever, 72 | including without limitation commercial, advertising or promotional 73 | purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each 74 | member of the public at large and to the detriment of Affirmer's heirs and 75 | successors, fully intending that such Waiver shall not be subject to 76 | revocation, rescission, cancellation, termination, or any other legal or 77 | equitable action to disrupt the quiet enjoyment of the Work by the public 78 | as contemplated by Affirmer's express Statement of Purpose. 79 | 80 | 3. Public License Fallback. Should any part of the Waiver for any reason 81 | be judged legally invalid or ineffective under applicable law, then the 82 | Waiver shall be preserved to the maximum extent permitted taking into 83 | account Affirmer's express Statement of Purpose. In addition, to the 84 | extent the Waiver is so judged Affirmer hereby grants to each affected 85 | person a royalty-free, non transferable, non sublicensable, non exclusive, 86 | irrevocable and unconditional license to exercise Affirmer's Copyright and 87 | Related Rights in the Work (i) in all territories worldwide, (ii) for the 88 | maximum duration provided by applicable law or treaty (including future 89 | time extensions), (iii) in any current or future medium and for any number 90 | of copies, and (iv) for any purpose whatsoever, including without 91 | limitation commercial, advertising or promotional purposes (the 92 | "License"). The License shall be deemed effective as of the date CC0 was 93 | applied by Affirmer to the Work. Should any part of the License for any 94 | reason be judged legally invalid or ineffective under applicable law, such 95 | partial invalidity or ineffectiveness shall not invalidate the remainder 96 | of the License, and in such case Affirmer hereby affirms that he or she 97 | will not (i) exercise any of his or her remaining Copyright and Related 98 | Rights in the Work or (ii) assert any associated claims and causes of 99 | action with respect to the Work, in either case contrary to Affirmer's 100 | express Statement of Purpose. 101 | 102 | 4. Limitations and Disclaimers. 103 | 104 | a. No trademark or patent rights held by Affirmer are waived, abandoned, 105 | surrendered, licensed or otherwise affected by this document. 106 | b. Affirmer offers the Work as-is and makes no representations or 107 | warranties of any kind concerning the Work, express, implied, 108 | statutory or otherwise, including without limitation warranties of 109 | title, merchantability, fitness for a particular purpose, non 110 | infringement, or the absence of latent or other defects, accuracy, or 111 | the present or absence of errors, whether or not discoverable, all to 112 | the greatest extent permissible under applicable law. 113 | c. Affirmer disclaims responsibility for clearing rights of other persons 114 | that may apply to the Work or any use thereof, including without 115 | limitation any person's Copyright and Related Rights in the Work. 116 | Further, Affirmer disclaims responsibility for obtaining any necessary 117 | consents, permissions or other rights required for any use of the 118 | Work. 119 | d. Affirmer understands and acknowledges that Creative Commons is not a 120 | party to this document and has no duty or obligation with respect to 121 | this CC0 or use of the Work. 122 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PyTorch Build for Apple Silicon 2 | **NOTE**: This document is useful only for **scratch build** from source code for Apple Silicon. 3 | 4 | ## Work-flow 5 | - install prerequisites 6 | - download PyTorch source from repository 7 | - install PyTorch through a wheel-build, or standard install 8 | 9 | ## Test Environment 10 | - Python 3.9.8 11 | - macOS Montorey (ver.12.0.1) 12 | - PyTorch 1.12 (2022/02/23@JST) 13 | ## Prerequisite Installation 14 | 15 | ### - CMake 16 | **NOTE**: Compilation needs version 3.13 or greater of cmake. 17 | ``` 18 | $brew install cmake 19 | ``` 20 | ### - OpenBLAS 21 | ``` 22 | $brew install openblas 23 | ``` 24 | ### - Yaml 25 | ``` 26 | $python3 -m pip install pyyaml 27 | ``` 28 | 29 | ## Download the source-code of PyTorch 30 | ``` 31 | git clone --recursive https://github.com/pytorch/pytorch 32 | ``` 33 | 34 | Enter the directory; 35 | ``` 36 | $cd pytorch 37 | ``` 38 | 39 | ## Setting CMakeList.txt 40 | pThead is not supported on OS-X; 41 | ``` 42 | set(THREADS_PREFER_PTHREAD_FLAG OFF) 43 | ``` 44 | 45 | OS-X does not support Microsoft VisualStudio, so comment-out between these lines; 46 | ``` 47 | if(MSVC) 48 | ``` 49 | and 50 | ``` 51 | endif(MSVC) 52 | ``` 53 | 54 | And, setting APPLE as **TRUE**. 55 | 56 | At line 184, 57 | Apple Silicon does not support NVIDIA's CUDA; 58 | ``` 59 | option(USE_CUDA "Use CUDA" OFF) 60 | ``` 61 | Apple Silicon does not support NVIDIA's cuDNN; 62 | ``` 63 | USE_CUDNN "Use cuDNN" OFF 64 | ``` 65 | 66 | At line 193, 67 | Apple Silicon does not support AMD's ROCm; 68 | ``` 69 | option(USE_ROCM "Use ROCm" OFF) 70 | ``` 71 | 72 | At line 230, 73 | Apple Silicon does not support NVIDIA's NCCL Library; 74 | ``` 75 | USE_NCCL "Use NCCL" OFF 76 | ``` 77 | At line 232, 78 | Apple Silicon does not support AMD's RCCL Library; 79 | ``` 80 | cmake_dependent_option(USE_RCCL "Use RCCL" OFF 81 | ``` 82 | 83 | At line 243, 84 | Apple Silicon does not support NUMA Memory; 85 | ``` 86 | USE_NUMA "Use NUMA. Only available on Linux." OFF 87 | ``` 88 | 89 | 90 | At line 252, 91 | Apple Silicon does not support OpenMP; 92 | ``` 93 | option(USE_OPENMP "Use OpenMP for parallel code" OFF) 94 | ``` 95 | ## Build for Wheel-based Install 96 | ### - Compile Sorce-Code 97 | Compile with below command; 98 | ``` 99 | $python3 setup.py build 100 | ``` 101 | ### - Build Wheel 102 | ``` 103 | $python3 setup.py bdist bdist_wheel 104 | ``` 105 | ### - Install 106 | Wheel file is in "dist" directory. 107 | You can use **pip install** for the wheel file. 108 | 109 | ## Build for Standard-Install 110 | ### - Compile Sorce-Code 111 | Compile with below command; 112 | ``` 113 | $python3 setup.py build 114 | ``` 115 | ### - Install 116 | ``` 117 | python3 setup.py develop && python3 -c "import torch" 118 | ``` -------------------------------------------------------------------------------- /old/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.5 FATAL_ERROR) 2 | #cmake_policy(SET CMP0022 NEW) 3 | #cmake_policy(SET CMP0023 NEW) 4 | 5 | # Use compiler ID "AppleClang" instead of "Clang" for XCode. 6 | # Not setting this sometimes makes XCode C compiler gets detected as "Clang", 7 | # even when the C++ one is detected as "AppleClang". 8 | cmake_policy(SET CMP0010 NEW) 9 | cmake_policy(SET CMP0025 NEW) 10 | 11 | # Suppress warning flags in default MSVC configuration. It's not 12 | # mandatory that we do this (and we don't if cmake is old), but it's 13 | # nice when it's possible, and it's possible on our Windows configs. 14 | if(NOT CMAKE_VERSION VERSION_LESS 3.15.0) 15 | cmake_policy(SET CMP0092 NEW) 16 | endif() 17 | 18 | if(NOT CMAKE_VERSION VERSION_LESS 3.10) 19 | set(FIND_CUDA_MODULE_DEPRECATED ON) 20 | endif() 21 | 22 | # ---[ Project and semantic versioning. 23 | project(Torch CXX C) 24 | 25 | if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux") 26 | set(LINUX TRUE) 27 | else() 28 | set(LINUX FALSE) 29 | endif() 30 | 31 | set(CMAKE_INSTALL_MESSAGE NEVER) 32 | 33 | set(CMAKE_CXX_STANDARD 14) 34 | set(CMAKE_C_STANDARD 11) 35 | if(DEFINED GLIBCXX_USE_CXX11_ABI) 36 | if(${GLIBCXX_USE_CXX11_ABI} EQUAL 1) 37 | set(CXX_STANDARD_REQUIRED ON) 38 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D_GLIBCXX_USE_CXX11_ABI=1") 39 | endif() 40 | endif() 41 | 42 | set(CMAKE_EXPORT_COMPILE_COMMANDS ON) 43 | 44 | # One variable that determines whether the current cmake process is being run 45 | # with the main Caffe2 library. This is useful for building modules - if 46 | # modules are built with the main Caffe2 library then one does not need to do 47 | # find caffe2 in the cmake script. One can usually guard it in some way like 48 | # if(NOT CAFFE2_CMAKE_BUILDING_WITH_MAIN_REPO) 49 | # find_package(Caffe2 REQUIRED) 50 | # endif() 51 | set(CAFFE2_CMAKE_BUILDING_WITH_MAIN_REPO ON) 52 | 53 | # Googletest's cmake files are going to set it on once they are processed. Let's 54 | # set it at the very beginning so that the entire build is deterministic. 55 | set(THREADS_PREFER_PTHREAD_FLAG ON) 56 | 57 | if(NOT DEFINED BLAS_SET_BY_USER) 58 | if(DEFINED BLAS) 59 | set(BLAS_SET_BY_USER TRUE) 60 | else() 61 | message(STATUS "Not forcing any particular BLAS to be found") 62 | set(BLAS_SET_BY_USER FALSE) 63 | endif() 64 | set(BLAS_SET_BY_USER ${BLAS_SET_BY_USER} CACHE STRING "Marks whether BLAS was manually set by user or auto-detected") 65 | endif() 66 | 67 | # Apple specific 68 | if(APPLE) 69 | # These lines are an attempt to make find_package(cuda) pick up 70 | # libcuda.dylib, and not cuda.framework. It doesn't work all 71 | # the time, but it seems to help for some users. 72 | # TODO: replace this with a more robust fix 73 | set(CMAKE_FIND_FRAMEWORK LAST) 74 | set(CMAKE_FIND_APPBUNDLE LAST) 75 | 76 | # Get clang version on macOS 77 | execute_process( COMMAND ${CMAKE_CXX_COMPILER} --version OUTPUT_VARIABLE clang_full_version_string ) 78 | string(REGEX REPLACE "Apple LLVM version ([0-9]+\\.[0-9]+).*" "\\1" CLANG_VERSION_STRING ${clang_full_version_string}) 79 | message( STATUS "CLANG_VERSION_STRING: " ${CLANG_VERSION_STRING} ) 80 | 81 | 82 | # RPATH stuff 83 | set(CMAKE_MACOSX_RPATH ON) 84 | endif() 85 | 86 | if(WIN32) 87 | # On Windows, CMAKE_HOST_SYSTEM_PROCESSOR is calculated through `PROCESSOR_ARCHITECTURE`, 88 | # which only has the value of `x86` or `AMD64`. We cannot infer whether it's a Intel CPU 89 | # or not. However, the environment variable `PROCESSOR_IDENTIFIER` could be used. 90 | if($ENV{PROCESSOR_IDENTIFIER} MATCHES "Intel") 91 | set(CPU_INTEL ON) 92 | else() 93 | set(CPU_INTEL OFF) 94 | endif() 95 | else() 96 | if(${CMAKE_HOST_SYSTEM_PROCESSOR} MATCHES "(x86_64|i[3-6]+86)") 97 | set(CPU_INTEL ON) 98 | else() 99 | set(CPU_INTEL OFF) 100 | endif() 101 | endif() 102 | 103 | 104 | # For non-supported platforms, turn USE_DISTRIBUTED off by default. 105 | # It is not tested and likely won't work without additional changes. 106 | if(NOT LINUX AND NOT WIN32) 107 | set(USE_DISTRIBUTED OFF CACHE STRING "Use distributed") 108 | # On macOS, if USE_DISTRIBUTED is enabled (specified by the user), 109 | # then make Gloo build with the libuv transport. 110 | if(APPLE AND USE_DISTRIBUTED) 111 | set(USE_LIBUV ON CACHE STRING "") 112 | endif() 113 | endif() 114 | 115 | # ---[ Options. 116 | # Note to developers: if you add an option below, make sure you also add it to 117 | # cmake/Summary.cmake so that the summary prints out the option values. 118 | include(CMakeDependentOption) 119 | option(ATEN_NO_TEST "Do not build ATen test binaries" OFF) 120 | option(BUILD_BINARY "Build C++ binaries" OFF) 121 | option(BUILD_DOCS "Build Caffe2 documentation" OFF) 122 | option(BUILD_CUSTOM_PROTOBUF "Build and use Caffe2's own protobuf under third_party" ON) 123 | option(BUILD_PYTHON "Build Python binaries" ON) 124 | option(BUILD_CAFFE2 "Master flag to build Caffe2" ON) 125 | set(PYTHON_EXECUTABLE=/usr/bin/python3) 126 | cmake_dependent_option( 127 | BUILD_CAFFE2_OPS "Build Caffe2 operators" ON 128 | "BUILD_CAFFE2" OFF) 129 | cmake_dependent_option( 130 | BUILD_CAFFE2_MOBILE "Build libcaffe2 for mobile (deprecating)" OFF 131 | "BUILD_CAFFE2" OFF) 132 | option(BUILD_SHARED_LIBS "Build libcaffe2.so" ON) 133 | cmake_dependent_option( 134 | CAFFE2_LINK_LOCAL_PROTOBUF "If set, build protobuf inside libcaffe2.so." ON 135 | "BUILD_SHARED_LIBS AND BUILD_CUSTOM_PROTOBUF" OFF) 136 | cmake_dependent_option( 137 | CAFFE2_USE_MSVC_STATIC_RUNTIME "Using MSVC static runtime libraries" ON 138 | "NOT BUILD_SHARED_LIBS" OFF) 139 | option(BUILD_TEST "Build C++ test binaries (need gtest and gbenchmark)" OFF) 140 | option(BUILD_STATIC_RUNTIME_BENCHMARK "Build C++ binaries for static runtime benchmarks (need gbenchmark)" OFF) 141 | option(BUILD_TENSOREXPR_BENCHMARK "Build C++ binaries for tensorexpr benchmarks (need gbenchmark)" OFF) 142 | option(BUILD_MOBILE_BENCHMARK "Build C++ test binaries for mobile (ARM) targets(need gtest and gbenchmark)" OFF) 143 | option(BUILD_MOBILE_TEST "Build C++ test binaries for mobile (ARM) targets(need gtest and gbenchmark)" OFF) 144 | option(BUILD_JNI "Build JNI bindings" OFF) 145 | option(BUILD_MOBILE_AUTOGRAD "Build autograd function in mobile build (in development)" OFF) 146 | cmake_dependent_option( 147 | INSTALL_TEST "Install test binaries if BUILD_TEST is on" ON 148 | "BUILD_TEST" OFF) 149 | option(USE_CPP_CODE_COVERAGE "Compile C/C++ with code coverage flags" OFF) 150 | option(COLORIZE_OUTPUT "Colorize output during compilation" ON) 151 | option(USE_ASAN "Use Address Sanitizer" OFF) 152 | option(USE_TSAN "Use Thread Sanitizer" OFF) 153 | option(USE_CUDA "Use CUDA" OFF) 154 | option(USE_ROCM "Use ROCm" OFF) 155 | option(CAFFE2_STATIC_LINK_CUDA "Statically link CUDA libraries" OFF) 156 | cmake_dependent_option( 157 | USE_CUDNN "Use cuDNN" OFF 158 | "USE_CUDA" OFF) 159 | cmake_dependent_option( 160 | USE_STATIC_CUDNN "Use cuDNN static libraries" OFF 161 | "USE_CUDNN" OFF) 162 | option(USE_FBGEMM "Use FBGEMM (quantized 8-bit server operators)" ON) 163 | option(USE_KINETO "Use Kineto profiling library" OFF) 164 | option(USE_FAKELOWP "Use FakeLowp operators" OFF) 165 | option(USE_FFMPEG "Use ffmpeg" OFF) 166 | option(USE_GFLAGS "Use GFLAGS" OFF) 167 | option(USE_GLOG "Use GLOG" OFF) 168 | option(USE_LEVELDB "Use LEVELDB" OFF) 169 | option(USE_LITE_PROTO "Use lite protobuf instead of full." OFF) 170 | option(USE_LMDB "Use LMDB" OFF) 171 | option(USE_METAL "Use Metal for Caffe2 iOS build" ON) 172 | option(USE_PYTORCH_METAL "Use Metal for PyTorch iOS build" OFF) 173 | option(USE_NATIVE_ARCH "Use -march=native" OFF) 174 | cmake_dependent_option( 175 | USE_NCCL "Use NCCL" ON 176 | "USE_CUDA OR USE_ROCM;UNIX;NOT APPLE" OFF) 177 | cmake_dependent_option( 178 | USE_STATIC_NCCL "Use static NCCL" OFF 179 | "USE_NCCL" OFF) 180 | cmake_dependent_option( 181 | USE_SYSTEM_NCCL "Use system-wide NCCL" OFF 182 | "USE_NCCL" OFF) 183 | option(USE_NNAPI "Use NNAPI" OFF) 184 | option(USE_NNPACK "Use NNPACK" ON) 185 | cmake_dependent_option( 186 | USE_NUMA "Use NUMA. Only available on Linux." OFF 187 | "LINUX" OFF) 188 | cmake_dependent_option( 189 | USE_NVRTC "Use NVRTC. Only available if USE_CUDA is on." OFF 190 | "USE_CUDA" OFF) 191 | option(USE_NUMPY "Use NumPy" ON) 192 | option(USE_OBSERVERS "Use observers module." OFF) 193 | option(USE_OPENCL "Use OpenCL" OFF) 194 | option(USE_OPENCV "Use OpenCV" OFF) 195 | option(USE_OPENMP "Use OpenMP for parallel code" ON) 196 | option(USE_PROF "Use profiling" OFF) 197 | option(USE_QNNPACK "Use QNNPACK (quantized 8-bit operators)" ON) 198 | option(USE_PYTORCH_QNNPACK "Use ATen/QNNPACK (quantized 8-bit operators)" ON) 199 | option(USE_REDIS "Use Redis" OFF) 200 | option(USE_ROCKSDB "Use RocksDB" OFF) 201 | option(USE_SNPE "Use Qualcomm's SNPE library" OFF) 202 | option(USE_SYSTEM_EIGEN_INSTALL 203 | "Use system Eigen instead of the one under third_party" OFF) 204 | option(USE_TENSORRT "Using Nvidia TensorRT library" OFF) 205 | cmake_dependent_option( 206 | USE_VALGRIND "Use Valgrind. Only available on Linux." OFF 207 | "LINUX" OFF) 208 | option(USE_VULKAN "Use Vulkan GPU backend" OFF) 209 | option(USE_VULKAN_FP16_INFERENCE "Vulkan - Use fp16 inference even on fp32 tensors" ON) 210 | option(USE_VULKAN_RELAXED_PRECISION "Vulkan - Use relaxed precision math in the kernels (mediump)" OFF) 211 | option(USE_VULKAN_SHADERC_RUNTIME "Vulkan - Use runtime shader compilation (needs libshaderc)" OFF) 212 | option(USE_VULKAN_WRAPPER "Vulkan - Dynamically load Vulkan functions" ON) 213 | option(USE_XNNPACK "Use XNNPACK" ON) 214 | option(USE_ZMQ "Use ZMQ" OFF) 215 | option(USE_ZSTD "Use ZSTD" OFF) 216 | cmake_dependent_option( 217 | USE_MKLDNN "Use MKLDNN. Only available on x86 and x86_64." OFF 218 | "CPU_INTEL" OFF) 219 | set(MKLDNN_ENABLE_CONCURRENT_EXEC ${USE_MKLDNN}) 220 | cmake_dependent_option( 221 | USE_MKLDNN_CBLAS "Use CBLAS in MKLDNN" OFF 222 | "USE_MKLDNN" OFF) 223 | option(USE_DISTRIBUTED "Use distributed" ON) 224 | cmake_dependent_option( 225 | USE_MPI "Use MPI for Caffe2. Only available if USE_DISTRIBUTED is on." ON 226 | "USE_DISTRIBUTED" OFF) 227 | cmake_dependent_option( 228 | USE_GLOO "Use Gloo. Only available if USE_DISTRIBUTED is on." ON 229 | "USE_DISTRIBUTED" OFF) 230 | cmake_dependent_option( 231 | USE_TENSORPIPE "Use TensorPipe. Only available if USE_DISTRIBUTED is on." ON 232 | "USE_DISTRIBUTED" OFF) 233 | option(USE_TBB "Use TBB" OFF) 234 | option(ONNX_ML "Enable traditional ONNX ML API." ON) 235 | option(HAVE_SOVERSION "Whether to add SOVERSION to the shared objects" OFF) 236 | 237 | # Since TensorPipe does not support Windows, set it to OFF when WIN32 detected 238 | # On Windows platform, if user does not install libuv in build conda env and 239 | # does not set libuv_ROOT environment variable. Set USE_DISTRIBUTED to OFF. 240 | if(WIN32) 241 | set(USE_TENSORPIPE OFF) 242 | message(WARNING "TensorPipe cannot be used on Windows. Set it to OFF") 243 | 244 | if(USE_DISTRIBUTED AND NOT DEFINED ENV{libuv_ROOT}) 245 | find_library( 246 | libuv_tmp_LIBRARY 247 | NAMES uv libuv 248 | HINTS $ENV{CONDA_PREFIX}\\Library $ENV{PREFIX}\\Library 249 | PATH_SUFFIXES lib 250 | NO_DEFAULT_PATH) 251 | if(NOT libuv_tmp_LIBRARY) 252 | set(USE_DISTRIBUTED OFF) 253 | set(USE_GLOO OFF) 254 | message( 255 | WARNING "Libuv is not installed in current conda env. Set USE_DISTRIBUTED to OFF. " 256 | "Please run command 'conda install -c conda-forge libuv=1.39' to install libuv.") 257 | else() 258 | set(ENV{libuv_ROOT} ${libuv_tmp_LIBRARY}/../../) 259 | endif() 260 | endif() 261 | endif() 262 | 263 | # Linux distributions do not want too many embedded sources, in that sense we 264 | # need to be able to build pytorch with an (almost) empty third_party 265 | # directory. 266 | # USE_SYSTEM_LIBS is a shortcut variable to toggle all the # USE_SYSTEM_* 267 | # variables on. Individual USE_SYSTEM_* variables can be toggled with 268 | # USE_SYSTEM_LIBS being "OFF". 269 | option(USE_SYSTEM_LIBS "Use all available system-provided libraries." OFF) 270 | option(USE_SYSTEM_CPUINFO "Use system-provided cpuinfo." OFF) 271 | option(USE_SYSTEM_SLEEF "Use system-provided sleef." OFF) 272 | option(USE_SYSTEM_GLOO "Use system-provided gloo." OFF) 273 | option(USE_SYSTEM_FP16 "Use system-provided fp16." OFF) 274 | option(USE_SYSTEM_PTHREADPOOL "Use system-provided pthreadpool." OFF) 275 | option(USE_SYSTEM_PSIMD "Use system-provided psimd." OFF) 276 | option(USE_SYSTEM_FXDIV "Use system-provided fxdiv." OFF) 277 | option(USE_SYSTEM_BENCHMARK "Use system-provided google benchmark." OFF) 278 | option(USE_SYSTEM_ONNX "Use system-provided onnx." OFF) 279 | option(USE_SYSTEM_XNNPACK "Use system-provided xnnpack." OFF) 280 | if(USE_SYSTEM_LIBS) 281 | set(USE_SYSTEM_CPUINFO ON) 282 | set(USE_SYSTEM_SLEEF ON) 283 | set(USE_SYSTEM_GLOO ON) 284 | set(BUILD_CUSTOM_PROTOBUF OFF) 285 | set(USE_SYSTEM_EIGEN_INSTALL ON) 286 | set(USE_SYSTEM_FP16 ON) 287 | set(USE_SYSTEM_PTHREADPOOL ON) 288 | set(USE_SYSTEM_PSIMD ON) 289 | set(USE_SYSTEM_FXDIV ON) 290 | set(USE_SYSTEM_BENCHMARK ON) 291 | set(USE_SYSTEM_ONNX ON) 292 | set(USE_SYSTEM_XNNPACK ON) 293 | endif() 294 | 295 | # Used when building Caffe2 through setup.py 296 | option(BUILDING_WITH_TORCH_LIBS "Tell cmake if Caffe2 is being built alongside torch libs" ON) 297 | 298 | # /Z7 override option 299 | # When generating debug symbols, CMake default to use the flag /Zi. 300 | # However, it is not compatible with sccache. So we rewrite it off. 301 | # But some users don't use sccache; this override is for them. 302 | cmake_dependent_option( 303 | MSVC_Z7_OVERRIDE "Work around sccache bug by replacing /Zi and /ZI with /Z7 when using MSVC (if you are not using sccache, you can turn this OFF)" ON 304 | "MSVC" OFF) 305 | 306 | if(NOT USE_SYSTEM_ONNX) 307 | set(ONNX_NAMESPACE "onnx_torch" CACHE STRING "A namespace for ONNX; needed to build with other frameworks that share ONNX.") 308 | elseif() 309 | set(ONNX_NAMESPACE "onnx" CACHE STRING "A namespace for ONNX; needed to build with other frameworks that share ONNX.") 310 | endif() 311 | set(SELECTED_OP_LIST "" CACHE STRING 312 | "Path to the yaml file that contains the list of operators to include for custom build. Include all operators by default.") 313 | set(OP_DEPENDENCY "" CACHE STRING 314 | "Path to the yaml file that contains the op dependency graph for custom build.") 315 | 316 | # This is a fix for a rare build issue on Ubuntu: 317 | # symbol lookup error: miniconda3/envs/pytorch-py3.7/lib/libmkl_intel_lp64.so: undefined symbol: mkl_blas_dsyrk 318 | # https://software.intel.com/en-us/articles/symbol-lookup-error-when-linking-intel-mkl-with-gcc-on-ubuntu 319 | if(LINUX) 320 | set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--no-as-needed ${CMAKE_SHARED_LINKER_FLAGS}") 321 | endif() 322 | 323 | if(MSVC) 324 | foreach(flag_var 325 | CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE 326 | CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO 327 | CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE 328 | CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO) 329 | # Replace /Zi and /ZI with /Z7 330 | if(MSVC_Z7_OVERRIDE) 331 | if(${flag_var} MATCHES "/Z[iI]") 332 | string(REGEX REPLACE "/Z[iI]" "/Z7" ${flag_var} "${${flag_var}}") 333 | endif(${flag_var} MATCHES "/Z[iI]") 334 | endif(MSVC_Z7_OVERRIDE) 335 | # Turn off warnings on Windows. In an ideal world we'd be warning 336 | # clean on Windows too, but this is too much work for our 337 | # non-Windows developers. 338 | # 339 | # NB: Technically, this is not necessary if CMP0092 was applied 340 | # properly, but only cmake >= 3.15 has this policy, so we nail 341 | # it one more time just be safe. 342 | # 343 | # NB2: This is NOT enough to prevent warnings from nvcc on MSVC. At the 344 | # moment only CMP0092 is enough to prevent those warnings too. 345 | string(REPLACE "/W3" "" ${flag_var} "${${flag_var}}") 346 | 347 | # Turn off warnings (Windows build is currently is extremely warning 348 | # unclean and the warnings aren't telling us anything useful.) 349 | string(APPEND ${flag_var} " /w") 350 | 351 | if(${CAFFE2_USE_MSVC_STATIC_RUNTIME}) 352 | if(${flag_var} MATCHES "/MD") 353 | string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}") 354 | endif(${flag_var} MATCHES "/MD") 355 | else() 356 | if(${flag_var} MATCHES "/MT") 357 | string(REGEX REPLACE "/MT" "/MD" ${flag_var} "${${flag_var}}") 358 | endif() 359 | endif() 360 | 361 | # /bigobj increases number of sections in .obj file, which is needed to link 362 | # against libraries in Python 2.7 under Windows 363 | # For Visual Studio generators, if /MP is not added, then we may need 364 | # to add /MP to the flags. 365 | # For other generators like ninja, we don't need to add /MP because it is 366 | # already handled by the generator itself. 367 | if(CMAKE_GENERATOR MATCHES "Visual Studio" AND NOT ${flag_var} MATCHES "/MP") 368 | set(${flag_var} "${${flag_var}} /MP /bigobj") 369 | else() 370 | set(${flag_var} "${${flag_var}} /bigobj") 371 | endif() 372 | endforeach(flag_var) 373 | 374 | foreach(flag_var 375 | CMAKE_C_FLAGS CMAKE_C_FLAGS_RELEASE CMAKE_C_FLAGS_MINSIZEREL 376 | CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_MINSIZEREL) 377 | if(${flag_var} MATCHES "/Z[iI7]") 378 | string(REGEX REPLACE "/Z[iI7]" "" ${flag_var} "${${flag_var}}") 379 | endif() 380 | endforeach(flag_var) 381 | 382 | foreach(flag_var 383 | CMAKE_SHARED_LINKER_FLAGS_DEBUG CMAKE_STATIC_LINKER_FLAGS_DEBUG 384 | CMAKE_EXE_LINKER_FLAGS_DEBUG CMAKE_MODULE_LINKER_FLAGS_DEBUG) 385 | # Switch off incremental linking in debug builds 386 | if(${flag_var} MATCHES "/INCREMENTAL" AND NOT ${flag_var} MATCHES "/INCREMENTAL:NO") 387 | string(REGEX REPLACE "/INCREMENTAL" "/INCREMENTAL:NO" ${flag_var} "${${flag_var}}") 388 | endif() 389 | endforeach(flag_var) 390 | 391 | foreach(flag_var 392 | CMAKE_SHARED_LINKER_FLAGS CMAKE_STATIC_LINKER_FLAGS 393 | CMAKE_EXE_LINKER_FLAGS CMAKE_MODULE_LINKER_FLAGS) 394 | string(APPEND ${flag_var} " /ignore:4049 /ignore:4217") 395 | endforeach(flag_var) 396 | 397 | # Try harder 398 | list(APPEND CUDA_NVCC_FLAGS "-Xcompiler" "/w" "-w") 399 | endif(MSVC) 400 | 401 | list(APPEND CUDA_NVCC_FLAGS "-Xfatbin" "-compress-all") 402 | list(APPEND CUDA_NVCC_FLAGS_DEBUG "-Xfatbin" "-compress-all") 403 | list(APPEND CUDA_NVCC_FLAGS_RELWITHDEBINFO "-Xfatbin" "-compress-all") 404 | 405 | if(NOT MSVC) 406 | list(APPEND CUDA_NVCC_FLAGS_DEBUG "-g" "-lineinfo" "--source-in-ptx") 407 | list(APPEND CUDA_NVCC_FLAGS_RELWITHDEBINFO "-g" "-lineinfo" "--source-in-ptx") 408 | endif(NOT MSVC) 409 | 410 | # Set INTERN_BUILD_MOBILE for all mobile builds. Components that are not 411 | # applicable to mobile are disabled by this variable. 412 | # Setting `BUILD_PYTORCH_MOBILE_WITH_HOST_TOOLCHAIN` environment variable can 413 | # force it to do mobile build with host toolchain - which is useful for testing 414 | # purpose. 415 | if(ANDROID OR IOS OR DEFINED ENV{BUILD_PYTORCH_MOBILE_WITH_HOST_TOOLCHAIN}) 416 | set(INTERN_BUILD_MOBILE ON) 417 | 418 | if(DEFINED ENV{BUILD_PYTORCH_MOBILE_WITH_HOST_TOOLCHAIN}) 419 | # C10_MOBILE is derived from Android/iOS toolchain macros in 420 | # c10/macros/Macros.h, so it needs to be explicitly set here. 421 | string(APPEND CMAKE_CXX_FLAGS " -DC10_MOBILE") 422 | endif() 423 | endif() 424 | 425 | # INTERN_BUILD_ATEN_OPS is used to control whether to build ATen/TH operators. 426 | # It's disabled for caffe2 mobile library. 427 | if(INTERN_BUILD_MOBILE AND BUILD_CAFFE2_MOBILE) 428 | set(INTERN_BUILD_ATEN_OPS OFF) 429 | else() 430 | set(INTERN_BUILD_ATEN_OPS ON) 431 | endif() 432 | 433 | # BUILD_CAFFE2_MOBILE is the master switch to choose between libcaffe2 v.s. libtorch mobile build. 434 | # When it's enabled it builds original libcaffe2 mobile library without ATen/TH ops nor TorchScript support; 435 | # When it's disabled it builds libtorch mobile library, which contains ATen/TH ops and native support for 436 | # TorchScript model, but doesn't contain not-yet-unified caffe2 ops; 437 | if(INTERN_BUILD_MOBILE AND NOT BUILD_CAFFE2_MOBILE) 438 | if(NOT BUILD_SHARED_LIBS AND NOT "${SELECTED_OP_LIST}" STREQUAL "") 439 | string(APPEND CMAKE_CXX_FLAGS " -DNO_EXPORT") 440 | endif() 441 | if(BUILD_MOBILE_AUTOGRAD) 442 | set(INTERN_DISABLE_AUTOGRAD OFF) 443 | else() 444 | set(INTERN_DISABLE_AUTOGRAD ON) 445 | endif() 446 | set(BUILD_PYTHON OFF) 447 | set(BUILD_CAFFE2_OPS OFF) 448 | set(USE_DISTRIBUTED OFF) 449 | set(FEATURE_TORCH_MOBILE ON) 450 | set(NO_API ON) 451 | set(USE_FBGEMM OFF) 452 | set(USE_QNNPACK OFF) 453 | set(INTERN_DISABLE_ONNX ON) 454 | set(INTERN_USE_EIGEN_BLAS ON) 455 | # Disable developing mobile interpreter for actual mobile build. 456 | # Enable it elsewhere to capture build error. 457 | set(INTERN_DISABLE_MOBILE_INTERP ON) 458 | endif() 459 | 460 | # ---[ Utils 461 | include(cmake/public/utils.cmake) 462 | 463 | # ---[ Version numbers for generated libraries 464 | file(READ version.txt TORCH_DEFAULT_VERSION) 465 | # Strip trailing newline 466 | string(REGEX REPLACE "\n$" "" TORCH_DEFAULT_VERSION "${TORCH_DEFAULT_VERSION}") 467 | if("${TORCH_DEFAULT_VERSION} " STREQUAL " ") 468 | message(WARNING "Could not get version from base 'version.txt'") 469 | # If we can't get the version from the version file we should probably 470 | # set it to something non-sensical like 0.0.0 471 | set(TORCH_DEFAULT_VERSION, "0.0.0") 472 | endif() 473 | set(TORCH_BUILD_VERSION "${TORCH_DEFAULT_VERSION}" CACHE STRING "Torch build version") 474 | if(DEFINED ENV{PYTORCH_BUILD_VERSION}) 475 | set(TORCH_BUILD_VERSION "$ENV{PYTORCH_BUILD_VERSION}" 476 | CACHE STRING "Torch build version" FORCE) 477 | endif() 478 | if(NOT TORCH_BUILD_VERSION) 479 | # An empty string was specified so force version to the default 480 | set(TORCH_BUILD_VERSION "${TORCH_DEFAULT_VERSION}" 481 | CACHE STRING "Torch build version" FORCE) 482 | endif() 483 | caffe2_parse_version_str(TORCH ${TORCH_BUILD_VERSION}) 484 | caffe2_parse_version_str(CAFFE2 ${TORCH_BUILD_VERSION}) 485 | set(TORCH_SOVERSION "${TORCH_VERSION_MAJOR}.${TORCH_VERSION_MINOR}") 486 | 487 | # ---[ CMake scripts + modules 488 | list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/Modules) 489 | 490 | # ---[ CMake build directories 491 | set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) 492 | set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) 493 | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) 494 | 495 | enable_testing() 496 | 497 | # ---[ Build variables set within the cmake tree 498 | include(cmake/BuildVariables.cmake) 499 | set(CAFFE2_ALLOWLIST "" CACHE STRING "A allowlist file of files that one should build.") 500 | 501 | # Set default build type 502 | if(NOT CMAKE_BUILD_TYPE) 503 | message(STATUS "Build type not set - defaulting to Release") 504 | set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build from: Debug Release RelWithDebInfo MinSizeRel Coverage." FORCE) 505 | endif() 506 | 507 | # ---[ Misc checks to cope with various compiler modes 508 | include(cmake/MiscCheck.cmake) 509 | 510 | # External projects 511 | include(ExternalProject) 512 | 513 | # ---[ Dependencies 514 | # ---[ FBGEMM doesn't work on x86 32bit and CMAKE_SYSTEM_PROCESSOR thinks its 64bit 515 | if(USE_FBGEMM AND ((CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64" AND CMAKE_SIZEOF_VOID_P EQUAL 4) OR CMAKE_SYSTEM_PROCESSOR STREQUAL "x86")) 516 | set(USE_FBGEMM OFF) 517 | endif() 518 | 519 | if(USE_KINETO AND INTERN_BUILD_MOBILE) 520 | message(STATUS "Not using libkineto in a mobile build.") 521 | set(USE_KINETO OFF) 522 | endif() 523 | 524 | if(USE_KINETO AND (NOT USE_CUDA)) 525 | message(STATUS "Not using libkineto in a non-CUDA build.") 526 | set(USE_KINETO OFF) 527 | endif() 528 | 529 | if(USE_KINETO AND MSVC) 530 | message(STATUS "Not using libkineto in a Windows build.") 531 | set(USE_KINETO OFF) 532 | endif() 533 | 534 | include(cmake/Dependencies.cmake) 535 | 536 | if(USE_FBGEMM) 537 | string(APPEND CMAKE_CXX_FLAGS " -DUSE_FBGEMM") 538 | endif() 539 | 540 | if(USE_KINETO) 541 | string(APPEND CMAKE_CXX_FLAGS " -DUSE_KINETO") 542 | endif() 543 | 544 | if(USE_QNNPACK) 545 | string(APPEND CMAKE_CXX_FLAGS " -DUSE_QNNPACK") 546 | endif() 547 | 548 | if(USE_PYTORCH_QNNPACK) 549 | string(APPEND CMAKE_CXX_FLAGS " -DUSE_PYTORCH_QNNPACK") 550 | endif() 551 | 552 | if(USE_XNNPACK) 553 | string(APPEND CMAKE_CXX_FLAGS " -DUSE_XNNPACK") 554 | endif() 555 | 556 | if(USE_VULKAN) 557 | string(APPEND CMAKE_CXX_FLAGS " -DUSE_VULKAN") 558 | string(APPEND CMAKE_CXX_FLAGS " -DUSE_VULKAN_API") 559 | 560 | if(USE_VULKAN_FP16_INFERENCE) 561 | string(APPEND CMAKE_CXX_FLAGS " -DUSE_VULKAN_FP16_INFERENCE") 562 | endif() 563 | 564 | if(USE_VULKAN_RELAXED_PRECISION) 565 | string(APPEND CMAKE_CXX_FLAGS " -DUSE_VULKAN_RELAXED_PRECISION") 566 | endif() 567 | 568 | if(USE_VULKAN_SHADERC_RUNTIME) 569 | string(APPEND CMAKE_CXX_FLAGS " -DUSE_VULKAN_SHADERC_RUNTIME") 570 | endif() 571 | 572 | if(USE_VULKAN_WRAPPER) 573 | string(APPEND CMAKE_CXX_FLAGS " -DUSE_VULKAN_WRAPPER") 574 | endif() 575 | endif() 576 | 577 | if(USE_PYTORCH_METAL) 578 | string(APPEND CMAKE_CXX_FLAGS " -DUSE_PYTORCH_METAL") 579 | endif() 580 | 581 | # ---[ Allowlist file if allowlist is specified 582 | include(cmake/Allowlist.cmake) 583 | 584 | # ---[ Set link flag, handle additional deps for gcc 4.8 and above 585 | if(CMAKE_COMPILER_IS_GNUCXX AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.8.0 AND NOT ANDROID) 586 | message(STATUS "GCC ${CMAKE_CXX_COMPILER_VERSION}: Adding gcc and gcc_s libs to link line") 587 | list(APPEND Caffe2_DEPENDENCY_LIBS gcc_s gcc) 588 | endif() 589 | 590 | # ---[ Build flags 591 | set(CMAKE_C_STANDARD 11) 592 | set(CMAKE_CXX_STANDARD 14) 593 | if(NOT MSVC) 594 | string(APPEND CMAKE_CXX_FLAGS " -O2 -fPIC") 595 | string(APPEND CMAKE_CXX_FLAGS " -Wno-narrowing") 596 | # Eigen fails to build with some versions, so convert this to a warning 597 | # Details at http://eigen.tuxfamily.org/bz/show_bug.cgi?id=1459 598 | string(APPEND CMAKE_CXX_FLAGS " -Wall") 599 | string(APPEND CMAKE_CXX_FLAGS " -Wextra") 600 | string(APPEND CMAKE_CXX_FLAGS " -Werror=return-type") 601 | string(APPEND CMAKE_CXX_FLAGS " -Wno-missing-field-initializers") 602 | string(APPEND CMAKE_CXX_FLAGS " -Wno-type-limits") 603 | string(APPEND CMAKE_CXX_FLAGS " -Wno-array-bounds") 604 | string(APPEND CMAKE_CXX_FLAGS " -Wno-unknown-pragmas") 605 | string(APPEND CMAKE_CXX_FLAGS " -Wno-sign-compare") 606 | string(APPEND CMAKE_CXX_FLAGS " -Wno-unused-parameter") 607 | string(APPEND CMAKE_CXX_FLAGS " -Wno-unused-variable") 608 | string(APPEND CMAKE_CXX_FLAGS " -Wno-unused-function") 609 | string(APPEND CMAKE_CXX_FLAGS " -Wno-unused-result") 610 | string(APPEND CMAKE_CXX_FLAGS " -Wno-unused-local-typedefs") 611 | string(APPEND CMAKE_CXX_FLAGS " -Wno-strict-overflow") 612 | string(APPEND CMAKE_CXX_FLAGS " -Wno-strict-aliasing") 613 | string(APPEND CMAKE_CXX_FLAGS " -Wno-error=deprecated-declarations") 614 | if(CMAKE_COMPILER_IS_GNUCXX AND NOT (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 7.0.0)) 615 | string(APPEND CMAKE_CXX_FLAGS " -Wno-stringop-overflow") 616 | endif() 617 | if(CMAKE_COMPILER_IS_GNUCXX) 618 | # Suppress "The ABI for passing parameters with 64-byte alignment has changed in GCC 4.6" 619 | string(APPEND CMAKE_CXX_FLAGS " -Wno-psabi") 620 | endif() 621 | string(APPEND CMAKE_CXX_FLAGS " -Wno-error=pedantic") 622 | string(APPEND CMAKE_CXX_FLAGS " -Wno-error=redundant-decls") 623 | string(APPEND CMAKE_CXX_FLAGS " -Wno-error=old-style-cast") 624 | # These flags are not available in GCC-4.8.5. Set only when using clang. 625 | # Compared against https://gcc.gnu.org/onlinedocs/gcc-4.8.5/gcc/Option-Summary.html 626 | if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") 627 | string(APPEND CMAKE_CXX_FLAGS " -Wno-invalid-partial-specialization") 628 | string(APPEND CMAKE_CXX_FLAGS " -Wno-typedef-redefinition") 629 | string(APPEND CMAKE_CXX_FLAGS " -Wno-unknown-warning-option") 630 | string(APPEND CMAKE_CXX_FLAGS " -Wno-unused-private-field") 631 | string(APPEND CMAKE_CXX_FLAGS " -Wno-inconsistent-missing-override") 632 | string(APPEND CMAKE_CXX_FLAGS " -Wno-aligned-allocation-unavailable") 633 | string(APPEND CMAKE_CXX_FLAGS " -Wno-c++14-extensions") 634 | string(APPEND CMAKE_CXX_FLAGS " -Wno-constexpr-not-const") 635 | string(APPEND CMAKE_CXX_FLAGS " -Wno-missing-braces") 636 | string(APPEND CMAKE_CXX_FLAGS " -Qunused-arguments") 637 | if(${COLORIZE_OUTPUT}) 638 | string(APPEND CMAKE_CXX_FLAGS " -fcolor-diagnostics") 639 | endif() 640 | endif() 641 | if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.9) 642 | if(${COLORIZE_OUTPUT}) 643 | string(APPEND CMAKE_CXX_FLAGS " -fdiagnostics-color=always") 644 | endif() 645 | endif() 646 | if((APPLE AND (NOT ("${CLANG_VERSION_STRING}" VERSION_LESS "9.0"))) 647 | OR(CMAKE_COMPILER_IS_GNUCXX 648 | AND(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 7.0 AND NOT APPLE))) 649 | string(APPEND CMAKE_CXX_FLAGS " -faligned-new") 650 | endif() 651 | if(WERROR) 652 | check_cxx_compiler_flag("-Werror" COMPILER_SUPPORT_WERROR) 653 | if(NOT COMPILER_SUPPORT_WERROR) 654 | set(WERROR FALSE) 655 | else() 656 | string(APPEND CMAKE_CXX_FLAGS " -Werror") 657 | endif() 658 | endif(WERROR) 659 | if(NOT APPLE) 660 | string(APPEND CMAKE_CXX_FLAGS " -Wno-unused-but-set-variable") 661 | string(APPEND CMAKE_CXX_FLAGS " -Wno-maybe-uninitialized") 662 | endif() 663 | string(APPEND CMAKE_CXX_FLAGS_DEBUG " -fno-omit-frame-pointer -O0") 664 | string(APPEND CMAKE_LINKER_FLAGS_DEBUG " -fno-omit-frame-pointer -O0") 665 | string(APPEND CMAKE_CXX_FLAGS " -fno-math-errno") 666 | string(APPEND CMAKE_CXX_FLAGS " -fno-trapping-math") 667 | check_cxx_compiler_flag("-Werror=format" HAS_WERROR_FORMAT) 668 | if(HAS_WERROR_FORMAT) 669 | string(APPEND CMAKE_CXX_FLAGS " -Werror=format") 670 | endif() 671 | check_cxx_compiler_flag("-Werror=cast-function-type" HAS_WERROR_CAST_FUNCTION_TYPE) 672 | if(HAS_WERROR_CAST_FUNCTION_TYPE) 673 | string(APPEND CMAKE_CXX_FLAGS " -Werror=cast-function-type") 674 | endif() 675 | endif() 676 | 677 | if(USE_ASAN) 678 | string(APPEND CMAKE_CXX_FLAGS_DEBUG " -fsanitize=address") 679 | string(APPEND CMAKE_LINKER_FLAGS_DEBUG " -fsanitize=address") 680 | endif() 681 | 682 | if(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64") 683 | include(CheckCSourceCompiles) 684 | check_c_source_compiles("#include 685 | int main() { 686 | float a[] = {1.0, 1.0}; 687 | float32x4x2_t v; 688 | v.val[0] = vcombine_f32 (vcreate_f32 (0UL), vcreate_f32 (0UL)); 689 | v.val[1] = vcombine_f32 (vcreate_f32 (0UL), vcreate_f32 (0UL)); 690 | vst1q_f32_x2(a, v); 691 | return 0; 692 | }" HAS_VST1) 693 | 694 | if(NOT HAS_VST1) 695 | string(APPEND CMAKE_CXX_FLAGS " -DMISSING_ARM_VST1") 696 | endif() 697 | endif() 698 | 699 | if(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64") 700 | include(CheckCSourceCompiles) 701 | check_c_source_compiles("#include 702 | int main() { 703 | float a[] = {1.0, 1.0}; 704 | vld1q_f32_x2(a); 705 | return 0; 706 | }" HAS_VLD1) 707 | 708 | if(NOT HAS_VLD1) 709 | string(APPEND CMAKE_CXX_FLAGS " -DMISSING_ARM_VLD1") 710 | endif() 711 | endif() 712 | 713 | 714 | # Add code coverage flags to supported compilers 715 | if(USE_CPP_CODE_COVERAGE) 716 | if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU") 717 | string(APPEND CMAKE_C_FLAGS " --coverage -fprofile-abs-path") 718 | string(APPEND CMAKE_CXX_FLAGS " --coverage -fprofile-abs-path") 719 | elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") 720 | string(APPEND CMAKE_C_FLAGS " -fprofile-instr-generate -fcoverage-mapping") 721 | string(APPEND CMAKE_CXX_FLAGS " -fprofile-instr-generate -fcoverage-mapping") 722 | else() 723 | message(ERROR "Code coverage for compiler ${CMAKE_CXX_COMPILER_ID} is unsupported") 724 | endif() 725 | 726 | endif() 727 | 728 | if(APPLE) 729 | string(APPEND CMAKE_CXX_FLAGS " -Wno-unused-private-field") 730 | string(APPEND CMAKE_CXX_FLAGS " -Wno-missing-braces") 731 | string(APPEND CMAKE_CXX_FLAGS " -Wno-c++14-extensions") 732 | string(APPEND CMAKE_CXX_FLAGS " -Wno-constexpr-not-const") 733 | endif() 734 | 735 | if(EMSCRIPTEN) 736 | string(APPEND CMAKE_CXX_FLAGS " -Wno-implicit-function-declaration -DEMSCRIPTEN -s DISABLE_EXCEPTION_CATCHING=0") 737 | endif() 738 | 739 | if(CMAKE_COMPILER_IS_GNUCXX AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 7.0.0) 740 | string(APPEND CMAKE_CXX_FLAGS " -Wno-stringop-overflow") 741 | endif() 742 | 743 | if(ANDROID AND (NOT ANDROID_DEBUG_SYMBOLS)) 744 | if(CMAKE_COMPILER_IS_GNUCXX) 745 | string(APPEND CMAKE_CXX_FLAGS " -s") 746 | elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") 747 | string(APPEND CMAKE_CXX_FLAGS " -g0") 748 | else() 749 | string(APPEND CMAKE_EXE_LINKER_FLAGS " -s") 750 | endif() 751 | endif() 752 | 753 | if(NOT APPLE AND UNIX) 754 | list(APPEND Caffe2_DEPENDENCY_LIBS dl) 755 | endif() 756 | 757 | # Prefix path to Caffe2 headers. 758 | # If a directory containing installed Caffe2 headers was inadvertently 759 | # added to the list of include directories, prefixing 760 | # PROJECT_SOURCE_DIR means this source tree always takes precedence. 761 | include_directories(BEFORE ${PROJECT_SOURCE_DIR}) 762 | 763 | # Prefix path to generated Caffe2 headers. 764 | # These need to take precedence over their empty counterparts located 765 | # in PROJECT_SOURCE_DIR. 766 | include_directories(BEFORE ${PROJECT_BINARY_DIR}) 767 | 768 | include_directories(BEFORE ${PROJECT_SOURCE_DIR}/aten/src/) 769 | include_directories(BEFORE ${PROJECT_BINARY_DIR}/aten/src/) 770 | 771 | # ---[ Main build 772 | add_subdirectory(c10) 773 | add_subdirectory(caffe2) 774 | 775 | # --[ Documentation 776 | if(BUILD_DOCS) 777 | # check if Doxygen is installed 778 | find_package(Doxygen) 779 | if(DOXYGEN_FOUND) 780 | message("Generating documentation") 781 | 782 | set(DOXYGEN_C_IN ${CMAKE_CURRENT_SOURCE_DIR}/docs/caffe2/.Doxyfile-c) 783 | set(DOXYGEN_C_OUT ${CMAKE_CURRENT_SOURCE_DIR}/docs/caffe2/Doxyfile-c) 784 | set(DOXYGEN_P_IN ${CMAKE_CURRENT_SOURCE_DIR}/docs/caffe2/.Doxyfile-python) 785 | set(DOXYGEN_P_OUT ${CMAKE_CURRENT_SOURCE_DIR}/docs/caffe2/Doxyfile-python) 786 | 787 | if(EXISTS ${CMAKE_CURRENT_BINARY_DIR}/docs) 788 | file(REMOVE_RECURSE ${CMAKE_CURRENT_BINARY_DIR}/docs) 789 | endif() 790 | 791 | file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/docs) 792 | configure_file(${DOXYGEN_C_IN} ${DOXYGEN_C_OUT} @ONLY) 793 | configure_file(${DOXYGEN_P_IN} ${DOXYGEN_P_OUT} @ONLY) 794 | 795 | add_custom_target(doc_doxygen_c ALL 796 | COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYGEN_C_OUT} 797 | WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} 798 | COMMENT "Generating C++ API documentation with Doxygen" 799 | VERBATIM) 800 | 801 | add_custom_target(doc_doxygen_python ALL 802 | COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYGEN_P_OUT} 803 | WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} 804 | COMMENT "Generating Python API documentation with Doxygen" 805 | VERBATIM) 806 | else() 807 | message(FATAL_ERROR "Doxygen needs to be installed to generate the documentation") 808 | endif() 809 | endif() 810 | 811 | # ---[ CMake related files 812 | # Uninistall option. 813 | if(NOT TARGET caffe2_uninstall) 814 | configure_file( 815 | ${CMAKE_CURRENT_SOURCE_DIR}/cmake/cmake_uninstall.cmake.in 816 | ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake 817 | IMMEDIATE @ONLY) 818 | 819 | add_custom_target(caffe2_uninstall 820 | COMMAND ${CMAKE_COMMAND} -P 821 | ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake) 822 | endif() 823 | 824 | # ---[ Make configuration files for cmake to allow dependent libraries 825 | # easier access to Caffe2. 826 | 827 | if((NOT USE_GLOG) OR (NOT USE_GFLAGS) OR BUILD_CUSTOM_PROTOBUF) 828 | message(WARNING 829 | "Generated cmake files are only fully tested if one builds " 830 | "with system glog, gflags, and protobuf. Other settings may " 831 | "generate files that are not well tested.") 832 | endif() 833 | 834 | if(USE_CUDA OR USE_ROCM) 835 | # TODO: check if we should include other cuda dependency libraries 836 | # to the interface as well. 837 | 838 | endif() 839 | 840 | # Note(jiayq): when building static libraries, all PRIVATE dependencies 841 | # will also become interface libraries, and as a result if there are any 842 | # dependency libraries that are not exported, the following install export 843 | # script will fail. As a result, we will only provide the targets cmake 844 | # files for shared lib installation. For more info, read: 845 | # https://cmake.org/pipermail/cmake/2016-May/063400.html 846 | if(BUILD_SHARED_LIBS) 847 | configure_file( 848 | ${PROJECT_SOURCE_DIR}/cmake/Caffe2ConfigVersion.cmake.in 849 | ${PROJECT_BINARY_DIR}/Caffe2ConfigVersion.cmake 850 | @ONLY) 851 | configure_file( 852 | ${PROJECT_SOURCE_DIR}/cmake/Caffe2Config.cmake.in 853 | ${PROJECT_BINARY_DIR}/Caffe2Config.cmake 854 | @ONLY) 855 | install(FILES 856 | ${PROJECT_BINARY_DIR}/Caffe2ConfigVersion.cmake 857 | ${PROJECT_BINARY_DIR}/Caffe2Config.cmake 858 | DESTINATION share/cmake/Caffe2 859 | COMPONENT dev) 860 | install(FILES 861 | ${PROJECT_SOURCE_DIR}/cmake/public/cuda.cmake 862 | ${PROJECT_SOURCE_DIR}/cmake/public/glog.cmake 863 | ${PROJECT_SOURCE_DIR}/cmake/public/gflags.cmake 864 | ${PROJECT_SOURCE_DIR}/cmake/public/mkl.cmake 865 | ${PROJECT_SOURCE_DIR}/cmake/public/mkldnn.cmake 866 | ${PROJECT_SOURCE_DIR}/cmake/public/protobuf.cmake 867 | ${PROJECT_SOURCE_DIR}/cmake/public/threads.cmake 868 | ${PROJECT_SOURCE_DIR}/cmake/public/utils.cmake 869 | DESTINATION share/cmake/Caffe2/public 870 | COMPONENT dev) 871 | install(DIRECTORY 872 | ${PROJECT_SOURCE_DIR}/cmake/Modules_CUDA_fix 873 | DESTINATION share/cmake/Caffe2/ 874 | COMPONENT dev) 875 | 876 | install(EXPORT Caffe2Targets DESTINATION share/cmake/Caffe2 877 | FILE Caffe2Targets.cmake 878 | COMPONENT dev) 879 | else() 880 | message(WARNING 881 | "Generated cmake files are only available when building " 882 | "shared libs.") 883 | endif() 884 | 885 | # ---[ Modules 886 | # If master flag for buildling Caffe2 is disabled, we also disable the 887 | # build for Caffe2 related operator modules. 888 | if(BUILD_CAFFE2) 889 | add_subdirectory(modules) 890 | endif() 891 | 892 | # ---[ Binaries 893 | # Binaries will be built after the Caffe2 main libraries and the modules 894 | # are built. For the binaries, they will be linked to the Caffe2 main 895 | # libraries, as well as all the modules that are built with Caffe2 (the ones 896 | # built in the previous Modules section above). 897 | if(BUILD_BINARY) 898 | add_subdirectory(binaries) 899 | endif() 900 | 901 | # ---[ JNI 902 | if(BUILD_JNI) 903 | set(BUILD_LIBTORCH_WITH_JNI 1) 904 | set(FBJNI_SKIP_TESTS 1) 905 | add_subdirectory(android/pytorch_android) 906 | endif() 907 | 908 | include(cmake/Summary.cmake) 909 | caffe2_print_configuration_summary() 910 | -------------------------------------------------------------------------------- /old/FindOpenBLAS.cmake: -------------------------------------------------------------------------------- 1 | 2 | 3 | SET(Open_BLAS_INCLUDE_SEARCH_PATHS 4 | /usr/include 5 | /usr/include/openblas 6 | /usr/include/openblas-base 7 | /usr/local/include 8 | /usr/local/include/openblas 9 | /usr/local/include/openblas-base 10 | /usr/local/opt/openblas/include 11 | /opt/OpenBLAS/include 12 | /opt/homebrew/opt/openblas/include 13 | $ENV{OpenBLAS_HOME} 14 | $ENV{OpenBLAS_HOME}/include 15 | ) 16 | 17 | SET(Open_BLAS_LIB_SEARCH_PATHS 18 | /lib/ 19 | /lib/openblas-base 20 | /lib64/ 21 | /usr/lib 22 | /usr/lib/openblas-base 23 | /usr/lib64 24 | /usr/local/lib 25 | /usr/local/lib64 26 | /usr/local/opt/openblas/lib 27 | /opt/OpenBLAS/lib 28 | /opt/homebrew/opt/openblas/lib 29 | $ENV{OpenBLAS} 30 | $ENV{OpenBLAS}/lib 31 | $ENV{OpenBLAS_HOME} 32 | $ENV{OpenBLAS_HOME}/lib 33 | ) 34 | 35 | FIND_PATH(OpenBLAS_INCLUDE_DIR NAMES cblas.h PATHS ${Open_BLAS_INCLUDE_SEARCH_PATHS}) 36 | FIND_LIBRARY(OpenBLAS_LIB NAMES openblas PATHS ${Open_BLAS_LIB_SEARCH_PATHS}) 37 | 38 | SET(OpenBLAS_FOUND ON) 39 | 40 | # Check include files 41 | IF(NOT OpenBLAS_INCLUDE_DIR) 42 | SET(OpenBLAS_FOUND OFF) 43 | MESSAGE(STATUS "Could not find OpenBLAS include. Turning OpenBLAS_FOUND off") 44 | ENDIF() 45 | 46 | # Check libraries 47 | IF(NOT OpenBLAS_LIB) 48 | SET(OpenBLAS_FOUND OFF) 49 | MESSAGE(STATUS "Could not find OpenBLAS lib. Turning OpenBLAS_FOUND off") 50 | ENDIF() 51 | 52 | IF (OpenBLAS_FOUND) 53 | IF (NOT OpenBLAS_FIND_QUIETLY) 54 | MESSAGE(STATUS "Found OpenBLAS libraries: ${OpenBLAS_LIB}") 55 | MESSAGE(STATUS "Found OpenBLAS include: ${OpenBLAS_INCLUDE_DIR}") 56 | ENDIF (NOT OpenBLAS_FIND_QUIETLY) 57 | ELSE (OpenBLAS_FOUND) 58 | IF (OpenBLAS_FIND_REQUIRED) 59 | MESSAGE(FATAL_ERROR "Could not find OpenBLAS") 60 | ENDIF (OpenBLAS_FIND_REQUIRED) 61 | ENDIF (OpenBLAS_FOUND) 62 | 63 | MARK_AS_ADVANCED( 64 | OpenBLAS_INCLUDE_DIR 65 | OpenBLAS_LIB 66 | OpenBLAS 67 | ) 68 | 69 | -------------------------------------------------------------------------------- /old/FindvecLib.cmake: -------------------------------------------------------------------------------- 1 | # Find the vecLib libraries as part of Accelerate.framework or as standalone framework 2 | # 3 | # The following are set after configuration is done: 4 | # VECLIB_FOUND 5 | # vecLib_INCLUDE_DIR 6 | # vecLib_LINKER_LIBS 7 | 8 | 9 | if(NOT APPLE) 10 | return() 11 | endif() 12 | 13 | set(__veclib_include_suffix "Frameworks/vecLib.framework/Versions/Current/Headers") 14 | 15 | find_path(vecLib_INCLUDE_DIR vecLib.h 16 | DOC "vecLib include directory" 17 | PATHS /System/Library/Frameworks/Accelerate.framework/Versions/Current/${__veclib_include_suffix} 18 | /System/Library/${__veclib_include_suffix} 19 | /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Accelerate.framework/Versions/Current/Frameworks/vecLib.framework/Headers/ 20 | NO_DEFAULT_PATH) 21 | 22 | include(FindPackageHandleStandardArgs) 23 | find_package_handle_standard_args(vecLib DEFAULT_MSG vecLib_INCLUDE_DIR) 24 | 25 | if(VECLIB_FOUND) 26 | if(vecLib_INCLUDE_DIR MATCHES "^/System/Library/Frameworks/vecLib.framework.*") 27 | set(vecLib_LINKER_LIBS -lcblas "-framework vecLib") 28 | message(STATUS "Found standalone vecLib.framework") 29 | else() 30 | set(vecLib_LINKER_LIBS -lcblas "-framework Accelerate") 31 | message(STATUS "Found vecLib as part of Accelerate.framework") 32 | endif() 33 | 34 | mark_as_advanced(vecLib_INCLUDE_DIR) 35 | endif() 36 | -------------------------------------------------------------------------------- /old/README.md: -------------------------------------------------------------------------------- 1 | # PyTorch Build for Apple Silicon 2 | 3 | **NOTE**: PyTorch supports Apple Silicon now (you can install with pip) 4 | 5 | This document is useful only for scratch build from source code for Apple Silicon. 6 | 7 | ## Work-flow 8 | 9 | - install prerequisites 10 | - install pytorch through a wheel-build 11 | 12 | ## Test Environment 13 | 14 | - Python 3.8.2 (Pre-installed) 15 | - venv 16 | - macOS Big Sur (ver.11.1) 17 | 18 | ## Prerequisite Installation 19 | 20 | - homebrew 21 | 22 | **NOTE Outdated**: homebrew supports Apple Silicon now 23 | 24 | https://zenn.dev/ress/articles/069baf1c305523dfca3d 25 | 26 | - OpenBLAS for Apple Silicon 27 | 28 | This is based on above setting. Currently OpenBLAS supports M1. 29 | ``` 30 | $=brew install openblas 31 | ``` 32 | ## Download Source-code of PyTorch 33 | ``` 34 | git clone --recursive https://github.com/pytorch/pytorch 35 | ``` 36 | Enter to the directory; 37 | ``` 38 | $cd pytorch 39 | ``` 40 | Paste three files to the pytoch directory 41 | 42 | **NOTE**: if you want OpenCV setting then you need to set "OK" in the cmake file. 43 | 44 | ## Build 45 | 46 | ### - Compilation 47 | ``` 48 | $python3 setup.py build 49 | ``` 50 | 51 | ### - Build Wheel 52 | ``` 53 | $python3 setup.py bdist bdist_wheel 54 | ``` 55 | ## install 56 | 57 | You can use **pip install** for the wheel file. --------------------------------------------------------------------------------