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

[Fx Importer] fix mutation importer with non persistent buffer #3798

Merged
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
15 changes: 11 additions & 4 deletions python/torch_mlir/extras/fx_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -723,10 +723,17 @@ def import_program(
# on a symbolic or other non-SSA association. As such, they
# are not modeled with mutable IR but will trigger an output
# store hook when the final value is produced.
value = prog.state_dict.get(input_spec.target)
assert (
not input_spec.persistent or value is not None
), "Expected state_dict value for persistent value"
if input_spec.persistent:
value = prog.state_dict.get(input_spec.target)
assert (
value is not None
), "Expected state_dict value for persistent buffer"
else:
value = prog.constants.get(input_spec.target)
assert (
value is not None
), "Expected constants value for non-persistent buffer"

node = placeholder_nodes[arg.name]
mutable_producer_node_name = mutable_buffer_target_producers.get(
input_spec.target
Expand Down
21 changes: 21 additions & 0 deletions test/python/fx_importer/v2.3/mutation_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,27 @@ def forward(self, x):
m.operation.verify()


@run
# CHECK-LABEL: test_frozen_buffer_non_persistent
# CHECK: %[[buffer_literal:.+]] = torch.vtensor.literal
# CHECK: %[[mul:.+]] = torch.aten.mul.Tensor %arg0, %0
# CHECK: return %[[mul]]
def test_frozen_buffer_non_persistent():
class Basic(nn.Module):
def __init__(self):
super().__init__()
self.register_buffer("buffer", torch.randn(3, 4), persistent=False)
stellaraccident marked this conversation as resolved.
Show resolved Hide resolved

def forward(self, x):
return x * self.buffer

m = fx.export_and_import(
Basic(), torch.randn(3, 4), experimental_support_mutation=True
)
print(m)
m.operation.verify()


class ExternalBufferHooks(fx.FxImporterHooks):
def prepare_module(self, module_op: Operation):
module_op.context.allow_unregistered_dialects = True
Expand Down
Loading