mirror of
https://github.com/zebrajr/pytorch.git
synced 2025-12-07 12:21:27 +01:00
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
198 lines
5.8 KiB
C++
198 lines
5.8 KiB
C++
#pragma once
|
|
|
|
#include <ATen/ATen.h>
|
|
#include <torch/csrc/autograd/variable.h>
|
|
|
|
#include <cstdint>
|
|
#include <tuple>
|
|
#include <type_traits>
|
|
#include <utility>
|
|
|
|
namespace torch {
|
|
|
|
// This class allows you to write variadic functions which
|
|
// call a (possibly overloaded) function on each argument,
|
|
// in order. This is most commonly used in autogenerated code,
|
|
// where it is convenient to have a function that can uniformly
|
|
// take arguments of different types. If your arguments
|
|
// are homogenous consider using a std::initializer_list instead.
|
|
template <typename F>
|
|
struct IterArgs {
|
|
template <typename... Args>
|
|
inline F& apply() {
|
|
return self();
|
|
}
|
|
|
|
// NB: Use perfect forwarding here, otherwise we'll make value
|
|
// copies of all arguments!
|
|
template <typename T, typename... Args>
|
|
inline F& apply(T&& arg, Args&&... args) {
|
|
self()(std::forward<T>(arg));
|
|
if (self().short_circuit()) {
|
|
return self();
|
|
} else {
|
|
return apply(std::forward<Args>(args)...);
|
|
}
|
|
}
|
|
|
|
// Here are some handy overloads which provide sensible
|
|
// defaults for container-like structures that one might
|
|
// be interested in recursing into. You can enable them
|
|
// by adding:
|
|
//
|
|
// using IterArgs<YourStructName>::operator()
|
|
//
|
|
// to your struct. These are not enabled by default because
|
|
// you may be able to process these structures more efficiently
|
|
// than handling them one-by-one.
|
|
|
|
template <typename T>
|
|
void operator()(at::ArrayRef<T> args) {
|
|
for (const auto& arg : args) {
|
|
self()(arg);
|
|
if (short_circuit())
|
|
return;
|
|
}
|
|
}
|
|
|
|
// NB: we need to specify std::vector manually as C++ won't
|
|
// do an implicit conversion to make a template deduction go through.
|
|
template <typename T>
|
|
void operator()(const std::vector<T>& args) {
|
|
self()(at::ArrayRef<T>{args});
|
|
}
|
|
|
|
bool short_circuit() {
|
|
return false;
|
|
}
|
|
|
|
private:
|
|
inline F& self() {
|
|
return *static_cast<F*>(this);
|
|
}
|
|
};
|
|
|
|
struct CountTensors : IterArgs<CountTensors> {
|
|
size_t out = 0;
|
|
void operator()(const at::Tensor& x) {
|
|
out += 1;
|
|
}
|
|
void operator()(at::ArrayRef<at::Tensor> xs) {
|
|
out += xs.size();
|
|
}
|
|
};
|
|
|
|
template <typename... Args>
|
|
size_t count_tensors(Args&&... args) {
|
|
return CountTensors().apply(std::forward<Args>(args)...).out;
|
|
}
|
|
|
|
struct CountVariables : IterArgs<CountVariables> {
|
|
size_t out = 0;
|
|
void operator()(const autograd::Variable& x) {
|
|
out += 1;
|
|
}
|
|
void operator()(at::ArrayRef<autograd::Variable> xs) {
|
|
out += xs.size();
|
|
}
|
|
};
|
|
|
|
template <typename... Args>
|
|
inline size_t count_variables(Args&&... args) {
|
|
return CountVariables().apply(std::forward<Args>(args)...).out;
|
|
}
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
// std::index_sequence shim for C++11
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// A container of type-template parameter indices.
|
|
template <size_t... Is>
|
|
struct Indices {};
|
|
|
|
// Decrements the index N, adds N-1 to the list of indices and forwards
|
|
// whatever we arleady have.
|
|
template <size_t N, size_t... Is>
|
|
struct MakeIndices : MakeIndices<N - 1, N - 1, Is...> {};
|
|
|
|
// Partial specialization that forms our base case. When N is zero, we stop
|
|
// and define a typedef that will be visible to earlier classes due to
|
|
// inheritance. The typedef we define is an index list containing the numbers
|
|
// 0 through N-1.
|
|
template <size_t... Is>
|
|
struct MakeIndices<0, Is...> {
|
|
using indices = Indices<Is...>;
|
|
};
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
// Utilities
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
template <bool value, typename T = void>
|
|
using enable_if_t = typename std::enable_if<value, T>::type;
|
|
|
|
template <bool value, typename T = void>
|
|
using disable_if_t = enable_if_t<!value, T>;
|
|
|
|
template <typename T>
|
|
using decay_t = typename std::decay<T>::type;
|
|
|
|
namespace detail {
|
|
template <bool...>
|
|
struct pack;
|
|
} // namespace detail
|
|
|
|
template <bool... values>
|
|
struct all_of : std::is_same<
|
|
detail::pack<values..., true>,
|
|
detail::pack<true, values...>> {};
|
|
|
|
template <bool...>
|
|
struct any_of;
|
|
|
|
template <>
|
|
struct any_of<> : std::false_type {};
|
|
|
|
template <bool head, bool... tail>
|
|
struct any_of<head, tail...> {
|
|
static constexpr bool value = head || any_of<tail...>::value;
|
|
};
|
|
|
|
template <bool... values>
|
|
struct none_of {
|
|
static constexpr bool value = !any_of<values...>::value;
|
|
};
|
|
|
|
template <bool... values>
|
|
using enable_if_all_of_t = enable_if_t<all_of<values...>::value>;
|
|
|
|
template <typename T, typename... Ts>
|
|
using disable_if_contains_t =
|
|
enable_if_all_of_t<(!std::is_same<T, decay_t<Ts>>::value)...>;
|
|
|
|
template <typename Function, typename... Ts>
|
|
void apply(Function function, Ts&&... ts) {
|
|
// https://stackoverflow.com/questions/13978916/inserting-a-variadic-argument-list-into-a-vector
|
|
// Creates a dummy array, so that each function call is evaluated in order.
|
|
// `(function(), 0)` is because `function` should (!) return `void`, so
|
|
// according to the comma operator, it is evaluated and its result (`void`)
|
|
// is discarded. Then the zero is evaluated and used as an element in the
|
|
// array. The first zero ensures the array is not empty.
|
|
int _[]{0, (function(std::forward<Ts>(ts)), 0)...};
|
|
(void)_;
|
|
}
|
|
|
|
template <typename ReturnType, typename... Ts, typename Function, typename Accessor>
|
|
ReturnType unpack(Function function, Accessor accessor) {
|
|
return ReturnType(unpack<ReturnType, Ts...>(
|
|
std::move(function),
|
|
std::move(accessor),
|
|
typename MakeIndices<sizeof...(Ts)>::indices()));
|
|
}
|
|
|
|
template <typename ReturnType, typename... Ts, typename Function, typename Accessor, size_t... Is>
|
|
ReturnType unpack(Function function, Accessor accessor, Indices<Is...>) {
|
|
return ReturnType(function(accessor.template operator()<Ts>(Is)...));
|
|
}
|
|
} // namespace torch
|