Skip to content

Commit 11120c8

Browse files
authored
Deduce the scalar type from a typed data pointer (pytorch#22945)
1 parent 7a8e860 commit 11120c8

5 files changed

Lines changed: 353 additions & 0 deletions

File tree

‎extension/module/test/module_test.cpp‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,28 @@ TEST_F(ModuleTest, TestForward) {
301301
EXPECT_TENSOR_CLOSE(result2->at(0).toTensor(), *expected2.get());
302302
}
303303

304+
TEST_F(ModuleTest, TestForwardRefusesTensorFromMistypedPointer) {
305+
Module module(model_path_);
306+
307+
// A byte buffer used to become a Float tensor, so the runtime's own dtype
308+
// check agreed with the claim and the method ran on reinterpreted bytes,
309+
// returning a plausible wrong number with no error anywhere.
310+
std::vector<uint8_t> bytes(4, 1);
311+
auto mistyped = make_tensor_ptr({2, 2}, bytes.data());
312+
EXPECT_EQ(mistyped->scalar_type(), executorch::aten::ScalarType::Byte);
313+
314+
const auto result = module.forward({mistyped, mistyped, 1.0});
315+
EXPECT_NE(result.error(), Error::Ok);
316+
317+
// A correctly typed call still runs and still gives the right answer.
318+
auto correct = make_tensor_ptr({2, 2}, {1.f, 1.f, 1.f, 1.f});
319+
const auto ok = module.forward({correct, correct, 1.0});
320+
EXPECT_EQ(ok.error(), Error::Ok);
321+
322+
const auto expected = make_tensor_ptr({2, 2}, {2.f, 2.f, 2.f, 2.f});
323+
EXPECT_TENSOR_CLOSE(ok->at(0).toTensor(), *expected.get());
324+
}
325+
304326
TEST_F(ModuleTest, TestForwardWithInvalidInputs) {
305327
Module module(model_path_);
306328

‎extension/tensor/tensor_ptr.h‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,93 @@ inline TensorPtr make_tensor_ptr(
9999
std::move(deleter));
100100
}
101101

102+
/**
103+
* Creates a TensorPtr that manages a Tensor with the specified properties.
104+
*
105+
* Overload for a typed data pointer. The scalar type is deduced from the
106+
* pointer's element type, the same way the vector overload deduces it from the
107+
* vector's element type, so a buffer of one type does not become a Tensor of
108+
* another. Pass `type` explicitly to reinterpret the buffer as a different
109+
* type.
110+
*
111+
* @tparam T The C++ type of the tensor elements, deduced from the pointer.
112+
* @param sizes A vector specifying the size of each dimension.
113+
* @param data A pointer to the data buffer (CPU or device, see device).
114+
* @param dim_order A vector specifying the order of dimensions.
115+
* @param strides A vector specifying the strides of the tensor.
116+
* @param type The scalar type of the tensor elements.
117+
* @param device The device on which `data` resides (default CPU).
118+
* @param dynamism Specifies the mutability of the tensor's shape.
119+
* @param deleter A custom deleter function for managing the lifetime of the
120+
* data buffer.
121+
* @return A TensorPtr that manages the newly created Tensor.
122+
*/
123+
template <
124+
typename T,
125+
executorch::aten::ScalarType deduced_type =
126+
runtime::CppTypeToScalarType<T>::value>
127+
inline TensorPtr make_tensor_ptr(
128+
std::vector<executorch::aten::SizesType> sizes,
129+
T* data,
130+
std::vector<executorch::aten::DimOrderType> dim_order,
131+
std::vector<executorch::aten::StridesType> strides,
132+
const executorch::aten::ScalarType type = deduced_type,
133+
executorch::aten::Device device =
134+
executorch::aten::Device(executorch::aten::DeviceType::CPU),
135+
const executorch::aten::TensorShapeDynamism dynamism =
136+
executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND,
137+
std::function<void(void*)> deleter = nullptr) {
138+
return make_tensor_ptr(
139+
std::move(sizes),
140+
static_cast<void*>(data),
141+
std::move(dim_order),
142+
std::move(strides),
143+
type,
144+
device,
145+
dynamism,
146+
std::move(deleter));
147+
}
148+
149+
/**
150+
* Creates a TensorPtr that manages a Tensor with the specified properties.
151+
*
152+
* Convenience overload for a typed data pointer; see the typed overload above
153+
* for type deduction.
154+
*
155+
* @tparam T The C++ type of the tensor elements, deduced from the pointer.
156+
* @param sizes A vector specifying the size of each dimension.
157+
* @param data A pointer to the data buffer (CPU or device, see device).
158+
* @param type The scalar type of the tensor elements.
159+
* @param device The device on which `data` resides (default CPU).
160+
* @param dynamism Specifies the mutability of the tensor's shape.
161+
* @param deleter A custom deleter function for managing the lifetime of the
162+
* data buffer.
163+
* @return A TensorPtr that manages the newly created Tensor.
164+
*/
165+
template <
166+
typename T,
167+
executorch::aten::ScalarType deduced_type =
168+
runtime::CppTypeToScalarType<T>::value>
169+
inline TensorPtr make_tensor_ptr(
170+
std::vector<executorch::aten::SizesType> sizes,
171+
T* data,
172+
const executorch::aten::ScalarType type = deduced_type,
173+
executorch::aten::Device device =
174+
executorch::aten::Device(executorch::aten::DeviceType::CPU),
175+
const executorch::aten::TensorShapeDynamism dynamism =
176+
executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND,
177+
std::function<void(void*)> deleter = nullptr) {
178+
return make_tensor_ptr(
179+
std::move(sizes),
180+
static_cast<void*>(data),
181+
{},
182+
{},
183+
type,
184+
device,
185+
dynamism,
186+
std::move(deleter));
187+
}
188+
102189
/**
103190
* Creates a TensorPtr that manages a Tensor with the specified properties.
104191
*

‎extension/tensor/tensor_ptr_maker.h‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,30 @@ inline TensorPtrMaker for_blob(
183183
return TensorPtrMaker(data, std::move(sizes), type);
184184
}
185185

186+
/**
187+
* Creates a TensorPtrMaker from a typed data pointer and tensor sizes.
188+
*
189+
* The scalar type is deduced from the pointer's element type, so a buffer of
190+
* one type does not become a Tensor of another. Pass `type` explicitly to
191+
* reinterpret the buffer as a different type.
192+
*
193+
* @tparam T The C++ type of the tensor elements, deduced from the pointer.
194+
* @param data A pointer to the raw data used by the tensor.
195+
* @param sizes A vector specifying the size of each dimension.
196+
* @param type The scalar type of the tensor elements.
197+
* @return A TensorPtrMaker instance for creating a TensorPtr.
198+
*/
199+
template <
200+
typename T,
201+
executorch::aten::ScalarType deduced_type =
202+
runtime::CppTypeToScalarType<T>::value>
203+
inline TensorPtrMaker for_blob(
204+
T* data,
205+
std::vector<executorch::aten::SizesType> sizes,
206+
executorch::aten::ScalarType type = deduced_type) {
207+
return for_blob(static_cast<void*>(data), std::move(sizes), type);
208+
}
209+
186210
/**
187211
* Creates a TensorPtr from a raw data pointer and tensor sizes, with an
188212
* optional dynamism setting.
@@ -213,6 +237,38 @@ inline TensorPtr from_blob(
213237
.make_tensor_ptr();
214238
}
215239

240+
/**
241+
* Creates a TensorPtr from a typed data pointer and tensor sizes.
242+
*
243+
* The scalar type is deduced from the pointer's element type; see `for_blob`.
244+
*
245+
* @tparam T The C++ type of the tensor elements, deduced from the pointer.
246+
* @param data A pointer to the raw data used by the tensor. The data must
247+
* outlive the TensorPtr created by this function.
248+
* @param sizes A vector specifying the size of each dimension.
249+
* @param type The scalar type of the tensor elements.
250+
* @param device The device on which `data` resides (default CPU).
251+
* @param dynamism Specifies whether the tensor's shape is static or dynamic.
252+
* @return A TensorPtr instance managing the newly created Tensor.
253+
*/
254+
template <
255+
typename T,
256+
executorch::aten::ScalarType deduced_type =
257+
runtime::CppTypeToScalarType<T>::value>
258+
inline TensorPtr from_blob(
259+
T* data,
260+
std::vector<executorch::aten::SizesType> sizes,
261+
executorch::aten::ScalarType type = deduced_type,
262+
executorch::aten::Device device =
263+
executorch::aten::Device(executorch::aten::DeviceType::CPU),
264+
executorch::aten::TensorShapeDynamism dynamism =
265+
executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) {
266+
return for_blob(static_cast<void*>(data), std::move(sizes), type)
267+
.device(device)
268+
.dynamism(dynamism)
269+
.make_tensor_ptr();
270+
}
271+
216272
/**
217273
* Creates a TensorPtr from a raw data pointer, tensor sizes, and strides, with
218274
* an optional dynamism setting.
@@ -247,6 +303,41 @@ inline TensorPtr from_blob(
247303
.make_tensor_ptr();
248304
}
249305

306+
/**
307+
* Creates a TensorPtr from a typed data pointer, tensor sizes, and strides.
308+
*
309+
* The scalar type is deduced from the pointer's element type; see `for_blob`.
310+
*
311+
* @tparam T The C++ type of the tensor elements, deduced from the pointer.
312+
* @param data A pointer to the raw data used by the tensor. The data must
313+
* outlive the TensorPtr created by this function.
314+
* @param sizes A vector specifying the size of each dimension.
315+
* @param strides A vector specifying the stride for each dimension.
316+
* @param type The scalar type of the tensor elements.
317+
* @param device The device on which `data` resides (default CPU).
318+
* @param dynamism Specifies whether the tensor's shape is static or dynamic.
319+
* @return A TensorPtr instance managing the newly created Tensor.
320+
*/
321+
template <
322+
typename T,
323+
executorch::aten::ScalarType deduced_type =
324+
runtime::CppTypeToScalarType<T>::value>
325+
inline TensorPtr from_blob(
326+
T* data,
327+
std::vector<executorch::aten::SizesType> sizes,
328+
std::vector<executorch::aten::StridesType> strides,
329+
executorch::aten::ScalarType type = deduced_type,
330+
executorch::aten::Device device =
331+
executorch::aten::Device(executorch::aten::DeviceType::CPU),
332+
executorch::aten::TensorShapeDynamism dynamism =
333+
executorch::aten::TensorShapeDynamism::DYNAMIC_BOUND) {
334+
return for_blob(static_cast<void*>(data), std::move(sizes), type)
335+
.strides(std::move(strides))
336+
.device(device)
337+
.dynamism(dynamism)
338+
.make_tensor_ptr();
339+
}
340+
250341
/**
251342
* Creates a TensorPtr from a raw data pointer and tensor sizes, with an
252343
* optional dynamism setting.

‎extension/tensor/test/tensor_ptr_maker_test.cpp‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,64 @@ class TensorPtrMakerTest : public ::testing::Test {
2323
}
2424
};
2525

26+
TEST_F(TensorPtrMakerTest, TypedPointerDeducesItsOwnScalarType) {
27+
// for_blob, from_blob and from_blob with strides are three separate entry
28+
// points. All three used to take the pointer as void* and default to Float.
29+
std::vector<int8_t> bytes = {1, 2, 3, 4};
30+
31+
auto from_maker = for_blob(bytes.data(), {2, 2}).make_tensor_ptr();
32+
EXPECT_EQ(from_maker->scalar_type(), executorch::aten::ScalarType::Char);
33+
EXPECT_EQ(from_maker->nbytes(), bytes.size() * sizeof(int8_t));
34+
35+
auto blob = from_blob(bytes.data(), {2, 2});
36+
EXPECT_EQ(blob->scalar_type(), executorch::aten::ScalarType::Char);
37+
EXPECT_EQ(blob->nbytes(), bytes.size() * sizeof(int8_t));
38+
39+
auto strided = from_blob(bytes.data(), {2, 2}, {2, 1});
40+
EXPECT_EQ(strided->scalar_type(), executorch::aten::ScalarType::Char);
41+
EXPECT_EQ(strided->nbytes(), bytes.size() * sizeof(int8_t));
42+
43+
// An untyped pointer has nothing to deduce from and keeps the default.
44+
void* untyped = bytes.data();
45+
auto from_void = from_blob(untyped, {2, 2});
46+
EXPECT_EQ(from_void->scalar_type(), executorch::aten::ScalarType::Float);
47+
48+
// An explicit type still reinterprets, on the builder and on the factory.
49+
auto reinterpreted =
50+
from_blob(bytes.data(), {2, 2}, executorch::aten::ScalarType::Float);
51+
EXPECT_EQ(reinterpreted->scalar_type(), executorch::aten::ScalarType::Float);
52+
auto overridden = for_blob(bytes.data(), {2, 2})
53+
.type(executorch::aten::ScalarType::Float)
54+
.make_tensor_ptr();
55+
EXPECT_EQ(overridden->scalar_type(), executorch::aten::ScalarType::Float);
56+
}
57+
58+
TEST_F(TensorPtrMakerTest, TypedPointerDeducesItsOwnScalarTypeWithDeleter) {
59+
// The fluent builder is the only path that can combine deduction with a
60+
// custom deleter, because the deleter is not a positional parameter there.
61+
// The deleter still has to run exactly once and receive the pointer it was
62+
// given, so that a typed delete[] stays correct.
63+
auto deleter_calls = 0;
64+
void* deleted_ptr = nullptr;
65+
auto* data = new int8_t[4]();
66+
67+
{
68+
auto tensor = for_blob(data, {2, 2})
69+
.deleter([&deleter_calls, &deleted_ptr](void* ptr) {
70+
++deleter_calls;
71+
deleted_ptr = ptr;
72+
delete[] static_cast<int8_t*>(ptr);
73+
})
74+
.make_tensor_ptr();
75+
EXPECT_EQ(tensor->scalar_type(), executorch::aten::ScalarType::Char);
76+
EXPECT_EQ(tensor->nbytes(), 4 * sizeof(int8_t));
77+
EXPECT_EQ(deleter_calls, 0);
78+
}
79+
80+
EXPECT_EQ(deleter_calls, 1);
81+
EXPECT_EQ(deleted_ptr, static_cast<void*>(data));
82+
}
83+
2684
TEST_F(TensorPtrMakerTest, CreateTensorUsingTensorMaker) {
2785
float data[20] = {2};
2886
auto tensor =

0 commit comments

Comments
 (0)