Skip to content

Commit b031287

Browse files
authored
Fix deprecated importlib.resources and exir.capture() API usage (pytorch#16461)
- Replace deprecated `read_binary()` with `files().joinpath().read_bytes()` in xnnpack_graph_serialize.py - Replace deprecated `is_resource()` with `files().joinpath().is_file()` in _flatbuffer.py - Replace deprecated `exir.capture()` with `torch.export.export()` + `to_edge()` in bilinear_2d.py
1 parent c36b06a commit b031287

3 files changed

Lines changed: 21 additions & 18 deletions

File tree

‎backends/xnnpack/partition/graphs/bilinear_2d.py‎

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
from functools import lru_cache
88
from typing import Dict, List
99

10-
import executorch.exir as exir
1110
import torch
1211

1312
from executorch.backends.xnnpack.utils.configs import get_xnnpack_edge_compile_config
13+
from executorch.exir import to_edge
1414

1515

1616
@lru_cache(maxsize=None)
@@ -31,21 +31,19 @@ def forward(self, x):
3131

3232
sample_inputs = (torch.randn(1, 3, 4, 4),)
3333
_bilinear2d_graphs = {}
34-
capture_configs = [
35-
exir.CaptureConfig(enable_aot=True, _unlift=False),
36-
exir.CaptureConfig(enable_aot=True, _unlift=True),
37-
]
3834
for align_corners in [True, False]:
39-
for config in capture_configs:
40-
for skip_dim_order_flag in [True, False]:
41-
edge = exir.capture(
42-
bilinear2d(align_corners), sample_inputs, config
43-
).to_edge(
44-
config=get_xnnpack_edge_compile_config(
45-
skip_dim_order=skip_dim_order_flag
46-
)
47-
)
48-
_bilinear2d_graphs[edge.exported_program.graph_module] = align_corners
35+
for skip_dim_order_flag in [True, False]:
36+
# Use torch.export instead of deprecated exir.capture
37+
exported_program = torch.export.export(
38+
bilinear2d(align_corners), sample_inputs, strict=True
39+
)
40+
edge = to_edge(
41+
exported_program,
42+
compile_config=get_xnnpack_edge_compile_config(
43+
skip_dim_order=skip_dim_order_flag
44+
),
45+
)
46+
_bilinear2d_graphs[edge.exported_program().graph_module] = align_corners
4947
return _bilinear2d_graphs
5048

5149

‎backends/xnnpack/serialization/xnnpack_graph_serialize.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,9 @@ def convert_to_flatbuffer(xnnpack_graph: XNNGraph) -> bytes:
317317
schema_path = os.path.join(d, "schema.fbs")
318318
with open(schema_path, "wb") as schema_file:
319319
schema_file.write(
320-
_resources.read_binary(serialization_package, "schema.fbs")
320+
_resources.files(serialization_package)
321+
.joinpath("schema.fbs")
322+
.read_bytes()
321323
)
322324
json_path = os.path.join(d, "schema.json")
323325
with open(json_path, "wb") as json_file:

‎exir/_serialize/_flatbuffer.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,12 @@ def _run_flatc(args: Sequence[str]) -> None:
187187
If a resource matching _FLATC_RESOURCE_NAME exists, uses that executable.
188188
Otherwise, expects the `flatc` tool to be available on the system path.
189189
"""
190-
if importlib.resources.is_resource(__package__, _FLATC_RESOURCE_NAME):
190+
flatc_resource = importlib.resources.files(__package__).joinpath(
191+
_FLATC_RESOURCE_NAME
192+
)
193+
if flatc_resource.is_file():
191194
# Use the provided flatc binary.
192-
with importlib.resources.path(__package__, _FLATC_RESOURCE_NAME) as flatc_path:
195+
with importlib.resources.as_file(flatc_resource) as flatc_path:
193196
subprocess.run([flatc_path] + list(args), check=True)
194197
else:
195198
# Expect the `flatc` tool to be on the system path or set as an env var.

0 commit comments

Comments
 (0)