Skip to content

Merge from upstream #137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 24 commits into from
Aug 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
b4e72ea
Revert D9377394: [pytorch][PR] [Caffe2] Add AT_CORE_EXPORT and AT_COR…
kennyhorror Aug 17, 2018
5205820
Add nn functional tests in JIT (#10409)
wanchaol Aug 17, 2018
d35f365
Remove all cuDNN specific inputs to RNN functions (#10581)
apaszke Aug 17, 2018
f3ac619
Add fusion support for batchnorm and convolution without bias
Jokeren Aug 17, 2018
86c9856
Fuse tensor-scalar ops when scalar is constant (#10511)
zou3519 Aug 17, 2018
5dcd358
Merge remote-tracking branch 'upstream/master'
iotamudelta Aug 17, 2018
c5c1c05
Fix dropout fused kernel applied in eval mode (#10621)
ssnl Aug 17, 2018
b62b378
Adding torch support for CMAKE_ARGS env
pjh5 Aug 17, 2018
65b9308
Basic infrastructure for C++ documentation (#10569)
goldsborough Aug 17, 2018
7c55d11
Make sure we don't relocate the weight name buffer (#10630)
Aug 17, 2018
e29b5a1
graph fuser inserts explicit expands where necessary (#10325)
zou3519 Aug 17, 2018
152762a
Fix warnings diagnosed in recent clang (#10647)
taewookoh Aug 18, 2018
d87b4e9
fix python interpreter can not be found without `PYTHON_EXECUTABLE` (…
pohmelie Aug 18, 2018
f1420ad
Move at::chunk into the graph fuser (#10178)
zou3519 Aug 18, 2018
8bacfd9
Merge remote-tracking branch 'upstream/master'
iotamudelta Aug 19, 2018
6bdbad9
Refactor Device to not depend on Backend. (#10478)
ezyang Aug 19, 2018
108b657
Import DistributedSampler in utils/data/__init__ (#10671)
ssnl Aug 19, 2018
3f603ee
some improvements on distributed docs
ssnl Aug 20, 2018
83066e9
Add trigonometry functions for ONNX export (#7540)
zasdfgbnm Aug 20, 2018
c37fac4
Fixing stop condition on composite reader (#9888)
kittipatv Aug 20, 2018
9ad9191
Fix cuDNN dropout state cache (#10662)
apaszke Aug 20, 2018
f0035f9
Merge remote-tracking branch 'upstream/master'
iotamudelta Aug 20, 2018
f98aa63
Disable more routines that run into the cannot compile fusion group
iotamudelta Aug 20, 2018
69dfb5e
Another test missing fusion group.
iotamudelta Aug 20, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ aten/src/ATen/cuda/CUDAConfig.h
build/
dist/
docs/src/**/*
docs/cpp/xml/
docs/cpp/html/
docs/cpp/api/
test/.coverage
test/cpp/api/mnist
test/data/gpu_tensors.pt
Expand Down
2 changes: 1 addition & 1 deletion aten/src/ATen/Allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class DataPtr {
public:
// Choice of CPU here is arbitrary; if there's an "undefined" device
// we could use that too
DataPtr() : ptr_(), device_(kCPU) {}
DataPtr() : ptr_(), device_(DeviceType::CPU) {}
DataPtr(void* data, Device device)
: ptr_(data), device_(device) {}
DataPtr(void* data, void* ctx, DeleterFnPtr ctx_deleter, Device device)
Expand Down
82 changes: 77 additions & 5 deletions aten/src/ATen/Backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,25 @@
#include <ATen/core/TensorTypeId.h>
#include <ATen/core/TensorTypeIdRegistration.h>
#include <ATen/core/Error.h>
#include <ATen/core/DeviceType.h>

#include <stdexcept>

namespace at {

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

constexpr Backend kCPU = Backend::CPU;
constexpr Backend kCUDA = Backend::CUDA;
constexpr Backend kSparseCPU = Backend::SparseCPU;
constexpr Backend kSparseCUDA = Backend::SparseCUDA;

static inline Backend toSparse(Backend b) {
switch (b) {
case Backend::CPU:
Expand Down Expand Up @@ -78,6 +85,71 @@ static inline TensorTypeId backendToTensorTypeId(Backend b) {
}
}

static inline DeviceType backendToDeviceType(Backend b) {
switch (b) {
case Backend::CPU:
return DeviceType::CPU;
case Backend::CUDA:
return DeviceType::CUDA;
case Backend::SparseCPU:
return DeviceType::CPU;
case Backend::SparseCUDA:
return DeviceType::CUDA;
case Backend::Undefined:
AT_ERROR("Undefined backend is not a valid device type");
default:
AT_ERROR("Unknown backend");
}
}

static inline Backend deviceTypeToBackend(DeviceType d) {
switch (d) {
case DeviceType::CPU:
return Backend::CPU;
case DeviceType::CUDA:
return Backend::CUDA;
default:
AT_ERROR("Unknown device type ", d);
}
}

static inline Backend backendToCPU(Backend b) {
switch (b) {
case Backend::CPU:
return Backend::CPU;
case Backend::CUDA:
return Backend::CPU;
case Backend::SparseCPU:
return Backend::SparseCPU;
case Backend::SparseCUDA:
return Backend::SparseCPU;
case Backend::Undefined:
return Backend::Undefined;
default:
AT_ERROR("Unknown backend");
}
}

static inline Backend backendToCUDA(Backend b) {
switch (b) {
case Backend::CPU:
return Backend::CUDA;
case Backend::CUDA:
return Backend::CUDA;
case Backend::SparseCPU:
return Backend::SparseCUDA;
case Backend::SparseCUDA:
return Backend::SparseCUDA;
case Backend::Undefined:
return Backend::Undefined;
default:
AT_ERROR("Unknown backend");
}
}

constexpr DeviceType kCPU = DeviceType::CPU;
constexpr DeviceType kCUDA = DeviceType::CUDA;

static inline const char* toString(Backend b) {
switch (b) {
case Backend::CPU:
Expand Down
2 changes: 1 addition & 1 deletion aten/src/ATen/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Context::Context()
THSetDefaultErrorHandler(errorHandler,nullptr);
THSetDefaultArgErrorHandler(argErrorHandler,nullptr);

generator_registry[static_cast<int>(Backend::CPU)]
generator_registry[static_cast<int>(DeviceType::CPU)]
.reset(new CPUGenerator(this));
Type::registerCPU(this);
}
Expand Down
23 changes: 14 additions & 9 deletions aten/src/ATen/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class AT_API Context {
return type_registry[static_cast<int>(p)][static_cast<int>(s)].get();
}
Type * getTypeOpt(Backend p, ScalarType s) {
initCUDAIfNeeded(p);
if (p != Backend::Undefined) initCUDAIfNeeded(backendToDeviceType(p));
auto type = getTypeRaw(p, s);

if(!type) {
Expand All @@ -42,11 +42,11 @@ class AT_API Context {
if (!type) AT_ERROR(toString(p), toString(s), "Type is not enabled.");
return *type;
}
Generator & defaultGenerator(Backend p) {
initCUDAIfNeeded(p);
auto & generator = generator_registry[static_cast<int>(p)];
Generator & defaultGenerator(DeviceType device_type) {
initCUDAIfNeeded(device_type);
auto & generator = generator_registry[static_cast<int>(device_type)];
if(!generator)
AT_ERROR(toString(p), " backend type not enabled.");
AT_ERROR(DeviceTypeName(device_type), " backend type not enabled.");
return *generator;
}
bool hasMKL() const;
Expand All @@ -64,7 +64,7 @@ class AT_API Context {
THCState* lazyInitCUDA() {
std::call_once(thc_init,[&] {
thc_state = detail::getCUDAHooks().initCUDA();
generator_registry[static_cast<int>(Backend::CUDA)] =
generator_registry[static_cast<int>(DeviceType::CUDA)] =
detail::getCUDAHooks().initCUDAGenerator(this);
detail::getCUDAHooks().registerCUDATypes(this);
});
Expand Down Expand Up @@ -95,16 +95,17 @@ class AT_API Context {
bool deterministicCuDNN() const;
void setDeterministicCuDNN(bool);
std::unique_ptr<Generator>
generator_registry[static_cast<int>(Backend::NumOptions)];
generator_registry[static_cast<int>(DeviceType::COMPILE_TIME_MAX_DEVICE_TYPES)];
private:
// NB: type_registry has nullptr for all CUDA backends until
// CUDA initialization has occurred
std::unique_ptr<Type> type_registry
[static_cast<int>(Backend::NumOptions)]
[static_cast<int>(ScalarType::NumOptions)];
void initCUDAIfNeeded(Backend p) {
if(p == Backend::CUDA)
void initCUDAIfNeeded(DeviceType p) {
if (p == DeviceType::CUDA) {
lazyInitCUDA();
}
}
std::once_flag thc_init;
bool enabled_cudnn = true;
Expand Down Expand Up @@ -132,6 +133,10 @@ static inline Type& getType(Backend p, ScalarType s) {
return globalContext().getType(p, s);
}

static inline Type& getType(DeviceType p, ScalarType s) {
return globalContext().getType(deviceTypeToBackend(p), s);
}

static inline Type& CPU(ScalarType s) {
return getType(Backend::CPU, s);
}
Expand Down
23 changes: 2 additions & 21 deletions aten/src/ATen/Device.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#pragma once

#include <ATen/ScalarType.h>
#include <ATen/ATenGeneral.h>
#include <ATen/core/Error.h>
#include <ATen/core/DeviceType.h>
#include <ATen/core/Error.h>
#include <ATen/Backend.h>

#include <cstddef>
#include <iosfwd>
Expand All @@ -24,21 +25,6 @@ namespace at {
struct Device {
using Type = at::DeviceType;

/// Converts a `Backend` to a `DeviceType` if possible.
static DeviceType backend_to_type(Backend backend) {
switch (backend) {
case kCPU:
case kSparseCPU:
return DeviceType::CPU;
case kCUDA:
case kSparseCUDA:
return DeviceType::CUDA;
default:
AT_ERROR(
"Invalid backend ", toString(backend), " for Device construction");
}
}

/// Constructs a new `Device` from a `DeviceType` and an optional device
/// index.
/* implicit */ Device(DeviceType type, int32_t index = -1)
Expand All @@ -60,11 +46,6 @@ struct Device {
/// `<device-index>` optionally specifies a device index.
/* implicit */ Device(const std::string& device_string);

/// Constructs a new `Device` from a `Backend` (which is converted to a
/// `DeviceType`, if possible) and an optional device index.
/* implicit */ Device(Backend backend, int32_t index = -1)
: Device(backend_to_type(backend), index) {}

/// Returns true if the type and index of this `Device` matches that of
/// `other`.
bool operator==(const Device& other) const noexcept {
Expand Down
2 changes: 1 addition & 1 deletion aten/src/ATen/Formatting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ std::ostream& print(std::ostream& stream, const Tensor & tensor_, int64_t linesi
stream << "size:\n" << tensor_.sizes() << "\n";
stream << "]";
} else {
Type& cpudouble = tensor_.type().toBackend(kCPU).toScalarType(kDouble);
Type& cpudouble = tensor_.type().toBackend(Backend::CPU).toScalarType(kDouble);
Tensor tensor = tensor_.toType(cpudouble).contiguous();
if(tensor.ndimension() == 0) {
stream << defaultfloat << tensor.data<double>()[0] << std::endl;
Expand Down
14 changes: 10 additions & 4 deletions aten/src/ATen/TensorOptions.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <ATen/Backend.h>
#include <ATen/Context.h>
#include <ATen/Device.h>
#include <ATen/DeviceGuard.h>
Expand Down Expand Up @@ -67,7 +68,7 @@ struct AT_API TensorOptions {
type_ = &type;
}
this->dtype(type.scalarType());
this->device({type.backend(), device_index});
this->device({backendToDeviceType(type.backend()), device_index});
this->layout(type.layout());
}

Expand All @@ -84,7 +85,12 @@ struct AT_API TensorOptions {
/// Constructs a `TensorOptions` object from a backend, forwarded to the
/// `Device` constructor.
/* implicit */ TensorOptions(Backend backend)
: TensorOptions(Device(backend)) {}
: TensorOptions(Device(backendToDeviceType(backend))) {}

/// Constructs a `TensorOptions` object from a device type, forwarded to the
/// `Device` constructor.
/* implicit */ TensorOptions(DeviceType device_type)
: TensorOptions(Device(device_type)) {}

/// Constructs a `TensorOptions` object with the given dtype.
/* implicit */ TensorOptions(ScalarType dtype) : TensorOptions() {
Expand Down Expand Up @@ -190,9 +196,9 @@ struct AT_API TensorOptions {
Backend backend() const noexcept {
Backend backend;
if (device_.type() == Device::Type::CPU) {
backend = (layout_ == kStrided) ? kCPU : kSparseCPU;
backend = (layout_ == kStrided) ? Backend::CPU : Backend::SparseCPU;
} else {
backend = (layout_ == kStrided) ? kCUDA : kSparseCUDA;
backend = (layout_ == kStrided) ? Backend::CUDA : Backend::SparseCUDA;
}
return backend;
}
Expand Down
32 changes: 12 additions & 20 deletions aten/src/ATen/core/Macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,22 @@
// static library (in which case, saying the symbol is coming
// from a DLL would be incorrect).

#define AT_CORE_EXPORT
#define AT_CORE_IMPORT

#ifdef _WIN32
#ifndef AT_CORE_STATIC_WINDOWS
#undef AT_CORE_EXPORT
#undef AT_CORE_IMPORT
#define AT_CORE_EXPORT __declspec(dllexport)
#define AT_CORE_IMPORT __declspec(dllimport)
#endif // !defined(AT_CORE_STATIC_WINDOWS)
#else // _WIN32
#if defined(__GNUC__) || defined(__llvm__)
#undef AT_CORE_EXPORT
#undef AT_CORE_IMPORT
#define AT_CORE_EXPORT __attribute__((__visibility__("default")))
#define AT_CORE_IMPORT AT_CORE_EXPORT
#endif // defined(__GNUC__) || defined(__llvm__)
#endif // _WIN32

#if !defined(AT_CORE_STATIC_WINDOWS)
// TODO: unfiy the controlling macros.
#if defined(CAFFE2_BUILD_MAIN_LIBS) || defined(ATen_cpu_EXPORTS) || defined(caffe2_EXPORTS)
#define AT_CORE_API AT_CORE_EXPORT
#define AT_CORE_API __declspec(dllexport)
#else // defined(CAFFE2_BUILD_MAIN_LIBS) || defined(ATen_cpu_EXPORTS) || defined(caffe2_EXPORTS)
#define AT_CORE_API AT_CORE_IMPORT
#define AT_CORE_API __declspec(dllimport)
#endif // defined(CAFFE2_BUILD_MAIN_LIBS) || defined(ATen_cpu_EXPORTS) || defined(caffe2_EXPORTS)
#else // !defined(AT_CORE_STATIC_WINDOWS)
#define AT_CORE_API
#endif // !defined(AT_CORE_STATIC_WINDOWS)
#else // _WIN32
#if defined(__GNUC__)
#define AT_CORE_API __attribute__((__visibility__("default")))
#endif // defined(__GNUC__)
#endif // _WIN32

// Disable the copy and assignment operator for a class. Note that this will
// disable the usage of the class in std containers.
Expand Down
39 changes: 33 additions & 6 deletions aten/src/ATen/core/typeid.h
Original file line number Diff line number Diff line change
Expand Up @@ -391,26 +391,53 @@ inline bool operator!=(const TypeMeta& lhs, const TypeMeta& rhs) noexcept {
*
* NOTE: the macro needs to be invoked in ::caffe2 namespace
*/

// Implementation note: in MSVC, we will need to prepend the AT_CORE_API
// keyword in order to get things compiled properly. in Linux, gcc seems to
// create attribute ignored error for explicit template instantiations, see
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0537r0.html
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=51930
// and as a result, we define these two macros slightly differently.
// TODO(jiayq): AT_CORE_API below is not correct, because we may use the
// definition in third party dependent libraries. The proper way is to use
// CAFFE2_EXPORT (which explicitly requires dllexport). Marking this as a
// todo item when the unified build is finished.
#ifdef _MSC_VER
#define CAFFE_KNOWN_TYPE(T) \
template <> \
AT_CORE_EXPORT TypeIdentifier TypeMeta::Id<T>() { \
AT_CORE_API TypeIdentifier TypeMeta::Id<T>() { \
static const TypeIdentifier type_id = TypeIdentifier::createTypeId(); \
static TypeNameRegisterer<T> registerer(type_id, #T); \
return type_id; \
}
#else // _MSC_VER
#define CAFFE_KNOWN_TYPE(T) \
template <> \
TypeIdentifier TypeMeta::Id<T>() { \
static const TypeIdentifier type_id = TypeIdentifier::createTypeId(); \
static TypeNameRegisterer<T> registerer(type_id, #T); \
return type_id; \
}
#endif

/**
* CAFFE_DECLARE_KNOWN_TYPE and CAFFE_DEFINE_KNOWN_TYPE are used
* to preallocate ids for types that are queried very often so that they
* can be resolved at compile time. Please use CAFFE_KNOWN_TYPE() instead
* for your own types to allocate dynamic ids for them.
*/
#define CAFFE_DECLARE_KNOWN_TYPE(PreallocatedId, T) \
template <> \
AT_CORE_EXPORT inline AT_CORE_API TypeIdentifier TypeMeta::Id<T>() { \
return TypeIdentifier(PreallocatedId); \
#ifdef _MSC_VER
#define CAFFE_DECLARE_KNOWN_TYPE(PreallocatedId, T) \
template <> \
inline AT_CORE_API TypeIdentifier TypeMeta::Id<T>() { \
return TypeIdentifier(PreallocatedId); \
}
#else // _MSC_VER
#define CAFFE_DECLARE_KNOWN_TYPE(PreallocatedId, T) \
template <> \
inline TypeIdentifier TypeMeta::Id<T>() { \
return TypeIdentifier(PreallocatedId); \
}
#endif

#define CONCAT_IMPL(x, y) x##y
#define MACRO_CONCAT(x, y) CONCAT_IMPL(x, y)
Expand Down
Loading