def forward(self, x: torch.Tensor,
                mask: Optional[torch.Tensor]) -> torch.Tensor:
        """Generate features for masked images.

        The function supports two kind of forward behaviors. If the ``mask`` is
        not ``None``, the forward function will be executed as masked image
        modeling pre-training; if the ``mask`` is ``None``, the forward
        function will call ``super().forward()``, which extract features from
        images without mask.

        Args:
            x (torch.Tensor): Input images.
            mask (torch.Tensor, optional): Input masks.

        Returns:
            torch.Tensor: Features with cls_tokens.
        """
        if mask is None:
            return super().forward(x)

        else:
            B = x.shape[0]
            x = self.patch_embed(x)[0]

            # masking: length -> length * mask_ratio
            B, L, _ = x.shape
            mask_tokens = self.mask_token.expand(B, L, -1)
            mask = mask.unsqueeze(-1)
            x = x * (1 - mask.int()) + mask_tokens * mask

            # append cls token
            cls_tokens = self.cls_token.expand(B, -1, -1)
            x = torch.cat((cls_tokens, x), dim=1)
            x = x + self.pos_embed
            x = self.drop_after_pos(x)

            for i, layer in enumerate(self.layers):
                x = layer(x)

                if i == len(self.layers) - 1 and self.final_norm:
                    x = self.norm1(x)

            return x
Generate features for masked images.

        The function supports two kind of forward behaviors. If the ``mask`` is
        not ``None``, the forward function will be executed as masked image
        modeling pre-training; if the ``mask`` is ``None``, the forward
        function will call ``super().forward()``, which extract features from
        images without mask.

        Args:
            x (torch.Tensor): Input images.
            mask (torch.Tensor, optional): Input masks.

        Returns:
            torch.Tensor: Features with cls_tokens.
        
mmpretrain/models/selfsup/maskfeat.py