When compile custom_op, got error: failed to legalize operation 'torch.operator' that was explicitly marked illegal
Open
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 1.9k
- Forks
- 736
- Avg merge
- 5d 22h
- Merged PRs (30d)
- 15
Description
test code:
import pathlib
import torch
from torch_mlir import compiler_utils, torchscript
import numpy as np
@torch.library.custom_op("mylib::numpy_sin", mutates_args=[], schema='(Tensor x) -> Tensor')
def numpy_sin(input_tensor: torch.Tensor) -> torch.Tensor:
input_np = input_tensor.numpy()
output_np = np.zeros_like(input_np)
np.sin(input_np, out=output_np)
return torch.from_numpy(output_np).to(device=input_tensor.device)
class NumpySinModule(torch.nn.Module):
def forward(self, a: torch.Tensor) -> torch.Tensor:
return numpy_sin(a)
def test_custom_numpy_sin(tmp_path):
inputs = [torch.ones(128, 128, dtype=torch.float)]
model = NumpySinModule().eval()
compile_type = compiler_utils.OutputType.TORCH
result = torchscript.compile(
model, inputs[0], output_type=compile_type, use_tracing=True, enable_ir_printing=True
)
assert torch.allclose(result, inputs[0].sin())
if __name__ == '__main__':
test_custom_numpy_sin(pathlib.Path('.'))
bottom of error:
> raise TorchMlirCompilerError(trimmed_message) from None
E torch_mlir.compiler_utils.TorchMlirCompilerError: Lowering TorchScript IR -> Torch Backend IR failed with the following diagnostics:
E error: failed to legalize operation 'torch.operator' that was explicitly marked illegal
E note: see current operation: %3 = "torch.operator"(%2) <{name = "mylib.numpy_sin"}> : (!torch.tensor<[128,128],f32>) -> !torch.tensor<[128,128],f32>
E
E
E python exception: Failure while executing pass pipeline
E
E For Torch-MLIR developers, the error can be reproduced with:
E $ torch-mlir-opt -pass-pipeline='builtin.module(torchscript-module-to-torch-backend-pipeline{backend-legal-ops= extra-library=})' /tmp/NumpySinModule.mlir
E Add '-mlir-print-ir-after-all -mlir-disable-threading' to get the IR dump for debugging purpose.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the failure with the provided NumpySinModule test and the torch-mlir-opt pass pipeline shown in the error. Read the generated TorchScript IR around the torch.operator named mylib.numpy_sin and the backend-legal-ops and extra-library options; done means the custom operation lowers without the legalization error and the allclose assertion passes.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100