Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[compute] Apply std::variant to Shape #14679

Closed

Conversation

ragmani
Copy link
Contributor

@ragmani ragmani commented Feb 14, 2025

This applies std::variant to replace in the union in Shape.

ONE-DCO-1.0-Signed-off-by: ragmani [email protected]

@ragmani
Copy link
Contributor Author

ragmani commented Feb 14, 2025

This approach leverages automatic memory management provided by std::variant and avoids the potential pitfalls of manually managing memory with a union.

Before the Change:

The original code used a union to store internal data: if the shape size was less than or equal to kMaxSmallSize, it was stored in a fixed-size array (_dims); otherwise, it was stored in a dynamically allocated pointer (_dims_pointer).

After the Change:

The union is replaced with a std::variant so that the internal data is stored as follows:

When the shape size is less than or equal to kMaxSmallSize, it uses std::array<int32_t, kMaxSmallSize>.

When the shape size is larger, it uses std::vector<int32_t>.

Key Modifications:

The member variable _dims is now declared as
std::variant<std::array<int32_t, kMaxSmallSize>, std::vector<int32_t>> instead of a union.

The constructors, Dims(), SetDim(), DimsData(), Resize(), and ReplaceWith() functions have been updated to use the std::variant access methods instead of direct union access.

The rest of the functions remain largely unchanged, preserving the original comments and functionality.

// (a) as Dims<4>-dependent code is eliminated, the reliance on this should be
// reduced, and (b) some kernels are stricly 4-D, but then the shapes of their
// inputs should already be 4-D, so this function should not be needed.
inline static Shape ExtendedShape(int new_shape_size, const Shape &shape)
Copy link
Contributor Author

@ragmani ragmani Feb 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally, there is no strict requirement to mark a function template as inline. Template functions (especially non-member ones) already avoid multiple-definition errors due to how templates are instantiated and handled by the compiler. Marking it inline usually does no harm, but it’s not strictly necessary in this situation. Nevertheless, I added it for code consistency.

@ragmani ragmani changed the title [compute] Apply std::variable to Shape [compute] Apply std::variant to Shape Feb 17, 2025
This applies std::variable to replace in the union in Shape.

ONE-DCO-1.0-Signed-off-by: ragmani <[email protected]>
@ragmani ragmani force-pushed the onert/apply_std_variant_cker_shape branch from e4ba985 to c5dc4ee Compare February 17, 2025 06:53
@ragmani
Copy link
Contributor Author

ragmani commented Feb 17, 2025

This PR might negatively affect latency, so I discard it.

@ragmani ragmani closed this Feb 17, 2025
@ragmani ragmani deleted the onert/apply_std_variant_cker_shape branch February 19, 2025 06:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant