This repository was archived by the owner on Jun 11, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
[WIP] Support TorchScript and graph rewrite #54
Open
feihugis
wants to merge
12
commits into
microsoft:main
Choose a base branch
from
feihugis:dev_torchscript
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
3ac6c2c
Fix prophenet dict loading. (#58)
JiushengChen 62b6657
made ngram op device agnostic, unit test cleaned (#61)
NickNickGo 4c58e5a
Generate the XML log file for each fastseq unit test (#56)
feihugis 01e7d49
Update install_requires and enable fairseq to work with torch 1.6&1.7…
feihugis 7558a5c
Add missing init files (#62)
feihugis a55e9de
Update the instructions for installation (#64)
feihugis f9d5a82
Init code to support TorchScript and graph rewrite
feihugis 510f7f1
Improve einsum_rewrite_pattern
feihugis 83154f1
Make einsum_rewrite_pattern more general
feihugis 69e5410
Enhance rewrite pattern and tests
feihugis 699fba7
Add synchronize and check for contiguous
feihugis 19fcdb1
Test commit for public fastseq
feihugis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
|
|
||
| """Optmize einsum operation in the graph""" | ||
|
|
||
| from typing import List | ||
|
|
||
| import torch | ||
| from torch import Tensor | ||
|
|
||
| from fastseq.optimizer.jit.utils import graph_pattern, rewrite_graph | ||
|
|
||
| def einsum_pattern_0(t0: str, t1: List[Tensor]): | ||
| r = torch.einsum(t0, t1) | ||
| return r | ||
|
|
||
| def einsum_rewrite_pattern_0(eqn: str, operands: List[Tensor]): | ||
| # eqn = eqn.replace(' ', '') # TODO: fix the issue: ValueError: stoll | ||
| # for cases like "bmhtd,bnhsd->bmhts" | ||
| if (len(eqn) == 18 and eqn[0:4] == eqn[13:17] and eqn[0] == eqn[6] and | ||
| eqn[2] == eqn[8] and eqn[4] == eqn[10] and eqn[9] == eqn[17]): | ||
| t0 = operands[0] | ||
| t1 = operands[1] | ||
| b, m, h, t, d = t0.shape | ||
| s = t1.size(3) | ||
| n = t1.size(1) | ||
| t1 = t1.permute(0, 2, 3, 4, 1) # (b, h, s, d, n) | ||
| if n > 1: | ||
| t1 = t1.sum(dim=4, keepdim=True) # (b, h, s, d, 1) | ||
|
|
||
| t0 = t0.permute(0, 2, 1, 3, 4) # (b, h, m, t, d) | ||
| t1 = t1.permute(0, 1, 3, 4, 2) # (b, h, d, 1, s) | ||
| t0 = t0.reshape(b*h, m*t, d) | ||
| t1 = t1.view(b*h, d, s) | ||
| r = torch.bmm(t0, t1).view(b, h, m, t, s).permute(0, 2, 1, 3, 4) | ||
| return r | ||
|
|
||
| # for cases like "bmhts,bnhsd->bmhtd" | ||
| if (len(eqn) == 18 and eqn[0:4] == eqn[13:17] and eqn[0] == eqn[6] and | ||
| eqn[2] == eqn[8] and eqn[4] == eqn[9] and eqn[10] == eqn[17]): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same. |
||
| t0 = operands[0] | ||
| t1 = operands[1] | ||
| b, m, h, t, s = t0.shape | ||
| n = t1.size(1) | ||
| d = t1.size(4) | ||
| t1 = t1.permute(0, 2, 4, 3, 1) # (b, h, d, s, n) | ||
| if n > 1: | ||
| t1 = t1.sum(dim=4, keepdim=True) # (b, h, d, s, 1) | ||
| # t1 = t1.squeeze(1) # (b, h, s, d) | ||
| t0 = t0.permute(0, 2, 1, 3, 4) # (b, h, m, t, s) | ||
| t1 = t1.permute(0, 1, 3, 4, 2) # (b, h, s, 1, d) | ||
| t0 = t0.reshape(b*h, m*t, s) | ||
| t1 = t1.view(b*h, s, d) | ||
| r = torch.bmm(t0, t1).view(b, h, m, t, d).permute(0, 2, 1, 3, 4) | ||
| return r | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is returned tensor contiguous ? When comparing speedup with einsum, please take this into account as well.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, the returned tensor is not contiguous, and the output of |
||
|
|
||
| return torch.einsum(eqn, operands) | ||
|
|
||
| EINSUM_PATTERN_STR = graph_pattern(einsum_pattern_0)() | ||
| EINSUM_REWRITE_PATTERN_STR = graph_pattern(einsum_rewrite_pattern_0)() | ||
|
|
||
| def rewrite_einsum(input_graph: torch._C.Graph): | ||
| rewrite_graph(EINSUM_PATTERN_STR, EINSUM_REWRITE_PATTERN_STR, input_graph) | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
|
|
||
| """Load and apply the registered graph rewrite patterns""" | ||
|
|
||
| import torch | ||
|
|
||
| from fastseq.optimizer.jit.einsum_rewriter import rewrite_einsum | ||
|
|
||
| def optimize_graph(input_graph: torch._C.Graph): | ||
| rewrite_einsum(input_graph) |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we make this more general ? Same pattern can be used for equations without batch dimension.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer to leaving it as what it is. If we meet the cases in the future, it can be added easily with similar code block. To make it more general, it will be more like the implementation of einsum kernel.
From the micro benchmarking result, the runtime for large tensors will be very similar with/without the optimization.