MIGraphX Field Guide
Languages

Python API

Use MIGraphX from Python for smoke tests, model conversion checks, and lightweight inference scripts.

The Python API is useful for integration tests, model validation, benchmarking harnesses, and quick experiments.

Minimal Run

import migraphx

program = migraphx.parse_onnx("model.onnx")
program.compile(migraphx.get_target("gpu"))

params = {}
for name, shape in program.get_parameter_shapes().items():
    params[name] = migraphx.generate_argument(shape)

outputs = program.run(params)
print(outputs)

Save And Load

import migraphx

program = migraphx.parse_onnx("model.onnx")
program.compile(migraphx.get_target("gpu"))
migraphx.save(program, "model.mxr")

loaded = migraphx.load("model.mxr")

Declare Input Shapes

For dynamic ONNX models, pass expected dimensions at parse time:

import migraphx

program = migraphx.parse_onnx(
    "yolo.onnx",
    map_input_dims={
        "images": [1, 3, 640, 640],
    },
)
program.compile(migraphx.get_target("gpu"))

Replace images with the actual ONNX input name.

Inspect Parameters

for name, shape in program.get_parameter_shapes().items():
    print(name, shape)

Use this output to wire preprocessing correctly.

Smoke Test Function

import migraphx


def compile_and_check(path: str, target: str = "gpu"):
    program = migraphx.parse_onnx(path)
    program.compile(migraphx.get_target(target))

    params = {
        name: migraphx.generate_argument(shape)
        for name, shape in program.get_parameter_shapes().items()
    }
    outputs = program.run(params)
    return program, outputs


program, outputs = compile_and_check("model.onnx")
print("outputs:", len(outputs))

Where Python Fits

Use Python to:

  • validate model compatibility before C++ integration
  • test exported ONNX graph shapes
  • compare reference and GPU results
  • automate performance sweeps

For a C++ video or service backend, Python should not be the only integration test. Keep one native smoke test too.

On this page