Skip to content

Commit 34cd1de

Browse files
authored
[executorch][native] Let the delegate hand constants back for .ptn
Differential Revision: D116231150 Pull Request resolved: pytorch#22008
1 parent 8ac4894 commit 34cd1de

6 files changed

Lines changed: 342 additions & 12 deletions

File tree

‎backends/native/partitioner.py‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@
4545
}
4646

4747
EXTERNAL_CONSTANTS_TAG_KEY = "external_constants_tag"
48+
# Opt preprocess into handing constants back on NativeDelegateInfo instead of
49+
# copying them into a NamedDataStore. Set by to_native; a program lowered this way
50+
# has no constants to serialize into a PTE, so it must not reach to_executorch.
51+
PTN_SERIALIZATION_KEY = "serialize_as_ptn"
4852

4953

5054
class NativeSupportedOperators(OperatorSupportBase):
@@ -120,10 +124,30 @@ def _submodule_fully_supported(
120124
@final
121125
class NativePartitioner(Partitioner):
122126
def __init__(
123-
self, external_constants_tag: Optional[str] = "native_weights"
127+
self,
128+
external_constants_tag: Optional[str] = "native_weights",
129+
*,
130+
_serialize_as_ptn: bool = False,
124131
) -> None:
132+
"""Partition for the native backend.
133+
134+
Args:
135+
external_constants_tag: NamedDataStore tag for constant data, or None
136+
to embed it in the PTE.
137+
_serialize_as_ptn: Private, for ``to_native`` only. A program lowered
138+
with this set has no constants to serialize into a PTE, so
139+
``to_executorch`` would emit an incomplete one. Public callers get
140+
the ordinary ExecuTorch delegate path.
141+
"""
125142
from executorch.backends.native.preprocess import NativeBackend
126143

144+
if _serialize_as_ptn and external_constants_tag is not None:
145+
raise ValueError(
146+
"NativePartitioner: external_constants_tag only applies to the "
147+
"NamedDataStore path; pass external_constants_tag=None alongside "
148+
"_serialize_as_ptn=True."
149+
)
150+
127151
compile_specs: List[CompileSpec] = []
128152
if external_constants_tag is not None:
129153
compile_specs.append(
@@ -132,6 +156,8 @@ def __init__(
132156
external_constants_tag.encode("utf-8"),
133157
)
134158
)
159+
if _serialize_as_ptn:
160+
compile_specs.append(CompileSpec(PTN_SERIALIZATION_KEY, b"1"))
135161
self.delegation_spec = DelegationSpec(NativeBackend.__name__, compile_specs)
136162

137163
def ops_to_not_decompose(

‎backends/native/preprocess.py‎

Lines changed: 89 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,29 @@
1212
Constant tensor data is shipped via the NamedDataStore, referenced by
1313
fully-qualified name -- never embedded in the graph flatbuffer.
1414
15+
When the partitioner sets PTN_SERIALIZATION_KEY, the constants are handed back on
16+
NativeDelegateInfo instead of being copied into a NamedDataStore, and to_native
17+
packs them once, globally deduped, across every method. processed_bytes is the
18+
same graph flatbuffer either way; only the constant channel differs.
19+
20+
Such a program must be consumed by to_native. _delegate_info_meta is never
21+
serialized into a PTE, so lowering in this mode and then calling to_executorch
22+
yields a program whose constants are simply absent -- see to_native.
23+
1524
Graph cleanup (CSE, reinplace, view_copy collapsing) is expected to run before
1625
lowering via the ``transform_passes`` argument of
1726
``to_edge_transform_and_lower`` (see ``passes.get_default_passes``).
1827
"""
1928

20-
from typing import final, List
29+
from dataclasses import dataclass, field
30+
from typing import Dict, final, List, Optional
31+
32+
import torch
33+
34+
from executorch.backends.native.partitioner import (
35+
EXTERNAL_CONSTANTS_TAG_KEY,
36+
PTN_SERIALIZATION_KEY,
37+
)
2138

2239
from executorch.backends.native.serialization import serialize_graph
2340

@@ -33,6 +50,56 @@
3350
from torch._subclasses.fake_tensor import FakeTensor
3451

3552

53+
@dataclass(frozen=True)
54+
class NativeDelegateInfo:
55+
"""What preprocess hands to to_native alongside the graph blob.
56+
57+
Carried on ``lowered_module.meta["_delegate_info_meta"]``, which ExecuTorch
58+
never serializes into a PTE. Tensors are passed by reference rather than
59+
copied, so to_native must consume them before anything mutates the edge
60+
program; the sanctioned sequence lowers and packs with nothing in between.
61+
62+
A container rather than a bare dict so later additions do not have to fight
63+
over the single _delegate_info_meta slot.
64+
"""
65+
66+
constants: Dict[str, torch.Tensor] = field(default_factory=dict)
67+
68+
69+
def _parse_compile_specs(
70+
module_compile_spec: List[CompileSpec],
71+
) -> tuple[Optional[str], bool]:
72+
"""Parse the native specs, rejecting ambiguous recognized configuration."""
73+
external_tag = None
74+
serialize_as_ptn = False
75+
seen: set[str] = set()
76+
for spec in module_compile_spec:
77+
if spec.key not in (EXTERNAL_CONSTANTS_TAG_KEY, PTN_SERIALIZATION_KEY):
78+
continue
79+
if spec.key in seen:
80+
raise ValueError(
81+
f"NativeBackend: duplicate compile spec {spec.key!r} is ambiguous."
82+
)
83+
seen.add(spec.key)
84+
85+
value = bytes(spec.value)
86+
if spec.key == EXTERNAL_CONSTANTS_TAG_KEY:
87+
external_tag = value.decode("utf-8")
88+
else:
89+
if value != b"1":
90+
raise ValueError(
91+
f"NativeBackend: {PTN_SERIALIZATION_KEY!r} must have value b'1'."
92+
)
93+
serialize_as_ptn = True
94+
95+
if serialize_as_ptn and external_tag is not None:
96+
raise ValueError(
97+
"NativeBackend: PTN serialization cannot be combined with "
98+
"external_constants_tag; the two modes use different constant channels."
99+
)
100+
return external_tag, serialize_as_ptn
101+
102+
36103
@final
37104
class NativeBackend(BackendDetails):
38105
@staticmethod
@@ -47,18 +114,31 @@ def preprocess(
47114
edge_program.constants,
48115
)
49116

50-
external_tag = None
51-
for spec in module_compile_spec:
52-
if spec.key == "external_constants_tag":
53-
external_tag = bytes(spec.value).decode("utf-8")
117+
external_tag, serialize_as_ptn = _parse_compile_specs(module_compile_spec)
118+
119+
constants = {
120+
fqn: tensor.detach()
121+
for fqn, tensor in constant_data.items()
122+
if not isinstance(tensor, FakeTensor)
123+
}
124+
125+
if serialize_as_ptn:
126+
# No dedup here: nothing is serialized yet, so hashing would cost a
127+
# full pass over every weight and free nothing (the tensors are owned
128+
# by the edge program). Preserve source layout/storage as well, so the
129+
# PTN writer can reject mutable alias topology before normalization.
130+
# to_native dedups once, across all methods.
131+
return PreprocessResult(
132+
processed_bytes=flatbuffer_bytes,
133+
data_store_output=None,
134+
_delegate_info_meta=NativeDelegateInfo(constants=constants),
135+
)
54136

55137
named_data_store = NamedDataStore()
56-
for fqn, tensor in constant_data.items():
57-
if isinstance(tensor, FakeTensor):
58-
continue
138+
for fqn, tensor in constants.items():
59139
named_data_store.add_named_data(
60140
fqn,
61-
tensor.detach().contiguous(),
141+
tensor.contiguous(),
62142
external_tag=external_tag,
63143
)
64144

‎backends/native/serialization/graph_serialize.py‎

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,21 @@
2727
import tempfile
2828
from dataclasses import fields, is_dataclass
2929
from enum import IntEnum
30-
from typing import Any, cast, get_args, get_origin, get_type_hints, Union
30+
from typing import (
31+
Any,
32+
cast,
33+
get_args,
34+
get_origin,
35+
get_type_hints,
36+
Iterator,
37+
Optional,
38+
Union,
39+
)
3140

3241
import torch
3342

3443
from executorch.backends.native.serialization.schema import (
44+
AffineGroup,
3545
Argument,
3646
ArgumentValue,
3747
BoolArg,
@@ -1166,6 +1176,81 @@ def deserialize_graph(data: bytes) -> Graph:
11661176
return program.methods[0].graph
11671177

11681178

1179+
def merge_programs(method_blobs: dict[str, bytes]) -> bytes:
1180+
"""Combine per-method native Program blobs into one multi-method Program.
1181+
1182+
Each input blob (e.g. a delegate's processed_bytes) carries a single method;
1183+
it is renamed to its key so the merged Program exposes every method by name.
1184+
1185+
TODO: revisit this, and if we need to create a full PTG in the preprocess.
1186+
"""
1187+
methods: list[Method] = []
1188+
for name, blob in method_blobs.items():
1189+
program = deserialize_program(blob)
1190+
if len(program.methods) != 1:
1191+
raise ValueError(
1192+
f"merge_programs: blob for {name!r} has {len(program.methods)} "
1193+
f"methods; expected exactly one."
1194+
)
1195+
method = program.methods[0]
1196+
method.name = name
1197+
methods.append(method)
1198+
return _compile_to_bytes(Program(version=SCHEMA_VERSION, methods=methods))
1199+
1200+
1201+
def _iter_graph_args(value: object) -> Iterator[GraphArg]:
1202+
"""Yield GraphArgs from any nested schema field without entering their graphs."""
1203+
if isinstance(value, GraphArg):
1204+
yield value
1205+
return
1206+
if is_dataclass(value):
1207+
for field in fields(value):
1208+
yield from _iter_graph_args(getattr(value, field.name))
1209+
return
1210+
if isinstance(value, (list, tuple)):
1211+
for item in value:
1212+
yield from _iter_graph_args(item)
1213+
elif isinstance(value, dict):
1214+
for item in value.values():
1215+
yield from _iter_graph_args(item)
1216+
1217+
1218+
def collect_data_keys(program: Program) -> set[str]:
1219+
"""Return every out-of-line data key the program references.
1220+
1221+
Covers constant references (NamedTensorRef.data_key) plus the quant keys
1222+
(AffineGroup scale and zero-point) carried on any TensorMeta.quant, whether
1223+
it is attached to a constant or to a graph value (intermediates and I/O),
1224+
recursing into HOP subgraphs wherever a schema field carries a ``GraphArg``.
1225+
PackedQuant carries no external keys.
1226+
"""
1227+
keys: set[str] = set()
1228+
1229+
def add_meta(meta: Optional[TensorMeta]) -> None:
1230+
if meta is None or meta.quant is None:
1231+
return
1232+
scheme = meta.quant.scheme
1233+
if isinstance(scheme, AffineGroup):
1234+
keys.add(scheme.scale_data_key)
1235+
if scheme.zero_point_data_key:
1236+
keys.add(scheme.zero_point_data_key)
1237+
1238+
def visit_graph(graph: Graph) -> None:
1239+
for value in graph.tensor_values or []:
1240+
add_meta(value.meta)
1241+
for node in graph.nodes:
1242+
for graph_arg in _iter_graph_args(node):
1243+
visit_graph(graph_arg.graph)
1244+
1245+
for method in program.methods:
1246+
for constant in method.constants or []:
1247+
keys.add(constant.data_key)
1248+
add_meta(constant.meta)
1249+
visit_graph(method.graph)
1250+
1251+
return keys
1252+
1253+
11691254
def _check_optional_tensor_list_refs(
11701255
value: OptionalTensorListArg, ctx: str, check_ref: Any
11711256
) -> None:

‎backends/native/test/test_partitioner.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
from executorch.backends.native import get_default_compile_config
1414
from executorch.backends.native.partitioner import (
1515
_SUPPORTED_NON_CORE_OPS,
16+
EXTERNAL_CONSTANTS_TAG_KEY,
1617
NativePartitioner,
1718
NativeSupportedOperators,
19+
PTN_SERIALIZATION_KEY,
1820
)
1921
from executorch.backends.native.passes import get_default_passes
2022
from executorch.backends.native.serialization import deserialize_graph
@@ -200,3 +202,29 @@ def test_cond_delegated_as_single_native_program(self):
200202
self.assertTrue(
201203
any(n.target and n.target.endswith("cond") for n in graph.nodes)
202204
)
205+
206+
207+
class NativePartitionerCompileSpecTest(unittest.TestCase):
208+
def _keys(self, partitioner: NativePartitioner) -> list[str]:
209+
return [s.key for s in partitioner.delegation_spec.compile_specs]
210+
211+
def test_default_selects_the_named_data_store_path(self):
212+
partitioner = NativePartitioner()
213+
self.assertEqual(self._keys(partitioner), [EXTERNAL_CONSTANTS_TAG_KEY])
214+
value = partitioner.delegation_spec.compile_specs[0].value
215+
self.assertEqual(bytes(value).decode("utf-8"), "native_weights")
216+
217+
def test_ptn_mode_selects_only_ptn_serialization(self):
218+
partitioner = NativePartitioner(
219+
external_constants_tag=None, _serialize_as_ptn=True
220+
)
221+
self.assertEqual(self._keys(partitioner), [PTN_SERIALIZATION_KEY])
222+
223+
def test_no_specs_when_neither_is_requested(self):
224+
self.assertEqual(self._keys(NativePartitioner(external_constants_tag=None)), [])
225+
226+
def test_ptn_mode_with_external_constants_tag_raises(self):
227+
# The tag only means anything to the NamedDataStore path, so requesting
228+
# both would silently ignore it.
229+
with self.assertRaisesRegex(ValueError, "only applies to the "):
230+
NativePartitioner(_serialize_as_ptn=True)

0 commit comments

Comments
 (0)