Remove Type constructor from TensorOptions, add Type::options (#11189)

Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/11189

Replaces it with an operator TensorOptions() method on
Type, reestablishing the implicit conversion.  I originally
wanted to get rid of the implicit conversion entirely, but
there were a *lot* of use-sites, so I added it back to avoid
a huge codemod.  In this patch, I only had to fix sites that
used the optional device_index API.

Reviewed By: cpuhrsch

Differential Revision: D9628281

fbshipit-source-id: 5fe2a68eefb77a3c9bb446f03a94ad723ef90210
This commit is contained in:
Edward Yang
2018-09-04 08:01:22 -07:00
committed by Facebook Github Bot
parent 0d5e4a2c66
commit 0ff1bb0d8a
7 changed files with 31 additions and 28 deletions
-13
View File
@@ -1,12 +1,9 @@
#pragma once
#include <ATen/core/Backend.h>
#include <ATen/Context.h>
#include <ATen/core/Device.h>
#include <ATen/DeviceGuard.h>
#include <ATen/core/Layout.h>
#include <ATen/core/ScalarType.h>
#include <ATen/Type.h>
#include <cstddef>
#include <iosfwd>
@@ -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);
+2 -3
View File
@@ -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());
}
""")
+2
View File
@@ -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 {
+15
View File
@@ -15,6 +15,7 @@
#include "ATen/core/Half.h"
#include "ATen/core/TensorTypeIdRegistration.h"
#include "ATen/core/Reduction.h"
#include "ATen/TensorOptions.h"
#include <array>
#include <cstddef>
@@ -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}
+6 -6
View File
@@ -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);
}
+1 -1
View File
@@ -36,7 +36,7 @@ struct InputMetadata {
}
at::Tensor zeros_like() const {
return at::zeros(shape_, at::TensorOptions(*type_, static_cast<int32_t>(device_)));
return at::zeros(shape_, type_->options(static_cast<int32_t>(device_)));
}
private:
+5 -5
View File
@@ -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<void*>(r.toInt64(0));
return type.unsafeTensorFromTH(cdata, true);