Skip to content

Commit 247294d

Browse files
committed
PR small fix
Signed-off-by: Chizkiyahu Raful <chizkiyahu.raful@arm.com> Change-Id: Iae1bbafa0f38a7031d2bc805403e4075a523cd5e
1 parent a8027cf commit 247294d

8 files changed

Lines changed: 32 additions & 12 deletions

File tree

.github/workflows/validate_flatbuffer_gen.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ on:
55
pull_request:
66
paths:
77
- "schema/**"
8-
- "executorch_flatbuffer/**"
8+
- "exir/_serialize/generated/executorch_flatbuffer/**"
99

1010
jobs:
1111
exir-flatbuffer:

exir/_serialize/_flatbuffer_program.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
# pyre-strict
77

88
import enum
9+
import functools
910
import importlib
1011
import tempfile
1112

@@ -111,15 +112,12 @@ def _pack_backend_delegate_inline_data(self: Any, builder: Any) -> int:
111112
return _BackendDelegateInlineData.BackendDelegateInlineDataEnd(builder)
112113

113114

115+
@functools.lru_cache(maxsize=1)
114116
def _install_fast_packers() -> None:
115-
global _PACKERS_INSTALLED
116-
if _PACKERS_INSTALLED:
117-
return
118-
_Buffer.BufferT.Pack = _pack_buffer # pyre-ignore[16]
119-
_BackendDelegateInlineData.BackendDelegateInlineDataT.Pack = ( # pyre-ignore[16]
117+
_Buffer.BufferT.Pack = _pack_buffer
118+
_BackendDelegateInlineData.BackendDelegateInlineDataT.Pack = (
120119
_pack_backend_delegate_inline_data
121120
)
122-
_PACKERS_INSTALLED = True
123121

124122

125123
def _set_pack_alignments(tensor_alignment: int, delegate_alignment: int) -> None:

exir/_serialize/generate_program.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ def _run_flatc(repo_root: Path) -> None:
5252

5353

5454
def _rewrite_imports(repo_root: Path) -> int:
55+
"""Rewrite flatc imports to the in-tree package path.
56+
57+
flatc derives the import path from the namespace declaration (executorch_flatbuffer).
58+
Modifying the namespace results in widespread breaking
59+
changes across both the Python and C++ codebases.
60+
so we rewrite the generated files to import
61+
from executorch.exir._serialize.generated.executorch_flatbuffer instead
62+
"""
5563
generated_dir = (
5664
repo_root / "exir" / "_serialize" / "generated" / "executorch_flatbuffer"
5765
)
@@ -106,7 +114,7 @@ def _write_init_files(repo_root: Path) -> int:
106114
top_init = generated_dir / "__init__.py"
107115
top_init_contents = "\n".join(
108116
[
109-
"# automatically generated by exir/_serialize/generate.py, do not modify",
117+
"# automatically generated by exir/_serialize/generate_program.py, do not modify",
110118
"",
111119
"from . import executorch_flatbuffer",
112120
"",
@@ -129,7 +137,7 @@ def _write_init_files(repo_root: Path) -> int:
129137
)
130138
fb_init = generated_fb_dir / "__init__.py"
131139
fb_init_contents = [
132-
"# automatically generated by exir/_serialize/generate.py, do not modify",
140+
"# automatically generated by exir/_serialize/generate_program.py, do not modify",
133141
"",
134142
]
135143
fb_init_contents.extend(f"from . import {name}" for name in module_names)

exir/_serialize/generated/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# automatically generated by exir/_serialize/generate.py, do not modify
1+
# automatically generated by exir/_serialize/generate_program.py, do not modify
22

33
from . import executorch_flatbuffer
44

exir/_serialize/generated/executorch_flatbuffer/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# automatically generated by exir/_serialize/generate.py, do not modify
1+
# automatically generated by exir/_serialize/generate_program.py, do not modify
22

33
from . import AllocationDetails
44
from . import BackendDelegate

exir/_serialize/test/test_flatbuffer.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env fbpython
22
# Copyright (c) Meta Platforms, Inc. and affiliates.
3+
# Copyright 2026 Arm Limited and/or its affiliates.
34
# All rights reserved.
45
#
56
# This source code is licensed under the BSD-style license found in the
@@ -76,6 +77,7 @@ def test_load_patch_and_write(self) -> None:
7677
SCHEMA_FILES: Dict[str, bytes] = {
7778
"program.fbs": b"\n".join(
7879
[
80+
b'file_identifier "ET12";',
7981
b"table Program {",
8082
# Space after the colon.
8183
b" tensor_data: [ubyte] (force_align: 8); // @executorch-tensor-alignment",
@@ -146,6 +148,7 @@ def test_update_tensor_alignment(self) -> None:
146148
read_file(out_dir, "program.fbs"),
147149
b"\n".join(
148150
[
151+
b'file_identifier "ET12";',
149152
b"table Program {",
150153
# Now 128:
151154
b" tensor_data: [ubyte] (force_align: 128); // @executorch-tensor-alignment",
@@ -182,6 +185,7 @@ def test_update_delegate_alignment(self) -> None:
182185
read_file(out_dir, "program.fbs"),
183186
b"\n".join(
184187
[
188+
b'file_identifier "ET12";',
185189
b"table Program {",
186190
b" tensor_data: [ubyte] (force_align: 8); // @executorch-tensor-alignment",
187191
# Now 256:
@@ -221,6 +225,7 @@ def test_update_tensor_and_delegate_alignment(self) -> None:
221225
read_file(out_dir, "program.fbs"),
222226
b"\n".join(
223227
[
228+
b'file_identifier "ET12";',
224229
b"table Program {",
225230
# Now 1:
226231
b" tensor_data: [ubyte] (force_align: 1); // @executorch-tensor-alignment",

schema/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ The `scalar_type.fbs` file contains schema for scalar types.
77
## Regenerating generated code
88

99
Schema changes require regenerating the Python bindings in
10-
`executorch_flatbuffer` and committing the updated files. From the repo root:
10+
`exir/_serialize/generated/executorch_flatbuffer` and committing the updated files. From the repo root:
1111

1212
```sh
1313
python exir/_serialize/generate_program.py

schema/program.fbs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
// Copyright 2026 Arm Limited and/or its affiliates.
23

34
//
45
// See README.md before modifying this file.
@@ -389,6 +390,10 @@ table ExecutionPlan {
389390
}
390391

391392
// Constant tensor data stored directly in the flatbuffer.
393+
// NOTE: If you change Buffer or BackendDelegateInlineData fields, update the
394+
// custom packers in exir/_serialize/_flatbuffer_program.py
395+
// (_pack_buffer and _pack_backend_delegate_inline_data);
396+
// they override the flatc-generated packers.
392397
table Buffer {
393398
// During serialization, this alignment may be rewritten to a larger value.
394399
// The magic "@executorch-tensor-alignment" comment tells EXIR which lines to
@@ -399,6 +404,10 @@ table Buffer {
399404
// Delegate data stored directly in the flatbuffer. This is a different type
400405
// than Buffer because tensors and delegates can have different alignment
401406
// requirements.
407+
// NOTE: If you change Buffer or BackendDelegateInlineData fields, update the
408+
// custom packers in exir/_serialize/_flatbuffer_program.py
409+
// (_pack_buffer and _pack_backend_delegate_inline_data);
410+
// they override the flatc-generated packers.
402411
table BackendDelegateInlineData {
403412
// During serialization, this alignment may be rewritten to a larger value.
404413
// The magic "@executorch-delegate-alignment" comment tells EXIR which lines

0 commit comments

Comments
 (0)