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

Function _run_python_program

backends/nxp/tests/nsys_testing.py:298–368  ·  view source on GitHub ↗

Run a model in Python with channels first (contiguous) inputs. :param model: Any PyTorch/ExecuTorch model runnable directly in python with channels first (contiguous) inputs. :param testing_dataset_dir: Directory containing testing data. The samples can be channels last (NHWC) or channels

(
    model: torch.nn.Module | GraphModule,
    testing_dataset_dir,
    input_spec: list[ModelInputSpec],
    output_spec: list[torch.Tensor],
    cpu_results_dir,
    npu_results_dir,
)

Source from the content-addressed store, hash-verified

296
297
298def _run_python_program(
299 model: torch.nn.Module | GraphModule,
300 testing_dataset_dir,
301 input_spec: list[ModelInputSpec],
302 output_spec: list[torch.Tensor],
303 cpu_results_dir,
304 npu_results_dir,
305):
306 """Run a model in Python with channels first (contiguous) inputs.
307
308 :param model: Any PyTorch/ExecuTorch model runnable directly in python with channels first (contiguous) inputs.
309 :param testing_dataset_dir: Directory containing testing data. The samples can be channels last (NHWC) or channels
310 first (NCHW). The format must match the input_spec.dim_order. The data will be
311 converted to channels first if needed.
312 :param input_spec: List of ModelInputSpec defining the shape, type, and dimension order of each input.
313 :param output_spec: List of output tensor specifications.
314 :param cpu_results_dir: Directory where CPU results will be stored. The structure will match the existing structure
315 of `npu_results_dir`.
316 :param npu_results_dir: Directory where NPU results are already stored, to serve as reference directory structure
317 for `cpu_results_dir`.
318 """
319 all_outputs = []
320
321 for input_samples in read_prepared_samples(testing_dataset_dir, input_spec):
322 current_input_samples = []
323 for spec, sample in zip(input_spec, input_samples, strict=True):
324 match spec.dim_order:
325 case torch.contiguous_format:
326 # Use the data as is, just turn it into a PyTorch tensor.
327 sample = torch.tensor(sample)
328
329 case torch.channels_last:
330 # The tensor data was stored by the DatasetCreator as channels last (NHWC), but it was now
331 # incorrectly parsed as contiguous/channels first (NCHW). Transpose it to channels last to preserve
332 # the semantics.
333 channels_last_shape = translator.dims_to_channels_last(
334 list(spec.shape)
335 )
336 sample = np.moveaxis(sample.reshape(channels_last_shape), -1, 1)
337 sample = torch.tensor(sample).to(memory_format=torch.channels_last)
338
339 case _:
340 raise ValueError(f"Unsupported dim_order: {spec.dim_order}")
341
342 current_input_samples.append(sample)
343
344 # Run the model.
345 output = model(*current_input_samples)
346 if isinstance(output, torch.Tensor):
347 output = (output,)
348
349 current_outputs = []
350
351 for o, o_spec in zip(output, output_spec, strict=True):
352 dim_order = list(o_spec.dim_order()) # ExecuTorch dim order.
353 rank = len(o_spec.shape)
354 if dim_order == list(range(rank)): # Contiguous dim order.
355 current_outputs.append(o.detach().numpy())

Callers 1

lower_run_compareFunction · 0.85

Calls 5

read_prepared_samplesFunction · 0.85
store_resultsFunction · 0.85
toMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected