An ExecuTorch program, loaded from binary PTE data. This can be used to load the methods/models defined by the program.
| 160 | |
| 161 | |
| 162 | class Program: |
| 163 | """An ExecuTorch program, loaded from binary PTE data. |
| 164 | |
| 165 | This can be used to load the methods/models defined by the program. |
| 166 | """ |
| 167 | |
| 168 | def __init__(self, program: ExecuTorchProgram, data: Optional[bytes]) -> None: |
| 169 | # Hold the data so the program is not freed. |
| 170 | self._data = data |
| 171 | self._program = program |
| 172 | self._methods: Dict[str, Optional[Method]] = {} |
| 173 | # The names of the methods are preemptively added to the dictionary, |
| 174 | # but only map to None until they are loaded. |
| 175 | for method_idx in range(self._program.num_methods()): |
| 176 | self._methods[self._program.get_method_name(method_idx)] = None |
| 177 | |
| 178 | @property |
| 179 | def method_names(self) -> Set[str]: |
| 180 | """Returns method names of the Program as a set of strings.""" |
| 181 | return set(self._methods.keys()) |
| 182 | |
| 183 | def load_method(self, name: str) -> Optional[Method]: |
| 184 | """Loads a method from the program. |
| 185 | |
| 186 | Args: |
| 187 | name: The name of the method to load. |
| 188 | |
| 189 | Returns: |
| 190 | The loaded method. |
| 191 | """ |
| 192 | |
| 193 | method = self._methods[name] |
| 194 | if method is None: |
| 195 | method = Method(self._program.load_method(name)) |
| 196 | self._methods[name] = method |
| 197 | return method |
| 198 | |
| 199 | def metadata(self, method_name: str) -> MethodMeta: |
| 200 | """Gets the metadata for the specified method without loading it. |
| 201 | |
| 202 | Args: |
| 203 | method_name: The name of the method. |
| 204 | |
| 205 | Returns: |
| 206 | The metadata for the method, including input/output specifications. |
| 207 | """ |
| 208 | return self._program.method_meta(method_name) |
| 209 | |
| 210 | def write_etdump_result_to_file( |
| 211 | self, etdump_path: str, debug_buffer_path: str |
| 212 | ) -> None: |
| 213 | """Writes the etdump and debug result to a file. |
| 214 | |
| 215 | Args: |
| 216 | etdump_path: The path to the etdump file. |
| 217 | debug_buffer_path: The path to the debug buffer file. |
| 218 | """ |
| 219 | self._program.write_etdump_result_to_file(etdump_path, debug_buffer_path) |