transformers pipeline calls are counted twice
What happens
Each call to a transformers pipeline records two inference events for the same model.
clf = transformers.pipeline("text-classification", model=path, tokenizer=path)
clf("hello world") # 2 inference events
- One comes from the
Pipeline.__call__patch, withbatch_sizeandinput_meta. - The other comes from the forward hook on the model inside the pipeline (
TransformersExtractor.install_hooks,post_hook), without them.
pipeline() loads the model with from_pretrained before Pipeline.__init__ runs, so the _tl.inside_pipeline_init guard doesn't apply. The inner model is registered and gets forward hooks. When the pipeline later replaces it as the tracked object (the already_tracked branch in WildEdge._on_model_auto_loaded), the pipeline gets its own hooks, but the inner model's forward hooks are never removed.
This happens with transformers 4.40.0 (min) and 5.20.1 (current). Inference counts for pipeline users are doubled.
Expected
One inference event per pipeline call: remove the inner model's hooks when the pipeline supersedes it, or make the forward hook skip calls made inside a tracked pipeline call.
Repro
tests/compat/test_transformers_compat.py::test_pipeline_load_and_call_are_tracked (marked xfail(strict=True) until this is fixed).
🤖 Generated with Claude Code