Skip to content

Commit 6d0fb85

Browse files
bdhirshfacebook-github-bot
authored andcommitted
Revert D28833086: beef up at::_ops API
Test Plan: revert-hammer Differential Revision: D28833086 (pytorch@e2129d1) Original commit changeset: 55f322a8378c fbshipit-source-id: e55bf812ec411bb6bee87654f1d65ff10c046106
1 parent 0cbb5e1 commit 6d0fb85

File tree

18 files changed

+510
-495
lines changed

18 files changed

+510
-495
lines changed

BUILD.bazel

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,22 +136,20 @@ genrule(
136136
"aten/src/ATen/RegisterMeta.cpp",
137137
"aten/src/ATen/RegisterSchema.cpp",
138138
"aten/src/ATen/CPUFunctions.h",
139-
"aten/src/ATen/CPUFunctions_inl.h",
140139
"aten/src/ATen/CUDAFunctions.h",
141-
"aten/src/ATen/CUDAFunctions_inl.h",
142140
"aten/src/ATen/CompositeExplicitAutogradFunctions.h",
143-
"aten/src/ATen/CompositeExplicitAutogradFunctions_inl.h",
144141
"aten/src/ATen/CompositeImplicitAutogradFunctions.h",
145-
"aten/src/ATen/CompositeImplicitAutogradFunctions_inl.h",
146142
"aten/src/ATen/Functions.h",
143+
"aten/src/ATen/Functions.cpp",
147144
"aten/src/ATen/RedispatchFunctions.h",
145+
"aten/src/ATen/RedispatchFunctions.cpp",
148146
"aten/src/ATen/Operators.h",
149147
"aten/src/ATen/Operators.cpp",
150148
"aten/src/ATen/NativeFunctions.h",
151149
"aten/src/ATen/MetaFunctions.h",
152-
"aten/src/ATen/MetaFunctions_inl.h",
153150
"aten/src/ATen/NativeMetaFunctions.h",
154151
"aten/src/ATen/core/TensorBody.h",
152+
"aten/src/ATen/core/TensorMethods.cpp",
155153
"aten/src/ATen/core/ATenOpList.cpp",
156154
],
157155
cmd = "$(location :gen) --source-path aten/src/ATen --install_dir `dirname $(location aten/src/ATen/Declarations.yaml)`",

