Avoid "unsupported by backend contract: module initializers" when an attribute is written to but never read
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 1.9k
- Forks
- 736
- Avg merge
- 5d 22h
- Merged PRs (30d)
- 15
Description
The code
```
import torch
import torch_mlir
class Model(torch.nn.Module):
def __init__(self):
super().__init__()
self.p = torch.zeros([3])
def forward(self):
torch.ops.aten.uniform_(self.p, 0.0, 1.0)
return True
model = Model()
inps = ()
out = model(*inps)
print(out)
print(torch_mlir.compile(model, inps, output_type='raw'))
torch_mlir.compile(model, inps, output_type='torch')
```
fails with
```
python exception: Failure while executing pass pipeline:
error: unknown: unsupported by backend contract: module initializers
note: unknown: see current operation: "torch.initialize.global_slots"(%2) {slotSymNames = [@p]} : (!torch.tensor<[3],f32>) -> ()
note: unknown: this is likely due to InlineGlobalSlots being unable to inline a global slot
```
The IR is
```
// -----// IR Dump After LowerToBackendContract Failed (torch-lower-to-backend-contract) //----- //
module attributes {torch.debug_module_name = "Model"} {
torch.global_slot.module_initializer {
%none = torch.constant.none
%true = torch.constant.bool true
%0 = torch.tensor.literal(dense<[0.557137787, 0.770422995, 0.0203713775]> : tensor<3xf32>) : !torch.tensor<[3],f32>
torch.initialize.global_slots [
@p(%0 : !torch.tensor<[3],f32>)
]
}
torch.global_slot "private" @p : !torch.tensor
func.func @forward() -> !torch.bool {
%true = torch.constant.bool true
%none = torch.constant.none
%float0.000000e00 = torch.constant.float 0.000000e+00
%float1.000000e00 = torch.constant.float 1.000000e+00
%0 = torch.global_slot.get @p : !torch.tensor
%1 = torch.copy.to_vtensor %0 : !torch.vtensor
%2 = torch.aten.uniform %1, %float0.000000e00, %float1.000000e00, %none : !torch.vtensor, !torch.float, !torch.float, !torch.none -> !torch.vtensor
torch.overwrite.tensor.contents %2 overwrites %0 : !torch.vtensor, !torch.tensor
return %true : !torch.bool
}
}
```
I understand that it's generally complicated to allow models to modify the attributes, but I'm seeing some models that modify attributes which are then unused.
Is there a way to see that `self.p` single use is in `torch.ops.aten.uniform_` and thus remove that dead write?
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.
Assessment
This issue has not been assessed yet.