{"record":{"id":"207645af12c17c66","repo":"huggingface/pytorch-image-models","slug":"output-fmt-nchw-is-not-supported-for-naflex-dic","errorCode":null,"errorMessage":"output_fmt=\"NCHW\" is not supported for NaFlex (dict) inputs, use \"NLC\". Per-sample grids vary; reconstruct spatial maps downstream via patch_coord.","messagePattern":"output_fmt=\"NCHW\" is not supported for NaFlex \\(dict\\) inputs, use \"NLC\"\\. Per-sample grids vary; reconstruct spatial maps downstream via patch_coord\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"timm/models/naflexvit.py","lineNumber":1670,"sourceCode":"        Returns:\n            A tuple with (final_features, intermediates), a list of intermediate features, or a dictionary containing\n            'image_features' and 'image_intermediates' (and optionally 'image_intermediates_prefix').\n\n        NaFlex (dict / pre-patchified) inputs: NLC output only (per-sample grids are variable, a single\n        spatial reshape is undefined); with ``output_dict=True`` the result also carries 'patch_valid'\n        aligned with the spatial intermediates so consumers can mask padding or scatter via patch_coord.\n        \"\"\"\n\n        assert output_fmt in ('NCHW', 'NLC'), 'Output format must be one of NCHW or NLC.'\n        reshape = output_fmt == 'NCHW'\n        intermediates = []\n        take_indices, max_index = feature_take_indices(len(self.blocks), indices)\n        if isinstance(x, dict):\n            # Dictionary input from the NaFlex collator. Per-sample grids are variable\n            # (native aspect) and padding tokens belong to no grid, so a single spatial\n            # reshape is undefined -- NLC output only.\n            if reshape:\n                raise ValueError(\n                    'output_fmt=\"NCHW\" is not supported for NaFlex (dict) inputs, use \"NLC\". '\n                    'Per-sample grids vary; reconstruct spatial maps downstream via patch_coord.')\n            patch_coord = x['patch_coord']\n            patch_valid = x.get('patch_valid', patch_valid)\n            attn_mask = x.get('attn_mask', attn_mask)\n            patches = x['patches']\n            H = W = None\n            if not output_dict and self.training and self.patch_drop is not None:\n                # patch dropout gathers the token sequence, so the caller's input patch_valid no\n                # longer aligns with the returned tokens -- the gathered mask is only surfaced in\n                # dict output mode. Tuple mode is fine at eval / without patch dropout.\n                raise ValueError(\n                    'NaFlex forward_intermediates with active patch dropout requires '\n                    'output_dict=True to return the gathered patch_valid.')\n        else:\n            patches = x\n            height, width = x.shape[-2:]\n            H, W = self.embeds.dynamic_feat_size((height, width))","sourceCodeStart":1652,"sourceCodeEnd":1688,"githubUrl":"https://github.com/huggingface/pytorch-image-models/blob/9a5261e31b3b5128526eb2658333b4c0a54464ae/timm/models/naflexvit.py#L1652-L1688","documentation":"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.","triggerScenarios":"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.","commonSituations":"Reusing a feature-pyramid extraction pipeline written for fixed-size ViTs and feeding it NaFlex-native batches with mixed aspect ratios.","solutions":["Use output_fmt='NLC' (default) and reshape downstream per sample using the returned patch_coord","If you truly need fixed maps, feed fixed-size tensors instead of the NaFlex dict"],"exampleFix":"# before\nfeats = model.forward_intermediates(batch_dict, indices=[0,2,4,6], output_fmt='NCHW')\n# after\nfeats = model.forward_intermediates(batch_dict, indices=[0,2,4,6], output_fmt='NLC')","handlingStrategy":"validation","validationCode":"fmt = 'NLC' if isinstance(x, dict) else fmt\nfeats = model.forward_intermediates(x, output_fmt=fmt)","typeGuard":"def is_naflex_batch(x) -> bool:\n    return isinstance(x, dict) and 'patch_coord' in x","tryCatchPattern":null,"preventionTips":["Branch output_fmt on input type","Use patch_coord to rebuild per-sample maps downstream"],"tags":["timm","naflexvit","features","output-format"],"backgroundTag":"unsupported-output-format","analyzedSha":"9a5261e31b3b5128526eb2658333b4c0a54464ae","analyzedAt":"2026-08-27T02:34:25.417Z","schemaVersion":2},"datasetVersion":"2026-08-27T03:17:27.898Z"}