diff --git a/aten/src/ATen/ATen.h b/aten/src/ATen/ATen.h index 84568880fcf8..d962215268cc 100644 --- a/aten/src/ATen/ATen.h +++ b/aten/src/ATen/ATen.h @@ -9,6 +9,7 @@ #include "ATen/Context.h" #include "ATen/Storage.h" #include "ATen/Tensor.h" +#include "ATen/Device.h" #include "ATen/TensorGeometry.h" #include "ATen/Functions.h" #include "ATen/Formatting.h" @@ -16,3 +17,6 @@ #include "ATen/TensorMethods.h" #include "ATen/Dispatch.h" #include "ATen/DimVector.h" +#include "ATen/DeviceGuard.h" +#include "ATen/TensorOptions.h" +#include "ATen/Layout.h" diff --git a/aten/src/ATen/Declarations.cwrap b/aten/src/ATen/Declarations.cwrap index 22316fb85cd1..edfef43cb29e 100644 --- a/aten/src/ATen/Declarations.cwrap +++ b/aten/src/ATen/Declarations.cwrap @@ -2,7 +2,7 @@ name: storageOffset python_name: storage_offset cpu_half: True - auto_gpu: False + device_guard: False return: long arguments: - THTensor* self @@ -11,7 +11,7 @@ name: nDimension python_name: ndimension cpu_half: True - auto_gpu: False + device_guard: False return: long arguments: - THTensor* self @@ -31,7 +31,7 @@ name: set_ cname: set cpu_half: True - auto_gpu: False + device_guard: False return: argument 0 options: - cname: set @@ -78,7 +78,7 @@ name: isContiguous python_name: is_contiguous cpu_half: True - auto_gpu: False + device_guard: False return: bool arguments: - THTensor* self @@ -87,7 +87,7 @@ name: isSetTo python_name: is_set_to cpu_half: True - auto_gpu: False + device_guard: False return: bool arguments: - THTensor* self @@ -168,7 +168,7 @@ [[ name: view cname: newView - auto_gpu: False + device_guard: False return: THTensor* arguments: - THTensor* self @@ -277,7 +277,7 @@ [[ name: unfold cpu_half: True - auto_gpu: False + device_guard: False return: argument 0 arguments: - arg: THTensor* result @@ -378,7 +378,7 @@ [[ name: data_ptr with_gil: True - auto_gpu: False + device_guard: False return: void* cpu_half: True cname: data @@ -1037,6 +1037,7 @@ [[ name: getDevice python_name: get_device + device_guard: False backends: - CUDA aten_sparse: True @@ -1713,7 +1714,7 @@ // norm(value) for a sparse tensor returns a DENSE 0-dim tensor if (self.is_sparse()) { auto result = ${THTensor}_normall(${state,} self_->tensor, convert<${THScalarType}>(p_)); - return toDense().tensor({}).fill_(result); + return at::native::full({}, result, toDense()); } // aten_custom_call is followed by the generated call to normall ]] @@ -3952,7 +3953,7 @@ aten_custom_call: | if (self_->isScalar()) { // Empty tensor - return toDense().toScalarType(kLong).tensor({0}); + return at::native::empty({0}, toDense().toScalarType(kLong)); } ]] diff --git a/aten/src/ATen/Device.cpp b/aten/src/ATen/Device.cpp new file mode 100644 index 000000000000..14ad860b380e --- /dev/null +++ b/aten/src/ATen/Device.cpp @@ -0,0 +1,100 @@ +#include + +#include + +#include +#include +#include +#include + +namespace at { +namespace { +std::pair parse_type(const std::string& device_string) { + auto position = device_string.find("cpu"); + if (position != std::string::npos) { + return {Device::Type::CPU, 3}; + } + position = device_string.find("cuda"); + if (position != std::string::npos) { + return {Device::Type::CUDA, 4}; + } + AT_ERROR("Expected 'cpu' or 'cuda' device type at start of device string"); +} +} // namespace + +// `std::regex` is still in a very incomplete state in GCC 4.8.x, +// so we have to do our own parsing, like peasants. +// https://stackoverflow.com/questions/12530406/is-gcc-4-8-or-earlier-buggy-about-regular-expressions +// +// Replace with the following code once we shed our GCC skin: +// +// static const std::regex regex( +// "(cuda|cpu)|(cuda|cpu):([0-9]+)|([0-9]+)", +// std::regex_constants::basic); +// std::smatch match; +// const bool ok = std::regex_match(device_string, match, regex); +// AT_CHECK(ok, "Invalid device string: '", device_string, "'"); +// if (match[1].matched) { +// type_ = parse_type_from_string(match[1].str()); +// } else { +// if (match[2].matched) { +// type_ = parse_type_from_string(match[1].str()); +// } else { +// type_ = Type::CUDA; +// } +// AT_ASSERT(match[3].matched); +// index_ = std::stoi(match[3].str()); +// } +Device::Device(const std::string& device_string) : Device(Type::CPU) { + AT_CHECK(!device_string.empty(), "Device string must not be empty"); + + size_t position; + std::tie(type_, position) = parse_type(device_string); + + // e.g. 'cuda', 'cpu'. + if (position == device_string.size()) { + return; + } + + AT_CHECK( + device_string[position] == ':', + "Expected ':' to separate device type from index in device string"); + // Skip the colon. + position += 1; + + const auto index_string = device_string.substr(position); + try { + index_ = std::stoi(index_string); + } catch (const std::exception&) { + AT_ERROR( + "Could not parse device index '", + index_string, + "' in device string '", + device_string, + "'"); + } +} + +} // namespace at + +std::ostream& operator<<(std::ostream& stream, at::Device::Type type) { + switch (type) { + case at::Device::Type::CPU: { + stream << "cpu"; + break; + } + case at::Device::Type::CUDA: { + stream << "cuda"; + break; + } + } + return stream; +} + +std::ostream& operator<<(std::ostream& stream, const at::Device& device) { + stream << device.type(); + if (device.has_index()) { + stream << ":" << device.index(); + } + return stream; +} diff --git a/aten/src/ATen/Device.h b/aten/src/ATen/Device.h new file mode 100644 index 000000000000..a6afa187e550 --- /dev/null +++ b/aten/src/ATen/Device.h @@ -0,0 +1,114 @@ +#pragma once + +#include +#include + +#include +#include +#include + +namespace at { +/// Represents a a compute device on which a tensor is located. A device is +/// uniquely identified by a type, which specifies the type of machine it is +/// (e.g. CPU or CUDA GPU), and a device index or ordinal, which identifies the +/// specific compute device when there is more than one of a certain type. The +/// device index is optional, and in its defaulted state represents (abstractly) +/// "the current device". Further, there are two constraints on the value of the +/// device index, if one is explicitly stored: +/// 1. A negative index represents the current device, a non-negative index +/// represents a specific, concrete device, +/// 2. When the device type is CPU, the device index must be zero. +struct Device { + /// The possible values of the device *type*. + enum class Type { CPU, CUDA }; + + /// Converts a `Backend` to a `Device::Type` if possible. + static Type backend_to_type(Backend backend) { + switch (backend) { + case kCPU: + case kSparseCPU: + return Type::CPU; + case kCUDA: + case kSparseCUDA: + return Type::CUDA; + default: + AT_ERROR( + "Invalid backend ", toString(backend), " for Device construction"); + } + } + + /// Constructs a new `Device` from a `Type` and an optional device index. + /* implicit */ Device(Type type, int32_t index = -1) + : type_(type), index_(index) { + AT_CHECK( + index == -1 || index >= 0, + "Device index must be -1 or non-negative, got ", + index); + AT_CHECK( + !is_cpu() || index <= 0, + "CPU device index must be -1 or zero, got ", + index); + } + + /// Constructs a `Device` from a string description, for convenience. + /// The string supplied must follow the following schema: + /// `(cpu|cuda):[]` + /// where `cpu:` or `cuda:` specifies the device type, and + /// `` optionally specifies a device index. + /* implicit */ Device(const std::string& device_string); + + /// Constructs a new `Device` from a `Backend` (which is converted to a + /// `Type`, 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 { + return this->type_ == other.type_ && this->index_ == other.index_; + } + + /// Returns true if the type or index of this `Device` differs from that of + /// `other`. + bool operator!=(const Device& other) const noexcept { + return !(*this == other); + } + + /// Sets the device index. + void set_index(int32_t index) { + index_ = index; + } + + /// Returns the type of device this is. + Type type() const noexcept { + return type_; + } + + /// Returns the optional index. + const int32_t& index() const noexcept { + return index_; + } + + /// Returns true if the device has a non-default index. + bool has_index() const noexcept { + return index_ != -1; + } + + /// Return true if the device is of CUDA type. + bool is_cuda() const noexcept { + return type_ == Type::CUDA; + } + + /// Return true if the device is of CPU type. + bool is_cpu() const noexcept { + return type_ == Type::CPU; + } + + private: + Type type_; + int32_t index_ = -1; +}; +} // namespace at + +std::ostream& operator<<(std::ostream& stream, at::Device::Type type); +std::ostream& operator<<(std::ostream& stream, const at::Device& device); diff --git a/aten/src/ATen/DeviceGuard.h b/aten/src/ATen/DeviceGuard.h new file mode 100644 index 000000000000..3d6811c55308 --- /dev/null +++ b/aten/src/ATen/DeviceGuard.h @@ -0,0 +1,102 @@ +#pragma once + +#include +#include +#include +#include +#include + +#include + +namespace at { +/// RAII guard that sets a certain default GPU index in its constructor, and +/// changes it back to the device that was originally active upon destruction. +/// +/// The index is always reset to the one that was active at the time of +/// construction of the guard. Even if you `set_index` after construction, the +/// destructor will still reset the index to the one that was active at +/// construction time. +struct DeviceGuard { + /// Default constructor, does nothing. + DeviceGuard() = default; + + /// Uses the given device's `index()` if it is a CUDA device, else does + /// nothing. + explicit DeviceGuard(Device device) { + if (device.is_cuda()) { + set_index(device.index()); + } + } + + /// Calls `set_device` with the given index. + explicit DeviceGuard(int32_t index) { + set_index(index); + } + + /// Sets the device to the index on which the given tensor is located. + explicit DeviceGuard(const Tensor& tensor) { + set_index_from(tensor); + } + + /// Sets the device to the index on which the first tensor in the list is + /// located. If the list is empty, does nothing. + explicit DeviceGuard(const TensorList& tensors) { + if (!tensors.empty()) { + set_index_from(tensors.front()); + } + } + + /// Resets the device to the index that was active at construction of the + /// guard. + ~DeviceGuard() { + // It should only not have a value if an index was never actually set. + if (original_index_ != -1) { + // Unchecked because we don't want to throw in the destructor. + detail::DynamicCUDAInterface::unchecked_set_device(original_index_); + } + } + + /// Sets the device to the given one if its index is not `nullopt`. + void set_index(int32_t index) { + if (index == -1) { + return; + } + AT_ASSERT(index >= 0); + if (original_index_ == -1) { + int32_t previous_index = -123; + detail::DynamicCUDAInterface::get_device(&previous_index); + original_index_ = previous_index; + if (index != original_index_) { + detail::DynamicCUDAInterface::set_device(index); + } + } else { + detail::DynamicCUDAInterface::set_device(index); + } + last_index_ = index; + } + + /// Calls `set_index` with the `Tensor`'s current device, if it is a CUDA + /// tensor. Does nothing if the `tensor` is not defined. + void set_index_from(const Tensor& tensor) { + if (tensor.defined() && tensor.is_cuda()) { + set_index(tensor.get_device()); + } + } + + /// Returns the device that was set upon construction of the guard. + int32_t original_index() const noexcept { + return original_index_; + } + + // /// Returns the last device that was set via `set_device`, if any. + int32_t last_index() const noexcept { + return last_index_; + } + + private: + /// The original device that was active at construction of this object. + int32_t original_index_ = -1; + /// The last index that was set via `set_device`. + int32_t last_index_ = -1; +}; +} // namespace at diff --git a/aten/src/ATen/Layout.h b/aten/src/ATen/Layout.h new file mode 100644 index 000000000000..010248a010a5 --- /dev/null +++ b/aten/src/ATen/Layout.h @@ -0,0 +1,20 @@ +#pragma once + +#include + +namespace at { +enum class Layout { Strided, Sparse }; + +constexpr auto kStrided = Layout::Strided; +constexpr auto kSparse = Layout::Sparse; + +inline Layout layout_from_backend(Backend backend) { + switch (backend) { + case Backend::SparseCPU: + case Backend::SparseCUDA: + return Layout::Sparse; + default: + return Layout::Strided; + } +} +} // namespace at diff --git a/aten/src/ATen/TensorOptions.h b/aten/src/ATen/TensorOptions.h new file mode 100644 index 000000000000..c7f416d64fb1 --- /dev/null +++ b/aten/src/ATen/TensorOptions.h @@ -0,0 +1,208 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +#include +#include + +namespace at { + +/// A class to encapsulate construction axes of a `Tensor`. +/// `TensorOptions` is a virtual class to enable overriding of certain methods +/// by subclasses in other libraries, such as PyTorch. In PyTorch, there is a +/// `torch::TensorOptions` subclass of this `TensorOptions`, which changes +/// `type()` to return a variable type instead of a tensor type, such that +/// variables are created inside factory methods, instead of tensors. +struct TensorOptions { + /// Constructs the `TensorOptions` with valid defaults, which are: + /// - dtype: float + /// - device: CPU + /// - layout: strided + /// - requires_grad: false + TensorOptions() = default; + + /// Constructs the `TensorOptions` from the type of the given `Tensor`. + /// If the `Tensor` has a CUDA type, the `device_index` will match that of the + /// tensor. See the constructor from `Type` for the semantics w.r.t. the + /// `type()` method. + explicit TensorOptions(Tensor tensor, bool discard_runtime_type = false) { + if (!discard_runtime_type) { + type_ = &tensor.type(); + } + this->dtype(tensor.dtype()); + this->device(tensor.device()); + this->layout(tensor.layout()); + } + + /// Constructs the `TensorOptions` from a type and a `device_index`. + /// + /// If `discard_runtime_type` is false (the default), the behavior of + /// `TensorOptions::type()` is changed in that it will always return this + /// `type`, irrespective of any `device` or `dtype` or `layout` specified at a + /// later time. This is to ensure that when a `TensorOptions` object is + /// constructed from a tensor's type, and that type has a dynamic type other + /// than `at::Type` (e.g. `torch::autograd::VariableType`), constructing a new + /// tensor from this `TensorOptions` will use this same derived type. If + /// instead the given `type` were destructured into its components (backend, + /// dtype and layout), information about the runtime type of the `Type` would + /// be lost. Set `discard_runtime_type` to `true` to always destructure the + /// type into its components and discard its runtime type. + /* implicit */ TensorOptions( + const Type& type, + int32_t device_index = -1, + bool discard_runtime_type = false) { + if (!discard_runtime_type) { + type_ = &type; + } + this->dtype(type.scalarType()); + this->device({type.backend(), device_index}); + this->layout(type.layout()); + } + + /// Constructs a `TensorOptions` object with the given layout. + /* implicit */ TensorOptions(Layout layout) : TensorOptions() { + this->layout(layout); + } + + /// Constructs a `TensorOptions` object with the given device. + /* implicit */ TensorOptions(Device device) : TensorOptions() { + this->device(device); + } + + /// Constructs a `TensorOptions` object from a backend, forwarded to the + /// `Device` constructor. + /* implicit */ TensorOptions(Backend backend) + : TensorOptions(Device(backend)) {} + + /// Constructs a `TensorOptions` object with the given dtype. + /* implicit */ TensorOptions(ScalarType dtype) : TensorOptions() { + this->dtype(dtype); + } + + /// Discards the runtime type stored if the `TensorOptions` was constructed + /// from a `Tensor` or a `Type`. See the documentation of the constructor from + /// a `Type` for implications on the behavior of the `type()` method on + /// `TensorOptions`. + const TensorOptions& discard_runtime_type() const { + type_ = nullptr; + return *this; + } + + // NOTE: These methods are defined in TensorOptions.cpp because I get funny + // linker errors for their missing definition if they're defined in the + // header. Who knows why? + + /// Sets the device of the `TensorOptions`. + TensorOptions& device(Device device) { + device_ = std::move(device); + return *this; + } + + /// Sets the device of the `TensorOptions` to CUDA, and then sets the device + /// index to the given one. + TensorOptions& device_index(int32_t device_index) { + return device({Device::Type::CUDA, device_index}); + } + + /// Sets the dtype of the `TensorOptions`. + TensorOptions& dtype(ScalarType dtype) { + dtype_ = dtype; + return *this; + } + + /// Sets the layout of the `TensorOptions`. + TensorOptions& layout(Layout layout) { + layout_ = layout; + return *this; + } + + /// Sets the `requires_grad` property of the `TensorOptions`. + TensorOptions& requires_grad(bool requires_grad = true) { + requires_grad_ = requires_grad; + return *this; + } + + /// Returns the device of the `TensorOptions`. + const Device& device() const noexcept { + return device_; + } + + /// Returns the device index of the `TensorOptions`. + int32_t device_index() const noexcept { + return device_.index(); + } + + /// Returns the dtype of the `TensorOptions`. + ScalarType dtype() const noexcept { + return dtype_; + } + + /// Returns the layout of the `TensorOptions`. + Layout layout() const noexcept { + return layout_; + } + + /// Returns the `requires_grad` property of the `TensorOptions`. + bool requires_grad() const noexcept { + return requires_grad_; + } + + /// Constructs an `at::Type` from the members of the `TensorOptions`. + const Type& type() const { + if (type_ != nullptr) { + return *type_; + } + Backend backend; + if (device_.type() == Device::Type::CPU) { + backend = (layout_ == kStrided) ? kCPU : kSparseCPU; + } else { + backend = (layout_ == kStrided) ? kCUDA : kSparseCUDA; + } + return getType(backend, dtype_); + } + + protected: + ScalarType dtype_{kFloat}; + Device device_{Device::Type::CPU}; + Layout layout_{Layout::Strided}; + bool requires_grad_{false}; + // Not part of the observable API, so make `mutable` so we can set it to + // `null` in `discard_runtime_type`. + mutable const Type* type_{nullptr}; +}; + +/// Convenience function that returns a `TensorOptions` object with the `dtype` +/// set to the given one. +inline TensorOptions dtype(ScalarType dtype) { + return TensorOptions().dtype(dtype); +} + +/// Convenience function that returns a `TensorOptions` object with the `layout` +/// set to the given one. +inline TensorOptions layout(Layout layout) { + return TensorOptions().layout(layout); +} + +/// Convenience function that returns a `TensorOptions` object with the `device` +/// set to the given one. +inline TensorOptions device(Device device) { + return TensorOptions().device(std::move(device)); +} + +/// Convenience function that returns a `TensorOptions` object with the +/// `device_index` set to the given one. +inline TensorOptions device_index(int32_t device_index) { + return TensorOptions().device_index(device_index); +} + +/// Convenience function that returns a `TensorOptions` object with the +/// `requires_grad` set to the given one. +inline TensorOptions requires_grad(bool requires_grad = true) { + return TensorOptions().requires_grad(requires_grad); +} +} // namespace at diff --git a/aten/src/ATen/cuda/detail/CUDAHooks.cpp b/aten/src/ATen/cuda/detail/CUDAHooks.cpp index 11b68688d137..69388946ce7e 100644 --- a/aten/src/ATen/cuda/detail/CUDAHooks.cpp +++ b/aten/src/ATen/cuda/detail/CUDAHooks.cpp @@ -1,9 +1,12 @@ #include -#include + #include +#include +#include #include #include -#include +#include +#include #include "THC/THC.h" @@ -13,22 +16,69 @@ #include -namespace at { namespace cuda { namespace detail { +#include +#include +#include + +namespace at { +namespace cuda { +namespace detail { +namespace { + +void check_status(int32_t status) { + AT_CHECK( + static_cast(status) == cudaSuccess, + "CUDA error (", + static_cast(status), + "): ", + cudaGetErrorString(static_cast(status))); +} + +void set_device(int32_t device) { + check_status(cudaSetDevice(device)); +} + +void get_device(int32_t* device) { + check_status(cudaGetDevice(device)); +} + +void unchecked_set_device(int32_t device) { + const auto return_code = cudaSetDevice(device); + (void)return_code; +} + +struct DynamicCUDAInterfaceSetter { + DynamicCUDAInterfaceSetter() { + at::detail::DynamicCUDAInterface::set_device = set_device; + at::detail::DynamicCUDAInterface::get_device = get_device; + at::detail::DynamicCUDAInterface::unchecked_set_device = + unchecked_set_device; + } +}; + +// Single, global, static (because of the anonymous namespace) instance, whose +// constructor will set the static members of `DynamicCUDAInterface` to CUDA +// functions when the ATen CUDA library is loaded. +DynamicCUDAInterfaceSetter _; +} // namespace // NB: deleter is dynamic, because we need it to live in a separate // compilation unit (alt is to have another method in hooks, but // let's not if we don't need to!) -std::unique_ptr CUDAHooks::initCUDA() const { +std::unique_ptr CUDAHooks::initCUDA() const { THCState* thc_state = THCState_alloc(); THCState_setDeviceAllocator(thc_state, THCCachingAllocator_get()); thc_state->cudaHostAllocator = &THCCachingHostAllocator; THCudaInit(thc_state); - return std::unique_ptr(thc_state, [](THCState* p) { - if (p) THCState_free(p); + return std::unique_ptr( + thc_state, [](THCState* p) { + if (p) + THCState_free(p); }); } -std::unique_ptr CUDAHooks::initCUDAGenerator(Context* context) const { +std::unique_ptr CUDAHooks::initCUDAGenerator( + Context* context) const { return std::unique_ptr(new CUDAGenerator(context)); } @@ -48,13 +98,18 @@ bool CUDAHooks::hasCuDNN() const { cudaStream_t CUDAHooks::getCurrentCUDAStream(THCState* thc_state) const { return THCState_getCurrentStream(thc_state); } -cudaStream_t CUDAHooks::getCurrentCUDAStreamOnDevice(THCState* thc_state, int64_t device) const { +cudaStream_t CUDAHooks::getCurrentCUDAStreamOnDevice( + THCState* thc_state, + int64_t device) const { return THCState_getCurrentStreamOnDevice(thc_state, device); } -struct cudaDeviceProp* CUDAHooks::getCurrentDeviceProperties(THCState* thc_state) const { +struct cudaDeviceProp* CUDAHooks::getCurrentDeviceProperties( + THCState* thc_state) const { return THCState_getCurrentDeviceProperties(thc_state); } -struct cudaDeviceProp* CUDAHooks::getDeviceProperties(THCState* thc_state, int device) const { +struct cudaDeviceProp* CUDAHooks::getDeviceProperties( + THCState* thc_state, + int device) const { return THCState_getDeviceProperties(thc_state, device); } @@ -81,10 +136,13 @@ bool CUDAHooks::compiledWithCuDNN() const { bool CUDAHooks::supportsDilatedConvolutionWithCuDNN() const { #if AT_CUDNN_ENABLED() - cudaDeviceProp* prop = getCurrentDeviceProperties(globalContext().getTHCState()); + cudaDeviceProp* prop = + getCurrentDeviceProperties(globalContext().getTHCState()); // NOTE: extra parenthesis around numbers disable clang warnings about // dead code - return ((CUDNN_VERSION >= (6021)) || (CUDNN_VERSION >= (6000) && prop->major >= 5)); + return ( + (CUDNN_VERSION >= (6021)) || + (CUDNN_VERSION >= (6000) && prop->major >= 5)); #else return false; #endif @@ -102,7 +160,8 @@ double CUDAHooks::batchnormMinEpsilonCuDNN() const { #if AT_CUDNN_ENABLED() return CUDNN_BN_MIN_EPSILON; #else - AT_ERROR("Cannot query CUDNN_BN_MIN_EPSILON if ATen_cuda is not built with CuDNN"); + AT_ERROR( + "Cannot query CUDNN_BN_MIN_EPSILON if ATen_cuda is not built with CuDNN"); #endif } @@ -112,15 +171,18 @@ int CUDAHooks::getNumGPUs() const { if (err == cudaErrorNoDevice) { return 0; } else if (err != cudaSuccess) { - AT_ERROR("CUDA error (", static_cast(err), "): ", cudaGetErrorString(err)); + AT_ERROR( + "CUDA error (", static_cast(err), "): ", cudaGetErrorString(err)); } return count; } // Sigh, the registry doesn't support namespaces :( -using at::RegistererCUDAHooksRegistry; using at::CUDAHooksRegistry; +using at::RegistererCUDAHooksRegistry; REGISTER_CUDA_HOOKS(CUDAHooks); -}}} // namespace at::cuda::detail +} // namespace detail +} // namespace cuda +} // namespace at diff --git a/aten/src/ATen/detail/CUDAHooksInterface.cpp b/aten/src/ATen/detail/CUDAHooksInterface.cpp index c6528c537fcc..b6c31bed6eda 100644 --- a/aten/src/ATen/detail/CUDAHooksInterface.cpp +++ b/aten/src/ATen/detail/CUDAHooksInterface.cpp @@ -1,28 +1,59 @@ #include -namespace at { +#include +#include +#include +#include + +namespace at { namespace detail { - const CUDAHooksInterface& getCUDAHooks() { - static std::unique_ptr cuda_hooks; - // NB: The once_flag here implies that if you try to call any CUDA - // functionality before libATen_cuda.so is loaded, CUDA is permanently - // disabled for that copy of ATen. In principle, we can relax this - // restriction, but you might have to fix some code. See getVariableHooks() - // for an example where we relax this restriction (but if you try to avoid - // needing a lock, be careful; it doesn't look like Registry.h is thread - // safe...) - static std::once_flag once; - std::call_once(once, []{ - cuda_hooks = CUDAHooksRegistry()->Create("CUDAHooks", CUDAHooksArgs{}); - if (!cuda_hooks) { - cuda_hooks = std::unique_ptr(new CUDAHooksInterface()); - } - }); - return *cuda_hooks; - } + +void default_set_device(int32_t) { + AT_ERROR( + "DynamicCUDAInterface::set_device called " + "before CUDA library was loaded"); } +void default_get_device(int32_t*) { + AT_ERROR( + "DynamicCUDAInterface::get_device called " + "before CUDA library was loaded"); +} + +void default_unchecked_set_device(int32_t) { + AT_ERROR( + "DynamicCUDAInterface::unchecked_set_device called " + "before CUDA library was loaded"); +} + +// Default the static members of DynamicCUDAInterface. +void (*DynamicCUDAInterface::set_device)(int32_t) = default_set_device; +void (*DynamicCUDAInterface::get_device)(int32_t*) = default_get_device; +void (*DynamicCUDAInterface::unchecked_set_device)(int32_t) = + default_unchecked_set_device; + +const CUDAHooksInterface& getCUDAHooks() { + static std::unique_ptr cuda_hooks; + // NB: The once_flag here implies that if you try to call any CUDA + // functionality before libATen_cuda.so is loaded, CUDA is permanently + // disabled for that copy of ATen. In principle, we can relax this + // restriction, but you might have to fix some code. See getVariableHooks() + // for an example where we relax this restriction (but if you try to avoid + // needing a lock, be careful; it doesn't look like Registry.h is thread + // safe...) + static std::once_flag once; + std::call_once(once, [] { + cuda_hooks = CUDAHooksRegistry()->Create("CUDAHooks", CUDAHooksArgs{}); + if (!cuda_hooks) { + cuda_hooks = + std::unique_ptr(new CUDAHooksInterface()); + } + }); + return *cuda_hooks; +} +} // namespace detail + AT_DEFINE_REGISTRY(CUDAHooksRegistry, CUDAHooksInterface, CUDAHooksArgs) } // namespace at diff --git a/aten/src/ATen/detail/CUDAHooksInterface.h b/aten/src/ATen/detail/CUDAHooksInterface.h index e999ee222797..1e60605dadba 100644 --- a/aten/src/ATen/detail/CUDAHooksInterface.h +++ b/aten/src/ATen/detail/CUDAHooksInterface.h @@ -1,22 +1,26 @@ #pragma once -#include -#include -#include #include +#include +#include +#include + +#include +#include +#include // Forward declare these CUDA types here to avoid including CUDA headers in // ATen headers, which would make ATen always require CUDA to build. struct THCState; struct CUstream_st; -typedef struct CUstream_st *cudaStream_t; +typedef struct CUstream_st* cudaStream_t; struct cudaDeviceProp; namespace at { - class Context; +class Context; } -// NB: Class must live in at due to limitations of Registry.h +// NB: Class must live in `at` due to limitations of Registry.h. namespace at { // The CUDAHooksInterface is an omnibus interface for any CUDA functionality @@ -36,17 +40,16 @@ namespace at { // never forgets to implement each virtual function in the real implementation // in CUDAHooks. This probably doesn't buy us much though. struct AT_API CUDAHooksInterface { - // This should never actually be implemented, but it is used to // squelch -Werror=non-virtual-dtor virtual ~CUDAHooksInterface() {} // Initialize THCState and, transitively, the CUDA state - virtual std::unique_ptr initCUDA() const { + virtual std::unique_ptr initCUDA() const { AT_ERROR("cannot initialize CUDA without ATen_cuda library"); } - virtual std::unique_ptr initCUDAGenerator(Context*) const { + virtual std::unique_ptr initCUDAGenerator(Context*) const { AT_ERROR("cannot initialize CUDA generator without ATen_cuda library"); } @@ -62,7 +65,8 @@ struct AT_API CUDAHooksInterface { AT_ERROR("cannot getCurrentCUDAStream() without ATen_cuda library"); } - virtual cudaStream_t getCurrentCUDAStreamOnDevice(THCState*, int64_t device) const { + virtual cudaStream_t getCurrentCUDAStreamOnDevice(THCState*, int64_t device) + const { AT_ERROR("cannot getCurrentCUDAStream() without ATen_cuda library"); } @@ -70,7 +74,8 @@ struct AT_API CUDAHooksInterface { AT_ERROR("cannot getCurrentDeviceProperties() without ATen_cuda library"); } - virtual struct cudaDeviceProp* getDeviceProperties(THCState*, int device) const { + virtual struct cudaDeviceProp* getDeviceProperties(THCState*, int device) + const { AT_ERROR("cannot getDeviceProperties() without ATen_cuda library"); } @@ -99,13 +104,13 @@ struct AT_API CUDAHooksInterface { } virtual double batchnormMinEpsilonCuDNN() const { - AT_ERROR("cannot query batchnormMinEpsilonCuDNN() without ATen_cuda library"); + AT_ERROR( + "cannot query batchnormMinEpsilonCuDNN() without ATen_cuda library"); } virtual int getNumGPUs() const { return 0; } - }; // NB: dummy argument to suppress "ISO C++11 requires at least one argument @@ -113,10 +118,22 @@ struct AT_API CUDAHooksInterface { struct AT_API CUDAHooksArgs {}; AT_DECLARE_REGISTRY(CUDAHooksRegistry, CUDAHooksInterface, CUDAHooksArgs) -#define REGISTER_CUDA_HOOKS(clsname) AT_REGISTER_CLASS(CUDAHooksRegistry, clsname, clsname) +#define REGISTER_CUDA_HOOKS(clsname) \ + AT_REGISTER_CLASS(CUDAHooksRegistry, clsname, clsname) namespace detail { - AT_API const CUDAHooksInterface& getCUDAHooks(); -} +AT_API const CUDAHooksInterface& getCUDAHooks(); +/// This class exists to let us access `cudaSetDevice`, `cudaGetDevice` and CUDA +/// error handling functions, when CUDA is available. These functions will first +/// default to no-ops. When the `ATen` GPU library is loaded, they will be set to +/// the `cudaSetDevice`/`cudaGetDevice` functions. This allows us to access them +/// with only a single pointer indirection, while virtual dispatch would require +/// two (one for the virtual call, one for `cudaSetDevice`/`cudaGetDevice`). +struct AT_API DynamicCUDAInterface { + static void (*set_device)(int32_t); + static void (*get_device)(int32_t*); + static void (*unchecked_set_device)(int32_t); +}; +} // namespace detail } // namespace at diff --git a/aten/src/ATen/function_wrapper.py b/aten/src/ATen/function_wrapper.py index c34f97f3fad8..5befa25b7172 100644 --- a/aten/src/ATen/function_wrapper.py +++ b/aten/src/ATen/function_wrapper.py @@ -44,6 +44,7 @@ ${return_type} ${api_name}(${type_method_formals_with_defaults}) const; # 2. broadcasting functions are implemented in Type.cpp TYPE_METHOD_DEFINITION_BROADCAST = CodeTemplate("""\ ${return_type} Type::${api_name}(${type_method_formals}) const { + ${device_guard_declaration} Tensor ${broadcast_returns}; std::tie(${broadcast_returns}) = ${broadcast_function}(${broadcast_actuals}, "${api_name}"); return ${method_prefix_derived}${api_name}(${broadcast_modified_actuals}); @@ -69,11 +70,22 @@ ${return_type} Type::${method_prefix_derived}${api_name}(${type_method_formals}) TYPE_METHOD_DECLARATION_CONCRETE = CodeTemplate("""\ virtual ${return_type} ${api_name}(${type_method_formals_with_defaults}) const; """) +DEPRECATED_TYPE_METHOD_DECLARATION_CONCRETE = CodeTemplate("""\ +AT_DEPRECATED(virtual ${return_type} ${api_name}(${type_method_formals_with_defaults}) const); +""") TYPE_METHOD_DEFINITION_CONCRETE = CodeTemplate("""\ ${return_type} Type::${api_name}(${type_method_formals}) const { + ${device_guard_declaration} ${type_definition_body} } """) +DEPRECATED_TYPE_METHOD_DEFINITION_CONCRETE = CodeTemplate("""\ +${return_type} Type::${api_name}(${type_method_formals}) const { + TensorOptions options(*this); + ${device_guard_declaration} + return at::native::${api_name}(${type_method_actuals}, options); +} +""") # 4. add virtual override to TypeDerived.h TYPE_DERIVED_DECLARATION = CodeTemplate("""\ virtual ${return_type} ${method_prefix_derived}${api_name}(${type_method_formals}) const override; @@ -81,6 +93,7 @@ virtual ${return_type} ${method_prefix_derived}${api_name}(${type_method_formals # 5. add override definition to TypeDerived.cpp TYPE_DERIVED_DEFINITION = CodeTemplate("""\ ${return_type} ${Type}::${method_prefix_derived}${api_name}(${type_method_formals}) const { + ${device_guard_declaration} ${type_definition_body} } """) @@ -89,6 +102,7 @@ ${return_type} ${Type}::${method_prefix_derived}${api_name}(${type_method_formal # the superclass. But it doesn't seem to be harmful. TYPE_DERIVED_DEFINITION_NATIVE = CodeTemplate("""\ ${return_type} ${Type}::${api_name}(${type_method_formals}) const { + ${device_guard_declaration} const auto& self_ty = *this; (void)self_ty; ${return_call} at::native::${native_type_method_dispatch}(/* actuals */ ${actuals}); @@ -103,35 +117,50 @@ TYPE_DEFINITION_BODY_NATIVE = CodeTemplate("""\ ${return_call} at::native::${native_type_method_dispatch}(/* native_actuals */ ${native_actuals}); """) -# 6. add non-virtual declaration to Tensor.h +# add non-virtual declaration to Tensor.h TENSOR_METHOD_DECLARATION = CodeTemplate("""\ ${return_type} ${api_name}(${method_formals_with_defaults})${const_mark}; """) -# 7. add non-virtual declaration to Tensor.cpp +# add non-virtual declaration to Tensor.cpp TENSOR_METHOD_DEFINITION = CodeTemplate("""\ inline ${return_type} Tensor::${api_name}(${method_formals})${const_mark} { return type().${api_name}(${method_actuals}); } """) -# 8. add a method declaration in Functions.h +# add a method declaration in Functions.h FUNCTION_DECLARATION = CodeTemplate("""\ static inline ${return_type} ${api_name}(${formals_with_defaults}); """) -# 9. add a method declaration in Functions.h +# add a method declaration in Functions.h DEPRECATED_FUNCTION_DECLARATION = CodeTemplate("""\ AT_DEPRECATED(static inline ${return_type} ${api_name}(${formals_with_defaults})); """) -# 10. add method definition in Functions.h +# add method definition in Functions.h FUNCTION_DEFINITION = CodeTemplate("""\ static inline ${return_type} ${api_name}(${formals}) { return ${inferred_type}.${api_name}(${type_method_actuals}); } """) -# 11. add a native declaration for a native function +# add a native declaration for a native function NATIVE_DECLARATION = CodeTemplate("""\ ${return_type} ${native_type_method_dispatch}(${formals_with_defaults}); """) +# special method definition for factory functions in Functions.h +FACTORY_DEFINITION = CodeTemplate("""\ +static inline ${return_type} ${api_name}(${formals}) { + const DeviceGuard guard(options.device()); + return at::native::${api_name}(${type_method_actuals}); +} +""") + +# special method definition for *deprecated* factory functions in Functions.h +DEPRECATED_FACTORY_DEFINITION = CodeTemplate("""\ +static inline ${return_type} ${api_name}(${formals}) { + return at::${api_name}(${type_method_actuals}, TensorOptions(${inferred_type})); +} +""") + # We need to cast to the base type because C++ may hide the base class # implementation of ${api_name} if we have overloaded a function with # the same name (but different signature) already @@ -142,7 +171,7 @@ if (${check_name}.dim() == 0) { ZERO_DIM_ONLY = CodeTemplate("""\ AT_ERROR("${api_name} only supports a 0-dimensional ${check_name} tensor, but got tensor " - "with %" PRId64 " dimension(s)", ${check_name}.dim()); + "with ", ${check_name}.dim(), " dimension(s)."); """) SPARSE_CHECK = CodeTemplate("""\ @@ -398,7 +427,6 @@ FunctionOption = TypedDict('FunctionOption', { 'arguments': List[THFormal], 'aten_custom_call': str, 'aten_dense_sparse': bool, - 'auto_gpu': bool, 'backend_type_pairs': List[Tuple[str, str]], 'backends': List[str], 'broadcast_actuals': List[str], @@ -411,6 +439,9 @@ FunctionOption = TypedDict('FunctionOption', { 'cname': str, 'condition': str, 'const_mark': str, + 'device_guard': bool, + 'device_guard_declaration': str, + 'with_gil': bool, 'cpu_half': bool, 'deprecated': bool, 'formals_list': List[AtFormal], @@ -457,11 +488,22 @@ OutputDeclaration = NamedTuple('OutputDeclaration', [ ('returns', List[ReturnType]), ('inplace', bool), ('abstract', bool), - ('auto_gpu', bool), + ('device_guard', bool), ('with_gil', bool), + ('deprecated', bool), ]) +def device_guard(option, formals, is_factory_method=False): + # For factory methods the `DeviceGuard` is already in the template. + if option.get('device_guard', True) and not is_factory_method: + tensor_arguments = [f for f in formals if f['dynamic_type'] in {'Tensor', 'TensorList'}] + if tensor_arguments: + tensor_argument = tensor_arguments[0]['name'] + return 'const DeviceGuard device_guard({});'.format(tensor_argument) + return '// DeviceGuard omitted' + + def is_real_argument_to_wrapper(argument): # type: (THFormal) -> bool return not argument.get('output', False) and\ @@ -724,6 +766,8 @@ def create_generic(top_env, declarations): broadcast_arg = get_broadcast_argument(option) # "s_" for "same size". option['method_prefix_derived'] = '' if broadcast_arg is None else 's_' + option['device_guard_declaration'] = device_guard(option, formals) + env = nested_dict(option, top_env) mode = option['mode'] @@ -796,8 +840,9 @@ def create_generic(top_env, declarations): inplace=option['inplace'], # See Note [Abstract ATen methods] abstract=abstract, - auto_gpu=option.get('auto_gpu', True), + device_guard=option.get('device_guard', True), with_gil=option.get('with_gil', False), + deprecated=option.get('deprecated', False) )) def native_get_formals(option, include_constants=False): @@ -838,6 +883,7 @@ def create_generic(top_env, declarations): 'BoolTensor': 'const Tensor &' if const else 'Tensor &', 'IndexTensor': 'const Tensor &' if const else 'Tensor &', 'Type': 'const Type &' if const else 'Type &', + 'TensorOptions': 'const TensorOptions &' if const else 'TensorOptions &', } if (option['inplace'] and argument['name'] == 'self') or argument.get('output', False): @@ -902,14 +948,14 @@ def create_generic(top_env, declarations): option['method_actuals'] = [ f['name'] if f['name'] != 'self' else '*this' for f in formals] - def find_dispatch_type(formals): + def find_formal(formal_name, formals): for formal in formals: - if 'Type' == formal['dynamic_type']: + if formal_name == formal['dynamic_type']: return formal return None dispatch_tensor = find_dispatch_tensor(formals) - dispatch_type = None if dispatch_tensor else find_dispatch_type(formals) + dispatch_type = None if dispatch_tensor else find_formal('Type', formals) if dispatch_type: dispatch_type['is_type_dispatched'] = True @@ -922,9 +968,17 @@ def create_generic(top_env, declarations): is_method = 'method' in option['variants'] is_function = 'function' in option['variants'] - is_namespace_function = is_function and (dispatch_tensor or dispatch_type) + is_factory_method = find_formal('TensorOptions', formals) + is_deprecated_factory_method = \ + formals[0]['dynamic_type'] == 'Type' and option['return_type'] == 'Tensor' and option['deprecated'] + needs_native_definition = not is_deprecated_factory_method + + has_dispatch = dispatch_tensor or dispatch_type + is_namespace_function = is_function and (has_dispatch or is_factory_method) option['method_prefix_derived'] = '' + option['device_guard_declaration'] = device_guard(option, formals, is_factory_method) + env = nested_dict(option, top_env) broadcast_arg = get_broadcast_argument(option) @@ -932,8 +986,12 @@ def create_generic(top_env, declarations): raise Exception("broadcasting is not yet supported for native functions, " "but specified for function {}", option['name']) - top_env['type_method_declarations'].append( - TYPE_METHOD_DECLARATION_CONCRETE.substitute(env)) + # Factory methods are not dispatched over `Type`. + if not is_factory_method: + if option['deprecated']: + top_env['type_method_declarations'].append(DEPRECATED_TYPE_METHOD_DECLARATION_CONCRETE.substitute(env)) + else: + top_env['type_method_declarations'].append(TYPE_METHOD_DECLARATION_CONCRETE.substitute(env)) dispatch = option['type_method_definition_dispatch'] option['native_type_method_dispatch'] = dispatch @@ -945,30 +1003,34 @@ def create_generic(top_env, declarations): # method is one which has the same dispatch for all types; # we just implement it in the base Type. This is exposed # in Declarations.yaml via a field named 'abstract'. + abstract = False if isinstance(dispatch, dict): abstract = True top_env['type_method_definitions'].append( TYPE_METHOD_DEFINITION_ABSTRACT.substitute(env)) - else: - abstract = False + elif is_deprecated_factory_method: + top_env['type_method_definitions'].append( + DEPRECATED_TYPE_METHOD_DEFINITION_CONCRETE.substitute(env)) + elif not is_factory_method: body = TYPE_DEFINITION_BODY_NATIVE.substitute(env) top_env['type_method_definitions'].append( TYPE_METHOD_DEFINITION_CONCRETE.substitute( env, type_definition_body=body)) # generate the at::native function declarations (i.e. what the user will implement) - if isinstance(dispatch, dict): - generated_native_functions = [] # type: List[str] - for key in sorted(dispatch.keys()): - value = dispatch[key] - if value not in generated_native_functions: - option['native_type_method_dispatch'] = value - top_env['native_function_declarations'].append( - NATIVE_DECLARATION.substitute(env)) - generated_native_functions.append(value) - else: - top_env['native_function_declarations'].append( - NATIVE_DECLARATION.substitute(env)) + if needs_native_definition: + if isinstance(dispatch, dict): + generated_native_functions = [] # type: List[str] + for key in sorted(dispatch.keys()): + value = dispatch[key] + if value not in generated_native_functions: + option['native_type_method_dispatch'] = value + top_env['native_function_declarations'].append( + NATIVE_DECLARATION.substitute(env)) + generated_native_functions.append(value) + else: + top_env['native_function_declarations'].append( + NATIVE_DECLARATION.substitute(env)) method_of = ['Type'] if is_method: @@ -981,11 +1043,16 @@ def create_generic(top_env, declarations): if is_namespace_function: if dispatch_type: option['inferred_type'] = dispatch_type['name'] - else: + elif dispatch_tensor: option['inferred_type'] = 'infer_type({})'.format(dispatch_tensor) - declaration = DEPRECATED_FUNCTION_DECLARATION if option.get('deprecated') else FUNCTION_DECLARATION + declaration = DEPRECATED_FUNCTION_DECLARATION if option['deprecated'] else FUNCTION_DECLARATION top_env['function_declarations'].append(declaration.substitute(env)) - top_env['function_definitions'].append(FUNCTION_DEFINITION.substitute(env)) + if is_factory_method: + top_env['function_definitions'].append(FACTORY_DEFINITION.substitute(env)) + elif is_deprecated_factory_method: + top_env['function_definitions'].append(DEPRECATED_FACTORY_DEFINITION.substitute(env)) + else: + top_env['function_definitions'].append(FUNCTION_DEFINITION.substitute(env)) method_of.append('namespace') output_options.append(OutputDeclaration( @@ -999,8 +1066,9 @@ def create_generic(top_env, declarations): inplace=option['inplace'], # See Note [Abstract ATen methods] abstract=abstract, - auto_gpu=option.get('auto_gpu', True), + device_guard=option.get('device_guard', True), with_gil=option.get('with_gil', False), + deprecated=option['deprecated'], )) output_declarations = [] # type: List[OutputDeclaration] @@ -1083,6 +1151,9 @@ def create_derived(backend_type_env, declarations): def drop_argument(argument, option): # type: (THFormal, FunctionOption) -> bool + # Devices are handled in the body of the function. + if argument['name'] == 'device': + return True return 'CUDA' in backend_type_env['Backend'] and ( option['mode'] == 'TH' and argument['type'] == 'THGenerator*') diff --git a/aten/src/ATen/gen.py b/aten/src/ATen/gen.py index dc0e7e260f71..7bceff1db122 100644 --- a/aten/src/ATen/gen.py +++ b/aten/src/ATen/gen.py @@ -264,6 +264,7 @@ def generate_storage_type_and_tensor(backend, density, scalar_type, declarations '#undef THCIndexTensor_', ] env['extra_cuda_headers'] = ['#include '] + env['extra_cuda_headers'].append('#include ') sname = '' if scalar_name == "Float" else scalar_name env['THType'] = 'Cuda{}'.format(sname) env['THStorage'] = 'THCuda{}Storage'.format(sname) diff --git a/aten/src/ATen/native/Distributions.cpp b/aten/src/ATen/native/Distributions.cpp index 4b7781057be9..c50309dd9cc9 100644 --- a/aten/src/ATen/native/Distributions.cpp +++ b/aten/src/ATen/native/Distributions.cpp @@ -149,7 +149,7 @@ Tensor& bernoulli_(Tensor& self, double p, Generator* gen) { self._cpu_bernoulli_(p, gen); return self; } - Tensor probs = self.type().toScalarType(kDouble).tensor({}).fill_(p); + Tensor probs = at::full({}, p, self.type().toScalarType(kDouble)); return native::bernoulli_(self, probs, gen); } @@ -174,7 +174,7 @@ Tensor _standard_gamma_grad_cpu(const Tensor& self, const Tensor& output) { */ Tensor _s_poisson_cpu(const Tensor& lambda, Generator *gen) { - Tensor ret = at::zeros(lambda.type(), lambda.sizes()); + Tensor ret = at::zeros(lambda.sizes(), lambda.type()); AT_DISPATCH_FLOATING_TYPES(ret.type(), "poisson", [&] { THGenerator* generator = get_generator(gen); std::lock_guard lock(generator->mutex); @@ -188,7 +188,7 @@ Tensor _s_poisson_cpu(const Tensor& lambda, Generator *gen) { } Tensor _s_gamma_cpu(const Tensor& alpha, Generator *gen) { - Tensor ret = alpha.type().zeros(alpha.sizes()); + Tensor ret = at::zeros(alpha.sizes(), alpha.type()); AT_DISPATCH_FLOATING_TYPES(ret.type(), "gamma", [&] { THGenerator* generator = get_generator(gen); std::lock_guard lock(generator->mutex); diff --git a/aten/src/ATen/native/Embedding.cpp b/aten/src/ATen/native/Embedding.cpp index 778189041e57..9be5060cc826 100644 --- a/aten/src/ATen/native/Embedding.cpp +++ b/aten/src/ATen/native/Embedding.cpp @@ -105,7 +105,7 @@ Tensor embedding_backward_cpu( } auto grad = grad_.contiguous().view({numel, grad_.size(-1)}); - auto grad_weight = at::zeros(grad_.type(), {num_weights, grad_.size(-1)}); + auto grad_weight = at::zeros({num_weights, grad_.size(-1)}, grad_.type()); #ifdef _OPENMP if (numel > 1000) { diff --git a/aten/src/ATen/native/EmbeddingBag.cpp b/aten/src/ATen/native/EmbeddingBag.cpp index ea36b0a92a6b..77d8f220cc75 100644 --- a/aten/src/ATen/native/EmbeddingBag.cpp +++ b/aten/src/ATen/native/EmbeddingBag.cpp @@ -107,7 +107,7 @@ template std::tuple embedding_bag_cpu_max( const Tensor& weight, const Tensor &indices, const Tensor& offset2bag, const Tensor& output, const Tensor& bag_size, const Tensor& offsets) { - auto max_indices = at::zeros(indices.type(), {offsets.size(0), weight.size(1)}); + auto max_indices = at::zeros({offsets.size(0), weight.size(1)}, indices.type()); int64_t numel = indices.numel(); int64_t dims = weight.size(1); @@ -156,7 +156,7 @@ embedding_bag_cpu(const Tensor &weight, const Tensor &indices__, auto weight_arg = TensorArg(weight, "weight", 1); checkScalarTypes("embedding_bag", weight_arg, {kFloat, kDouble}); - auto bag_size = at::zeros(indices.type(), offsets.sizes()); + auto bag_size = at::zeros(offsets.sizes(), indices.type()); make_bag_size(offsets, indices, mode, bag_size); // If the last entries are empty, that the last offsets are irrelevant as they @@ -164,13 +164,13 @@ embedding_bag_cpu(const Tensor &weight, const Tensor &indices__, // throw out of bounds error. So to keep it simple we just add one more // entry to the end then get rid of it after make_offset2bag. auto offset2bag = at::zeros( - indices__.type(), {indices.sizes()[0] + 1}); // offset2bag = [0 0 0 0 0] + {indices.sizes()[0] + 1}, indices__.type()); // offset2bag = [0 0 0 0 0] make_offset2bag(offsets, indices, offset2bag); offset2bag.resize_({indices.sizes()[0]}); - auto output = at::zeros(weight.type(), {offsets.size(0), weight.size(1)}); + auto output = at::zeros({offsets.size(0), weight.size(1)}, weight.type()); if (mode == MODE_MEAN || mode == MODE_SUM) { if (weight.type().scalarType() == kFloat) { @@ -258,7 +258,7 @@ Tensor embedding_bag_backward_cpu(const Tensor &grad_, const Tensor &indices__, } auto index_grad_weight = - at::zeros(grad.type(), {num_weights, grad.size(1)}).contiguous(); + at::zeros({num_weights, grad.size(1)}, grad.type()).contiguous(); std::vector counts_uniq; counts_uniq.reserve(num_weights); diff --git a/aten/src/ATen/native/Gesv.cpp b/aten/src/ATen/native/Gesv.cpp index a39a1d0498c9..0e9a59476454 100644 --- a/aten/src/ATen/native/Gesv.cpp +++ b/aten/src/ATen/native/Gesv.cpp @@ -57,7 +57,7 @@ static void applyGesv(Tensor& b, Tensor& A, std::vector infos) { auto n = A.size(-2); auto nrhs = b.size(-1); - auto ipiv = b.type().toScalarType(kInt).tensor(n); + auto ipiv = at::empty({n}, b.type().toScalarType(kInt)); for (int64_t i = 0; i < batch_size; i++) { int info; diff --git a/aten/src/ATen/native/Indexing.cpp b/aten/src/ATen/native/Indexing.cpp index 0446b8c8a392..14927ccdfe94 100644 --- a/aten/src/ATen/native/Indexing.cpp +++ b/aten/src/ATen/native/Indexing.cpp @@ -186,13 +186,13 @@ static Tensor computeLinearIndex(const Tensor & src, TensorList indices) { // Compute the linear indices for the parts of the tensor not being indexed Tensor beforeIndex; if (emptyBefore > 0) { - auto index = at::arange(longType, 0, nElemBefore) * strides[emptyBefore - 1]; + auto index = at::arange(0, nElemBefore, longType) * strides[emptyBefore - 1]; index = index.view(src.sizes().slice(0, emptyBefore)); beforeIndex = unsqueezeN(index, 0, linearIndex.dim() + emptyAfter); } Tensor afterIndex; if (emptyAfter > 0) { - auto index = at::arange(longType, 0, nElemAfter); + auto index = at::arange(0, nElemAfter, longType); index = index.view(src.sizes().slice(src.dim() - emptyAfter, emptyAfter)); afterIndex = unsqueezeN(index, linearIndex.dim() + emptyBefore, 0); } diff --git a/aten/src/ATen/native/LinearAlgebra.cpp b/aten/src/ATen/native/LinearAlgebra.cpp index 4778a3669562..ff7b20bde3c1 100644 --- a/aten/src/ATen/native/LinearAlgebra.cpp +++ b/aten/src/ATen/native/LinearAlgebra.cpp @@ -25,7 +25,7 @@ static inline std::tuple _lu_det_P_diag_U_info(const Tensor throw std::runtime_error(ss.str()); } auto n = self.size(0); - auto num_exchanges = (p.type().arange(1, n + 1) != p).nonzero().size(0); + auto num_exchanges = (at::arange(1, n + 1, p.type()) != p).nonzero().size(0); if (num_exchanges % 2 == 1) { return std::make_tuple(-1., lu.diag(), int_info); } else { @@ -46,7 +46,7 @@ Tensor det(const Tensor& self) { int info; std::tie(det_P, diag_U, info) = _lu_det_P_diag_U_info(self); if (info > 0) { - return at::zeros(self.type(), {}); + return at::zeros({}, self.type()); } else { return diag_U.prod().mul_(det_P); } @@ -65,7 +65,7 @@ Tensor logdet(const Tensor& self) { int info; std::tie(det_P, diag_U, info) = _lu_det_P_diag_U_info(self); if (info > 0) { - det = at::zeros(self.type(), {}); + det = at::zeros({}, self.type()); } else { det = diag_U.prod().mul_(det_P); } @@ -89,7 +89,7 @@ std::tuple slogdet(const Tensor& self) { int info; std::tie(det_P, diag_U, info) = _lu_det_P_diag_U_info(self); if (info > 0) { - det = at::zeros(self.type(), {}); + det = at::zeros({}, self.type()); } else { det = diag_U.prod().mul_(det_P); } @@ -116,14 +116,14 @@ Tensor& ger_out(Tensor& result, const Tensor& self, const Tensor& vec2) { Tensor mm(const Tensor& self, const Tensor& mat2) { if (self.is_sparse()) { - return mat2.type().addmm(at::zeros(mat2.type(), {}), self, mat2, 0, 1); + return mat2.type().addmm(at::zeros({}, mat2.type()), self, mat2, 0, 1); } return self.type()._mm(self, mat2); } Tensor& mm_out(Tensor& result, const Tensor& self, const Tensor& mat2) { if (self.is_sparse()) { - return mat2.type().addmm_out(result, at::zeros(mat2.type(), {}), self, mat2, 0, 1); + return mat2.type().addmm_out(result, at::zeros({}, mat2.type()), self, mat2, 0, 1); } return self.type()._mm_out(result, self, mat2); } diff --git a/aten/src/ATen/native/ReduceOps.cpp b/aten/src/ATen/native/ReduceOps.cpp index d675122f623f..be1129d86980 100644 --- a/aten/src/ATen/native/ReduceOps.cpp +++ b/aten/src/ATen/native/ReduceOps.cpp @@ -94,7 +94,7 @@ Tensor sum(const Tensor &self) { Tensor _sum_cpu(const Tensor& self) { if (self.is_contiguous()) { - Tensor result = self.type().tensor({}); + Tensor result = at::empty({}, self.type()); sum_kernel(result, self, at::nullopt); return result; } @@ -115,7 +115,7 @@ Tensor prod(const Tensor &self) { Tensor _prod_cpu(const Tensor &self) { if (self.is_contiguous()) { - Tensor result = self.type().tensor({}); + Tensor result = at::empty({}, self.type()); prod_kernel(result, self, at::nullopt); return result; } diff --git a/aten/src/ATen/native/SpectralOps.cpp b/aten/src/ATen/native/SpectralOps.cpp index fb8dc5f5bb86..abcaae111869 100644 --- a/aten/src/ATen/native/SpectralOps.cpp +++ b/aten/src/ATen/native/SpectralOps.cpp @@ -203,7 +203,7 @@ Tensor stft(const Tensor& self, const int64_t frame_length, } // pad zeros if (pad_end != 0) { - Tensor padded_input = at::zeros(self.type(), {batch, len + pad_end}); + Tensor padded_input = at::zeros({batch, len + pad_end}, self.type()); padded_input.narrow(1, 0, len).copy_(input); len += pad_end; input = padded_input; @@ -236,8 +236,8 @@ Tensor stft(const Tensor& self, const int64_t frame_length, // build ft kernel // k[omega, t] = cos (2 pi omega t / N) - j sin (2 pi omega t / N) double N = static_cast(fft_size); - auto freq_arange = at::arange(self.type(), 0, return_size).mul_(M_PI * 2. / N); - auto time_arange = at::arange(self.type(), 0, frame_length); + auto freq_arange = at::arange(0, return_size, self.type()).mul_(M_PI * 2. / N); + auto time_arange = at::arange(0, frame_length, self.type()); auto arange_2d = at::ger(freq_arange, time_arange); auto re_kernel = arange_2d.cos(); auto im_kernel = arange_2d.sin().neg_(); diff --git a/aten/src/ATen/native/SummaryOps.cpp b/aten/src/ATen/native/SummaryOps.cpp index 5a2bc1467e90..6117b817d16b 100644 --- a/aten/src/ATen/native/SummaryOps.cpp +++ b/aten/src/ATen/native/SummaryOps.cpp @@ -33,14 +33,14 @@ Tensor _bincount_cpu_template( const input_t* self_p = self.contiguous().data(); if (has_weights) { - output = zeros(weights.type(), {nbins}); + output = native::zeros({nbins}, TensorOptions(weights)); weights_t* output_p = output.data(); const weights_t* weights_p = weights.contiguous().data(); for (int64_t i = 0; i < self.size(0); i++) { output_p[self_p[i]] += weights_p[i]; } } else { - output = zeros(CPU(kLong), {nbins}); + output = native::zeros({nbins}, kLong); int64_t* output_p = output.data(); for (int64_t i = 0; i < self.size(0); i++) { output_p[self_p[i]] += 1L; diff --git a/aten/src/ATen/native/TensorFactories.cpp b/aten/src/ATen/native/TensorFactories.cpp index 6346d04d7178..a86c583d8383 100644 --- a/aten/src/ATen/native/TensorFactories.cpp +++ b/aten/src/ATen/native/TensorFactories.cpp @@ -11,25 +11,62 @@ #include "ATen/Error.h" #include "ATen/NativeFunctions.h" #include "ATen/ScalarType.h" +#include "ATen/Deprecated.h" #include "TH/THRandom.h" #include -#include #include +#include namespace at { namespace native { +namespace { +void window_function_checks( + const char* function_name, + const TensorOptions& options, + int64_t window_length) { + AT_CHECK( + options.layout() != kSparse, + function_name, + " is not implemented for sparse types, got: ", + options.type().toString()); + AT_CHECK( + at::isFloatingType(options.dtype()), + function_name, + " expects floating point dtypes, got: ", + options.type().toString()); + AT_CHECK( + window_length > 0, + function_name, + " requires positive window_length, got window_length=%lld", + window_length); +} +} // namespace -Tensor arange(const Type& dtype, Scalar start, Scalar end, Scalar step) { - return dtype._arange(start, end, step); +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ arange ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Tensor arange(Scalar start, Scalar end, const TensorOptions& options) { + return native::arange(start, end, /*step=*/1, options); +} + +Tensor arange( + Scalar start, + Scalar end, + Scalar step, + const TensorOptions& options) { + return options.type()._arange(start, end, step); +} + +Tensor& arange_out(Tensor& result, Scalar start, Scalar end) { + return native::arange_out(result, start, end, /*step=*/1); } Tensor& arange_out(Tensor& result, Scalar start, Scalar end, Scalar step) { return at::_arange_out(result, start, end, step); } -Tensor arange(const Type& dtype, Scalar end) { - return dtype._arange(end); +Tensor arange(Scalar end, const TensorOptions& options) { + return options.type()._arange(end); } Tensor& arange_out(Tensor& result, Scalar end) { @@ -40,8 +77,10 @@ Tensor _dim_arange(const Tensor& like, int64_t dim) { return like.type().toScalarType(at::kLong)._arange(like.size(dim)); } -Tensor empty(const Type& dtype, IntList size) { - return dtype.tensor(size); +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ empty ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Tensor empty(IntList size, const TensorOptions& options) { + return options.type().tensor(size); } Tensor& empty_out(Tensor& result, IntList size) { @@ -71,30 +110,37 @@ AT_FORALL_SCALAR_TYPES(DEFINE_CAST_OP) #undef DEFINE_CAST_OP Tensor empty_like(const Tensor& self) { - return at::native::empty_like(self, self.type()); + return native::empty_like(self, TensorOptions(self)); } -Tensor empty_like(const Tensor& self, const Type& dtype) { - if (dtype.is_sparse() && self.type().is_sparse()) { - auto res = dtype.tensor(); +Tensor empty_like(const Tensor& self, const TensorOptions& options) { + if (options.layout() == kSparse && self.type().is_sparse()) { + auto res = options.type().tensor({}); // resize_as_ requires the same exact type. res.sparse_raw_resize_(self.sizes(), self._sparseDims(), self._denseDims()); + return res; } - return at::native::empty(dtype, self.sizes()); + return native::empty(self.sizes(), options); } -Tensor eye(const Type& dtype, int64_t n, int64_t m) { - auto result = dtype.tensor(); - return at::eye_out(result, n, m); +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ eye ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Tensor eye(int64_t n, const TensorOptions& options) { + return native::eye(n, -1, options); +} + +Tensor eye(int64_t n, int64_t m, const TensorOptions& options) { + auto tensor = options.type().tensor({}); + return at::eye_out(tensor, n, m); +} + +Tensor& eye_out_cpu(Tensor& result, int64_t n) { + return native::eye_out_cpu(result, n, -1); } Tensor& eye_out_cpu(Tensor& result, int64_t n, int64_t m) { - if (n <= 0) { - std::ostringstream oss; - oss << "n must be greater than 0, got: " << n; - throw std::runtime_error(oss.str()); - } + AT_CHECK(n > 0, "n must be greater than 0, got", n); if(m <= 0) { m = n; } @@ -113,136 +159,234 @@ Tensor& eye_out_cpu(Tensor& result, int64_t n, int64_t m) { return result; } -Tensor full(const Type& dtype, IntList size, Scalar fill_value) { - if (dtype.is_sparse()) { - AT_ERROR("full(...) is not implemented for sparse types, got: ", dtype.toString()); +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ full ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Tensor full(IntList size, Scalar fill_value, const TensorOptions& options) { + if (options.layout() == kSparse) { + AT_ERROR("full(...) is not implemented for sparse layout"); } - auto result = dtype.tensor(size); + auto result = options.type().tensor(size); return result.fill_(fill_value); } Tensor& full_out(Tensor& result, IntList size, Scalar fill_value) { if (result.is_sparse()) { - AT_ERROR("full(...) is not implemented for sparse types, got: ", result.type().toString()); + AT_ERROR("full(...) is not implemented for sparse layout"); } result.resize_(size); return result.fill_(fill_value); } Tensor full_like(const Tensor& self, Scalar fill_value) { - return at::native::full_like(self, fill_value, self.type()); + return native::full_like(self, fill_value, TensorOptions(self)); } -Tensor full_like(const Tensor& self, Scalar fill_value, const Type& dtype) { - return at::native::full(dtype, self.sizes(), fill_value); +Tensor full_like(const Tensor& self, Scalar fill_value, const TensorOptions& options) { + return native::full(self.sizes(), fill_value, options); } -Tensor linspace(const Type& dtype, Scalar start, Scalar end, int64_t steps) { - return dtype._linspace(start, end, steps); +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ linspace ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Tensor linspace(Scalar start, Scalar end, const TensorOptions& options) { + return native::linspace(start, end, /*steps=*/100, options); +} + +Tensor linspace( + Scalar start, + Scalar end, + int64_t steps, + const TensorOptions& options) { + return options.type()._linspace(start, end, steps); +} + +Tensor& linspace_out(Tensor& result, Scalar start, Scalar end) { + return native::linspace_out(result, start, end, /*steps=*/100); } Tensor& linspace_out(Tensor& result, Scalar start, Scalar end, int64_t steps) { return at::_linspace_out(result, start, end, steps); } -Tensor logspace(const Type& dtype, Scalar start, Scalar end, int64_t steps) { - return dtype._logspace(start, end, steps); +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ logspace ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Tensor logspace(Scalar start, Scalar end, const TensorOptions& options) { + return native::logspace(start, end, /*steps=*/100, options); +} + +Tensor logspace( + Scalar start, + Scalar end, + int64_t steps, + const TensorOptions& options) { + return options.type()._logspace(start, end, steps); +} + +Tensor& logspace_out(Tensor& result, Scalar start, Scalar end) { + return native::logspace_out(result, start, end, /*steps=*/100); } Tensor& logspace_out(Tensor& result, Scalar start, Scalar end, int64_t steps) { return at::_logspace_out(result, start, end, steps); } -Tensor ones(const Type& dtype, IntList size) { - auto result = dtype.tensor(size); - return result.fill_(1); +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ones ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Tensor ones(IntList size, const TensorOptions& options) { + return native::full(size, /*fill_value=*/1, options); } Tensor& ones_out(Tensor& result, IntList size) { - result.resize_(size); - return result.fill_(1); + return native::full_out(result, size, /*fill_value=*/1); } Tensor ones_like(const Tensor& self) { - return at::native::ones(self.type(), self.sizes()); + return native::ones(self.sizes(), TensorOptions(self)); } -Tensor ones_like(const Tensor& self, const Type& dtype) { - return at::native::ones(dtype, self.sizes()); +Tensor ones_like(const Tensor& self, const TensorOptions& options) { + return native::ones(self.sizes(), options); } -Tensor rand(const Type& dtype, IntList size, Generator* generator) { - Tensor result = dtype.tensor(size); +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ rand ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Tensor rand(IntList size, const TensorOptions& options) { + return native::rand(size, nullptr, options); +} + +Tensor rand(IntList size, Generator* generator, const TensorOptions& options) { + auto result = options.type().tensor(size); return result.uniform_(0, 1, generator); } +Tensor& rand_out(Tensor& result, IntList size) { + return native::rand_out(result, size, nullptr); +} + Tensor& rand_out(Tensor& result, IntList size, Generator* generator) { result.resize_(size); return result.uniform_(0, 1, generator); } Tensor rand_like(const Tensor& self) { - return at::native::rand_like(self, self.type()); + return native::rand_like(self, TensorOptions(self)); } -Tensor rand_like(const Tensor& self, const Type& dtype) { - return at::native::rand(dtype, self.sizes()); +Tensor rand_like(const Tensor& self, const TensorOptions& options) { + return native::rand(self.sizes(), options); } -Tensor randint(const Type& dtype, int64_t high, IntList size, Generator* generator) { - Tensor result = dtype.tensor(size); - return result.random_(0, high, generator); +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ randint ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Tensor randint(int64_t high, IntList size, const TensorOptions& options) { + return native::randint(high, size, nullptr, options); } -Tensor randint(const Type& dtype, int64_t low, int64_t high, IntList size, Generator* generator) { - Tensor result = dtype.tensor(size); +Tensor randint( + int64_t high, + IntList size, + Generator* generator, + const TensorOptions& options) { + return native::randint(0, high, size, generator, options); +} + +Tensor randint( + int64_t low, + int64_t high, + IntList size, + const TensorOptions& options) { + return native::randint(low, high, size, nullptr, options); +} + +Tensor randint( + int64_t low, + int64_t high, + IntList size, + Generator* generator, + const TensorOptions& options) { + auto result = options.type().tensor(size); return result.random_(low, high, generator); } -Tensor& randint_out(Tensor& result, int64_t high, IntList size, Generator* generator) { +Tensor& randint_out(Tensor& result, int64_t high, IntList size) { + return native::randint_out(result, high, size, nullptr); +} + +Tensor& randint_out( + Tensor& result, + int64_t high, + IntList size, + Generator* generator) { result.resize_(size); return result.random_(0, high, generator); } -Tensor& randint_out(Tensor& result, int64_t low, int64_t high, IntList size, Generator* generator) { +Tensor& randint_out(Tensor& result, int64_t low, int64_t high, IntList size) { + return native::randint_out(result, low, high, size, nullptr); +} + +Tensor& randint_out( + Tensor& result, + int64_t low, + int64_t high, + IntList size, + Generator* generator) { result.resize_(size); return result.random_(low, high, generator); } Tensor randint_like(const Tensor& self, int64_t high) { - return at::native::randint_like(self, high, self.type()); + return native::randint_like(self, high, TensorOptions(self)); } Tensor randint_like(const Tensor& self, int64_t low, int64_t high) { - return at::native::randint_like(self, low, high, self.type()); + return native::randint_like(self, low, high, TensorOptions(self)); } -Tensor randint_like(const Tensor& self, int64_t high, const Type& dtype) { - return at::native::randint(dtype, high, self.sizes(), nullptr); +Tensor randint_like( + const Tensor& self, + int64_t high, + const TensorOptions& options) { + return native::randint(high, self.sizes(), nullptr, options); } -Tensor randint_like(const Tensor& self, int64_t low, int64_t high, const Type& dtype) { - return at::native::randint(dtype, low, high, self.sizes(), nullptr); +Tensor randint_like( + const Tensor& self, + int64_t low, + int64_t high, + const TensorOptions& options) { + return native::randint(low, high, self.sizes(), nullptr, options); } -Tensor randn(const Type& dtype, IntList size, Generator* generator) { - Tensor result = dtype.tensor(size); +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ randn ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Tensor randn(IntList size, const TensorOptions& options) { + return native::randn(size, nullptr, options); +} + +Tensor randn(IntList size, Generator* generator, const TensorOptions& options) { + auto result = options.type().tensor(size); return result.normal_(0, 1, generator); } +Tensor& randn_out(Tensor& result, IntList size) { + return native::randn_out(result, size, nullptr); +} + Tensor& randn_out(Tensor& result, IntList size, Generator* generator) { result.resize_(size); return result.normal_(0, 1, generator); } Tensor randn_like(const Tensor& self) { - return at::native::randn_like(self, self.type()); + return native::randn_like(self, TensorOptions(self)); } -Tensor randn_like(const Tensor& self, const Type& dtype) { - return at::native::randn(dtype, self.sizes(), nullptr); +Tensor randn_like(const Tensor& self, const TensorOptions& options) { + return native::randn(self.sizes(), nullptr, options); } +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ randperm ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + namespace { template void randperm_cpu(Tensor& result, int64_t n, THGenerator* generator) { @@ -272,18 +416,21 @@ THGenerator* get_generator(at::Generator* gen) { return gen_->generator; } -Tensor randperm(const Type& dtype, int64_t n, Generator* generator) { - Tensor result = dtype.tensor(n); - return at::randperm_out(result, n, generator); +Tensor randperm(int64_t n, const TensorOptions& options) { + return native::randperm(n, nullptr, options); +} + +Tensor randperm(int64_t n, Generator* generator, const TensorOptions& options) { + auto tensor = options.type().tensor(n); + return at::randperm_out(tensor, n, generator); +} + +Tensor& randperm_out(Tensor& result, int64_t n) { + return at::randperm_out(result, n, nullptr); } Tensor& randperm_out_cpu(Tensor& result, int64_t n, Generator* generator) { - if (n < 0) { - std::ostringstream oss; - oss << "n must be non-negative, got " << n; - throw std::runtime_error(oss.str()); - } - + AT_CHECK(n >= 0, "n must be non-negative, got", n); result.resize_({n}); auto gen = get_generator(generator); AT_DISPATCH_ALL_TYPES(result.type(), "randperm", [&]() -> void { @@ -293,17 +440,33 @@ Tensor& randperm_out_cpu(Tensor& result, int64_t n, Generator* generator) { return result; } -Tensor range(const Type& dtype, Scalar start, Scalar end, Scalar step) { - return dtype._range(start, end, step); +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ range ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Tensor range(Scalar start, Scalar end, const TensorOptions& options) { + return native::range(start, end, /*step=*/1, options); +} + +Tensor range( + Scalar start, + Scalar end, + Scalar step, + const TensorOptions& options) { + return options.type()._range(start, end, step); +} + +Tensor& range_out(Tensor& result, Scalar start, Scalar end) { + return native::range_out(result, start, end, 1); } Tensor& range_out(Tensor& result, Scalar start, Scalar end, Scalar step) { return at::_range_out(result, start, end, step); } -Tensor zeros(const Type& dtype, IntList size) { - auto result = dtype.tensor(size); - return at::native::zeros_out(result, size); +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ zeros ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Tensor zeros(IntList size, const TensorOptions& options) { + auto result = options.type().tensor(size); + return result.zero_(); } Tensor& zeros_out(Tensor& result, IntList size) { @@ -316,98 +479,119 @@ Tensor& zeros_out(Tensor& result, IntList size) { } Tensor zeros_like(const Tensor& self) { - return at::native::zeros_like(self, self.type()); + return native::zeros_like(self, TensorOptions(self)); } -Tensor zeros_like(const Tensor& self, const Type& dtype) { - if (dtype.is_sparse() && self.type().is_sparse()) { - auto res = dtype.tensor(); +Tensor zeros_like(const Tensor& self, const TensorOptions& options) { + if (options.layout() == kSparse && self.type().is_sparse()) { + auto res = options.type().tensor({}); // resize_as_ requires the same exact type. res.sparse_raw_resize_(self.sizes(), self._sparseDims(), self._denseDims()); return res; } - return at::native::zeros(dtype, self.sizes()); + return native::zeros(self.sizes(), options); } -// Signal Processing Window Functions +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~ bartlett_window ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -Tensor bartlett_window(const Type& dtype, int64_t window_length, bool periodic) { - if (dtype.is_sparse()) { - AT_ERROR("bartlett_window(...) is not implemented for sparse types, got: ", dtype.toString()); - } - if (!at::isFloatingType(dtype.scalarType())) { - AT_ERROR("bartlett_window(...) expects floating point dtypes, got: ", dtype.toString()); - } - if (window_length <= 0) { - AT_ERROR("bartlett_window(...) requires positive window_length, got window_length=%lld", window_length); - } +Tensor bartlett_window(int64_t window_length, const TensorOptions& options) { + return native::bartlett_window(window_length, /*periodic=*/true, options); +} + +Tensor bartlett_window( + int64_t window_length, + bool periodic, + const TensorOptions& options) { + window_function_checks("bartlett_window", options, window_length); if (window_length == 1) { - return at::ones(dtype, {1}); + return native::ones({1}, options); } if (periodic) { window_length += 1; } - auto window = at::arange(dtype, window_length).mul_(2. / static_cast(window_length - 1)); - int64_t first_half_size = ((window_length - 1) >> 1) + 1; + auto window = native::arange(window_length, options).mul_(2. / static_cast(window_length - 1)); + const int64_t first_half_size = ((window_length - 1) >> 1) + 1; window.narrow(0, first_half_size, window_length - first_half_size).mul_(-1).add_(2); return periodic ? window.narrow(0, 0, window_length - 1) : window; } -Tensor blackman_window(const Type& dtype, int64_t window_length, bool periodic) { - if (dtype.is_sparse()) { - AT_ERROR("blackman_window(...) is not implemented for sparse types, got: ", dtype.toString()); - } - if (!at::isFloatingType(dtype.scalarType())) { - AT_ERROR("blackman_window(...) expects floating point dtypes, got: ", dtype.toString()); - } - if (window_length <= 0) { - AT_ERROR("blackman_window(...) requires positive window_length, got window_length=%lld", window_length); - } +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~ blackman_window ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Tensor blackman_window(int64_t window_length, const TensorOptions& options) { + return native::blackman_window(window_length, /*periodic=*/true, options); +} + +Tensor blackman_window( + int64_t window_length, + bool periodic, + const TensorOptions& options) { + window_function_checks("blackman_window", options, window_length); if (window_length == 1) { - return at::ones(dtype, {1}); + return native::ones({1}, options); } if (periodic) { window_length += 1; } // from https://en.wikipedia.org/wiki/Window_function#Blackman_window - auto window = at::arange(dtype, window_length).mul_(M_PI / static_cast(window_length - 1)); + auto window = native::arange(window_length, options).mul_(M_PI / static_cast(window_length - 1)); window = window.mul(4).cos_().mul_(0.08) - window.mul(2).cos_().mul_(0.5) + 0.42; return periodic ? window.narrow(0, 0, window_length - 1) : window; } -Tensor hamming_window(const Type& dtype, int64_t window_length, bool periodic, double alpha, double beta) { - if (dtype.is_sparse()) { - AT_ERROR("hamming_window(...) is not implemented for sparse types, got: ", dtype.toString()); - } - if (!at::isFloatingType(dtype.scalarType())) { - AT_ERROR("hamming_window(...) expects floating point dtypes, got: ", dtype.toString()); - } - if (window_length <= 0) { - AT_ERROR("hamming_window(...) requires positive window_length, got window_length=%lld", window_length); - } +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ hamming_window ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Tensor hamming_window(int64_t window_length, const TensorOptions& options) { + return native::hamming_window(window_length, /*periodic=*/true, options); +} + +Tensor hamming_window( + int64_t window_length, + bool periodic, + const TensorOptions& options) { + return native::hamming_window( + window_length, periodic, /*alpha=*/0.54, options); +} + +Tensor hamming_window( + int64_t window_length, + bool periodic, + double alpha, + const TensorOptions& options) { + return native::hamming_window( + window_length, periodic, alpha, /*beta=*/0.46, options); +} + +Tensor hamming_window( + int64_t window_length, + bool periodic, + double alpha, + double beta, + const TensorOptions& options) { + window_function_checks("hamming_window", options, window_length); if (window_length == 1) { - return at::ones(dtype, {1}); + return native::ones({1}, options); } if (periodic) { window_length += 1; } - auto window = at::arange(dtype, window_length); + auto window = native::arange(window_length, options); window.mul_(M_PI * 2. / static_cast(window_length - 1)).cos_().mul_(-beta).add_(alpha); return periodic ? window.narrow(0, 0, window_length - 1) : window; } -Tensor hann_window(const Type& dtype, int64_t window_length, bool periodic) { - if (dtype.is_sparse()) { - AT_ERROR("hann_window(...) is not implemented for sparse types, got: ", dtype.toString()); - } - if (!at::isFloatingType(dtype.scalarType())) { - AT_ERROR("hann_window(...) expects floating point dtypes, got: ", dtype.toString()); - } - if (window_length <= 0) { - AT_ERROR("hann_window(...) requires positive window_length, got window_length=%lld", window_length); - } - return at::native::hamming_window(dtype, window_length, periodic, 0.5, 0.5); +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ hann_window ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Tensor hann_window(int64_t window_length, const TensorOptions& options) { + return native::hann_window(window_length, /*periodic=*/true, options); } +Tensor hann_window( + int64_t window_length, + bool periodic, + const TensorOptions& options) { + window_function_checks("hann_window", options, window_length); + return native::hamming_window( + window_length, periodic, /*alpha=*/0.5, /*beta=*/0.5, options); } -} +} // namespace native +} // namespace at diff --git a/aten/src/ATen/native/TensorTransformations.cpp b/aten/src/ATen/native/TensorTransformations.cpp index 88e3980459ca..797ca89f1483 100644 --- a/aten/src/ATen/native/TensorTransformations.cpp +++ b/aten/src/ATen/native/TensorTransformations.cpp @@ -1,6 +1,10 @@ -#include "TensorTransformations.h" +#include "ATen/native/TensorTransformations.h" -#include "ATen/NativeFunctions.h" +#include +#include + +#include +#include namespace at { namespace native { @@ -15,7 +19,7 @@ Tensor flip_cpu(const Tensor& self, IntList dims) { auto indices = std::vector(flip_dims_size); for (int64_t i = 0; i < flip_dims_size; i++) { - indices[i] = at::arange(self.type().toScalarType(at::ScalarType::Long), self.size(i) - 1, -1, -1); + indices[i] = at::arange(self.size(i) - 1, -1, -1, self.type().toScalarType(at::kLong)); // creates a meshgrid auto temp = std::vector(flip_dims_size, 1); temp[i] = indices[i].size(0); diff --git a/aten/src/ATen/native/TensorTransformations.h b/aten/src/ATen/native/TensorTransformations.h index 6203abc60922..554a46f281e7 100644 --- a/aten/src/ATen/native/TensorTransformations.h +++ b/aten/src/ATen/native/TensorTransformations.h @@ -1,5 +1,10 @@ #include "ATen/ATen.h" +#include + +#include +#include + namespace at { namespace native { diff --git a/aten/src/ATen/native/Unique.cpp b/aten/src/ATen/native/Unique.cpp index 99a83ae5fb2a..d9bd94e1f781 100644 --- a/aten/src/ATen/native/Unique.cpp +++ b/aten/src/ATen/native/Unique.cpp @@ -21,7 +21,7 @@ std::tuple _unique_cpu_template( const Tensor& input = self.contiguous(); const scalar_t* input_data = input.data(); std::unordered_set set(input_data, input_data + input.numel()); - Tensor output = input.type().tensor({static_cast(set.size())}); + Tensor output = at::empty({static_cast(set.size())}, input.type()); scalar_t* output_data = output.data(); if (sorted) { @@ -32,7 +32,7 @@ std::tuple _unique_cpu_template( std::copy(set.begin(), set.end(), output_data); } - Tensor inverse_indices = self.type().toScalarType(kLong).tensor({0}); + Tensor inverse_indices = at::empty({0}, self.type().toScalarType(kLong)); if (return_inverse) { inverse_indices.resize_(input.sizes()); int64_t* inverse_indices_data = inverse_indices.data(); diff --git a/aten/src/ATen/native/cuda/Embedding.cu b/aten/src/ATen/native/cuda/Embedding.cu index 9fbc303645b5..08e7a71fc25a 100644 --- a/aten/src/ATen/native/cuda/Embedding.cu +++ b/aten/src/ATen/native/cuda/Embedding.cu @@ -1,6 +1,5 @@ #include "ATen/ATen.h" #include "ATen/TensorUtils.h" -#include "ATen/NativeFunctions.h" #include "ATen/Error.h" #include "ATen/AccumulateType.h" @@ -220,7 +219,7 @@ Tensor embedding_backward_cuda(const Tensor & grad_, const Tensor & indices, auto num_indices = indices.numel(); auto grad = grad_.contiguous().view({num_indices, grad_.size(-1)}); - auto grad_weight = at::zeros(grad_.type(), {num_weights, grad_.size(-1)}); + auto grad_weight = at::zeros({num_weights, grad_.size(-1)}, grad_.type()); int64_t stride = grad_weight.stride(0); cudaStream_t stream = globalContext().getCurrentCUDAStream(); diff --git a/aten/src/ATen/native/cuda/EmbeddingBag.cu b/aten/src/ATen/native/cuda/EmbeddingBag.cu index 29bebfae6e31..e14b2ad2dd04 100644 --- a/aten/src/ATen/native/cuda/EmbeddingBag.cu +++ b/aten/src/ATen/native/cuda/EmbeddingBag.cu @@ -62,7 +62,7 @@ __global__ void EmbeddingBag_updateOutputKernel( if (mode == MODE_MAX) { if (emb == begin || weightValue > weightFeatMax) { weightFeatMax = weightValue; - maxWord = input[emb]; + maxWord = input[emb]; } } else { weightFeatSum += scalar_cast(weightValue); @@ -166,7 +166,7 @@ __global__ void EmbeddingBag_accGradParametersKernel_sum_avg( Tensor embedding_bag_backward_cuda_sum_avg( - const Tensor &grad, + const Tensor &grad, const Tensor &indices, const Tensor &offset2bag, const Tensor &bag_size_, @@ -175,7 +175,7 @@ Tensor embedding_bag_backward_cuda_sum_avg( Tensor &bag_size = const_cast(bag_size_); - auto grad_weight = at::zeros(grad.type(), {num_weights, grad.size(1)}); + auto grad_weight = at::zeros({num_weights, grad.size(1)}, grad.type()); cudaStream_t stream = globalContext().getCurrentCUDAStream(); @@ -269,7 +269,7 @@ __global__ void EmbeddingBag_accGradParametersKernel_max( int64_t bag = chunk / chunksPerBag; int64_t word_idx = max_indices[bag * stride + featureDim]; - + atomicAdd(&(gradWeight[word_idx * stride + featureDim]), gradOutput[bag * stride + featureDim]); } } @@ -279,7 +279,7 @@ Tensor embedding_bag_backward_cuda_max(const Tensor &grad, const Tensor &max_indices, int64_t num_weights) { - auto grad_weight = at::zeros(grad.type(), {num_weights, grad.size(1)}); + auto grad_weight = at::zeros({num_weights, grad.size(1)}, grad.type()); int64_t stride = grad_weight.stride(0); @@ -295,7 +295,7 @@ Tensor embedding_bag_backward_cuda_max(const Tensor &grad, using cuda_scalar_t = cuda::into_type; EmbeddingBag_accGradParametersKernel_max< cuda_scalar_t><<>>( - max_indices.data(), grad.data(), + max_indices.data(), grad.data(), grad_weight.data(), stride, numBags); }); @@ -323,21 +323,21 @@ embedding_bag_cuda(const Tensor &weight, const Tensor &indices, int64_t numBags = offsets.size(0); int64_t stride = weight.size(1); - auto bag_size = at::zeros(indices.type(), offsets.sizes()); + auto bag_size = at::zeros(offsets.sizes(), indices.type()); auto offset2bag = - at::zeros(indices.type(), {indices.size(0)}); // offset2bag = [0 0 0 0 0] + at::zeros({indices.size(0)}, indices.type()); // offset2bag = [0 0 0 0 0] cudaStream_t stream = globalContext().getCurrentCUDAStream(); - auto output = at::zeros(weight.type(), {offsets.size(0), weight.size(1)}); - + auto output = at::zeros({offsets.size(0), weight.size(1)}, weight.type()); + Tensor max_indices; - + if (mode == MODE_MAX) { - max_indices = at::zeros(indices.type(), {offsets.size(0), weight.size(1)}); + max_indices = at::zeros({offsets.size(0), weight.size(1)}, indices.type()); } else { // No need to allocate if we aren't doing a backwards pass - max_indices = at::zeros(indices.type(), {0}); + max_indices = at::zeros({0}, indices.type()); } dim3 block = dim3(32, 8); @@ -383,7 +383,7 @@ Tensor embedding_bag_backward_cuda(const Tensor &grad_, const Tensor &indices, return embedding_bag_backward_cuda_max(grad, max_indices, num_weights); default: - AT_ERROR( + AT_ERROR( "Unknown mode for embedding_bag_backward_cuda %d", mode); } } diff --git a/aten/src/ATen/native/cuda/RoiPooling.cu b/aten/src/ATen/native/cuda/RoiPooling.cu index cb683b2360ae..63f1f26a979f 100644 --- a/aten/src/ATen/native/cuda/RoiPooling.cu +++ b/aten/src/ATen/native/cuda/RoiPooling.cu @@ -1,5 +1,9 @@ +#include "ATen/ATen.h" #include "ATen/NativeFunctions.h" +#include "ATen/Error.h" + #include +#include namespace at { namespace native { diff --git a/aten/src/ATen/native/cuda/SummaryOps.cu b/aten/src/ATen/native/cuda/SummaryOps.cu index fdfbee65091a..6626c30e8bbd 100644 --- a/aten/src/ATen/native/cuda/SummaryOps.cu +++ b/aten/src/ATen/native/cuda/SummaryOps.cu @@ -208,7 +208,7 @@ bool CUDA_tensor_histogram( detail::TensorInfo pInfo(nullptr, 0, {}, {}); Tensor partial_output; if (memType == CUDAHistogramMemoryType::MULTI_BLOCK) { - partial_output = a.type().zeros({grid.x, nbins}); + partial_output = native::zeros({grid.x, nbins}, TensorOptions(a)); pInfo = detail::getTensorInfo(partial_output); } @@ -261,11 +261,11 @@ Tensor _bincount_cuda_template( // alloc output counter on GPU Tensor output; if (has_weights) { - output = zeros(weights.type(), {nbins}); + output = native::zeros({nbins}, TensorOptions(weights)); auto ret = cuda::CUDA_tensor_histogram( output, self, weights, nbins, 1); } else { - output = zeros(CUDA(kLong), {nbins}); + output = native::zeros({nbins}, device(kCUDA).dtype(kLong)); auto ret = cuda::CUDA_tensor_histogram( output, self, weights, nbins, 1); } diff --git a/aten/src/ATen/native/cuda/TensorFactories.cu b/aten/src/ATen/native/cuda/TensorFactories.cu index 88fc932aaa95..f522f161c494 100644 --- a/aten/src/ATen/native/cuda/TensorFactories.cu +++ b/aten/src/ATen/native/cuda/TensorFactories.cu @@ -1,4 +1,5 @@ #include "ATen/ATen.h" +#include "ATen/Error.h" #include "ATen/NativeFunctions.h" #include "ATen/cuda/CUDATypeConversion.cuh" @@ -10,17 +11,17 @@ #include #include -#include +#include namespace at { namespace native { +Tensor& eye_out_cuda(Tensor& result, int64_t n) { + return at::native::eye_out_cuda(result, n, /*m=*/-1); +} + Tensor& eye_out_cuda(Tensor& result, int64_t n, int64_t m) { - if (n <= 0) { - std::ostringstream oss; - oss << "n must be greater than 0, got: " << n; - std::runtime_error(oss.str()); - } + AT_CHECK(n > 0, "n must be greater than zero, got", n); if(m <= 0) { m = n; } @@ -37,14 +38,9 @@ Tensor& eye_out_cuda(Tensor& result, int64_t n, int64_t m) { } Tensor& randperm_out_cuda(Tensor& result, int64_t n, Generator* generator) { - if (n < 0) { - std::ostringstream oss; - oss << "n must be non-negative, got " << n; - throw std::runtime_error(oss.str()); - } - + AT_CHECK(n >= 0, "n must be non-negative, got", n); AT_CHECK(result.type().scalarTensor(n).defined(), - "n is too large for result tensor type: '", result.type().toString(), "'"); + "n is too large for result tensor type: '", result.type().toString(), "'"); result.resize_({n}); diff --git a/aten/src/ATen/native/cuda/TensorTransformations.cu b/aten/src/ATen/native/cuda/TensorTransformations.cu index cfcdc0299483..4e9d59df9e12 100644 --- a/aten/src/ATen/native/cuda/TensorTransformations.cu +++ b/aten/src/ATen/native/cuda/TensorTransformations.cu @@ -5,6 +5,9 @@ #include "ATen/cuda/CUDATensorMethods.cuh" #include "ATen/cuda/CUDATypeConversion.cuh" +#include +#include + namespace at { namespace native { @@ -100,7 +103,7 @@ Tensor flip_cuda(const Tensor& self, IntList dims) { auto out_tensor = at::empty_like(in_tensor); // stride_contiguous is the stride of non-contiguous tensor after called contiguous(), it is used to compute indices for each element in non-contiguous tensor - Tensor stride_contiguous = at::zeros(CPU(kLong), {total_dims}); + Tensor stride_contiguous = at::zeros({total_dims}, kLong); int64_t* stride_contiguous_d = stride_contiguous.data(); int64_t tmp = N; for (int64_t i = 0; i < total_dims; i++) { diff --git a/aten/src/ATen/native/native_functions.yaml b/aten/src/ATen/native/native_functions.yaml index 7a9d70ef62c2..555d04ae408f 100644 --- a/aten/src/ATen/native/native_functions.yaml +++ b/aten/src/ATen/native/native_functions.yaml @@ -82,6 +82,7 @@ variants: function - func: allclose(Tensor self, Tensor other, double rtol=1e-5, double atol=1e-8, bool equal_nan=False) -> bool + device_guard: false - func: addmv(Tensor self, Tensor mat, Tensor vec, *, Scalar beta=1, Scalar alpha=1) -> Tensor @@ -98,18 +99,32 @@ - func: addr_out(Tensor result, Tensor self, Tensor vec1, Tensor vec2, *, Scalar beta=1, Scalar alpha=1) -> Tensor variants: function -- func: arange(Type dtype, Scalar start, Scalar end, Scalar step=1) -> Tensor +- func: arange(Scalar start, Scalar end, TensorOptions options={}) -> Tensor variants: function -- func: arange_out(Tensor result, Scalar start, Scalar end, Scalar step=1) -> Tensor +- func: arange(Scalar start, Scalar end, Scalar step, TensorOptions options={}) -> Tensor variants: function -- func: arange(Type dtype, Scalar end) -> Tensor +- func: arange_out(Tensor result, Scalar start, Scalar end) -> Tensor + variants: function + +- func: arange_out(Tensor result, Scalar start, Scalar end, Scalar step) -> Tensor + variants: function + +- func: arange(Scalar end, TensorOptions options={}) -> Tensor variants: function - func: arange_out(Tensor result, Scalar end) -> Tensor variants: function +- func: arange(Type dtype, Scalar start, Scalar end, Scalar step=1) -> Tensor + variants: function + deprecated: true + +- func: arange(Type dtype, Scalar end) -> Tensor + variants: function + deprecated: true + # This function is a temporary hack to allow tracing of arange like constructs with dynamic # bounds on arange. Normal arange is not traceable because it does not take any tensor inputs; # if the range you need is based on another tensor, calling this function directly will @@ -157,7 +172,10 @@ CPU: _atan_out_cpu CUDA: _atan_out_cuda -- func: bartlett_window(Type dtype, int64_t window_length, bool periodic=True) -> Tensor +- func: bartlett_window(int64_t window_length, TensorOptions options={}) -> Tensor + variants: function + +- func: bartlett_window(int64_t window_length, bool periodic, TensorOptions options={}) -> Tensor variants: function - func: batch_norm(Tensor input, Tensor? weight, Tensor? bias, Tensor? running_mean, Tensor? running_var, bool training, double momentum, double eps, bool cudnn_enabled) -> Tensor @@ -183,7 +201,10 @@ CPU: _bincount_cpu CUDA: _bincount_cuda -- func: blackman_window(Type dtype, int64_t window_length, bool periodic=True) -> Tensor +- func: blackman_window(int64_t window_length, TensorOptions options={}) -> Tensor + variants: function + +- func: blackman_window(int64_t window_length, bool periodic, TensorOptions options={}) -> Tensor variants: function - func: cat(TensorList tensors, int64_t dim=0) -> Tensor @@ -209,6 +230,7 @@ - func: cudnn_is_acceptable(Tensor self) -> bool variants: function + device_guard: false - func: convolution(Tensor input, Tensor weight, Tensor? bias, IntList stride, IntList padding, IntList dilation, bool transposed, IntList output_padding, int64_t groups) -> Tensor variants: function @@ -451,7 +473,7 @@ CPU: embedding_bag_backward_cpu CUDA: embedding_bag_backward_cuda -- func: empty(Type dtype, IntList size) -> Tensor +- func: empty(IntList size, TensorOptions options={}) -> Tensor variants: function - func: empty_out(Tensor result, IntList size) -> Tensor @@ -460,10 +482,12 @@ - func: empty_like(Tensor self) -> Tensor variants: function -- func: empty_like(Tensor self, *, Type dtype) -> Tensor +- func: empty_like(Tensor self, *, TensorOptions options) -> Tensor variants: function - python_default_init: - dtype: self.type() + +- func: empty(Type dtype, IntList size) -> Tensor + variants: function + deprecated: true - func: erf(Tensor self) -> Tensor @@ -510,15 +534,28 @@ - func: expand_as(Tensor self, Tensor other) -> Tensor variants: method # This is method-only to match the previous tensor API. In the future we could make this a function too. -- func: eye(Type dtype, int64_t n, int64_t m=-1) -> Tensor +- func: eye(int64_t n, TensorOptions options={}) -> Tensor variants: function -- func: eye_out(Tensor result, int64_t n, int64_t m=-1) -> Tensor +- func: eye(int64_t n, int64_t m, TensorOptions options={}) -> Tensor + variants: function + +- func: eye_out(Tensor result, int64_t n) -> Tensor variants: function dispatch: CPU: eye_out_cpu CUDA: eye_out_cuda +- func: eye_out(Tensor result, int64_t n, int64_t m) -> Tensor + variants: function + dispatch: + CPU: eye_out_cpu + CUDA: eye_out_cuda + +- func: eye(Type dtype, int64_t n, int64_t m=-1) -> Tensor + variants: function + deprecated: true + - func: fill_(Tensor self, Scalar value) -> Tensor - func: fill_(Tensor self, Tensor value) -> Tensor @@ -536,7 +573,7 @@ CPU: _floor_out_cpu CUDA: _floor_out_cuda -- func: full(Type dtype, IntList size, Scalar fill_value) -> Tensor +- func: full(IntList size, Scalar fill_value, TensorOptions options={}) -> Tensor variants: function - func: full_out(Tensor result, IntList size, Scalar fill_value) -> Tensor @@ -545,15 +582,29 @@ - func: full_like(Tensor self, Scalar fill_value) -> Tensor variants: function -- func: full_like(Tensor self, Scalar fill_value, *, Type dtype) -> Tensor - variants: function - python_default_init: - dtype: self.type() - -- func: hann_window(Type dtype, int64_t window_length, bool periodic=True) -> Tensor +- func: full_like(Tensor self, Scalar fill_value, *, TensorOptions options) -> Tensor variants: function -- func: hamming_window(Type dtype, int64_t window_length, bool periodic=True, double alpha=0.54, double beta=0.46) -> Tensor +- func: full(Type dtype, IntList size, Scalar fill_value) -> Tensor + variants: function + deprecated: true + +- func: hann_window(int64_t window_length, TensorOptions options={}) -> Tensor + variants: function + +- func: hann_window(int64_t window_length, bool periodic, TensorOptions options={}) -> Tensor + variants: function + +- func: hamming_window(int64_t window_length, TensorOptions options={}) -> Tensor + variants: function + +- func: hamming_window(int64_t window_length, bool periodic, TensorOptions options={}) -> Tensor + variants: function + +- func: hamming_window(int64_t window_length, bool periodic, double alpha, TensorOptions options={}) -> Tensor + variants: function + +- func: hamming_window(int64_t window_length, bool periodic, double alpha, double beta, TensorOptions options={}) -> Tensor variants: function - func: hinge_embedding_loss(Tensor self, Tensor target, double margin=1.0, bool size_average=true, bool reduce=true) -> Tensor @@ -606,28 +657,45 @@ - func: isclose(Tensor self, Tensor other, double rtol=1e-5, double atol=1e-8, bool equal_nan=False) -> Tensor - func: is_cuda(Tensor self) -> bool + device_guard: false - func: is_distributed(Tensor self) -> bool + device_guard: false - func: is_floating_point(Tensor self) -> bool + device_guard: false - func: is_nonzero(Tensor self) -> bool + device_guard: false - func: is_same_size(Tensor self, Tensor other) -> bool + device_guard: false - func: is_signed(Tensor self) -> bool + device_guard: false - func: is_sparse(Tensor self) -> bool + device_guard: false - func: layer_norm(Tensor input, IntList normalized_shape, Tensor? weight={}, Tensor? bias={}, double eps=1e-5, bool cudnn_enable=True) -> Tensor variants: function -- func: linspace(Type dtype, Scalar start, Scalar end, int64_t steps=100) -> Tensor +- func: linspace(Scalar start, Scalar end, TensorOptions options={}) -> Tensor variants: function -- func: linspace_out(Tensor result, Scalar start, Scalar end, int64_t steps=100) -> Tensor +- func: linspace(Scalar start, Scalar end, int64_t steps, TensorOptions options={}) -> Tensor variants: function +- func: linspace_out(Tensor result, Scalar start, Scalar end) -> Tensor + variants: function + +- func: linspace_out(Tensor result, Scalar start, Scalar end, int64_t steps) -> Tensor + variants: function + +- func: linspace(Type dtype, Scalar start, Scalar end, int64_t steps=100) -> Tensor + variants: function + deprecated: true + - func: log(Tensor self) -> Tensor - func: log_(Tensor self) -> Tensor @@ -682,12 +750,22 @@ - func: logdet(Tensor self) -> Tensor -- func: logspace(Type dtype, Scalar start, Scalar end, int64_t steps=100) -> Tensor +- func: logspace(Scalar start, Scalar end, TensorOptions options={}) -> Tensor variants: function -- func: logspace_out(Tensor result, Scalar start, Scalar end, int64_t steps=100) -> Tensor +- func: logspace(Scalar start, Scalar end, int64_t steps, TensorOptions options={}) -> Tensor variants: function +- func: logspace_out(Tensor result, Scalar start, Scalar end) -> Tensor + variants: function + +- func: logspace_out(Tensor result, Scalar start, Scalar end, int64_t steps) -> Tensor + variants: function + +- func: logspace(Type dtype, Scalar start, Scalar end, int64_t steps=100) -> Tensor + variants: function + deprecated: true + - func: log_softmax(Tensor self, int64_t dim) -> Tensor dispatch: CPU: log_softmax_cpu @@ -742,7 +820,7 @@ - func: narrow(Tensor self, int64_t dim, int64_t start, int64_t length) -> Tensor -- func: ones(Type dtype, IntList size) -> Tensor +- func: ones(IntList size, TensorOptions options={}) -> Tensor variants: function - func: ones_out(Tensor result, IntList size) -> Tensor @@ -751,10 +829,12 @@ - func: ones_like(Tensor self) -> Tensor variants: function -- func: ones_like(Tensor self, *, Type dtype) -> Tensor +- func: ones_like(Tensor self, *, TensorOptions options) -> Tensor variants: function - python_default_init: - dtype: self.type() + +- func: ones(Type dtype, IntList size) -> Tensor + variants: function + deprecated: true - func: pairwise_distance(Tensor x1, Tensor x2, double p=2, double eps=1e-6, bool keepdim=false) -> Tensor variants: function @@ -764,30 +844,58 @@ - func: pin_memory(Tensor self) -> Tensor -- func: rand(Type dtype, IntList size, *, Generator* generator=nullptr) -> Tensor +- func: rand(IntList size, *, TensorOptions options={}) -> Tensor variants: function -- func: rand_out(Tensor result, IntList size, *, Generator* generator=nullptr) -> Tensor +- func: rand(IntList size, *, Generator* generator, TensorOptions options={}) -> Tensor + variants: function + +- func: rand_out(Tensor result, IntList size, *) -> Tensor + variants: function + +- func: rand_out(Tensor result, IntList size, *, Generator* generator) -> Tensor variants: function - func: rand_like(Tensor self) -> Tensor variants: function -- func: rand_like(Tensor self, *, Type dtype) -> Tensor +- func: rand_like(Tensor self, *, TensorOptions options) -> Tensor + variants: function + +- func: rand(Type dtype, IntList size, *, Generator* generator=nullptr) -> Tensor + variants: function + deprecated: true + +- func: randint(int64_t high, IntList size, *, TensorOptions options={}) -> Tensor + variants: function + +- func: randint(int64_t high, IntList size, *, Generator* generator, TensorOptions options={}) -> Tensor + variants: function + +- func: randint(int64_t low, int64_t high, IntList size, *, TensorOptions options={}) -> Tensor + variants: function + +- func: randint(int64_t low, int64_t high, IntList size, *, Generator* generator, TensorOptions options={}) -> Tensor variants: function - python_default_init: - dtype: self.type() - func: randint(Type dtype, int64_t high, IntList size, *, Generator* generator=nullptr) -> Tensor variants: function + deprecated: true - func: randint(Type dtype, int64_t low, int64_t high, IntList size, *, Generator* generator=nullptr) -> Tensor variants: function + deprecated: true -- func: randint_out(Tensor result, int64_t high, IntList size, *, Generator* generator=nullptr) -> Tensor +- func: randint_out(Tensor result, int64_t high, IntList size, *) -> Tensor variants: function -- func: randint_out(Tensor result, int64_t low, int64_t high, IntList size, *, Generator* generator=nullptr) -> Tensor +- func: randint_out(Tensor result, int64_t high, IntList size, *, Generator* generator) -> Tensor + variants: function + +- func: randint_out(Tensor result, int64_t low, int64_t high, IntList size, *) -> Tensor + variants: function + +- func: randint_out(Tensor result, int64_t low, int64_t high, IntList size, *, Generator* generator) -> Tensor variants: function - func: randint_like(Tensor self, int64_t high) -> Tensor @@ -796,45 +904,69 @@ - func: randint_like(Tensor self, int64_t low, int64_t high) -> Tensor variants: function -- func: randint_like(Tensor self, int64_t high, *, Type dtype) -> Tensor - variants: function - python_default_init: - dtype: self.type() - -- func: randint_like(Tensor self, int64_t low, int64_t high, *, Type dtype) -> Tensor - variants: function - python_default_init: - dtype: self.type() - -- func: randn(Type dtype, IntList size, *, Generator* generator=nullptr) -> Tensor +- func: randint_like(Tensor self, int64_t high, *, TensorOptions options) -> Tensor variants: function -- func: randn_out(Tensor result, IntList size, *, Generator* generator=nullptr) -> Tensor +- func: randint_like(Tensor self, int64_t low, int64_t high, *, TensorOptions options) -> Tensor + variants: function + +- func: randn(IntList size, *, TensorOptions options={}) -> Tensor + variants: function + +- func: randn(IntList size, *, Generator* generator, TensorOptions options={}) -> Tensor + variants: function + +- func: randn_out(Tensor result, IntList size, *) -> Tensor + variants: function + +- func: randn_out(Tensor result, IntList size, *, Generator* generator) -> Tensor variants: function - func: randn_like(Tensor self) -> Tensor variants: function -- func: randn_like(Tensor self, *, Type dtype) -> Tensor - variants: function - python_default_init: - dtype: self.type() - -- func: randperm(Type dtype, int64_t n, *, Generator* generator=nullptr) -> Tensor +- func: randn_like(Tensor self, *, TensorOptions options) -> Tensor variants: function -- func: randperm_out(Tensor result, int64_t n, *, Generator* generator=nullptr) -> Tensor +- func: randn(Type dtype, IntList size, *, Generator* generator=nullptr) -> Tensor + variants: function + deprecated: true + +- func: randperm(int64_t n, *, TensorOptions options={}) -> Tensor + variants: function + +- func: randperm(int64_t n, *, Generator* generator, TensorOptions options={}) -> Tensor + variants: function + +- func: randperm_out(Tensor result, int64_t n, *) -> Tensor + variants: function + +- func: randperm_out(Tensor result, int64_t n, *, Generator* generator) -> Tensor variants: function dispatch: CPU: randperm_out_cpu CUDA: randperm_out_cuda -- func: range(Type dtype, Scalar start, Scalar end, Scalar step=1) -> Tensor +- func: randperm(Type dtype, int64_t n, *, Generator* generator=nullptr) -> Tensor + variants: function + deprecated: true + +- func: range(Scalar start, Scalar end, TensorOptions options={}) -> Tensor variants: function -- func: range_out(Tensor result, Scalar start, Scalar end, Scalar step=1) -> Tensor +- func: range(Scalar start, Scalar end, Scalar step, TensorOptions options={}) -> Tensor variants: function +- func: range_out(Tensor result, Scalar start, Scalar end) -> Tensor + variants: function + +- func: range_out(Tensor result, Scalar start, Scalar end, Scalar step) -> Tensor + variants: function + +- func: range(Type dtype, Scalar start, Scalar end, Scalar step=1) -> Tensor + variants: function + deprecated: true + - func: repeat(Tensor self, IntList repeats) -> Tensor variants: method # This is method-only to match the previous tensor API. In the future we could make this a function too. @@ -935,6 +1067,7 @@ CUDA: _sinh_out_cuda - func: size(Tensor self, int64_t dim) -> int64_t + device_guard: false - func: slice(Tensor self, int64_t dim=0, int64_t start=0, int64_t end=9223372036854775807, int64_t step=1) -> Tensor @@ -987,6 +1120,7 @@ fft_size: frame_length - func: stride(Tensor self, int64_t dim) -> int64_t + device_guard: false # FIXME: These could be combined as optional but for https://github.com/pytorch/pytorch/issues/6593. - func: sum(Tensor self, *, ScalarType dtype) -> Tensor @@ -1157,7 +1291,7 @@ CPU: _s_where_cpu CUDA: _s_where_cuda -- func: zeros(Type dtype, IntList size) -> Tensor +- func: zeros(IntList size, TensorOptions options={}) -> Tensor variants: function - func: zeros_out(Tensor result, IntList size) -> Tensor @@ -1166,10 +1300,12 @@ - func: zeros_like(Tensor self) -> Tensor variants: function -- func: zeros_like(Tensor self, *, Type dtype) -> Tensor +- func: zeros_like(Tensor self, *, TensorOptions options) -> Tensor variants: function - python_default_init: - dtype: self.type() + +- func: zeros(Type dtype, IntList size) -> Tensor + variants: function + deprecated: true - func: _standard_gamma_grad(Tensor self, Tensor output) -> Tensor dispatch: diff --git a/aten/src/ATen/native/sparse/SparseTensor.cpp b/aten/src/ATen/native/sparse/SparseTensor.cpp index 19df1cebd98c..743e96ae6177 100644 --- a/aten/src/ATen/native/sparse/SparseTensor.cpp +++ b/aten/src/ATen/native/sparse/SparseTensor.cpp @@ -293,7 +293,7 @@ SparseTensor& resize_as_sparse_(SparseTensor& self, const SparseTensor& src) { // NB: Dropped the resizeNd variants Tensor sparse_to_dense(const SparseTensor& self) { - Tensor dst = self.type().toDense().zeros(self.sizes()); + Tensor dst = at::zeros(self.sizes(), self.type().toDense()); return dst.add_(self); } @@ -345,7 +345,7 @@ SparseTensor coalesce_sparse_cpu(const SparseTensor& self) { int64_t denseDims = self._denseDims(); int64_t nnz = self._nnz(); - LongTensor indices_scalar = at::CPU(kLong).zeros({nnz}); + LongTensor indices_scalar = at::zeros({nnz}, kLong); int64_t factor = 1; for (int64_t d = sparseDims - 1; d >= 0; d--) { diff --git a/aten/src/ATen/native/sparse/SparseTensorMath.cpp b/aten/src/ATen/native/sparse/SparseTensorMath.cpp index d2353761f59d..5b87377f3ac1 100644 --- a/aten/src/ATen/native/sparse/SparseTensorMath.cpp +++ b/aten/src/ATen/native/sparse/SparseTensorMath.cpp @@ -47,7 +47,7 @@ namespace { LongTensor _to_csr(const int64_t* indices, int64_t dim, int64_t nnz) { int64_t h, i, hp0, hp1; - LongTensor csr = at::CPU(kLong).zeros({dim + 1}); + LongTensor csr = native::zeros({dim + 1}, kLong); // TODO: eliminate this conditional when zero-size dims supported correctly if (nnz > 0) { @@ -746,8 +746,8 @@ SparseTensor& _sspaddmm_out_cpu( int64_t t_nnz = t._nnz(); int64_t r_nnz = nnz * dim_k + t_nnz; - LongTensor newi = at::CPU(kLong).tensor({2, r_nnz}); - LongTensor newv = values.type().zeros({r_nnz}); + LongTensor newi = native::empty({2, r_nnz}, kLong); + LongTensor newv = native::zeros({r_nnz}, TensorOptions(values)); if (t_nnz != 0) { LongTensor narrowi = newi.narrow(1, 0, t_nnz); diff --git a/aten/src/ATen/native_parse.py b/aten/src/ATen/native_parse.py index d4b5c2bedefe..93262c59eb1a 100644 --- a/aten/src/ATen/native_parse.py +++ b/aten/src/ATen/native_parse.py @@ -117,6 +117,7 @@ def run(paths): declaration['return'] = list(func.get('return', return_type)) declaration['variants'] = func.get('variants', ['method', 'function']) declaration['deprecated'] = func.get('deprecated', False) + declaration['device_guard'] = func.get('device_guard', True) declaration['arguments'] = func.get('arguments', parse_arguments(arguments, func, declaration['name'], declaration['return'])) declaration['type_method_definition_dispatch'] = func.get('dispatch', declaration['name']) diff --git a/aten/src/ATen/optional.h b/aten/src/ATen/optional.h index 5283b2988eb3..287ddd8577b3 100644 --- a/aten/src/ATen/optional.h +++ b/aten/src/ATen/optional.h @@ -9,7 +9,9 @@ // // From https://github.com/akrzemi1/Optional // -// ATen: move to at namespace +// ATen: +// - Move to `at` namespace. +// - Remove macro use in line 478 because the nvcc device compiler cannot handle it. #pragma once @@ -475,7 +477,7 @@ public: } constexpr T const& operator *() const { - return TR2_OPTIONAL_ASSERTED_EXPRESSION(initialized(), contained_val()); + return contained_val(); } T& operator *() { diff --git a/aten/src/ATen/templates/Functions.h b/aten/src/ATen/templates/Functions.h index f3bf63f95102..bbc165af80a0 100644 --- a/aten/src/ATen/templates/Functions.h +++ b/aten/src/ATen/templates/Functions.h @@ -8,6 +8,9 @@ #include "ATen/Storage.h" #include "ATen/Generator.h" #include "ATen/Deprecated.h" +#include "ATen/NativeFunctions.h" +#include "ATen/DeviceGuard.h" +#include "ATen/TensorOptions.h" namespace at { diff --git a/aten/src/ATen/templates/NativeFunctions.h b/aten/src/ATen/templates/NativeFunctions.h index 02ca4c929b1c..1356ee2c5e5b 100644 --- a/aten/src/ATen/templates/NativeFunctions.h +++ b/aten/src/ATen/templates/NativeFunctions.h @@ -2,14 +2,27 @@ // ${generated_comment} -#include "ATen/ATen.h" +#include +#include +#include +#include + +#include +#include #include #include +namespace at { +struct Generator; +class Scalar; +struct Tensor; +struct Type; +} // namespace at + namespace at { namespace native { ${native_function_declarations} -} -} +} // namespace native +} // namespace at diff --git a/aten/src/ATen/templates/Tensor.h b/aten/src/ATen/templates/Tensor.h index d4256f30ce78..1b6874dafafc 100644 --- a/aten/src/ATen/templates/Tensor.h +++ b/aten/src/ATen/templates/Tensor.h @@ -11,6 +11,8 @@ #include "ATen/TensorBase.h" #include "ATen/TensorImpl.h" #include "ATen/Utils.h" +#include "ATen/Device.h" +#include "ATen/Layout.h" namespace at { struct Type; @@ -87,6 +89,15 @@ struct Tensor : public detail::TensorBase { /// Defined in Type.h because of include order issues. bool is_variable() const noexcept; + /// Returns a `Tensor`'s layout. Defined in Type.h + Layout layout() const noexcept; + + /// Returns a `Tensor`'s dtype (`ScalarType`). Defined in Type.h + ScalarType dtype() const noexcept; + + /// Returns a `Tensor`'s device. + Device device() const; + template T * data() const; @@ -135,8 +146,9 @@ struct Tensor : public detail::TensorBase { // ~~~~~ Autograd API ~~~~~ - void set_requires_grad(bool requires_grad) { + Tensor& set_requires_grad(bool requires_grad) { pImpl->set_requires_grad(requires_grad); + return *this; } bool requires_grad() const { return pImpl->requires_grad(); diff --git a/aten/src/ATen/templates/TensorMethods.h b/aten/src/ATen/templates/TensorMethods.h index b1fd477da9a6..846f5c5ccbd4 100644 --- a/aten/src/ATen/templates/TensorMethods.h +++ b/aten/src/ATen/templates/TensorMethods.h @@ -5,6 +5,7 @@ #include "ATen/Tensor.h" #include "ATen/Scalar.h" #include "ATen/SparseTensorRef.h" +#include "ATen/Type.h" namespace at { diff --git a/aten/src/ATen/templates/Type.cpp b/aten/src/ATen/templates/Type.cpp index 42659a5b211b..9ccc552a7673 100644 --- a/aten/src/ATen/templates/Type.cpp +++ b/aten/src/ATen/templates/Type.cpp @@ -2,13 +2,16 @@ // ${generated_comment} -#include "ATen/Tensor.h" -#include "ATen/Storage.h" -#include "ATen/Scalar.h" -#include "ATen/SparseTensorRef.h" #include "ATen/ExpandUtils.h" #include "ATen/NativeFunctions.h" +#include "ATen/Scalar.h" +#include "ATen/SparseTensorRef.h" +#include "ATen/Storage.h" +#include "ATen/Tensor.h" +#include "ATen/TensorOptions.h" #include "ATen/UndefinedType.h" +#include "ATen/DeviceGuard.h" + #include #include @@ -29,6 +32,7 @@ Tensor & Type::copy_(Tensor & self, const Tensor & src, bool non_blocking) const } Tensor Type::copy(const Tensor & src, bool non_blocking) const { + // TODO(psag): have a DeviceGuard here AT_CHECK(src.defined(), "attempt to copy an undefined tensor"); if (is_sparse()) { auto indices = src._indices(); diff --git a/aten/src/ATen/templates/Type.h b/aten/src/ATen/templates/Type.h index 7a362de7a807..53eec291f0d5 100644 --- a/aten/src/ATen/templates/Type.h +++ b/aten/src/ATen/templates/Type.h @@ -11,6 +11,8 @@ #include "ATen/ScalarType.h" #include "ATen/SparseTensorRef.h" #include "ATen/Tensor.h" +#include "ATen/Deprecated.h" +#include "ATen/Layout.h" #include #include @@ -46,6 +48,7 @@ struct AT_API Type { virtual ~Type() {} virtual ScalarType scalarType() const = 0; virtual Backend backend() const = 0; + Layout layout() const noexcept { return layout_from_backend(backend()); } virtual bool is_cuda() const = 0; virtual bool is_sparse() const = 0; virtual bool is_distributed() const = 0; @@ -103,4 +106,16 @@ inline bool Tensor::is_variable() const noexcept { return type().is_variable(); } +inline ScalarType Tensor::dtype() const noexcept { + return type().scalarType(); +} + +inline Layout Tensor::layout() const noexcept { + return type().layout(); +} + +inline Device Tensor::device() const { + return Device(type().backend(), type().is_cuda() ? get_device() : -1); +} + } // namespace at diff --git a/aten/src/ATen/templates/TypeDerived.cpp b/aten/src/ATen/templates/TypeDerived.cpp index 938fbe1d928a..229166772f2b 100644 --- a/aten/src/ATen/templates/TypeDerived.cpp +++ b/aten/src/ATen/templates/TypeDerived.cpp @@ -17,14 +17,19 @@ #include "ATen/${DenseTensor}.h" #include "ATen/${DenseBackend}LongTensor.h" #include "ATen/Allocator.h" -#include "ATen/Utils.h" #include "ATen/Half.h" #include "ATen/WrapDimUtils.h" +#include "ATen/NativeFunctions.h" #include "ATen/THLongStorageView.h" #include "ATen/UndefinedTensor.h" -#include "ATen/NativeFunctions.h" -#include -#include +#include "ATen/Utils.h" +#include "ATen/DeviceGuard.h" +#include "ATen/optional.h" + +#include +#include +#include +#include #include "ATen/Config.h" $extra_cuda_headers diff --git a/aten/src/ATen/test/apply_utils_test.cpp b/aten/src/ATen/test/apply_utils_test.cpp index 2fd3d67a0e71..24359a05e4bf 100644 --- a/aten/src/ATen/test/apply_utils_test.cpp +++ b/aten/src/ATen/test/apply_utils_test.cpp @@ -26,14 +26,14 @@ void fill_tensor(int64_t scalar, Tensor& t_) { // double (using a4 as a target). We also exercise on a zero_dim and empty // tensor. void test(Type& type, IntList shape, int64_t a = 0, int64_t b = 1) { - auto zero_dim = type.tensor({}); + auto zero_dim = at::empty({}, type); zero_dim.fill_(2); zero_dim.exp_(); AT_DISPATCH_FLOATING_TYPES(zero_dim.type(), "test0", [&] { ASSERT(zero_dim.data()[0] == std::exp(2)); }); - auto empty_t = type.tensor({0}); + auto empty_t = at::empty({0}, type); empty_t.fill_(3); empty_t.exp_(); diff --git a/aten/src/ATen/test/atest.cpp b/aten/src/ATen/test/atest.cpp index eff411b783b9..c8d88b658b43 100644 --- a/aten/src/ATen/test/atest.cpp +++ b/aten/src/ATen/test/atest.cpp @@ -9,7 +9,7 @@ using namespace std; using namespace at; void trace() { - Tensor foo = rand(CPU(kFloat), {12,12}); + Tensor foo = rand({12,12}); // ASSERT foo is 2-dimensional and holds floats. auto foo_a = foo.accessor(); @@ -27,7 +27,7 @@ TEST_CASE( "atest", "[]" ) { manual_seed(123, at::Backend::CPU); manual_seed(123, at::Backend::CUDA); - auto foo = rand(CPU(kFloat), {12,6}); + auto foo = rand({12,6}); REQUIRE(foo.data() == foo.toFloatData()); REQUIRE(foo.size(0) == 12); @@ -56,7 +56,7 @@ TEST_CASE( "atest", "[]" ) { } } - REQUIRE(foo.equal(4 * CPU(kByte).ones({12, 6}))); + REQUIRE(foo.equal(4 * ones({12, 6}, kByte))); trace(); diff --git a/aten/src/ATen/test/basic.cpp b/aten/src/ATen/test/basic.cpp index 2109ea6d8c35..627471ea9819 100644 --- a/aten/src/ATen/test/basic.cpp +++ b/aten/src/ATen/test/basic.cpp @@ -29,13 +29,13 @@ static void test(Type & type) { } SECTION( "ones and dot" ) { - Tensor b0 = ones(type, {1, 1}); + Tensor b0 = ones({1, 1}, type); REQUIRE(2 == (b0+b0).sum().toCDouble()); - Tensor b1 = ones(type, {1, 2}); + Tensor b1 = ones({1, 2}, type); REQUIRE(4 == (b1+b1).sum().toCDouble()); - Tensor b = ones(type, {3, 4}); + Tensor b = ones({3, 4}, type); REQUIRE(24 == (b+b).sum().toCDouble()); REQUIRE(12 == b.numel()); REQUIRE(b.view(-1).dot(b.view(-1)).toCDouble() == 12); @@ -43,14 +43,12 @@ static void test(Type & type) { SECTION( "rand" ) { for(auto i = 0; i < 10; i++) { - Tensor a = rand(type.toScalarType(i % 2 == 0 ? kFloat : kDouble), {3,4}); - //std::cout << a << std::endl; - //TODO EXPECT + Tensor a = rand({3,4}, type.toScalarType(i % 2 == 0 ? kFloat : kDouble)); } } SECTION( "sort" ) { - Tensor b = rand(type, {3, 4}); + Tensor b = rand({3, 4}, type); auto z = b.sort(1); auto z_sorted = std::get<0>(z); @@ -60,7 +58,7 @@ static void test(Type & type) { if(type.backend() != kCUDA) SECTION( "randperm" ) { - Tensor b = randperm(type, 15); + Tensor b = randperm(15, type); Tensor rv, ri; std::tie(rv, ri) = sort(b, 0); REQUIRE(Scalar(rv[0]).toFloat() <= Scalar(rv[1]).toFloat()); @@ -72,8 +70,8 @@ static void test(Type & type) { } SECTION( "add" ) { - Tensor a = rand(type, {3, 4}); - Tensor b = rand(type, {3, 4}); + Tensor a = rand({3, 4}, type); + Tensor b = rand({3, 4}, type); Tensor c = add(a, add(a, b)); //TODO:0-dim Tensor d(3.f); Scalar d = 3.f; @@ -82,8 +80,8 @@ static void test(Type & type) { SECTION( "loads of adds" ) { auto begin = std::chrono::high_resolution_clock::now(); - Tensor d = ones(type, {3, 4}); - Tensor r = zeros(type, {3, 4}); + Tensor d = ones({3, 4}, type); + Tensor r = zeros({3, 4}, type); for(auto i = 0; i < 100000; i++) { add_out(r, r, d); } @@ -95,8 +93,8 @@ static void test(Type & type) { SECTION( "loads of adds (with copy)" ) { auto begin = std::chrono::high_resolution_clock::now(); - Tensor d = ones(type, {3, 4}); - Tensor r = zeros(type, {3, 4}); + Tensor d = ones({3, 4}, type); + Tensor r = zeros({3, 4}, type); for(auto i = 0; i < 100000; i++) { r = add(r, d); } @@ -107,46 +105,46 @@ static void test(Type & type) { } SECTION( "isContiguous" ) { - Tensor a = rand(type, {3, 4}); + Tensor a = rand({3, 4}, type); REQUIRE(a.is_contiguous()); a = a.transpose(0, 1); REQUIRE(!a.is_contiguous()); } SECTION( "permute" ) { - Tensor a = rand(type, {3, 4, 5}); + Tensor a = rand({3, 4, 5}, type); Tensor b = a.permute({1, 2, 0}); REQUIRE(b.sizes().equals({4, 5, 3})); REQUIRE(b.strides().equals({5, 1, 20})); } SECTION( "mm" ) { - Tensor a = rand(type, {3, 4}); - Tensor b = rand(type, {4}); + Tensor a = rand({3, 4}, type); + Tensor b = rand({4}, type); Tensor c = mv(a, b); - REQUIRE(c.equal(addmv(zeros(type, {3}), a, b, 0, 1))); + REQUIRE(c.equal(addmv(zeros({3}, type), a, b, 0, 1))); } SECTION( "squeeze" ) { - Tensor a = rand(type, {2, 1}); + Tensor a = rand({2, 1}, type); Tensor b = squeeze(a); REQUIRE(b.dim() == 1); - a = rand(type, {1}); + a = rand({1}, type); b = squeeze(a); //TODO 0-dim squeeze REQUIRE(a[0].equal(b)); } SECTION( "copy" ) { - Tensor a = zeros(type, {4, 3}); - Tensor e = rand(type, {4, 3}); + Tensor a = zeros({4, 3}, type); + Tensor e = rand({4, 3}, type); a.copy_(e); REQUIRE(a.equal(e)); } SECTION( "copy (broadcasting)" ) { - Tensor a = zeros(type, {4, 3}); - Tensor e = rand(type, {3}); + Tensor a = zeros({4, 3}, type); + Tensor e = rand({3}, type); a.copy_(e); for (int i = 0; i < 4; ++i) { REQUIRE(a[i].equal(e)); @@ -170,12 +168,12 @@ static void test(Type & type) { #endif SECTION( "adding a value with a scalar" ) { - Tensor a = rand(type, {4, 3}); - REQUIRE((ones(type, {4,3}) + a).equal(add(a,1))); + Tensor a = rand({4, 3}, type); + REQUIRE((ones({4,3}, type) + a).equal(add(a,1))); } SECTION( "select" ) { - Tensor a = rand(type, {3, 7}); + Tensor a = rand({3, 7}, type); auto a_13 = select(a, 1, 3); auto a_13_02 = select(select(a, 1, 3), 0, 2); REQUIRE( a[0][3].equal(a_13[0]) ); @@ -186,16 +184,16 @@ static void test(Type & type) { Tensor a = type.scalarTensor(4); //rand(type, {1}); REQUIRE_NOTHROW(Scalar(a)); - Tensor b = rand(type, {3,4}); + Tensor b = rand({3,4}, type); REQUIRE((a + a).dim() == 0); REQUIRE((1 + a).dim() == 0); REQUIRE((b + a).dim() == 2); REQUIRE((a + b).dim() == 2); - auto c = rand(type, {3,4}); + auto c = rand({3,4}, type); REQUIRE(c[1][2].dim() == 0); - auto f = rand(type, {3,4}); - f[2] = zeros(type, {4}); + auto f = rand({3,4}, type); + f[2] = zeros({4}, type); f[1][0] = -1; REQUIRE(Scalar(f[2][0]).toDouble() == 0); } @@ -209,25 +207,25 @@ static void test(Type & type) { } SECTION( "toCFloat" ) { - Tensor a = zeros(CPU(kFloat), {3,4}); - Tensor b = ones(CPU(kFloat), {3,7}); + Tensor a = zeros({3,4}); + Tensor b = ones({3,7}); Tensor c = cat({a,b},1); REQUIRE(c.size(1) == 11); - Tensor e = rand(CPU(kFloat), {}); + Tensor e = rand({}); REQUIRE(*e.data() == e.sum().toCFloat()); } SECTION( "to string" ) { - Tensor b = ones(CPU(kFloat), {3,7})*.0000001f; + Tensor b = ones({3,7})*.0000001f; std::stringstream s; s << b << "\n"; std::string expect = "1e-07 *"; REQUIRE(s.str().substr(0,expect.size()) == expect); } SECTION("indexing by Scalar") { - Tensor tensor = CPU(kInt).arange(0, 10); - Tensor one = CPU(kInt).ones({1}); + Tensor tensor = arange(0, 10, kInt); + Tensor one = ones({1}, kInt); for (int64_t i = 0; i < tensor.numel(); ++i) { REQUIRE(tensor[i].equal(one * i)); } @@ -249,25 +247,25 @@ static void test(Type & type) { "Can only index tensors with integral scalars (got CPUDoubleType)")); } SECTION("indexing by zero-dim tensor") { - Tensor tensor = CPU(kInt).arange(0, 10); - Tensor one = CPU(kInt).ones({}); + Tensor tensor = arange(0, 10, kInt); + Tensor one = ones({}, kInt); for (int i = 0; i < tensor.numel(); ++i) { REQUIRE(tensor[one * i].equal(one * i)); } REQUIRE_THROWS_WITH( - tensor[CPU(kFloat).ones({}) * 3.14].equal(one), + tensor[ones({}) * 3.14].equal(one), StartsWith( "Can only index tensors with integral scalars (got CPUFloatType)")); REQUIRE_THROWS_WITH( tensor[Tensor()].equal(one), StartsWith("Can only index with tensors that are defined")); REQUIRE_THROWS_WITH( - tensor[CPU(kInt).ones({2, 3, 4})].equal(one), + tensor[ones({2, 3, 4}, kInt)].equal(one), StartsWith("Can only index with tensors that are scalars (zero-dim)")); } SECTION("dispatch") { - Tensor tensor = CPU(kFloat).randn({20, 20}); - Tensor other = CPU(kFloat).randn({20, 20}); + Tensor tensor = randn({20, 20}); + Tensor other = randn({20, 20}); auto result = tensor.m(relu).m(mse_loss, other, true, true); REQUIRE(result.allclose(mse_loss(relu(tensor), other))); } diff --git a/aten/src/ATen/test/broadcast_test.cpp b/aten/src/ATen/test/broadcast_test.cpp index a397eaacf722..2c98121da031 100644 --- a/aten/src/ATen/test/broadcast_test.cpp +++ b/aten/src/ATen/test/broadcast_test.cpp @@ -14,7 +14,7 @@ TEST_CASE( "broadcast", "[]" ) { // 0) pre-req tests: SECTION( "can't expand empty tensor" ) { - auto empty = randn(T, {0}); + auto empty = randn({0}, T); REQUIRE_THROWS(empty.expand({3})); } @@ -22,28 +22,28 @@ TEST_CASE( "broadcast", "[]" ) { SECTION( "out-place function with 2 args" ) { SECTION( "basic" ) { - auto a = randn(T, {3, 1}); - auto b = randn(T, {5}); + auto a = randn({3, 1}, T); + auto b = randn({5}, T); std::vector expanded_sizes = {3, 5}; REQUIRE((a + b).equal(a.expand(expanded_sizes) + b.expand(expanded_sizes))); } SECTION( "with scalar" ) { - auto aScalar = ones(T, {1}); + auto aScalar = ones({1}, T); aScalar.get()->maybeScalar(true); - auto b = randn(T, {3, 5}); + auto b = randn({3, 5}, T); REQUIRE((aScalar + b).equal(aScalar.expand(b.sizes()) + b.expand(b.sizes()))); } SECTION( "old fallback behavior yields error" ) { - auto a = randn(T, {3, 5}); - auto b = randn(T, {5, 3}); + auto a = randn({3, 5}, T); + auto b = randn({5, 3}, T); REQUIRE_THROWS(a + b); } SECTION( "with mismatched sizes" ) { - auto a = randn(T, {3, 5}); - auto b = randn(T, {7, 5}); + auto a = randn({3, 5}, T); + auto b = randn({7, 5}, T); REQUIRE_THROWS(a + b); } } @@ -51,64 +51,64 @@ TEST_CASE( "broadcast", "[]" ) { SECTION( "out-place function with 3 args" ) { SECTION( "basic" ) { - auto a = randn(T, {3, 1, 1}); - auto b = randn(T, {1, 2, 1}); - auto c = randn(T, {1, 1, 5}); + auto a = randn({3, 1, 1}, T); + auto b = randn({1, 2, 1}, T); + auto c = randn({1, 1, 5}, T); std::vector expanded_sizes = {3, 2, 5}; REQUIRE((a + b + c).equal(a.expand(expanded_sizes) + b.expand(expanded_sizes) + c.expand(expanded_sizes))); } SECTION( "with scalar" ) { - auto aTensorScalar = ones(T, {1}); + auto aTensorScalar = ones({1}, T); aTensorScalar.get()->maybeScalar(true); - auto b = randn(T, {3, 2, 1}); - auto c = randn(T, {1, 2, 5}); + auto b = randn({3, 2, 1}, T); + auto c = randn({1, 2, 5}, T); std::vector expanded_sizes = {3, 2, 5}; REQUIRE(aTensorScalar.addcmul(b, c).equal( aTensorScalar.expand(expanded_sizes).addcmul(b.expand(expanded_sizes), c.expand(expanded_sizes)))); } SECTION( "old fallback behavior yields error" ) { - auto a = randn(T, {3, 2, 5}); - auto b = randn(T, {2, 3, 5}); - auto c = randn(T, {5, 3, 2}); + auto a = randn({3, 2, 5}, T); + auto b = randn({2, 3, 5}, T); + auto c = randn({5, 3, 2}, T); REQUIRE_THROWS(a.addcmul(b, c)); } SECTION( "with mismatched sizes" ){ - auto a = randn(T, {3, 2, 5}); - auto b = randn(T, {2, 3, 5}); - auto c = randn(T, {5, 5, 5}); + auto a = randn({3, 2, 5}, T); + auto b = randn({2, 3, 5}, T); + auto c = randn({5, 5, 5}, T); REQUIRE_THROWS(a.addcmul(b, c)); } } SECTION( "in-place function with 2 args" ) { SECTION( "basic" ) { - auto a = randn(T, {3, 5}); - auto b = randn(T, {3, 1}); + auto a = randn({3, 5}, T); + auto b = randn({3, 1}, T); REQUIRE((a + b).equal(a + b.expand({3, 5}))); } SECTION( "with scalar" ) { - auto a = randn(T, {3, 5}); - auto bScalar = ones(T, {1}); + auto a = randn({3, 5}, T); + auto bScalar = ones({1}, T); bScalar.get()->maybeScalar(true); REQUIRE((a + bScalar).equal(a + bScalar.expand(a.sizes()))); } SECTION( "error: would have to expand inplace arg" ) { - auto a = randn(T, {1, 5}); - auto b = randn(T, {3, 1}); + auto a = randn({1, 5}, T); + auto b = randn({3, 1}, T); REQUIRE_THROWS(a.add_(b)); } } SECTION( "in-place function with 3 args" ) { - auto a = randn(T, {3, 5, 2}); - auto b = randn(T, {3, 1, 2}); - auto c = randn(T, {1, 5, 1}); + auto a = randn({3, 5, 2}, T); + auto b = randn({3, 1, 2}, T); + auto c = randn({1, 5, 1}, T); SECTION( "basic" ) { auto aClone = a.clone(); @@ -117,37 +117,37 @@ TEST_CASE( "broadcast", "[]" ) { SECTION( "with scalar" ) { auto aClone = a.clone(); - auto bScalar = ones(T, {1}); + auto bScalar = ones({1}, T); bScalar.get()->maybeScalar(true); REQUIRE(a.addcmul_(bScalar, c).equal(aClone.addcmul_(bScalar.expand(a.sizes()), c.expand(a.sizes())))); } SECTION( "error: would have to expand inplace arg" ) { - auto a = randn(T, {1, 3, 5}); - auto b = randn(T, {4, 1, 1}); - auto c = randn(T, {1, 3, 1}); + auto a = randn({1, 3, 5}, T); + auto b = randn({4, 1, 1}, T); + auto c = randn({1, 3, 1}, T); REQUIRE_THROWS(a.addcmul_(b, c)); } } SECTION( "explicit dim specification" ) { - auto a = randn(T, {1}); - auto b = randn(T, {5, 3}); - auto c = randn(T, {3, 7}); + auto a = randn({1}, T); + auto b = randn({5, 3}, T); + auto c = randn({3, 7}, T); SECTION( "basic" ) { REQUIRE(a.addmm(b, c).equal(a.expand({5,7}).addmm(b, c))); } SECTION( "with scalar" ) { - Tensor aScalar = ones(T, {1}); + Tensor aScalar = ones({1}, T); aScalar.get()->maybeScalar(true); REQUIRE(aScalar.addmm(b, c).equal(aScalar.expand({5, 7}).addmm(b, c))); } SECTION( "with mismatched sizes" ) { - auto a = randn(T, {3, 3}); + auto a = randn({3, 3}, T); REQUIRE_THROWS(a.addmm(b, c)); } } diff --git a/aten/src/ATen/test/cuda_rng_test.cpp b/aten/src/ATen/test/cuda_rng_test.cpp index 536598eeba24..d32903dd2fe1 100644 --- a/aten/src/ATen/test/cuda_rng_test.cpp +++ b/aten/src/ATen/test/cuda_rng_test.cpp @@ -8,7 +8,7 @@ void makeRandomNumber() { cudaSetDevice(std::rand() % 2); - auto x = at::CUDA(at::kFloat).randn({1000}); + auto x = at::randn({1000}); } void testCudaRNGMultithread() { @@ -24,4 +24,4 @@ void testCudaRNGMultithread() { TEST_CASE( "CUDA RNG test", "[cuda]" ) { SECTION( "multithread" ) testCudaRNGMultithread(); -} \ No newline at end of file +} diff --git a/aten/src/ATen/test/dlconvertor_test.cpp b/aten/src/ATen/test/dlconvertor_test.cpp index 77d894a7bb7a..1603e3d54b16 100644 --- a/aten/src/ATen/test/dlconvertor_test.cpp +++ b/aten/src/ATen/test/dlconvertor_test.cpp @@ -17,7 +17,7 @@ TEST_CASE( "dlconvertor", "[cpu]" ) { INFO( "convert ATen to DLTensor" ); - Tensor a = rand(CPU(at::kFloat), {3,4}); + Tensor a = rand({3,4}); DLManagedTensor* dlMTensor = toDLPack(a); INFO( "convert DLTensor to ATen" ); @@ -25,4 +25,3 @@ TEST_CASE( "dlconvertor", "[cpu]" ) { REQUIRE(a.equal(b)); } - diff --git a/aten/src/ATen/test/native_test.cpp b/aten/src/ATen/test/native_test.cpp index ee1ec12a6b6d..99a21d36d772 100644 --- a/aten/src/ATen/test/native_test.cpp +++ b/aten/src/ATen/test/native_test.cpp @@ -25,7 +25,7 @@ void requireEqualTensorList(TensorList t1, TensorList t2) { } void test(Type & T, Type & AccT) { - auto t = randn(T, {3, 3}); + auto t = randn({3, 3}, T); SECTION( "split: test method, type, namespace give same result" ) { auto splitMethod = t.split(1, 0); @@ -52,9 +52,9 @@ void test(Type & T, Type & AccT) { // stack SECTION( "stack" ) { - auto x = rand(T, {2, 3, 4}); - auto y = rand(T, {2, 3, 4}); - auto z = rand(T, {2, 3, 4}); + auto x = rand({2, 3, 4}); + auto y = rand({2, 3, 4}); + auto z = rand({2, 3, 4}); for (int64_t dim = 0; dim < 4; ++dim) { auto res = at::stack({x, y, z}, dim); auto res_neg = at::stack({x, y, z}, dim - 4); @@ -72,13 +72,13 @@ void test(Type & T, Type & AccT) { } SECTION( "size / stride" ) { - auto scalar = randn(T, {}); + auto scalar = randn({}, T); REQUIRE_THROWS_WITH(scalar.size(0), "dimension specified as 0 but tensor has no dimensions"); REQUIRE_THROWS_WITH(scalar.size(-1), "dimension specified as -1 but tensor has no dimensions"); REQUIRE_THROWS_WITH(scalar.stride(0), "dimension specified as 0 but tensor has no dimensions"); REQUIRE_THROWS_WITH(scalar.stride(-1), "dimension specified as -1 but tensor has no dimensions"); - auto empty = randn(T, {0}); + auto empty = randn({0}, T); REQUIRE(empty.size(0) == 0); REQUIRE(empty.size(-1) == 0); REQUIRE(empty.stride(0) == 1); @@ -87,9 +87,9 @@ void test(Type & T, Type & AccT) { // matmul SECTION( "matmul" ) { - auto scalar = randn(T, {}); - auto d1 = randn(T, {3}); - auto d2 = randn(T, {2, 3}); + auto scalar = randn({}, T); + auto d1 = randn({3}, T); + auto d2 = randn({2, 3}, T); // 0-d REQUIRE_THROWS_WITH(scalar.matmul(d2), Catch::StartsWith("both arguments to matmul need to be at least 1D")); @@ -98,19 +98,19 @@ void test(Type & T, Type & AccT) { // 1-d REQUIRE_ALLCLOSE(d1.matmul(d1), d1.dot(d1)); REQUIRE_ALLCLOSE(d2.matmul(d1), d2.mv(d1)); - auto d1o = randn(T, {2}); + auto d1o = randn({2}, T); REQUIRE_ALLCLOSE(d1o.matmul(d2), d1o.unsqueeze(0).mm(d2).squeeze(0)); // 2-d - auto d2o = randn(T, {3, 5}); + auto d2o = randn({3, 5}, T); REQUIRE_ALLCLOSE(d2.matmul(d2o), d2.mm(d2o)); // > 2-d, 1-d - auto d3 = randn(T, {5, 2, 3}); + auto d3 = randn({5, 2, 3}, T); REQUIRE_ALLCLOSE(d3.matmul(d1), d3.bmm(d1.view({1, 3, 1}).expand({5, 3, 1})).view({5, 2})); REQUIRE_ALLCLOSE(d1o.matmul(d3), d1o.expand({5, 1, 2}).bmm(d3).view({5, 3})); - auto d5 = randn(T, {3, 2, 4, 2, 3}); + auto d5 = randn({3, 2, 4, 2, 3}, T); REQUIRE_ALLCLOSE(d5.matmul(d1), d5.view({24, 2, 3}).bmm(d1.view({1, 3, 1}).expand({24, 3, 1})).view({3, 2, 4, 2})); REQUIRE_ALLCLOSE(d1o.matmul(d5), d1o.expand({24, 1, 2}).bmm(d5.view({24, 2, 3})).view({3, 2, 4, 3})); @@ -120,8 +120,8 @@ void test(Type & T, Type & AccT) { // Tolerances are selected empirically. double atol = 1e-04; double rtol = 1e-06; - d2 = randn(T, {3, 4}); - d2o = randn(T, {4, 2}); + d2 = randn({3, 4}, T); + d2o = randn({4, 2}, T); auto result = d5.matmul(d2).toType(AccT); auto d5Acc = d5.toType(AccT); @@ -131,45 +131,45 @@ void test(Type & T, Type & AccT) { REQUIRE_ALLCLOSE(d2o.matmul(d5), d2o.expand({24, 4, 2}).bmm(d5.view({24, 2, 3})).view({3, 2, 4, 4, 3})); // > 2-d, > 2-d - auto d5o = randn(T, {2, 1, 2, 4, 3, 2}); + auto d5o = randn({2, 1, 2, 4, 3, 2}, T); auto d5_bmm_view = d5.expand({2, 3, 2, 4, 2, 3}).contiguous().view({48, 2, 3}); auto d5o_bmm_view = d5o.expand({2, 3, 2, 4, 3, 2}).contiguous().view({48, 3, 2}); REQUIRE_ALLCLOSE(d5.matmul(d5o), d5_bmm_view.bmm(d5o_bmm_view).view({2, 3, 2, 4, 2, 2})); // non-expandable case - auto d5wrong = randn(T, {2, 4, 2, 4, 3, 2}); + auto d5wrong = randn({2, 4, 2, 4, 3, 2}, T); REQUIRE_THROWS_WITH(d5.matmul(d5wrong), Catch::Contains("must match the size")); } // _standard_gamma_grad SECTION( "_standard_gamma_grad" ) { // check empty - auto empty = ones(T, {0}); + auto empty = ones({0}, T); REQUIRE_EQUAL(empty, empty._standard_gamma_grad(empty)); // check scalar equals one element - auto one_scalar = ones(T, {}).mul(5); - auto one_with_dim = ones(T, {1}).mul(5); + auto one_scalar = ones({}, T).mul(5); + auto one_with_dim = ones({1}, T).mul(5); REQUIRE_ALLCLOSE(one_scalar._standard_gamma_grad(one_scalar), one_with_dim._standard_gamma_grad(one_with_dim).sum()); // check mixing types - auto t1 = randn(T, {3, 4}); - auto t2 = randn(T, {3, 4}).toType(kDouble); + auto t1 = randn({3, 4}, T); + auto t2 = randn({3, 4}, T).toType(kDouble); REQUIRE_THROWS_WITH(t1._standard_gamma_grad(t2), Catch::StartsWith("expected scalar type")); } SECTION( "where" ) { // empty - auto empty = ones(T, {0}); + auto empty = ones({0}, T); auto &bT = T.toScalarType(ScalarType::Byte); - auto empty_byte = ones(bT, {0}); + auto empty_byte = ones({0}, bT); REQUIRE_EQUAL(empty, at::where(empty_byte, empty, empty)); // check scalar equals one element - auto x_scalar = ones(T, {}).mul(5); - auto y_scalar = ones(T, {}).mul(7); - auto cond_scalar = zeros(bT, {}); + auto x_scalar = ones({}, T).mul(5); + auto y_scalar = ones({}, T).mul(7); + auto cond_scalar = zeros({}, bT); auto x_1d = x_scalar.unsqueeze(0); auto y_1d = y_scalar.unsqueeze(0); auto cond_1d = cond_scalar.unsqueeze(0); diff --git a/aten/src/ATen/test/scalar_tensor_test.cpp b/aten/src/ATen/test/scalar_tensor_test.cpp index fac48be8a5fa..bc8315f2a10a 100644 --- a/aten/src/ATen/test/scalar_tensor_test.cpp +++ b/aten/src/ATen/test/scalar_tensor_test.cpp @@ -48,7 +48,7 @@ void test(Type &T) { // single-tensor/size tests for (auto s = sizes.begin(); s != sizes.end(); ++s) { // verify that the dim, sizes, strides, etc match what was requested. - auto t = ones(T, *s); + auto t = ones(*s, T); REQUIRE((size_t)t.dim() == s->size()); REQUIRE((size_t)t.ndimension() == s->size()); REQUIRE(t.sizes().equals(*s)); @@ -60,9 +60,9 @@ void test(Type &T) { REQUIRE_NOTHROW(ss << t << std::endl); // set_ - auto t2 = ones(T, *s); + auto t2 = ones(*s, T); t2.set_(); - require_equal_size_dim(t2, ones(T, {0})); + require_equal_size_dim(t2, ones({0}, T)); // unsqueeze if (t.numel() != 0) { @@ -73,7 +73,7 @@ void test(Type &T) { // unsqueeze_ { - auto t2 = ones(T, *s); + auto t2 = ones(*s, T); if (t2.numel() != 0) { auto r = t2.unsqueeze_(0); REQUIRE(r.dim() == t.dim() + 1); @@ -100,12 +100,12 @@ void test(Type &T) { } } auto result = t.squeeze(); - require_equal_size_dim(result, ones(T, size_without_ones)); + require_equal_size_dim(result, ones(size_without_ones, T)); } { // squeeze_ (with dimension argument) - auto t2 = ones(T, *s); + auto t2 = ones(*s, T); if (t2.dim() == 0 || t2.sizes()[0] == 1) { REQUIRE(t2.squeeze_(0).dim() == std::max(t.dim() - 1, 0)); } else { @@ -117,7 +117,7 @@ void test(Type &T) { // squeeze_ (with no dimension argument) { - auto t2 = ones(T, *s); + auto t2 = ones(*s, T); std::vector size_without_ones; for (auto size : *s) { if (size != 1) { @@ -125,7 +125,7 @@ void test(Type &T) { } } auto r = t2.squeeze_(); - require_equal_size_dim(t2, ones(T, size_without_ones)); + require_equal_size_dim(t2, ones(size_without_ones, T)); } // reduce (with dimension argument and with 1 return argument) @@ -133,7 +133,7 @@ void test(Type &T) { REQUIRE(t.sum(0).dim() == std::max(t.dim() - 1, 0)); } else { if (!T.is_cuda()) { // FIXME: out of range exception in CUDA - REQUIRE(t.sum(0).equal(T.tensor({0}))); + REQUIRE(t.sum(0).equal(at::empty({0}, T))); } } @@ -164,8 +164,8 @@ void test(Type &T) { for (auto rhs_it = sizes.begin(); rhs_it != sizes.end(); ++rhs_it) { // is_same_size should only match if they are the same shape { - auto lhs = ones(T, *lhs_it); - auto rhs = ones(T, *rhs_it); + auto lhs = ones(*lhs_it, T); + auto rhs = ones(*rhs_it, T); if(*lhs_it != *rhs_it) { REQUIRE(!lhs.is_same_size(rhs)); REQUIRE(!rhs.is_same_size(lhs)); @@ -175,15 +175,15 @@ void test(Type &T) { { // resize_ { - auto lhs = ones(T, *lhs_it); - auto rhs = ones(T, *rhs_it); + auto lhs = ones(*lhs_it, T); + auto rhs = ones(*rhs_it, T); lhs.resize_(*rhs_it); require_equal_size_dim(lhs, rhs); } // resize_as_ { - auto lhs = ones(T, *lhs_it); - auto rhs = ones(T, *rhs_it); + auto lhs = ones(*lhs_it, T); + auto rhs = ones(*rhs_it, T); lhs.resize_as_(rhs); require_equal_size_dim(lhs, rhs); } @@ -191,15 +191,15 @@ void test(Type &T) { { { // with tensor - auto lhs = ones(T, *lhs_it); - auto rhs = ones(T, *rhs_it); + auto lhs = ones(*lhs_it, T); + auto rhs = ones(*rhs_it, T); lhs.set_(rhs); require_equal_size_dim(lhs, rhs); } { // with storage - auto lhs = ones(T, *lhs_it); - auto rhs = ones(T, *rhs_it); + auto lhs = ones(*lhs_it, T); + auto rhs = ones(*rhs_it, T); auto storage = T.storage(rhs.numel()); lhs.set_(*storage); // should not be dim 0 because an empty storage is dim 1; all other storages aren't scalars @@ -207,8 +207,8 @@ void test(Type &T) { } { // with storage, offset, sizes, strides - auto lhs = ones(T, *lhs_it); - auto rhs = ones(T, *rhs_it); + auto lhs = ones(*lhs_it, T); + auto rhs = ones(*rhs_it, T); auto storage = T.storage(rhs.numel()); lhs.set_(*storage, rhs.storage_offset(), rhs.sizes(), rhs.strides()); require_equal_size_dim(lhs, rhs); @@ -218,8 +218,8 @@ void test(Type &T) { // view { - auto lhs = ones(T, *lhs_it); - auto rhs = ones(T, *rhs_it); + auto lhs = ones(*lhs_it, T); + auto rhs = ones(*rhs_it, T); auto rhs_size = *rhs_it; TRY_CATCH_ELSE(auto result = lhs.view(rhs_size), REQUIRE(lhs.numel() != rhs.numel()), @@ -228,8 +228,8 @@ void test(Type &T) { // take { - auto lhs = ones(T, *lhs_it); - auto rhs = zeros(T, *rhs_it).toType(ScalarType::Long); + auto lhs = ones(*lhs_it, T); + auto rhs = zeros(*rhs_it, T).toType(ScalarType::Long); TRY_CATCH_ELSE(auto result = lhs.take(rhs), REQUIRE(lhs.numel() == 0); REQUIRE(rhs.numel() != 0), require_equal_size_dim(result, rhs)); @@ -238,8 +238,8 @@ void test(Type &T) { // ger { - auto lhs = ones(T, *lhs_it); - auto rhs = ones(T, *rhs_it); + auto lhs = ones(*lhs_it, T); + auto rhs = ones(*rhs_it, T); TRY_CATCH_ELSE(auto result = lhs.ger(rhs), REQUIRE((lhs.numel() == 0 || rhs.numel() == 0 || lhs.dim() != 1 || rhs.dim() != 1)), [&]() { @@ -251,9 +251,9 @@ void test(Type &T) { // expand { - auto lhs = ones(T, *lhs_it); + auto lhs = ones(*lhs_it, T); auto lhs_size = *lhs_it; - auto rhs = ones(T, *rhs_it); + auto rhs = ones(*rhs_it, T); auto rhs_size = *rhs_it; bool should_pass = should_expand(lhs_size, rhs_size); TRY_CATCH_ELSE(auto result = lhs.expand(rhs_size), @@ -267,7 +267,7 @@ void test(Type &T) { bool should_pass_inplace = should_expand(rhs_size, lhs_size); TRY_CATCH_ELSE(lhs.add_(rhs), REQUIRE(!should_pass_inplace), - REQUIRE(should_pass_inplace); require_equal_size_dim(lhs, ones(T, *lhs_it));); + REQUIRE(should_pass_inplace); require_equal_size_dim(lhs, ones(*lhs_it, T));); } } } diff --git a/aten/src/ATen/test/scalar_test.cpp b/aten/src/ATen/test/scalar_test.cpp index 342132f28a97..ccdab08dc820 100644 --- a/aten/src/ATen/test/scalar_test.cpp +++ b/aten/src/ATen/test/scalar_test.cpp @@ -85,7 +85,7 @@ TEST_CASE( "scalar test", "[]" ) { auto && C = at::globalContext(); if(at::hasCUDA()) { auto & CUDAFloat = C.getType(Backend::CUDA,ScalarType::Float); - auto t2 = zeros(CUDAFloat, {4,4}); + auto t2 = zeros({4,4}, CUDAFloat); cout << &t2 << "\n"; cout << "AFTER GET TYPE " << &CUDAFloat << "\n"; auto s = CUDAFloat.storage(4); @@ -93,9 +93,9 @@ TEST_CASE( "scalar test", "[]" ) { s->fill(7); REQUIRE( s->get(3).toFloat() == 7.0 ); } - auto t = ones(CPU(Float), {4,4}); + auto t = ones({4,4}); - auto wha2 = zeros(CPU(Float), {4,4}).add(t).sum(); + auto wha2 = zeros({4,4}).add(t).sum(); REQUIRE( wha2.toCDouble() == 16.0 ); REQUIRE( t.sizes()[0] == 4 ); @@ -104,10 +104,10 @@ TEST_CASE( "scalar test", "[]" ) { REQUIRE( t.strides()[1] == 1 ); Type & T = CPU(Float); - Tensor x = randn(T, {1,10}); - Tensor prev_h = randn(T, {1,20}); - Tensor W_h = randn(T, {20,20}); - Tensor W_x = randn(T, {20,10}); + Tensor x = randn({1,10}, T); + Tensor prev_h = randn({1,20}, T); + Tensor W_h = randn({20,20}, T); + Tensor W_x = randn({20,10}, T); Tensor i2h = at::mm(W_x, x.t()); Tensor h2h = at::mm(W_h, prev_h.t()); Tensor next_h = i2h.add(h2h); @@ -122,12 +122,12 @@ TEST_CASE( "scalar test", "[]" ) { auto r = CUDA(Float).copy(next_h); REQUIRE(CPU(Float).copy(r).equal(next_h)); } - REQUIRE_NOTHROW(randn(T, {10,10,2})); + REQUIRE_NOTHROW(randn({10,10,2}, T)); // check Scalar.toTensor on Scalars backed by different data types REQUIRE(bar.toTensor().type().scalarType() == kDouble); REQUIRE(what.toTensor().type().scalarType() == kLong); - REQUIRE(Scalar(ones(CPU(kFloat), {})).toTensor().type().scalarType() == kFloat); + REQUIRE(Scalar(ones({})).toTensor().type().scalarType() == kFloat); if (x.type().scalarType() != ScalarType::Half) { AT_DISPATCH_ALL_TYPES(x.type(), "foo", [&] { @@ -141,10 +141,10 @@ TEST_CASE( "scalar test", "[]" ) { // test direct C-scalar type conversions { - auto x = ones(T, {1,2}); + auto x = ones({1,2}, T); REQUIRE_THROWS(x.toCFloat()); } - auto float_one = ones(T, {}); + auto float_one = ones({}, T); REQUIRE(float_one.toCFloat() == 1); REQUIRE(float_one.toCInt() == 1); REQUIRE((float_one.toCHalf() == 1)); diff --git a/aten/src/ATen/test/tbb_init_test.cpp b/aten/src/ATen/test/tbb_init_test.cpp index 2327674cea42..027b87822787 100644 --- a/aten/src/ATen/test/tbb_init_test.cpp +++ b/aten/src/ATen/test/tbb_init_test.cpp @@ -8,10 +8,10 @@ using namespace at; // This checks whether threads can see the global // numbers of threads set and also whether the scheduler -// will throw an exception when multiple threads call +// will throw an exception when multiple threads call // their first parallel construct. void test(int given_num_threads) { - auto t = ones(CPU(kFloat), {1000 * 1000}); + auto t = ones({1000 * 1000}, CPU(kFloat)); if (given_num_threads >= 0) { ASSERT(at::get_num_threads() == given_num_threads); } else { diff --git a/aten/src/ATen/test/test_install/main.cpp b/aten/src/ATen/test/test_install/main.cpp index 901844d480d5..adeae38a6d31 100644 --- a/aten/src/ATen/test/test_install/main.cpp +++ b/aten/src/ATen/test/test_install/main.cpp @@ -1,5 +1,5 @@ #include "ATen/ATen.h" int main() { - std::cout << at::ones(at::CPU(at::kFloat), {3,4}) << "\n"; + std::cout << at::ones({3,4}, at::CPU(at::kFloat)) << "\n"; } diff --git a/aten/src/ATen/test/test_parallel.cpp b/aten/src/ATen/test/test_parallel.cpp index c83bf0a858a7..5dbd9676e5de 100644 --- a/aten/src/ATen/test/test_parallel.cpp +++ b/aten/src/ATen/test/test_parallel.cpp @@ -16,14 +16,13 @@ TEST_CASE( "parallel", "[cpu]" ) { manual_seed(123, at::Backend::CPU); set_num_threads(1); - Tensor a = rand(CPU(at::kFloat), {1,3}); + Tensor a = rand({1,3}); a[0][0] = 1; a[0][1] = 0; a[0][2] = 0; - Tensor as = rand(CPU(at::kFloat), {3}); + Tensor as = rand({3}); as[0] = 1; as[1] = 0; as[2] = 0; REQUIRE(a.sum(0).equal(as)); } - diff --git a/aten/src/ATen/test/undefined_tensor_test.cpp b/aten/src/ATen/test/undefined_tensor_test.cpp index f2a1656961c6..d88923d57d53 100644 --- a/aten/src/ATen/test/undefined_tensor_test.cpp +++ b/aten/src/ATen/test/undefined_tensor_test.cpp @@ -13,7 +13,7 @@ TEST_CASE( "undefined tensor test", "[]" ) { // mainly test ops on undefined tensors don't segfault and give a reasonable errror message. Tensor und; - Tensor ft = ones(CPU(kFloat), {1}); + Tensor ft = ones({1}, CPU(kFloat)); std::stringstream ss; ss << und << std::endl; @@ -46,9 +46,8 @@ TEST_CASE( "undefined tensor test", "[]" ) { REQUIRE_THROWS_WITH(und.toBackend(Backend::CPU), Catch::Contains("toBackend")); REQUIRE_THROWS_WITH(ft.toBackend(Backend::Undefined), Catch::Contains("UndefinedType")); - Tensor to_move = ones(CPU(kFloat), {1}); + Tensor to_move = ones({1}, CPU(kFloat)); Tensor m(std::move(to_move)); REQUIRE(!to_move.defined()); REQUIRE(to_move.get() == UndefinedTensor::singleton()); } - diff --git a/aten/src/ATen/test/wrapdim_test.cpp b/aten/src/ATen/test/wrapdim_test.cpp index 35c1bb8d5aba..19357677a089 100644 --- a/aten/src/ATen/test/wrapdim_test.cpp +++ b/aten/src/ATen/test/wrapdim_test.cpp @@ -12,29 +12,29 @@ TEST_CASE( "wrapdim test", "[]" ) { Type & T = CPU(kFloat); SECTION( "simple case" ) { - auto a = randn(T, {2, 3, 4, 5}); + auto a = randn({2, 3, 4, 5}, T); REQUIRE(a.prod(-4).equal(a.prod(0))); REQUIRE(a.prod(3).equal(a.prod(-1))); } SECTION( "expression specification" ) { - auto a = randn(T, {2, 3, 4, 5}); + auto a = randn({2, 3, 4, 5}, T); REQUIRE(a.unsqueeze(-5).equal(a.unsqueeze(0))); REQUIRE(a.unsqueeze(4).equal(a.unsqueeze(-1))); // can unsqueeze scalar - auto b = randn(T, 1); + auto b = randn(1, T); b.get()->maybeScalar(true); REQUIRE(b.unsqueeze(0).equal(b.unsqueeze(-1))); } SECTION( "empty tensor" ) { - auto a = randn(T, 0); - REQUIRE(a.prod(0).equal(T.tensor({0}))); + auto a = randn(0, T); + REQUIRE(a.prod(0).equal(at::empty({0}, T))); } SECTION( "scalar vs 1-dim, 1-size" ) { - auto a = randn(T, 1); + auto a = randn(1, T); REQUIRE(a.prod(0).equal(a.prod(-1))); a.get()->maybeScalar(true); REQUIRE(a.get()->isScalar()); diff --git a/caffe2/contrib/aten/gen_op.py b/caffe2/contrib/aten/gen_op.py index 998943b6651a..18a3db4c7dae 100755 --- a/caffe2/contrib/aten/gen_op.py +++ b/caffe2/contrib/aten/gen_op.py @@ -107,7 +107,14 @@ def expand(o): # filter the list of declarations removing things we cannot support -def supports(o): +def supports(o, factory_methods): + # Ignore all families (!) of functions that have TensorOptions (i.e. tensor factory methods). + if o['name'] in factory_methods: + if factory_methods[o['name']] == 0: + print("Skipping {} because it is a factory method".format(o['name'])) + factory_methods[o['name']] += 1 + return False + # skip all in-place operators for now since aten cannot Resize # caffe2 memory inside an operator if o['inplace']: @@ -183,9 +190,18 @@ def get_num_inputs(o): return str(args) +def find_factory_methods(decls): + factory_methods = {} + for o in decls: + if any(arg['dynamic_type'] == 'TensorOptions' for arg in o['arguments']): + factory_methods[o['name']] = 0 + return factory_methods + + if __name__ == '__main__': decls = yaml.load(read(os.path.join(args.yaml_dir, 'Declarations.yaml')), Loader=Loader) - filtered = [expanded for o in decls for expanded in expand(o) if supports(expanded)] + factory_methods = find_factory_methods(decls) + filtered = [expanded for o in decls for expanded in expand(o) if supports(expanded, factory_methods)] top_env = { 'mappings': [], 'implementations': [], diff --git a/setup.py b/setup.py index 3c0a48e5b099..cd0bfec85167 100644 --- a/setup.py +++ b/setup.py @@ -729,7 +729,6 @@ main_sources = [ "torch/csrc/torch.cpp", "torch/csrc/utils.cpp", "torch/csrc/utils/cuda_lazy_init.cpp", - "torch/csrc/utils/device.cpp", "torch/csrc/utils/invalid_arguments.cpp", "torch/csrc/utils/object_ptr.cpp", "torch/csrc/utils/python_arg_parser.cpp", diff --git a/test/cpp/api/any.cpp b/test/cpp/api/any.cpp index 7988c876603a..b4a9397b401d 100644 --- a/test/cpp/api/any.cpp +++ b/test/cpp/api/any.cpp @@ -61,8 +61,7 @@ TEST_CASE("any-module") { const std::string& b, std::string&& c) { const auto s = a + b + c; - return autograd::make_variable( - at::ones(at::CPU(at::kFloat), {static_cast(s.size())})); + return torch::ones({static_cast(s.size())}); } }; AnyModule any(M{}); diff --git a/test/cpp/api/container.cpp b/test/cpp/api/container.cpp index b28e06099f1d..ca55899810cf 100644 --- a/test/cpp/api/container.cpp +++ b/test/cpp/api/container.cpp @@ -1,5 +1,6 @@ #include +#include #include #include #include @@ -34,8 +35,7 @@ class NestedModel : public Module { NestedModel() { l1 = register_module("l1", Linear(5, 20).build()); t = register_module("test", std::make_shared()); - param_ = - register_parameter("param", at::CPU(at::kFloat).tensor({3, 2, 21})); + param_ = register_parameter("param", torch::empty({3, 2, 21})); } std::vector forward(std::vector input) { @@ -51,7 +51,7 @@ TEST_CASE("containers") { SECTION("conv") { SECTION("1d") { auto model = Conv1d(3, 2, 3).stride(2).build(); - auto x = Var(at::CPU(at::kFloat).randn({2, 3, 5}), true); + auto x = torch::randn({2, 3, 5}, at::requires_grad()); auto y = model->forward({x})[0]; Variable s = y.sum(); @@ -67,7 +67,7 @@ TEST_CASE("containers") { SECTION("2d") { SECTION("even") { auto model = Conv2d(3, 2, 3).stride(2).build(); - auto x = Var(at::CPU(at::kFloat).randn({2, 3, 5, 5}), true); + auto x = torch::randn({2, 3, 5, 5}, at::requires_grad()); auto y = model->forward({x})[0]; Variable s = y.sum(); @@ -78,13 +78,12 @@ TEST_CASE("containers") { REQUIRE(y.size(i) == 2); } - REQUIRE( - model->parameters()["weight"].grad().numel() == 3 * 2 * 3 * 3); + REQUIRE(model->parameters()["weight"].grad().numel() == 3 * 2 * 3 * 3); } SECTION("uneven") { auto model = Conv2d(3, 2, {3, 2}).stride({2, 2}).build(); - auto x = Var(at::CPU(at::kFloat).randn({2, 3, 5, 4}), true); + auto x = torch::randn({2, 3, 5, 4}, at::requires_grad()); auto y = model->forward({x})[0]; Variable s = y.sum(); @@ -95,13 +94,12 @@ TEST_CASE("containers") { REQUIRE(y.size(i) == 2); } - REQUIRE( - model->parameters()["weight"].grad().numel() == 3 * 2 * 3 * 2); + REQUIRE(model->parameters()["weight"].grad().numel() == 3 * 2 * 3 * 2); } } SECTION("3d") { auto model = Conv3d(3, 2, 3).stride(2).build(); - auto x = Var(at::CPU(at::kFloat).randn({2, 3, 5, 5, 5}), true); + auto x = torch::randn({2, 3, 5, 5, 5}, at::requires_grad()); auto y = model->forward({x})[0]; Variable s = y.sum(); @@ -113,14 +111,13 @@ TEST_CASE("containers") { } REQUIRE( - model->parameters()["weight"].grad().numel() == - 3 * 2 * 3 * 3 * 3); + model->parameters()["weight"].grad().numel() == 3 * 2 * 3 * 3 * 3); } } SECTION("linear") { SECTION("basic1") { auto model = Linear(5, 2).build(); - auto x = Var(at::CPU(at::kFloat).randn({10, 5}), true); + auto x = torch::randn({10, 5}, at::requires_grad()); auto y = model->forward({x})[0]; Variable s = y.sum(); @@ -140,7 +137,7 @@ TEST_CASE("containers") { auto l2 = model->add(Linear(3, 5).build(), "l2"); auto l3 = model->add(Linear(5, 100).build(), "l3"); - auto x = Var(at::CPU(at::kFloat).randn({1000, 10})); + auto x = torch::randn({1000, 10}, at::requires_grad()); x = l1->forward({x})[0].clamp_min(0); x = l2->forward({x})[0].clamp_min(0); x = l3->forward({x})[0].clamp_min(0); @@ -158,7 +155,7 @@ TEST_CASE("containers") { auto model = Embedding(dict_size, 2).build(); // Cannot get gradients to change indices (input) - only for embedding // params - auto x = Var(at::CPU(at::kLong).tensor({10}).fill_(dict_size - 1), false); + auto x = torch::full({10}, dict_size - 1, at::kLong); auto y = model->forward({x})[0]; Variable s = y.sum(); @@ -168,13 +165,12 @@ TEST_CASE("containers") { REQUIRE(y.size(0) == 10); REQUIRE(y.size(1) == 2); - REQUIRE( - model->parameters()["table"].grad().numel() == 2 * dict_size); + REQUIRE(model->parameters()["table"].grad().numel() == 2 * dict_size); } SECTION("list") { auto model = Embedding(6, 4).build(); - auto x = Var(at::CPU(at::kLong).tensor({2, 3}).fill_(5), false); + auto x = torch::full({2, 3}, 5, at::kLong); auto y = model->forward({x})[0]; Variable s = y.sum(); @@ -188,7 +184,7 @@ TEST_CASE("containers") { SECTION("dropout") { auto dropout = Dropout(0.5).build(); - Variable x = Var(at::CPU(at::kFloat).ones(100)); + Variable x = torch::ones(100, at::requires_grad()); Variable y = dropout->forward({x})[0]; y.backward(); @@ -232,10 +228,10 @@ TEST_CASE("containers") { return input; }).build(); // clang-format on - auto output = functional->forward({Var(at::CPU(at::kFloat).ones(5))}); + auto output = functional->forward({torch::ones(5, at::requires_grad())}); REQUIRE(was_called); REQUIRE(output.size() == 1); - REQUIRE(output.front().equal(Var(at::CPU(at::kFloat).ones(5)))); + REQUIRE(output.front().equal(torch::ones(5, at::requires_grad()))); } } @@ -243,7 +239,7 @@ TEST_CASE("containers_cuda", "[cuda]") { SECTION("1") { auto model = Linear(5, 2).build(); model->cuda(); - auto x = Var(at::CUDA(at::kFloat).randn({10, 5}), true); + auto x = torch::randn({10, 5}, at::device(at::kCUDA).requires_grad()); auto y = model->forward({x})[0]; Variable s = y.sum(); @@ -260,7 +256,7 @@ TEST_CASE("containers_cuda", "[cuda]") { auto model = Linear(5, 2).build(); model->cuda(); model->cpu(); - auto x = Var(at::CPU(at::kFloat).randn({10, 5}), true); + auto x = torch::randn({10, 5}, at::requires_grad()); auto y = model->forward({x})[0]; Variable s = y.sum(); diff --git a/test/cpp/api/cursor.cpp b/test/cpp/api/cursor.cpp index 9a4a902bf0c9..06a7d3af47b8 100644 --- a/test/cpp/api/cursor.cpp +++ b/test/cpp/api/cursor.cpp @@ -1,7 +1,9 @@ #include +#include #include #include +#include #include #include @@ -18,10 +20,8 @@ using Catch::StartsWith; struct TestModule : public torch::nn::Module { TestModule(int64_t size) { - tensor1 = - register_parameter("tensor1", at::randn(at::CPU(at::kFloat), {size})); - tensor2 = - register_parameter("tensor2", at::randn(at::CPU(at::kFloat), {size})); + tensor1 = register_parameter("tensor1", torch::randn({size})); + tensor2 = register_parameter("tensor2", torch::randn({size})); } autograd::Variable tensor1; diff --git a/test/cpp/api/integration.cpp b/test/cpp/api/integration.cpp index e9ed110b88db..98d0feec1de6 100644 --- a/test/cpp/api/integration.cpp +++ b/test/cpp/api/integration.cpp @@ -1,5 +1,6 @@ #include +#include #include #include #include @@ -34,12 +35,12 @@ class CartPole { double x_threshold = 2.4; int steps_beyond_done = -1; - at::Tensor state; + Variable state; double reward; bool done; int step_ = 0; - at::Tensor getState() { + Variable getState() { return state; } @@ -52,7 +53,7 @@ class CartPole { } void reset() { - state = at::CPU(at::kFloat).tensor({4}).uniform_(-0.05, 0.05); + state = torch::empty({4}).uniform_(-0.05, 0.05); steps_beyond_done = -1; step_ = 0; } @@ -80,10 +81,10 @@ class CartPole { x_dot = x_dot + tau * xacc; theta = theta + tau * theta_dot; theta_dot = theta_dot + tau * thetaacc; - state[0] = x; - state[1] = x_dot; - state[2] = theta; - state[3] = theta_dot; + state.data()[0] = x; + state.data()[1] = x_dot; + state.data()[2] = theta; + state.data()[3] = theta_dot; done = x < -x_threshold || x > x_threshold || theta < -theta_threshold_radians || theta > theta_threshold_radians || step_ > 200; @@ -152,8 +153,7 @@ bool test_mnist( int image_rows = rd.read_int(); int image_cols = rd.read_int(); - auto data = - at::CPU(at::kFloat).tensor({image_count, 1, image_rows, image_cols}); + auto data = torch::empty({image_count, 1, image_rows, image_cols}); auto a_data = data.accessor(); for (int c = 0; c < image_count; c++) { @@ -172,7 +172,7 @@ bool test_mnist( /* int label_magic = */ rd.read_int(); int label_count = rd.read_int(); - auto data = at::CPU(at::kLong).tensor({label_count}); + auto data = torch::empty({label_count}, at::kLong); auto a_data = data.accessor(); for (int i = 0; i < label_count; ++i) { @@ -200,17 +200,20 @@ bool test_mnist( } std::shuffle(shuffled_inds.begin(), shuffled_inds.end(), generator); - auto inp = (useGPU ? at::CUDA : at::CPU)(at::kFloat) - .tensor({batch_size, 1, trdata.size(2), trdata.size(3)}); - auto lab = (useGPU ? at::CUDA : at::CPU)(at::kLong).tensor({batch_size}); + const auto backend = useGPU ? at::kCUDA : at::kCPU; + auto inp = + torch::empty({batch_size, 1, trdata.size(2), trdata.size(3)}, backend); + auto lab = torch::empty({batch_size}, at::device(backend).dtype(at::kLong)); for (auto p = 0U; p < shuffled_inds.size() - batch_size; p++) { inp[p % batch_size] = trdata[shuffled_inds[p]]; lab[p % batch_size] = trlabel[shuffled_inds[p]]; if (p % batch_size != batch_size - 1) continue; - Variable x = forward_op(Var(inp)); - Variable y = Var(lab, false); + inp.set_requires_grad(true); + Variable x = forward_op(inp); + inp.set_requires_grad(false); + Variable y = lab; Variable loss = at::nll_loss(x, y); optim->zero_grad(); @@ -220,8 +223,8 @@ bool test_mnist( } NoGradGuard guard; - auto result = std::get<1>(forward_op(Var(tedata, false)).max(1)); - Variable correct = (result == Var(telabel)).toType(at::kFloat); + auto result = std::get<1>(forward_op(tedata).max(1)); + Variable correct = (result == telabel).toType(at::kFloat); std::cout << "Num correct: " << correct.data().sum().toCFloat() << " out of" << telabel.size(0) << std::endl; return correct.data().sum().toCFloat() > telabel.size(0) * 0.8; @@ -251,7 +254,7 @@ TEST_CASE("integration") { auto selectAction = [&](at::Tensor state) { // Only work on single state right now, change index to gather for batch - auto out = forward({Var(state, false)}); + auto out = forward({state}); auto probs = Variable(std::get<0>(out)); auto value = Variable(std::get<1>(out)); auto action = probs.data().multinomial(1)[0].toCInt(); @@ -281,10 +284,8 @@ TEST_CASE("integration") { for (auto i = 0U; i < saved_log_probs.size(); i++) { auto r = rewards[i] - saved_values[i].toCFloat(); policy_loss.push_back(-r * saved_log_probs[i]); - value_loss.push_back(at::smooth_l1_loss( - saved_values[i], - Var(at::CPU(at::kFloat).scalarTensor(at::Scalar(rewards[i])), - false))); + value_loss.push_back( + at::smooth_l1_loss(saved_values[i], torch::ones({1}) * rewards[i])); } auto loss = at::stack(policy_loss).sum() + at::stack(value_loss).sum(); diff --git a/test/cpp/api/misc.cpp b/test/cpp/api/misc.cpp index 0f4fa33ddf3e..91ebe08c278a 100644 --- a/test/cpp/api/misc.cpp +++ b/test/cpp/api/misc.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -22,7 +23,7 @@ TEST_CASE("misc") { SECTION("no_grad") { NoGradGuard guard; auto model = Linear(5, 2).build(); - auto x = Var(at::CPU(at::kFloat).randn({10, 5}), true); + auto x = torch::randn({10, 5}, at::requires_grad()); auto y = model->forward({x})[0]; Variable s = y.sum(); @@ -33,9 +34,9 @@ TEST_CASE("misc") { SECTION("CPU random seed") { int size = 100; torch::manual_seed(7); - auto x1 = Var(at::CPU(at::kFloat).randn({size})); + auto x1 = torch::randn({size}); torch::manual_seed(7); - auto x2 = Var(at::CPU(at::kFloat).randn({size})); + auto x2 = torch::randn({size}); auto l_inf = (x1.data() - x2.data()).abs().max().toCFloat(); REQUIRE(l_inf < 1e-10); @@ -46,9 +47,9 @@ TEST_CASE("misc_cuda", "[cuda]") { SECTION("CUDA random seed") { int size = 100; torch::manual_seed(7); - auto x1 = Var(at::CUDA(at::kFloat).randn({size})); + auto x1 = torch::randn({size}, at::kCUDA); torch::manual_seed(7); - auto x2 = Var(at::CUDA(at::kFloat).randn({size})); + auto x2 = torch::randn({size}, at::kCUDA); auto l_inf = (x1.data() - x2.data()).abs().max().toCFloat(); REQUIRE(l_inf < 1e-10); @@ -56,10 +57,8 @@ TEST_CASE("misc_cuda", "[cuda]") { } TEST_CASE("autograd") { - auto x = autograd::make_variable( - at::randn(at::CPU(at::kFloat), {3, 3}), /*requires_grad=*/true); - auto y = autograd::make_variable( - at::randn(at::CPU(at::kFloat), {3, 3}), /*requires_grad=*/false); + auto x = torch::randn({3, 3}, at::requires_grad()); + auto y = torch::randn({3, 3}); auto z = x * y; SECTION("derivatives of zero-dim tensors") { z.sum().backward(); @@ -70,8 +69,7 @@ TEST_CASE("autograd") { REQUIRE(x.grad().allclose(y)); } SECTION("custom gradient inputs") { - z.sum().backward( - autograd::make_variable(at::ones(at::CPU(at::kFloat), {}) * 2)); + z.sum().backward(torch::ones({}) * 2); REQUIRE(x.grad().allclose(y * 2)); } // Assume everything else is safe from PyTorch tests. diff --git a/test/cpp/api/module.cpp b/test/cpp/api/module.cpp index 352efc8ff836..aca13ecfea45 100644 --- a/test/cpp/api/module.cpp +++ b/test/cpp/api/module.cpp @@ -35,7 +35,7 @@ TEST_CASE("module/training-mode") { TEST_CASE("module/zero-grad") { auto module = Linear(3, 4).build(); - auto weight = Var(at::ones(at::CPU(at::kFloat), {8, 3})); + auto weight = torch::ones({8, 3}, at::requires_grad()); auto loss = module->forward({weight}).front().sum(); loss.backward(); for (auto& parameter : module->parameters()) { @@ -154,8 +154,7 @@ TEST_CASE("module/clone") { SECTION("Cloning preserves external references") { struct TestModule : public CloneableModule { void reset() override { - weight = - register_parameter("weight", at::ones(at::CPU(at::kFloat), {4, 4})); + weight = register_parameter("weight", torch::ones({4, 4})); } Variable weight; }; @@ -176,8 +175,7 @@ TEST_CASE("module/clone") { SECTION("Cloning copies the values of variables of submodules") { struct TestModule : public CloneableModule { void reset() override { - weight = - register_parameter("weight", at::ones(at::CPU(at::kFloat), {4, 4})); + weight = register_parameter("weight", torch::ones({4, 4})); } Variable weight; @@ -208,9 +206,9 @@ TEST_CASE("module/clone") { TEST_CASE("module/parameters") { struct TestModule : Module { TestModule() { - a = register_parameter("a", at::zeros(at::CPU(at::kFloat), {2, 2})); - b = register_parameter("b", at::ones(at::CPU(at::kFloat), {2, 2})); - c = register_parameter("c", at::ones(at::CPU(at::kFloat), {2, 2}) * 2); + a = register_parameter("a", torch::zeros({2, 2})); + b = register_parameter("b", torch::ones({2, 2})); + c = register_parameter("c", torch::ones({2, 2}) * 2); } Variable a, b, c; diff --git a/test/cpp/api/optim.cpp b/test/cpp/api/optim.cpp index 5f5479c4a672..256bdc5a609b 100644 --- a/test/cpp/api/optim.cpp +++ b/test/cpp/api/optim.cpp @@ -1,5 +1,6 @@ #include +#include #include #include #include @@ -17,8 +18,8 @@ bool test_optimizer_xor(Optimizer optim, std::shared_ptr model) { int epoch = 0; while (running_loss > 0.1) { int64_t bs = 4; - auto inp = at::CPU(at::kFloat).tensor({bs, 2}); - auto lab = at::CPU(at::kFloat).tensor({bs}); + auto inp = torch::empty({bs, 2}); + auto lab = torch::empty({bs}); for (size_t i = 0; i < bs; i++) { const int64_t a = std::rand() % 2; const int64_t b = std::rand() % 2; @@ -27,12 +28,12 @@ bool test_optimizer_xor(Optimizer optim, std::shared_ptr model) { inp[i][1] = b; lab[i] = c; } + inp.set_requires_grad(true); // forward - auto y = Var(lab, false); std::function closure = [&]() -> at::Scalar { optim->zero_grad(); - auto x = model->forward(Var(inp)); - Variable loss = at::binary_cross_entropy(x, y); + auto x = model->forward(inp); + Variable loss = at::binary_cross_entropy(x, lab); loss.backward(); return at::Scalar(loss.data()); }; diff --git a/test/cpp/api/rnn.cpp b/test/cpp/api/rnn.cpp index 526b93191726..9a43108c68c5 100644 --- a/test/cpp/api/rnn.cpp +++ b/test/cpp/api/rnn.cpp @@ -37,17 +37,13 @@ bool test_RNN_xor(Func&& model_maker, bool cuda = false) { while (running_loss > 1e-2) { auto bs = 16U; auto nlen = 5U; - auto inp = - at::CPU(at::kFloat).rand({nlen, bs, 1}).round().toType(at::kFloat); + + const auto backend = cuda ? at::kCUDA : at::kCPU; + auto inp = at::rand({nlen, bs, 1}, backend).round().toType(at::kFloat); auto lab = inp.sum(0); - if (cuda) { - inp = inp.toBackend(at::kCUDA); - lab = lab.toBackend(at::kCUDA); - } - - auto x = Var(inp); - auto y = Var(lab, false); + auto x = autograd::make_variable(inp, /*requires_grad=*/true); + auto y = autograd::make_variable(lab); x = forward_op(x); Variable loss = at::mse_loss(x, y); @@ -90,7 +86,7 @@ TEST_CASE("rnn") { SECTION("lstm") { SECTION("sizes") { auto model = LSTM(128, 64).layers(3).dropout(0.2).build(); - Variable x = Var(at::CPU(at::kFloat).randn({10, 16, 128})); + auto x = torch::randn({10, 16, 128}, at::requires_grad()); auto tup = model->forward({x}); auto y = x.mean(); @@ -118,7 +114,7 @@ TEST_CASE("rnn") { } } - Variable x = Var(at::CPU(at::kFloat).tensor({3, 4, 2})); + auto x = torch::empty({3, 4, 2}, at::requires_grad()); float size = x.data().numel(); auto p = static_cast(x.data().storage()->data()); for (size_t i = 0; i < size; i++) { @@ -193,7 +189,7 @@ TEST_CASE("rnn_cuda", "[cuda]") { SECTION("sizes") { auto model = LSTM(128, 64).layers(3).dropout(0.2).build(); model->cuda(); - Variable x = Var(at::CUDA(at::kFloat).randn({10, 16, 128})); + auto x = torch::randn({10, 16, 128}, at::requires_grad().device(at::kCUDA)); auto tup = model->forward({x}); auto y = x.mean(); diff --git a/test/cpp/api/sequential.cpp b/test/cpp/api/sequential.cpp index ef923da7c0c0..b74efbf8fba4 100644 --- a/test/cpp/api/sequential.cpp +++ b/test/cpp/api/sequential.cpp @@ -1,5 +1,6 @@ #include +#include #include #include #include @@ -116,8 +117,7 @@ TEST_CASE("sequential") { }; Sequential sequential(M{}); - auto variable = - autograd::make_variable(at::CPU(at::kFloat).ones({3, 3}), true); + auto variable = torch::ones({3, 3}, at::requires_grad()); REQUIRE(sequential.forward(variable).equal(variable)); } } @@ -126,8 +126,9 @@ TEST_CASE("sequential") { Sequential sequential( Linear(10, 3).build(), Linear(3, 5).build(), Linear(5, 100).build()); - auto x = Var(at::CPU(at::kFloat).randn({1000, 10})); - auto y = sequential.forward>(std::vector{x}).front(); + auto x = torch::randn({1000, 10}, at::requires_grad()); + auto y = sequential.forward>(std::vector{x}) + .front(); REQUIRE(y.ndimension() == 2); REQUIRE(y.size(0) == 1000); REQUIRE(y.size(1) == 100); diff --git a/test/cpp/api/serialization.cpp b/test/cpp/api/serialization.cpp index d99a98226d44..6c9d7ac32471 100644 --- a/test/cpp/api/serialization.cpp +++ b/test/cpp/api/serialization.cpp @@ -1,5 +1,6 @@ #include +#include #include #include #include @@ -30,7 +31,7 @@ TEST_CASE("serialization") { REQUIRE(!x.defined()); - auto y = at::CPU(at::kFloat).randn({5}); + auto y = at::randn({5}); std::stringstream ss; save(ss, &x); @@ -51,8 +52,8 @@ TEST_CASE("serialization") { continue; } - auto x = - at::getType(at::kCPU, static_cast(i)).ones({5, 5}); + auto x = at::ones( + {5, 5}, at::getType(at::kCPU, static_cast(i))); auto y = at::Tensor(); std::stringstream ss; @@ -70,7 +71,7 @@ TEST_CASE("serialization") { } SECTION("binary") { - auto x = at::CPU(at::kFloat).randn({5, 5}); + auto x = at::randn({5, 5}); auto y = at::Tensor(); std::stringstream ss; @@ -88,7 +89,7 @@ TEST_CASE("serialization") { REQUIRE(x.allclose(y)); } SECTION("portable_binary") { - auto x = at::CPU(at::kFloat).randn({5, 5}); + auto x = at::randn({5, 5}); auto y = at::Tensor(); std::stringstream ss; @@ -107,7 +108,7 @@ TEST_CASE("serialization") { } SECTION("resized") { - auto x = at::CPU(at::kFloat).randn({11, 5}); + auto x = at::randn({11, 5}); x.resize_({5, 5}); auto y = at::Tensor(); @@ -126,7 +127,7 @@ TEST_CASE("serialization") { REQUIRE(x.allclose(y)); } SECTION("sliced") { - auto x = at::CPU(at::kFloat).randn({11, 5}); + auto x = at::randn({11, 5}); x = x.slice(0, 1, 3); auto y = at::Tensor(); @@ -146,7 +147,7 @@ TEST_CASE("serialization") { } SECTION("noncontig") { - auto x = at::CPU(at::kFloat).randn({11, 5}); + auto x = at::randn({11, 5}); x = x.slice(1, 1, 4); auto y = at::Tensor(); @@ -168,8 +169,8 @@ TEST_CASE("serialization") { SECTION("xor") { // We better be able to save and load a XOR model! auto getLoss = [](std::shared_ptr model, uint32_t bs) { - auto inp = at::CPU(at::kFloat).tensor({bs, 2}); - auto lab = at::CPU(at::kFloat).tensor({bs}); + auto inp = torch::empty({bs, 2}); + auto lab = torch::empty({bs}); for (auto i = 0U; i < bs; i++) { auto a = std::rand() % 2; auto b = std::rand() % 2; @@ -180,9 +181,8 @@ TEST_CASE("serialization") { } // forward - auto x = model->forward(Var(inp)); - auto y = Var(lab, false); - return at::binary_cross_entropy(x, y); + auto x = model->forward(inp); + return at::binary_cross_entropy(x, lab); }; auto model = xor_model(); @@ -231,7 +231,7 @@ TEST_CASE("serialization") { auto optim3 = SGD(model3, 1e-1).momentum(0.9).make(); auto optim3_2 = SGD(model3, 1e-1).momentum(0.9).make(); - auto x = Var(at::CPU(at::kFloat).ones({10, 5}), true); + auto x = torch::ones({10, 5}, at::requires_grad()); auto step = [&](Optimizer optim, std::shared_ptr model) { optim->zero_grad(); @@ -271,8 +271,8 @@ TEST_CASE("serialization_cuda", "[cuda]") { SECTION("xor") { // We better be able to save and load a XOR model! auto getLoss = [](std::shared_ptr model, uint32_t bs) { - auto inp = at::CPU(at::kFloat).tensor({bs, 2}); - auto lab = at::CPU(at::kFloat).tensor({bs}); + auto inp = torch::empty({bs, 2}); + auto lab = torch::empty({bs}); for (auto i = 0U; i < bs; i++) { auto a = std::rand() % 2; auto b = std::rand() % 2; @@ -283,9 +283,8 @@ TEST_CASE("serialization_cuda", "[cuda]") { } // forward - auto x = model->forward(Var(inp)); - auto y = Var(lab, false); - return at::binary_cross_entropy(x, y); + auto x = model->forward(inp); + return at::binary_cross_entropy(x, lab); }; auto model = xor_model(); diff --git a/test/cpp/api/tensor.cpp b/test/cpp/api/tensor.cpp new file mode 100644 index 000000000000..13742751fd8f --- /dev/null +++ b/test/cpp/api/tensor.cpp @@ -0,0 +1,23 @@ +#include + +#include + +TEST_CASE("tensor/device-placement") { + SECTION("DeviceGuard") { + // SECTION("On index zero by default") { + // auto tensor = at::ones({3, 3}, at::kCUDA); + // REQUIRE(tensor.get_device() == 0); + // } + + // // right hand side is TensorOptions + // torch::OptionGuard guard = torch::device(torch::kCUDA, 1); + // // convenience wrapper over OptionGuard + // torch::DeviceGuard guard(torch::kCUDA, 1); + // /// default device is CUDA + // torch::DeviceGuard guard(1); + + // note that this is separate from DeviceGuard. DeviceGuard should move into the + // detail namespace and do the actual thing. OptionGuard just modifies a + // global singleton of option defaults. It operates at a higher level. + } +} diff --git a/test/cpp/api/tensor_options.cpp b/test/cpp/api/tensor_options.cpp new file mode 100644 index 000000000000..ffcbf0ff6d1b --- /dev/null +++ b/test/cpp/api/tensor_options.cpp @@ -0,0 +1,86 @@ +#include "catch.hpp" + +#include +#include +#include + +using namespace at; + +// A macro so we don't lose location information when an assertion fails. +#define REQUIRE_OPTIONS(device_, index_, type_, layout_) \ + REQUIRE(options.device().type() == Device((device_), (index_)).type()); \ + REQUIRE(options.device().index() == Device((device_), (index_)).index()); \ + REQUIRE(options.dtype() == (type_)); \ + REQUIRE(options.layout() == (layout_)) + +TEST_CASE("TensorOptions/DefaultsToTheRightValues") { + TensorOptions options; + REQUIRE_OPTIONS(kCPU, -1, kFloat, kStrided); +} + +TEST_CASE("TensorOptions/ReturnsTheCorrectType") { + auto options = TensorOptions().device(kCPU).dtype(kInt).layout(kSparse); + REQUIRE(options.type() == getType(kSparseCPU, kInt)); +} + +TEST_CASE("TensorOptions/UtilityFunctionsReturnTheRightTensorOptions") { + auto options = dtype(kInt); + REQUIRE_OPTIONS(kCPU, -1, kInt, kStrided); + + options = layout(kSparse); + REQUIRE_OPTIONS(kCPU, -1, kFloat, kSparse); + + options = device({kCUDA, 1}); + REQUIRE_OPTIONS(kCUDA, 1, kFloat, kStrided); + + options = device_index(1); + REQUIRE_OPTIONS(kCUDA, 1, kFloat, kStrided); + + options = dtype(kByte).layout(kSparse).device({kCUDA, 2}).device_index(3); + REQUIRE_OPTIONS(kCUDA, 3, kByte, kSparse); +} + +TEST_CASE("TensorOptions/ConstructsWellFromCPUTypes") { + auto options = TensorOptions(); + REQUIRE_OPTIONS(kCPU, -1, kFloat, kStrided); + + options = TensorOptions({kCPU, 0}); + REQUIRE_OPTIONS(kCPU, 0, kFloat, kStrided); + + options = TensorOptions(kInt); + REQUIRE_OPTIONS(kCPU, -1, kInt, kStrided); + + options = TensorOptions(getType(kSparseCPU, kFloat)); + REQUIRE_OPTIONS(kCPU, -1, kFloat, kSparse); + + options = TensorOptions(getType(kSparseCPU, kByte)); + REQUIRE_OPTIONS(kCPU, -1, kByte, kSparse); +} + +TEST_CASE("TensorOptions/ConstructsWellFromCPUTensors") { + auto options = TensorOptions(empty(5, kDouble)); + REQUIRE_OPTIONS(kCPU, -1, kDouble, kStrided); + + options = TensorOptions(empty(5, getType(kSparseCPU, kByte))); + REQUIRE_OPTIONS(kCPU, -1, kByte, kSparse); +} + +TEST_CASE("Device/ParsesCorrectlyFromString") { + Device device("cpu:0"); + REQUIRE(device == Device(kCPU, 0)); + + device = Device("cpu"); + REQUIRE(device == Device(kCPU)); + + device = Device("cuda:123"); + REQUIRE(device == Device(kCUDA, 123)); + + device = Device("cuda"); + REQUIRE(device == Device(kCUDA)); + + std::vector badnesses = { + "", "cud:1", "cuda:", "cpu::1", ":1", "3", "tpu:4", "??"}; + for (const auto& badness : badnesses) { + REQUIRE_THROWS(Device(badness)); + } +} diff --git a/test/cpp/api/tensor_options_cuda.cpp b/test/cpp/api/tensor_options_cuda.cpp new file mode 100644 index 000000000000..18a79777e77d --- /dev/null +++ b/test/cpp/api/tensor_options_cuda.cpp @@ -0,0 +1,61 @@ +#include "catch.hpp" + +#include +#include +#include + +#include + +using namespace at; + +// A macro so we don't lose location information when an assertion fails. +#define REQUIRE_OPTIONS(device_, index_, type_, layout_) \ + REQUIRE(options.device().type() == Device((device_), (index_)).type()); \ + REQUIRE(options.device().index() == Device((device_), (index_)).index()); \ + REQUIRE(options.dtype() == (type_)); \ + REQUIRE(options.layout() == (layout_)) + +TEST_CASE("TensorOptions/ConstructsWellFromCUDATypes", "[cuda]") { + auto options = TensorOptions(CUDA(kFloat)); + REQUIRE_OPTIONS(kCUDA, -1, kFloat, kStrided); + + options = TensorOptions(CUDA(kInt)); + REQUIRE_OPTIONS(kCUDA, -1, kInt, kStrided); + + options = TensorOptions(getType(kSparseCUDA, kFloat)); + REQUIRE_OPTIONS(kCUDA, -1, kFloat, kSparse); + + options = TensorOptions(getType(kSparseCUDA, kByte)); + REQUIRE_OPTIONS(kCUDA, -1, kByte, kSparse); + + options = TensorOptions(CUDA(kFloat), /*device=*/5); + REQUIRE_OPTIONS(kCUDA, 5, kFloat, kStrided); + + options = TensorOptions(getType(kSparseCUDA, kFloat), /*device=*/5); + REQUIRE_OPTIONS(kCUDA, 5, kFloat, kSparse); +} + +TEST_CASE("TensorOptions/ConstructsWellFromCUDATensors", "[cuda]") { + auto options = TensorOptions(empty(5, device(kCUDA).dtype(kDouble))); + REQUIRE_OPTIONS(kCUDA, 0, kDouble, kStrided); + + options = TensorOptions(empty(5, getType(kSparseCUDA, kByte))); + REQUIRE_OPTIONS(kCUDA, 0, kByte, kSparse); + + if (at::globalContext().getNumGPUs() > 1) { + Tensor tensor; + { + DeviceGuard guard(1); + tensor = empty(5, device(kCUDA)); + } + options = TensorOptions(tensor); + REQUIRE_OPTIONS(kCUDA, 1, kFloat, kStrided); + + { + DeviceGuard guard(1); + tensor = empty(5, device(kCUDA).layout(kSparse)); + } + options = TensorOptions(tensor); + REQUIRE_OPTIONS(kCUDA, 1, kFloat, kSparse); + } +} diff --git a/test/cpp_extensions/doubler.h b/test/cpp_extensions/doubler.h index 7b4b105b13a3..2b22dca1284c 100644 --- a/test/cpp_extensions/doubler.h +++ b/test/cpp_extensions/doubler.h @@ -2,7 +2,7 @@ struct Doubler { Doubler(int A, int B) { - tensor_ = at::ones(torch::CPU(at::kDouble), {A, B}); + tensor_ = at::ones({A, B}, torch::CPU(at::kDouble)); torch::set_requires_grad(tensor_, true); } at::Tensor forward() { diff --git a/test/cpp_extensions/extension.cpp b/test/cpp_extensions/extension.cpp index 9994e1729363..8e7939729691 100644 --- a/test/cpp_extensions/extension.cpp +++ b/test/cpp_extensions/extension.cpp @@ -6,7 +6,7 @@ at::Tensor sigmoid_add(at::Tensor x, at::Tensor y) { struct MatrixMultiplier { MatrixMultiplier(int A, int B) { - tensor_ = at::ones(torch::CPU(at::kDouble), {A, B}); + tensor_ = at::ones({A, B}, torch::CPU(at::kDouble)); torch::set_requires_grad(tensor_, true); } at::Tensor forward(at::Tensor weights) { diff --git a/test/onnx/test_operators.py b/test/onnx/test_operators.py index 35b4912da119..939f4b5a6d04 100644 --- a/test/onnx/test_operators.py +++ b/test/onnx/test_operators.py @@ -189,11 +189,6 @@ class TestOperators(TestCase): y = nn.Parameter(torch.Tensor([[1, 2], [3, 4]]), requires_grad=True) self.assertONNX(lambda x, y: -torch.sigmoid(torch.tanh(x * (x + y))), x, params=(y, )) - def test_non_float_params(self): - x = Variable(torch.LongTensor([[1, 2], [3, 4]]), requires_grad=True) - y = nn.Parameter(torch.LongTensor([[1, 2], [3, 4]]), requires_grad=True) - self.assertONNX(lambda x, y: x * (x + y), x, params=(y, )) - def test_symbolic_mismatch(self): class MyFun(Function): @staticmethod @@ -313,28 +308,28 @@ class TestOperators(TestCase): self.assertONNX(lambda x: torch.sqrt(x), x) def test_equal(self): - x = Variable(torch.randn(3, 4).int(), requires_grad=True) - y = Variable(torch.randn(3, 4).int(), requires_grad=True) + x = Variable(torch.randn(3, 4).int(), requires_grad=False) + y = Variable(torch.randn(3, 4).int(), requires_grad=False) self.assertONNX(lambda x, y: x == y, (x, y)) def test_lt(self): - x = Variable(torch.randn(3, 4).int(), requires_grad=True) - y = Variable(torch.randn(3, 4).int(), requires_grad=True) + x = Variable(torch.randn(3, 4).int(), requires_grad=False) + y = Variable(torch.randn(3, 4).int(), requires_grad=False) self.assertONNX(lambda x, y: x < y, (x, y)) def test_gt(self): - x = Variable(torch.randn(3, 4).int(), requires_grad=True) - y = Variable(torch.randn(3, 4).int(), requires_grad=True) + x = Variable(torch.randn(3, 4).int(), requires_grad=False) + y = Variable(torch.randn(3, 4).int(), requires_grad=False) self.assertONNX(lambda x, y: x > y, (x, y)) def test_le(self): - x = Variable(torch.randn(3, 4).int(), requires_grad=True) - y = Variable(torch.randn(3, 4).int(), requires_grad=True) + x = Variable(torch.randn(3, 4).int(), requires_grad=False) + y = Variable(torch.randn(3, 4).int(), requires_grad=False) self.assertONNX(lambda x, y: x <= y, (x, y)) def test_ge(self): - x = Variable(torch.randn(3, 4).int(), requires_grad=True) - y = Variable(torch.randn(3, 4).int(), requires_grad=True) + x = Variable(torch.randn(3, 4).int(), requires_grad=False) + y = Variable(torch.randn(3, 4).int(), requires_grad=False) self.assertONNX(lambda x, y: x >= y, (x, y)) def test_exp(self): diff --git a/test/test_cuda.py b/test/test_cuda.py index 2ea1a7d87ffe..535853026531 100644 --- a/test/test_cuda.py +++ b/test/test_cuda.py @@ -776,7 +776,6 @@ class TestCuda(TestCase): w = torch.randn(5, 5).cuda() self.assertEqual(w.get_device(), 1) self.assertEqual(y.cuda().get_device(), 1) - self.assertEqual(y.cuda(-1).get_device(), 1) z = z.cuda() self.assertEqual(z.get_device(), 0) diff --git a/test/test_sparse.py b/test/test_sparse.py index 8c4098e80992..f17af08120a1 100644 --- a/test/test_sparse.py +++ b/test/test_sparse.py @@ -983,7 +983,7 @@ class TestSparse(TestCase): all_sparse_dtypes = [dtype for dtype in torch.testing.get_all_dtypes() if dtype != torch.float16] TestTorch._test_empty_full(self, all_sparse_dtypes, torch.sparse_coo, torch.device('cpu')) if torch.cuda.device_count() > 0: - TestTorch._test_empty_full(self, all_sparse_dtypes, torch.sparse_coo, -1) + TestTorch._test_empty_full(self, all_sparse_dtypes, torch.sparse_coo, None) TestTorch._test_empty_full(self, all_sparse_dtypes, torch.sparse_coo, torch.device('cuda:0')) def test_is_sparse(self): diff --git a/test/test_torch.py b/test/test_torch.py index 4a2b6f352b3b..a675ba0b3700 100644 --- a/test/test_torch.py +++ b/test/test_torch.py @@ -1810,6 +1810,7 @@ class TestTorch(TestCase): self.assertRaises(RuntimeError, lambda: torch.device('cpu', 1)) self.assertRaises(RuntimeError, lambda: torch.device('cuda:-1')) self.assertRaises(RuntimeError, lambda: torch.device('cuda', -1)) + self.assertRaises(RuntimeError, lambda: torch.device(-1)) self.assertRaises(TypeError, lambda: torch.device('other')) self.assertRaises(TypeError, lambda: torch.device('other:0')) @@ -1891,7 +1892,7 @@ class TestTorch(TestCase): self.assertIs(dtype, tensor.dtype) self.assertIs(layout, tensor.layout) self.assertEqual(tensor.requires_grad, requires_grad) - if tensor.is_cuda and device != -1: + if tensor.is_cuda and device is not None: self.assertEqual(device, tensor.device) if value is not None: fill = tensor.new(shape).fill_(value) @@ -1939,7 +1940,7 @@ class TestTorch(TestCase): def test_empty_full(self): self._test_empty_full(self, torch.testing.get_all_dtypes(), torch.strided, torch.device('cpu')) if torch.cuda.device_count() > 0: - self._test_empty_full(self, torch.testing.get_all_dtypes(), torch.strided, -1) + self._test_empty_full(self, torch.testing.get_all_dtypes(), torch.strided, None) self._test_empty_full(self, torch.testing.get_all_dtypes(), torch.strided, torch.device('cuda:0')) def test_dtype_out_match(self): diff --git a/tools/autograd/derivatives.yaml b/tools/autograd/derivatives.yaml index b6cccaee8c5d..a71732eb237c 100644 --- a/tools/autograd/derivatives.yaml +++ b/tools/autograd/derivatives.yaml @@ -279,7 +279,7 @@ self: grad - name: gather(Tensor self, int64_t dim, Tensor index) - self: at::zeros(grad.type(), self.sizes()).scatter_add_(dim, index, grad) + self: at::zeros(self.sizes(), grad.type()).scatter_add_(dim, index, grad) - name: ge_(Tensor self, Scalar other) self: zeros_like(self) @@ -336,7 +336,7 @@ value: grad.index_select(dim, index).sum() - name: index_select(Tensor self, int64_t dim, Tensor index) - self: at::zeros(grad.type(), self.sizes()).index_add_(dim, index, grad) + self: at::zeros(self.sizes(), grad.type()).index_add_(dim, index, grad) - name: inverse(Tensor self) self: -at::mm(output.t(), at::mm(grad, output.t())) @@ -480,14 +480,14 @@ self: zeros_like(grad) - name: normal(Tensor mean, double std, Generator generator) - mean: at::zeros(grad.type(), mean.sizes()) + mean: at::zeros(mean.sizes(), grad.type()) - name: normal(double mean, Tensor std, Generator generator) - std: at::zeros(grad.type(), std.sizes()) + std: at::zeros(std.sizes(), grad.type()) - name: normal(Tensor mean, Tensor std, Generator generator) - mean: at::zeros(grad.type(), mean.sizes()) - std: at::zeros(grad.type(), std.sizes()) + mean: at::zeros(mean.sizes(), grad.type()) + std: at::zeros(std.sizes(), grad.type()) - name: orgqr(Tensor self, Tensor input2) self: not_implemented("orgqr") diff --git a/tools/autograd/gen_autograd.py b/tools/autograd/gen_autograd.py index 760d26be98ba..be424aa4e77f 100644 --- a/tools/autograd/gen_autograd.py +++ b/tools/autograd/gen_autograd.py @@ -50,7 +50,10 @@ def load_aten_declarations(path): declarations = yaml.load(f, Loader=YamlLoader) # enrich declarations with additional information + selected_declarations = [] for declaration in declarations: + if declaration.get('deprecated'): + continue for arg in declaration['arguments']: simple_type = arg['type'] simple_type = simple_type.replace(' &', '').replace('const ', '') @@ -68,7 +71,9 @@ def load_aten_declarations(path): declaration['return_type'] = format_return_type(declaration['returns']) declaration['base_name'] = declaration['name'] - return declarations + selected_declarations.append(declaration) + + return selected_declarations def load_deprecated_signatures(aten_decls, deprecated_path): @@ -141,27 +146,33 @@ def gen_autograd(aten_path, out, autograd_dir): autograd_functions = load_derivatives( os.path.join(autograd_dir, 'derivatives.yaml'), aten_decls) + template_path = os.path.join(autograd_dir, 'templates') + # Generate VariableType.h/cpp from .gen_variable_type import gen_variable_type - gen_variable_type(out, aten_decls, os.path.join(autograd_dir, 'templates')) + gen_variable_type(out, aten_decls, template_path) # Generate Functions.h/cpp from .gen_autograd_functions import gen_autograd_functions gen_autograd_functions( - out, autograd_functions, os.path.join(autograd_dir, 'templates')) + out, autograd_functions, template_path) # Load deprecated signatures deprecated = load_deprecated_signatures( aten_decls, os.path.join(autograd_dir, 'deprecated.yaml')) - # Genereate Python bindings + # Generate Python bindings from . import gen_python_functions gen_python_functions.gen_py_variable_methods( - out, aten_decls + deprecated, os.path.join(autograd_dir, 'templates')) + out, aten_decls + deprecated, template_path) gen_python_functions.gen_py_torch_functions( - out, aten_decls + deprecated, os.path.join(autograd_dir, 'templates')) + out, aten_decls + deprecated, template_path) gen_python_functions.gen_py_nn_functions( - out, aten_decls, os.path.join(autograd_dir, 'templates')) + out, aten_decls, template_path) + + # Generate variable_factories.h + from .gen_variable_factories import gen_variable_factories + gen_variable_factories(out, aten_decls, template_path) def main(): diff --git a/tools/autograd/gen_python_functions.py b/tools/autograd/gen_python_functions.py index dcd5e472cd11..512eecea23be 100644 --- a/tools/autograd/gen_python_functions.py +++ b/tools/autograd/gen_python_functions.py @@ -82,7 +82,7 @@ PY_VARIABLE_CALL_DISPATCH = CodeTemplate("""\ ${dispatch_name}(${actuals})""") PY_VARIABLE_SET_REQUIRES_GRAD = CodeTemplate("""\ -set_requires_grad(${call_dispatch}, ${requires_grad})""") +${call_dispatch}.set_requires_grad(${requires_grad})""") PY_VARIABLE_WRAP = CodeTemplate("""\ return wrap(${call_dispatch});""") @@ -91,7 +91,6 @@ PY_VARIABLE_DISPATCH = CodeTemplate("""\ inline ${return_type} ${dispatch_name}(${formal_args}) { ${initialize_cuda} ${AutoNoGIL} - ${AutoGPU} return ${dispatch_call}(${dispatch_args}); } """) @@ -116,6 +115,14 @@ SUPPORTED_RETURN_TYPES = { 'Scalar', 'bool', 'int64_t', 'void*' } +TENSOR_OPTIONS = CodeTemplate("""\ +const auto options = TensorOptions() + .dtype(${dtype}) + .device(${device}) + .layout(${layout}.layout) + .requires_grad(${requires_grad}); +""") + def should_generate_python_binding(declaration): name = declaration['name'] @@ -238,25 +245,6 @@ def create_python_bindings(python_functions, has_self, is_module=False): 'ScalarType': 'scalartypeWithDefault', } - def first_tensor_arg(arguments): - for arg in arguments: - if arg['simple_type'] in {'Tensor', 'TensorList'}: - return arg['name'] - return None - - def auto_gpu(option, has_device_bind): - if option['auto_gpu']: - tensor_arg = first_tensor_arg(option['arguments']) - if tensor_arg is not None: - if not has_device_bind: - return 'AutoGPU auto_gpu({});'.format(tensor_arg) - else: # e.g. for ones_like, the default is the device of the tensor arg - device_to_use = '({}.type().is_cuda() ? {}.get_device() : -1)'.format(tensor_arg, tensor_arg) - return 'AutoGPU auto_gpu(device == -1 ? {} : device);'.format(device_to_use) - elif has_device_bind: - return 'AutoGPU auto_gpu(device);' - return '' - def emit_single_dispatch(declaration, out_idx, base_env): env = {} simple_return_type = declaration['return_type'].replace(' &', '') @@ -274,6 +262,8 @@ def create_python_bindings(python_functions, has_self, is_module=False): inputs = [arg for arg in declaration['arguments'] if not is_output(arg)] outputs = [arg for arg in declaration['arguments'] if is_output(arg)] + has_tensor_options = any(arg['simple_type'] == 'TensorOptions' for arg in declaration['arguments']) + def get_type_args(args): return [arg for arg in args if arg['simple_type'] == 'Type'] type_actual_args = get_type_args(declaration['arguments']) @@ -324,7 +314,7 @@ def create_python_bindings(python_functions, has_self, is_module=False): elif dispatch_type == 'Tensor &': dispatch_type = 'Tensor' elif dispatch_type == 'const Device &': - dispatch_type = 'int64_t' + dispatch_type = 'at::optional' formal = '{} {}'.format(dispatch_type, name) return expr, formal @@ -332,9 +322,10 @@ def create_python_bindings(python_functions, has_self, is_module=False): actuals.append(actual) formal_args.append(formal) - unpack = any(arg.get('python_default_init') for arg in inputs) + # We always want to unpack when we have TensorOptions. + unpack = any(arg.get('python_default_init') for arg in inputs) or has_tensor_options for arg in inputs: - if arg['simple_type'] == 'Type': + if arg['simple_type'] in ['Type', 'TensorOptions']: continue if has_self and arg['name'] == 'self': formal_args.append('Tensor & self') @@ -353,6 +344,7 @@ def create_python_bindings(python_functions, has_self, is_module=False): actuals.append('results[{}]'.format(i)) layout = None + parsed_type_args = None # type args go after the outputs to match the signature generation. arg_idx = arg_idx if out_idx is None else out_idx + 1 for arg in type_args: @@ -364,13 +356,15 @@ def create_python_bindings(python_functions, has_self, is_module=False): requires_grad = None python_binding_arguments = declaration.get('python_binding_arguments', []) if 'dtype' in (a['name'] for a in python_binding_arguments): - arg_idx += 1 # we already handled this in type_dispatched_args + if not has_tensor_options: + arg_idx += 1 if 'layout' in (a['name'] for a in python_binding_arguments): layout_idx, device_idx, requires_grad_idx = (arg_idx, arg_idx + 1, arg_idx + 2) else: device_idx, requires_grad_idx = (arg_idx, arg_idx + 1) + device = None for arg in python_binding_arguments: if arg['name'] == 'dtype' and arg['simple_type'] == 'Type': pass # already handled by type_dispatched_args @@ -382,14 +376,17 @@ def create_python_bindings(python_functions, has_self, is_module=False): if len(outputs) == 0: assert parsed_type_args assert layout - device_arg = parse_arg(arg, device_idx, True) - # add type, device formals and corresponding actuals. - # The type actual isthe ATen type mapped from (ScalarType, Layout, Device) - # The device actual is the corresponding AutoGPU index for the Device. - formal_args.append(parsed_type_args[1]) - formal_args.append(device_arg[1]) - actuals.append("torch::getType({}, {}, {}.type)".format(parsed_type_args[0], layout, device_arg[0])) - actuals.append('{}.deviceInt64()'.format(device_arg[0])) + device, device_type = parse_arg(arg, device_idx, True) + + if not has_tensor_options: + # add type, device formals and corresponding actuals. + # The type actual isthe ATen type mapped from (ScalarType, Layout, Device) + # The device actual is the corresponding AutoGPU index for the Device. + formal_args.append(parsed_type_args[1]) + formal_args.append(device_type) + actuals.append("torch::getType({}, {}, {})".format(parsed_type_args[0], layout, device)) + actuals.append('{}.index()'.format(device)) + has_device_bind = True elif arg['name'] == 'requires_grad' and arg['simple_type'] == 'bool': requires_grad = parse_arg(arg, requires_grad_idx)[0] @@ -398,28 +395,45 @@ def create_python_bindings(python_functions, has_self, is_module=False): "\"bool requires_grad\", \"ScalarType dtype\", \"Layout layout\", " "\"Device device\" are supported".format(arg))) + dtype = parsed_type_args[0] if parsed_type_args else None + if has_tensor_options and all([dtype, device, layout, requires_grad]): + body.append(TENSOR_OPTIONS.substitute({ + 'dtype': dtype, + 'layout': layout, + 'device': device, + 'requires_grad': requires_grad + })) + formal_args.append('const TensorOptions & options') + actuals.append('options') + env['unpack_args'] = [] env['formal_args'] = formal_args env['actuals'] = actuals - maybe_init_cuda = type_args[0]['name'] if type_args else None - env['initialize_cuda'] = 'maybe_initialize_cuda({});'.format(maybe_init_cuda) if maybe_init_cuda else [] + + if has_tensor_options: + env['initialize_cuda'] = 'maybe_initialize_cuda(options.type());' + else: + env['initialize_cuda'] = 'maybe_initialize_cuda({});'.format(type_args[0]['name']) if type_args else '' + if 'call_args' in declaration: env['dispatch_args'] = declaration['call_args'] else: env['dispatch_args'] = [arg['name'] for arg in declaration['arguments']] + if 'Tensor' in declaration['method_of']: env['dispatch_args'] = [arg for arg in env['dispatch_args'] if arg != 'self'] env['dispatch_call'] = 'self.{}'.format(declaration['name']) elif 'namespace' in declaration['method_of']: - env['dispatch_call'] = 'at::{}'.format(declaration['name']) + namespace = 'torch' if (has_tensor_options or declaration['name'].endswith('_like')) else 'at' + env['dispatch_call'] = '{}::{}'.format(namespace, declaration['name']) else: raise RuntimeError('could not dispatch, neither namespace function nor Tensor method') + env['AutoNoGIL'] = 'AutoNoGIL no_gil;' if not declaration['with_gil'] else '' - env['AutoGPU'] = auto_gpu(declaration, has_device_bind) env = nested_dict(env, nested_dict(base_env, declaration)) call_dispatch = PY_VARIABLE_CALL_DISPATCH.substitute(env) - if requires_grad: + if requires_grad and not has_tensor_options: call_dispatch = PY_VARIABLE_SET_REQUIRES_GRAD.substitute(env, call_dispatch=call_dispatch, requires_grad=requires_grad) body.append(PY_VARIABLE_WRAP.substitute(env, call_dispatch=call_dispatch)) @@ -450,6 +464,7 @@ def create_python_bindings(python_functions, has_self, is_module=False): python_binding_arguments = [] has_tensor_input_arg = False has_type_input_arg = False + has_options_arg = False for arg in declaration['arguments']: if arg.get('output', False): continue @@ -458,6 +473,8 @@ def create_python_bindings(python_functions, has_self, is_module=False): has_tensor_input_arg = True if arg['simple_type'] == 'Type': has_type_input_arg = True + elif arg['simple_type'] == 'TensorOptions': + has_options_arg = True if arg['name'] == 'requires_grad': raise ValueError("argument named requires_grad not supported") @@ -469,12 +486,13 @@ def create_python_bindings(python_functions, has_self, is_module=False): has_tensor_return = True is_like_function = name.endswith('_like') - is_typed_like_function = is_like_function and has_type_input_arg + is_like_function_with_options = is_like_function and has_options_arg is_factory_function = has_tensor_return and not has_tensor_input_arg is_factory_or_like_function = has_tensor_return and (not has_tensor_input_arg or is_like_function) - if is_factory_function and not has_type_input_arg: + if (is_factory_function and not has_type_input_arg) or has_options_arg: default_type = get_type_default(declaration) + py_default_dtype = 'self.type()' if is_like_function_with_options else None dtype_arg = { 'default': default_type, 'dynamic_type': 'Type', @@ -483,10 +501,11 @@ def create_python_bindings(python_functions, has_self, is_module=False): 'type': 'const Type &', 'simple_type': 'Type', 'is_type_dispatched': True, + 'python_default_init': py_default_dtype, } python_binding_arguments.append(dtype_arg) - if is_factory_function or is_typed_like_function: - py_default_layout = '*torch::getLayout(self.type().backend())' if is_typed_like_function else None + if is_factory_function or is_like_function_with_options: + py_default_layout = '*torch::getLayout(self.type().backend())' if is_like_function_with_options else None layout_arg = { 'default': 'torch.strided', 'dynamic_type': 'Layout', @@ -497,7 +516,7 @@ def create_python_bindings(python_functions, has_self, is_module=False): 'python_default_init': py_default_layout, } python_binding_arguments.append(layout_arg) - py_default_device = 'torch::utils::getDevice(self)' if is_typed_like_function else None + py_default_device = 'self.device()' if is_like_function_with_options else None device_arg = { 'default': 'None', 'default_init': 'None', @@ -606,7 +625,7 @@ def group_declarations(declarations): result = [] for _, dictionary in sorted(grouped.items()): if 'base' not in dictionary: - raise RuntimeError('\'base\' not in dictionary', dictionary) + raise RuntimeError("'base' not in dictionary", dictionary) result.append(dictionary) return sort_declarations(result) @@ -719,6 +738,9 @@ def get_python_signature(declaration, include_out): if arg['simple_type'] == 'Type': type_args.append(arg) continue + # Skip `TensorOptions` in Python, as it is only used on the C++ side. + if arg['simple_type'] == 'TensorOptions': + continue if arg.get('kwarg_only', False) and positional: py_formal_args.append('*') positional = False diff --git a/tools/autograd/gen_variable_factories.py b/tools/autograd/gen_variable_factories.py new file mode 100644 index 000000000000..c14609fe0b7b --- /dev/null +++ b/tools/autograd/gen_variable_factories.py @@ -0,0 +1,44 @@ +# Generates C++ functions that wrap ATen tensor factory methods to turn them into Variables. +# +# This writes one file: variable_factories.h + +from .utils import CodeTemplate, write + +FUNCTION_TEMPLATE = CodeTemplate("""\ +inline autograd::Variable ${name}(${formals}) { + at::Tensor tensor = at::${name}(${actuals}); + return autograd::make_variable(tensor, /*requires_grad=*/${requires_grad}); +} +""") + + +def gen_variable_factories(out, declarations, template_path): + function_definitions = [] + for decl in declarations: + has_tensor_options = any(a["simple_type"] == "TensorOptions" for a in decl["arguments"]) + if has_tensor_options or decl["name"].endswith("_like"): + function_definitions.append(process_function(decl, has_tensor_options)) + write(out, + "variable_factories.h", + CodeTemplate.from_file(template_path + "/variable_factories.h"), + {"function_definitions": function_definitions}) + + +def process_function(decl, has_tensor_options): + formals = [] + actuals = [] + for argument in decl["arguments"]: + default = "= {}".format(argument["default"]) if "default" in argument else "" + formals.append("{} {} {}".format(argument["type"], argument["name"], default)) + actual = argument["name"] + if argument["simple_type"] == "TensorOptions": + # We want to discard the runtime type so that `at::{name}` always returns a + # tensor and not a variable, since we create a variable right after. + actual += ".discard_runtime_type()" + actuals.append(actual) + requires_grad = "options.requires_grad()" if has_tensor_options else "false" + if decl['name'].endswith('_like') and not has_tensor_options: + actuals.append('TensorOptions({}, /*discard_runtime_type=*/true)'.format(actuals[0])) + return FUNCTION_TEMPLATE.substitute( + name=decl["name"], formals=formals, actuals=actuals, requires_grad=requires_grad + ) diff --git a/tools/autograd/gen_variable_type.py b/tools/autograd/gen_variable_type.py index 8bd3a97c5402..c2ae06b12aae 100644 --- a/tools/autograd/gen_variable_type.py +++ b/tools/autograd/gen_variable_type.py @@ -202,6 +202,10 @@ def gen_variable_type(out, aten_declarations, template_path): type_definitions = [] for declaration in aten_declarations: + # Factory methods shall not appear in `VariableType` at all, since they + # don't dispatch via `Type`. + if any(arg['simple_type'] == 'TensorOptions' for arg in declaration['arguments']): + continue type_declarations.append(METHOD_DECLARATION.substitute(declaration)) if declaration['name'] not in MANUAL_IMPLEMENTATIONS: type_definitions.append(emit_method_definition(declaration)) diff --git a/tools/autograd/templates/Functions.cpp b/tools/autograd/templates/Functions.cpp index 182bde215e31..68b240f0298a 100644 --- a/tools/autograd/templates/Functions.cpp +++ b/tools/autograd/templates/Functions.cpp @@ -152,7 +152,7 @@ Tensor sum_backward(const Tensor & grad, IntList sizes, IntList dims, bool keepd } Tensor reverse_dim(const Tensor& t, int64_t dim) { - Tensor index = at::arange(t.type().toScalarType(at::ScalarType::Long), t.size(dim) - 1, -1, -1); + Tensor index = at::arange(t.size(dim) - 1, -1, -1, t.type().toScalarType(at::ScalarType::Long)); return t.index_select(dim, index); } @@ -163,7 +163,7 @@ Tensor prod_safe_zeros_backward(const Tensor &grad, const Tensor& inp, int64_t d std::vector ones_size(inp.sizes()); ones_size[dim] = 1; - Tensor ones = at::ones(grad.type(), ones_size); + Tensor ones = at::ones(ones_size, grad.type()); Tensor exclusive_normal_nocp = at::cat({ones, inp.narrow(dim, 0, inp.size(dim) - 1)}, dim); Tensor exclusive_normal = exclusive_normal_nocp.cumprod(dim); @@ -316,8 +316,8 @@ Tensor cumprod_backward(const Tensor &grad, const Tensor &input, int64_t dim) { std::vector ones_size(input.sizes()); ones_size[dim] = 1; - Tensor ones = at::ones(grad.type(), {1}).expand(ones_size); - Tensor grad_input = at::zeros(grad.type(), input.sizes()); + Tensor ones = at::ones({1}, grad.type()).expand(ones_size); + Tensor grad_input = at::zeros(input.sizes(), grad.type()); Tensor prods_from_k_plus_1; Tensor omitted_products; for (int k = 0; k < dim_size; ++k) { @@ -407,7 +407,7 @@ std::vector cat_tensors_backward(const Tensor & grad, const std::vector< auto& shape = sizes[i]; // If input was empty tensor, gradInput should be empty tensor. if (shape == std::vector({0})) { - grad_inputs[i] = at::zeros(grad.type(), {0}); + grad_inputs[i] = at::zeros({0}, grad.type()); continue; } auto size = shape[dim]; @@ -499,7 +499,7 @@ Tensor select_backward(Tensor grad, int64_t dim, Tensor indices, IntList sizes, grad = grad.unsqueeze(dim); indices = indices.unsqueeze(dim); } - return at::zeros(grad.type(), sizes).scatter_(dim, indices, grad); + return at::zeros(sizes, grad.type()).scatter_(dim, indices, grad); } Tensor trace_backward(const Tensor & grad, IntList sizes) { @@ -509,8 +509,8 @@ Tensor trace_backward(const Tensor & grad, IntList sizes) { auto& long_type = grad.type().toScalarType(at::kLong); - auto grad_input = at::zeros(grad.type(), sizes[0] * sizes[1]); - auto indices = at::arange(long_type, 0, grad_input.numel(), sizes[1] + 1); + auto grad_input = at::zeros(sizes[0] * sizes[1], grad.type()); + auto indices = at::arange(0, grad_input.numel(), sizes[1] + 1, long_type); grad_input.index_fill_(0, indices, grad); return grad_input.view(sizes); } @@ -523,9 +523,9 @@ Tensor unfold_backward(const Tensor & grad, IntList input_sizes, int64_t dim, in numel *= size; } - auto idx = at::arange(long_type, 0, numel).view(input_sizes); + auto idx = at::arange(0, numel, long_type).view(input_sizes); auto idx_unfolded = idx.unfold(dim, size, step).contiguous().view(-1); - auto grad_input = at::zeros(grad.type(), {numel}); + auto grad_input = at::zeros({numel}, grad.type()); grad_input.index_add_(0, idx_unfolded, grad.contiguous().view(-1)); return grad_input.view(input_sizes); } @@ -554,7 +554,7 @@ Tensor masked_scatter_backward(const Tensor & grad, const Tensor & mask, IntList if (diff_nelem > 0) { // because mask_selected returns a 1-d tensor with size of masked elements that are 1, // we need to fill out the rest with zeros then reshape back to tensor2's size. - auto zeros_fillin = at::zeros(grad.type(), {diff_nelem}); + auto zeros_fillin = at::zeros({diff_nelem}, grad.type()); mask_selected = at::cat({mask_selected, zeros_fillin}, 0); } return mask_selected.view(sizes); @@ -602,7 +602,7 @@ Tensor split_with_sizes_backward(const std::vector &g auto length = split_sizes[j]; std::vector grad_size(sizes); grad_size[dim] = length; - grads_all_defined[j] = at::zeros(type, grad_size); + grads_all_defined[j] = at::zeros(grad_size, type); } } @@ -651,7 +651,7 @@ Tensor glu_double_backward_grad_output(const Tensor & grad, const Tensor & input if (dim < 0) dim += input.dim(); std::vector sizes = input.sizes(); sizes[dim] /= 2; - auto tmp = grad * glu_backward(at::ones(input.type(), sizes), input, dim); + auto tmp = grad * glu_backward(at::ones(sizes, input.type()), input, dim); return tmp.narrow(dim, 0, sizes[dim]) + tmp.narrow(dim, sizes[dim], sizes[dim]); } @@ -755,7 +755,7 @@ Tensor diag_backward(const Tensor & grad, IntList input_sizes, int64_t diagonal) } // Input was a matrix but was not square - auto grad_input = at::zeros(grad.type(), input_sizes); + auto grad_input = at::zeros(input_sizes, grad.type()); auto diagonal_size = diag_size(input_sizes[0], input_sizes[1], diagonal); auto storage_offset = diagonal >= 0 ? diagonal : -diagonal * input_sizes[1]; auto diag = grad_input.as_strided({diagonal_size}, {input_sizes[1] + 1}, storage_offset); @@ -764,7 +764,7 @@ Tensor diag_backward(const Tensor & grad, IntList input_sizes, int64_t diagonal) } Tensor diagonal_backward(const Tensor & grad, IntList input_sizes, int64_t offset, int64_t dim1, int64_t dim2) { - auto grad_input = at::zeros(grad.type(), input_sizes); + auto grad_input = at::zeros(input_sizes, grad.type()); auto diag = grad_input.diagonal(offset, dim1, dim2); diag.copy_(grad); return grad_input; @@ -938,7 +938,7 @@ Tensor svd_backward(const std::vector &grads, const T if (gsigma.defined()) { sigma_term = u.mm(gsigma.diag()).mm(vt); } else { - sigma_term = at::zeros(self.type(), {1}).expand_as(self); + sigma_term = at::zeros({1}, self.type()).expand_as(self); } // in case that there are no gu and gv, we can avoid the series of kernel // calls below @@ -947,8 +947,8 @@ Tensor svd_backward(const std::vector &grads, const T } auto ut = u.t(); - auto im = self.type().eye(m); - auto in = self.type().eye(n); + auto im = eye(m, self.type()); + auto in = eye(n, self.type()); auto sigma_mat = sigma.diag(); auto sigma_mat_inv = sigma.pow(-1).diag(); auto sigma_expanded_sq = sigma.pow(2).expand_as(sigma_mat); @@ -968,7 +968,7 @@ Tensor svd_backward(const std::vector &grads, const T } u_term = u_term.mm(vt); } else { - u_term = at::zeros(self.type(), {1}).expand_as(self); + u_term = at::zeros({1}, self.type()).expand_as(self); } if (gv.defined()) { @@ -979,7 +979,7 @@ Tensor svd_backward(const std::vector &grads, const T } v_term = u.mm(v_term); } else { - v_term = at::zeros(self.type(), {1}).expand_as(self); + v_term = at::zeros({1}, self.type()).expand_as(self); } return u_term + sigma_term + v_term; @@ -1051,10 +1051,10 @@ std::tuple trtrs_backward( } } if (!grad_a.defined()) { - grad_a = at::zeros(a.type(), {1}).expand_as(a); + grad_a = at::zeros({1}, a.type()).expand_as(a); } if (!grad_b.defined()) { - grad_b = at::zeros(b.type(), {1}).expand_as(b); + grad_b = at::zeros({1}, b.type()).expand_as(b); } if (output_mask[1] && grad_m.defined()) { grad_a = grad_a.add(grad_m); @@ -1094,7 +1094,7 @@ Tensor fft_backward(const Tensor& self, const Tensor& grad, int64_t signal_ndim, } zero_grad_shape[signal_ndim] = zero_length; zero_grad_shape[signal_ndim + 1] = 2; - complex_full_grad = at::cat({ grad, at::zeros(grad.type(), zero_grad_shape) }, signal_ndim); + complex_full_grad = at::cat({ grad, at::zeros(zero_grad_shape, grad.type()) }, signal_ndim); } gI = _fft_with_size(complex_full_grad, signal_ndim, /* complex_input */ true, /* complex_output */ true, diff --git a/tools/autograd/templates/Functions.h b/tools/autograd/templates/Functions.h index 47141c1f7735..6532a5f317d9 100644 --- a/tools/autograd/templates/Functions.h +++ b/tools/autograd/templates/Functions.h @@ -30,7 +30,7 @@ struct TypeAndSize { : sizes(t.sizes()) , type(&t.type()) {} - Tensor zeros() { return at::zeros(*type, sizes); } + Tensor zeros() { return at::zeros(sizes, *type); } private: std::vector sizes; diff --git a/tools/autograd/templates/VariableType.cpp b/tools/autograd/templates/VariableType.cpp index 4dceec595f51..5f41fafb348e 100644 --- a/tools/autograd/templates/VariableType.cpp +++ b/tools/autograd/templates/VariableType.cpp @@ -15,10 +15,16 @@ #include "torch/csrc/jit/tensor_conversions.h" #include "torch/csrc/utils/variadic.h" -#include -#include -#include +#include #include +#include +#include +#include +#include +#include +#include +#include +#include #ifdef _MSC_VER #ifdef Type @@ -428,7 +434,9 @@ Tensor & VariableType::s_copy_(Tensor & self, const Tensor & src, bool non_block grad_fn = std::make_shared(); grad_fn->set_next_edges(collect_next_edges(self, src)); grad_fn->src_type = &src.type(); - grad_fn->src_device = src.is_cuda() ? src.get_device() : -1; + if (src.is_cuda()) { + grad_fn->src_device = src.get_device(); + } } baseType->s_copy_(self_, src_, non_blocking); increment_version(self); diff --git a/tools/autograd/templates/VariableType.h b/tools/autograd/templates/VariableType.h index e5a852f7d2cc..5c88bdc5edf0 100644 --- a/tools/autograd/templates/VariableType.h +++ b/tools/autograd/templates/VariableType.h @@ -18,6 +18,7 @@ using at::Generator; using at::IntList; using at::Scalar; using at::SparseTensorRef; +using at::ScalarType; using at::Storage; using at::Tensor; using at::TensorList; diff --git a/tools/autograd/templates/python_nn_functions_dispatch.h b/tools/autograd/templates/python_nn_functions_dispatch.h index e9785906c566..f7677a7df2d7 100644 --- a/tools/autograd/templates/python_nn_functions_dispatch.h +++ b/tools/autograd/templates/python_nn_functions_dispatch.h @@ -2,9 +2,9 @@ // ${generated_comment} -#include #include "torch/csrc/utils/auto_gil.h" -#include "torch/csrc/utils/auto_gpu.h" + +#include // Contains inline wrappers around ATen functions that release the GIL and // switch to the correct CUDA device. diff --git a/tools/autograd/templates/python_torch_functions.cpp b/tools/autograd/templates/python_torch_functions.cpp index 75fdcc7669c6..6e4079b9fd00 100644 --- a/tools/autograd/templates/python_torch_functions.cpp +++ b/tools/autograd/templates/python_torch_functions.cpp @@ -8,26 +8,33 @@ #include +#include "python_torch_functions_dispatch.h" + +#include "torch/csrc/autograd/python_variable.h" +#include "torch/csrc/autograd/utils/wrap_outputs.h" #include "torch/csrc/Dtype.h" #include "torch/csrc/DynamicTypes.h" #include "torch/csrc/Exceptions.h" -#include "torch/csrc/autograd/python_variable.h" -#include "torch/csrc/autograd/utils/python_variables.h" -#include "torch/csrc/autograd/utils/wrap_outputs.h" #include "torch/csrc/utils/python_arg_parser.h" +#include "torch/csrc/utils/tensor_layouts.h" #include "torch/csrc/utils/tensor_new.h" #include "torch/csrc/utils/tensor_numpy.h" -#include "torch/csrc/utils/tensor_devices.h" -#include "torch/csrc/utils/tensor_layouts.h" +#include "torch/csrc/autograd/generated/variable_factories.h" + +#include -#include "python_torch_functions_dispatch.h" #include +#include +#include +#include using at::Tensor; +using at::Device; using at::Scalar; using at::ScalarType; using at::Backend; -using torch::autograd::utils::set_requires_grad; +using at::DeviceGuard; +using at::TensorOptions; using namespace torch::autograd::utils; @@ -42,7 +49,7 @@ static void check_out_type_matches(Tensor result, } auto scalarType_arg = scalarType_is_none ? result.type().scalarType() : scalarType; auto layout_arg = layout_is_none ? *torch::getLayout(result.type().backend()) : layout; - auto device_type_arg = device_is_none ? torch::getDeviceType(result.type()) : device.type; + auto device_type_arg = device_is_none ? torch::getDeviceType(result.type()) : device.type(); const auto& type = torch::getType(scalarType_arg, layout_arg, device_type_arg); if (result.type() != type) { AT_ERROR( @@ -54,28 +61,24 @@ static void check_out_type_matches(Tensor result, inline Tensor & dispatch_arange(Scalar end, Tensor result) { AutoNoGIL no_gil; - AutoGPU auto_gpu(result); return at::arange_out(result, end); } -inline Tensor dispatch_arange(Scalar end, const Type & dtype, int64_t device) { - maybe_initialize_cuda(dtype); +inline Tensor dispatch_arange(Scalar end, const TensorOptions& options) { + maybe_initialize_cuda(options.type()); AutoNoGIL no_gil; - AutoGPU auto_gpu(device); - return at::arange(dtype, end); + return torch::arange(end, options); } inline Tensor & dispatch_arange(Scalar start, Scalar end, Scalar step, Tensor result) { AutoNoGIL no_gil; - AutoGPU auto_gpu(result); return at::arange_out(result, start, end, step); } -inline Tensor dispatch_arange(Scalar start, Scalar end, Scalar step, const Type & dtype, int64_t device) { - maybe_initialize_cuda(dtype); +inline Tensor dispatch_arange(Scalar start, Scalar end, Scalar step, const TensorOptions& options) { + maybe_initialize_cuda(options.type()); AutoNoGIL no_gil; - AutoGPU auto_gpu(device); - return at::arange(dtype, start, end, step); + return torch::arange(start, end, step, options); } static inline bool allIntegral(std::initializer_list> l) { @@ -100,31 +103,37 @@ static PyObject * THPVariable_arange(PyObject* self, PyObject* args, PyObject* k if (r.idx == 0) { if (r.isNone(1)) { - auto device = r.device(4); auto end = r.scalar(0); // NOTE: r.scalartype(X) gives the default dtype if r.isNone(X) auto scalarType = r.isNone(2) && allIntegral({end}) ? at::ScalarType::Long : r.scalartype(2); - auto& type = torch::getType(scalarType, r.layout(3), device.type); - return wrap(set_requires_grad(dispatch_arange(end, type, device.deviceInt64()), r.toBool(5))); + const auto options = TensorOptions() + .dtype(scalarType) + .device(r.device(4)) + .layout(r.layout(3).layout) + .requires_grad(r.toBool(5)); + return wrap(dispatch_arange(end, options)); } else { check_out_type_matches(r.tensor(1), r.scalartype(2), r.isNone(2), r.layout(3), r.isNone(3), r.device(4), r.isNone(4)); - return wrap(set_requires_grad(dispatch_arange(r.scalar(0), r.tensor(1)), r.toBool(5))); + return wrap(dispatch_arange(r.scalar(0), r.tensor(1)).set_requires_grad(r.toBool(5))); } } else if (r.idx == 1) { if (r.isNone(3)) { - auto device = r.device(6); auto start = r.scalar(0); auto end = r.scalar(1); auto step = r.scalar(2); // NOTE: r.scalartype(X) gives the default dtype if r.isNone(X) auto scalarType = r.isNone(4) && allIntegral({start, end, step}) ? at::ScalarType::Long : r.scalartype(4); - auto& type = torch::getType(scalarType, r.layout(5), device.type); - return wrap(set_requires_grad(dispatch_arange(start, end, step, type, device.deviceInt64()), r.toBool(7))); + const auto options = TensorOptions() + .dtype(scalarType) + .device(r.device(6)) + .layout(r.layout(5).layout) + .requires_grad(r.toBool(7)); + return wrap(dispatch_arange(start, end, step, options)); } else { check_out_type_matches(r.tensor(3), r.scalartype(4), r.isNone(4), r.layout(5), r.isNone(5), r.device(6), r.isNone(6)); - return wrap(set_requires_grad(dispatch_arange(r.scalar(0), r.scalar(1), r.scalar(2), r.tensor(3)), r.toBool(7))); + return wrap(dispatch_arange(r.scalar(0), r.scalar(1), r.scalar(2), r.tensor(3)).set_requires_grad(r.toBool(7))); } } Py_RETURN_NONE; @@ -133,15 +142,15 @@ static PyObject * THPVariable_arange(PyObject* self, PyObject* args, PyObject* k inline Tensor & dispatch_range(Scalar start, Scalar end, Scalar step, Tensor result) { AutoNoGIL no_gil; - AutoGPU auto_gpu(result); + DeviceGuard device_guard(result); return at::range_out(result, start, end, step); } -inline Tensor dispatch_range(Scalar start, Scalar end, Scalar step, const Type & dtype, int64_t device) { - maybe_initialize_cuda(dtype); +inline Tensor dispatch_range(Scalar start, Scalar end, Scalar step, const TensorOptions& options) { + maybe_initialize_cuda(options.type()); AutoNoGIL no_gil; - AutoGPU auto_gpu(device); - return at::range(dtype, start, end, step); + DeviceGuard device_guard(options.device()); + return torch::range(start, end, step, options); } static PyObject * THPVariable_range(PyObject* self, PyObject* args, PyObject* kwargs) @@ -158,15 +167,17 @@ static PyObject * THPVariable_range(PyObject* self, PyObject* args, PyObject* kw "and will be removed in 0.5. Note that arange generates values in [start; end), " "not [start; end].", 1); if (r.isNone(3)) { - auto device = r.device(6); - return wrap(set_requires_grad(dispatch_range(r.scalar(0), r.scalar(1), r.scalar(2), - torch::getType(r.scalartype(4), r.layout(5), device.type), - device.deviceInt64()), r.toBool(7))); + const auto options = TensorOptions() + .dtype(r.scalartype(4)) + .device(r.device(6)) + .layout(r.layout(5).layout) + .requires_grad(r.toBool(7)); + return wrap(dispatch_range(r.scalar(0), r.scalar(1), r.scalar(2), options)); } else { check_out_type_matches(r.tensor(3), r.scalartype(4), r.isNone(4), r.layout(5), r.isNone(5), r.device(6), r.isNone(6)); - return wrap(set_requires_grad(dispatch_range(r.scalar(0), r.scalar(1), r.scalar(2), r.tensor(3)), r.toBool(7))); + return wrap(dispatch_range(r.scalar(0), r.scalar(1), r.scalar(2), r.tensor(3)).set_requires_grad(r.toBool(7))); } } Py_RETURN_NONE; diff --git a/tools/autograd/templates/python_torch_functions_dispatch.h b/tools/autograd/templates/python_torch_functions_dispatch.h index c6ab4d5d18e5..1c80ae17fa21 100644 --- a/tools/autograd/templates/python_torch_functions_dispatch.h +++ b/tools/autograd/templates/python_torch_functions_dispatch.h @@ -2,12 +2,13 @@ // ${generated_comment} -#include -#include "torch/csrc/utils/auto_gil.h" -#include "torch/csrc/utils/auto_gpu.h" -#include "torch/csrc/utils/cuda_lazy_init.h" #include "torch/csrc/autograd/generated/VariableType.h" +#include "torch/csrc/autograd/generated/variable_factories.h" #include "torch/csrc/tensor/python_tensor.h" +#include "torch/csrc/utils/auto_gil.h" +#include "torch/csrc/utils/cuda_lazy_init.h" + +#include // Contains inline wrappers around ATen functions that release the GIL and // switch to the correct CUDA device. @@ -21,6 +22,7 @@ using at::IntList; using at::Generator; using at::SparseTensorRef; using at::Storage; +using at::TensorOptions; static at::Type& default_type() { return torch::tensor::get_default_tensor_type(); @@ -35,32 +37,26 @@ static void maybe_initialize_cuda(const at::Type &type) { // manual dispatch code for clamp inline Tensor dispatch_clamp(const Tensor & self, Scalar min, Scalar max) { AutoNoGIL no_gil; - AutoGPU auto_gpu(self); return self.clamp(min, max); } inline Tensor dispatch_clamp_min(const Tensor & self, Scalar min) { AutoNoGIL no_gil; - AutoGPU auto_gpu(self); return self.clamp_min(min); } inline Tensor dispatch_clamp_max(const Tensor & self, Scalar max) { AutoNoGIL no_gil; - AutoGPU auto_gpu(self); return self.clamp_max(max); } inline Tensor & dispatch_clamp(const Tensor & self, Scalar min, Scalar max, Tensor result) { AutoNoGIL no_gil; - AutoGPU auto_gpu(result); return at::clamp_out(result, self, min, max); } inline Tensor & dispatch_clamp_min(const Tensor & self, Scalar min, Tensor result) { AutoNoGIL no_gil; - AutoGPU auto_gpu(result); return at::clamp_min_out(result, self, min); } inline Tensor & dispatch_clamp_max(const Tensor & self, Scalar max, Tensor result) { AutoNoGIL no_gil; - AutoGPU auto_gpu(result); return at::clamp_max_out(result, self, max); } diff --git a/tools/autograd/templates/python_variable_methods.cpp b/tools/autograd/templates/python_variable_methods.cpp index 81d925f7dd07..8e8c87607b47 100644 --- a/tools/autograd/templates/python_variable_methods.cpp +++ b/tools/autograd/templates/python_variable_methods.cpp @@ -26,12 +26,18 @@ #include "torch/csrc/utils/tensor_numpy.h" #include "torch/csrc/utils/tensor_types.h" +#include +#include + #include "python_variable_methods_dispatch.h" -using at::Tensor; +#include + +using at::DeviceGuard; +using at::Backend; using at::Scalar; using at::ScalarType; -using at::Backend; +using at::Tensor; using namespace torch::autograd::utils; namespace torch { namespace autograd { @@ -51,17 +57,17 @@ static PyObject * THPVariable_apply_(PyObject* self, PyObject* arg) static Tensor dispatch_clamp(const Tensor & self, Scalar min, Scalar max) { AutoNoGIL no_gil; - AutoGPU auto_gpu(self); + DeviceGuard device_guard(self); return self.clamp(min, max); } static Tensor dispatch_clamp_min(const Tensor & self, Scalar min) { AutoNoGIL no_gil; - AutoGPU auto_gpu(self); + DeviceGuard device_guard(self); return self.clamp_min(min); } static Tensor dispatch_clamp_max(const Tensor & self, Scalar max) { AutoNoGIL no_gil; - AutoGPU auto_gpu(self); + DeviceGuard device_guard(self); return self.clamp_max(max); } @@ -88,17 +94,17 @@ static PyObject * THPVariable_clamp(PyObject* self, PyObject* args, PyObject* kw static Tensor & dispatch_clamp_(Tensor & self, Scalar min, Scalar max) { AutoNoGIL no_gil; - AutoGPU auto_gpu(self); + DeviceGuard device_guard(self); return self.clamp_(min, max); } static Tensor & dispatch_clamp_min_(Tensor & self, Scalar min) { AutoNoGIL no_gil; - AutoGPU auto_gpu(self); + DeviceGuard device_guard(self); return self.clamp_min_(min); } static Tensor & dispatch_clamp_max_(Tensor & self, Scalar max) { AutoNoGIL no_gil; - AutoGPU auto_gpu(self); + DeviceGuard device_guard(self); return self.clamp_max_(max); } @@ -181,7 +187,7 @@ static PyObject * THPVariable_dim(PyObject* self, PyObject* args) static Tensor dispatch_contiguous(const Tensor & self) { AutoNoGIL no_gil; - AutoGPU auto_gpu(self); + DeviceGuard device_guard(self); return self.contiguous(); } @@ -200,7 +206,7 @@ static PyObject * THPVariable_contiguous(PyObject* self, PyObject* args) static Tensor dispatch_copy_(Tensor & self, const Tensor & other, bool non_blocking) { AutoNoGIL no_gil; - AutoGPU auto_gpu(self); + DeviceGuard device_guard(self); return self.copy_(other, non_blocking); } @@ -237,7 +243,7 @@ static PyObject * THPVariable_detach_(PyObject* self, PyObject* args) static double dispatch_to_CDouble(const Tensor & self) { AutoNoGIL no_gil; - AutoGPU auto_gpu(self); + DeviceGuard device_guard(self); if (self.numel() != 1) { throw ValueError("only one element tensors can be converted to Python scalars"); } @@ -246,7 +252,7 @@ static double dispatch_to_CDouble(const Tensor & self) { static int64_t dispatch_to_CLong(const Tensor & self) { AutoNoGIL no_gil; - AutoGPU auto_gpu(self); + DeviceGuard device_guard(self); if (self.numel() != 1) { throw ValueError("only one element tensors can be converted to Python scalars"); } @@ -289,7 +295,7 @@ static PyObject * THPVariable_index_scalar(PyObject* self, PyObject* args) { static Tensor dispatch_invert(const Tensor & self) { AutoNoGIL no_gil; - AutoGPU auto_gpu(self); + DeviceGuard device_guard(self); return 1 - self; } @@ -326,11 +332,14 @@ static PyObject * THPVariable_cuda(PyObject* self, PyObject* args, PyObject* kwa auto backend = self_.is_sparse() ? at::kSparseCUDA : at::kCUDA; auto& type = self_.type().toBackend(backend); auto device_obj = r.device(0); - if (!r.isNone(0) && device_obj.type != DeviceType::CUDA) { + if (!r.isNone(0) && device_obj.is_cpu()) { throw std::runtime_error("Invalid device, must be cuda device"); } - auto device = (device_obj.is_default || device_obj.type == DeviceType::CPU) ? -1 : device_obj.index; - return THPVariable_Wrap(torch::utils::dispatch_type_conversion(self_, type, device, r.toBool(1))); + int32_t device_index = -1; + if (device_obj.has_index() && device_obj.is_cuda()) { + device_index = device_obj.index(); + } + return THPVariable_Wrap(torch::utils::dispatch_type_conversion(self_, type, device_index, r.toBool(1))); END_HANDLE_TH_ERRORS } @@ -487,7 +496,7 @@ static PyObject * THPVariable_new(PyObject* self, PyObject* args, PyObject* kwar { HANDLE_TH_ERRORS auto& self_ = reinterpret_cast(self)->cdata; - AutoGPU auto_gpu(self_); + DeviceGuard device_guard(self_); return THPVariable_Wrap(torch::utils::legacy_tensor_new(self_.type(), args, kwargs)); END_HANDLE_TH_ERRORS } @@ -496,7 +505,7 @@ static PyObject * THPVariable_new_empty(PyObject* self, PyObject* args, PyObject { HANDLE_TH_ERRORS auto& self_ = reinterpret_cast(self)->cdata; - AutoGPU auto_gpu(self_); + DeviceGuard device_guard(self_); return THPVariable_Wrap(torch::utils::new_empty(self_.type(), args, kwargs)); END_HANDLE_TH_ERRORS } @@ -505,7 +514,7 @@ static PyObject * THPVariable_new_full(PyObject* self, PyObject* args, PyObject* { HANDLE_TH_ERRORS auto& self_ = reinterpret_cast(self)->cdata; - AutoGPU auto_gpu(self_); + DeviceGuard device_guard(self_); return THPVariable_Wrap(torch::utils::new_full(self_.type(), args, kwargs)); END_HANDLE_TH_ERRORS } @@ -514,7 +523,7 @@ static PyObject * THPVariable_new_ones(PyObject* self, PyObject* args, PyObject* { HANDLE_TH_ERRORS auto& self_ = reinterpret_cast(self)->cdata; - AutoGPU auto_gpu(self_); + DeviceGuard device_guard(self_); return THPVariable_Wrap(torch::utils::new_ones(self_.type(), args, kwargs)); END_HANDLE_TH_ERRORS } @@ -523,7 +532,7 @@ static PyObject * THPVariable_new_tensor(PyObject* self, PyObject* args, PyObjec { HANDLE_TH_ERRORS auto& self_ = reinterpret_cast(self)->cdata; - AutoGPU auto_gpu(self_); + DeviceGuard device_guard(self_); return THPVariable_Wrap(torch::utils::new_tensor(self_.type(), args, kwargs)); END_HANDLE_TH_ERRORS } @@ -532,7 +541,7 @@ static PyObject * THPVariable_new_zeros(PyObject* self, PyObject* args, PyObject { HANDLE_TH_ERRORS auto& self_ = reinterpret_cast(self)->cdata; - AutoGPU auto_gpu(self_); + DeviceGuard device_guard(self_); return THPVariable_Wrap(torch::utils::new_zeros(self_.type(), args, kwargs)); END_HANDLE_TH_ERRORS } @@ -571,10 +580,10 @@ static PyObject * THPVariable_to(PyObject* self, PyObject* args, PyObject* kwarg } else { // device and maybe dtype are given auto& self_ = reinterpret_cast(self)->cdata; - auto deviceAutoGPU = device->deviceInt64(); auto& layout = *torch::getLayout(self_.type().backend()); - auto& type = torch::getType(scalarType.value_or(self_.type().scalarType()), layout, device->type); - return THPVariable_Wrap(torch::utils::dispatch_type_conversion(self_, type, deviceAutoGPU, non_blocking)); + auto& type = torch::getType(scalarType.value_or(self_.type().scalarType()), layout, device->type()); + const int32_t device_index = type.is_cuda() ? device->index() : -1; + return THPVariable_Wrap(torch::utils::dispatch_type_conversion(self_, type, device_index, non_blocking)); } Py_RETURN_NONE; END_HANDLE_TH_ERRORS diff --git a/tools/autograd/templates/python_variable_methods_dispatch.h b/tools/autograd/templates/python_variable_methods_dispatch.h index 82e7e409472e..97b4c53936a3 100644 --- a/tools/autograd/templates/python_variable_methods_dispatch.h +++ b/tools/autograd/templates/python_variable_methods_dispatch.h @@ -2,9 +2,9 @@ // ${generated_comment} -#include #include "torch/csrc/utils/auto_gil.h" -#include "torch/csrc/utils/auto_gpu.h" + +#include // Contains inline wrappers around ATen functions that release the GIL and // switch to the correct CUDA device. diff --git a/tools/autograd/templates/variable_factories.h b/tools/autograd/templates/variable_factories.h new file mode 100644 index 000000000000..431b4933487b --- /dev/null +++ b/tools/autograd/templates/variable_factories.h @@ -0,0 +1,20 @@ +#pragma once + +// ${generated_comment} + +#include + +#include + +namespace torch { +using at::Generator; +using at::IntList; +using at::Scalar; +using at::Tensor; +using at::TensorList; +using at::TensorOptions; +} // namespace torch::autograd + +namespace torch { +${function_definitions} +} // namespace torch::autograd diff --git a/tools/cwrap/plugins/AutoGPU.py b/tools/cwrap/plugins/AutoGPU.py index da37508cce10..d6abea7f17f4 100644 --- a/tools/cwrap/plugins/AutoGPU.py +++ b/tools/cwrap/plugins/AutoGPU.py @@ -8,7 +8,7 @@ class AutoGPU(CWrapPlugin): self.condition = condition def process_pre_arg_assign(self, template, option): - if not option.get('auto_gpu', True): + if not option.get('device_guard', True): return template - call = 'AutoGPU auto_gpu(get_device(args));' + call = 'at::DeviceGuard device_guard(get_device(args));' return [call] + template diff --git a/tools/cwrap/plugins/NNExtension.py b/tools/cwrap/plugins/NNExtension.py index 7d1a8f2aecdf..838fa0bfd94e 100644 --- a/tools/cwrap/plugins/NNExtension.py +++ b/tools/cwrap/plugins/NNExtension.py @@ -8,9 +8,10 @@ MODULE_HEAD = """ #include #include "THP.h" -#include "torch/csrc/utils/auto_gpu.h" #include "torch/csrc/nn/type_checks.h" +#include + """ REGISTER_METHOD_TEMPLATE = Template(' {"$name", (PyCFunction)$name, METH_STATIC | METH_VARARGS, NULL},\n') diff --git a/tools/jit/gen_jit_dispatch.py b/tools/jit/gen_jit_dispatch.py index 1e9b9c258109..c91cb0a2f4f9 100644 --- a/tools/jit/gen_jit_dispatch.py +++ b/tools/jit/gen_jit_dispatch.py @@ -40,16 +40,19 @@ auto ${name} = tensor_as(std::move(${name}_tensor));\ """) CALL_NAMESPACE = CodeTemplate("""\ -AutoGPU device_guard(deviceForInputs(stack, ${num_dynamic_inputs})); auto result = at::${name}(${args}); """) CALL_METHOD = CodeTemplate("""\ -AutoGPU device_guard(deviceForInputs(stack, ${num_dynamic_inputs})); +DeviceGuard device_guard(deviceForInputs(stack, ${num_dynamic_inputs})); auto result = (${first}).${name}(${args}); """) -CALL_DTYPE = CodeTemplate("""\ -AutoGPU device_guard(device[1]); -auto result = resolveType(device[0], layout, (at::ScalarType)dtype).${name}(${args}); +CALL_TENSOR_OPTIONS = CodeTemplate("""\ +const auto device_index = static_cast(device[1]); +const auto options = TensorOptions() + .dtype(static_cast(dtype)) + .layout(static_cast(layout)) + .device({static_cast(device[0]), device_index}); +auto result = torch::${name}(${args}, options); """) CONSTRUCTOR = CodeTemplate("""\ @@ -81,8 +84,7 @@ def is_jit_arg(i, arg): return False if simple_type in default_only_types and 'default' not in arg: return False - # allow 'Type dtype' as the first argument only - if simple_type == 'Type' and (i > 0 or arg['name'] != 'dtype'): + if simple_type == 'Type': return False return True @@ -130,8 +132,8 @@ def gen_jit_dispatch(declarations, out, template_path): ops = {} def get_invocation(decl, args, num_dynamic_inputs): - if decl.get('has_dtype'): - return CALL_DTYPE.substitute(name=decl['name'], args=args[:-3]) + if decl.get('has_tensor_options'): + return CALL_TENSOR_OPTIONS.substitute(name=decl['name'], args=args[:-3]) elif 'namespace' in decl['method_of']: return CALL_NAMESPACE.substitute(name=decl['name'], args=args, num_dynamic_inputs=num_dynamic_inputs) else: @@ -215,6 +217,7 @@ def gen_jit_dispatch(declarations, out, template_path): else: simple_type = arg['simple_type'] + assert simple_type in ATTR_METHOD_MAP, (decl['name'], simple_type) attr_method = ATTR_METHOD_MAP[simple_type] assign = KW_ASSIGNMENT.substitute(type_cast=TYPE_CASTS.get(simple_type, simple_type), name=arg['name'], @@ -286,19 +289,19 @@ def gen_jit_dispatch(declarations, out, template_path): # add arguments dtype and device for functions like zeros for decl in jit_decls: arguments = decl['arguments'] - if len(arguments) == 0 or arguments[0]['simple_type'] != 'Type': - continue - del arguments[0] - arguments.extend([ - # dtype is specified as an int64_t of at::ScalarType - {'name': 'dtype', 'simple_type': 'int64_t', 'default': 'static_cast(at::kFloat)'}, - # device is specified as an IntList of { torch::DeviceType, device_id } - {'name': 'device', 'simple_type': 'IntList', - 'default': '{static_cast(torch::DeviceType::CPU), -1}'}, - # layout is specified as an int64_t of Layouts's is_strided - {'name': 'layout', 'simple_type': 'int64_t', 'default': '1'} - ]) - decl['has_dtype'] = True + for n, arg in enumerate(arguments): + if arg['simple_type'] == 'TensorOptions': + del arguments[n] + arguments.extend([ + # dtype is specified as an int64_t of at::ScalarType + {'name': 'dtype', 'simple_type': 'int64_t', 'default': 'static_cast(at::kFloat)'}, + # device is specified as an IntList of { at::Device::Type, device_id } + {'name': 'device', 'simple_type': 'IntList', + 'default': '{static_cast(at::Device::Type::CPU), -1}'}, + # layout is specified as an int64_t of at::Layout + {'name': 'layout', 'simple_type': 'int64_t', 'default': 'static_cast(at::kStrided)'} + ]) + decl['has_tensor_options'] = True for decl in jit_decls: emit_decl(decl) diff --git a/tools/jit/templates/aten_dispatch.cpp b/tools/jit/templates/aten_dispatch.cpp index d1545d528988..8869a2f5ee0f 100644 --- a/tools/jit/templates/aten_dispatch.cpp +++ b/tools/jit/templates/aten_dispatch.cpp @@ -1,14 +1,25 @@ #include "torch/csrc/jit/aten_dispatch.h" + #include "torch/csrc/autograd/profiler.h" #include "torch/csrc/jit/interned_strings.h" #include "torch/csrc/jit/tensor_conversions.h" #include "torch/csrc/utils/functional.h" #include "torch/csrc/variable_tensor_functions.h" -#include "torch/csrc/utils/device.h" +#include "torch/csrc/autograd/generated/variable_factories.h" -#include +#include + +#include +#include +#include #include +#include +#include #include +#include +#include +#include +#include // ${generated_comment} @@ -20,6 +31,8 @@ using at::Scalar; using at::Tensor; using at::IntList; using at::TensorList; +using at::TensorOptions; +using at::DeviceGuard; namespace { @@ -36,6 +49,10 @@ void pack(Stack & stack, Tensor&& v) { stack.push_back(std::move(v)); } template<> +void pack(Stack & stack, autograd::Variable&& v) { + stack.push_back(std::move(v)); +} +template<> void pack(Stack & stack, std::vector&& ts) { for(auto& t : ts) { stack.push_back(std::move(t)); @@ -91,26 +108,6 @@ std::array as_bool_array(const std::vector& vec) { return res; } -static at::Backend resolveBackend(int64_t device_type, int64_t is_strided) { - switch(static_cast(device_type)) { - case torch::DeviceType::CPU: - if(is_strided) - return at::kCPU; - else - return at::kSparseCPU; - case torch::DeviceType::CUDA: - if(is_strided) - return at::kCUDA; - else - return at::kSparseCUDA; - } - throw std::runtime_error("unknown backend"); -} - -static at::Type& resolveType(int64_t device_type, int64_t is_strided, at::ScalarType type) { - return torch::getType(resolveBackend(device_type, is_strided), type); -} - using operator_constructor = std::function; std::unordered_map constructors = { ${constructors} diff --git a/tools/jit/templates/aten_schema.cpp b/tools/jit/templates/aten_schema.cpp index 5c289b279eed..ae1978387a46 100644 --- a/tools/jit/templates/aten_schema.cpp +++ b/tools/jit/templates/aten_schema.cpp @@ -1,6 +1,5 @@ #include "torch/csrc/jit/aten_schema.h" #include "torch/csrc/jit/tensor_conversions.h" -#include "torch/csrc/utils/device.h" namespace torch { namespace jit { diff --git a/torch/CMakeLists.txt b/torch/CMakeLists.txt index 16b7ef6204f1..1107e6bf3e14 100644 --- a/torch/CMakeLists.txt +++ b/torch/CMakeLists.txt @@ -360,7 +360,12 @@ if (TORCH_BUILD_TEST) ${TORCH_API_TEST_DIR}/sequential.cpp ${TORCH_API_TEST_DIR}/rnn.cpp ${TORCH_API_TEST_DIR}/serialization.cpp - ${TORCH_API_TEST_DIR}/static.cpp) + ${TORCH_API_TEST_DIR}/static.cpp + ${TORCH_API_TEST_DIR}/tensor.cpp + # Temporary until ATen tests are built with Caffe2 + ${TORCH_API_TEST_DIR}/tensor_options.cpp + ${TORCH_API_TEST_DIR}/tensor_options_cuda.cpp + ) target_include_directories(test_api PUBLIC diff --git a/torch/csrc/Device.cpp b/torch/csrc/Device.cpp index e28ff42fb97e..f130f8164c0a 100644 --- a/torch/csrc/Device.cpp +++ b/torch/csrc/Device.cpp @@ -1,15 +1,19 @@ -#include "Device.h" +#include "torch/csrc/Device.h" -#include -#include -#include #include "torch/csrc/Exceptions.h" #include "torch/csrc/utils/object_ptr.h" #include "torch/csrc/utils/python_arg_parser.h" #include "torch/csrc/utils/python_strings.h" #include "torch/csrc/utils/pybind.h" -PyObject *THPDevice_New(const torch::Device& device) +#include +#include + +#include +#include +#include + +PyObject *THPDevice_New(const at::Device& device) { auto type = (PyTypeObject*)&THPDeviceType; auto self = THPObjectPtr{type->tp_alloc(type, 0)}; @@ -19,26 +23,12 @@ PyObject *THPDevice_New(const torch::Device& device) return self.release(); } -static const char* cuda_str = "cuda"; -static const char* cpu_str = "cpu"; - -static inline const char* deviceTypeString(torch::DeviceType device_type) { - switch (device_type) { - case torch::DeviceType::CUDA: - return cuda_str; - case torch::DeviceType::CPU: - return cpu_str; - default: - throw std::runtime_error("unexpected device type"); - } -} - PyObject *THPDevice_repr(THPDevice *self) { std::ostringstream oss; - oss << "device(type=\'" << deviceTypeString(self->device.type) << "\'"; - if (!self->device.is_default) { - oss << ", index=" << self->device.index; + oss << "device(type=\'" << self->device.type() << "\'"; + if (self->device.has_index()) { + oss << ", index=" << self->device.index(); } oss << ")"; return THPUtils_packString(oss.str().c_str()); @@ -47,11 +37,7 @@ PyObject *THPDevice_repr(THPDevice *self) PyObject *THPDevice_str(THPDevice *self) { std::ostringstream oss; - if (!self->device.is_default) { - oss << deviceTypeString(self->device.type) << ":" << self->device.index; - } else { - oss << deviceTypeString(self->device.type); - } + oss << self->device; return THPUtils_packString(oss.str().c_str()); } @@ -70,15 +56,18 @@ PyObject *THPDevice_pynew(PyTypeObject *type, PyObject *args, PyObject *kwargs) } else if (r.idx == 1) { auto as_device = r.device(0); // this works, because device can take strings auto device_type = r.string(0); - if (!as_device.is_default) { + if (as_device.has_index()) { throw std::runtime_error("type (string) must not include an index because index " "was passed explicitly: " + device_type); } - - auto is_default = r.isNone(1); - auto device_index = r.toInt64WithDefault(1, -1); - // make sure this is constructible - auto device = torch::Device(as_device.type, device_index, is_default); + int32_t device_index = -1; + if (!r.isNone(1)) { + device_index = r.toInt64(1); + // -1 is allowed in ATen/C++, to mean the default device, but not in + // Python. + AT_CHECK(device_index >= 0, "Device index must not be negative"); + } + at::Device device(as_device.type(), device_index); return THPDevice_New(device); } Py_RETURN_NONE; @@ -88,7 +77,9 @@ PyObject *THPDevice_pynew(PyTypeObject *type, PyObject *args, PyObject *kwargs) PyObject *THPDevice_type(THPDevice *self) { HANDLE_TH_ERRORS - return THPUtils_packString(deviceTypeString(self->device.type)); + std::ostringstream oss; + oss << self->device.type(); + return THPUtils_packString(oss.str().c_str()); Py_RETURN_NONE; END_HANDLE_TH_ERRORS } @@ -96,10 +87,10 @@ PyObject *THPDevice_type(THPDevice *self) PyObject *THPDevice_index(THPDevice *self) { HANDLE_TH_ERRORS - if (self->device.is_default) { - Py_RETURN_NONE; + if (self->device.has_index()) { + return THPUtils_packInt64(self->device.index()); } else { - return THPUtils_packInt64(self->device.index); + Py_RETURN_NONE; } END_HANDLE_TH_ERRORS } @@ -149,10 +140,12 @@ PyObject *THPDevice_reduce(THPDevice *self) PyTuple_SET_ITEM(ret.get(), 0, torch_device.release().ptr()); THPObjectPtr args; - if (self->device.is_default) { - args = THPObjectPtr{Py_BuildValue("(s)", deviceTypeString(self->device.type))}; + std::ostringstream oss; + oss << self->device.type(); + if (self->device.has_index()) { + args = THPObjectPtr{Py_BuildValue("(si)", oss.str().c_str(), self->device.index())}; } else { - args = THPObjectPtr{Py_BuildValue("(si)", deviceTypeString(self->device.type), self->device.index)}; + args = THPObjectPtr{Py_BuildValue("(s)", oss.str().c_str())}; } if (!args) throw python_error(); PyTuple_SET_ITEM(ret.get(), 1, args.release()); diff --git a/torch/csrc/Device.h b/torch/csrc/Device.h index d0c1691c02c4..d14c4006033f 100644 --- a/torch/csrc/Device.h +++ b/torch/csrc/Device.h @@ -1,11 +1,12 @@ #pragma once #include "torch/csrc/python_headers.h" -#include "torch/csrc/utils/device.h" + +#include struct THPDevice { PyObject_HEAD - torch::Device device; + at::Device device; }; extern PyTypeObject THPDeviceType; @@ -14,6 +15,6 @@ inline bool THPDevice_Check(PyObject *obj) { return Py_TYPE(obj) == &THPDeviceType; } -PyObject * THPDevice_New(const torch::Device& device); +PyObject * THPDevice_New(const at::Device& device); void THPDevice_init(PyObject *module); diff --git a/torch/csrc/DynamicTypes.cpp b/torch/csrc/DynamicTypes.cpp index a9dbf40fa9db..321fbc791e3c 100644 --- a/torch/csrc/DynamicTypes.cpp +++ b/torch/csrc/DynamicTypes.cpp @@ -1,14 +1,21 @@ #include "torch/csrc/python_headers.h" -#include "DynamicTypes.h" -#include "PythonTypes.h" -#include "Exceptions.h" +#include "torch/csrc/DynamicTypes.h" +#include "torch/csrc/Dtype.h" +#include "torch/csrc/Layout.h" +#include "torch/csrc/PythonTypes.h" +#include "torch/csrc/Exceptions.h" #include "torch/csrc/autograd/generated/VariableType.h" #include "torch/csrc/utils/cuda_enabled.h" -#include -#include +#include + +#include #include +#include +#include +#include +#include #ifdef USE_CUDA #include @@ -16,8 +23,8 @@ #endif namespace torch { - -static std::unordered_map attype_names = { +namespace { +const std::unordered_map attype_names = { {"Float", at::kFloat}, {"Double", at::kDouble}, {"Half", at::kHalf}, @@ -28,16 +35,16 @@ static std::unordered_map attype_names = { {"Long", at::kLong}, }; -static std::unordered_map attype_to_py_storage_type; -static std::unordered_map py_storage_type_to_attype; +std::unordered_map attype_to_py_storage_type; +std::unordered_map py_storage_type_to_attype; -static THPDtype* dtype_registry +THPDtype* dtype_registry [static_cast(at::ScalarType::NumOptions)] = {}; -static THPLayout* layout_registry +THPLayout* layout_registry [static_cast(at::Backend::NumOptions)] = {}; -static at::Backend get_backend(bool is_cuda, bool is_sparse) { +at::Backend get_backend(bool is_cuda, bool is_sparse) { if (is_cuda) { if (is_sparse){ return at::kSparseCUDA; @@ -53,7 +60,7 @@ static at::Backend get_backend(bool is_cuda, bool is_sparse) { } } -static at::Type* get_type(const std::string& name, bool is_cuda, bool is_sparse) { +at::Type* get_type(const std::string& name, bool is_cuda, bool is_sparse) { if (is_sparse && name == "Half") { return nullptr; } @@ -61,6 +68,16 @@ static at::Type* get_type(const std::string& name, bool is_cuda, bool is_sparse) return &at::getType(backend, attype_names.at(name)); } +PyTypeObject* getPyTypeObject(const at::Storage& storage) +{ + auto it = attype_to_py_storage_type.find(&storage.type()); + if (it != attype_to_py_storage_type.end()) { + return it->second; + } + throw std::invalid_argument("unsupported Storage type"); +} +} // namespace + void registerStoragePyTypeObject(PyTypeObject *pytype, const std::string& name, bool is_cuda, bool is_sparse) { auto attype = get_type(name, is_cuda, is_sparse); @@ -78,23 +95,14 @@ void registerLayoutObject(THPLayout *layout, at::Backend backend) { layout_registry[static_cast(backend)] = layout; } -static PyTypeObject* getPyTypeObject(const at::Storage& storage) -{ - auto it = attype_to_py_storage_type.find(&storage.type()); - if (it != attype_to_py_storage_type.end()) { - return it->second; - } - throw std::invalid_argument("unsupported Storage type"); -} - -at::Type& getType(at::ScalarType scalarType, const THPLayout& layout, const DeviceType& deviceType) { - at::Backend backend = get_backend(deviceType == DeviceType::CUDA, !layout.is_strided); +at::Type& getType(at::ScalarType scalarType, const THPLayout& layout, const at::Device& device) { + const at::Backend backend = get_backend(device.type() == at::Device::Type::CUDA, layout.layout == at::Layout::Sparse); auto baseType = at::globalContext().getTypeOpt(backend, scalarType); if (!baseType) { std::ostringstream oss; oss << "Error attempting to use dtype " << getDtype(scalarType)->name << " with layout " << layout.name - << " and device type " << (deviceType == DeviceType::CPU ? "CPU" : "CUDA") << "."; - if (deviceType == DeviceType::CUDA && !torch::utils::cuda_enabled()) { + << " and device type " << device.type() << "."; + if (device.type() == at::Device::Type::CUDA && !torch::utils::cuda_enabled()) { oss << " Torch not compiled with CUDA enabled." << std::endl; } throw std::runtime_error(oss.str()); @@ -118,8 +126,8 @@ THPLayout* getLayout(at::Backend backend) { return layout; } -DeviceType getDeviceType(const at::Type& type) { - return type.is_cuda() ? torch::DeviceType::CUDA : torch::DeviceType::CPU; +at::Device::Type getDeviceType(const at::Type& type) { + return type.is_cuda() ? at::Device::Type::CUDA : at::Device::Type::CPU; } PyObject* createPyObject(const at::Storage& storage) diff --git a/torch/csrc/DynamicTypes.h b/torch/csrc/DynamicTypes.h index bcd92022f278..25238cb68dd4 100644 --- a/torch/csrc/DynamicTypes.h +++ b/torch/csrc/DynamicTypes.h @@ -3,15 +3,23 @@ // Provides conversions between Python tensor objects and at::Tensor. #include "torch/csrc/python_headers.h" + +#include + #include -#include -#include -#include "torch/csrc/Dtype.h" -#include "torch/csrc/Layout.h" -#include "torch/csrc/utils/device.h" +#include + +struct THPDtype; +struct THPLayout; + +namespace at { +enum class Backend; +enum class ScalarType; +struct Storage; +struct Type; +} // namespace at namespace torch { - // Register a PyTypeObject* with the given attributes void registerStoragePyTypeObject( PyTypeObject *pytype, const std::string& name, @@ -26,7 +34,6 @@ bool isStorage(PyObject* obj); THPDtype* getDtype(at::ScalarType scalarType); THPLayout* getLayout(at::Backend backend); -at::Type& getType(at::ScalarType scalarType, const THPLayout& layout, const DeviceType& deviceType); -DeviceType getDeviceType(const at::Type& type); - +at::Type& getType(at::ScalarType scalarType, const THPLayout& layout, const at::Device& device); +at::Device::Type getDeviceType(const at::Type& type); } // namespace torch diff --git a/torch/csrc/Layout.cpp b/torch/csrc/Layout.cpp index 3af53d49a71d..2aa6d7aaa7e6 100644 --- a/torch/csrc/Layout.cpp +++ b/torch/csrc/Layout.cpp @@ -1,18 +1,22 @@ -#include "Layout.h" +#include "torch/csrc/Layout.h" -#include -#include #include "torch/csrc/Exceptions.h" #include "torch/csrc/utils/object_ptr.h" #include "torch/csrc/utils/python_strings.h" -PyObject *THPLayout_New(bool is_strided, const std::string& name) +#include + +#include +#include +#include + +PyObject *THPLayout_New(at::Layout layout, const std::string& name) { auto type = (PyTypeObject*)&THPLayoutType; auto self = THPObjectPtr{type->tp_alloc(type, 0)}; if (!self) throw python_error(); auto self_ = reinterpret_cast(self.get()); - self_->is_strided = is_strided; + self_->layout = layout; std::strncpy (self_->name, name.c_str(), LAYOUT_NAME_LEN); self_->name[LAYOUT_NAME_LEN] = '\0'; return self.release(); diff --git a/torch/csrc/Layout.h b/torch/csrc/Layout.h index d96d4541c49d..a166169acff2 100644 --- a/torch/csrc/Layout.h +++ b/torch/csrc/Layout.h @@ -1,13 +1,16 @@ #pragma once #include "torch/csrc/python_headers.h" + +#include + #include const int LAYOUT_NAME_LEN = 64; struct THPLayout { PyObject_HEAD - bool is_strided; + at::Layout layout; char name[LAYOUT_NAME_LEN + 1]; }; @@ -17,6 +20,6 @@ inline bool THPLayout_Check(PyObject *obj) { return Py_TYPE(obj) == &THPLayoutType; } -PyObject * THPLayout_New(bool is_strided, const std::string& name); +PyObject * THPLayout_New(at::Layout layout, const std::string& name); void THPLayout_init(PyObject *module); diff --git a/torch/csrc/api/include/torch/functions.h b/torch/csrc/api/include/torch/functions.h new file mode 100644 index 000000000000..6faa0433c232 --- /dev/null +++ b/torch/csrc/api/include/torch/functions.h @@ -0,0 +1,3 @@ +#pragma once + +#include diff --git a/torch/csrc/api/include/torch/nn/module.h b/torch/csrc/api/include/torch/nn/module.h index 755fcef46f84..7e3f359c0dab 100644 --- a/torch/csrc/api/include/torch/nn/module.h +++ b/torch/csrc/api/include/torch/nn/module.h @@ -110,8 +110,11 @@ class Module { } protected: - autograd::Variable& register_parameter(std::string name, at::Tensor tensor); - autograd::Variable& register_buffer(std::string name, at::Tensor tensor); + Variable& register_parameter( + std::string name, + Variable tensor, + bool requires_grad = true); + Variable& register_buffer(std::string name, Variable tensor); template std::shared_ptr register_module( diff --git a/torch/csrc/api/include/torch/serialization.h b/torch/csrc/api/include/torch/serialization.h index c518c1b629c0..b1eddc3ffd5c 100644 --- a/torch/csrc/api/include/torch/serialization.h +++ b/torch/csrc/api/include/torch/serialization.h @@ -2,6 +2,9 @@ #include +#include +#include + #include "cereal/archives/binary.hpp" #include "cereal/types/polymorphic.hpp" @@ -208,13 +211,13 @@ void load(Archive& archive, at::Tensor& tensor) { at::Backend backend = ::torch::detail::backendFromId(backendId); if (!tensor.defined() || tensor.type().scalarType() != type) { - tensor = at::getType(backend, type).tensor(); + tensor = at::empty({}, at::getType(backend, type)); } tensor.resize_(sizes); if (tensor.type().is_cuda()) { // should actually use cudamemcpy probably - auto cputensor = at::CPU(tensor.type().scalarType()).tensor(sizes); + auto cputensor = at::empty(sizes, tensor.type().scalarType()); agimpl::loadBinary( archive, cputensor.data_ptr(), diff --git a/torch/csrc/api/include/torch/tensor.h b/torch/csrc/api/include/torch/tensor.h index 0cb5d3589c02..69fd0612dd68 100644 --- a/torch/csrc/api/include/torch/tensor.h +++ b/torch/csrc/api/include/torch/tensor.h @@ -4,12 +4,7 @@ #include -// This is all temporary until I create a nice torch::Tensor API. - namespace torch { +// TODO: Rename to `Tensor`. using Variable = autograd::Variable; - -inline Variable Var(at::Tensor data, bool requires_grad = true) { - return autograd::make_variable(data, requires_grad); -} } // namespace torch diff --git a/torch/csrc/api/include/torch/torch.h b/torch/csrc/api/include/torch/torch.h index 6cb3719a4dfe..723c7df48bc7 100644 --- a/torch/csrc/api/include/torch/torch.h +++ b/torch/csrc/api/include/torch/torch.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include diff --git a/torch/csrc/api/src/nn/module.cpp b/torch/csrc/api/src/nn/module.cpp index e7864fb3c7e0..bf6248f1c04d 100644 --- a/torch/csrc/api/src/nn/module.cpp +++ b/torch/csrc/api/src/nn/module.cpp @@ -148,16 +148,14 @@ void Module::zero_grad() { autograd::Variable& Module::register_parameter( std::string name, - at::Tensor tensor) { - auto variable = autograd::make_variable(tensor, /*requires_grad=*/true); - return parameters_.insert(std::move(name), std::move(variable)); + Variable tensor, + bool requires_grad) { + tensor.set_requires_grad(requires_grad); + return parameters_.insert(std::move(name), std::move(tensor)); } -autograd::Variable& Module::register_buffer( - std::string name, - at::Tensor tensor) { - auto variable = autograd::make_variable(tensor, /*requires_grad=*/false); - return parameters_.insert(std::move(name), std::move(variable)); +autograd::Variable& Module::register_buffer(std::string name, Variable tensor) { + return parameters_.insert(std::move(name), std::move(tensor)); } void Module::clone_(Module& other) {} diff --git a/torch/csrc/api/src/nn/modules/batchnorm.cpp b/torch/csrc/api/src/nn/modules/batchnorm.cpp index 31d5115d4ef3..2c8e951d3386 100644 --- a/torch/csrc/api/src/nn/modules/batchnorm.cpp +++ b/torch/csrc/api/src/nn/modules/batchnorm.cpp @@ -1,6 +1,8 @@ #include #include +#include +#include #include @@ -11,16 +13,15 @@ BatchNorm::BatchNorm(int64_t features) : features_(features) {} void BatchNorm::reset() { if (affine_) { - weight_ = register_parameter( - "weight", at::CPU(at::kFloat).empty({features_}).uniform_()); - bias_ = register_parameter("bias", at::CPU(at::kFloat).zeros({features_})); + weight_ = + register_parameter("weight", torch::empty({features_}).uniform_()); + bias_ = register_parameter("bias", torch::zeros({features_})); } if (stateful_) { - running_mean_ = - register_buffer("running_mean", at::CPU(at::kFloat).zeros({features_})); - running_variance_ = register_buffer( - "running_variance", at::CPU(at::kFloat).ones({features_})); + running_mean_ = register_buffer("running_mean", torch::zeros({features_})); + running_variance_ = + register_buffer("running_variance", torch::ones({features_})); } } diff --git a/torch/csrc/api/src/nn/modules/conv.cpp b/torch/csrc/api/src/nn/modules/conv.cpp index b709a171e1f1..51514f0ae031 100644 --- a/torch/csrc/api/src/nn/modules/conv.cpp +++ b/torch/csrc/api/src/nn/modules/conv.cpp @@ -1,6 +1,8 @@ #include #include +#include +#include #include @@ -41,11 +43,9 @@ void Conv::reset() { weights_size.end(), kernel_size_->begin(), kernel_size_->end()); AT_ASSERT(weights_size.size() == 2 + kernel_size_->size()); - weight_ = this->register_parameter( - "weight", at::CPU(at::kFloat).empty(weights_size)); + weight_ = this->register_parameter("weight", torch::empty(weights_size)); if (with_bias_) { - bias_ = this->register_parameter( - "bias", at::CPU(at::kFloat).empty(output_channels_)); + bias_ = this->register_parameter("bias", torch::empty(output_channels_)); } const auto number_of_features = std::accumulate( diff --git a/torch/csrc/api/src/nn/modules/dropout.cpp b/torch/csrc/api/src/nn/modules/dropout.cpp index 8eac9dbe2950..73b4f8f9ba27 100644 --- a/torch/csrc/api/src/nn/modules/dropout.cpp +++ b/torch/csrc/api/src/nn/modules/dropout.cpp @@ -1,5 +1,8 @@ #include +#include +#include + namespace torch { namespace detail { @@ -37,7 +40,8 @@ Variable Dropout::noise_mask(Variable input) const { } Variable Dropout2d::noise_mask(Variable input) const { - return input.type().empty({input.size(0), input.size(1), 1, 1}); + return torch::empty( + {input.size(0), input.size(1), 1, 1}, at::TensorOptions(input)); } } // namespace nn } // namespace torch diff --git a/torch/csrc/api/src/nn/modules/embedding.cpp b/torch/csrc/api/src/nn/modules/embedding.cpp index 6d6ae419b771..94131e86094d 100644 --- a/torch/csrc/api/src/nn/modules/embedding.cpp +++ b/torch/csrc/api/src/nn/modules/embedding.cpp @@ -1,5 +1,8 @@ #include +#include +#include + #include namespace torch { @@ -9,8 +12,7 @@ Embedding::Embedding(int64_t count, int64_t dimension) : count_(count), dimension_(dimension) {} void Embedding::reset() { - table_ = register_parameter( - "table", at::CPU(at::kFloat).empty({count_, dimension_})); + table_ = register_parameter("table", torch::empty({count_, dimension_})); table_.data().normal_(0, 1); } diff --git a/torch/csrc/api/src/nn/modules/linear.cpp b/torch/csrc/api/src/nn/modules/linear.cpp index 7a704ac8322a..86747bf1a1aa 100644 --- a/torch/csrc/api/src/nn/modules/linear.cpp +++ b/torch/csrc/api/src/nn/modules/linear.cpp @@ -1,5 +1,8 @@ #include +#include +#include + #include #include @@ -13,8 +16,8 @@ Linear::Linear(size_t features_in, size_t features_out) void Linear::reset() { weight_ = - register_parameter("weight", at::CPU(at::kFloat).empty({out_, in_})); - bias_ = register_parameter("bias", at::CPU(at::kFloat).empty(out_)); + register_parameter("weight", torch::empty({out_, in_})); + bias_ = register_parameter("bias", torch::empty(out_)); const auto stdv = 1.0 / std::sqrt(weight_.size(1)); for (auto& p : this->parameters()) { diff --git a/torch/csrc/api/src/nn/modules/rnn.cpp b/torch/csrc/api/src/nn/modules/rnn.cpp index 2db64ab76c65..8d40bedbb91e 100644 --- a/torch/csrc/api/src/nn/modules/rnn.cpp +++ b/torch/csrc/api/src/nn/modules/rnn.cpp @@ -1,6 +1,8 @@ #include +#include #include +#include #include #include @@ -68,18 +70,16 @@ void RNNBase::reset() { const int64_t input_size = (layer == 0) ? input_size_ : hidden_size_; ihw_[layer] = this->register_parameter( "weight_ih_l" + std::to_string(layer), - at::CPU(at::kFloat).empty({gate_size, input_size})); + torch::empty({gate_size, input_size})); hhw_[layer] = this->register_parameter( "weight_hh_l" + std::to_string(layer), - at::CPU(at::kFloat).empty({gate_size, hidden_size_})); + torch::empty({gate_size, hidden_size_})); if (with_bias_) { ihb_[layer] = this->register_parameter( - "bias_ih_l" + std::to_string(layer), - at::CPU(at::kFloat).empty({gate_size})); + "bias_ih_l" + std::to_string(layer), torch::empty({gate_size})); hhb_[layer] = this->register_parameter( - "bias_hh_l" + std::to_string(layer), - at::CPU(at::kFloat).empty({gate_size})); + "bias_hh_l" + std::to_string(layer), torch::empty({gate_size})); } } flatten_parameters_for_cudnn(); @@ -129,8 +129,8 @@ std::vector RNNBase::autograd_forward( has_hidden ? inputs[1].select(layer_dimension, layer) : Variable()); } - auto output = - Variable(inp.type().zeros({inp.size(0), inp.size(1), hidden_size_})); + auto output = torch::zeros( + {inp.size(0), inp.size(1), hidden_size_}, at::TensorOptions(inp)); for (int64_t t = 0; t < inp.size(0); t++) { auto x = inp.select(0, t); for (int64_t i = 0; i < layers_; i++) { @@ -210,12 +210,13 @@ std::vector RNNBase::CUDNN_forward( hx = inputs[1]; } } else { - hx = x.type().zeros({layers_, x.size(1), hidden_size_}); + hx = torch::zeros({layers_, x.size(1), hidden_size_}, at::TensorOptions(x)); if (has_cell_state_) { - cx = x.type().zeros({layers_, x.size(1), hidden_size_}); + cx = torch::zeros( + {layers_, x.size(1), hidden_size_}, at::TensorOptions(x)); } } - auto dropout_state = x.type().tensor(); + auto dropout_state = torch::empty({}, x.type()); std::vector weight_data_ptrs; auto params = this->parameters(); @@ -298,8 +299,9 @@ std::vector LSTM::cell_forward( std::vector inputs, int64_t layer) { auto x = inputs[0]; - auto hid = inputs[1].defined() ? inputs[1] - : x.type().zeros({2, x.size(0), hidden_size_}); + auto hid = inputs[1].defined() + ? inputs[1] + : torch::zeros({2, x.size(0), hidden_size_}, torch::TensorOptions(x)); auto hx = hid[0]; auto cx = hid[1]; @@ -327,8 +329,9 @@ std::vector GRU::cell_forward( std::vector inputs, int64_t layer) { auto x = inputs[0]; - auto hx = inputs[1].defined() ? inputs[1] - : x.type().zeros({x.size(0), hidden_size_}); + auto hx = inputs[1].defined() + ? inputs[1] + : torch::zeros({x.size(0), hidden_size_}, torch::TensorOptions(x)); auto gi = linear(x, ihw_[layer], ihb_[layer]); auto gh = linear(x, hhw_[layer], hhb_[layer]); @@ -367,8 +370,9 @@ std::vector RNN::cell_forward( std::vector inputs, int64_t layer) { auto x = inputs[0]; - auto hx = inputs[1].defined() ? inputs[1] - : x.type().zeros({x.size(0), hidden_size_}); + auto hx = inputs[1].defined() + ? inputs[1] + : torch::zeros({x.size(0), hidden_size_}, torch::TensorOptions(x)); auto h = linear(x, ihw_[layer], ihb_[layer]) + linear(hx, hhw_[layer], hhb_[layer]); diff --git a/torch/csrc/api/src/optimizers.cpp b/torch/csrc/api/src/optimizers.cpp index aeb1450b11dc..ce4ef29e45c5 100644 --- a/torch/csrc/api/src/optimizers.cpp +++ b/torch/csrc/api/src/optimizers.cpp @@ -1,6 +1,8 @@ #include "torch/optimizers.h" #include +#include +#include namespace torch { @@ -158,10 +160,10 @@ at::Scalar LBFGS::step(std::function closure) { } void LBFGS::init_state() { - d = at::CPU(at::kFloat).empty({0}); + d = torch::empty({0}); t = 0; - H_diag = at::CPU(at::kFloat).empty({0}); - prev_flat_grad = at::CPU(at::kFloat).empty({0}); + H_diag = torch::empty({0}); + prev_flat_grad = torch::empty({0}); prev_loss = 0; ro.resize(history_size_); al.resize(history_size_); diff --git a/torch/csrc/autograd/engine.cpp b/torch/csrc/autograd/engine.cpp index d0d0485f24ba..666486dc0db3 100644 --- a/torch/csrc/autograd/engine.cpp +++ b/torch/csrc/autograd/engine.cpp @@ -5,7 +5,8 @@ #include "torch/csrc/autograd/grad_mode.h" #include "torch/csrc/autograd/anomaly_mode.h" #include "torch/csrc/autograd/variable.h" -#include "torch/csrc/utils/auto_gpu.h" + +#include #include #include @@ -204,7 +205,7 @@ Engine::~Engine() = default; auto Engine::thread_init(int device) -> void { THInferNumThreads(); - AutoGPU guard(device); + at::DeviceGuard guard(device); worker_device = device; thread_main(nullptr); } @@ -382,7 +383,7 @@ auto Engine::evaluate_function(FunctionTask& task) -> void { AutoGradMode grad_mode(false); for (int i = 0; i < num_outputs; ++i) { auto& output = outputs[i]; - AutoGPU guard(output); + at::DeviceGuard guard(output); if (output.defined() && output.ne(output).any().toCByte()) { std::stringstream ss; ss << "Function '" << fn.name() << "' returned nan values in its " << i << "th output."; diff --git a/torch/csrc/autograd/functions/accumulate_grad.cpp b/torch/csrc/autograd/functions/accumulate_grad.cpp index fdbe54d3fc3e..11e1e77a3858 100644 --- a/torch/csrc/autograd/functions/accumulate_grad.cpp +++ b/torch/csrc/autograd/functions/accumulate_grad.cpp @@ -5,9 +5,10 @@ #include "torch/csrc/autograd/functions/basic_ops.h" #include "torch/csrc/autograd/functions/tensor.h" #include "torch/csrc/autograd/functions/utils.h" -#include "torch/csrc/utils/auto_gpu.h" #include +#include +#include using at::Tensor; diff --git a/torch/csrc/autograd/functions/basic_ops.cpp b/torch/csrc/autograd/functions/basic_ops.cpp index a576d2009a8a..fb5186599ded 100644 --- a/torch/csrc/autograd/functions/basic_ops.cpp +++ b/torch/csrc/autograd/functions/basic_ops.cpp @@ -3,7 +3,6 @@ #include "torch/csrc/autograd/function.h" #include "torch/csrc/autograd/variable.h" #include "torch/csrc/autograd/functions/utils.h" -#include "torch/csrc/utils/auto_gpu.h" #include diff --git a/torch/csrc/autograd/functions/tensor.cpp b/torch/csrc/autograd/functions/tensor.cpp index 5df72d526306..8d5f720e4a80 100644 --- a/torch/csrc/autograd/functions/tensor.cpp +++ b/torch/csrc/autograd/functions/tensor.cpp @@ -1,15 +1,16 @@ #include "torch/csrc/autograd/functions/tensor.h" #include "torch/csrc/autograd/function.h" -#include "torch/csrc/autograd/variable.h" #include "torch/csrc/autograd/functions/basic_ops.h" #include "torch/csrc/autograd/functions/utils.h" #include "torch/csrc/autograd/generated/Functions.h" #include "torch/csrc/autograd/variable.h" -#include "torch/csrc/utils/auto_gpu.h" -#include +#include + +#include #include +#include #include namespace torch { namespace autograd { @@ -22,7 +23,7 @@ auto CopyBackwards::apply(const variable_list& grads) -> variable_list { grad_inputs[0] = at::zeros_like(grad); } if (should_compute_output(1)) { - AutoGPU autoGPU(src_device); + at::DeviceGuard device_guard(src_device); if (grad.is_cuda() && grad.get_device() != src_device) { grad_inputs[1] = src_type->copy(grad); } else { diff --git a/torch/csrc/autograd/functions/tensor.h b/torch/csrc/autograd/functions/tensor.h index 22b8b8d17a8c..923c8c4319cd 100644 --- a/torch/csrc/autograd/functions/tensor.h +++ b/torch/csrc/autograd/functions/tensor.h @@ -5,6 +5,7 @@ #include "ATen/Type.h" #include +#include #include #include @@ -15,7 +16,7 @@ struct CopyBackwards : public Function { virtual variable_list apply(const variable_list& inputs) override; at::Type *src_type; - int64_t src_device; + int32_t src_device = -1; }; // Performs grad[idx] = fn(grad[idx]), but out-of-place. The slicing operation diff --git a/torch/csrc/autograd/input_buffer.cpp b/torch/csrc/autograd/input_buffer.cpp index e8aecc120f8e..78ccc8bcc285 100644 --- a/torch/csrc/autograd/input_buffer.cpp +++ b/torch/csrc/autograd/input_buffer.cpp @@ -2,7 +2,12 @@ #include "torch/csrc/assertions.h" #include "torch/csrc/autograd/functions/basic_ops.h" -#include "torch/csrc/utils/auto_gpu.h" + +#include + +#include +#include +#include namespace torch { namespace autograd { @@ -16,7 +21,7 @@ void InputBuffer::add(size_t pos, Variable var) { if (!old_var.defined()) { buffer[pos] = std::move(var); } else { - AutoGPU auto_gpu(var); + at::DeviceGuard device_guard(var); // ATen doesn't route sparse additions correctly... if (old_var.type().is_sparse()) { buffer[pos] = var + old_var; diff --git a/torch/csrc/autograd/profiler.cpp b/torch/csrc/autograd/profiler.cpp index ad861959f032..d9f2f37adc16 100644 --- a/torch/csrc/autograd/profiler.cpp +++ b/torch/csrc/autograd/profiler.cpp @@ -16,11 +16,11 @@ void RecordFunction::pushFunctionRange(Function* fn) { #ifdef USE_CUDA static void onEachDevice(std::function op) { - AutoGPU gpu_guard; + at::DeviceGuard device_guard; int count; TORCH_CUDA_CHECK(cudaGetDeviceCount(&count)); for(int i = 0; i < count; i++) { - gpu_guard.setDevice(i); + device_guard.set_index(i); op(i); } } diff --git a/torch/csrc/autograd/python_function.cpp b/torch/csrc/autograd/python_function.cpp index c30d25347ba6..8944e2939f3a 100644 --- a/torch/csrc/autograd/python_function.cpp +++ b/torch/csrc/autograd/python_function.cpp @@ -20,9 +20,17 @@ #include "torch/csrc/jit/python_tracer.h" #include "torch/csrc/DynamicTypes.h" #include "torch/csrc/utils/auto_gil.h" -#include "torch/csrc/utils/auto_gpu.h" #include "torch/csrc/Exceptions.h" +#include +#include +#include +#include +#include +#include +#include +#include + using namespace torch; using namespace torch::autograd; using namespace torch::jit; @@ -37,7 +45,6 @@ namespace torch { namespace autograd { VariableInfo::VariableInfo(const Variable& var) : type(&var.type()) - , device(-1) , size(var.sizes()) , requires_grad(var.requires_grad()) { if (var.type().is_cuda()) { @@ -45,9 +52,9 @@ VariableInfo::VariableInfo(const Variable& var) } } -Variable VariableInfo::zeros(AutoGPU& gpu_guard) const { - gpu_guard.setDevice(device); - return at::zeros(*type, size); +Variable VariableInfo::zeros(at::DeviceGuard& device_guard) const { + device_guard.set_index(device); + return at::zeros(size, *type); } auto PyFunction::legacy_apply(const variable_list& inputs) -> variable_list { @@ -99,7 +106,7 @@ auto PyFunction::legacy_apply(const variable_list& inputs) -> variable_list { // C++'s Function::apply to a Python method "apply". auto PyFunction::apply(const variable_list& inputs) -> variable_list { AutoGIL gil; - AutoGPU _gpu_guard(-1); + at::DeviceGuard _device_guard; THPFunction* py_fn = (THPFunction*)obj; THPObjectPtr _legacy(PyObject_GetAttrString(obj, "_is_legacy")); @@ -117,7 +124,7 @@ auto PyFunction::apply(const variable_list& inputs) -> variable_list { if (inputs[i].defined()) { input = THPVariable_Wrap(inputs[i]); } else { - input = THPVariable_Wrap(output_info[i].zeros(_gpu_guard)); + input = THPVariable_Wrap(output_info[i].zeros(_device_guard)); } if (!input) throw python_error(); PyTuple_SET_ITEM(pyInputs.get(), i, input); @@ -174,7 +181,7 @@ auto PyFunction::apply(const variable_list& inputs) -> variable_list { if (output == Py_None) { auto& info = input_info[results.size()]; if (info.requires_grad) { - results.emplace_back(info.zeros(_gpu_guard)); + results.emplace_back(info.zeros(_device_guard)); } else { results.emplace_back(); } @@ -751,7 +758,7 @@ PyObject *THPFunction_apply(PyObject *cls, PyObject *inputs) static void _prepare_grads(THPFunction *self, THPObjectPtr& raw_grads, bool is_grad_output) { - AutoGPU gpu_guard(-1); + at::DeviceGuard device_guard; int num_grads = PyTuple_GET_SIZE(raw_grads.get()); // First, check if any of grads is None. If not, there's nothing to do bool has_none = false; @@ -771,7 +778,7 @@ static void _prepare_grads(THPFunction *self, THPObjectPtr& raw_grads, bool is_g for (int i = 0; i < num_grads; i++) { PyObject *grad = PyTuple_GET_ITEM(raw_grads.get(), i); if (grad == Py_None) { - grad = THPVariable_Wrap(grads_info[i].zeros(gpu_guard)); + grad = THPVariable_Wrap(grads_info[i].zeros(device_guard)); if (!grad) throw python_error(); } else { Py_INCREF(grad); diff --git a/torch/csrc/autograd/python_function.h b/torch/csrc/autograd/python_function.h index 529bcaf4546a..0eaf63a64ed3 100644 --- a/torch/csrc/autograd/python_function.h +++ b/torch/csrc/autograd/python_function.h @@ -1,9 +1,6 @@ #pragma once #include "torch/csrc/python_headers.h" -#include -#include -#include #include "torch/csrc/Exceptions.h" #include "torch/csrc/autograd/function.h" @@ -11,6 +8,15 @@ #include "torch/csrc/autograd/saved_variable.h" #include "torch/csrc/utils/object_ptr.h" +#include + +#include +#include +#include + +namespace at { +struct DeviceGuard; +} // namespace at namespace torch { namespace jit { struct Graph; }} namespace torch { namespace autograd { @@ -18,10 +24,10 @@ namespace torch { namespace autograd { struct VariableInfo { explicit VariableInfo(const Variable& var); - Variable zeros(AutoGPU& gpu_guard) const; + Variable zeros(at::DeviceGuard& device_guard) const; at::Type* type; - int device; + int32_t device = -1; std::vector size; bool requires_grad; }; diff --git a/torch/csrc/autograd/python_variable.cpp b/torch/csrc/autograd/python_variable.cpp index f5be308e1434..c6bed929bad5 100644 --- a/torch/csrc/autograd/python_variable.cpp +++ b/torch/csrc/autograd/python_variable.cpp @@ -1,9 +1,9 @@ #include "torch/csrc/autograd/python_variable.h" #include "THP.h" -#include "torch/csrc/Device.h" #include "torch/csrc/DynamicTypes.h" #include "torch/csrc/Exceptions.h" +#include "torch/csrc/Device.h" #include "torch/csrc/Size.h" #include "torch/csrc/Types.h" #include "torch/csrc/autograd/edge.h" @@ -26,10 +26,10 @@ #include -#include -#include #include -#include +#include +#include +#include using namespace at; using namespace torch; diff --git a/torch/csrc/autograd/python_variable_indexing.cpp b/torch/csrc/autograd/python_variable_indexing.cpp index 0ad0ae4f4a07..7754e3f7498b 100644 --- a/torch/csrc/autograd/python_variable_indexing.cpp +++ b/torch/csrc/autograd/python_variable_indexing.cpp @@ -12,8 +12,12 @@ #include "torch/csrc/utils/tensor_new.h" #include "torch/csrc/utils/tensor_conversion_dispatch.h" +#include #include +#include + #include +#include using namespace at; using namespace torch::autograd::utils; @@ -177,7 +181,7 @@ static Variable applySlicing(const Variable& self, PyObject* index, variable_lis static std::vector typeConvertIndices(const Variable& self, const variable_list& indices) { std::vector converted_inds(indices.size()); - int64_t device = self.is_cuda() ? self.get_device() : -1; + int32_t device = self.is_cuda() ? self.get_device() : -1; for (size_t i = 0; i < indices.size(); ++i) { const auto &ind = indices[i]; if (ind.defined()) { @@ -193,14 +197,14 @@ static std::vector typeConvertIndices(const Variable& self, const variab static Variable dispatch_index(const Variable& self, const variable_list& indices) { std::vector converted_indices = typeConvertIndices(self, indices); AutoNoGIL no_gil; - AutoGPU auto_gpu(self); + DeviceGuard device_guard(self); return self.index(converted_indices); } static Variable dispatch_index_put_(Variable& self, const variable_list& indices, const Variable& value) { std::vector converted_indices = typeConvertIndices(self, indices); AutoNoGIL no_gil; - AutoGPU auto_gpu(self); + DeviceGuard device_guard(self); return self.index_put_(converted_indices, value); } @@ -263,14 +267,14 @@ static PyObject* applyBoolGetitem(const Variable& self, bool index) { if (index) { return wrap(self.type().copy(self.unsqueeze(0))); } else { - return wrap(self.type().tensor({0})); + return wrap(at::empty({0}, at::TensorOptions(self))); } } PyObject* THPVariable_getitem(PyObject* self, PyObject* index) { HANDLE_TH_ERRORS auto& self_ = reinterpret_cast(self)->cdata; - AutoGPU auto_gpu(self_); + DeviceGuard device_guard(self_); // handle simple types: integers, slices, ellipsis if (index == Py_None) { @@ -329,7 +333,7 @@ static void copy_to(Variable dst, const Variable& src) { int THPVariable_setitem(PyObject* self, PyObject* index, PyObject* py_value) { HANDLE_TH_ERRORS auto& self_ = reinterpret_cast(self)->cdata; - AutoGPU auto_gpu(self_); + DeviceGuard device_guard(self_); auto value = valueToTensor(self_.type(), py_value); // handle simple types: integers, slices, ellipsis, bool diff --git a/torch/csrc/autograd/utils/python_arg_parsing.h b/torch/csrc/autograd/utils/python_arg_parsing.h index f4ce991c2a81..144a4fe7dd7b 100644 --- a/torch/csrc/autograd/utils/python_arg_parsing.h +++ b/torch/csrc/autograd/utils/python_arg_parsing.h @@ -4,11 +4,10 @@ #include #include "torch/csrc/utils/python_arg_parser.h" -#include "torch/csrc/utils/device.h" namespace torch { namespace autograd { namespace utils { -inline std::tuple, at::optional, bool> +inline std::tuple, at::optional, bool> parse_to_conversion(PyObject *args, PyObject *kwargs) { static PythonArgParser parser({ "to(Device device=None, ScalarType dtype=None, bool non_blocking=False)", diff --git a/torch/csrc/autograd/utils/python_variables.h b/torch/csrc/autograd/utils/python_variables.h deleted file mode 100644 index 5f96835eaba0..000000000000 --- a/torch/csrc/autograd/utils/python_variables.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -#include -#include "torch/csrc/autograd/python_variable.h" - -namespace torch { namespace autograd { namespace utils { - -inline at::Tensor set_requires_grad(at::Tensor self, bool requires_grad) { - if (requires_grad && !at::isFloatingType(self.type().scalarType())) { - throw std::runtime_error("only Tensors of floating point dtype can require gradients"); - } - as_variable_ref(self).set_requires_grad(requires_grad); - return self; -} - -}}} // namespace torch::autograd::utils diff --git a/torch/csrc/autograd/variable.cpp b/torch/csrc/autograd/variable.cpp index 48a35f4f8879..a271febf5a34 100644 --- a/torch/csrc/autograd/variable.cpp +++ b/torch/csrc/autograd/variable.cpp @@ -26,10 +26,12 @@ Variable::Impl::Impl(at::Tensor data, bool requires_grad, Edge gradient_edge) : TensorImpl(VariableType::getType(data)), data_(std::move(data)), grad_fn_(std::move(gradient_edge.function)), - requires_grad_(requires_grad), + requires_grad_(false), is_view_(false), output_nr_(gradient_edge.input_nr), pyobj_(nullptr) { + // set_requires_grad also checks error conditions. + set_requires_grad(requires_grad); TORCH_ASSERTM( !grad_fn_ || !requires_grad_, "requires_grad should be false if grad_fn is set"); diff --git a/torch/csrc/autograd/variable.h b/torch/csrc/autograd/variable.h index baad19fe5128..a0a6d05a5c49 100644 --- a/torch/csrc/autograd/variable.h +++ b/torch/csrc/autograd/variable.h @@ -9,6 +9,7 @@ #include "torch/csrc/utils/auto_unique_ptr.h" #include +#include #include #include @@ -308,6 +309,9 @@ struct Variable::Impl : public at::TensorImpl { /// leaf variables that want to accumulate gradients, and false for all other /// variables. void set_requires_grad(bool requires_grad) override { + AT_CHECK( + !requires_grad || at::isFloatingType(type().scalarType()), + "Only Tensors of floating point dtype can require gradients"); requires_grad_ = requires_grad; } diff --git a/torch/csrc/cuda/comm.cpp b/torch/csrc/cuda/comm.cpp index 4e5950853445..12188554f447 100644 --- a/torch/csrc/cuda/comm.cpp +++ b/torch/csrc/cuda/comm.cpp @@ -1,7 +1,6 @@ #include "comm.h" #include "torch/csrc/utils/tensor_flatten.h" -#include "torch/csrc/utils/auto_gpu.h" #include "torch/csrc/cuda/device_set.h" #ifdef USE_NCCL #include "torch/csrc/cuda/nccl.h" @@ -9,6 +8,8 @@ #include +#include + namespace torch { namespace cuda { using namespace at; @@ -38,7 +39,7 @@ std::vector broadcast(const Tensor& tensor, IntList devices) { if (nccl::is_available({tensor})) { tensors.push_back(tensor); for (auto device : devices.slice(1)) { - AutoGPU _gpu_guard(device); + at::DeviceGuard _device_guard(device); tensors.push_back(type.tensor(tensor.sizes())); } nccl::broadcast(tensors); @@ -48,7 +49,7 @@ std::vector broadcast(const Tensor& tensor, IntList devices) { #endif auto & gpu_type = type.toBackend(type.is_sparse() ? at::kSparseCUDA : at::kCUDA); for (auto device : devices) { - AutoGPU _gpu_guard(device); + at::DeviceGuard _device_guard(device); tensors.push_back(gpu_type.copy(tensor, true)); } } @@ -77,7 +78,7 @@ tensor_list2d broadcast_coalesced(TensorList tensors, IntList devices, size_t bu std::vector broadcast_values = broadcast(flat_tuple.second, devices); results.reserve(devices.size()); for (size_t i = 1, num_devices = devices.size(); i < num_devices; ++i) { - AutoGPU auto_gpu(devices[i]); + at::DeviceGuard device_guard(devices[i]); auto & device_outputs = outputs[i]; auto & inds = broadcast_indices[i]; auto & vals = broadcast_values[i]; @@ -85,11 +86,11 @@ tensor_list2d broadcast_coalesced(TensorList tensors, IntList devices, size_t bu device_outputs.push_back(std::move(t)); } } else { - AutoGPU auto_gpu(devices[0]); + at::DeviceGuard device_guard(devices[0]); std::vector results = broadcast(utils::flatten_dense_tensors(chunk.tensors), devices); for (size_t i = 1, num_devices = devices.size(); i < num_devices; ++i) { - auto_gpu.setDevice(devices[i]); + device_guard.set_index(devices[i]); auto & device_outputs = outputs[i]; for (auto & t : utils::unflatten_dense_tensors(results[i], chunk.tensors)) device_outputs.push_back(std::move(t)); diff --git a/torch/csrc/cuda/nccl.cpp b/torch/csrc/cuda/nccl.cpp index d93347892ec9..dd6ab884daf0 100644 --- a/torch/csrc/cuda/nccl.cpp +++ b/torch/csrc/cuda/nccl.cpp @@ -1,7 +1,6 @@ #include "nccl.h" #include "torch/csrc/cuda/device_set.h" #include "torch/csrc/utils/functional.h" -#include "torch/csrc/utils/auto_gpu.h" #include "torch/csrc/utils/hash.h" #include @@ -190,10 +189,10 @@ void broadcast(TensorList tensors, const stream_list& streams, const comm_list& std::lock_guard free_mutex(*(THCCachingAllocator_getCudaFreeMutex())); const auto comms = user_comms.empty() ? _get_communicators(tensors) : ArrayRef(user_comms); - AutoGPU gpu_guard; + at::DeviceGuard device_guard; AutoNcclGroup nccl_group_guard; for (size_t i = 0, num_tensors = tensors.size(); i < num_tensors; i++) { - gpu_guard.setDevice(tensors[i].get_device()); + device_guard.set_index(tensors[i].get_device()); // TODO: use current stream const auto stream = (streams.empty() || !streams[i]) ? NULL : streams[i]->stream; CHECK(ncclBcast(tensors[i].data_ptr(), numel, data_type, 0, comms[i], stream)); diff --git a/torch/csrc/cuda/python_nccl.cpp b/torch/csrc/cuda/python_nccl.cpp index f99de6448034..13aff2a1267f 100644 --- a/torch/csrc/cuda/python_nccl.cpp +++ b/torch/csrc/cuda/python_nccl.cpp @@ -143,11 +143,11 @@ PyObject * THCPModule_nccl_reduce(PyObject *self, PyObject *args) { int64_t count = inputs[0].numel(); std::lock_guard lock(*(THCCachingAllocator_getCudaFreeMutex())); auto comms = user_comms.empty() ? _get_communicators(inputs) : ArrayRef(user_comms); - AutoGPU gpu_guard; + at::DeviceGuard device_guard; AutoNcclGroup nccl_group_guard; for (size_t i = 0; i < len; i++) { int device = inputs[i].get_device(); - gpu_guard.setDevice(device); + device_guard.set_index(device); auto stream = (streams[i] == NULL) ? NULL : THCStream_stream(streams[i]); CHECK(ncclReduce(inputs[i].data_ptr(), outputs[i].data_ptr(), count, data_type, (ncclRedOp_t) op, root, comms[i], stream)); @@ -185,11 +185,11 @@ PyObject * THCPModule_nccl_all_reduce(PyObject *self, PyObject *args) { int64_t count = inputs[0].numel(); std::lock_guard lock(*(THCCachingAllocator_getCudaFreeMutex())); auto comms = user_comms.empty() ? _get_communicators(inputs) : ArrayRef(user_comms); - AutoGPU gpu_guard; + at::DeviceGuard device_guard; AutoNcclGroup nccl_group_guard; for (size_t i = 0; i < len; i++) { int device = inputs[i].get_device(); - gpu_guard.setDevice(device); + device_guard.set_index(device); auto stream = (streams[i] == NULL) ? NULL : THCStream_stream(streams[i]); CHECK(ncclAllReduce(inputs[i].data_ptr(), outputs[i].data_ptr(), count, data_type, (ncclRedOp_t) op, comms[i], stream)); @@ -248,11 +248,11 @@ PyObject * THCPModule_nccl_all_gather(PyObject *self, PyObject *args) { int64_t count = inputs[0].numel(); std::lock_guard lock(*(THCCachingAllocator_getCudaFreeMutex())); auto comms = user_comms.empty() ? _get_communicators(inputs) : ArrayRef(user_comms); - AutoGPU gpu_guard; + at::DeviceGuard device_guard; AutoNcclGroup nccl_group_guard; for (size_t i = 0; i < len; i++) { int device = inputs[i].get_device(); - gpu_guard.setDevice(device); + device_guard.set_index(device); auto stream = (streams[i] == NULL) ? NULL : THCStream_stream(streams[i]); #if defined(NCCL_MAJOR) && (NCCL_MAJOR >= 2) CHECK(ncclAllGather(inputs[i].data_ptr(), outputs[i].data_ptr(), @@ -293,11 +293,11 @@ PyObject * THCPModule_nccl_reduce_scatter(PyObject *self, PyObject *args) { int64_t count = inputs[0].numel() / len; std::lock_guard lock(*(THCCachingAllocator_getCudaFreeMutex())); auto comms = user_comms.empty() ? _get_communicators(inputs) : ArrayRef(user_comms); - AutoGPU gpu_guard; + at::DeviceGuard device_guard; AutoNcclGroup nccl_group_guard; for (size_t i = 0; i < len; i++) { int device = inputs[i].get_device(); - gpu_guard.setDevice(device); + device_guard.set_index(device); auto stream = (streams[i] == NULL) ? NULL : THCStream_stream(streams[i]); CHECK(ncclReduceScatter(inputs[i].data_ptr(), outputs[i].data_ptr(), count, data_type, (ncclRedOp_t) op, comms[i], stream)); diff --git a/torch/csrc/generic/StorageSharing.cpp b/torch/csrc/generic/StorageSharing.cpp index e9fbc47b2dd9..302051581eae 100644 --- a/torch/csrc/generic/StorageSharing.cpp +++ b/torch/csrc/generic/StorageSharing.cpp @@ -237,7 +237,7 @@ static PyObject * THPStorage_(shareCuda)(THPStorage *self) { HANDLE_TH_ERRORS THWStorage *storage = self->cdata; - AutoGPU gpu_guard(storage->device); + at::DeviceGuard device_guard(storage->device); THPObjectPtr tuple(PyTuple_New(5)); THPObjectPtr device(PyLong_FromLong(storage->device)); THPObjectPtr _handle(Py_None); @@ -291,7 +291,7 @@ static PyObject * THPStorage_(newSharedCuda)(PyObject *_unused, PyObject *args) size_t view_size = (size_t)THPUtils_unpackLong(_view_size); int64_t device = THPUtils_unpackLong(_device); - AutoGPU __autogpu(device); + at::DeviceGuard device_guard(device); char *buffer; Py_ssize_t handle_size; diff --git a/torch/csrc/jit/autodiff.cpp b/torch/csrc/jit/autodiff.cpp index 625a554960a5..40401460e762 100644 --- a/torch/csrc/jit/autodiff.cpp +++ b/torch/csrc/jit/autodiff.cpp @@ -3,7 +3,6 @@ #include "torch/csrc/jit/passes/dead_code_elimination.h" #include "torch/csrc/jit/symbolic_variable.h" #include "torch/csrc/utils/functional.h" -#include "torch/csrc/utils/auto_gpu.h" #include @@ -228,10 +227,10 @@ static Value* createZerosLike(Value *v) { JIT_EXPECTM(v->isTensor(), "can't allocate zero gradient for a value without a type"); Graph *graph = v->owningGraph(); auto type = v->type()->expect(); - AutoGPU gpu_guard(type->device()); + at::DeviceGuard device_guard(type->device()); auto & at_type = type->device() == -1 ? at::CPU(type->scalarType()) : at::CUDA(type->scalarType()); - auto zeros = at::zeros(at_type, {}).expand(type->sizes()); + auto zeros = at::zeros({}, at_type).expand(type->sizes()); Node *constant = graph->createConstant(zeros) ->i_(attr::is_zero, 1); graph->insertNode(constant); diff --git a/torch/csrc/jit/fusion_compiler.cpp b/torch/csrc/jit/fusion_compiler.cpp index f1264d76f1b2..83faba76b3b3 100644 --- a/torch/csrc/jit/fusion_compiler.cpp +++ b/torch/csrc/jit/fusion_compiler.cpp @@ -66,10 +66,10 @@ so typedefs help it handle those cases*/ auto type_declarations_template = CodeTemplate(R"( #if defined(__CUDACC_RTC__) -typedef unsigned char uint8_t; -typedef signed char int8_t; -typedef short int int16_t; -typedef long long int int64_t; +typedef unsigned char uint8_t; +typedef signed char int8_t; +typedef short int int16_t; +typedef long long int int64_t; #endif typedef ${IndexType} IndexType; template @@ -424,7 +424,7 @@ void compressContiguous( } // anonymous namespace void CompiledFusionFunction::launch_with_tensors(at::ArrayRef inputs, at::ArrayRef outputs) { - AutoGPU gpu_guard(inputs); + at::DeviceGuard device_guard(inputs); JIT_ASSERT(inputs.size() == input_desc.size()); JIT_ASSERT(outputs.size() == output_desc.size()); size_t flat_outputs_size = 0; @@ -484,7 +484,7 @@ void CompiledFusionFunction::launch_with_tensors(at::ArrayRef inputs } void CompiledFusionFunction::launch(at::ArrayRef inputs, std::vector & outputs) { - AutoGPU guard(inputs.back()); + at::DeviceGuard guard(inputs.back()); outputs.clear(); outputs.reserve(outputDescriptors().size()); for(auto & od : outputDescriptors()) { @@ -509,7 +509,7 @@ void checkCUDAVersion(const cudaDeviceProp & prop) { struct CUDAFusionFunction : public CompiledFusionFunction { CUDAFusionFunction(const std::string & name, AnnotatedGraph & agraph) : CompiledFusionFunction(name, agraph) { - AutoGPU gpu_guard(agraph.device); + at::DeviceGuard device_guard(agraph.device); TORCH_CUDA_CHECK(cudaGetDeviceProperties(&prop, agraph.device)); checkCUDAVersion(prop); diff --git a/torch/csrc/jit/init.cpp b/torch/csrc/jit/init.cpp index b7514465db0c..7d43107b68fe 100644 --- a/torch/csrc/jit/init.cpp +++ b/torch/csrc/jit/init.cpp @@ -168,11 +168,11 @@ void initJITBindings(PyObject *module) { if (outputs.size() == 0) { return py::none(); } else if (outputs.size() == 1) { - return py::cast(static_cast(outputs[0])); + return py::cast(autograd::as_variable_ref(outputs[0])); } else { py::tuple tuple(outputs.size()); for(size_t i = 0; i < outputs.size(); i++) { - tuple[i] = py::cast(static_cast(outputs[i])); + tuple[i] = py::cast(autograd::as_variable_ref(outputs[i])); } return tuple; } diff --git a/torch/csrc/jit/interpreter.cpp b/torch/csrc/jit/interpreter.cpp index 9528c87f3df7..2320b86dbf38 100644 --- a/torch/csrc/jit/interpreter.cpp +++ b/torch/csrc/jit/interpreter.cpp @@ -5,14 +5,25 @@ #include "torch/csrc/autograd/functions/special.h" #include "torch/csrc/autograd/profiler.h" #include "torch/csrc/autograd/variable.h" -#include "torch/csrc/jit/fusion_compiler.h" #include "torch/csrc/jit/aten_dispatch.h" +#include "torch/csrc/jit/fusion_compiler.h" #include "torch/csrc/jit/graph_executor.h" #include "torch/csrc/jit/ir.h" #include "torch/csrc/jit/tensor_conversions.h" #include "torch/csrc/variable_tensor_functions.h" +#include "torch/csrc/autograd/generated/variable_factories.h" +#include +#include +#include +#include +#include +#include #include +#include +#include +#include +#include namespace torch { namespace jit { @@ -102,7 +113,7 @@ void desugarTripCounts(Block * b) { WithInsertPoint guard(n); // int i = 0 Value* initial_trip_count = - g->insertNode(g->createConstant(at::zeros(at::CPU(at::kLong), {1}))) + g->insertNode(g->createConstant(at::zeros({1}, at::kLong))) ->output(); // Set up initial iteration number value for loop-carried dependency n->removeInput(0); @@ -122,7 +133,7 @@ void desugarTripCounts(Block * b) { // conjunctive stopping condition as above. Value* const_one = - g->insertNode(g->createConstant(at::ones(at::CPU(at::kLong), {1}))) + g->insertNode(g->createConstant(at::ones({1}, at::kLong))) ->output(); Value* inc_trip_count = @@ -761,7 +772,7 @@ struct CodeImpl { return [=](Stack& stack) { auto t = pop(stack); at::IntList sizes = t.sizes(); - auto sizes_tensor = torch::CPU(at::kLong).tensor(sizes.size()); + auto sizes_tensor = torch::empty({static_cast(sizes.size())}, at::dtype(at::kLong)); auto accessor = sizes_tensor.accessor(); for (size_t i=0; it_(attr::value, ref.clone()); n->output()->inferTypeFrom(ref); diff --git a/torch/csrc/jit/passes/onnx/peephole.cpp b/torch/csrc/jit/passes/onnx/peephole.cpp index 76fa5a99b1ac..b3c87c914d15 100644 --- a/torch/csrc/jit/passes/onnx/peephole.cpp +++ b/torch/csrc/jit/passes/onnx/peephole.cpp @@ -330,7 +330,7 @@ void fixDefaultRNNState(Graph* graph, Node * n, int input_index) { Node * hidden_size = graph->create(onnx::Constant, 1); hidden_size->insertBefore(n); - hidden_size->t_(attr::value, at::CPU(at::kLong).tensor({1}).fill_(n->i(attr::hidden_size))); // at::Scalar(n->i(attr::hidden_size)).toTensor()); + hidden_size->t_(attr::value, at::full({1}, n->i(attr::hidden_size), at::kLong)); // at::Scalar(n->i(attr::hidden_size)).toTensor()); Node * num_directions = graph->create(onnx::Constant, 1); num_directions->insertBefore(n); diff --git a/torch/csrc/jit/passes/shape_analysis.cpp b/torch/csrc/jit/passes/shape_analysis.cpp index be18bf1db145..0478d50c0325 100644 --- a/torch/csrc/jit/passes/shape_analysis.cpp +++ b/torch/csrc/jit/passes/shape_analysis.cpp @@ -1,10 +1,10 @@ #include "torch/csrc/jit/passes/shape_analysis.h" -#include "torch/csrc/utils/auto_gpu.h" #include "torch/csrc/jit/ir.h" #include "torch/csrc/jit/argument_spec.h" #include "torch/csrc/jit/aten_dispatch.h" +#include #include #include @@ -28,7 +28,7 @@ void setDynamicType(Node * node) { at::Tensor representativeTensor(const TensorType * type) { auto backend = type->device() == -1 ? at::kCPU : at::kCUDA; - AutoGPU gpu_guard(type->device()); + at::DeviceGuard device_guard(type->device()); auto & attype = at::getType(backend, type->scalarType()); return attype.tensor(type->sizes(), type->strides()).zero_(); } diff --git a/torch/csrc/jit/script/compiler.cpp b/torch/csrc/jit/script/compiler.cpp index 7b9afd316590..851ea932fe00 100644 --- a/torch/csrc/jit/script/compiler.cpp +++ b/torch/csrc/jit/script/compiler.cpp @@ -1307,7 +1307,7 @@ private: return emitNode( Symbol::aten("type_as"), input.range(), - {emitExpr(input), createConstant(*graph, input.range(), at::ones(at::CPU(t), {1}))}, + {emitExpr(input), createConstant(*graph, input.range(), at::ones({1}, t))}, 1) ->output(); } diff --git a/torch/csrc/jit/script/init.cpp b/torch/csrc/jit/script/init.cpp index d9d44fe56586..b6b1df722a7a 100644 --- a/torch/csrc/jit/script/init.cpp +++ b/torch/csrc/jit/script/init.cpp @@ -1,14 +1,21 @@ #include "torch/csrc/jit/script/init.h" -#include "torch/csrc/jit/script/compiler.h" + #include "torch/csrc/Device.h" +#include "torch/csrc/Dtype.h" +#include "torch/csrc/Layout.h" +#include "torch/csrc/jit/script/compiler.h" #include "torch/csrc/jit/tensor_conversions.h" #include "torch/csrc/jit/python_tracer.h" #include -#include +#include + +#include #include +#include #include +#include #include #include @@ -174,14 +181,15 @@ struct VISIBILITY_HIDDEN ConstantPythonValue : public PythonValue { return createConstant(loc, m, at::CPU(at::kByte).scalarTensor(py::cast(self))); } else if(THPDevice_Check(self.ptr())) { auto device = (THPDevice*) self.ptr(); - auto t = as_tensor({static_cast(device->device.type), device->device.deviceInt64()}); + auto t = as_tensor({static_cast(device->device.type()), device->device.index()}); return createConstant(loc, m, t); } else if(THPLayout_Check(self.ptr())) { auto layout = (THPLayout*) self.ptr(); - return createConstant(loc, m, at::CPU(at::kLong).scalarTensor(layout->is_strided)); + const auto v = static_cast(layout->layout); + return createConstant(loc, m, at::CPU(at::kLong).scalarTensor(v)); } else if(THPDtype_Check(self.ptr())) { auto dtype = (THPDtype*)(self.ptr()); - int64_t v = static_cast(dtype->scalar_type); + const auto v = static_cast(dtype->scalar_type); return createConstant(loc, m, at::CPU(at::kLong).scalarTensor(v)); } return std::make_shared(self); @@ -327,11 +335,11 @@ py::object unpackVariableTensorList(std::vector outputs) { if (outputs.size() == 0) { return py::none(); } else if (outputs.size() == 1) { - return py::cast(static_cast(outputs[0])); + return py::cast(autograd::as_variable_ref(outputs[0])); } else { py::tuple tuple(outputs.size()); for(size_t i = 0; i < outputs.size(); i++) { - tuple[i] = py::cast(static_cast(outputs[i])); + tuple[i] = py::cast(autograd::as_variable_ref(outputs[i])); } return tuple; } @@ -399,7 +407,7 @@ void initJitScriptBindings(PyObject* module) { py::tuple r(3); result[i] = std::make_tuple( p.key, - static_cast(*p->slot()), + autograd::as_variable_ref(*p->slot()), p->is_buffer); } @@ -440,7 +448,7 @@ void initJitScriptBindings(PyObject* module) { std::vector parameters; gatherParametersAndBuffers(parameters, self); for(at::Tensor* param : parameters) { - inputs.push_back(static_cast(*param)); + inputs.push_back(autograd::as_variable_ref(*param)); } auto graph = tracer::createGraphByTracing(func, std::move(inputs), num_inputs); self.create_method(name, std::move(graph), std::move(parameters)); diff --git a/torch/csrc/jit/script/module.h b/torch/csrc/jit/script/module.h index b51a5fdf82d1..b2f38fc29414 100644 --- a/torch/csrc/jit/script/module.h +++ b/torch/csrc/jit/script/module.h @@ -244,7 +244,7 @@ struct Module : public std::enable_shared_from_this { } autograd::Variable get_parameter(const std::string& name) const { - return static_cast(*parameter_slot(name)); + return autograd::as_variable_ref(*parameter_slot(name)); } // each module owns its method. The reference returned here diff --git a/torch/csrc/jit/tensor_conversions.h b/torch/csrc/jit/tensor_conversions.h index ef3c0febdaa7..d445b3a8c418 100644 --- a/torch/csrc/jit/tensor_conversions.h +++ b/torch/csrc/jit/tensor_conversions.h @@ -110,7 +110,7 @@ inline at::Tensor as_tensor(const at::Scalar& s) { template inline at::Tensor as_tensor(std::array&& bools) { - auto r = at::CPU(at::kByte).tensor({N}); + auto r = at::empty({N}, at::kByte); auto accessor = r.accessor(); for(size_t i = 0; i < N; ++i) { accessor[i] = bools[i]; diff --git a/torch/csrc/jit/test_jit.cpp b/torch/csrc/jit/test_jit.cpp index 8334e299dcc1..1cad88af050a 100644 --- a/torch/csrc/jit/test_jit.cpp +++ b/torch/csrc/jit/test_jit.cpp @@ -37,8 +37,19 @@ #include "onnx/onnx_pb.h" -#include +#include + +#include +#include +#include #include +#include +#include +#include +#include +#include +#include +#include namespace torch { namespace jit { @@ -126,9 +137,9 @@ static void fusionTests() { Var i1 = Var::asNewInput(graph); auto o0 = i0 * i1; o0.addAsOutput(); - auto a = at::rand(at::CUDA(at::kFloat), {3,4}); - auto b = at::rand(at::CUDA(at::kFloat), {4,3}).transpose(0,1); - auto o = at::zeros(at::CUDA(at::kFloat), {3,4}); + auto a = at::rand({3,4}, at::kCUDA); + auto b = at::rand({4,3}, at::kCUDA).transpose(0,1); + auto o = at::zeros({3,4}, at::kCUDA); comp.debugLaunchGraph(graph, 0, {a,b}, {o}); auto o2 = a*b; float max_diff = (o2 - o).abs().max().toCDouble(); @@ -170,12 +181,12 @@ static void fusionTests() { for(size_t i = 0; i < graph.inputs().size(); i++) { std::vector dims = {128, 128, 32}; std::swap(dims[ti],dims[tj]); - inputs.push_back(at::rand(at::CUDA(at::kFloat), dims).transpose(ti, tj)); + inputs.push_back(at::rand(dims, at::kCUDA).transpose(ti, tj)); } for(size_t i = 0; i < graph.outputs().size(); i++) { std::vector dims = {128, 128, 32}; std::swap(dims[toi],dims[toj]); - outputs.push_back(at::zeros(at::CUDA(at::kFloat), dims).transpose(toi,toj)); + outputs.push_back(at::zeros(dims, at::kCUDA).transpose(toi,toj)); } auto t22 = inputs[4].sigmoid(); @@ -215,13 +226,13 @@ static void fusionTests() { o0.addAsOutput(); Var::cat({i0, o0}, dim).addAsOutput(); - auto a = at::rand(at::CUDA(at::kFloat), {3,4,5}); - auto b = at::rand(at::CUDA(at::kFloat), {4,3,5}).transpose(0,1); - auto o = at::zeros(at::CUDA(at::kFloat), {3,4,5}); + auto a = at::rand({3,4,5}, at::kCUDA); + auto b = at::rand({4,3,5}, at::kCUDA).transpose(0,1); + auto o = at::zeros({3,4,5}, at::kCUDA); auto o_r = a*b; auto o2_r = at::cat({a, o_r}, dim); - auto o2 = at::zeros(at::CUDA(at::kFloat), o2_r.sizes()); + auto o2 = at::zeros(o2_r.sizes(), at::kCUDA); comp.debugLaunchGraph(graph, 0, {a,b}, {o, o2}); float max_diff = (o_r - o).abs().max().toCDouble(); @@ -431,11 +442,11 @@ void interpTest() { int hidden_size = 2*input_size; - auto input = at::randn(at::CUDA(at::kFloat), {seq_len, batch_size, input_size}); - auto hx = at::randn(at::CUDA(at::kFloat), {batch_size, hidden_size}); - auto cx = at::randn(at::CUDA(at::kFloat), {batch_size, hidden_size}); - auto w_ih = t_def(at::randn(at::CUDA(at::kFloat), {4 * hidden_size, input_size})); - auto w_hh = t_def(at::randn(at::CUDA(at::kFloat), {4 * hidden_size, hidden_size})); + auto input = at::randn({seq_len, batch_size, input_size}, at::kCUDA); + auto hx = at::randn({batch_size, hidden_size}, at::kCUDA); + auto cx = at::randn({batch_size, hidden_size}, at::kCUDA); + auto w_ih = t_def(at::randn({4 * hidden_size, input_size}, at::kCUDA)); + auto w_hh = t_def(at::randn({4 * hidden_size, hidden_size}, at::kCUDA)); auto lstm_g = build_lstm(); Code lstm_function(lstm_g); @@ -455,12 +466,12 @@ void interpStageTest() { constexpr int seq_len = 32; int hidden_size = 2*input_size; - auto input = at::randn(at::CUDA(at::kFloat), {seq_len, batch_size, input_size}); - auto hx = at::randn(at::CUDA(at::kFloat), {batch_size, hidden_size}); - auto cx = at::randn(at::CUDA(at::kFloat), {batch_size, hidden_size}); - auto cx1 = at::randn(at::CUDA(at::kFloat), {batch_size, hidden_size}); - auto w_ih = t_def(at::randn(at::CUDA(at::kFloat), {4 * hidden_size, input_size})); - auto w_hh = t_def(at::randn(at::CUDA(at::kFloat), {4 * hidden_size, hidden_size})); + auto input = at::randn({seq_len, batch_size, input_size}, at::kCUDA); + auto hx = at::randn({batch_size, hidden_size}, at::kCUDA); + auto cx = at::randn({batch_size, hidden_size}, at::kCUDA); + auto cx1 = at::randn({batch_size, hidden_size}, at::kCUDA); + auto w_ih = t_def(at::randn({4 * hidden_size, input_size}, at::kCUDA)); + auto w_hh = t_def(at::randn({4 * hidden_size, hidden_size}, at::kCUDA)); auto lstm_g = build_lstm_stages(); @@ -676,7 +687,7 @@ void testCreateAutodiffSubgraphs(std::ostream & out) { } autograd::Variable var(at::Type & t, at::IntList sizes, bool requires_grad) { - return autograd::make_variable(at::rand(t, sizes), requires_grad); + return autograd::make_variable(at::rand(sizes, t), requires_grad); } autograd::Variable undef() { return autograd::Variable(); @@ -762,11 +773,11 @@ void shapeAnalysisTest() { auto v = [](at::Tensor t) { return autograd::make_variable(t, false); }; - auto input = at::randn(at::CUDA(at::kFloat), {batch_size, input_size}); - auto hx = at::randn(at::CUDA(at::kFloat), {batch_size, hidden_size}); - auto cx = at::randn(at::CUDA(at::kFloat), {batch_size, hidden_size}); - auto w_ih = t_def(at::randn(at::CUDA(at::kFloat), {4 * hidden_size, input_size})); - auto w_hh = t_def(at::randn(at::CUDA(at::kFloat), {4 * hidden_size, hidden_size})); + auto input = at::randn({batch_size, input_size}, at::kCUDA); + auto hx = at::randn({batch_size, hidden_size}, at::kCUDA); + auto cx = at::randn({batch_size, hidden_size}, at::kCUDA); + auto w_ih = t_def(at::randn({4 * hidden_size, input_size}, at::kCUDA)); + auto w_hh = t_def(at::randn({4 * hidden_size, hidden_size}, at::kCUDA)); auto g = build_lstm(); ArgumentSpec spec(false, createVarList({v(input), v(hx), v(cx), v(w_ih), v(w_hh) })); @@ -788,11 +799,11 @@ void testGraphExecutor() { auto v = [](at::Tensor t) { return autograd::make_variable(t, false); }; - auto input = at::randn(at::CUDA(at::kFloat), {batch_size, input_size}); - auto hx = at::randn(at::CUDA(at::kFloat), {batch_size, hidden_size}); - auto cx = at::randn(at::CUDA(at::kFloat), {batch_size, hidden_size}); - auto w_ih = t_def(at::randn(at::CUDA(at::kFloat), {4 * hidden_size, input_size})); - auto w_hh = t_def(at::randn(at::CUDA(at::kFloat), {4 * hidden_size, hidden_size})); + auto input = at::randn({batch_size, input_size}, at::kCUDA); + auto hx = at::randn({batch_size, hidden_size}, at::kCUDA); + auto cx = at::randn({batch_size, hidden_size}, at::kCUDA); + auto w_ih = t_def(at::randn({4 * hidden_size, input_size}, at::kCUDA)); + auto w_hh = t_def(at::randn({4 * hidden_size, hidden_size}, at::kCUDA)); std::vector inputs = {v(input), v(hx), v(cx), v(w_ih), v(w_hh) }; auto g = build_lstm(); diff --git a/torch/csrc/tensor/python_tensor.cpp b/torch/csrc/tensor/python_tensor.cpp index 03abae05108d..929948b2209d 100644 --- a/torch/csrc/tensor/python_tensor.cpp +++ b/torch/csrc/tensor/python_tensor.cpp @@ -19,7 +19,11 @@ #include "torch/csrc/utils/tensor_new.h" #include "torch/csrc/utils/tensor_types.h" +#include + #include +#include +#include #include namespace torch { namespace tensor { @@ -108,10 +112,10 @@ PyObject *Tensor_is_cuda(PyTensorType* self) { } PyObject *Tensor_is_sparse(PyTensorType *self) { - if (!self->layout->is_strided) { - Py_RETURN_TRUE; - } else { + if (self->layout->layout == at::Layout::Strided) { Py_RETURN_FALSE; + } else { + Py_RETURN_TRUE; } } @@ -344,7 +348,7 @@ void py_set_default_dtype(PyObject* obj) { if (THPDtype_Check(obj)) { auto ¤t_default = get_default_tensor_type(); type = &get_tensor_type((THPDtype*)obj, torch::getLayout(current_default.backend()), - torch::getDeviceType(current_default) == DeviceType::CUDA); + torch::getDeviceType(current_default) == at::Device::Type::CUDA); } else { throw TypeError("invalid type object"); } @@ -386,10 +390,8 @@ at::Type& get_default_tensor_type() { Device getDevice(const at::Tensor& tensor) { if (tensor.type().is_cuda()) { - return torch::Device(torch::DeviceType::CUDA, tensor.get_device(), false); - } else { - return torch::Device(torch::DeviceType::CPU, -1, true); + return at::Device(at::kCUDA, tensor.get_device()); } + return at::Device(at::kCPU); } - }} // namespace torch::tensor diff --git a/torch/csrc/tensor/python_tensor.h b/torch/csrc/tensor/python_tensor.h index 1485ad017041..ea5c2dc5e7f2 100644 --- a/torch/csrc/tensor/python_tensor.h +++ b/torch/csrc/tensor/python_tensor.h @@ -1,8 +1,12 @@ #pragma once #include "torch/csrc/python_headers.h" -#include "torch/csrc/utils/device.h" -#include + +namespace at { +struct Type; +struct Device; +struct Tensor; +} // namespace at namespace torch { namespace tensor { @@ -25,6 +29,6 @@ void py_set_default_dtype(PyObject* dtype_obj); at::Type& get_default_tensor_type(); // Gets the torch::Device object of a given at::Tensor -Device getDevice(const at::Tensor& tensor); +at::Device getDevice(const at::Tensor& tensor); }} // namespace torch::tensor diff --git a/torch/csrc/utils/auto_gpu.h b/torch/csrc/utils/auto_gpu.h deleted file mode 100644 index 66f220c4dc15..000000000000 --- a/torch/csrc/utils/auto_gpu.h +++ /dev/null @@ -1,69 +0,0 @@ -#pragma once - -// RAII structs to set CUDA device - -#include -#include - -#include - -#ifdef USE_CUDA -#include -#include -#endif - -struct AutoGPU { - explicit AutoGPU(int device=-1) { - setDevice(device); - } - - explicit AutoGPU(const at::Tensor& t) { - setDevice(t.type().is_cuda() ? (int) t.get_device() : -1); - } - - explicit AutoGPU(at::TensorList &tl) { - if (tl.size() > 0) { - auto& t = tl[0]; - setDevice(t.type().is_cuda() ? t.get_device() : -1); - } - } - - ~AutoGPU() { -#ifdef USE_CUDA - if (original_device != -1) { - cudaSetDevice(original_device); - } -#endif - } - - inline void setDevice(int device) { -#ifdef USE_CUDA - if (device == -1) { - return; - } - if (original_device == -1) { - cudaCheck(cudaGetDevice(&original_device)); - if (device != original_device) { - cudaCheck(cudaSetDevice(device)); - } - } else { - cudaCheck(cudaSetDevice(device)); - } -#endif - } - - int original_device = -1; - -private: -#ifdef USE_CUDA - static void cudaCheck(cudaError_t err) { - if (err != cudaSuccess) { - std::string msg = "CUDA error ("; - msg += std::to_string(static_cast(err)); - msg += "): "; - msg += cudaGetErrorString(err); - throw std::runtime_error(msg); - } - } -#endif -}; diff --git a/torch/csrc/utils/device.cpp b/torch/csrc/utils/device.cpp deleted file mode 100644 index 330a0989a584..000000000000 --- a/torch/csrc/utils/device.cpp +++ /dev/null @@ -1,31 +0,0 @@ -#include "device.h" -#include -#include - -namespace torch { - -Device::Device(DeviceType type, int64_t index, bool is_default) - : type(type), index(index), is_default(is_default) { - if (!is_default) { - switch (type) { - case DeviceType::CPU: - if (index != 0) { - throw std::runtime_error("cpu device index must be 0, got " + std::to_string(index)); - } - break; - case DeviceType::CUDA: - if (index < 0) { - throw std::runtime_error("device index must be positive, got " + std::to_string(index)); - } - break; - default: - throw std::runtime_error("unexpected DeviceType"); - } - } -} - -bool Device::operator==(const Device& rhs) { - return this->type == rhs.type && this->index == rhs.index && this->is_default == rhs.is_default; -} - -} diff --git a/torch/csrc/utils/device.h b/torch/csrc/utils/device.h deleted file mode 100644 index 0487726e68d7..000000000000 --- a/torch/csrc/utils/device.h +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once - -#include - -namespace torch { - -enum class DeviceType {CPU=0, CUDA=1}; - -struct Device { - DeviceType type; - int64_t index; - bool is_default; // is default device for type. - Device(DeviceType type, int64_t index, bool is_default); - - bool operator==(const Device& rhs); - inline int64_t deviceInt64() { return (this->is_default || this->type == DeviceType::CPU) ? -1 : this->index; } -}; - -} diff --git a/torch/csrc/utils/python_arg_parser.cpp b/torch/csrc/utils/python_arg_parser.cpp index 92b0ee1e0ed0..0869b3bc32ef 100644 --- a/torch/csrc/utils/python_arg_parser.cpp +++ b/torch/csrc/utils/python_arg_parser.cpp @@ -1,14 +1,17 @@ #include "torch/csrc/utils/python_arg_parser.h" -#include -#include -#include - #include "torch/csrc/Exceptions.h" -#include "torch/csrc/utils/python_strings.h" +#include "torch/csrc/Layout.h" #include "torch/csrc/utils/invalid_arguments.h" +#include "torch/csrc/utils/python_strings.h" -using namespace at; +#include + +#include +#include +#include +#include +#include namespace torch { @@ -177,11 +180,11 @@ void FunctionParameter::set_default_str(const std::string& str) { if (str == "None") { // This is a bit awkward, but convenient for clamp which takes Scalars, // but allows None. - default_scalar = Scalar(NAN); + default_scalar = at::Scalar(NAN); } else { // we sometimes rely on integer-vs-float values, e.g. with arange. - auto as_integer = parse_as_integer(str); - default_scalar = Scalar(as_integer.value_or(atof(str.c_str()))); + const auto as_integer = parse_as_integer(str); + default_scalar = at::Scalar(as_integer.value_or(atof(str.c_str()))); } } else if (type_ == ParameterType::INT_LIST) { if (str != "None") { diff --git a/torch/csrc/utils/python_arg_parser.h b/torch/csrc/utils/python_arg_parser.h index 4bc03e22d744..5b7cdbf358b7 100644 --- a/torch/csrc/utils/python_arg_parser.h +++ b/torch/csrc/utils/python_arg_parser.h @@ -41,25 +41,29 @@ #include "torch/csrc/python_headers.h" -#include -#include -#include -#include #include "torch/csrc/Device.h" #include "torch/csrc/Dtype.h" #include "torch/csrc/DynamicTypes.h" #include "torch/csrc/Exceptions.h" #include "torch/csrc/Generator.h" -#include "torch/csrc/autograd/python_variable.h" #include "torch/csrc/autograd/generated/VariableType.h" +#include "torch/csrc/autograd/python_variable.h" #include "torch/csrc/jit/tracer.h" #include "torch/csrc/tensor/python_tensor.h" -#include "torch/csrc/utils/device.h" +#include "torch/csrc/utils/numpy_stub.h" #include "torch/csrc/utils/object_ptr.h" #include "torch/csrc/utils/python_numbers.h" #include "torch/csrc/utils/python_strings.h" -#include "torch/csrc/utils/numpy_stub.h" + +#include + +#include +#include +#include +#include +#include +#include namespace torch { @@ -122,10 +126,9 @@ struct PythonArgs { inline at::optional scalartypeOptional(int i); inline const THPLayout& layout(int i); inline const THPLayout& layoutWithDefault(int i, const THPLayout& default_layout); - inline Device device(int i); - inline Device deviceWithDefault(int i, const Device& default_device); - inline int64_t deviceInt64(int i); - inline at::optional deviceOptional(int i); + inline at::Device device(int i); + inline at::Device deviceWithDefault(int i, const at::Device& default_device); + inline at::optional deviceOptional(int i); inline std::string string(int i); inline PyObject* pyobject(int i); inline int64_t toInt64(int i); @@ -328,46 +331,43 @@ static std::string cpu_str = "cpu"; static std::string cuda_prefix = "cuda:"; static std::string cpu_prefix = "cpu:"; -inline Device PythonArgs::device(int i) { +inline at::Device PythonArgs::device(int i) { if (!args[i]) { const auto& default_tensor_type = torch::tensor::get_default_tensor_type(); - const auto device_type = torch::getDeviceType(default_tensor_type); - return Device(device_type, -1, true); + return at::Device(default_tensor_type.backend()); } if (THPDevice_Check(args[i])) { - auto device = reinterpret_cast(args[i]); + const auto device = reinterpret_cast(args[i]); return device->device; } if (THPUtils_checkLong(args[i])) { - auto index = THPUtils_unpackLong(args[i]); - return Device(DeviceType::CUDA, index, index == -1); + const auto device_index = THPUtils_unpackLong(args[i]); + AT_CHECK(device_index >= 0, "Device index must not be negative"); + return at::Device(at::kCUDA, device_index); } - std::string device_str = THPUtils_unpackString(args[i]); + const std::string device_str = THPUtils_unpackString(args[i]); if (device_str == cpu_str) { - return Device(DeviceType::CPU, -1, true); + return at::Device(at::kCPU); } else if (device_str == cuda_str) { - return Device(DeviceType::CUDA, -1, true); + return at::Device(at::kCUDA); } else if (device_str.compare(0, cpu_prefix.length(), cpu_prefix) == 0) { - auto device_index = std::stoi(device_str.substr(cpu_prefix.length())); - return Device(DeviceType::CPU, device_index, false); + const auto device_index = std::stoi(device_str.substr(cpu_prefix.length())); + AT_CHECK(device_index >= 0, "Device index must not be negative"); + return at::Device(at::kCPU, device_index); } else if (device_str.compare(0, cuda_prefix.length(), cuda_prefix) == 0) { - auto device_index = std::stoi(device_str.substr(cuda_prefix.length())); - return Device(DeviceType::CUDA, device_index, false); + const auto device_index = std::stoi(device_str.substr(cuda_prefix.length())); + AT_CHECK(device_index >= 0, "Device index must not be negative"); + return at::Device(at::kCUDA, device_index); } throw torch::TypeError("only \"cuda\" and \"cpu\" are valid device types, got %s", device_str.c_str()); } -inline Device PythonArgs::deviceWithDefault(int i, const Device& default_device) { +inline at::Device PythonArgs::deviceWithDefault(int i, const at::Device& default_device) { if (!args[i]) return default_device; return device(i); } -inline int64_t PythonArgs::deviceInt64(int i) { - auto dev = device(i); - return dev.deviceInt64(); -} - -inline at::optional PythonArgs::deviceOptional(int i) { +inline at::optional PythonArgs::deviceOptional(int i) { if (!args[i]) return at::nullopt; return device(i); } diff --git a/torch/csrc/utils/tensor_conversion_dispatch.cpp b/torch/csrc/utils/tensor_conversion_dispatch.cpp index ae8b027e6706..030565e8339e 100644 --- a/torch/csrc/utils/tensor_conversion_dispatch.cpp +++ b/torch/csrc/utils/tensor_conversion_dispatch.cpp @@ -3,23 +3,26 @@ #include "tensor_conversion_dispatch.h" #include "torch/csrc/utils/auto_gil.h" -#include "torch/csrc/utils/auto_gpu.h" #include "torch/csrc/utils/cuda_lazy_init.h" +#include + +#include + namespace torch { namespace utils { at::Tensor dispatch_type_conversion( const at::Tensor & self, const at::Type & type, - at::optional device, + at::optional device_index, bool non_blocking) { if (type.is_cuda()) { torch::utils::cuda_lazy_init(); } AutoNoGIL no_gil; - int64_t tensor_device = self.is_cuda() ? self.get_device() : -1; - AutoGPU auto_gpu(device.value_or(tensor_device)); + const int32_t tensor_device = self.is_cuda() ? self.get_device() : -1; + at::DeviceGuard device_guard(device_index.value_or(tensor_device)); if (self.is_cuda() && type.is_cuda() && tensor_device != at::current_device()) { // copy if the devices are different even if the types are the same diff --git a/torch/csrc/utils/tensor_conversion_dispatch.h b/torch/csrc/utils/tensor_conversion_dispatch.h index ce9eec74956f..35544588bda1 100644 --- a/torch/csrc/utils/tensor_conversion_dispatch.h +++ b/torch/csrc/utils/tensor_conversion_dispatch.h @@ -4,6 +4,8 @@ #include +#include + namespace torch { namespace utils { // Returns a tensor with the same data as `self` and the specified type and @@ -23,7 +25,7 @@ namespace torch { namespace utils { // w.r.t the host. at::Tensor dispatch_type_conversion(const at::Tensor & self, const at::Type & type, - at::optional device=at::nullopt, + at::optional device_index = at::nullopt, bool non_blocking=false); }} // namespace torch::utils diff --git a/torch/csrc/utils/tensor_devices.h b/torch/csrc/utils/tensor_devices.h deleted file mode 100644 index 31515f3d751d..000000000000 --- a/torch/csrc/utils/tensor_devices.h +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once - -#include -#include "DynamicTypes.h" -#include "device.h" - -namespace torch { namespace utils { - -Device getDevice(const at::Tensor tensor) { - return torch::Device(torch::getDeviceType(tensor.type()), tensor.type().is_cuda() ? tensor.get_device(): 0, false); -} - -}} // namespace torch::utils diff --git a/torch/csrc/utils/tensor_layouts.cpp b/torch/csrc/utils/tensor_layouts.cpp index 5d872f7d73e2..6fd210f41860 100644 --- a/torch/csrc/utils/tensor_layouts.cpp +++ b/torch/csrc/utils/tensor_layouts.cpp @@ -1,16 +1,21 @@ #include "torch/csrc/python_headers.h" -#include -#include "tensor_layouts.h" + +#include "torch/csrc/utils/tensor_layouts.h" + +#include "torch/csrc/Layout.h" #include "torch/csrc/DynamicTypes.h" #include "torch/csrc/Exceptions.h" +#include +#include + namespace torch { namespace utils { void initializeLayouts() { auto torch_module = THPObjectPtr(PyImport_ImportModule("torch")); if (!torch_module) python_error(); - PyObject *strided_layout = THPLayout_New(true, "torch.strided"); + PyObject *strided_layout = THPLayout_New(at::Layout::Strided, "torch.strided"); Py_INCREF(strided_layout); if (PyModule_AddObject(torch_module, "strided", strided_layout) != 0) { throw python_error(); @@ -19,7 +24,7 @@ void initializeLayouts() { registerLayoutObject((THPLayout*)strided_layout, at::Backend::CPU); registerLayoutObject((THPLayout*)strided_layout, at::Backend::CUDA); - PyObject *sparse_coo_layout = THPLayout_New(false, "torch.sparse_coo"); + PyObject *sparse_coo_layout = THPLayout_New(at::Layout::Sparse, "torch.sparse_coo"); Py_INCREF(sparse_coo_layout); if (PyModule_AddObject(torch_module, "sparse_coo", sparse_coo_layout) != 0) { throw python_error(); diff --git a/torch/csrc/utils/tensor_new.cpp b/torch/csrc/utils/tensor_new.cpp index ff84adf8d5a5..26d5e9252d82 100644 --- a/torch/csrc/utils/tensor_new.cpp +++ b/torch/csrc/utils/tensor_new.cpp @@ -1,17 +1,11 @@ #include "torch/csrc/python_headers.h" #include "tensor_new.h" -#include -#include -#include - #include "torch/csrc/DynamicTypes.h" #include "torch/csrc/Exceptions.h" #include "torch/csrc/Size.h" #include "torch/csrc/autograd/variable.h" -#include "torch/csrc/autograd/utils/python_variables.h" #include "torch/csrc/utils/auto_gil.h" -#include "torch/csrc/utils/auto_gpu.h" #include "torch/csrc/utils/cuda_lazy_init.h" #include "torch/csrc/utils/numpy_stub.h" #include "torch/csrc/utils/python_arg_parser.h" @@ -20,73 +14,92 @@ #include "torch/csrc/utils/python_strings.h" #include "torch/csrc/utils/tensor_conversion_dispatch.h" #include "torch/csrc/utils/tensor_numpy.h" +#include "torch/csrc/autograd/generated/variable_factories.h" -static const int MAX_DIMS = 128; +#include +#include +#include -using namespace at; -using torch::autograd::utils::set_requires_grad; +#include +#include + +using at::Device; +using at::IntList; +using at::kCPU; +using at::kCUDA; +using at::kLong; +using at::kSparseCPU; +using at::kSparseCUDA; +using at::optional; +using at::Scalar; +using at::ScalarType; +using at::Storage; +using at::Tensor; +using at::TensorOptions; +using at::Type; namespace torch { namespace utils { +namespace { +const int MAX_DIMS = 128; -static void maybe_initialize_cuda(const at::Type &type) { +void maybe_initialize_cuda(const Type &type) { if (type.is_cuda()) { torch::utils::cuda_lazy_init(); } } -static Tensor dispatch_zeros(const Type& type, int device, IntList sizes) { +Tensor dispatch_zeros(const Type& type, int32_t device_index, IntList sizes) { maybe_initialize_cuda(type); AutoNoGIL no_gil; - AutoGPU auto_gpu(device); - return type.zeros(sizes); + return torch::zeros(sizes, TensorOptions(type, device_index)); } -static Tensor dispatch_ones(const Type& type, int device, IntList sizes) { +Tensor dispatch_ones(const Type& type, int32_t device_index, IntList sizes) { maybe_initialize_cuda(type); AutoNoGIL no_gil; - AutoGPU auto_gpu(device); - return type.ones(sizes); + return torch::ones(sizes, TensorOptions(type, device_index)); } -static Tensor dispatch_full(const Type& type, Scalar fill_value, int device, IntList sizes) { +Tensor dispatch_full(const Type& type, Scalar fill_value, int32_t device_index, IntList sizes) { maybe_initialize_cuda(type); AutoNoGIL no_gil; - AutoGPU auto_gpu(device); - return type.full(sizes, fill_value); + return torch::full(sizes, fill_value, TensorOptions(type, device_index)); } -static Tensor new_with_sizes(const Type& type, int device, IntList sizes) { +Tensor new_with_sizes(const Type& type, int32_t device_index, IntList sizes) { maybe_initialize_cuda(type); AutoNoGIL no_gil; - AutoGPU auto_gpu(device); - return type.tensor(sizes); + return torch::empty(sizes, TensorOptions(type, device_index)); } -static Tensor new_with_storage(const Type& type, Storage& storage) { - auto tensor = type.tensor(); +Tensor new_with_storage(const Type& type, Storage& storage) { + auto tensor = at::empty({}, type); tensor.set_(storage); return tensor; } -static Tensor new_with_tensor(const Type& type, Tensor other) { +Tensor new_with_tensor(const Type& type, Tensor other) { if (other.type() != type) { throw TypeError("expected %s (got %s)", type.toString(), other.type().toString()); } return other.slice(); } -static Tensor new_with_type_conversion(const Type& type, Tensor other, int64_t device) { - return dispatch_type_conversion(other, type, device, false); +Tensor new_with_type_conversion(const Type& type, Tensor other, int32_t device_index) { + return dispatch_type_conversion(other, type, device_index, false); } -static Tensor new_with_tensor_copy(const Type& type, Tensor other, int64_t device) { +Tensor new_with_tensor_copy(const Type& type, Tensor other, int32_t device_index) { maybe_initialize_cuda(type); AutoNoGIL no_gil; - AutoGPU auto_gpu(device); + at::DeviceGuard device_guard; + if (type.is_cuda()) { + device_guard.set_index(device_index); + } return type.copy(other); } -static std::vector compute_sizes(PyObject* seq) { +std::vector compute_sizes(PyObject* seq) { std::vector sizes; THPObjectPtr handle; while (PySequence_Check(seq)) { @@ -107,7 +120,7 @@ static std::vector compute_sizes(PyObject* seq) { return sizes; } -static ScalarType infer_scalar_type(PyObject *obj) { +ScalarType infer_scalar_type(PyObject *obj) { if (PyFloat_Check(obj)) { // this is always guaranteed to be a floating-point type, and makes it more // convenient to write e.g. torch.tensor(0.) than torch.tensor(0., dtype=torch.Tensor.dtype). @@ -152,7 +165,7 @@ static ScalarType infer_scalar_type(PyObject *obj) { AT_ERROR("Could not infer dtype of ", Py_TYPE(obj)->tp_name); } -static void recursive_store(char* data, IntList sizes, IntList strides, int64_t dim, +void recursive_store(char* data, IntList sizes, IntList strides, int64_t dim, ScalarType scalarType, int elementSize, PyObject* obj) { int64_t ndim = sizes.size(); if (dim == ndim) { @@ -176,17 +189,20 @@ static void recursive_store(char* data, IntList sizes, IntList strides, int64_t } } -static Tensor internal_new_from_data(const Type & type, at::optional device_opt, PyObject* data, +Tensor internal_new_from_data(const Type & type, at::optional device_opt, PyObject* data, bool copy_variables, bool copy_numpy, bool type_inference) { - int64_t device = device_opt.has_value() ? device_opt.value().deviceInt64() : -1; + int32_t device_index = -1; + if (device_opt.has_value()) { + device_index = device_opt->index(); + } if (THPUtils_checkString(data)) { throw TypeError("new(): invalid data type '%s'", Py_TYPE(data)->tp_name); } if (THPVariable_Check(data)) { auto var = reinterpret_cast(data)->cdata; - auto type_inference_device_type = device_opt.has_value() ? device_opt.value().type + auto type_inference_device_type = device_opt.has_value() ? device_opt->type() : torch::getDeviceType(var.type()); // infer the scalar type and device type; it's not expected to infer the layout since these constructors // are defined per-layout-type (e.g. tensor vs sparse_coo_tensor). @@ -194,16 +210,16 @@ static Tensor internal_new_from_data(const Type & type, at::optional dev *torch::getLayout(type.backend()), type_inference_device_type); const auto& type_to_use = type_inference ? type_inference_type : type; - return copy_variables ? new_with_tensor_copy(type_to_use, var, device) : - new_with_type_conversion(type_to_use, var, device); + return copy_variables ? new_with_tensor_copy(type_to_use, var, device_index) : + new_with_type_conversion(type_to_use, var, device_index); } #ifdef USE_NUMPY if (PyArray_Check(data)) { auto tensor = autograd::make_variable(tensor_from_numpy(data), /*requires_grad=*/false); const auto& type_to_use = type_inference ? type.toScalarType(tensor.type().scalarType()) : type; - return copy_numpy ? new_with_tensor_copy(type_to_use, tensor, device) : - new_with_type_conversion(type_to_use, tensor, device); + return copy_numpy ? new_with_tensor_copy(type_to_use, tensor, device_index) : + new_with_type_conversion(type_to_use, tensor, device_index); } #endif @@ -214,25 +230,21 @@ static Tensor internal_new_from_data(const Type & type, at::optional dev (char*)tensor.data_ptr(), tensor.sizes(), tensor.strides(), 0, scalarType, tensor.type().elementSizeInBytes(), data); const auto& type_to_use = type_inference ? type.toScalarType(scalarType) : type; - return new_with_type_conversion(type_to_use, tensor, device); + return new_with_type_conversion(type_to_use, tensor, device_index); } -Tensor legacy_new_from_data(const Type & type, at::optional device, PyObject *data) { - return internal_new_from_data(type, device, data, false, false, false); -} - -static Tensor new_from_data_copy(const Type & type, at::optional device, PyObject *data) { +Tensor new_from_data_copy(const Type & type, at::optional device, PyObject *data) { return internal_new_from_data(type, device, data, true, true, false); } -static Tensor legacy_new_from_sequence(const Type & type, at::optional device, PyObject* data) { +Tensor legacy_new_from_sequence(const Type & type, at::optional device, PyObject* data) { if (!PySequence_Check(data)) { throw TypeError("new(): data must be a sequence (got %s)", Py_TYPE(data)->tp_name); } return legacy_new_from_data(type, device, data); } -static Tensor legacy_sparse_tensor_ctor(const Type& type, PyObject* args, PyObject* kwargs) { +Tensor legacy_sparse_tensor_ctor(const Type& type, PyObject* args, PyObject* kwargs) { static PythonArgParser parser({ "new(*, Device? device=None)", "new(*, int64_t cdata)|hidden", @@ -243,16 +255,15 @@ static Tensor legacy_sparse_tensor_ctor(const Type& type, PyObject* args, PyObje ParsedArgs<4> parsed_args; auto r = parser.parse(args, kwargs, parsed_args); if (r.idx == 0) { - AutoGPU auto_gpu(r.deviceInt64(0)); - return type.tensor(); + return at::empty({}, TensorOptions(type, r.device(0).index())); } else if (r.idx == 1) { auto cdata = reinterpret_cast(r.toInt64(0)); return type.unsafeTensorFromTH(cdata, true); } else if (r.idx == 2) { - AutoGPU auto_gpu(r.deviceInt64(2)); + at::DeviceGuard device_guard(r.device(2)); return type.sparse_coo_tensor(r.tensor(0), r.tensor(1)); } else if (r.idx == 3) { - AutoGPU auto_gpu(r.deviceInt64(3)); + at::DeviceGuard device_guard(r.device(3)); return type.sparse_coo_tensor(r.tensor(0), r.tensor(1), r.intlist(2)); } else if (r.idx == 4) { PyObject* arg = r.pyobject(0); @@ -261,11 +272,57 @@ static Tensor legacy_sparse_tensor_ctor(const Type& type, PyObject* args, PyObje // unless the sequences is a torch.Size return legacy_new_from_sequence(type, r.deviceOptional(1), r.pyobject(0)); } - return new_with_sizes(type, r.deviceInt64(1), r.intlist(0)); + return new_with_sizes(type, r.device(1).index(), r.intlist(0)); } throw std::runtime_error("new(): invalid arguments"); } +Tensor legacy_sparse_tensor_new(const Type& type, PyObject* args, PyObject* kwargs) { + static PythonArgParser parser({ + "new(*, Device? device=None)", + "new(*, int64_t cdata)|hidden", + "new(Tensor indices, Tensor values, *, Device? device=None)", + "new(Tensor indices, Tensor values, IntList size, *, Device? device=None)", + "new(IntList size, *, Device? device=None)", + }); + ParsedArgs<5> parsed_args; + auto r = parser.parse(args, kwargs, parsed_args); + if (r.idx == 0) { + at::DeviceGuard device_guard(r.device(0)); + return type.tensor(); + } else if (r.idx == 1) { + auto cdata = reinterpret_cast(r.device(0).index()); + return type.unsafeTensorFromTH(cdata, true); + } else if (r.idx == 2) { + // Note: this signature doesn't have a dtype, even though it has a device; it probably shouldn't + // have a device (we should infer it). + at::DeviceGuard device_guard(r.device(2)); + return type.sparse_coo_tensor(r.tensor(0), r.tensor(1)); + } else if (r.idx == 3) { + // Note: this signature doesn't have a dtype, even though it has a device; it probably shouldn't + // have a device (we should infer it). + at::DeviceGuard device_guard(r.device(3)); + return type.sparse_coo_tensor(r.tensor(0), r.tensor(1), r.intlist(2)); + } else if (r.idx == 4) { + PyObject* arg = r.pyobject(0); + if (!THPSize_Check(arg) && PyTuple_GET_SIZE(args) >= 1 && arg == PyTuple_GET_ITEM(args, 0)) { + // new(sequence) binds to this signature but should be treated differently + // unless the sequences is a torch.Size + return legacy_new_from_sequence(type, r.deviceOptional(1), r.pyobject(0)); + } + return new_with_sizes(type, r.device(1).index(), r.intlist(0)); + } + throw std::runtime_error("new(): invalid arguments"); +} + +const Type& typeWithDefault(PythonArgs& r, int64_t dtype_idx, int64_t device_idx, const Type& type) { + const auto scalartype = r.scalartypeWithDefault(dtype_idx, type.scalarType()); + const Device types_device_type(toDense(type.backend())); + const auto device_type = r.isNone(device_idx) ? types_device_type : r.device(device_idx).type(); + return torch::getType(scalartype, *torch::getLayout(type.backend()), device_type); +} +} // namespace + Tensor legacy_tensor_ctor(const Type& type, PyObject* args, PyObject* kwargs) { static PythonArgParser parser({ "new(*, Device? device=None)", @@ -283,7 +340,7 @@ Tensor legacy_tensor_ctor(const Type& type, PyObject* args, PyObject* kwargs) { ParsedArgs<2> parsed_args; auto r = parser.parse(args, kwargs, parsed_args); if (r.idx == 0) { - AutoGPU auto_gpu(r.deviceInt64(0)); + at::DeviceGuard device_guard(r.device(0)); return type.tensor(); } else if (r.idx == 1) { return new_with_storage(type, *r.storage(0)); @@ -299,51 +356,13 @@ Tensor legacy_tensor_ctor(const Type& type, PyObject* args, PyObject* kwargs) { // unless the sequences is a torch.Size return legacy_new_from_sequence(type, r.deviceOptional(1), r.pyobject(0)); } - return new_with_sizes(type, r.deviceInt64(1), r.intlist(0)); + return new_with_sizes(type, r.device(1).index(), r.intlist(0)); } else if (r.idx == 5) { return legacy_new_from_sequence(type, r.deviceOptional(1), r.pyobject(0)); } throw std::runtime_error("new(): invalid arguments"); } -static Tensor legacy_sparse_tensor_new(const Type& type, PyObject* args, PyObject* kwargs) { - static PythonArgParser parser({ - "new(*, Device? device=None)", - "new(*, int64_t cdata)|hidden", - "new(Tensor indices, Tensor values, *, Device? device=None)", - "new(Tensor indices, Tensor values, IntList size, *, Device? device=None)", - "new(IntList size, *, Device? device=None)", - }); - ParsedArgs<5> parsed_args; - auto r = parser.parse(args, kwargs, parsed_args); - if (r.idx == 0) { - AutoGPU auto_gpu(r.deviceInt64(0)); - return type.tensor(); - } else if (r.idx == 1) { - auto cdata = reinterpret_cast(r.deviceInt64(0)); - return type.unsafeTensorFromTH(cdata, true); - } else if (r.idx == 2) { - // Note: this signature doesn't have a dtype, even though it has a device; it probably shouldn't - // have a device (we should infer it). - AutoGPU auto_gpu(r.deviceInt64(2)); - return type.sparse_coo_tensor(r.tensor(0), r.tensor(1)); - } else if (r.idx == 3) { - // Note: this signature doesn't have a dtype, even though it has a device; it probably shouldn't - // have a device (we should infer it). - AutoGPU auto_gpu(r.deviceInt64(3)); - return type.sparse_coo_tensor(r.tensor(0), r.tensor(1), r.intlist(2)); - } else if (r.idx == 4) { - PyObject* arg = r.pyobject(0); - if (!THPSize_Check(arg) && PyTuple_GET_SIZE(args) >= 1 && arg == PyTuple_GET_ITEM(args, 0)) { - // new(sequence) binds to this signature but should be treated differently - // unless the sequences is a torch.Size - return legacy_new_from_sequence(type, r.deviceOptional(1), r.pyobject(0)); - } - return new_with_sizes(type, r.deviceInt64(1), r.intlist(0)); - } - throw std::runtime_error("new(): invalid arguments"); -} - Tensor legacy_tensor_new(const Type& type, PyObject* args, PyObject* kwargs) { static PythonArgParser parser({ "new(*, Device? device=None)", @@ -361,7 +380,7 @@ Tensor legacy_tensor_new(const Type& type, PyObject* args, PyObject* kwargs) { ParsedArgs<3> parsed_args; auto r = parser.parse(args, kwargs, parsed_args); if (r.idx == 0) { - AutoGPU auto_gpu(r.deviceInt64(0)); + at::DeviceGuard device_guard(r.device(0)); return type.tensor(); } else if (r.idx == 1) { return new_with_storage(type, *r.storage(0)); @@ -377,22 +396,19 @@ Tensor legacy_tensor_new(const Type& type, PyObject* args, PyObject* kwargs) { // unless the sequences is a torch.Size return legacy_new_from_sequence(type, r.deviceOptional(1), r.pyobject(0)); } - return new_with_sizes(type, r.deviceInt64(1), r.intlist(0)); + return new_with_sizes(type, r.device(1).index(), r.intlist(0)); } else if (r.idx == 5) { return legacy_new_from_sequence(type, r.deviceOptional(1), r.pyobject(0)); } throw std::runtime_error("new(): invalid arguments"); } -static const Type& typeWithDefault(PythonArgs& r, int64_t dtype_idx, int64_t device_idx, const Type& type) { - auto scalartype = r.scalartypeWithDefault(dtype_idx, type.scalarType()); - auto types_device_type = torch::getDeviceType(type); - auto device_type = r.isNone(device_idx) ? types_device_type : r.device(device_idx).type; - return torch::getType(scalartype, *torch::getLayout(type.backend()), device_type); +Tensor legacy_new_from_data(const Type & type, at::optional device, PyObject *data) { + return internal_new_from_data(type, device, data, false, false, false); } Tensor sparse_coo_tensor_ctor(const Type& type, PyObject* args, PyObject* kwargs) { - Backend sparse_backend = type.is_cuda() ? kSparseCUDA : kSparseCPU; + const auto sparse_backend = type.is_cuda() ? kSparseCUDA : kSparseCPU; const auto& default_sparse_type = type.toBackend(sparse_backend); static PythonArgParser parser({ @@ -406,23 +422,23 @@ Tensor sparse_coo_tensor_ctor(const Type& type, PyObject* args, PyObject* kwargs bool type_inference = r.isNone(2); const auto& sparse_type = typeWithDefault(r, 2, 3, default_sparse_type); const auto& dense_type = sparse_type.toBackend(sparse_type.is_cuda() ? kCUDA : kCPU); - AutoGPU autogpu(r.deviceInt64(3)); + at::DeviceGuard device_guard(r.device(3)); Tensor values = internal_new_from_data(dense_type, r.deviceOptional(3), r.pyobject(1), false, true, type_inference); // if no dtype provided, infer type based on value type. const auto& index_type = values.type().toScalarType(kLong); Tensor indices = internal_new_from_data(index_type, r.deviceOptional(3), r.pyobject(0), false, true, false); const auto& sparse_type_to_use = values.type().toBackend(values.type().is_cuda() ? kSparseCUDA : kSparseCPU); - return set_requires_grad(sparse_type_to_use.sparse_coo_tensor(indices, values), r.toBool(4)); + return sparse_type_to_use.sparse_coo_tensor(indices, values).set_requires_grad(r.toBool(4)); } else if (r.idx == 1) { bool type_inference = r.isNone(3); const auto& sparse_type = typeWithDefault(r, 3, 4, default_sparse_type); const auto& dense_type = sparse_type.toBackend(sparse_type.is_cuda() ? kCUDA : kCPU); - AutoGPU autogpu(r.deviceInt64(4)); + at::DeviceGuard device_guard(r.device(4)); Tensor values = internal_new_from_data(dense_type, r.deviceOptional(4), r.pyobject(1), false, true, type_inference); const auto& index_type = values.type().toScalarType(kLong); Tensor indices = internal_new_from_data(index_type, r.deviceOptional(4), r.pyobject(0), false, true, false); const auto& sparse_type_to_use = values.type().toBackend(values.type().is_cuda() ? kSparseCUDA : kSparseCPU); - return set_requires_grad(sparse_type_to_use.sparse_coo_tensor(indices, values, r.intlist(2)), r.toBool(5)); + return sparse_type_to_use.sparse_coo_tensor(indices, values, r.intlist(2)).set_requires_grad(r.toBool(5)); } throw std::runtime_error("sparse_coo_tensor(): invalid arguments"); } @@ -436,8 +452,14 @@ Tensor tensor_ctor(const Type& type, PyObject* args, PyObject* kwargs) { auto r = parser.parse(args, kwargs, parsed_args); if (r.idx == 0) { bool type_inference = r.isNone(1); - return set_requires_grad(internal_new_from_data( - typeWithDefault(r, 1, 2, type), r.deviceOptional(2), r.pyobject(0), true, true, type_inference), r.toBool(3)); + return internal_new_from_data( + typeWithDefault(r, 1, 2, type), + r.deviceOptional(2), + r.pyobject(0), + true, + true, + type_inference) + .set_requires_grad(r.toBool(3)); } throw std::runtime_error("tensor(): invalid arguments"); } @@ -466,13 +488,16 @@ Tensor new_tensor(const Type& type, PyObject* args, PyObject* kwargs) { ParsedArgs<4> parsed_args; auto r = parser.parse(args, kwargs, parsed_args); if (r.idx == 0) { - return set_requires_grad(new_from_data_copy( - typeWithDefault(r, 1, 2, type), r.deviceOptional(2), r.pyobject(0)), r.toBool(3)); + return new_from_data_copy( + typeWithDefault(r, 1, 2, type), + r.deviceOptional(2), + r.pyobject(0)) + .set_requires_grad(r.toBool(3)); } throw std::runtime_error("new_tensor(): invalid arguments"); } -Tensor new_empty(const at::Type& type, PyObject* args, PyObject* kwargs) { +Tensor new_empty(const Type& type, PyObject* args, PyObject* kwargs) { static PythonArgParser parser({ "new_empty(IntList size, *, ScalarType dtype=None, Device? device=None, bool requires_grad=False)", }); @@ -481,12 +506,12 @@ Tensor new_empty(const at::Type& type, PyObject* args, PyObject* kwargs) { auto r = parser.parse(args, kwargs, parsed_args); if (r.idx == 0) { const auto& actual_type = typeWithDefault(r, 1, 2, type); - return set_requires_grad(new_with_sizes(actual_type, r.deviceInt64(2), r.intlist(0)), r.toBool(3)); + return new_with_sizes(actual_type, r.device(2).index(), r.intlist(0)).set_requires_grad(r.toBool(3)); } throw std::runtime_error("new_empty(): invalid arguments"); } -Tensor new_full(const at::Type& type, PyObject* args, PyObject* kwargs) { +Tensor new_full(const Type& type, PyObject* args, PyObject* kwargs) { static PythonArgParser parser({ "new_full(IntList size, Scalar fill_value, *, ScalarType dtype=None, Device? device=None, bool requires_grad=False)", }); @@ -495,12 +520,12 @@ Tensor new_full(const at::Type& type, PyObject* args, PyObject* kwargs) { auto r = parser.parse(args, kwargs, parsed_args); if (r.idx == 0) { const auto& actual_type = typeWithDefault(r, 2, 3, type); - return set_requires_grad(dispatch_full(actual_type, r.scalar(1), r.deviceInt64(3), r.intlist(0)), r.toBool(4)); + return dispatch_full(actual_type, r.scalar(1), r.device(3).index(), r.intlist(0)).set_requires_grad(r.toBool(4)); } throw std::runtime_error("new_full(): invalid arguments"); } -Tensor new_ones(const at::Type& type, PyObject* args, PyObject* kwargs) { +Tensor new_ones(const Type& type, PyObject* args, PyObject* kwargs) { static PythonArgParser parser({ "new_ones(IntList size, *, ScalarType dtype=None, Device? device=None, bool requires_grad=False)", }); @@ -509,12 +534,12 @@ Tensor new_ones(const at::Type& type, PyObject* args, PyObject* kwargs) { auto r = parser.parse(args, kwargs, parsed_args); if (r.idx == 0) { const auto& actual_type = typeWithDefault(r, 1, 2, type); - return set_requires_grad(dispatch_ones(actual_type, r.deviceInt64(2), r.intlist(0)), r.toBool(3)); + return dispatch_ones(actual_type, r.device(2).index(), r.intlist(0)).set_requires_grad(r.toBool(3)); } throw std::runtime_error("new_ones(): invalid arguments"); } -Tensor new_zeros(const at::Type& type, PyObject* args, PyObject* kwargs) { +Tensor new_zeros(const Type& type, PyObject* args, PyObject* kwargs) { static PythonArgParser parser({ "new_zeros(IntList size, *, ScalarType dtype=None, Device? device=None, bool requires_grad=False)", }); @@ -523,7 +548,7 @@ Tensor new_zeros(const at::Type& type, PyObject* args, PyObject* kwargs) { auto r = parser.parse(args, kwargs, parsed_args); if (r.idx == 0) { const auto& actual_type = typeWithDefault(r, 1, 2, type); - return set_requires_grad(dispatch_zeros(actual_type, r.deviceInt64(2), r.intlist(0)), r.toBool(3)); + return dispatch_zeros(actual_type, r.device(2).index(), r.intlist(0)).set_requires_grad(r.toBool(3)); } throw std::runtime_error("new_zeros(): invalid arguments"); } diff --git a/torch/csrc/utils/tensor_new.h b/torch/csrc/utils/tensor_new.h index 2fa1c6ebead6..39993a5b6abc 100644 --- a/torch/csrc/utils/tensor_new.h +++ b/torch/csrc/utils/tensor_new.h @@ -1,14 +1,14 @@ #pragma once #include "torch/csrc/python_headers.h" -#include "torch/csrc/utils/device.h" + #include namespace torch { namespace utils { at::Tensor legacy_tensor_ctor(const at::Type& type, PyObject* args, PyObject* kwargs); at::Tensor legacy_tensor_new(const at::Type& type, PyObject* args, PyObject* kwargs); -at::Tensor legacy_new_from_data(const at::Type& type, at::optional device, PyObject *data); +at::Tensor legacy_new_from_data(const at::Type& type, at::optional device, PyObject *data); at::Tensor sparse_coo_tensor_ctor(const at::Type& type, PyObject* args, PyObject* kwargs); at::Tensor tensor_ctor(const at::Type& type, PyObject* args, PyObject* kwargs); at::Tensor as_tensor(const at::Type& type, PyObject* args, PyObject* kwargs); diff --git a/torch/lib/THD/base/data_channels/DataChannelMPI.cpp b/torch/lib/THD/base/data_channels/DataChannelMPI.cpp index 99d02f4237b0..2d9b91749299 100644 --- a/torch/lib/THD/base/data_channels/DataChannelMPI.cpp +++ b/torch/lib/THD/base/data_channels/DataChannelMPI.cpp @@ -135,8 +135,8 @@ rank_type DataChannelMPI::getNumProcesses() { return _num_processes; } -struct AutoGPU { - AutoGPU(int new_device) { +struct DeviceGuard { + DeviceGuard(int new_device) { if (new_device == -1) return; #ifdef USE_CUDA cudaGetDevice(&device_); @@ -144,7 +144,7 @@ struct AutoGPU { #endif } - ~AutoGPU() { + ~DeviceGuard() { if (device_ == -1) return; #ifdef USE_CUDA cudaSetDevice(device_); @@ -159,7 +159,7 @@ at::Tensor DataChannelMPI::_newLikeFlat(std::vector& tensors) const if (tensors.size() == 0) throw std::runtime_error("received an empty list"); auto & t = tensors[0]; - AutoGPU gpu_guard { t.is_cuda() ? static_cast(t.get_device()) : -1 }; + DeviceGuard gpu_guard { t.is_cuda() ? static_cast(t.get_device()) : -1 }; std::vector sizes { static_cast(tensors.size()) }; // sizes = [output.size()] + input.sizes() sizes.insert(sizes.end(), t.sizes().begin(), t.sizes().end()); return t.type().tensor(sizes); diff --git a/torch/lib/THD/base/data_channels/DataChannelNccl.cpp b/torch/lib/THD/base/data_channels/DataChannelNccl.cpp index c5aa1429cfa8..47c0840fa1e5 100644 --- a/torch/lib/THD/base/data_channels/DataChannelNccl.cpp +++ b/torch/lib/THD/base/data_channels/DataChannelNccl.cpp @@ -1,5 +1,4 @@ #include "../Cuda.hpp" -#include "../../../../csrc/utils/auto_gpu.h" #include "DataChannelNccl.hpp" #include "DataChannelUtils.hpp" @@ -159,7 +158,7 @@ void DataChannelNccl::destroy() { _destroySockets(); // Guard GPU device - AutoGPU gpuGuard; + at::DeviceGuard gpuGuard; /** * Destroy the CUDA and NCCL resources @@ -186,7 +185,7 @@ void DataChannelNccl::_destroyNcclResources(THDGroup groupId) { // Devices used for this group ID auto devices = getDevicesList(_groupDevices[groupId][i]); // Guard GPU device - AutoGPU gpuGuard; + at::DeviceGuard gpuGuard; // Destroy the CUDA events size_t idx = 0; for (auto& event : *(_groupNcclResources[groupId][i].ncclCudaEvents())) { @@ -301,7 +300,7 @@ NcclResourcePair DataChannelNccl::_getNcclResourcePair( broadcastUniqueNcclId(&ncclId); // Guard GPU device - AutoGPU gpuGuard; + at::DeviceGuard gpuGuard; // Now creating the CUDA events for (size_t i = 0; i < input.size(); ++i) { @@ -422,7 +421,7 @@ void DataChannelNccl::allReduce(std::vector& data, auto events = ncclResourcePair.second; // Guard GPU device - AutoGPU gpuGuard; + at::DeviceGuard gpuGuard; std::unique_lock cudaFreeMutexLock( *(THCCachingAllocator_getCudaFreeMutex())); @@ -473,7 +472,7 @@ void DataChannelNccl::allGather(std::vector& output, auto events = ncclResourcePair.second; // Guard GPU device - AutoGPU gpuGuard; + at::DeviceGuard gpuGuard; std::unique_lock cudaFreeMutexLock( *(THCCachingAllocator_getCudaFreeMutex())); @@ -525,7 +524,7 @@ void DataChannelNccl::reduce(std::vector& data, auto events = ncclResourcePair.second; // Guard GPU device - AutoGPU gpuGuard; + at::DeviceGuard gpuGuard; std::unique_lock cudaFreeMutexLock( *(THCCachingAllocator_getCudaFreeMutex())); @@ -577,7 +576,7 @@ void DataChannelNccl::broadcast(std::vector& data, auto events = ncclResourcePair.second; // Guard GPU device - AutoGPU gpuGuard; + at::DeviceGuard gpuGuard; std::unique_lock cudaFreeMutexLock( *(THCCachingAllocator_getCudaFreeMutex()));