Skip to content

[Relax][PyTorch] Support torch.bfloat16 dtype in pytorch frontend #17894

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
support bfloat16 dtype in pytorch frontend
  • Loading branch information
mshr-h committed Apr 25, 2025
commit cf33518b433da4809543d3b0f6281d96c24f01ee
2 changes: 2 additions & 0 deletions python/tvm/relax/frontend/torch/base_fx_graph_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ def _convert_data_type(input_type: Union[str, torch.dtype], env: Optional[Dict]
return "float32"
elif input_type in ["float16", "torch.float16", torch.float16]:
return "float16"
elif input_type in ["bfloat16", "torch.bfloat16", torch.bfloat16]:
return "bfloat16"
elif input_type in ["int64", "torch.int64", torch.int64]:
return "int64"
elif input_type in ["int32", "torch.int32", torch.int32]:
Expand Down
27 changes: 27 additions & 0 deletions tests/python/relax/test_frontend_from_exported_program.py
Original file line number Diff line number Diff line change
Expand Up @@ -4838,5 +4838,32 @@ def main(
verify_model(Linspace(), example_args, {}, Expected)


def test_bfloat16():
# TODO(mshr-h): Add tests for all the dtypes supported in fx frontend
example_args = (
torch.randn(10, 10, dtype=torch.bfloat16),
torch.randn(10, 10, dtype=torch.bfloat16),
)

class BFloat16Model(Module):
def forward(self, lhs: torch.Tensor, rhs: torch.Tensor):
return torch.ops.aten.add(lhs, rhs)

@tvm.script.ir_module
class expected:
@R.function
def main(
lhs: R.Tensor((10, 10), dtype="bfloat16"),
rhs: R.Tensor((10, 10), dtype="bfloat16"),
) -> R.Tuple(R.Tensor((10, 10), dtype="bfloat16")):
with R.dataflow():
lv: R.Tensor((10, 10), dtype="bfloat16") = relax.op.add(lhs, rhs)
gv: R.Tuple(R.Tensor((10, 10), dtype="bfloat16")) = (lv,)
R.output(gv)
return gv

verify_model(BFloat16Model(), example_args, {}, expected)


if __name__ == "__main__":
tvm.testing.main()
22 changes: 22 additions & 0 deletions tests/python/relax/test_frontend_from_fx.py
Original file line number Diff line number Diff line change
Expand Up @@ -5220,5 +5220,27 @@ def main(
verify_model(Norm(p, dim=dim, keepdim=keepdim), input_info, {}, expected)


def test_bfloat16():
# TODO(mshr-h): Add tests for all the dtypes supported in EP frontend
class BFloat16Model(Module):
def forward(self, lhs: torch.Tensor, rhs: torch.Tensor):
return torch.ops.aten.add(lhs, rhs)

@tvm.script.ir_module
class Expected:
@R.function
def main(
lhs: R.Tensor((10, 10), dtype="bfloat16"),
rhs: R.Tensor((10, 10), dtype="bfloat16"),
) -> R.Tensor((10, 10), dtype="bfloat16"):
with R.dataflow():
lv: R.Tensor((10, 10), dtype="bfloat16") = relax.op.add(lhs, rhs)
gv: R.Tensor((10, 10), dtype="bfloat16") = lv
R.output(gv)
return gv

verify_model(BFloat16Model(), [([10, 10], "bfloat16"), ([10, 10], "bfloat16")], {}, Expected)


if __name__ == "__main__":
tvm.testing.main()
Loading