Skip to content

Changes to allow the export of functions with no user input. #8031

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

Merged
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
16 changes: 14 additions & 2 deletions exir/memory_planning.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from executorch.exir.tensor import TensorSpec

from torch import fx
from torch.export.exported_program import ExportGraphSignature
from torch.export.exported_program import ExportGraphSignature, InputKind
from torch.fx import Node
from torch.utils._pytree import tree_flatten

Expand Down Expand Up @@ -247,7 +247,19 @@ def verify_graph_input_output(self) -> None:
graph_output_allocated = allocated
has_dynamic_unbound_output |= has_dynamic_unbound_tensor

if "placeholder" in check_list:
# only check if inputs are allocated if there are user inputs:
user_inputs_exist = (
len(
list(
filter(
lambda input: input.kind == InputKind.USER_INPUT,
self.graph_signature.input_specs,
)
)
)
) > 0

if "placeholder" in check_list and user_inputs_exist:
Copy link
Contributor

@JacobSzwejbka JacobSzwejbka Jan 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this change looks fine. On our side we should probably take a pass over all the memory planning infra. A lot of it was written before export graphs were functionalized so the concept of non user inputs didnt make exist.

Like realistically we should /only/ be verifying the memory planning of user inputs here

assert graph_input_allocated is not None, "graph_input_allocated not set"
if not has_dynamic_unbound_input:
assert (
Expand Down
2 changes: 2 additions & 0 deletions exir/program/_program.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ def lift_constant_tensor_pass(ep):
new_input_specs.extend(lifted_constants)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And if we just insert all the constants at the beginning, we can just directly prepend to input_specs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, and removed all the logic I had previously, now:

No user inputs -> inserting before None -> inserts at the beginning.

lifted_constants.clear()
new_input_specs.append(s)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a test case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a test case, shifted to the simpler implementation of just prepending the signature since the order of the non-user inputs does not matter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have also validated that it works with my export process as well.

if len(lifted_constants) > 0:
new_input_specs = lifted_constants + new_input_specs
ep.graph_signature.input_specs = new_input_specs
ep.graph_module.recompile()
return ep
Expand Down
30 changes: 30 additions & 0 deletions exir/tests/test_passes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,36 @@ def forward(self, x: torch.Tensor) -> torch.Tensor:
new_ep.graph_module.code
)

def test_pass_no_user_inputs(self) -> None:
class NoUserInputs(torch.nn.Module):
def __init__(self):
super().__init__()
self.register_buffer("a", torch.ones(1))

def forward(self) -> torch.Tensor:
return 3 + self.a

mod = NoUserInputs()
exported_program = export(mod, (), strict=True)
edge = to_edge(
exported_program,
compile_config=EdgeCompileConfig(_skip_dim_order=False),
)
ep = edge.exported_program()
# because there is no user input, the lifted constant should be the first input.
FileCheck().check("_lifted_tensor_constant1").check(
"b_a" # followed by the buffer input.
).run(ep.graph_module.code)

# the graph signature should also be the same:
self.assertEqual(
ep.graph_signature.input_specs[0].arg.name, "_lifted_tensor_constant1"
)
self.assertEqual(ep.graph_signature.input_specs[1].arg.name, "b_a")

# Validate that the program successfully passes validation to executorch:
edge.to_executorch()

def test_constant_prop_pass_for_parameter(self) -> None:
def count_additions(gm: torch.fx.GraphModule) -> int:
return sum(
Expand Down
Loading