aten/src/ATen/core/op_registration/adaption.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,26 @@
4343
namespace c10 {
4444
namespace impl {
4545

46+
inline c10::optional<MemoryFormat>
47+
check_tensor_options_and_extract_memory_format(
48+
const TensorOptions& options,
49+
c10::optional<MemoryFormat> memory_format) {
50+
TORCH_CHECK(
51+
options.requires_grad_opt() == c10::nullopt ||
52+
options.requires_grad_opt().value() == false,
53+
"Operators taking TensorOptions cannot take a TensorOptions with "
54+
"options.requires_grad set as true. This isn't implemented yet.");
55+
TORCH_CHECK(
56+
!(options.has_memory_format() && memory_format.has_value()),
57+
"Cannot set memory_format both in TensorOptions and explicit argument; please delete "
58+
"the redundant setter.");
59+
if (memory_format.has_value()) {
60+
return memory_format;
61+
} else {
62+
return options.memory_format_opt();
63+
}
64+
}
65+
4666
TORCH_API void common_device_check_failure(optional<Device>& common_device, const at::Tensor& tensor, at::CheckedFrom methodName, at::CheckedFrom argName);
4767

4868
inline void check_and_update_common_device(optional<Device>& common_device, const at::Tensor& tensor, at::CheckedFrom methodName, at::CheckedFrom argName) {
Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,14 @@
1-
#include <ATen/core/TensorBody.h>
2-
// Note [Avoiding Include Cycles In Static Dispatch]
3-
// In order to avoid #include cycles in the static dispatch build, we've carefully split out
4-
// the static function definition files into {DispatchKey}Functions.h and {DispatchKey}Functions_inl.h.
5-
//
6-
// Without this split, the include cycle looks like TensorBody.h -> CPUFunctions.h -> TensorBody.h.
7-
// - TensorBody.h #includes CPUFunctions.h in the static dispatch build, because the tensor methods
8-
// all need to call into the fastpath C++ API defined in CPUFunctions.h. The methods are also all
9-
// directly inlined into TensorBody.h.
10-
// - CPUFunctions.h #includes TensorBody.h because it contains function declarations for the entire C++ API,
11-
// which include functions that have defaultable optional<Tensor> arguments.
12-
// That requires knowing the full Tensor class definition.
13-
//
14-
// We break the cycle by doing the following:
15-
// - Split out CPUFunction.h into two files: CPUFunctions.h and CPUFunctions_inl.h
16-
// - CPUFunction.h is a dummy file that just includes the Tensor class and includes CPUFunctions_inl.,
17-
// - CPUFunctions_inl.h includes everything else
18-
// - (only in the static dispatch build) TensorBody.h makes sure to finish defining the Tensor class,
19-
// and then it includes CPUFunctions_inl.h.
20-
// - All other files that want the cpu fastpath functions can include CPUFunctions.h directly.
21-
// - This also means that static dispatch build, CPUFunctions.h only needs to
22-
// #include TensorBody.h, and it will automatically bring in CPUFunctions_inl.h.
23-
${inline_headers_for_nonstatic_build}
1+
// ${generated_comment}
2+
3+
// NB: The implementing C++ file is RegisterDispatchKey.cpp
4+
5+
// TODO: tighten this include
6+
#include <ATen/Functions.h>
7+
8+
namespace at {
9+
namespace ${dispatch_namespace} {
10+
11+
${dispatch_namespaced_declarations}
12+
13+
} // namespace ${dispatch_namespace}
14+
} // namespace at

aten/src/ATen/templates/DispatchKeyFunctions_inl.h

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
// ${generated_comment}
2+
3+
#include <array>
4+
5+
#include <ATen/Functions.h>
6+
#include <ATen/Utils.h>
7+
8+
#include <ATen/core/dispatch/Dispatcher.h>
9+
#include <ATen/core/op_registration/adaption.h>
10+
11+
${static_dispatch_extra_headers}
12+
13+
namespace at {
14+
15+
Tensor var(const Tensor& self, int dim) {
16+
return at::var(self, IntArrayRef{dim});
17+
}
18+
19+
std::tuple<Tensor, Tensor> var_mean(const Tensor& self, int dim) {
20+
return at::var_mean(self, IntArrayRef{dim});
21+
}
22+
23+
Tensor std(const Tensor& self, int dim) {
24+
return at::std(self, IntArrayRef{dim});
25+
}
26+
27+
std::tuple<Tensor, Tensor> std_mean(const Tensor& self, int dim) {
28+
return at::std_mean(self, IntArrayRef{dim});
29+
}
30+
31+
at::Tensor conv1d(
32+
const Tensor& input,
33+
const Tensor& weight,
34+
const Tensor& bias,
35+
IntArrayRef stride,
36+
std::initializer_list<int64_t> padding_,
37+
IntArrayRef dilation,
38+
int64_t groups) {
39+
auto padding = IntArrayRef(padding_);
40+
return at::conv1d(input, weight, bias, stride, padding, dilation, groups);
41+
}
42+
43+
at::Tensor conv2d(
44+
const Tensor& input,
45+
const Tensor& weight,
46+
const Tensor& bias,
47+
IntArrayRef stride,
48+
std::initializer_list<int64_t> padding_,
49+
IntArrayRef dilation,
50+
int64_t groups) {
51+
auto padding = IntArrayRef(padding_);
52+
return at::conv2d(input, weight, bias, stride, padding, dilation, groups);
53+
}
54+
55+
at::Tensor conv3d(
56+
const Tensor& input,
57+
const Tensor& weight,
58+
const Tensor& bias,
59+
IntArrayRef stride,
60+
std::initializer_list<int64_t> padding_,
61+
IntArrayRef dilation,
62+
int64_t groups) {
63+
auto padding = IntArrayRef(padding_);
64+
return at::conv3d(input, weight, bias, stride, padding, dilation, groups);
65+
}
66+
67+
namespace detail {
68+
69+
void noopDelete(void*) {}
70+
71+
} // namespace detail
72+
73+
Tensor TensorMaker::make_tensor() {
74+
AutoDispatchBelowADInplaceOrView guard{}; // TODO: Remove.
75+
tracer::impl::NoTracerDispatchMode tracer_guard{};
76+
77+
check_size_nonnegative(sizes_);
78+
79+
TORCH_CHECK_VALUE(
80+
!deleter_ || !ctx_,
81+
"The deleter and context arguments are mutually exclusive.");
82+
83+
if (device_ == nullopt) {
84+
device_ = globalContext().getDeviceFromPtr(data_, opts_.device().type());
85+
}
86+
87+
if (opts_.device().has_index()) {
88+
// clang-format off
89+
TORCH_CHECK_VALUE(
90+
opts_.device() == *device_,
91+
"Specified device ", opts_.device(), " does not match device of data ", *device_);
92+
// clang-format on
93+
}
94+
95+
std::size_t size_bytes = computeStorageSize();
96+
97+
DataPtr data_ptr{};
98+
if (deleter_) {
99+
data_ptr = makeDataPtrFromDeleter();
100+
} else {
101+
data_ptr = makeDataPtrFromContext();
102+
}
103+
104+
Storage storage{Storage::use_byte_size_t{}, size_bytes, std::move(data_ptr)};
105+
106+
Tensor tensor = detail::make_tensor<TensorImpl>(
107+
std::move(storage), opts_.computeDispatchKey(), opts_.dtype());
108+
109+
if (sizes_.size() != 1 || sizes_[0] != 0) {
110+
TensorImpl* tensor_impl = tensor.unsafeGetTensorImpl();
111+
112+
if (strides_) {
113+
tensor_impl->set_sizes_and_strides(sizes_, *strides_);
114+
} else {
115+
tensor_impl->set_sizes_contiguous(sizes_);
116+
}
117+
}
118+
119+
return tensor;
120+
}
121+
122+
std::size_t TensorMaker::computeStorageSize() const noexcept {
123+
std::size_t itemsize = opts_.dtype().itemsize();
124+
125+
if (strides_) {
126+
return detail::computeStorageNbytes(sizes_, *strides_, itemsize);
127+
}
128+
129+
std::size_t size = 1;
130+
for (std::int64_t s : sizes_) {
131+
size *= static_cast<std::size_t>(s);
132+
}
133+
return size * itemsize;
134+
}
135+
136+
inline DataPtr TensorMaker::makeDataPtrFromDeleter() const {
137+
return InefficientStdFunctionContext::makeDataPtr(data_, deleter_, *device_);
138+
}
139+
140+
inline DataPtr TensorMaker::makeDataPtrFromContext() noexcept {
141+
return DataPtr{data_, ctx_.release(), ctx_.get_deleter(), *device_};
142+
}
143+
144+
IntArrayRef TensorMaker::makeTempSizes() const noexcept {
145+
static std::int64_t zeros[5] = {0, 0, 0, 0, 0};
146+
if (opts_.has_memory_format()) {
147+
MemoryFormat format = *opts_.memory_format_opt();
148+
if (format == MemoryFormat::ChannelsLast) {
149+
return IntArrayRef(zeros, 4);
150+
}
151+
if (format == MemoryFormat::ChannelsLast3d) {
152+
return IntArrayRef(zeros, 5);
153+
}
154+
}
155+
return IntArrayRef(zeros, 1);
156+
}
157+
158+
${function_definitions}
159+
160+
} // namespace at

0 commit comments

Comments
 (0)