"Fossies" - the Fresh Open Source Software Archive

Member "pytorch-1.8.2/tools/build_pytorch_libs.py" (23 Jul 2021, 2470 Bytes) of package /linux/misc/pytorch-1.8.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "build_pytorch_libs.py" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 1.12.1_vs_1.13.0.

    1 import os
    2 from glob import glob
    3 import shutil
    4 
    5 from .setup_helpers.env import IS_64BIT, IS_WINDOWS, check_negative_env_flag
    6 from .setup_helpers.cmake import USE_NINJA
    7 
    8 
    9 def _overlay_windows_vcvars(env):
   10     from distutils._msvccompiler import _get_vc_env
   11     vc_arch = 'x64' if IS_64BIT else 'x86'
   12     vc_env = _get_vc_env(vc_arch)
   13     # Keys in `_get_vc_env` are always lowercase.
   14     # We turn them into uppercase before overlaying vcvars
   15     # because OS environ keys are always uppercase on Windows.
   16     # https://stackoverflow.com/a/7797329
   17     vc_env = {k.upper(): v for k, v in vc_env.items()}
   18     for k, v in env.items():
   19         uk = k.upper()
   20         if uk not in vc_env:
   21             vc_env[uk] = v
   22     return vc_env
   23 
   24 
   25 def _create_build_env():
   26     # XXX - our cmake file sometimes looks at the system environment
   27     # and not cmake flags!
   28     # you should NEVER add something to this list. It is bad practice to
   29     # have cmake read the environment
   30     my_env = os.environ.copy()
   31     if 'CUDA_HOME' in my_env:  # Keep CUDA_HOME. This env variable is still used in other part.
   32         my_env['CUDA_BIN_PATH'] = my_env['CUDA_HOME']
   33     elif IS_WINDOWS:  # we should eventually make this as part of FindCUDA.
   34         cuda_win = glob('C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v*.*')
   35         if len(cuda_win) > 0:
   36             my_env['CUDA_BIN_PATH'] = cuda_win[0]
   37 
   38     if IS_WINDOWS and USE_NINJA:
   39         # When using Ninja under Windows, the gcc toolchain will be chosen as
   40         # default. But it should be set to MSVC as the user's first choice.
   41         my_env = _overlay_windows_vcvars(my_env)
   42         my_env.setdefault('CC', 'cl')
   43         my_env.setdefault('CXX', 'cl')
   44     return my_env
   45 
   46 
   47 def build_caffe2(version, cmake_python_library, build_python, rerun_cmake, cmake_only, cmake):
   48     my_env = _create_build_env()
   49     build_test = not check_negative_env_flag('BUILD_TEST')
   50     cmake.generate(version,
   51                    cmake_python_library,
   52                    build_python,
   53                    build_test,
   54                    my_env,
   55                    rerun_cmake)
   56     if cmake_only:
   57         return
   58     cmake.build(my_env)
   59     if build_python:
   60         caffe2_proto_dir = os.path.join(cmake.build_dir, 'caffe2', 'proto')
   61         for proto_file in glob(os.path.join(caffe2_proto_dir, '*.py')):
   62             if proto_file != os.path.join(caffe2_proto_dir, '__init__.py'):
   63                 shutil.copy(proto_file, os.path.join('caffe2', 'proto'))