Skip to content

Commit 8cb191e

Browse files
authored
Update deprecated pkg_resources api in edge._ops (#13513)
pkg_resources is deprecated and set to be removed from setuptools as early as end of 2025. Since ET doesn't have an upper bound on the setuptools dependency this could be an issue. Additionally, this outputs a warning message everytime an executor_runner is built. This change updates the pkg_resrouces api to the more modern importlib.resources Test plan: Build linux wheels using ci. Download python 3.10 and wheel and install in new venv run ``` from executorch.exir.dialects._ops import ops as exir_ops from executorch.backends.apple.mps import serialization from executorch.backends.vulkan import serialization from executorch.backends.xnnpack import serialization from executorch.devtools.bundled_program import serialize from executorch.devtools.etdump import serialize from executorch.examples.models import checkpoint checkpoint.get_default_model_resource_dir("examples/models/llama") from executorch.exir.serde import schema_check from executorch.extension.flat_tensor.serialize import serialize ``` in a fresh python interpreter for every import The imports should not crash (due to importlib), and should not generate the warning message that it currently does Signed-off-by: Erik Lundell <[email protected]>
1 parent afb09d0 commit 8cb191e

File tree

13 files changed

+88
-69
lines changed

13 files changed

+88
-69
lines changed

backends/apple/mps/serialization/mps_graph_serialize.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
# Copyright (c) Meta Platforms, Inc. and affiliates.
22
# All rights reserved.
3+
# Copyright 2025 Arm Limited and/or its affiliates.
34
#
45
# This source code is licensed under the BSD-style license found in the
56
# LICENSE file in the root directory of this source tree.
67

8+
import importlib.resources as _resources
79
import json
810
import os
911
import tempfile
1012

11-
import pkg_resources
13+
import executorch.backends.apple.mps.serialization as serialization_package
1214
from executorch.backends.apple.mps.serialization.mps_graph_schema import MPSGraph
1315
from executorch.exir._serialize._dataclass import _DataclassEncoder
1416
from executorch.exir._serialize._flatbuffer import _flatc_compile
@@ -19,7 +21,9 @@ def convert_to_flatbuffer(mps_graph: MPSGraph) -> bytes:
1921
with tempfile.TemporaryDirectory() as d:
2022
schema_path = os.path.join(d, "schema.fbs")
2123
with open(schema_path, "wb") as schema_file:
22-
schema_file.write(pkg_resources.resource_string(__name__, "schema.fbs"))
24+
schema_file.write(
25+
_resources.read_binary(serialization_package, "schema.fbs")
26+
)
2327
json_path = os.path.join(d, "schema.json")
2428
with open(json_path, "wb") as json_file:
2529
json_file.write(mps_graph_json.encode("ascii"))

backends/arm/test/ops/test_sum.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ def test_sum_dim_intlist_tosa_INT(test_data: input_t1):
6060
aten_op,
6161
exir_op=[],
6262
)
63+
pipeline.dump_artifact("export")
6364
pipeline.run()
6465

6566

backends/qualcomm/serialization/qc_schema_serialize.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
# Copyright (c) Qualcomm Innovation Center, Inc.
2+
# Copyright 2025 Arm Limited and/or its affiliates.
23
# All rights reserved
34
#
45
# This source code is licensed under the BSD-style license found in the
56
# LICENSE file in the root directory of this source tree.
67

8+
import importlib.resources as _resources
79
import json
810
import os
911
import tempfile
1012

11-
import pkg_resources
13+
import executorch.backends.qualcomm.serialization as serialization_package
1214
from executorch.backends.qualcomm.serialization.qc_schema import QnnExecuTorchOptions
1315
from executorch.exir._serialize._dataclass import _DataclassEncoder, _json_to_dataclass
1416
from executorch.exir._serialize._flatbuffer import _flatc_compile, _flatc_decompile
@@ -19,7 +21,9 @@ def _convert_to_flatbuffer(obj, schema: str):
1921
with tempfile.TemporaryDirectory() as d:
2022
schema_path = os.path.join(d, f"{schema}.fbs")
2123
with open(schema_path, "wb") as schema_file:
22-
schema_file.write(pkg_resources.resource_string(__name__, f"{schema}.fbs"))
24+
schema_file.write(
25+
_resources.read_binary(serialization_package, f"{schema}.fbs")
26+
)
2327
json_path = os.path.join(d, f"{schema}.json")
2428
with open(json_path, "wb") as json_file:
2529
json_file.write(obj_json.encode("ascii"))
@@ -36,7 +40,9 @@ def _convert_to_object(flatbuffers: bytes, obj_type, schema: str):
3640
schema_path = os.path.join(d, f"{schema}.fbs")
3741
bin_path = os.path.join(d, f"{schema}.bin")
3842
with open(schema_path, "wb") as schema_file:
39-
schema_file.write(pkg_resources.resource_string(__name__, f"{schema}.fbs"))
43+
schema_file.write(
44+
_resources.read_binary(serialization_package, f"{schema}.fbs")
45+
)
4046
with open(bin_path, "wb") as bin_file:
4147
bin_file.write(flatbuffers)
4248

