{"record":{"id":"82172860e387b3a4","repo":"open-mmlab/mmdetection","slug":"if-you-want-to-reduce-gpu-memory-usage-821728","errorCode":null,"errorMessage":"If you want to reduce GPU memory usage,                     please install fairscale by executing the                     following command: pip install fairscale.","messagePattern":"If you want to reduce GPU memory usage,                     please install fairscale by executing the                     following command: pip install fairscale\\.","errorType":"exception","errorClass":"NotImplementedError","httpStatus":null,"severity":"error","filePath":"mmdet/models/layers/transformer/detr_layers.py","lineNumber":54,"sourceCode":"                 init_cfg: OptConfigType = None) -> None:\n\n        super().__init__(init_cfg=init_cfg)\n        self.num_layers = num_layers\n        self.layer_cfg = layer_cfg\n        self.num_cp = num_cp\n        assert self.num_cp <= self.num_layers\n        self._init_layers()\n\n    def _init_layers(self) -> None:\n        \"\"\"Initialize encoder layers.\"\"\"\n        self.layers = ModuleList([\n            DetrTransformerEncoderLayer(**self.layer_cfg)\n            for _ in range(self.num_layers)\n        ])\n\n        if self.num_cp > 0:\n            if checkpoint_wrapper is None:\n                raise NotImplementedError(\n                    'If you want to reduce GPU memory usage, \\\n                    please install fairscale by executing the \\\n                    following command: pip install fairscale.')\n            for i in range(self.num_cp):\n                self.layers[i] = checkpoint_wrapper(self.layers[i])\n\n        self.embed_dims = self.layers[0].embed_dims\n\n    def forward(self, query: Tensor, query_pos: Tensor,\n                key_padding_mask: Tensor, **kwargs) -> Tensor:\n        \"\"\"Forward function of encoder.\n\n        Args:\n            query (Tensor): Input queries of encoder, has shape\n                (bs, num_queries, dim).\n            query_pos (Tensor): The positional embeddings of the queries, has\n                shape (bs, num_queries, dim).\n            key_padding_mask (Tensor): The `key_padding_mask` of `self_attn`","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/open-mmlab/mmdetection/blob/cfd5d3a985b0249de009b67d04f37263e11cdf3d/mmdet/models/layers/transformer/detr_layers.py#L36-L72","documentation":"Raised when DetrTransformerEncoder has num_cp > 0 but fairscale is not installed. The encoder layers are wrapped with fairscale's checkpoint_wrapper to trade compute for GPU memory; without the package this is impossible.","triggerScenarios":"Config with with_cp=True or num_cp>0 on a DETR transformer encoder while fairscale is absent from the environment.","commonSituations":"Using memory-saving configs on machines without fairscale; upgrading mmdet versions where the checkpointing path changed.","solutions":["pip install fairscale","Or remove/zero num_cp in the encoder config"],"exampleFix":"# before\nnum_cp=6, fairscale not installed\n# after\npip install fairscale  # or set num_cp=0","handlingStrategy":"validation","validationCode":"if cfg.get('num_cp', 0) > 0:\n    import fairscale  # raises ImportError early with a clear message","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep fairscale in the env when memory-saving configs are used","CI smoke-test model builds to catch env mismatches"],"tags":["mmdet","detr","fairscale","dependency","gpu-memory"],"backgroundTag":"missing-dependency","analyzedSha":"cfd5d3a985b0249de009b67d04f37263e11cdf3d","analyzedAt":"2026-08-27T20:54:20.183Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}