From 9169f60a84449fcaf8f0ac29e75a527554998ea2 Mon Sep 17 00:00:00 2001 From: Adam Paszke Date: Sat, 29 Apr 2017 15:07:21 +0200 Subject: [PATCH] Parallelize TensorMethods.cpp builds (#1400) --- .gitignore | 1 + setup.py | 5 +- tools/setup_helpers/split_types.py | 58 +++++++++++++++++++++ torch/csrc/Tensor.cpp | 12 ++--- torch/csrc/cuda/Tensor.cpp | 11 ++-- torch/csrc/generic/methods/TensorMath.cwrap | 3 +- 6 files changed, 72 insertions(+), 18 deletions(-) create mode 100644 tools/setup_helpers/split_types.py diff --git a/.gitignore b/.gitignore index d49648c422fda..4c9f8bd5a01a4 100644 --- a/.gitignore +++ b/.gitignore @@ -19,6 +19,7 @@ torch/csrc/nn/THCUNN.cpp torch/csrc/nn/THNN_generic.cwrap torch/csrc/nn/THNN_generic.cpp torch/csrc/nn/THNN_generic.h +torch/csrc/generated docs/src/**/* test/data/legacy_modules.t7 test/data/gpu_tensors.pt diff --git a/setup.py b/setup.py index 24ee8a822cc1c..026dc30ef82ff 100644 --- a/setup.py +++ b/setup.py @@ -15,6 +15,7 @@ from tools.setup_helpers.env import check_env_flag from tools.setup_helpers.cuda import WITH_CUDA, CUDA_HOME from tools.setup_helpers.cudnn import WITH_CUDNN, CUDNN_LIB_DIR, CUDNN_INCLUDE_DIR +from tools.setup_helpers.split_types import split_types DEBUG = check_env_flag('DEBUG') WITH_DISTRIBUTED = check_env_flag('WITH_DISTRIBUTED') WITH_DISTRIBUTED_MW = WITH_DISTRIBUTED and check_env_flag('WITH_DISTRIBUTED_MW') @@ -259,7 +260,6 @@ def run(self): "torch/csrc/Generator.cpp", "torch/csrc/Size.cpp", "torch/csrc/Exceptions.cpp", - "torch/csrc/Tensor.cpp", "torch/csrc/Storage.cpp", "torch/csrc/DynamicTypes.cpp", "torch/csrc/byte_order.cpp", @@ -283,6 +283,7 @@ def run(self): "torch/csrc/autograd/functions/init.cpp", "torch/csrc/nn/THNN_generic.cpp", ] +main_sources += split_types("torch/csrc/Tensor.cpp") try: import numpy as np @@ -325,11 +326,11 @@ def run(self): "torch/csrc/cuda/Module.cpp", "torch/csrc/cuda/Storage.cpp", "torch/csrc/cuda/Stream.cpp", - "torch/csrc/cuda/Tensor.cpp", "torch/csrc/cuda/AutoGPU.cpp", "torch/csrc/cuda/utils.cpp", "torch/csrc/cuda/serialization.cpp", ] + main_sources += split_types("torch/csrc/cuda/Tensor.cpp") if WITH_NCCL: if SYSTEM_NCCL: diff --git a/tools/setup_helpers/split_types.py b/tools/setup_helpers/split_types.py new file mode 100644 index 0000000000000..0b3d8675f8aea --- /dev/null +++ b/tools/setup_helpers/split_types.py @@ -0,0 +1,58 @@ +import os + +this_file = os.path.dirname(os.path.abspath(__file__)) +generated_dir = os.path.abspath(os.path.join(this_file, '..', '..', 'torch', 'csrc', 'generated')) + +line_start = '//generic_include ' + +types = [ + 'Double', + 'Float', + 'Half', + 'Long', + 'Int', + 'Short', + 'Char', + 'Byte' +] + +generic_include = '#define {lib}_GENERIC_FILE "{path}"' +generate_include = '#include "{lib}/{lib}Generate{type}Type.h"' + + +def split_types(file_name): + assert file_name.startswith('torch/csrc/') + if not os.path.exists(generated_dir): + os.makedirs(generated_dir) + + with open(file_name, 'r') as f: + lines = f.read().split('\n') + + # Find //generic_include + for i, l in enumerate(lines): + if l.startswith(line_start): + args = l[len(line_start):] + lib_prefix, generic_file = filter(bool, args.split()) + break + else: + raise RuntimeError("generic include not found") + + gen_name_prefix = file_name[len('torch/csrc/'):].replace('/', '_').replace('.cpp', '') + gen_path_prefix = os.path.join(generated_dir, gen_name_prefix) + + prefix = '\n'.join(lines[:i]) + suffix = '\n'.join(lines[i + 1:]) + + to_build = [] + + g_include = generic_include.format(lib=lib_prefix, path=generic_file) + for t in types: + t_include = generate_include.format(lib=lib_prefix, type=t) + gen_path = gen_path_prefix + t + '.cpp' + to_build.append(gen_path) + with open(gen_path, 'w') as f: + f.write(prefix + '\n' + + g_include + '\n' + + t_include + '\n' + + suffix) + return to_build diff --git a/torch/csrc/Tensor.cpp b/torch/csrc/Tensor.cpp index 43841cc575f87..0c62370ab7e16 100644 --- a/torch/csrc/Tensor.cpp +++ b/torch/csrc/Tensor.cpp @@ -9,12 +9,8 @@ #include #include -#include "THP.h" -#include "copy_utils.h" -#include "DynamicTypes.h" +#include "torch/csrc/THP.h" +#include "torch/csrc/copy_utils.h" +#include "torch/csrc/DynamicTypes.h" -#include "generic/Tensor.cpp" -#include - -#include "generic/Tensor.cpp" -#include +//generic_include TH torch/csrc/generic/Tensor.cpp diff --git a/torch/csrc/cuda/Tensor.cpp b/torch/csrc/cuda/Tensor.cpp index e10ceaf14f53a..ea6f85b756fdd 100644 --- a/torch/csrc/cuda/Tensor.cpp +++ b/torch/csrc/cuda/Tensor.cpp @@ -6,14 +6,13 @@ #include #include #include -#include "THCP.h" +#include "torch/csrc/cuda/THCP.h" -#include "override_macros.h" +#include "torch/csrc/cuda/override_macros.h" #include "torch/csrc/copy_utils.h" #include "DynamicTypes.h" -#define THC_GENERIC_FILE "torch/csrc/generic/Tensor.cpp" -#include +//generic_include THC torch/csrc/generic/Tensor.cpp -#include "undef_macros.h" -#include "restore_macros.h" +#include "torch/csrc/cuda/undef_macros.h" +#include "torch/csrc/cuda/restore_macros.h" diff --git a/torch/csrc/generic/methods/TensorMath.cwrap b/torch/csrc/generic/methods/TensorMath.cwrap index 5a56634aa46df..823223a1f1722 100644 --- a/torch/csrc/generic/methods/TensorMath.cwrap +++ b/torch/csrc/generic/methods/TensorMath.cwrap @@ -1625,8 +1625,7 @@ - THTensor* tensor2 ]] -#ifndef THP_LAPACK_CONSTANTS -#define THP_LAPACK_CONSTANTS +#if defined(TH_REAL_IS_FLOAT) || defined(TH_REAL_IS_DOUBLE) || CUDA_FLOAT || CUDA_DOUBLE // We need to pass pointers to chars to tensor lapack functions... static const char __U = 'U'; static const char __L = 'L';