-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
62 additions
and
38 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1,34 +1,65 @@ | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT License. | ||
|
||
from typing import List | ||
import functools | ||
import logging | ||
import timeit | ||
|
||
from absl.testing import absltest | ||
from absl.testing import absltest, parameterized | ||
import torch | ||
from torch import Tensor | ||
|
||
from fastseq.logging import get_logger | ||
from fastseq.optimizer.jit.einsum_rewriter import rewrite_einsum | ||
from fastseq.utils.test_utils import TestCaseBase | ||
|
||
logger = get_logger(__name__, logging.INFO) | ||
|
||
class EinsumRewriterTest(TestCaseBase): | ||
|
||
def test_einsum_rewriter(self): | ||
@parameterized.parameters( | ||
{'eqn': "bmhtd,bnhsd->bmhts", | ||
'shape0': [128, 4, 16, 5, 64], | ||
'shape1': [128, 2, 16, 1024, 64]}, | ||
{'eqn': "kmijd,knisd->kmijs", | ||
'shape0': [128, 4, 16, 1, 64], | ||
'shape1': [128, 2, 16, 1024, 64]}, | ||
{'eqn': "bmhts,bnhsd->bmhtd", | ||
'shape0': [128, 4, 16, 3, 64], | ||
'shape1': [128, 2, 16, 64, 7]}, | ||
{'eqn': "impts,inpsw->imptw", | ||
'shape0': [128, 4, 16, 3, 64], | ||
'shape1': [128, 2, 16, 64, 7]}, | ||
) | ||
def test_einsum_rewriter(self, eqn, shape0, shape1): | ||
|
||
def run_einsum(t0: Tensor, t1: Tensor): | ||
r = torch.einsum("bmhtd,bnhsd->bmhts", t0, t1) | ||
r = r + 2.0 | ||
def run_einsum(eqn: str, t0: Tensor, t1: Tensor): | ||
r = torch.einsum(eqn, t0, t1) | ||
return r | ||
|
||
t0 = torch.randn(10, 3, 4, 3, 9, dtype=torch.float32) | ||
t1 = torch.randn(10, 1, 4, 7, 9, dtype=torch.float32) | ||
t0 = torch.randn(shape0, dtype=torch.float32).cuda() | ||
t1 = torch.randn(shape1, dtype=torch.float32).cuda() | ||
repeat_times = 1000 | ||
|
||
r0 = run_einsum(t0, t1) | ||
r0 = run_einsum(eqn, t0, t1) | ||
time0 = timeit.Timer(functools.partial(run_einsum, eqn, t0, t1)) | ||
s0 = time0.timeit(repeat_times) | ||
|
||
script_run_einsum = torch.jit.script(run_einsum) | ||
logger.debug(f"Original graph: \n{script_run_einsum.graph.str()}") | ||
rewrite_einsum(script_run_einsum.graph) | ||
r1 = script_run_einsum(t0, t1) | ||
logger.debug(f"Optimized graph: \n{script_run_einsum.graph.str()}") | ||
self.assertTrue('bmm' in script_run_einsum.graph.str()) | ||
|
||
r1 = script_run_einsum(eqn, t0, t1) | ||
time1 = timeit.Timer( | ||
functools.partial(script_run_einsum, eqn, t0, t1)) | ||
s1 = time1.timeit(repeat_times) | ||
|
||
self.assertTrue(torch.equal(r0, r1)) | ||
logger.info(f"einsum took: {s0}; optimized einsum torchscript took: " | ||
f"{s1};") | ||
|
||
|
||
if __name__ == "__main__": | ||
absltest.main() |