Add support for einsum operation to pytorch parser (requires 1116)#1273
Merged
Conversation
Contributor
Author
|
Now that #1116 is merged, this can also be added. |
calad0i
reviewed
Jun 3, 2025
|
|
||
| a = torch.randn(input_shapes_tmp[0]) | ||
| b = torch.randn(input_shapes_tmp[1]) | ||
| layer['out_shape'] = tuple(torch.einsum(layer['equation'], a, b).shape) |
Contributor
There was a problem hiding this comment.
It is preferred to use hls4ml.utils.einsum_utils._validate_einsum_expr here, something like:
inp_shape0 = input_shapes_tmp[0][1:]
inp_shape1 = input_shapes_tmp[1][1:]
layer['equation'], layer['out_shape'] = _validate_einsum_expr(node.args[0], inp_shape0, inp_shape1In this way wildcard dimensions (...) will be fixed to indices and explicit torch call will not be required.
Otherwise all looks good.
Contributor
Author
There was a problem hiding this comment.
Thanks for the suggestion, implemented.
Contributor
There was a problem hiding this comment.
Ok, actually I didn't expect the batch dimension when implementing this, and I explicitly striped off the batch dim in the keras impl. However, indeed setting batch dimension = 1 is functionally equivalent and should always work.
Merging after test pass.
calad0i
approved these changes
Jun 3, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Builds on Chang's keras v3 PR #1116 and exposes the new einsum implementation through the pytorch parser. pytorch doesn't have an an equivalent to
EinsumDensebut allowing the use of einsum operations in some custom model would still be useful.Type of change
Tests
Added 2 use cases (outer product and batch matrix multiplication to the pytests, works without issues.
Checklist
pre-commiton the files I edited or added.