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

Implement Einsum reference in nGraph interpreter #5923

Conversation

rkazants
Copy link
Member

@rkazants rkazants commented May 31, 2021

Description:

  1. It implements Einsum-7 reference. The implementation is added into nGraph interpreter due to huge size of the implementation. The interpreter binary size (ngraph_reference lib) increases from 2414KB to 3292KB by 878KB (or 36%). Since plugins do not support Einsum-7 operation directly, there exists EinsumDecomposition transformation that helps to complete all model requests.
  2. This Einsum reference does not have limitations and supports all equations including repeated labels in input subscribt (aka diagonal extraction), ellipsis, and others.
  3. The reference was tested using 11 unit tests with simple input data and ones generated with numpy

Ticket: 49327

Details: The main idea was inherited from EinsumDecomposition transformation implementation in PR5529 and extended to support diagonal extraction and ellipsis in an equation. To get the idea about support of equations without diagonal extraction and without ellipsis, please refer to PR5529 details where it describes the procedure to split labels into three groups (common, separated, reduced dimensions), permute them for grouping, apply MatMul operation, and reshape back the resulted tensor with transpose to get layout adjusted with output subscript.
Here it describes two cases: diagonal extraction and ellipsis.
EinsumDiagonal. For example, Einsum with equation="abb->a" will extract diagonals per batch (i.e. zero dimension):
image
It makes sense to note that diagonal extraction must be performed in the first step for each operand in case repeated labels in the corresponding input subscripts. For example, diagonal must be extracted for the first operand of Einsum with equation="abbac,bd->ad" in the first step. Only after the first step you can proceed with handling ellipsis and details from PR5529.
Let us perform diagonal extraction for the first operand with input subscript abbac and shape equal to [2, 3, 3, 2, 4].

  1. For this, determine repeated labels and corresponding dimension indices for them. In this case, a and b are repeated labels and [0, 3] and [1, 2] are the corresponding dimension indices.
  2. Build identity tensors for each repeated label. For a label, it will be I1 of shape [2,1,1,2,1] where I1[k,*,*,k,*]=1 for k=0,1 and 0 - otherwise. For b label, it will be I2 of shape [1,3,3,1,1] where I2[k,*,*,k,*]=1 for k=0,1,2 and 0 - otherwise. Note that identity rank matches to the input rank and its dimension sizes equal dimension sizes corresponding to given label from input shape and equal to 1 for all other labels.
  3. Multiples all identities by element-wise operation I=I1*I2*...*In where n is a number of repeated labels.
  4. Multiples the first operand by I and perform reduce-sum along dimensions from the corresponding dimension indices for repeated labels excluding the first dimension, i.e. [3] for a and [2] for b. Hence, the extracted diagonal will be of shape [2,3,4] with subscript abc.

Ellipsis. In case of ellipsis, the procedure described in PR5529 is extended with an ellipsis case. All steps from PR5529 are modified so that it takes into account that one ellipsis label can cover multiple dimension. Also, before MatMul operation you need to broadcast operands if they both have dimensions covered by ellipsis label.

Signed-off-by: Roman Kazantsev [email protected]

@rkazants rkazants requested a review from a team May 31, 2021 11:29
@openvino-pushbot openvino-pushbot added the category: Core OpenVINO Core (aka ngraph) label May 31, 2021
@rkazants rkazants requested review from a team, mvafin, vgavrilo, popovaan and pavel-esir and removed request for a team and vgavrilo May 31, 2021 11:31
@lazarevevgeny
Copy link
Contributor

Please, add information about the binary size increase of the ngraph library because of this operation.

@rkazants rkazants force-pushed the feature/rkazants/einsum_reference branch from 63e8ab8 to 52fc92e Compare May 31, 2021 19:10
@ilyachur
Copy link
Contributor

ilyachur commented Jun 1, 2021

Could you set the milestone for this activity?
Does this PR fix some ticket?

@rkazants rkazants added this to the 2021.4 milestone Jun 1, 2021
@rkazants
Copy link
Member Author

rkazants commented Jun 1, 2021

Could you set the milestone for this activity?
Does this PR fix some ticket?

Done.

@rkazants
Copy link
Member Author

rkazants commented Jun 1, 2021

Please, add information about the binary size increase of the ngraph library because of this operation.

Done

@rkazants rkazants force-pushed the feature/rkazants/einsum_reference branch 2 times, most recently from fec8058 to 8247866 Compare June 1, 2021 11:00
@rkazants rkazants force-pushed the feature/rkazants/einsum_reference branch from 8247866 to da05c67 Compare June 1, 2021 11:11
@rkazants rkazants force-pushed the feature/rkazants/einsum_reference branch from 24bdb1f to 421d6ee Compare June 1, 2021 16:52
@rkazants rkazants force-pushed the feature/rkazants/einsum_reference branch from 421d6ee to f11813e Compare June 1, 2021 19:03
@rkazants rkazants requested review from ilyachur and pavel-esir June 3, 2021 08:42
@rkazants rkazants requested a review from ilyachur June 3, 2021 17:20
Signed-off-by: Roman Kazantsev <[email protected]>
Copy link
Contributor

@lazarevevgeny lazarevevgeny left a comment

Choose a reason for hiding this comment

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

No major comments.

@lazarevevgeny lazarevevgeny merged commit 5e8d1cc into openvinotoolkit:master Jun 4, 2021
yekruglov pushed a commit to yekruglov/openvino that referenced this pull request Jun 7, 2021
* Implement Einsum reference in nGraph interpreter

Signed-off-by: Roman Kazantsev <[email protected]>

* Apply code-style patch and fix build issues

Signed-off-by: Roman Kazantsev <[email protected]>

* Fix CI build

Signed-off-by: Roman Kazantsev <[email protected]>

* Fix fast build and apply code review feedback

Signed-off-by: Roman Kazantsev <[email protected]>

* Add visitor API tests, check for input type and reduce memory consumption

Signed-off-by: Roman Kazantsev <[email protected]>

* Apply code style

Signed-off-by: Roman Kazantsev <[email protected]>

* Add visitor API tests

Signed-off-by: Roman Kazantsev <[email protected]>
rnugmanx pushed a commit to rnugmanx/openvino that referenced this pull request Aug 26, 2021
* Implement Einsum reference in nGraph interpreter

Signed-off-by: Roman Kazantsev <[email protected]>

* Apply code-style patch and fix build issues

Signed-off-by: Roman Kazantsev <[email protected]>

* Fix CI build

Signed-off-by: Roman Kazantsev <[email protected]>

* Fix fast build and apply code review feedback

Signed-off-by: Roman Kazantsev <[email protected]>

* Add visitor API tests, check for input type and reduce memory consumption

Signed-off-by: Roman Kazantsev <[email protected]>

* Apply code style

Signed-off-by: Roman Kazantsev <[email protected]>

* Add visitor API tests

Signed-off-by: Roman Kazantsev <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
category: Core OpenVINO Core (aka ngraph)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants