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] Introduce capabilities to find operands which can share memory #14228

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

mbencer
Copy link
Contributor

@mbencer mbencer commented Oct 16, 2024

This commit adds capabilities to find operands linked which Tensors which can share memory buffers. It's applicable for ops like Reshape, Squeeze and ExpandsDims (where only shape is changed and data is not modified).

ONE-DCO-1.0-Signed-off-by: Mateusz Bencer m.bencer@partner.samsung.com

Draft: #14057

This commit adds capabilities to find operands linked which Tensors
which can share memory buffers. It's applicable for ops like Reshape, Squeeze and ExpandsDims
(where only shape is changed and data is not modified).

ONE-DCO-1.0-Signed-off-by: Mateusz Bencer m.bencer@partner.samsung.com
Comment on lines 83 to 85
{
shared_memory_operand_map[op.getOutputs().at(0)] = op.getInputs().at(0);
}
Copy link
Contributor

@zetwhite zetwhite Oct 24, 2024

Choose a reason for hiding this comment

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

(optional) How about adding assertions here to check is_memory_sharing_allowed only captures single input - single output operation?

Before reading line 53 (ops_with_possible_memory_sharing), I didn't notice that it targets single input, single output operation.

Suggested change
{
shared_memory_operand_map[op.getOutputs().at(0)] = op.getInputs().at(0);
}
{
assert(op.getInputs().size() == 1);
assert(op.getOutputs().size() == 1);
shared_memory_operand_map[op.getOutputs().at(0)] = op.getInputs().at(0);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added (note that for Reshape/ExpandDims number of inputs is 2)

Copy link
Contributor

Choose a reason for hiding this comment

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

added (note that for Reshape/ExpandDims number of inputs is 2)

Oh, I see. I missed that. 👍

Comment on lines +33 to +46
for (auto [shared_ind, source_ind] : shared_memory_operand_map)
{
bool other_source_found = false;
auto it = std::end(shared_memory_operand_map);
while ((it = shared_memory_operand_map.find(source_ind)) != std::end(shared_memory_operand_map))
{
source_ind = shared_memory_operand_map[source_ind];
other_source_found = true;
}
if (other_source_found)
{
shared_memory_operand_map[shared_ind] = source_ind;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

If shared_memory_operand_map = {{1, 2}, {2, 3}, {2, 4}} is given, is this works well..?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As I understand 2 cannot indicate other operand two times. (I've made some experiments about this implementation here - https://godbolt.org/z/11KKr9xqY)

Copy link
Contributor

@zetwhite zetwhite Oct 30, 2024

Choose a reason for hiding this comment

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

Oh I see. I misunderstood the details.
Thank you for your implementations :)

My original question was that I it is safe with below graphs :

		   ->  reshape -> output operand  
		/ 
operand --
		\
		  -> other operation -> output operand 

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ohh, I see, definitely it makes sense to write such unit test, thank you!

Copy link
Contributor Author

@mbencer mbencer Oct 30, 2024

Choose a reason for hiding this comment

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

I added such a test:
image
and it seems to work correctly (the memory is sharing)

@mbencer mbencer requested a review from zetwhite October 25, 2024 15:49
zetwhite
zetwhite previously approved these changes Oct 30, 2024
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

}
} // namespace

TEST(SharedMemoryOperands, no_shared_memory_graph)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
TEST(SharedMemoryOperands, no_shared_memory_graph)
TEST(SharedMemoryOperands, neg_shared_memory_graph)

@mbencer Please follow onert's unittest naming policy: negative test should have naming prefix neg_.
And negative test count should same or more than positive test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done, used naming prefix neg_ + added more tests. Now there are 4 positive and 4 negative tests

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.

3 participants