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

[wasm] Add Diag kernel #7277

Merged
merged 3 commits into from
Jan 17, 2023
Merged

Conversation

chunnienc
Copy link
Collaborator

@chunnienc chunnienc commented Jan 17, 2023

tf.raw_ops.Diag does not support boolean as the input, but tfjs-core has op tests for boolean tensors. So wasm kernel still supports boolean tensors.

To see the logs from the Cloud Build CI, please join either our discussion or announcement mailing list.


This change is Reviewable

@chunnienc chunnienc marked this pull request as ready for review January 17, 2023 04:16
Copy link
Collaborator

@Linchenn Linchenn left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link
Member

@mattsoulanille mattsoulanille left a comment

Choose a reason for hiding this comment

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

LGTM

Comment on lines +34 to +39
inline void DiagImpl(const T* x_buf, int32_t x_size, T* out_buf) {
std::fill(out_buf, out_buf + x_size * x_size, 0);
for (int32_t i = 0; i < x_size; ++i) {
out_buf[x_size * i + i] = x_buf[i];
}
}
Copy link
Member

Choose a reason for hiding this comment

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

This is a really nice implementation. For a while, I thought you'd have to do a lot of index calculations to account for diagonals of different dimensions, but that's not the case.

For some k dimensional tensor,

output[i1,..., ik, i1,..., ik] = diagonal[i1, ..., ik]

But the index in the tensor's data array that i1,..., ik corresponds to is some unique number d. Then, we just have

output[d, d] = diagonal[d]

for all d in the input, and this is just

out_buf[x_size * d + d] = x_buf[d]

@mattsoulanille mattsoulanille merged commit 5cd5ad2 into tensorflow:master Jan 17, 2023
@chunnienc chunnienc deleted the wasm-diag branch January 19, 2023 17:36
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