Skip to content

Commit

Permalink
[ET-VK] Store unique ptr to Tensor in Value instead of inlined tensor…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
trivedivivek committed Dec 2, 2024
1 parent 13d55aa commit 2b41f6d
Showing 1 changed file with 42 additions and 10 deletions.
52 changes: 42 additions & 10 deletions backends/vulkan/runtime/graph/containers/Value.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ struct Value final {
bool as_bool;
} u;

api::vTensor as_tensor;
std::unique_ptr<api::vTensor> as_tensor;
api::StagingBuffer as_staging;
TensorRef as_tensorref;

Expand Down Expand Up @@ -106,15 +106,19 @@ struct Value final {
rhs.payload.member_name.~dtor_name(); \
break;

#define CASE_MOVE_UNIQUE_PTR_TYPE(type_tag, member_name) \
case type_tag: \
payload.member_name = std::move(rhs.payload.member_name); \
break;

Value(Value&& rhs) noexcept : tag(rhs.tag) {
switch (tag) {
// Scalar types
CASE_MOVE_TRIVIALLY_COPYABLE_TYPE(TypeTag::INT, as_int);
CASE_MOVE_TRIVIALLY_COPYABLE_TYPE(TypeTag::DOUBLE, as_double);
CASE_MOVE_TRIVIALLY_COPYABLE_TYPE(TypeTag::BOOL, as_bool);
// Tensor and tensor adjacent types
CASE_MOVE_MOVEABLE_TYPE(
TypeTag::TENSOR, api::vTensor, as_tensor, vTensor);
CASE_MOVE_UNIQUE_PTR_TYPE(TypeTag::TENSOR, as_tensor);
CASE_MOVE_MOVEABLE_TYPE(
TypeTag::STAGING, api::StagingBuffer, as_staging, StagingBuffer);
CASE_MOVE_MOVEABLE_TYPE(
Expand Down Expand Up @@ -142,6 +146,7 @@ struct Value final {

#undef CASE_MOVE_TRIVIALLY_COPYABLE_TYPE
#undef CASE_MOVE_MOVEABLE_TYPE
#undef CASE_MOVE_UNIQUE_PTR_TYPE

//
// Accessors
Expand All @@ -158,7 +163,7 @@ struct Value final {
~Value() {
switch (tag) {
case TypeTag::TENSOR:
payload.as_tensor.~vTensor();
payload.as_tensor.reset();
break;
case TypeTag::STAGING:
payload.as_staging.~StagingBuffer();
Expand Down Expand Up @@ -227,6 +232,39 @@ struct Value final {

#undef SUPPORT_TRIVIALLY_COPYABLE_TYPE

#define SUPPORT_TRIVIALLY_MOVEABLE_UNIQUE_PTR_TYPE( \
type, type_name, type_tag, member_name) \
explicit Value(type t) : tag(type_tag) { \
payload.member_name = std::make_unique<type>(std::move(t)); \
} \
inline bool is##type_name() const { \
return tag == type_tag; \
} \
inline type& to##type_name() const { \
VK_CHECK_COND( \
is##type_name(), \
"Expected value to have type " #type_name ", got ", \
tag, \
" instead."); \
return *payload.member_name; \
} \
inline const type& toConst##type_name() const { \
VK_CHECK_COND( \
is##type_name(), \
"Expected value to have type " #type_name ", got ", \
tag, \
" instead."); \
return *payload.member_name; \
}

SUPPORT_TRIVIALLY_MOVEABLE_UNIQUE_PTR_TYPE(
api::vTensor,
Tensor,
TypeTag::TENSOR,
as_tensor);

#undef SUPPORT_TRIVIALLY_MOVEABLE_UNIQUE_PTR_TYPE

#define SUPPORT_TRIVIALLY_MOVEABLE_TYPE( \
type, type_name, type_tag, member_name) \
explicit Value(type&& t) : tag(type_tag) { \
Expand All @@ -252,12 +290,6 @@ struct Value final {
return payload.member_name; \
}

SUPPORT_TRIVIALLY_MOVEABLE_TYPE(
api::vTensor,
Tensor,
TypeTag::TENSOR,
as_tensor);

SUPPORT_TRIVIALLY_MOVEABLE_TYPE(
api::StagingBuffer,
Staging,
Expand Down

0 comments on commit 2b41f6d

Please sign in to comment.