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

[onert/cpu] [Reshape, ExpandDims] Avoid copying memory if possible #14229

Merged

Conversation

mbencer
Copy link
Contributor

@mbencer mbencer commented Oct 16, 2024

This commit copying memory only if output buffer is different from input buffer. It they are equal it means that input and output tensors share the same memory. It is applicable for Reshape and ExpandDims operations.

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

Draft: #14057

This commit copying memory only if output buffer is different from input buffer.
It they are equal it means that input and output tensors share the same memory.
It is applicable for Reshape and ExpandDims operations.

ONE-DCO-1.0-Signed-off-by: Mateusz Bencer [email protected]
size_t count = _input->total_size();
memcpy(_output->buffer(), _input->buffer(), count);
// output buffer equals to input buffer means that copy is not needed
if (_output->buffer() != _input->buffer())
Copy link
Contributor

Choose a reason for hiding this comment

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

I have a question.
Did you have a case that input and output buffer are same?
For memcpy(), src and dest memory area must not overlap, otherwise, its behavior is undefined.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note that this change is a part of bigger issue implemented in #14057. In this draft I am changing tensor allocation mechanism to share memory memory between input and output tensors of Reshape/ExpandDims/Squeeze. In other words different tensors can indicate the same buffers (it requires additional handing of such memory management).

Copy link
Contributor

Choose a reason for hiding this comment

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

I've followed the draft and issue and understood.
Thanks for the explanation :)

Copy link
Contributor

@zetwhite zetwhite left a comment

Choose a reason for hiding this comment

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

LGTM

@mbencer mbencer requested a review from seockho-kim October 30, 2024 10:31
Copy link
Contributor

@hseok-oh hseok-oh left a comment

Choose a reason for hiding this comment

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

LGTM

@hseok-oh hseok-oh merged commit 8c31199 into Samsung:master Nov 20, 2024
9 checks passed
@hseok-oh
Copy link
Contributor

I'll merge this because it is too delayed.

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.

4 participants