backends/vulkan/serialization/vulkan_graph_serialize.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
# Copyright (c) Meta Platforms, Inc. and affiliates.
22
# All rights reserved.
3+
# Copyright 2025 Arm Limited and/or its affiliates.
34
#
45
# pyre-strict
56
#
67
# This source code is licensed under the BSD-style license found in the
78
# LICENSE file in the root directory of this source tree.
89

910
import ctypes
11+
import importlib.resources as _resources
1012
import json
1113
import os
1214
import tempfile
13-
1415
from dataclasses import dataclass
1516
from typing import ClassVar, List
1617

17-
import pkg_resources
18+
import executorch.backends.vulkan.serialization as serialization_package
19+
1820
import torch
1921

2022
from executorch.backends.vulkan.serialization.vulkan_graph_schema import (
2123
VkBytes,
2224
VkGraph,
2325
)
2426
from executorch.exir._serialize._dataclass import _DataclassEncoder, _json_to_dataclass
25-
2627
from executorch.exir._serialize._flatbuffer import _flatc_compile, _flatc_decompile
2728

2829

@@ -32,7 +33,9 @@ def convert_to_flatbuffer(vk_graph: VkGraph) -> bytes:
3233
with tempfile.TemporaryDirectory() as d:
3334
schema_path = os.path.join(d, "schema.fbs")
3435
with open(schema_path, "wb") as schema_file:
35-
schema_file.write(pkg_resources.resource_string(__name__, "schema.fbs"))
36+
schema_file.write(
37+
_resources.read_binary(serialization_package, "schema.fbs")
38+
)
3639
json_path = os.path.join(d, "schema.json")
3740
with open(json_path, "wb") as json_file:
3841
json_file.write(vk_graph_json.encode("ascii"))
@@ -48,7 +51,9 @@ def flatbuffer_to_vk_graph(flatbuffers: bytes) -> VkGraph:
4851
with tempfile.TemporaryDirectory() as d:
4952
schema_path = os.path.join(d, "schema.fbs")
5053
with open(schema_path, "wb") as schema_file:
51-
schema_file.write(pkg_resources.resource_string(__name__, "schema.fbs"))
54+
schema_file.write(
55+
_resources.read_binary(serialization_package, "schema.fbs")
56+
)
5257

5358
bin_path = os.path.join(d, "schema.bin")
5459
with open(bin_path, "wb") as bin_file:

backends/xnnpack/serialization/xnnpack_graph_serialize.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
# Copyright (c) Meta Platforms, Inc. and affiliates.
22
# All rights reserved.
3+
# Copyright 2025 Arm Limited and/or its affiliates.
34
#
45
# This source code is licensed under the BSD-style license found in the
56
# LICENSE file in the root directory of this source tree.
67

8+
import importlib.resources as _resources
79
import json
8-
910
import logging
1011
import os
1112
import tempfile
12-
1313
from dataclasses import dataclass, fields, is_dataclass
1414
from typing import ClassVar, Literal, Optional
1515

16-
import pkg_resources
16+
import executorch.backends.xnnpack.serialization as serialization_package
1717
from executorch.backends.xnnpack.serialization.xnnpack_graph_schema import XNNGraph
1818
from executorch.exir._serialize._dataclass import _DataclassEncoder
19-
2019
from executorch.exir._serialize._flatbuffer import _flatc_compile
2120

2221
logger = logging.getLogger(__name__)
@@ -317,7 +316,9 @@ def convert_to_flatbuffer(xnnpack_graph: XNNGraph) -> bytes:
317316
with tempfile.TemporaryDirectory() as d:
318317
schema_path = os.path.join(d, "schema.fbs")
319318
with open(schema_path, "wb") as schema_file:
320-
schema_file.write(pkg_resources.resource_string(__name__, "schema.fbs"))
319+
schema_file.write(
320+
_resources.read_binary(serialization_package, "schema.fbs")
321+
)
321322
json_path = os.path.join(d, "schema.json")
322323
with open(json_path, "wb") as json_file:
323324
json_file.write(xnnpack_graph_json.encode("ascii"))

