huggingface/pytorch-image-models · error · ValueError

NaFlex forward_intermediates with active patch dropout requi

Error message

NaFlex forward_intermediates with active patch dropout requires output_dict=True to return the gathered patch_valid.

What it means

When patch dropout is active in training mode, the token sequence is gathered and the caller's input patch_valid no longer matches returned tokens; the corrected mask is only returned in dict output mode. Tuple output is therefore rejected.

Source

Thrown at timm/models/naflexvit.py:1682

        take_indices, max_index = feature_take_indices(len(self.blocks), indices)
        if isinstance(x, dict):
            # Dictionary input from the NaFlex collator. Per-sample grids are variable
            # (native aspect) and padding tokens belong to no grid, so a single spatial
            # reshape is undefined -- NLC output only.
            if reshape:
                raise ValueError(
                    'output_fmt="NCHW" is not supported for NaFlex (dict) inputs, use "NLC". '
                    'Per-sample grids vary; reconstruct spatial maps downstream via patch_coord.')
            patch_coord = x['patch_coord']
            patch_valid = x.get('patch_valid', patch_valid)
            attn_mask = x.get('attn_mask', attn_mask)
            patches = x['patches']
            H = W = None
            if not output_dict and self.training and self.patch_drop is not None:
                # patch dropout gathers the token sequence, so the caller's input patch_valid no
                # longer aligns with the returned tokens -- the gathered mask is only surfaced in
                # dict output mode. Tuple mode is fine at eval / without patch dropout.
                raise ValueError(
                    'NaFlex forward_intermediates with active patch dropout requires '
                    'output_dict=True to return the gathered patch_valid.')
        else:
            patches = x
            height, width = x.shape[-2:]
            H, W = self.embeds.dynamic_feat_size((height, width))

        # Forward pass through patch and abs position embedding
        embeds = self._forward_embeds(
            patches,
            patch_coord=patch_coord,
            patch_valid=patch_valid,
            attn_mask=attn_mask,
        )
        x = embeds['patches']
        rope_embeds = embeds.get('rope_embeds', None)
        keep_indices = embeds.get('keep_indices', None)
        attn_mask = embeds.get('attn_mask', None)

View on GitHub (pinned to 9a5261e31b)

Solutions

  1. Pass output_dict=True to get the gathered patch_valid alongside features
  2. Or call model.eval() before forward_intermediates so dropout is inactive
  3. Or disable patch dropout (patch_drop_rate=0) if tuple output is required during training

Example fix

# before
feats = model.forward_intermediates(batch_dict, indices=[0,2,4,6])
# after
feats = model.forward_intermediates(batch_dict, indices=[0,2,4,6], output_dict=True)
Defensive patterns

Strategy: validation

Validate before calling

needs_dict = isinstance(x, dict) and model.training and getattr(model, 'patch_drop', None) is not None
feats = model.forward_intermediates(x, indices=idx, output_dict=needs_dict or output_dict)

Prevention

When it happens

Trigger: Calling forward_intermediates(naflex_dict, output_dict=False) while model.training is True and a patch_drop module is configured (patch_drop_rate > 0).

Common situations: Running training-time intermediate feature extraction (distillation, deep supervision) on NaFlex models with patch dropout enabled.

Related errors


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