Skip to content

Commit 38d7ad0

Browse files
authored
Pool immutable integer constants during EXIR emission (pytorch#22915)
### Summary Repeated literal operator arguments allocate separate EValue slots, and integer lists also duplicate persistent boxed-list storage. Pool read-only integer literals and lists made entirely from literal integers within each execution plan, using the argument schema to exclude out and aliased values. Preserve existing abstract values, method inputs and outputs, symbolic values, and opaque delegate arguments. Construct references with their final pooled IDs during emission so no serialization-time remapping is needed. Add tests for type distinctions, signed 64-bit limits, list references, mutation and aliasing boundaries, plan scope, and repeated control flow. Update the keyword-argument test to follow instruction argument IDs. ### Test plan Validation: 78 emitter tests and 14 subtests passed, with two existing skips. All four trained MLPerf Tiny models produced exact before/after outputs on 67,029 Cortex-M55 FVP validation inputs. Operator calls and planned memory were unchanged, and independent re-exports were byte-identical. DS-CNN persistent method metadata decreased from 8,008 to 5,056 measured bytes. Authored with OpenAI Codex.
1 parent 3ffd297 commit 38d7ad0

4 files changed

Lines changed: 331 additions & 10 deletions

File tree

‎exir/emit/_emitter.py‎

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ class _EmitterState:
150150
emit_mutable_buffer_names: bool
151151

152152
spec2id_dict: Dict[TensorSpec, int] = field(default_factory=dict)
153+
# Only literal arguments with no schema aliasing enter this per-plan pool.
154+
constant_cache: Dict[Union[int, Tuple[int, ...]], int] = field(default_factory=dict)
153155

154156
def spec2id(self, spec: TensorSpec) -> int:
155157
"""Map a TensorSpec to value index in the values array."""
@@ -270,7 +272,9 @@ def _internal_assert_emitter(
270272
if not pred:
271273
raise InternalError(self._emit_node_specific_error(node, assert_msg))
272274

273-
def _emit_int_list(self, val: List[_Argument]) -> EValue:
275+
def _emit_int_list(
276+
self, val: List[_Argument], *, immutable: bool = False
277+
) -> EValue:
274278
"""Emits a list of integers as a collection of EValues.
275279
276280
For every argument in 'val':
@@ -286,7 +290,7 @@ def _emit_int_list(self, val: List[_Argument]) -> EValue:
286290
boxed_list.append(item.id)
287291
elif isinstance(item, int):
288292
boxed_list.append(
289-
self._emit_evalue(self._constant_to_evalue(item, None)).id
293+
self._emit_argument(item, None, immutable=immutable).id
290294
)
291295
else:
292296
self._internal_assert_emitter(
@@ -295,7 +299,13 @@ def _emit_int_list(self, val: List[_Argument]) -> EValue:
295299

296300
return EValue(IntList(boxed_list))
297301

298-
def _emit_list(self, val: List[_Argument], val_type: _SchemaType) -> EValue:
302+
def _emit_list(
303+
self,
304+
val: List[_Argument],
305+
val_type: _SchemaType,
306+
*,
307+
immutable: bool = False,
308+
) -> EValue:
299309
"""Emits a list type.
300310
301311
Emits the list stored in val. If the list is of Tensors, Optionals, or Ints the emitted list
@@ -308,7 +318,7 @@ def _emit_list(self, val: List[_Argument], val_type: _SchemaType) -> EValue:
308318
return EValue(BoolList(typing.cast(List[bool], val)))
309319

310320
if isinstance(val_type, torch.IntType):
311-
return self._emit_int_list(val)
321+
return self._emit_int_list(val, immutable=immutable)
312322

313323
if isinstance(val_type, torch.FloatType):
314324
return EValue(DoubleList(typing.cast(List[float], val)))
@@ -541,6 +551,8 @@ def _constant_to_evalue( # noqa: C901
541551
self,
542552
val: _Argument,
543553
val_type: Optional[_SchemaType],
554+
*,
555+
immutable: bool = False,
544556
) -> EValue:
545557
"""Converts a constant value to an EValue.
546558
@@ -564,6 +576,7 @@ def _constant_to_evalue( # noqa: C901
564576
return self._emit_list(
565577
typing.cast(List[_Argument], val),
566578
typing.cast(_SchemaType, val_type.getElementType()),
579+
immutable=immutable,
567580
)
568581

569582
if isinstance(val, float):
@@ -1353,13 +1366,37 @@ def _add_delegate_map(
13531366
}
13541367

13551368
def _emit_argument(
1356-
self, arg: _Argument, arg_type: Optional[_SchemaType]
1369+
self,
1370+
arg: _Argument,
1371+
arg_type: Optional[_SchemaType],
1372+
*,
1373+
immutable: bool = False,
13571374
) -> _AbstractValue:
13581375
"""Emit an argument to an operator or delegate if it had not already been emitted otherwise
13591376
return the previously emitted location"""
13601377
if isinstance(arg, _AbstractValue):
13611378
return arg
1362-
return self._emit_evalue(self._constant_to_evalue(arg, arg_type))
1379+
value = self._constant_to_evalue(arg, arg_type, immutable=immutable)
1380+
key: Optional[Union[int, Tuple[int, ...]]] = None
1381+
if immutable:
1382+
if isinstance(value.val, Int):
1383+
key = value.val.int_val
1384+
elif (
1385+
isinstance(value.val, IntList)
1386+
and isinstance(arg, (list, tuple))
1387+
and all(type(item) is int for item in arg)
1388+
):
1389+
# Boxed lists can reference mutable SymInts. Only literal lists
1390+
# may share their unboxed buffer; their elements are already pooled.
1391+
key = tuple(value.val.items)
1392+
if key is not None:
1393+
index = self.emitter_state.constant_cache.get(key)
1394+
if index is not None:
1395+
return _AbstractValue(index, None)
1396+
result = self._emit_evalue(value)
1397+
if key is not None:
1398+
self.emitter_state.constant_cache[key] = result.id
1399+
return result
13631400

13641401
def _get_sym_ret(
13651402
self,
@@ -1537,7 +1574,13 @@ def _get_empty_tensor_evalue() -> EValue:
15371574
if kernel_arg is None and isinstance(schema_arg.type, torch.TensorType):
15381575
kernel_arg = self._emit_evalue(_get_empty_tensor_evalue())
15391576

1540-
kernel_args.append(self._emit_argument(kernel_arg, schema_arg.type).id)
1577+
kernel_args.append(
1578+
self._emit_argument(
1579+
kernel_arg,
1580+
schema_arg.type,
1581+
immutable=not schema_arg.is_out and schema_arg.alias_info is None,
1582+
).id
1583+
)
15411584

15421585
if schema_arg.is_out:
15431586
out_args.append((schema_arg.name, kernel_arg))

‎exir/emit/test/BUCK‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ fbcode_target(_kind = runtime.python_test,
1515
name = "emit",
1616
srcs = [
1717
"test_emit.py",
18+
"test_emit_constants.py",
1819
],
1920
deps = [
2021
"fbsource//third-party/pypi/pytest:pytest",
@@ -24,6 +25,7 @@ fbcode_target(_kind = runtime.python_test,
2425
"//executorch/exir:lib",
2526
"//executorch/exir:print_program",
2627
"//executorch/exir:schema",
28+
"//executorch/exir:tensor",
2729
"//executorch/exir/backend/test/demos/rpc:executor_backend_partitioner",
2830
"//executorch/exir/backend:backend_api",
2931
"//executorch/exir/backend:compile_spec_schema",

‎exir/emit/test/test_emit.py‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -427,9 +427,11 @@ def forward(self, x: torch.Tensor) -> torch.Tensor:
427427
.to_executorch()
428428
.executorch_program
429429
)
430-
# The value for beta should appear before alpha
431-
self.assertEqual(program.execution_plan[0].values[12].val, Int(3))
432-
self.assertEqual(program.execution_plan[0].values[13].val, Int(2))
430+
plan = program.execution_plan[0]
431+
call = plan.chains[0].instructions[-1].instr_args
432+
self.assertEqual(plan.operators[call.op_index].name, "aten::addbmm")
433+
self.assertEqual(plan.values[call.args[3]].val, Int(3))
434+
self.assertEqual(plan.values[call.args[4]].val, Int(2))
433435

434436
def test_kwargs2(self) -> None:
435437
"""Tests that the kwargs are placed in the order specified by

0 commit comments

Comments
 (0)