devtools/bundled_program/serialize/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Copyright (c) Meta Platforms, Inc. and affiliates.
22
# All rights reserved.
3+
# Copyright 2025 Arm Limited and/or its affiliates.
34
#
45
# This source code is licensed under the BSD-style license found in the
56
# LICENSE file in the root directory of this source tree.
@@ -8,16 +9,15 @@
89

910
# TODO(T138924864): Refactor to unify the serialization for bundled program and executorch program.
1011

12+
import importlib.resources as _resources
1113
import json
1214
import os
1315
import tempfile
1416

1517
import executorch.devtools.bundled_program.schema as bp_schema
1618

17-
# @manual=fbsource//third-party/pypi/setuptools:setuptools
18-
import pkg_resources
19+
import executorch.devtools.bundled_program.serialize as serialization_package
1920
from executorch.devtools.bundled_program.core import BundledProgram
20-
2121
from executorch.exir._serialize._dataclass import _DataclassEncoder, _json_to_dataclass
2222
from executorch.exir._serialize._flatbuffer import _flatc_compile, _flatc_decompile
2323

@@ -30,7 +30,7 @@ def write_schema(d: str, schema_name: str) -> None:
3030
schema_path = os.path.join(d, "{}.fbs".format(schema_name))
3131
with open(schema_path, "wb") as schema_file:
3232
schema_file.write(
33-
pkg_resources.resource_string(__name__, "{}.fbs".format(schema_name))
33+
_resources.read_binary(serialization_package, f"{schema_name}.fbs")
3434
)
3535

3636

devtools/etdump/serialize.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
# Copyright (c) Meta Platforms, Inc. and affiliates.
22
# All rights reserved.
3+
# Copyright 2025 Arm Limited and/or its affiliates.
34
#
45
# This source code is licensed under the BSD-style license found in the
56
# LICENSE file in the root directory of this source tree.
67

78
# pyre-strict
89

10+
import importlib.resources as _resources
911
import json
1012
import os
1113
import tempfile
1214

13-
import pkg_resources
15+
import executorch.devtools.etdump as etdump_package
1416
from executorch.devtools.etdump.schema_flatcc import ETDumpFlatCC
15-
1617
from executorch.exir._serialize._dataclass import _DataclassEncoder, _json_to_dataclass
17-
1818
from executorch.exir._serialize._flatbuffer import _flatc_compile, _flatc_decompile
1919

2020
# The prefix of schema files used for etdump
@@ -25,9 +25,7 @@
2525
def _write_schema(d: str, schema_name: str) -> None:
2626
schema_path = os.path.join(d, "{}.fbs".format(schema_name))
2727
with open(schema_path, "wb") as schema_file:
28-
schema_file.write(
29-
pkg_resources.resource_string(__name__, "{}.fbs".format(schema_name))
30-
)
28+
schema_file.write(_resources.read_binary(etdump_package, f"{schema_name}.fbs"))
3129

3230

3331
def _serialize_from_etdump_to_json(etdump: ETDumpFlatCC) -> str:

examples/models/checkpoint.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Copyright (c) Meta Platforms, Inc. and affiliates.
22
# All rights reserved.
3+
# Copyright 2025 Arm Limited and/or its affiliates.
34
#
45
# This source code is licensed under the BSD-style license found in the
56
# LICENSE file in the root directory of this source tree.
@@ -16,7 +17,7 @@ def get_default_model_resource_dir(model_file_path: str) -> Path:
1617
"""
1718
Get the default path to resouce files (which contain files such as the
1819
checkpoint and param files), either:
19-
1. Uses the path from pkg_resources, only works with buck2
20+
1. Uses the path from importlib.resources, only works with buck2
2021
2. Uses default path located in examples/models/llama/params
2122
2223
Expected to be called from with a `model.py` file located in a
@@ -33,22 +34,22 @@ def get_default_model_resource_dir(model_file_path: str) -> Path:
3334
"""
3435

3536
try:
36-
import pkg_resources
37+
import importlib.resources as _resources
3738

38-
# 1st way: If we can import this path, we are running with buck2 and all resources can be accessed with pkg_resources.
39+
# 1st way: If we can import this path, we are running with buck2 and all resources can be accessed with importlib.resources.
3940
# pyre-ignore
4041
from executorch.examples.models.llama import params # noqa
4142

4243
# Get the model name from the cwd, assuming that this module is called from a path such as
4344
# examples/models/<model_name>/model.py.
4445
model_name = Path(model_file_path).parent.name
45-
resource_dir = Path(
46-
pkg_resources.resource_filename(
47-
f"executorch.examples.models.{model_name}", "params"
48-
)
49-
)
50-
except:
51-
# 2nd way.
46+
model_dir = _resources.files(f"executorch.examples.models.{model_name}")
47+
with _resources.as_file(model_dir) as model_path:
48+
resource_dir = model_path / "params"
49+
assert resource_dir.exists()
50+
51+
except Exception:
52+
# 2nd way:
5253
resource_dir = Path(model_file_path).absolute().parent / "params"
5354

