Skip to content

heads= (Multi-Head Models)

For a model that returns several predictions from one forward pass, declare each head's structure under heads=: one HeadSpec per head, each carrying the same fields a single-head ModelSpec carries. Recording, upload, evaluation and DataFilter then all run per head.

New in v0.4.2

The heads= field and the shared entry points TrainingTracer / Logger / DataFilter were added in v0.4.2.


The declaration

  • inputs= / invoke= stay on the ModelSpec. How the model is called is shared by every head: the model runs once, and each head reads its own part of that one output.
  • labels / raw_output / postprocess / output move into each HeadSpec, plus an optional task when the declared structures alone do not decide it.

The example declares a model whose forward pass returns {"cls_out": <(B, 10) logits>, "det_out": {"boxes": ..., "logits": ...}}:

from ml_debugger.spec import HeadSpec, ModelSpec, TopK
from ml_debugger.spec import schema as S

spec = ModelSpec(
    heads={
        "cls": HeadSpec(
            task="classification",
            labels=10,                                       # 10 classes
            raw_output=S.Dict(cls_out=S.ClassConfs(activation="raw")),  # plain logits (pre-softmax)
        ),
        "det": HeadSpec(
            task="object_detection",
            labels=3,                                        # 3 foreground classes
            raw_output=S.Dict(
                det_out=S.Dict(
                    boxes=S.DetAxis[S.Coords("cxcywh", "normalized")],
                    logits=S.DetAxis[S.ClassConfs(activation="sigmoid", background="none")],
                ),
            ),
            postprocess=TopK(score_thresh=0.3, max_num=100),
        ),
    },
)

Head names are free (they need not match the forward-output keys)

The keys of heads= (here "cls" / "det") are names used for recording only — they do not have to correspond to the model's output keys. Which part of the model output a head reads is decided by its raw_output / output structure (here the cls_out / det_out keys of S.Dict). A head name may contain letters, digits, _ and -.


Shared entry points: TrainingTracer / Logger / DataFilter

Heads can serve different tasks, so no task-named class (ObjectDetectionTracer, …) can serve them. Use the entry points that do not name a task — one per purpose:

from ml_debugger import TrainingTracer

tracer = TrainingTracer(model, model_name="my_model", version_name="v1", spec=spec)
tracer(
    images,
    ground_truth={                       # ground truth is keyed by head name
        "cls": class_labels,             # (B,) class ids
        "det": det_ground_truth,         # the usual boxes / labels dicts
    },
    input_ids=["img_001", "img_002"],
    dataset_type="train",
)
tracer.wait_for_save()

Each head is recorded under its own name — my_model[cls] / my_model[det] — exactly as a model that only ever had that head would be.

As with a single-head spec, spec= is required only on the first construction; it is stored against (model_name, version_name) and the other classes load it automatically:

from ml_debugger import DataFilter, Logger

logger  = Logger(model, model_name="my_model", version_name="v1")
dfilter = DataFilter(model, model_name="my_model", version_name="v1")

The three entry points accept single-head models just as well, and the task-named classes remain available and unchanged.


Evaluation (Evaluator)

Build the Evaluator on the model's own name, as usual. On a multi-head model it finds the heads automatically: evaluating all heads at once returns a dict of Result objects keyed by head name, and passing head= evaluates a single head instead.

from ml_debugger.evaluator import Evaluator

evaluator = Evaluator(model_name="my_model", version_name="v1")
evaluator.heads                                     # ['cls', 'det']

results = evaluator.request_evaluation()            # all heads at once -> {head name: Result}
results["det"].get_summary()

result = evaluator.request_evaluation(head="cls")   # one head only -> Result

On a single-head model a single Result comes back as before (head= does not apply).


Notes

  • Task-specific arguments follow the first declared head. query and other operations that take task-specific arguments use the argument set of the first head's task — declare the head whose query you want to steer first.
  • Single-head models are unaffected: their behavior and recorded values do not change.