huggingface/pytorch-image-models · error · ValueError

Please provide `hook_fns` for each `hook_fn_locs`, their len

Error message

Please provide `hook_fns` for each `hook_fn_locs`, their lengths are different.

What it means

BenchmarkHook / hook-registration helper in timm.utils.model requires hook_fn_locs and hook_fns to be parallel lists: one hook function per location. ValueError is raised at __init__ when len(hook_fn_locs) != len(hook_fns).

Source

Thrown at timm/utils/model.py:73

    Arguments:
        model (nn.Module): model from which we will extract the activation stats
        hook_fn_locs (List[str]): List of `hook_fn` locations based on Unix type string 
            matching with the name of model's modules. 
        hook_fns (List[Callable]): List of hook functions to be registered at every
            module in `layer_names`.
    
    Inspiration from https://docs.fast.ai/callback.hook.html.

    Refer to https://gist.github.com/amaarora/6e56942fcb46e67ba203f3009b30d950 for an example 
    on how to plot Signal Propagation Plots using `ActivationStatsHook`.
    """

    def __init__(self, model, hook_fn_locs, hook_fns):
        self.model = model
        self.hook_fn_locs = hook_fn_locs
        self.hook_fns = hook_fns
        if len(hook_fn_locs) != len(hook_fns):
            raise ValueError("Please provide `hook_fns` for each `hook_fn_locs`, \
                their lengths are different.")
        self.stats = dict((hook_fn.__name__, []) for hook_fn in hook_fns)
        for hook_fn_loc, hook_fn in zip(hook_fn_locs, hook_fns):
            self.register_hook(hook_fn_loc, hook_fn)

    def _create_hook(self, hook_fn):
        def append_activation_stats(module, input, output):
            out = hook_fn(module, input, output)
            self.stats[hook_fn.__name__].append(out)

        return append_activation_stats

    def register_hook(self, hook_fn_loc, hook_fn):
        for name, module in self.model.named_modules():
            if not fnmatch.fnmatch(name, hook_fn_loc):
                continue
            module.register_forward_hook(self._create_hook(hook_fn))

View on GitHub (pinned to 9a5261e31b)

Solutions

  1. Make the lists equal length, e.g. hook_fns=[my_hook_fn] * len(hook_fn_locs) if the same fn is reused
  2. Double-check you passed a list (not a scalar) for both arguments when there are multiple hooks

Example fix

# before
hooks = BenchmarkHook(model, ['blocks.0.attn', 'blocks.1.attn'], [my_hook_fn])
# after
hooks = BenchmarkHook(model, ['blocks.0.attn', 'blocks.1.attn'], [my_hook_fn, my_hook_fn])
Defensive patterns

Strategy: validation

Validate before calling

assert len(hook_fn_locs) == len(hook_fns), 'locs and fns must align'

Prevention

When it happens

Trigger: Passing ModelEmlaHook-style API e.g. BenchmarkHook(model, ['blocks.0.attn', 'blocks.1.attn'], [my_hook_fn]) with 2 locs but 1 fn, or passing a single function instead of a list of functions.

Common situations: Reusing one hook function for several locations without wrapping it in a list comprehension; adding a new location but forgetting the corresponding function; passing a bare function where a list is expected.

Related errors


AI-assisted analysis of huggingface/pytorch-image-models@9a5261e31b (2026-08-27). Data as JSON: /api/errors/268a0e6ea0effa25. Report an issue: GitHub.