1 #include <torch/csrc/utils/tensor_apply.h> 3 #include <ATen/TensorUtils.h> 4 #include <ATen/ExpandUtils.h> 6 #include <torch/csrc/Exceptions.h> 7 #include <torch/csrc/utils/python_numbers.h> 8 #include <torch/csrc/utils/python_scalars.h> 12 namespace torch {
namespace utils {
16 : data(tensor.data_ptr())
17 , strides(tensor.strides())
18 , elementSize(tensor.element_size()) {}
25 data = (
char*)data + (strides[dim] * elementSize);
30 static void recursive_apply(
IntArrayRef sizes, ScalarType scalarType, int64_t dim,
31 PyObject* fn, std::array<StridedData, N> strided_data) {
32 int64_t ndim = sizes.
size();
36 for (
size_t i = 0; i < N; i++) {
37 PyObject* arg = load_scalar(strided_data[i].data, scalarType);
39 PyTuple_SET_ITEM(args.get(), i, arg);
41 auto ret =
THPObjectPtr(PyObject_CallObject(fn, args.get()));
43 store_scalar(strided_data[0].data, scalarType, ret.get());
48 for (int64_t i = 0; i < n; i++) {
49 recursive_apply(sizes, scalarType, dim + 1, fn, strided_data);
50 for (
auto& td : strided_data) {
57 if (
self.type().backend() != Backend::CPU) {
58 throw TypeError(
"apply_ is only implemented on CPU tensors");
60 auto scalarType =
self.scalar_type();
61 recursive_apply<1>(
self.sizes(), scalarType, 0, fn, {{
self }});
66 if (
self.type().backend() != Backend::CPU) {
67 throw TypeError(
"map_ is only implemented on CPU tensors");
69 if (other_.type() !=
self.type()) {
70 throw TypeError(
"map_: expected %s for 'other' (got %s)",
71 self.type().toString(), other_.type().toString());
74 std::tie(other) = expand_inplace(
self, other_,
"map_");
75 auto scalarType =
self.scalar_type();
76 recursive_apply<2>(
self.sizes(), scalarType, 0, fn, {{
self, other }});
81 if (
self.type().backend() != Backend::CPU || x_.type().backend() != Backend::CPU || y_.type().backend() != Backend::CPU) {
82 throw TypeError(
"map2_ is only implemented on CPU tensors");
84 if (x_.type() !=
self.type()) {
85 throw TypeError(
"map2_: expected %s for argument 'x' (got %s)",
86 self.type().toString(), x_.type().toString());
88 if (y_.type() !=
self.type()) {
89 throw TypeError(
"map2_: expected %s for argument 'y' (got %s)",
90 self.type().toString(), y_.type().toString());
93 std::tie(other1, other2) = expand_inplace(
self, x_, y_,
"map2_");
94 auto scalarType =
self.scalar_type();
95 recursive_apply<3>(
self.sizes(), scalarType, 0, fn, {{
self, other1, other2 }});
constexpr size_t size() const
size - Get the array size.
Flush-To-Zero and Denormals-Are-Zero mode.