Skip to content

AutoMode (Generating a ModelSpec)

AutoMode runs your model and infers a ModelSpec from its inputs and outputs. We recommend trying AutoMode first and switching to hand-writing only for the items it cannot decide.


Entry point: inspect_model

from ml_debugger.spec import inspect_model

result = inspect_model(model)

The returned InspectionResult holds the facts that were resolved (result.spec) and the questions that were not (result.questions).

Situation How to proceed
No questions Use result.spec directly → 1
Questions remain Render a draft to a file and fill in the blanks → 2
Questions remain (notebook / interactive) Answer them in place and re-infer → 3
You already know the values Pass them to infer_spec via known= → 4

Improving the confidence of the inference

Passing one representative batch as sample_batch= makes the inference observe a real run of the model, so input-dependent items (input shape, image size, …) are far more likely to resolve. For detection tasks you can also pass a ground-truth sample as ground_truth=.

result = inspect_model(model, sample_batch=batch, ground_truth=gt_batch)

sample_batch= is optional

When omitted, a standard image input for your framework is built from bundled assets and used. However, models that take point clouds, multiple cameras, or file paths do require sample_batch=.


1. Use the inferred spec as-is

If there are no questions, result.spec is a ModelSpec you can use directly.

result = inspect_model(model)

if not result.questions:
    spec = result.spec        # done — pass this as spec=

tracer = ObjectDetectionTracer(model, spec=spec, model_name="my_det", version_name="v1")

To also declare the ground-truth structure, render the draft as in 2 and rewrite labels= into LabelSchema(n_class=..., ground_truth=...) (see labels=).


2. Render a draft and finish it

result.render_spec_draft() returns a complete Python module as a string (imports included) with every inferred fact filled in. Items that could not be resolved are marked with <...> placeholders and # TODO notes — you only fill those in.

Because the spec becomes a file, it can be reviewed and version-controlled.

from pathlib import Path
from ml_debugger.spec import inspect_model

result = inspect_model(model)
Path("my_spec.py").write_text(result.render_spec_draft())

What gets written (a 2D detector where the class count and label base were ambiguous):

from ml_debugger.spec import schema as S
from ml_debugger.spec import ModelSpec, RawOutputSchema, NMS, TopK, Invoke
from ml_debugger.spec import LayerHook, HeadHook, InModelNMS

# Inference could not resolve every field on this sample. Fill each <...>
# placeholder (see the TODO note) or call result.answer(...) and re-render.
spec = ModelSpec(
    labels=<...>,  # TODO: n_class ambiguous on this sample
    output=S.BatchAxis[
        S.Dict(
            boxes=S.DetAxis[S.Coords("xyxy", "pixel")],
            scores=S.DetAxis[S.Score("raw")],
            labels=S.DetAxis[S.LabelId(<0 or 1>)],  # TODO: label_base ambiguous on this sample
        )
    ],
)
# TODO: n_class -- could not be determined on this sample; supply it via known=/spec=.
# TODO: label_base -- choose one of: 0, 1

Fill in the <...> placeholders (here, labels=LabelSchema(n_class=3, ground_truth=gt) and S.LabelId(0)), then import the spec where you need it.

from my_spec import spec

tracer = ObjectDetectionTracer(model, spec=spec, model_name="my_det", version_name="v1")

When editing the draft

  • The draft's labels= carries the class count only. To also declare the ground-truth structure, add from ml_debugger.spec import LabelSchema and rewrite it as labels=LabelSchema(n_class=..., ground_truth=...).
  • Always validate an edited ModelSpec before using it.

3. Answer the questions interactively

When questions remain, you can answer them in place and re-run the inference. This suits notebooks and other interactive environments (a script cannot pause, so use known= — see 4 — there).

result = inspect_model(model)

for q in result.questions:
    print(q.id, q.choices, q.reason)   # e.g. n_class None "n_class cannot be verified ..."

result = result.answer(n_class=3)      # answer with the question id as the keyword
spec = result.spec

Each Question carries:

Attribute Contents
id The keyword to answer with
reason Why it could not be resolved
choices The candidate values (None when the set is open)
evidence The clues that were observed

Questions can iterate

Answering one question may reveal the next. Repeat until questions is empty.


4. One-liner: infer_spec and known=

infer_spec(model) is meant for embedding spec construction directly in your logging code. When everything can be resolved automatically, use the returned ModelSpec as-is. Otherwise it raises SpecInferenceError, and you supply what is missing via known=.

from ml_debugger.spec import infer_spec, SpecInferenceError

try:
    spec = infer_spec(model)
except SpecInferenceError as exc:
    print(exc.asks)                          # e.g. ['n_class']
    spec = infer_spec(model, known={"n_class": 3})

The keys of known= are the same names as asks (that is, Question.id). sample_batch= and ground_truth= work exactly as with inspect_model.

If you know the values up front, this collapses to a single line:

spec = infer_spec(model, sample_batch=batch, known={"n_class": 3})

Next steps

  • Always run validation on an inferred spec before using it.
  • To fill in the rest by hand, see the per-field pages (labels=, output=, …).