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

[Relax][PyTorch][Fix] use_convert_torch_tensor_to_relax() where possible #17335

Merged
merged 2 commits into from
Sep 5, 2024
Merged
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
9 changes: 4 additions & 5 deletions python/tvm/relax/frontend/torch/fx_translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def _fetch_attr(self, model, target: str):
return attr_itr

@staticmethod
def _convert_data_type(input_type, env: Optional[Dict] = None):
def _convert_data_type(input_type: Union[str, torch.dtype], env: Optional[Dict] = None):
"""converts the PyTorch scalar type input_type to a TVM dtype."""
import torch # type: ignore

Expand Down Expand Up @@ -1206,9 +1206,8 @@ def _batch_norm_2d(self, node: fx.node.Node) -> relax.Var:
module = self.named_modules[node.target]
weight = self.params[module.weight]
bias = self.params[module.bias]
dtype = TorchFXImporter._convert_data_type(str(module.running_mean.dtype))
running_mean = relax.const(module.running_mean.cpu().detach().numpy(), dtype)
running_var = relax.const(module.running_var.cpu().detach().numpy(), dtype)
running_mean = self._convert_torch_tensor_to_relax(module.running_mean)
running_var = self._convert_torch_tensor_to_relax(module.running_var)
eps = module.eps

res_tuple = self.block_builder.emit(
Expand Down Expand Up @@ -1769,7 +1768,7 @@ def from_fx(
dtype = self._convert_data_type(str(param.data.dtype))
if dtype in ("float32", "float16"):
if not keep_params_as_input:
self.params[param] = relax.const(param.data.cpu().numpy(), dtype)
self.params[param] = self._convert_torch_tensor_to_relax(param)
else:
raise ValueError("Unsupported data type for model parameters: %s" % dtype)
# Translate the model.
Expand Down
Loading