diff --git a/aten/src/ATen/TensorOptions.h b/aten/src/ATen/TensorOptions.h index 0dc0b64a7780..2b589e9b13f4 100644 --- a/aten/src/ATen/TensorOptions.h +++ b/aten/src/ATen/TensorOptions.h @@ -1,12 +1,9 @@ #pragma once #include -#include #include -#include #include #include -#include #include #include @@ -62,16 +59,6 @@ struct AT_API TensorOptions { /// - requires_grad: false explicit TensorOptions(bool use_thread_local_default_options); - /// Constructs the `TensorOptions` from a type and a `device_index`. - /* implicit */ TensorOptions( - const Type& type, - int32_t device_index = -1) { - this->dtype(type.scalarType()); - this->device({backendToDeviceType(type.backend()), device_index}); - this->layout(type.layout()); - this->is_variable(type.is_variable()); - } - /// Constructs a `TensorOptions` object with the given layout. /* implicit */ TensorOptions(Layout layout) : TensorOptions() { this->layout(layout); diff --git a/aten/src/ATen/function_wrapper.py b/aten/src/ATen/function_wrapper.py index 45b0648254de..9c6c52891ef4 100644 --- a/aten/src/ATen/function_wrapper.py +++ b/aten/src/ATen/function_wrapper.py @@ -89,9 +89,8 @@ ${return_type} TypeDefault::${api_name}(${type_method_formals}) const { """) DEPRECATED_TYPE_METHOD_DEFINITION_CONCRETE = CodeTemplate("""\ ${return_type} TypeDefault::${api_name}(${type_method_formals}) const { - TensorOptions options(*this); ${device_guard_declaration} - return at::native::${api_name}(${type_method_actuals}, options); + return at::native::${api_name}(${type_method_actuals}, options()); } """) # 4. add virtual override to TypeDerived.h @@ -165,7 +164,7 @@ static inline ${return_type} ${api_name}(${formals}) { # 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})); + return at::${api_name}(${type_method_actuals}, ${inferred_type}.options()); } """) diff --git a/aten/src/ATen/templates/TensorMethods.h b/aten/src/ATen/templates/TensorMethods.h index 3fc61244574a..dbed52323227 100644 --- a/aten/src/ATen/templates/TensorMethods.h +++ b/aten/src/ATen/templates/TensorMethods.h @@ -7,6 +7,8 @@ #include "ATen/core/SparseTensorRef.h" #include "ATen/Type.h" #include "ATen/TensorOptions.h" +#include "ATen/DeviceGuard.h" +#include "ATen/Context.h" namespace at { diff --git a/aten/src/ATen/templates/Type.h b/aten/src/ATen/templates/Type.h index 0296deaab8e1..c564957c354d 100644 --- a/aten/src/ATen/templates/Type.h +++ b/aten/src/ATen/templates/Type.h @@ -15,6 +15,7 @@ #include "ATen/core/Half.h" #include "ATen/core/TensorTypeIdRegistration.h" #include "ATen/core/Reduction.h" +#include "ATen/TensorOptions.h" #include #include @@ -110,6 +111,20 @@ struct AT_API Type { return this != &other; } + /// Constructs the `TensorOptions` from a type and a `device_index`. + TensorOptions options(int32_t device_index = -1) const { + TensorOptions r; + r.dtype(scalarType()); + r.device({backendToDeviceType(backend()), device_index}); + r.layout(layout()); + r.is_variable(is_variable()); + return r; + } + + operator TensorOptions() const { + return options(); + } + // example // virtual Tensor * add(Tensor & a, Tensor & b) = 0; ${pure_virtual_type_method_declarations} diff --git a/test/cpp/api/tensor_options_cuda.cpp b/test/cpp/api/tensor_options_cuda.cpp index a5cc951b3bb7..9b5a5236ce9b 100644 --- a/test/cpp/api/tensor_options_cuda.cpp +++ b/test/cpp/api/tensor_options_cuda.cpp @@ -22,22 +22,22 @@ using namespace at; REQUIRE(tensor.type().layout() == (layout_)) TEST_CASE("TensorOptions/ConstructsWellFromCUDATypes", "[cuda]") { - auto options = TensorOptions(CUDA(kFloat)); + auto options = CUDA(kFloat).options(); REQUIRE_OPTIONS(kCUDA, -1, kFloat, kStrided); - options = TensorOptions(CUDA(kInt)); + options = CUDA(kInt).options(); REQUIRE_OPTIONS(kCUDA, -1, kInt, kStrided); - options = TensorOptions(getNonVariableType(Backend::SparseCUDA, kFloat)); + options = getNonVariableType(Backend::SparseCUDA, kFloat).options(); REQUIRE_OPTIONS(kCUDA, -1, kFloat, kSparse); - options = TensorOptions(getNonVariableType(Backend::SparseCUDA, kByte)); + options = getNonVariableType(Backend::SparseCUDA, kByte).options(); REQUIRE_OPTIONS(kCUDA, -1, kByte, kSparse); - options = TensorOptions(CUDA(kFloat), /*device=*/5); + options = CUDA(kFloat).options(/*device=*/5); REQUIRE_OPTIONS(kCUDA, 5, kFloat, kStrided); - options = TensorOptions(getNonVariableType(Backend::SparseCUDA, kFloat), /*device=*/5); + options = getNonVariableType(Backend::SparseCUDA, kFloat).options(/*device=*/5); REQUIRE_OPTIONS(kCUDA, 5, kFloat, kSparse); } diff --git a/torch/csrc/autograd/input_metadata.h b/torch/csrc/autograd/input_metadata.h index 7dfa951439c7..ef107aa565a0 100644 --- a/torch/csrc/autograd/input_metadata.h +++ b/torch/csrc/autograd/input_metadata.h @@ -36,7 +36,7 @@ struct InputMetadata { } at::Tensor zeros_like() const { - return at::zeros(shape_, at::TensorOptions(*type_, static_cast(device_))); + return at::zeros(shape_, type_->options(static_cast(device_))); } private: diff --git a/torch/csrc/utils/tensor_new.cpp b/torch/csrc/utils/tensor_new.cpp index ed8afc81b322..7dc6ff6ceb35 100644 --- a/torch/csrc/utils/tensor_new.cpp +++ b/torch/csrc/utils/tensor_new.cpp @@ -50,25 +50,25 @@ void maybe_initialize_cuda(const Type &type) { Tensor dispatch_zeros(const Type& type, int32_t device_index, IntList sizes) { maybe_initialize_cuda(type); AutoNoGIL no_gil; - return torch::zeros(sizes, TensorOptions(type, device_index)); + return torch::zeros(sizes, type.options(device_index)); } Tensor dispatch_ones(const Type& type, int32_t device_index, IntList sizes) { maybe_initialize_cuda(type); AutoNoGIL no_gil; - return torch::ones(sizes, TensorOptions(type, device_index)); + return torch::ones(sizes, type.options(device_index)); } Tensor dispatch_full(const Type& type, Scalar fill_value, int32_t device_index, IntList sizes) { maybe_initialize_cuda(type); AutoNoGIL no_gil; - return torch::full(sizes, fill_value, TensorOptions(type, device_index)); + return torch::full(sizes, fill_value, type.options(device_index)); } Tensor new_with_sizes(const Type& type, int32_t device_index, IntList sizes) { maybe_initialize_cuda(type); AutoNoGIL no_gil; - return torch::empty(sizes, TensorOptions(type, device_index)); + return torch::empty(sizes, type.options(device_index)); } Tensor new_with_storage(const Type& type, Storage storage) { @@ -256,7 +256,7 @@ Tensor legacy_sparse_tensor_ctor(const Type& type, PyObject* args, PyObject* kwa ParsedArgs<4> parsed_args; auto r = parser.parse(args, kwargs, parsed_args); if (r.idx == 0) { - return at::empty({0}, TensorOptions(type, r.device(0).index())); + return at::empty({0}, type.options(r.device(0).index())); } else if (r.idx == 1) { auto cdata = reinterpret_cast(r.toInt64(0)); return type.unsafeTensorFromTH(cdata, true);