pytorch/torch/csrc/jit/script/builtin_functions.cpp
Edward Yang 517c7c9861 Canonicalize all includes in PyTorch. (#14849)
Summary:
Anywhere we used #include "foo.h", we now say #include <foo.h>
Paths are adjusted to be rooted out of aten/src, torch/lib, or
the root level directory.

I modified CMakeLists.txt by hand to remove TH and THC from
the include paths.

I used the following script to do the canonicalization:

```
  import subprocess
  import re
  import os.path

  files = subprocess.check_output(['git', 'ls-files']).decode('utf-8').rstrip().split('\n')
  for fn in files:
      if not any(fn.endswith(suff) for suff in ['.cu', '.cpp', '.in', '.h', '.hpp', '.cu', '.cuh', '.cc']):
          continue
      if not any(fn.startswith(pref) for pref in ["aten/", "torch/"]):
          continue
      with open(fn, 'r') as f:
          c = f.read()
      def fmt(p):
          return "#include <{}>".format(p)
      def repl(m):
          p = m.group(1)
          if p in ["dlfcn.h", "unistd.h", "nvrtc.h", "cuda.h", "cuda_runtime.h", "cstdint", "cudnn.h", "Python.h", "cusparse.h", "cuda_runtime_api.h", "cuda_fp16.h", "cublas_v2.h", "stdint.h", "curand_kernel.h"]:
              return fmt(p)
          if any(p.startswith(pref) for pref in ["torch/csrc", "c10/", "ATen/", "caffe2/", "TH/", "THC/", "Eigen/", "gtest/", "zdl/", "gloo/", "onnx/", "miopen/"]):
              return fmt(p)
          for root in ["aten/src", "torch/lib", ""]:
              for bad_root in [os.path.dirname(fn), "aten/src/TH", "aten/src/THC", "torch/csrc"]:
                  new_p = os.path.relpath(os.path.join(bad_root, p), root)
                  if not new_p.startswith("../") and (os.path.exists(os.path.join(root, new_p)) or os.path.exists(os.path.join(root, new_p + ".in"))):
                      return fmt(new_p)
          print("ERROR: ", fn, p)
          return m.group(0)
      new_c = re.sub(r'#include "([^"]+)"', repl, c)
      if new_c != c:
          print(fn)
          with open(fn, 'w') as f:
              f.write(new_c)
```

Signed-off-by: Edward Z. Yang <ezyang@fb.com>
Pull Request resolved: https://github.com/pytorch/pytorch/pull/14849

Reviewed By: dzhulgakov

Differential Revision: D13363445

Pulled By: ezyang

fbshipit-source-id: 52361f878a672785f9306c9e9ab2513128092b68
2018-12-08 19:38:30 -08:00

107 lines
3.4 KiB
C++

#include <torch/csrc/jit/script/builtin_functions.h>
#include <torch/csrc/api/include/torch/jit.h>
#include <torch/csrc/jit/code_template.h>
namespace torch { namespace jit { namespace script {
auto scalar_operators_source = CodeTemplate(
R"SCRIPT(
def mul(a : ${Scalar}, b : Tensor) -> Tensor:
return b * a
def add(a : ${Scalar}, b : Tensor) -> Tensor:
return b + a
def ne(a : ${Scalar}, b : Tensor) -> Tensor:
return b != a
def eq(a : ${Scalar}, b : Tensor) -> Tensor:
return b == a
def lt(a : ${Scalar}, b : Tensor) -> Tensor:
return b > a
def le(a : ${Scalar}, b : Tensor) -> Tensor:
return b >= a
def gt(a : ${Scalar}, b : Tensor) -> Tensor:
return b < a
def ge(a : ${Scalar}, b : Tensor) -> Tensor:
return b <= a
def sub(a : ${Scalar}, b : Tensor) -> Tensor:
return torch.neg(b) + a
def div(a : ${Scalar}, b : Tensor) -> Tensor:
return torch.reciprocal(b) * a
)SCRIPT");
auto _ntuple_ops = CodeTemplate(
R"SCRIPT(
def _${name}(x: BroadcastingList${Length}[${Scalar}]) -> List[${Scalar}]:
return x
)SCRIPT");
struct BuiltinFunctionRegistry {
const std::vector<Method*>& getAllBuiltinFunctionsFor(Symbol name) {
const static std::vector<Method*> empty;
// when initializing the builtin function library, we will re-enter
// getAllBuiltinFunctionsFor since it is called in the compiler to
// lookup builtins and initializing the builtin functions calls the compiler.
// To avoid deadlocking, we use a recursive mutex (same thread can re-lock,
// the mutex without waiting), and report no loaded builtins during init.
std::lock_guard<std::recursive_mutex> guard(mutex);
if(state == INTIIALIZING) {
return empty;
} else if (state == UNINITIALIZED) {
state = INTIIALIZING;
loadBuiltinFunctions();
state = INITIALIZED;
}
JIT_ASSERT(state == INITIALIZED);
auto it = builtins_by_name.find(name);
if(it == builtins_by_name.end())
return empty;
return it->second;
}
private:
void loadSource(const std::string& source) {
auto module = std::make_shared<script::Module>();
defineMethodsInModule(
module, source, script::nativeResolver, /*self=*/nullptr);
modules.push_back(module);
for (auto& method : module->get_methods()) {
builtins_by_name[Symbol::fromQualString("aten::" + method.key())].push_back(
method->get());
}
}
void loadBuiltinFunctions() {
for(auto scalar : {"float", "int"}) {
TemplateEnv env;
env.s("Scalar", scalar);
loadSource(scalar_operators_source.format(env));
}
using str_pair = std::pair<std::string, std::string>;
const std::vector<str_pair> name_len = {
str_pair("single", "1"),
str_pair("pair", "2"),
str_pair("triple", "3"),
str_pair("quadruple", "4"),
};
for(auto scalar: {"float", "int"}) {
for (auto pair: name_len) {
TemplateEnv env;
env.s("Scalar", scalar);
env.s("name", pair.first);
env.s("Length", pair.second);
loadSource(_ntuple_ops.format(env));
}
}
}
enum {UNINITIALIZED, INTIIALIZING, INITIALIZED} state = UNINITIALIZED;
std::recursive_mutex mutex;
std::vector<std::shared_ptr<Module>> modules;
std::unordered_map<Symbol, std::vector<Method*>> builtins_by_name;
};
TORCH_API const std::vector<Method*>& getAllBuiltinFunctionsFor(Symbol name) {
static BuiltinFunctionRegistry registry;
return registry.getAllBuiltinFunctionsFor(name);
}
}}}