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

[TFL] Fix BatchMatMul constant RHS. #46749

Merged

Conversation

WindQAQ
Copy link
Member

@WindQAQ WindQAQ commented Jan 28, 2021

Fixes #46724. For constant RHS and !adj_y, transpose of RHS only occurs once, so the allocation should be persistent across runs. Old commit does this, but the allocation is accidentally overridden to kTfLiteArenaRw for constant RHS.

if (IsConstantTensor(op_context->rhs)) {
scratch_buffer->allocation_type = kTfLiteArenaRwPersistent;
} else {
scratch_buffer->allocation_type = kTfLiteArenaRw;
}
scratch_buffer->type = op_context->rhs->type;
scratch_buffer->allocation_type = kTfLiteArenaRw;

/cc @abattery for visibility.

For constant RHS and !adj_y, transpose of RHS only occurs once, so the allocation should be persistent across runs.
@google-ml-butler google-ml-butler bot added the size:M CL Change Size: Medium label Jan 28, 2021
@google-cla google-cla bot added the cla: yes label Jan 28, 2021
@gbaned gbaned self-assigned this Jan 28, 2021
@gbaned gbaned added the comp:lite TF Lite related issues label Jan 28, 2021
@thaink
Copy link
Member

thaink commented Jan 28, 2021

Verified that this fixes the issue.

CreateBatchMatMulOptions(builder_, adj_x, adj_y).Union(),
matmul_inputs, matmul_outputs);

// This is just a dummy op to check if the values of transposed RHS are
Copy link
Member

Choose a reason for hiding this comment

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

Can you explain why the neg op help here?

Copy link
Member Author

@WindQAQ WindQAQ Jan 28, 2021

Choose a reason for hiding this comment

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

It's somehow tricky. When there is no following ops (not limited to neg), temporary allocation with kTfLiteArenaRw type tends to re-claim the same memory across each evaluation and no other ops will modify values at that memory address (cause no other memory allocations take place). Therefore, the values of rhs are not polluted without neg op, and next evaluation can also get the same piece of memory, so the results are correct.

https://colab.research.google.com/drive/1eFVszZZN-NcR64X_VHns0w-l3Vwci0CY?usp=sharing

Here is the colab link from original post. Values of last tensor (temp tensor, transposed rhs) become

[[-51. -84.]
 [  3.   6.]
 [  7.   9.]]

where -51, -84 are the last two elements of Identity (output of neg op). While without neg op, everything works smoothly.

Copy link
Member

Choose a reason for hiding this comment

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

Acked. Thanks for explanation. May be it worths a comment then.

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated comments. See if it's better! Thanks!

@google-ml-butler google-ml-butler bot added kokoro:force-run Tests on submitted change ready to pull PR ready for merge process labels Jan 28, 2021
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Jan 28, 2021
@google-ml-butler google-ml-butler bot removed the ready to pull PR ready for merge process label Jan 28, 2021
@WindQAQ WindQAQ requested a review from thaink January 28, 2021 18:39
@google-ml-butler google-ml-butler bot added kokoro:force-run Tests on submitted change ready to pull PR ready for merge process labels Jan 29, 2021
@thaink thaink added ready to pull PR ready for merge process and removed ready to pull PR ready for merge process labels Jan 29, 2021
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Jan 29, 2021
@copybara-service copybara-service bot merged commit c378e23 into tensorflow:master Jan 29, 2021
@gbaned gbaned added the prtype:bugfix PR to fix a bug label Jan 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes comp:lite TF Lite related issues prtype:bugfix PR to fix a bug ready to pull PR ready for merge process size:M CL Change Size: Medium
Projects
None yet
Development

Successfully merging this pull request may close these issues.

tf.matmul and tf.tensordot behave different in converted concrete function in TensorFlowLite
4 participants