Repository navigation
Track each transformers pipeline call once - #77
Merged
Merged
Conversation
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
Coverage report
Top uncovered files
|
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.
…eline-double-count
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.
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 withfrom_pretrainedbeforePipeline.__init__runs, so theinside_pipeline_initguard 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: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
Counterof the models wrapped by running tracked pipeline calls, keyed byid(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 existinginside_pipeline_initflag, lives on a smallthreading.localsubclass that declares both.What stays tracked:
_forwardalso calls a separately tracked reranker. A first version suppressed every model on the thread; the review caught that.pipe.model(...), or a model passed topipeline()).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: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: thexfailfrom 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