Modifies annotated "force_align" values in a flatbuffer schema. Args: schema: The flatbuffer schema to modify. constant_tensor_alignment: If provided, the alignment to use for lines annotated with "@executorch-tensor-alignment". If not provided, does not patch
(
schema: bytes,
constant_tensor_alignment: Optional[int],
delegate_alignment: Optional[int],
)
| 34 | |
| 35 | |
| 36 | def _patch_schema_alignment( |
| 37 | schema: bytes, |
| 38 | constant_tensor_alignment: Optional[int], |
| 39 | delegate_alignment: Optional[int], |
| 40 | ) -> bytes: |
| 41 | """Modifies annotated "force_align" values in a flatbuffer schema. |
| 42 | |
| 43 | Args: |
| 44 | schema: The flatbuffer schema to modify. |
| 45 | constant_tensor_alignment: If provided, the alignment to use for lines annotated |
| 46 | with "@executorch-tensor-alignment". If not provided, does not patch |
| 47 | tensor alignment. |
| 48 | delegate_alignment: If provided, the alignment to use for lines |
| 49 | annotated with "@executorch-delegate-alignment". If not provided, |
| 50 | does not patch delegate alignment. |
| 51 | |
| 52 | Returns: |
| 53 | The possibly-modified flatbuffer schema. |
| 54 | """ |
| 55 | |
| 56 | def assert_valid_alignment(alignment: Optional[int], name: str) -> None: |
| 57 | if not (alignment is None or _is_valid_alignment(alignment)): |
| 58 | raise ValueError(f"Bad {name} {alignment}") |
| 59 | |
| 60 | assert_valid_alignment(constant_tensor_alignment, "constant_tensor_alignment") |
| 61 | assert_valid_alignment(delegate_alignment, "delegate_alignment") |
| 62 | |
| 63 | def patch_alignment(line: bytes, alignment: int) -> bytes: |
| 64 | """Replaces an existing alignment with a new alignment.""" |
| 65 | return re.sub( |
| 66 | rb"\(\s*force_align\s*:\s*\d+\s*\)", |
| 67 | f"(force_align: {alignment})".encode("utf-8"), |
| 68 | line, |
| 69 | ) |
| 70 | |
| 71 | lines = [] |
| 72 | for line in schema.splitlines(): |
| 73 | if constant_tensor_alignment and b"@executorch-tensor-alignment" in line: |
| 74 | lines.append(patch_alignment(line, constant_tensor_alignment)) |
| 75 | elif delegate_alignment and b"@executorch-delegate-alignment" in line: |
| 76 | lines.append(patch_alignment(line, delegate_alignment)) |
| 77 | else: |
| 78 | lines.append(line) |
| 79 | return b"\n".join(lines) |
| 80 | |
| 81 | |
| 82 | class _SchemaMaxAlignmentGetter: |
no test coverage detected