forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTensorFactory.cpp
More file actions
166 lines (149 loc) · 5.42 KB
/
TensorFactory.cpp
File metadata and controls
166 lines (149 loc) · 5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright © 2022 Apple Inc.
#include <ATen/ATen.h>
#include <ATen/Tensor.h>
#include <ATen/Utils.h>
#include <torch/library.h>
#include <ATen/mps/EmptyTensor.h>
#include <ATen/mps/MPSDevice.h>
#include <ATen/native/Resize.h>
#include <ATen/native/ResizeCommon.h>
#include <ATen/native/mps/Copy.h>
#include <ATen/native/mps/TensorFactory.h>
#include <ATen/Dispatch.h>
#ifndef AT_PER_OPERATOR_HEADERS
#include <ATen/Functions.h>
#include <ATen/NativeFunctions.h>
#endif
#include <ATen/ops/_efficientzerotensor_native.h>
#include <utility>
namespace at::native {
static inline void maybe_resize_storage_mps(TensorImpl* self, uint64_t new_size) {
if (new_size == 0) {
return;
}
auto storage = self->storage().unsafeGetStorageImpl();
if (!storage) {
TORCH_CHECK(false, "Tensor: invalid null storage");
}
uint64_t new_size_bytes = (new_size + self->storage_offset()) * self->dtype().itemsize();
if (new_size_bytes > self->storage().nbytes()) {
if (new_size_bytes == 0) {
storage->set_data_ptr_noswap(at::DataPtr(nullptr, at::Device(at::DeviceType::MPS, 0)));
storage->set_nbytes(0);
} else {
at::DataPtr new_data = storage->allocator()->allocate(new_size_bytes);
size_t copy_capacity = std::min<size_t>(new_size_bytes, storage->nbytes());
if (storage->data() && copy_capacity > 0) {
at::native::mps::copy_blit_mps(new_data.get(), storage->data(), copy_capacity);
}
// Destructively overwrite data_ptr
storage->set_data_ptr_noswap(std::move(new_data));
storage->set_nbytes(new_size_bytes);
}
}
}
inline TensorImpl* resize_impl_mps_(
TensorImpl* self,
IntArrayRef size,
std::optional<IntArrayRef> stride,
bool device_guard = true) {
if (self->sizes() == size && (!stride || self->strides() == stride)) {
return self;
}
int64_t storage_size = 1;
if (stride) {
self->set_sizes_and_strides(size, *stride);
// NB: storage size can be different from numel.
storage_size = storage_size_for(size, *stride);
} else {
self->set_sizes_contiguous(size);
storage_size = self->numel();
}
maybe_resize_storage_mps(self, storage_size);
return self;
}
Tensor empty_mps(
IntArrayRef size,
std::optional<ScalarType> dtype_opt,
std::optional<Layout> layout_opt,
std::optional<Device> device_opt,
std::optional<bool> pin_memory_opt,
std::optional<c10::MemoryFormat> memory_format_opt) {
return at::detail::empty_mps(size, dtype_opt, layout_opt, device_opt, pin_memory_opt, memory_format_opt);
}
Tensor empty_strided_mps(
IntArrayRef size,
IntArrayRef stride,
std::optional<ScalarType> dtype_opt,
std::optional<Layout> layout_opt,
std::optional<Device> device_opt,
std::optional<bool> pin_memory_opt) {
check_size_nonnegative(size);
// empty memory formatempty
auto t = at::native::empty_mps(
{0},
dtype_opt,
layout_opt,
device_opt,
pin_memory_opt);
resize_impl_mps_(t.unsafeGetTensorImpl(), size, stride);
return t;
}
const Tensor& resize_mps_(
const Tensor& self,
IntArrayRef size,
std::optional<MemoryFormat> optional_memory_format) {
if (self.has_names()) {
return resize_named_tensor_(self, size, optional_memory_format);
}
auto* self_ = self.unsafeGetTensorImpl();
int64_t old_storage_nbytes = self_->unsafe_storage() ? self_->unsafe_storage().nbytes() : 0;
resize_impl_mps_(self_, size, /*stride=*/std::nullopt);
if (optional_memory_format.has_value()) {
auto memory_format =
optional_memory_format.value();
TORCH_CHECK(
memory_format != MemoryFormat::Preserve,
"Unsupported memory format",
memory_format);
self_->empty_tensor_restride(memory_format);
}
// See Note [Enabling Deterministic Operations]
if (C10_UNLIKELY(at::globalContext().deterministicAlgorithms() && at::globalContext().deterministicFillUninitializedMemory())) {
at::native::fill_resize_deterministic_(self, old_storage_nbytes);
}
return self;
}
Tensor& set_mps_(Tensor& result) {
caffe2::TypeMeta dtype = result.dtype();
Storage storage(
Storage::use_byte_size_t(),
0,
at::mps::GetMPSAllocator(),
true);
result.set_(storage, 0, {0}, {});
TORCH_INTERNAL_ASSERT(dtype == result.dtype());
return result;
}
Tensor& set_storage_mps_(Tensor& result, Storage storage, int64_t storage_offset, IntArrayRef size, IntArrayRef stride) {
checkSetStorage(result, std::move(storage), storage_offset, size, stride);
//std::cout << "set storage_mps " << storage_offset << " stride " << stride << std::endl;
result.unsafeGetTensorImpl()->set_storage_offset(storage_offset);
std::optional<IntArrayRef> stride_opt = stride.data() != nullptr ?
std::optional<IntArrayRef>(stride) : std::nullopt;
at::native::resize_impl_mps_(result.unsafeGetTensorImpl(), size, stride_opt);
return result;
}
Tensor _efficientzerotensor_mps(IntArrayRef size,
std::optional<ScalarType> dtype,
std::optional<Layout> layout,
std::optional<Device> device,
std::optional<bool> pin_memory) {
auto device_ = device_or_default(device);
auto allocator = at::native::ZeroTensorAllocator(device_);
auto dtype_ = dtype_or_default(dtype);
auto zero_ks = at::DispatchKeySet(c10::DispatchKey::MPS) | at::DispatchKeySet(c10::DispatchKey::ZeroTensor);
auto out = at::detail::empty_generic(size, &allocator, zero_ks, dtype_, std::nullopt);
return out;
}
} // namespace at::native