Skip to content

Track each transformers pipeline call once - #77

Merged
piotrekno1 merged 4 commits into
develfrom
fix/transformers-pipeline-double-count
Oct 20, 2026
Merged

piotrekno1 merged 4 commits into
develfrom
fix/transformers-pipeline-double-count

Conversation

@piotrekno1

@piotrekno1 piotrekno1 commented Oct 20, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #70.

Problem

Every call to a transformers pipeline recorded two inference events for the same model, on transformers 4.40 (min) and 5.20 (current).

pipeline() loads the model with from_pretrained before Pipeline.__init__ runs, so the inside_pipeline_init guard doesn't stop it. The model is registered and gets forward hooks. When the pipeline is registered under the same id, it gets its own __call__ tracking, but the model's forward hooks stay. A pipeline call then records:

  1. the pipeline event, with batch size, input text stats and output task, and
  2. the model's forward-hook event, without them.

Fix

While a tracked pipeline call runs, the forward hooks of that pipeline's model record nothing; the pipeline event is the only record of the call. Each thread keeps a Counter of the models wrapped by running tracked pipeline calls, keyed by id(model) and counted so nested calls work. A forward hook (pre and post) skips only when its own model is in it. The per-thread state, this and the existing inside_pipeline_init flag, lives on a small threading.local subclass that declares both.

What stays tracked:

  • Other models a pipeline runs during its call, for example a custom pipeline whose _forward also calls a separately tracked reranker. A first version suppressed every model on the thread; the review caught that.
  • Direct calls to the model (pipe.model(...), or a model passed to pipeline()).
  • Calls to the model from another thread during a pipeline call.

The count is decremented in a finally, so a failing pipeline call doesn't leave the model suppressed.

Not covered: lazy inputs (#78). For a generator or dataset input, Pipeline.__call__ returns an iterator and inference runs as it's consumed, after the tracked call has returned. That already recorded wrong events before this change. The changelog says this fix applies to string and list inputs and points to #78.

Tests

  • tests/test_integrations_transformers.py (new), using a fake pipeline whose __call__ runs a fake model with forward hooks:

    • A pipeline call gives one event, the pipeline's.
    • A direct model call is still tracked.
    • Another tracked model run during the pipeline call is tracked (the review's case).
    • Nested pipeline calls on the same model stay suppressed until the outer call ends.
    • A failed pipeline call records an error and stops suppressing.
    • A pipeline without a handle doesn't suppress its model.
    • A call to the model from another thread during a pipeline call is tracked.

    Each of the last few was checked against a broken variant: the previous all-models suppression fails the two-model test, ending suppression at the first inner exit fails the nesting test, and sharing the state across threads fails the thread test.

  • tests/compat/test_transformers_compat.py::test_pipeline_load_and_call_are_tracked: the xfail from Fix the compat matrix and make it test each integration #69 is removed. It passes with real transformers 4.40.0 and 5.20.1.

  • Full suite: 709 passed, 28 skipped. ruff is clean under the Enable more ruff rules and check lint in CI #75 rules.

Not changed

A model's forward hook fires on every forward pass. For text generation, a direct model.generate(...) call (outside a pipeline) records one event per generated token rather than one per call. Pipelines are not affected. That's a separate issue; I can file it.

🤖 Generated with Claude Code

pipeline() loads the model before Pipeline.__init__ runs, so the model was
registered with forward hooks of its own, and every pipeline call produced
two inference events: one from the pipeline patch and one from the model
inside it. The pipeline patch now marks the call on a thread-local counter
and the model's forward hook skips while it is set. Direct model calls are
still tracked.

Closes #70
@github-actions

github-actions Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

Coverage report

  • Total: 88% (9997/11397 lines)

Top uncovered files

  • wildedge/integrations/mlx.py: 28.7% (122 lines missed)
  • wildedge/integrations/transformers.py: 62.9% (112 lines missed)
  • wildedge/integrations/ultralytics.py: 68.0% (82 lines missed)
  • wildedge/integrations/pytorch.py: 57.5% (79 lines missed)
  • wildedge/platforms/macos.py: 56.8% (67 lines missed)

The two per-thread flags were attributes on a bare threading.local read with
getattr defaults. A threading.local subclass now declares both, the
forward pre-hook skips inside a tracked pipeline call like the post-hook
does, and the cross-thread test checks behaviour instead of setting the
counter by hand.
Review of #77: the hook guard checked whether any tracked pipeline call was
running on the thread, so a custom pipeline that also ran a second tracked
model silently dropped that model's events. Each thread now counts the
models wrapped by running tracked pipeline calls (by id, for nesting), and a
forward hook skips only when its own model is among them.

The changelog no longer claims one event per call for generator and dataset
inputs, which run lazily after __call__ returns; that is #78.
@piotrekno1
piotrekno1 merged commit 17f4336 into devel Oct 20, 2026
90 checks passed
@piotrekno1
piotrekno1 deleted the fix/transformers-pipeline-double-count branch October 20, 2026 15:30
@github-actions github-actions Bot mentioned this pull request Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

transformers pipeline calls are counted twice

1 participant