(executorch_program, eager_reference, example_inputs, precision)
| 167 | |
| 168 | |
| 169 | def run_with_pybindings(executorch_program, eager_reference, example_inputs, precision): |
| 170 | if not _CAN_RUN_WITH_PYBINDINGS: |
| 171 | raise RuntimeError("Cannot run with pybindings on this platform.") |
| 172 | |
| 173 | dtype = { |
| 174 | "float32": torch.float32, |
| 175 | "float16": torch.float16, |
| 176 | }[precision] |
| 177 | |
| 178 | runtime = Runtime.get() |
| 179 | program = runtime.load_program(executorch_program.buffer) |
| 180 | method = program.load_method("forward") |
| 181 | et_outputs = method.execute(*example_inputs)[0] |
| 182 | eager_outputs = eager_reference(*example_inputs) |
| 183 | if isinstance(eager_outputs, collections.OrderedDict): |
| 184 | eager_outputs = eager_outputs["out"] |
| 185 | if isinstance(eager_outputs, list | tuple): |
| 186 | eager_outputs = eager_outputs[0] |
| 187 | |
| 188 | mse = ((et_outputs - eager_outputs) ** 2).mean().sqrt() |
| 189 | print(f"Mean square error: {mse}") |
| 190 | assert mse < 0.1, "Mean square error is too high." |
| 191 | |
| 192 | if dtype == torch.float32: |
| 193 | assert torch.allclose( |
| 194 | et_outputs, eager_outputs, atol=1e-02, rtol=1e-02 |
| 195 | ), f"""Outputs do not match eager reference: |
| 196 | \tet_outputs (first 5)={et_outputs.reshape(-1)[0:5]} |
| 197 | \teager_outputs (first 5)={eager_outputs.reshape(-1)[0:5]}""" |
| 198 | |
| 199 | |
| 200 | def main(): |
no test coverage detected