Skip to content

Commit 2b41f6d

Browse files
committed
[ET-VK] Store unique ptr to Tensor in Value instead of inlined tensor object, to reduce Value struct size from 448 to 80 bytes.
This diff aims to reduce the size of the Value struct in the Executorch Vulkan runtime by storing a unique pointer to the Tensor object instead of an inlined tensor object. This change reduces the size of the Value struct from 448 bytes to 80 bytes, which can improve performance and reduce memory usage. Differential Revision: [D66655991](https://our.internmc.facebook.com/intern/diff/D66655991/) ghstack-source-id: 256050917 Pull Request resolved: #7145
1 parent 13d55aa commit 2b41f6d

File tree

1 file changed

+42
-10
lines changed
  • backends/vulkan/runtime/graph/containers

1 file changed

+42
-10
lines changed

backends/vulkan/runtime/graph/containers/Value.h

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ struct Value final {
5858
bool as_bool;
5959
} u;
6060

61-
api::vTensor as_tensor;
61+
std::unique_ptr<api::vTensor> as_tensor;
6262
api::StagingBuffer as_staging;
6363
TensorRef as_tensorref;
6464

@@ -106,15 +106,19 @@ struct Value final {
106106
rhs.payload.member_name.~dtor_name(); \
107107
break;
108108

109+
#define CASE_MOVE_UNIQUE_PTR_TYPE(type_tag, member_name) \
110+
case type_tag: \
111+
payload.member_name = std::move(rhs.payload.member_name); \
112+
break;
113+
109114
Value(Value&& rhs) noexcept : tag(rhs.tag) {
110115
switch (tag) {
111116
// Scalar types
112117
CASE_MOVE_TRIVIALLY_COPYABLE_TYPE(TypeTag::INT, as_int);
113118
CASE_MOVE_TRIVIALLY_COPYABLE_TYPE(TypeTag::DOUBLE, as_double);
114119
CASE_MOVE_TRIVIALLY_COPYABLE_TYPE(TypeTag::BOOL, as_bool);
115120
// Tensor and tensor adjacent types
116-
CASE_MOVE_MOVEABLE_TYPE(
117-
TypeTag::TENSOR, api::vTensor, as_tensor, vTensor);
121+
CASE_MOVE_UNIQUE_PTR_TYPE(TypeTag::TENSOR, as_tensor);
118122
CASE_MOVE_MOVEABLE_TYPE(
119123
TypeTag::STAGING, api::StagingBuffer, as_staging, StagingBuffer);
120124
CASE_MOVE_MOVEABLE_TYPE(
@@ -142,6 +146,7 @@ struct Value final {
142146

143147
#undef CASE_MOVE_TRIVIALLY_COPYABLE_TYPE
144148
#undef CASE_MOVE_MOVEABLE_TYPE
149+
#undef CASE_MOVE_UNIQUE_PTR_TYPE
145150

146151
//
147152
// Accessors
@@ -158,7 +163,7 @@ struct Value final {
158163
~Value() {
159164
switch (tag) {
160165
case TypeTag::TENSOR:
161-
payload.as_tensor.~vTensor();
166+
payload.as_tensor.reset();
162167
break;
163168
case TypeTag::STAGING:
164169
payload.as_staging.~StagingBuffer();
@@ -227,6 +232,39 @@ struct Value final {
227232

228233
#undef SUPPORT_TRIVIALLY_COPYABLE_TYPE
229234

235+
#define SUPPORT_TRIVIALLY_MOVEABLE_UNIQUE_PTR_TYPE( \
236+
type, type_name, type_tag, member_name) \
237+
explicit Value(type t) : tag(type_tag) { \
238+
payload.member_name = std::make_unique<type>(std::move(t)); \
239+
} \
240+
inline bool is##type_name() const { \
241+
return tag == type_tag; \
242+
} \
243+
inline type& to##type_name() const { \
244+
VK_CHECK_COND( \
245+
is##type_name(), \
246+
"Expected value to have type " #type_name ", got ", \
247+
tag, \
248+
" instead."); \
249+
return *payload.member_name; \
250+
} \
251+
inline const type& toConst##type_name() const { \
252+
VK_CHECK_COND( \
253+
is##type_name(), \
254+
"Expected value to have type " #type_name ", got ", \
255+
tag, \
256+
" instead."); \
257+
return *payload.member_name; \
258+
}
259+
260+
SUPPORT_TRIVIALLY_MOVEABLE_UNIQUE_PTR_TYPE(
261+
api::vTensor,
262+
Tensor,
263+
TypeTag::TENSOR,
264+
as_tensor);
265+
266+
#undef SUPPORT_TRIVIALLY_MOVEABLE_UNIQUE_PTR_TYPE
267+
230268
#define SUPPORT_TRIVIALLY_MOVEABLE_TYPE( \
231269
type, type_name, type_tag, member_name) \
232270
explicit Value(type&& t) : tag(type_tag) { \
@@ -252,12 +290,6 @@ struct Value final {
252290
return payload.member_name; \
253291
}
254292

255-
SUPPORT_TRIVIALLY_MOVEABLE_TYPE(
256-
api::vTensor,
257-
Tensor,
258-
TypeTag::TENSOR,
259-
as_tensor);
260-
261293
SUPPORT_TRIVIALLY_MOVEABLE_TYPE(
262294
api::StagingBuffer,
263295
Staging,

0 commit comments

Comments
 (0)