-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[frontend] Add tests for tosa operator conversion functions
- Loading branch information
Showing
23 changed files
with
757 additions
and
3 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
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# RUN: %PYTHON %s 2>&1 | FileCheck %s | ||
|
||
import torch | ||
import torch._dynamo as dynamo | ||
from torch._inductor.decomposition import decompositions as inductor_decomp | ||
|
||
from buddy.compiler.frontend import DynamoCompiler | ||
from buddy.compiler.ops import tosa | ||
|
||
|
||
def foo(x, y, z): | ||
return torch.ops.aten.addmm(z, x, y) | ||
|
||
|
||
in1 = torch.randn(4, 2) | ||
in2 = torch.randn(2, 4) | ||
in3 = torch.randn(4, 4) | ||
|
||
# Initialize the dynamo compiler. | ||
dynamo_compiler = DynamoCompiler( | ||
primary_registry=tosa.ops_registry, | ||
aot_autograd_decomposition=inductor_decomp, | ||
) | ||
|
||
foo_mlir = dynamo.optimize(dynamo_compiler)(foo) | ||
foo_mlir(in1, in2, in3) | ||
|
||
# CHECK: module { | ||
# CHECK-LABEL: func.func @forward | ||
# CHECK: %{{.*}} = "tosa.matmul" | ||
# CHECK: %{{.*}} = "tosa.add" | ||
# CHECK: return %{{.*}} | ||
# CHECK: } | ||
# CHECK: } | ||
print(dynamo_compiler.imported_module) |
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# RUN: %PYTHON %s 2>&1 | FileCheck %s | ||
|
||
import random | ||
import torch | ||
import torch._dynamo as dynamo | ||
from torch._inductor.decomposition import decompositions as inductor_decomp | ||
|
||
from buddy.compiler.frontend import DynamoCompiler | ||
from buddy.compiler.ops import tosa | ||
|
||
|
||
def foo(x, dim): | ||
return torch.ops.aten.amax(x, dim, True) | ||
|
||
|
||
in1 = torch.randn(4, 5, 2, 9) | ||
dim = [random.randint(0, 3)] | ||
|
||
# Initialize the dynamo compiler. | ||
dynamo_compiler = DynamoCompiler( | ||
primary_registry=tosa.ops_registry, | ||
aot_autograd_decomposition=inductor_decomp, | ||
) | ||
|
||
foo_mlir = dynamo.optimize(dynamo_compiler)(foo) | ||
foo_mlir(in1, dim) | ||
|
||
# CHECK: module { | ||
# CHECK-LABEL: func.func @forward | ||
# CHECK: %{{.*}} = "tosa.reduce_max" | ||
# CHECK: return %{{.*}} | ||
# CHECK: } | ||
# CHECK: } | ||
print(dynamo_compiler.imported_module) |
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# RUN: %PYTHON %s 2>&1 | FileCheck %s | ||
|
||
import torch | ||
import torch._dynamo as dynamo | ||
from torch._inductor.decomposition import decompositions as inductor_decomp | ||
|
||
from buddy.compiler.frontend import DynamoCompiler | ||
from buddy.compiler.ops import tosa | ||
|
||
|
||
def foo(x, y): | ||
return x / y | ||
|
||
|
||
in1 = torch.randn(10) | ||
in2 = torch.randn(10) | ||
|
||
# Initialize the dynamo compiler. | ||
dynamo_compiler = DynamoCompiler( | ||
primary_registry=tosa.ops_registry, | ||
aot_autograd_decomposition=inductor_decomp, | ||
) | ||
|
||
foo_mlir = dynamo.optimize(dynamo_compiler)(foo) | ||
foo_mlir(in1, in2) | ||
|
||
# CHECK: module { | ||
# CHECK-LABEL: func.func @forward | ||
# CHECK: %{{.*}} = "tosa.reciprocal" | ||
# CHECK: %{{.*}} = "tosa.mul" | ||
# CHECK: return %{{.*}} | ||
# CHECK: } | ||
# CHECK: } | ||
print(dynamo_compiler.imported_module) |
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# RUN: %PYTHON %s 2>&1 | FileCheck %s | ||
|
||
import torch | ||
import torch._dynamo as dynamo | ||
from torch._inductor.decomposition import decompositions as inductor_decomp | ||
|
||
from buddy.compiler.frontend import DynamoCompiler | ||
from buddy.compiler.ops import tosa | ||
|
||
|
||
def foo(x, y): | ||
return x * y | ||
|
||
|
||
in1 = torch.randn(10) | ||
in2 = torch.randn(10) | ||
|
||
# Initialize the dynamo compiler. | ||
dynamo_compiler = DynamoCompiler( | ||
primary_registry=tosa.ops_registry, | ||
aot_autograd_decomposition=inductor_decomp, | ||
) | ||
|
||
foo_mlir = dynamo.optimize(dynamo_compiler)(foo) | ||
foo_mlir(in1, in2) | ||
|
||
# CHECK: module { | ||
# CHECK-LABEL: func.func @forward | ||
# CHECK: %{{.*}} = "tosa.mul" | ||
# CHECK: return %{{.*}} | ||
# CHECK: } | ||
# CHECK: } | ||
print(dynamo_compiler.imported_module) |
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# RUN: %PYTHON %s 2>&1 | FileCheck %s | ||
|
||
import torch | ||
import torch._dynamo as dynamo | ||
from torch._inductor.decomposition import decompositions as inductor_decomp | ||
|
||
from buddy.compiler.frontend import DynamoCompiler | ||
from buddy.compiler.ops import tosa | ||
|
||
|
||
def foo(x, y): | ||
return x - y | ||
|
||
|
||
in1 = torch.randn(10) | ||
in2 = torch.randn(10) | ||
|
||
# Initialize the dynamo compiler. | ||
dynamo_compiler = DynamoCompiler( | ||
primary_registry=tosa.ops_registry, | ||
aot_autograd_decomposition=inductor_decomp, | ||
) | ||
|
||
foo_mlir = dynamo.optimize(dynamo_compiler)(foo) | ||
foo_mlir(in1, in2) | ||
|
||
# CHECK: module { | ||
# CHECK-LABEL: func.func @forward | ||
# CHECK: %{{.*}} = "tosa.sub" | ||
# CHECK: return %{{.*}} | ||
# CHECK: } | ||
# CHECK: } | ||
print(dynamo_compiler.imported_module) |
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# RUN: %PYTHON %s 2>&1 | FileCheck %s | ||
|
||
import torch | ||
import torch._dynamo as dynamo | ||
from torch._inductor.decomposition import decompositions as inductor_decomp | ||
|
||
from buddy.compiler.frontend import DynamoCompiler | ||
from buddy.compiler.ops import tosa | ||
|
||
|
||
def foo(x, y): | ||
return torch.ops.aten.bmm(x, y) | ||
|
||
|
||
in1 = torch.randn(10, 3, 2) | ||
in2 = torch.randn(10, 2, 3) | ||
|
||
# Initialize the dynamo compiler. | ||
dynamo_compiler = DynamoCompiler( | ||
primary_registry=tosa.ops_registry, | ||
aot_autograd_decomposition=inductor_decomp, | ||
) | ||
|
||
foo_mlir = dynamo.optimize(dynamo_compiler)(foo) | ||
foo_mlir(in1, in2) | ||
|
||
# CHECK: module { | ||
# CHECK-LABEL: func.func @forward | ||
# CHECK: %{{.*}} = "tosa.matmul" | ||
# CHECK: return %{{.*}} | ||
# CHECK: } | ||
# CHECK: } | ||
print(dynamo_compiler.imported_module) |
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# RUN: %PYTHON %s 2>&1 | FileCheck %s | ||
|
||
import torch | ||
import torch._dynamo as dynamo | ||
from torch._inductor.decomposition import decompositions as inductor_decomp | ||
|
||
from buddy.compiler.frontend import DynamoCompiler | ||
from buddy.compiler.ops import tosa | ||
|
||
|
||
def foo(x): | ||
return torch.ops.aten.clone(x) | ||
|
||
|
||
in1 = torch.randn(10) | ||
|
||
# Initialize the dynamo compiler. | ||
dynamo_compiler = DynamoCompiler( | ||
primary_registry=tosa.ops_registry, | ||
aot_autograd_decomposition=inductor_decomp, | ||
) | ||
|
||
foo_mlir = dynamo.optimize(dynamo_compiler)(foo) | ||
foo_mlir(in1) | ||
|
||
# CHECK: module { | ||
# CHECK-LABEL: func.func @forward | ||
# CHECK: %{{.*}} = "tosa.identity" | ||
# CHECK: return %{{.*}} | ||
# CHECK: } | ||
# CHECK: } | ||
print(dynamo_compiler.imported_module) |
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# RUN: %PYTHON %s 2>&1 | FileCheck %s | ||
|
||
import torch | ||
import torch._dynamo as dynamo | ||
from torch._inductor.decomposition import decompositions as inductor_decomp | ||
|
||
from buddy.compiler.frontend import DynamoCompiler | ||
from buddy.compiler.ops import tosa | ||
|
||
|
||
def foo(x, to_cast_type): | ||
return torch.ops.prims.convert_element_type(x, to_cast_type) | ||
|
||
|
||
in1 = torch.randn(10).to(torch.float32) | ||
to_cast_type = torch.float16 | ||
|
||
# Initialize the dynamo compiler. | ||
dynamo_compiler = DynamoCompiler( | ||
primary_registry=tosa.ops_registry, | ||
aot_autograd_decomposition=inductor_decomp, | ||
) | ||
|
||
foo_mlir = dynamo.optimize(dynamo_compiler)(foo) | ||
foo_mlir(in1, to_cast_type) | ||
|
||
# CHECK: module { | ||
# CHECK-LABEL: func.func @forward | ||
# CHECK: %{{.*}} = "tosa.cast" | ||
# CHECK: return %{{.*}} | ||
# CHECK: } | ||
# CHECK: } | ||
print(dynamo_compiler.imported_module) |
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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# RUN: %PYTHON %s 2>&1 | FileCheck %s | ||
|
||
import torch | ||
import torch._dynamo as dynamo | ||
from torch._inductor.decomposition import decompositions as inductor_decomp | ||
|
||
from buddy.compiler.frontend import DynamoCompiler | ||
from buddy.compiler.ops import tosa | ||
|
||
|
||
def foo(weight, indices): | ||
return torch.ops.aten.embedding(weight, indices) | ||
|
||
|
||
# Initialize the dynamo compiler. | ||
dynamo_compiler = DynamoCompiler( | ||
primary_registry=tosa.ops_registry, | ||
aot_autograd_decomposition=inductor_decomp, | ||
) | ||
|
||
# test trivial case | ||
weight = torch.randn(10, 5) | ||
indices = torch.randint(10, (3, 3)).to(torch.int32) | ||
|
||
foo_mlir = dynamo.optimize(dynamo_compiler)(foo) | ||
foo_mlir(weight, indices) | ||
|
||
# CHECK: module { | ||
# CHECK-LABEL: func.func @forward | ||
# CHECK: %{{.*}} = "tosa.reshape" | ||
# CHECK: %{{.*}} = "tosa.reshape" | ||
# CHECK: %{{.*}} = "tosa.gather" | ||
# CHECK: %{{.*}} = "tosa.reshape" | ||
# CHECK: return %{{.*}} | ||
# CHECK: } | ||
# CHECK: } | ||
print(dynamo_compiler.imported_module) | ||
|
||
|
||
# test cast case | ||
weight = torch.randn(10, 5) | ||
indices = torch.randint(10, (3, 3)).to(torch.int64) | ||
|
||
|
||
foo_mlir = dynamo.optimize(dynamo_compiler)(foo) | ||
foo_mlir(weight, indices) | ||
|
||
# CHECK: module { | ||
# CHECK-LABEL: func.func @forward | ||
# CHECK: %{{.*}} = "tosa.reshape" | ||
# CHECK: %{{.*}} = "tosa.cast" | ||
# CHECK: %{{.*}} = "tosa.reshape" | ||
# CHECK: %{{.*}} = "tosa.gather" | ||
# CHECK: %{{.*}} = "tosa.reshape" | ||
# CHECK: return %{{.*}} | ||
# CHECK: } | ||
# CHECK: } | ||
print(dynamo_compiler.imported_module) |
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# RUN: %PYTHON %s 2>&1 | FileCheck %s | ||
|
||
import torch | ||
import torch._dynamo as dynamo | ||
from torch._inductor.decomposition import decompositions as inductor_decomp | ||
|
||
from buddy.compiler.frontend import DynamoCompiler | ||
from buddy.compiler.ops import tosa | ||
|
||
|
||
def foo(x): | ||
return torch.ops.aten.exp(x) | ||
|
||
|
||
in1 = torch.randn(10) | ||
|
||
# Initialize the dynamo compiler. | ||
dynamo_compiler = DynamoCompiler( | ||
primary_registry=tosa.ops_registry, | ||
aot_autograd_decomposition=inductor_decomp, | ||
) | ||
|
||
foo_mlir = dynamo.optimize(dynamo_compiler)(foo) | ||
foo_mlir(in1) | ||
|
||
# CHECK: module { | ||
# CHECK-LABEL: func.func @forward | ||
# CHECK: %{{.*}} = "tosa.exp" | ||
# CHECK: return %{{.*}} | ||
# CHECK: } | ||
# CHECK: } | ||
print(dynamo_compiler.imported_module) |
Oops, something went wrong.