huggingface/pytorch-image-models · error · ValueError

output_fmt="NCHW" is not supported for NaFlex (dict) inputs,

Error message

output_fmt="NCHW" is not supported for NaFlex (dict) inputs, use "NLC". Per-sample grids vary; reconstruct spatial maps downstream via patch_coord.

What it means

NaFlex dict inputs contain variable per-sample grids and padding tokens that belong to no grid, so tokens cannot be reshaped into one NCHW map. forward_intermediates therefore rejects reshape=True for dict inputs.

Source

Thrown at timm/models/naflexvit.py:1670

        Returns:
            A tuple with (final_features, intermediates), a list of intermediate features, or a dictionary containing
            'image_features' and 'image_intermediates' (and optionally 'image_intermediates_prefix').

        NaFlex (dict / pre-patchified) inputs: NLC output only (per-sample grids are variable, a single
        spatial reshape is undefined); with ``output_dict=True`` the result also carries 'patch_valid'
        aligned with the spatial intermediates so consumers can mask padding or scatter via patch_coord.
        """

        assert output_fmt in ('NCHW', 'NLC'), 'Output format must be one of NCHW or NLC.'
        reshape = output_fmt == 'NCHW'
        intermediates = []
        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))

View on GitHub (pinned to 9a5261e31b)

Solutions

  1. Use output_fmt='NLC' (default) and reshape downstream per sample using the returned patch_coord
  2. If you truly need fixed maps, feed fixed-size tensors instead of the NaFlex dict

Example fix

# before
feats = model.forward_intermediates(batch_dict, indices=[0,2,4,6], output_fmt='NCHW')
# after
feats = model.forward_intermediates(batch_dict, indices=[0,2,4,6], output_fmt='NLC')
Defensive patterns

Strategy: validation

Validate before calling

fmt = 'NLC' if isinstance(x, dict) else fmt
feats = model.forward_intermediates(x, output_fmt=fmt)

Type guard

def is_naflex_batch(x) -> bool:
    return isinstance(x, dict) and 'patch_coord' in x

Prevention

When it happens

Trigger: Calling model.forward_intermediates(naflex_batch_dict, indices=..., output_fmt='NCHW') or reshape=True where the input is the dict produced by the NaFlex collator.

Common situations: Reusing a feature-pyramid extraction pipeline written for fixed-size ViTs and feeding it NaFlex-native batches with mixed aspect ratios.

Related errors


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