{"record":{"id":"59d733bd815df57f","repo":"huggingface/pytorch-image-models","slug":"naflex-forward-intermediates-with-active-patch-dro","errorCode":null,"errorMessage":"NaFlex forward_intermediates with active patch dropout requires output_dict=True to return the gathered patch_valid.","messagePattern":"NaFlex forward_intermediates with active patch dropout requires output_dict=True to return the gathered patch_valid\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"timm/models/naflexvit.py","lineNumber":1682,"sourceCode":"        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))\n\n        # Forward pass through patch and abs position embedding\n        embeds = self._forward_embeds(\n            patches,\n            patch_coord=patch_coord,\n            patch_valid=patch_valid,\n            attn_mask=attn_mask,\n        )\n        x = embeds['patches']\n        rope_embeds = embeds.get('rope_embeds', None)\n        keep_indices = embeds.get('keep_indices', None)\n        attn_mask = embeds.get('attn_mask', None)","sourceCodeStart":1664,"sourceCodeEnd":1700,"githubUrl":"https://github.com/huggingface/pytorch-image-models/blob/9a5261e31b3b5128526eb2658333b4c0a54464ae/timm/models/naflexvit.py#L1664-L1700","documentation":"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.","triggerScenarios":"Calling forward_intermediates(naflex_dict, output_dict=False) while model.training is True and a patch_drop module is configured (patch_drop_rate > 0).","commonSituations":"Running training-time intermediate feature extraction (distillation, deep supervision) on NaFlex models with patch dropout enabled.","solutions":["Pass output_dict=True to get the gathered patch_valid alongside features","Or call model.eval() before forward_intermediates so dropout is inactive","Or disable patch dropout (patch_drop_rate=0) if tuple output is required during training"],"exampleFix":"# before\nfeats = model.forward_intermediates(batch_dict, indices=[0,2,4,6])\n# after\nfeats = model.forward_intermediates(batch_dict, indices=[0,2,4,6], output_dict=True)","handlingStrategy":"validation","validationCode":"needs_dict = isinstance(x, dict) and model.training and getattr(model, 'patch_drop', None) is not None\nfeats = model.forward_intermediates(x, indices=idx, output_dict=needs_dict or output_dict)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Default to output_dict=True for training-time feature extraction","Wrap extraction in model.eval() when dropout-free behavior is acceptable"],"tags":["timm","naflexvit","patch-dropout","features"],"backgroundTag":"api-precondition-violated","analyzedSha":"9a5261e31b3b5128526eb2658333b4c0a54464ae","analyzedAt":"2026-08-27T02:34:25.417Z","schemaVersion":2},"datasetVersion":"2026-08-27T03:17:27.898Z"}