5455
return resource_dir

examples/models/llama/export_llama_lib.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,29 @@
1717
import shlex
1818
from enum import Enum
1919
from functools import partial
20+
21+
from importlib import resources as _resources
2022
from json import JSONDecodeError
2123
from pathlib import Path
2224
from typing import Callable, List, Optional, Union
2325

24-
import pkg_resources
2526
import torch
2627

2728
from executorch.devtools.backend_debug import print_delegation_info
28-
2929
from executorch.devtools.etrecord import generate_etrecord as generate_etrecord_func
3030
from executorch.examples.models.llama.hf_download import (
3131
download_and_convert_hf_checkpoint,
3232
)
3333
from executorch.exir.passes.init_mutable_pass import InitializedMutableBufferPass
34-
3534
from executorch.extension.llm.export.builder import DType, LLMEdgeManager
36-
3735
from executorch.extension.llm.export.config.llm_config import LlmConfig
38-
3936
from executorch.extension.llm.export.partitioner_lib import (
4037
get_coreml_partitioner,
4138
get_mps_partitioner,
4239
get_qnn_partitioner,
4340
get_vulkan_partitioner,
4441
get_xnnpack_partitioner,
4542
)
46-
4743
from executorch.extension.llm.export.quantizer_lib import (
4844
get_coreml_quantizer,
4945
get_pt2e_quantization_params,
@@ -52,22 +48,19 @@
5248
get_vulkan_quantizer,
5349
)
5450
from executorch.util.activation_memory_profiler import generate_memory_trace
55-
5651
from omegaconf import DictConfig
5752

5853
from ..model_factory import EagerModelFactory
5954
from .source_transformation.apply_spin_quant_r1_r2 import (
6055
fuse_layer_norms,
6156
get_model_with_r1_r2,
6257
)
63-
6458
from .source_transformation.attention import replace_attention_to_attention_sha
6559
from .source_transformation.custom_kv_cache import (
6660
replace_kv_cache_with_custom_kv_cache,
6761
replace_kv_cache_with_quantized_kv_cache,
6862
replace_kv_cache_with_ring_kv_cache,
6963
)
70-
7164
from .source_transformation.quantize import (
7265
get_quant_embedding_transform,
7366
get_quant_weight_transform,
@@ -129,7 +122,7 @@ def set_pkg_name(name: str) -> None:
129122

130123

131124
def get_resource_path(resource_name) -> str:
132-
return pkg_resources.resource_filename(pkg_name, resource_name)
125+
return str(_resources.files(pkg_name).joinpath(resource_name))
133126

134127

135128
def set_verbosity(val):
@@ -575,7 +568,7 @@ def canonical_path(path: Union[str, Path], *, dir: bool = False) -> str:
575568
print("not FBCODE")
576569
return path[4:]
577570
else:
578-
return_val = pkg_resources.resource_filename(pkg_name, path[4:])
571+
return_val = str(_resources.files(pkg_name).joinpath(path[4:]))
579572
if verbose_export():
580573
print(f"canonical name is: {return_val}")
581574
return return_val

exir/dialects/edge/_ops.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
# Copyright (c) Meta Platforms, Inc. and affiliates.
22
# All rights reserved.
3+
# Copyright 2025 Arm Limited and/or its affiliates.
34
#
45
# This source code is licensed under the BSD-style license found in the
56
# LICENSE file in the root directory of this source tree.
67

8+
from importlib import resources
79
from typing import Any, Dict, List, Optional, Set, Union
810

9-
import pkg_resources
11+
import executorch.exir.dialects.edge as edge_package
1012

1113
import torch
1214

@@ -166,9 +168,8 @@ def __getitem__(self, arg_name: str) -> Set[torch.dtype]:
166168
def _load_edge_dialect_info() -> Dict[str, Dict[str, Any]]:
167169
# pyre-ignore
168170
yaml = YAML(typ="safe")
169-
edge_dialect_yaml_info = yaml.load(
170-
pkg_resources.resource_string(__name__, "edge.yaml").decode("utf8")
171-
)
171+
edge_yaml = resources.read_text(edge_package, "edge.yaml", encoding="utf-8")
172+
edge_dialect_yaml_info = yaml.load(edge_yaml)
172173
if edge_dialect_yaml_info:
173174
return {
174175
edge_op_yaml_info["inherits"]: edge_op_yaml_info

0 commit comments

Comments
 (0)