MCPcopy Create free account
hub / github.com/pytorch/executorch / generate_python_schema

Function generate_python_schema

backends/mlx/serialization/generate.py:404–514  ·  view source on GitHub ↗

Generate mlx_graph_schema.py from parsed FBS.

(schema: FBSSchema)

Source from the content-addressed store, hash-verified

402
403
404def generate_python_schema(schema: FBSSchema) -> str: # noqa: C901
405 """Generate mlx_graph_schema.py from parsed FBS."""
406 lines = _file_header("#")
407 lines.extend(
408 [
409 "",
410 "from __future__ import annotations",
411 "",
412 "from dataclasses import dataclass, field",
413 "from enum import IntEnum",
414 "from typing import List, Optional, Union",
415 "",
416 "",
417 *_section_header("#", "Enums"),
418 ]
419 )
420
421 # Generate enums
422 for enum in schema.enums:
423 lines.append(f"class {enum.name}(IntEnum):")
424 val = 0
425 for name, explicit_val in enum.values:
426 if explicit_val is not None:
427 val = explicit_val
428 lines.append(f" {name} = {val}")
429 val += 1
430 lines.append("")
431 lines.append("")
432
433 lines.extend(_section_header("#", "Core types"))
434
435 # Generate structs (Tid, Vid)
436 for struct in schema.structs:
437 lines.append("@dataclass")
438 lines.append(f"class {struct.name}:")
439 for fld in struct.fields:
440 py_type = _fbs_type_to_python(fld.type_str, fld.required)
441 default = _fbs_default_to_python(fld.default, fld.type_str)
442 if default:
443 lines.append(f" {fld.name}: {py_type} = {default}")
444 else:
445 lines.append(f" {fld.name}: {py_type}")
446 lines.append("")
447 lines.append("")
448
449 # Generate compound types (IntOrVid, FloatOrVid, TidOrVid) from schema
450 for type_name in sorted(FBS_COMPOUND_TYPES):
451 table = next((t for t in schema.tables if t.name == type_name), None)
452 if table:
453 lines.extend(_generate_compound_type(table))
454 lines.append("")
455
456 # Generate ShapeDim, SlotVariant, NamedSlot, TensorMeta (but not Instruction/MLXGraph yet - they reference OpNode)
457 other_tables = ["ShapeDim", "SlotVariant", "NamedSlot", "TensorMeta"]
458 for table_name in other_tables:
459 table = next((t for t in schema.tables if t.name == table_name), None)
460 if table:
461 lines.extend(_generate_dataclass(table))

Callers

nothing calls this directly

Calls 8

_file_headerFunction · 0.85
_section_headerFunction · 0.85
_fbs_type_to_pythonFunction · 0.85
_fbs_default_to_pythonFunction · 0.85
_generate_compound_typeFunction · 0.85
_generate_dataclassFunction · 0.85
get_op_nodesMethod · 0.80
appendMethod · 0.45

Tested by

no test coverage detected