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

Function _emit_cpp_load

backends/mlx/serialization/generate.py:1148–1214  ·  view source on GitHub ↗

Emit C++ load lines for a field kind, or None if kind is unrecognized.

(kind: str, name: str, fb_name: str)

Source from the content-addressed store, hash-verified

1146
1147
1148def _emit_cpp_load(kind: str, name: str, fb_name: str) -> "List[str] | None":
1149 """Emit C++ load lines for a field kind, or None if kind is unrecognized."""
1150 # Required struct / compound via converter
1151 if kind in _CPP_CONVERTER:
1152 conv = _CPP_CONVERTER[kind]
1153 return [f" node.{name} = {conv}(fb->{fb_name}());"]
1154 # Scalars (direct value)
1155 if kind in ("int", "float", "bool"):
1156 return [f" node.{name} = fb->{fb_name}();"]
1157 # Required string
1158 if kind == "str":
1159 return [f' node.{name} = fb->{fb_name}() ? fb->{fb_name}()->str() : "";']
1160 # Optional struct / compound via guarded converter
1161 base_kind = kind.removeprefix("optional_")
1162 if kind.startswith("optional_") and base_kind in _CPP_CONVERTER:
1163 conv = _CPP_CONVERTER[base_kind]
1164 return [
1165 f" if (fb->{fb_name}()) {{",
1166 f" node.{name} = {conv}(fb->{fb_name}());",
1167 " }",
1168 ]
1169 # Optional scalar (FlatBuffers returns flatbuffers::Optional)
1170 if kind in ("optional_float", "optional_int"):
1171 return [
1172 f" auto {fb_name}_opt = fb->{fb_name}();",
1173 f" if ({fb_name}_opt.has_value()) {{",
1174 f" node.{name} = {fb_name}_opt.value();",
1175 " }",
1176 ]
1177 # Optional string
1178 if kind == "optional_str":
1179 return [
1180 f" if (fb->{fb_name}()) {{",
1181 f" node.{name} = fb->{fb_name}()->str();",
1182 " }",
1183 ]
1184 # Integer/bool vector via to_vector
1185 if kind in ("list_int", "list_int8", "list_uint8"):
1186 return [f" node.{name} = to_vector(fb->{fb_name}());"]
1187 # Int-or-vid vector (indexed access)
1188 if kind == "list_int_or_vid":
1189 return [
1190 f" if (fb->{fb_name}()) {{",
1191 f" for (size_t i = 0; i < fb->{fb_name}()->size(); ++i) {{",
1192 f" node.{name}.push_back(convert_int_or_vid(fb->{fb_name}()->Get(static_cast<flatbuffers::uoffset_t>(i))));",
1193 " }",
1194 " }",
1195 ]
1196 # Tid vector (range-based iteration)
1197 if kind == "list_tid":
1198 return [
1199 f" if (fb->{fb_name}()) {{",
1200 f" for (auto fb_tid : *fb->{fb_name}()) {{",
1201 f" node.{name}.push_back(convert_tid(fb_tid));",
1202 " }",
1203 " }",
1204 ]
1205 # String vector

Callers 1

_generate_loader_caseFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected