Skip to content

Commit de5b1f4

Browse files
committed
Update on "[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-poisoned]
2 parents 0533702 + 3f898f1 commit de5b1f4

File tree

2 files changed

+36
-41
lines changed

2 files changed

+36
-41
lines changed

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

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,7 @@ struct Value final {
117117
CASE_MOVE_TRIVIALLY_COPYABLE_TYPE(TypeTag::INT, as_int);
118118
CASE_MOVE_TRIVIALLY_COPYABLE_TYPE(TypeTag::DOUBLE, as_double);
119119
CASE_MOVE_TRIVIALLY_COPYABLE_TYPE(TypeTag::BOOL, as_bool);
120-
// Tensor and tensor adjacent types
121-
CASE_MOVE_UNIQUE_PTR_TYPE(TypeTag::TENSOR, as_tensor);
120+
// Tensor adjacent types
122121
CASE_MOVE_MOVEABLE_TYPE(
123122
TypeTag::STAGING, api::StagingBuffer, as_staging, StagingBuffer);
124123
CASE_MOVE_MOVEABLE_TYPE(
@@ -136,6 +135,8 @@ struct Value final {
136135
CASE_MOVE_MOVEABLE_TYPE(
137136
TypeTag::STRING, std::string, as_string, basic_string);
138137
CASE_MOVE_MOVEABLE_TYPE(TypeTag::SYMINT, SymInt, as_symint, SymInt);
138+
// Tensor type
139+
CASE_MOVE_UNIQUE_PTR_TYPE(TypeTag::TENSOR, as_tensor);
139140

140141
case TypeTag::NONE:
141142
clearToNone();
@@ -162,9 +163,6 @@ struct Value final {
162163

163164
~Value() {
164165
switch (tag) {
165-
case TypeTag::TENSOR:
166-
payload.as_tensor.reset();
167-
break;
168166
case TypeTag::STAGING:
169167
payload.as_staging.~StagingBuffer();
170168
break;
@@ -196,6 +194,9 @@ struct Value final {
196194
case TypeTag::DOUBLE:
197195
case TypeTag::BOOL:
198196
break;
197+
case TypeTag::TENSOR:
198+
payload.as_tensor.reset();
199+
break;
199200
}
200201
}
201202

@@ -232,39 +233,6 @@ struct Value final {
232233

233234
#undef SUPPORT_TRIVIALLY_COPYABLE_TYPE
234235

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-
268236
#define SUPPORT_TRIVIALLY_MOVEABLE_TYPE( \
269237
type, type_name, type_tag, member_name) \
270238
explicit Value(type&& t) : tag(type_tag) { \
@@ -334,9 +302,36 @@ struct Value final {
334302

335303
SUPPORT_TRIVIALLY_MOVEABLE_TYPE(SymInt, SymInt, TypeTag::SYMINT, as_symint);
336304

337-
#undef SUPPORT_TRIVIALLY_COPYABLE_TYPE
338305
#undef SUPPORT_TRIVIALLY_MOVEABLE_TYPE
339306

307+
#define SUPPORT_UNIQUE_PTR_TYPE(type, type_name, type_tag, member_name) \
308+
explicit Value(type t) : tag(type_tag) { \
309+
payload.member_name = std::make_unique<type>(std::move(t)); \
310+
} \
311+
inline bool is##type_name() const { \
312+
return tag == type_tag; \
313+
} \
314+
inline type& to##type_name() const { \
315+
VK_CHECK_COND( \
316+
is##type_name(), \
317+
"Expected value to have type " #type_name ", got ", \
318+
tag, \
319+
" instead."); \
320+
return *payload.member_name; \
321+
} \
322+
inline const type& toConst##type_name() const { \
323+
VK_CHECK_COND( \
324+
is##type_name(), \
325+
"Expected value to have type " #type_name ", got ", \
326+
tag, \
327+
" instead."); \
328+
return *payload.member_name; \
329+
}
330+
331+
SUPPORT_UNIQUE_PTR_TYPE(api::vTensor, Tensor, TypeTag::TENSOR, as_tensor);
332+
333+
#undef SUPPORT_UNIQUE_PTR_TYPE
334+
340335
private:
341336
Payload payload;
342337
TypeTag tag;

backends/vulkan/test/vulkan_compute_api_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,8 +1087,8 @@ TEST_F(VulkanComputeAPITest, print_object_sizes) {
10871087

10881088
// Current known size on 64 bit system: 1040 B
10891089
EXPECT_TRUE(sizeof(vTensor) < 1200);
1090-
// Current known size on 64 bit system: 1056 B
1091-
EXPECT_TRUE(sizeof(Value) < 1200);
1090+
// Current known size on 64 bit system: 80 B
1091+
EXPECT_TRUE(sizeof(Value) < 88);
10921092
// Current known size on 64 bit system: 120 B
10931093
EXPECT_TRUE(sizeof(StagingBuffer) < 500);
10941094
// Current known size on 64 bit system: 384 B

0 commit comments

Comments
 (0)