Skip to content

[fx] Accept func_visibility= and return created func op. #3054

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 1 commit into from
Mar 25, 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
43 changes: 33 additions & 10 deletions python/torch_mlir/extras/fx_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,9 @@ def sparsity_encoding(shape: torch.Size, sparsity: SparsityMeta) -> str:
if sparsity.layout is torch.sparse_coo:
assert sparse_dim >= 2 and blocksize is None
trail_dim = batch_dim + sparse_dim - 1
coords = ",".join(f"d{d}:singleton(nonunique,soa)" for d in range(batch_dim+1, trail_dim))
coords = ",".join(
f"d{d}:singleton(nonunique,soa)" for d in range(batch_dim + 1, trail_dim)
)
sep = "," if sparse_dim > 2 else ""
lvls = f"d{batch_dim}:compressed(nonunique),{coords}{sep}d{trail_dim}:singleton(soa)"
elif sparsity.layout is torch.sparse_csr:
Expand Down Expand Up @@ -467,8 +469,12 @@ def module_op(self) -> Operation:
return self._m.operation

def import_program(
self, prog: torch.export.ExportedProgram, *, func_name: str = "main"
):
self,
prog: torch.export.ExportedProgram,
*,
func_name: str = "main",
func_visibility: Optional[str] = None,
) -> Operation:
"""Imports an ExportedProgram according to our chosen canonical representation.

This mechanism is the fully general solution for handling an ExportedProgram
Expand Down Expand Up @@ -628,7 +634,9 @@ def import_program(

# Create the function.
with loc:
func_op = func_dialect.FuncOp(func_name, ftype, ip=self._m_ip)
func_op = func_dialect.FuncOp(
func_name, ftype, ip=self._m_ip, visibility=func_visibility
)
entry_block = Block.create_at_start(func_op.body, ftype.inputs)

node_importer = GraphNodeImporter(
Expand Down Expand Up @@ -668,10 +676,15 @@ def import_program(
)
node_importer.return_node_values(loc, user_outputs)
self.symbol_table.insert(func_op)
return func_op

def import_frozen_program(
self, prog: torch.export.ExportedProgram, func_name: str = "main"
):
self,
prog: torch.export.ExportedProgram,
*,
func_name: str = "main",
func_visibility: Optional[str] = None,
) -> Operation:
"""Imports a consolidated torch.export.ExportedProgram instance.

If using the new torch.export path (vs a lower level precursor), then this is
Expand Down Expand Up @@ -750,17 +763,25 @@ def import_frozen_program(
node.replace_all_uses_with(replacement)
g.erase_node(node)

self.import_stateless_graph(g, func_name)
return self.import_stateless_graph(
g, func_name=func_name, func_visibility=func_visibility
)

def import_graph_module(self, gm: GraphModule):
def import_graph_module(self, gm: GraphModule) -> Operation:
"""Low-level import of a GraphModule assuming that it has been functionalized.

TODO: This mechanism is deprecated by the `import_program` entry-point and
it should be removed when no longer required for backwards compatibility.
"""
self.import_stateless_graph(gm.graph)
return self.import_stateless_graph(gm.graph)

def import_stateless_graph(self, g: Graph, func_name: str = "main"):
def import_stateless_graph(
self,
g: Graph,
*,
func_name: str = "main",
func_visibility: Optional[str] = None,
) -> Operation:
"""Low-level import of a functionalized, assumed stateless Graph as a func.

TODO: This mechanism is deprecated by the `import_program` entry-point and
Expand All @@ -775,6 +796,7 @@ def import_stateless_graph(self, g: Graph, func_name: str = "main"):
func_name,
ftype,
ip=self._m_ip,
visibility=func_visibility,
)
entry_block = Block.create_at_start(func.body, ftype.inputs)
node_importer = GraphNodeImporter(
Expand All @@ -785,6 +807,7 @@ def import_stateless_graph(self, g: Graph, func_name: str = "main"):
)
node_importer.import_nodes(g.nodes)
self.symbol_table.insert(func)
return func

def _graph_to_function_meta(self, g: Graph) -> Tuple[FunctionType, Location]:
"""Extracts function metadata from the Graph.
Expand Down