Skip to content

Commit 5c4a6f3

Browse files
authored
Merge pull request #137 from iotamudelta/master
Merge from upstream
2 parents c236adb + 69dfb5e commit 5c4a6f3

File tree

137 files changed

+5470
-789
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

137 files changed

+5470
-789
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ aten/src/ATen/cuda/CUDAConfig.h
2525
build/
2626
dist/
2727
docs/src/**/*
28+
docs/cpp/xml/
29+
docs/cpp/html/
30+
docs/cpp/api/
2831
test/.coverage
2932
test/cpp/api/mnist
3033
test/data/gpu_tensors.pt

aten/src/ATen/Allocator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class DataPtr {
2323
public:
2424
// Choice of CPU here is arbitrary; if there's an "undefined" device
2525
// we could use that too
26-
DataPtr() : ptr_(), device_(kCPU) {}
26+
DataPtr() : ptr_(), device_(DeviceType::CPU) {}
2727
DataPtr(void* data, Device device)
2828
: ptr_(data), device_(device) {}
2929
DataPtr(void* data, void* ctx, DeleterFnPtr ctx_deleter, Device device)

aten/src/ATen/Backend.h

Lines changed: 77 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,25 @@
33
#include <ATen/core/TensorTypeId.h>
44
#include <ATen/core/TensorTypeIdRegistration.h>
55
#include <ATen/core/Error.h>
6+
#include <ATen/core/DeviceType.h>
67

78
#include <stdexcept>
89

910
namespace at {
1011

12+
/**
13+
* This legacy enum class defines the set of backends supported by
14+
* old school, code generated Type-based ATen. The reason we are
15+
* sunsetting this enum class is because it doesn't allow for
16+
* open registration of backends. TensorTypeId is the replacement
17+
* for Backend which supports open registration.
18+
*
19+
* ARE YOU SURE YOU WANT TO USE THIS TYPE? Think about if SparseCPU/SparseCUDA
20+
* would make sense in your use case. If it doesn't make sense, maybe
21+
* you want DeviceType.
22+
*/
1123
enum class Backend { CPU, CUDA, SparseCPU, SparseCUDA, Undefined, NumOptions };
1224

13-
constexpr Backend kCPU = Backend::CPU;
14-
constexpr Backend kCUDA = Backend::CUDA;
15-
constexpr Backend kSparseCPU = Backend::SparseCPU;
16-
constexpr Backend kSparseCUDA = Backend::SparseCUDA;
17-
1825
static inline Backend toSparse(Backend b) {
1926
switch (b) {
2027
case Backend::CPU:
@@ -78,6 +85,71 @@ static inline TensorTypeId backendToTensorTypeId(Backend b) {
7885
}
7986
}
8087

88+
static inline DeviceType backendToDeviceType(Backend b) {
89+
switch (b) {
90+
case Backend::CPU:
91+
return DeviceType::CPU;
92+
case Backend::CUDA:
93+
return DeviceType::CUDA;
94+
case Backend::SparseCPU:
95+
return DeviceType::CPU;
96+
case Backend::SparseCUDA:
97+
return DeviceType::CUDA;
98+
case Backend::Undefined:
99+
AT_ERROR("Undefined backend is not a valid device type");
100+
default:
101+
AT_ERROR("Unknown backend");
102+
}
103+
}
104+
105+
static inline Backend deviceTypeToBackend(DeviceType d) {
106+
switch (d) {
107+
case DeviceType::CPU:
108+
return Backend::CPU;
109+
case DeviceType::CUDA:
110+
return Backend::CUDA;
111+
default:
112+
AT_ERROR("Unknown device type ", d);
113+
}
114+
}
115+
116+
static inline Backend backendToCPU(Backend b) {
117+
switch (b) {
118+
case Backend::CPU:
119+
return Backend::CPU;
120+
case Backend::CUDA:
121+
return Backend::CPU;
122+
case Backend::SparseCPU:
123+
return Backend::SparseCPU;
124+
case Backend::SparseCUDA:
125+
return Backend::SparseCPU;
126+
case Backend::Undefined:
127+
return Backend::Undefined;
128+
default:
129+
AT_ERROR("Unknown backend");
130+
}
131+
}
132+
133+
static inline Backend backendToCUDA(Backend b) {
134+
switch (b) {
135+
case Backend::CPU:
136+
return Backend::CUDA;
137+
case Backend::CUDA:
138+
return Backend::CUDA;
139+
case Backend::SparseCPU:
140+
return Backend::SparseCUDA;
141+
case Backend::SparseCUDA:
142+
return Backend::SparseCUDA;
143+
case Backend::Undefined:
144+
return Backend::Undefined;
145+
default:
146+
AT_ERROR("Unknown backend");
147+
}
148+
}
149+
150+
constexpr DeviceType kCPU = DeviceType::CPU;
151+
constexpr DeviceType kCUDA = DeviceType::CUDA;
152+
81153
static inline const char* toString(Backend b) {
82154
switch (b) {
83155
case Backend::CPU:

aten/src/ATen/Context.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Context::Context()
3232
THSetDefaultErrorHandler(errorHandler,nullptr);
3333
THSetDefaultArgErrorHandler(argErrorHandler,nullptr);
3434

35-
generator_registry[static_cast<int>(Backend::CPU)]
35+
generator_registry[static_cast<int>(DeviceType::CPU)]
3636
.reset(new CPUGenerator(this));
3737
Type::registerCPU(this);
3838
}

aten/src/ATen/Context.h

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class AT_API Context {
2525
return type_registry[static_cast<int>(p)][static_cast<int>(s)].get();
2626
}
2727
Type * getTypeOpt(Backend p, ScalarType s) {
28-
initCUDAIfNeeded(p);
28+
if (p != Backend::Undefined) initCUDAIfNeeded(backendToDeviceType(p));
2929
auto type = getTypeRaw(p, s);
3030

3131
if(!type) {
@@ -42,11 +42,11 @@ class AT_API Context {
4242
if (!type) AT_ERROR(toString(p), toString(s), "Type is not enabled.");
4343
return *type;
4444
}
45-
Generator & defaultGenerator(Backend p) {
46-
initCUDAIfNeeded(p);
47-
auto & generator = generator_registry[static_cast<int>(p)];
45+
Generator & defaultGenerator(DeviceType device_type) {
46+
initCUDAIfNeeded(device_type);
47+
auto & generator = generator_registry[static_cast<int>(device_type)];
4848
if(!generator)
49-
AT_ERROR(toString(p), " backend type not enabled.");
49+
AT_ERROR(DeviceTypeName(device_type), " backend type not enabled.");
5050
return *generator;
5151
}
5252
bool hasMKL() const;
@@ -64,7 +64,7 @@ class AT_API Context {
6464
THCState* lazyInitCUDA() {
6565
std::call_once(thc_init,[&] {
6666
thc_state = detail::getCUDAHooks().initCUDA();
67-
generator_registry[static_cast<int>(Backend::CUDA)] =
67+
generator_registry[static_cast<int>(DeviceType::CUDA)] =
6868
detail::getCUDAHooks().initCUDAGenerator(this);
6969
detail::getCUDAHooks().registerCUDATypes(this);
7070
});
@@ -95,16 +95,17 @@ class AT_API Context {
9595
bool deterministicCuDNN() const;
9696
void setDeterministicCuDNN(bool);
9797
std::unique_ptr<Generator>
98-
generator_registry[static_cast<int>(Backend::NumOptions)];
98+
generator_registry[static_cast<int>(DeviceType::COMPILE_TIME_MAX_DEVICE_TYPES)];
9999
private:
100100
// NB: type_registry has nullptr for all CUDA backends until
101101
// CUDA initialization has occurred
102102
std::unique_ptr<Type> type_registry
103103
[static_cast<int>(Backend::NumOptions)]
104104
[static_cast<int>(ScalarType::NumOptions)];
105-
void initCUDAIfNeeded(Backend p) {
106-
if(p == Backend::CUDA)
105+
void initCUDAIfNeeded(DeviceType p) {
106+
if (p == DeviceType::CUDA) {
107107
lazyInitCUDA();
108+
}
108109
}
109110
std::once_flag thc_init;
110111
bool enabled_cudnn = true;
@@ -132,6 +133,10 @@ static inline Type& getType(Backend p, ScalarType s) {
132133
return globalContext().getType(p, s);
133134
}
134135

136+
static inline Type& getType(DeviceType p, ScalarType s) {
137+
return globalContext().getType(deviceTypeToBackend(p), s);
138+
}
139+
135140
static inline Type& CPU(ScalarType s) {
136141
return getType(Backend::CPU, s);
137142
}

aten/src/ATen/Device.h

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#pragma once
22

3-
#include <ATen/ScalarType.h>
3+
#include <ATen/ATenGeneral.h>
44
#include <ATen/core/Error.h>
55
#include <ATen/core/DeviceType.h>
66
#include <ATen/core/Error.h>
7+
#include <ATen/Backend.h>
78

89
#include <cstddef>
910
#include <iosfwd>
@@ -24,21 +25,6 @@ namespace at {
2425
struct Device {
2526
using Type = at::DeviceType;
2627

27-
/// Converts a `Backend` to a `DeviceType` if possible.
28-
static DeviceType backend_to_type(Backend backend) {
29-
switch (backend) {
30-
case kCPU:
31-
case kSparseCPU:
32-
return DeviceType::CPU;
33-
case kCUDA:
34-
case kSparseCUDA:
35-
return DeviceType::CUDA;
36-
default:
37-
AT_ERROR(
38-
"Invalid backend ", toString(backend), " for Device construction");
39-
}
40-
}
41-
4228
/// Constructs a new `Device` from a `DeviceType` and an optional device
4329
/// index.
4430
/* implicit */ Device(DeviceType type, int32_t index = -1)
@@ -60,11 +46,6 @@ struct Device {
6046
/// `<device-index>` optionally specifies a device index.
6147
/* implicit */ Device(const std::string& device_string);
6248

63-
/// Constructs a new `Device` from a `Backend` (which is converted to a
64-
/// `DeviceType`, if possible) and an optional device index.
65-
/* implicit */ Device(Backend backend, int32_t index = -1)
66-
: Device(backend_to_type(backend), index) {}
67-
6849
/// Returns true if the type and index of this `Device` matches that of
6950
/// `other`.
7051
bool operator==(const Device& other) const noexcept {

aten/src/ATen/Formatting.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ std::ostream& print(std::ostream& stream, const Tensor & tensor_, int64_t linesi
250250
stream << "size:\n" << tensor_.sizes() << "\n";
251251
stream << "]";
252252
} else {
253-
Type& cpudouble = tensor_.type().toBackend(kCPU).toScalarType(kDouble);
253+
Type& cpudouble = tensor_.type().toBackend(Backend::CPU).toScalarType(kDouble);
254254
Tensor tensor = tensor_.toType(cpudouble).contiguous();
255255
if(tensor.ndimension() == 0) {
256256
stream << defaultfloat << tensor.data<double>()[0] << std::endl;

aten/src/ATen/TensorOptions.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include <ATen/Backend.h>
34
#include <ATen/Context.h>
45
#include <ATen/Device.h>
56
#include <ATen/DeviceGuard.h>
@@ -67,7 +68,7 @@ struct AT_API TensorOptions {
6768
type_ = &type;
6869
}
6970
this->dtype(type.scalarType());
70-
this->device({type.backend(), device_index});
71+
this->device({backendToDeviceType(type.backend()), device_index});
7172
this->layout(type.layout());
7273
}
7374

@@ -84,7 +85,12 @@ struct AT_API TensorOptions {
8485
/// Constructs a `TensorOptions` object from a backend, forwarded to the
8586
/// `Device` constructor.
8687
/* implicit */ TensorOptions(Backend backend)
87-
: TensorOptions(Device(backend)) {}
88+
: TensorOptions(Device(backendToDeviceType(backend))) {}
89+
90+
/// Constructs a `TensorOptions` object from a device type, forwarded to the
91+
/// `Device` constructor.
92+
/* implicit */ TensorOptions(DeviceType device_type)
93+
: TensorOptions(Device(device_type)) {}
8894

8995
/// Constructs a `TensorOptions` object with the given dtype.
9096
/* implicit */ TensorOptions(ScalarType dtype) : TensorOptions() {
@@ -190,9 +196,9 @@ struct AT_API TensorOptions {
190196
Backend backend() const noexcept {
191197
Backend backend;
192198
if (device_.type() == Device::Type::CPU) {
193-
backend = (layout_ == kStrided) ? kCPU : kSparseCPU;
199+
backend = (layout_ == kStrided) ? Backend::CPU : Backend::SparseCPU;
194200
} else {
195-
backend = (layout_ == kStrided) ? kCUDA : kSparseCUDA;
201+
backend = (layout_ == kStrided) ? Backend::CUDA : Backend::SparseCUDA;
196202
}
197203
return backend;
198204
}

aten/src/ATen/core/Macros.h

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,22 @@
77
// static library (in which case, saying the symbol is coming
88
// from a DLL would be incorrect).
99

10-
#define AT_CORE_EXPORT
11-
#define AT_CORE_IMPORT
12-
1310
#ifdef _WIN32
14-
#ifndef AT_CORE_STATIC_WINDOWS
15-
#undef AT_CORE_EXPORT
16-
#undef AT_CORE_IMPORT
17-
#define AT_CORE_EXPORT __declspec(dllexport)
18-
#define AT_CORE_IMPORT __declspec(dllimport)
19-
#endif // !defined(AT_CORE_STATIC_WINDOWS)
20-
#else // _WIN32
21-
#if defined(__GNUC__) || defined(__llvm__)
22-
#undef AT_CORE_EXPORT
23-
#undef AT_CORE_IMPORT
24-
#define AT_CORE_EXPORT __attribute__((__visibility__("default")))
25-
#define AT_CORE_IMPORT AT_CORE_EXPORT
26-
#endif // defined(__GNUC__) || defined(__llvm__)
27-
#endif // _WIN32
28-
11+
#if !defined(AT_CORE_STATIC_WINDOWS)
12+
// TODO: unfiy the controlling macros.
2913
#if defined(CAFFE2_BUILD_MAIN_LIBS) || defined(ATen_cpu_EXPORTS) || defined(caffe2_EXPORTS)
30-
#define AT_CORE_API AT_CORE_EXPORT
14+
#define AT_CORE_API __declspec(dllexport)
3115
#else // defined(CAFFE2_BUILD_MAIN_LIBS) || defined(ATen_cpu_EXPORTS) || defined(caffe2_EXPORTS)
32-
#define AT_CORE_API AT_CORE_IMPORT
16+
#define AT_CORE_API __declspec(dllimport)
3317
#endif // defined(CAFFE2_BUILD_MAIN_LIBS) || defined(ATen_cpu_EXPORTS) || defined(caffe2_EXPORTS)
18+
#else // !defined(AT_CORE_STATIC_WINDOWS)
19+
#define AT_CORE_API
20+
#endif // !defined(AT_CORE_STATIC_WINDOWS)
21+
#else // _WIN32
22+
#if defined(__GNUC__)
23+
#define AT_CORE_API __attribute__((__visibility__("default")))
24+
#endif // defined(__GNUC__)
25+
#endif // _WIN32
3426

3527
// Disable the copy and assignment operator for a class. Note that this will
3628
// disable the usage of the class in std containers.

aten/src/ATen/core/typeid.h

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -391,26 +391,53 @@ inline bool operator!=(const TypeMeta& lhs, const TypeMeta& rhs) noexcept {
391391
*
392392
* NOTE: the macro needs to be invoked in ::caffe2 namespace
393393
*/
394-
394+
// Implementation note: in MSVC, we will need to prepend the AT_CORE_API
395+
// keyword in order to get things compiled properly. in Linux, gcc seems to
396+
// create attribute ignored error for explicit template instantiations, see
397+
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0537r0.html
398+
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=51930
399+
// and as a result, we define these two macros slightly differently.
400+
// TODO(jiayq): AT_CORE_API below is not correct, because we may use the
401+
// definition in third party dependent libraries. The proper way is to use
402+
// CAFFE2_EXPORT (which explicitly requires dllexport). Marking this as a
403+
// todo item when the unified build is finished.
404+
#ifdef _MSC_VER
395405
#define CAFFE_KNOWN_TYPE(T) \
396406
template <> \
397-
AT_CORE_EXPORT TypeIdentifier TypeMeta::Id<T>() { \
407+
AT_CORE_API TypeIdentifier TypeMeta::Id<T>() { \
398408
static const TypeIdentifier type_id = TypeIdentifier::createTypeId(); \
399409
static TypeNameRegisterer<T> registerer(type_id, #T); \
400410
return type_id; \
401411
}
412+
#else // _MSC_VER
413+
#define CAFFE_KNOWN_TYPE(T) \
414+
template <> \
415+
TypeIdentifier TypeMeta::Id<T>() { \
416+
static const TypeIdentifier type_id = TypeIdentifier::createTypeId(); \
417+
static TypeNameRegisterer<T> registerer(type_id, #T); \
418+
return type_id; \
419+
}
420+
#endif
402421

403422
/**
404423
* CAFFE_DECLARE_KNOWN_TYPE and CAFFE_DEFINE_KNOWN_TYPE are used
405424
* to preallocate ids for types that are queried very often so that they
406425
* can be resolved at compile time. Please use CAFFE_KNOWN_TYPE() instead
407426
* for your own types to allocate dynamic ids for them.
408427
*/
409-
#define CAFFE_DECLARE_KNOWN_TYPE(PreallocatedId, T) \
410-
template <> \
411-
AT_CORE_EXPORT inline AT_CORE_API TypeIdentifier TypeMeta::Id<T>() { \
412-
return TypeIdentifier(PreallocatedId); \
428+
#ifdef _MSC_VER
429+
#define CAFFE_DECLARE_KNOWN_TYPE(PreallocatedId, T) \
430+
template <> \
431+
inline AT_CORE_API TypeIdentifier TypeMeta::Id<T>() { \
432+
return TypeIdentifier(PreallocatedId); \
413433
}
434+
#else // _MSC_VER
435+
#define CAFFE_DECLARE_KNOWN_TYPE(PreallocatedId, T) \
436+
template <> \
437+
inline TypeIdentifier TypeMeta::Id<T>() { \
438+
return TypeIdentifier(PreallocatedId); \
439+
}
440+
#endif
414441

415442
#define CONCAT_IMPL(x, y) x##y
416443
#define MACRO_CONCAT(x, y) CONCAT_IMPL(x, y)

0 commit comments

Comments
 (0)