docstr: If subset_mask is not None, we only want output for the subset of the sequence.
  This means that we only compute the last layer output for these tokens.
  subset_mask: (batch, seqlen), dtype=torch.bool
docstr_tok: ["If","subset_mask","is","not","None",",","we","only","want","output","for","the","subset","of","the","sequence",".","This","means","that","we","only","compute","the","last","layer","output","for","these","tokens",".","subset_mask",":","(","batch",",","seqlen",")",",","dtype=torch.bool"]
code: def forward(
      self, hidden_states, key_padding_mask=None, subset_mask=None
  ):
      """If subset_mask is not None, we only want output for the subset of the sequence.
      This means that we only compute the last layer output for these tokens.
      subset_mask: (batch, seqlen), dtype=torch.bool
      """
      if key_padding_mask is None or not self.use_flash_attn:
          mixer_kwargs = (
              {"key_padding_mask": key_padding_mask}
              if key_padding_mask is not None
              else None
          )
          for layer in self.layers:
              hidden_states = layer(
                  hidden_states, mixer_kwargs=mixer_kwargs
              )
          if subset_mask is not None:
              hidden_states = hidden_states[subset_mask]
      else:
          batch, seqlen = hidden_states.shape[:2]
          (
              hidden_states,
              indices,
              cu_seqlens,
              max_seqlen_in_batch,
          ) = unpad_input(hidden_states, key_padding_mask)
          mixer_kwargs = {
              "cu_seqlens": cu_seqlens,
              "max_seqlen": max_seqlen_in_batch,
          }
          if subset_mask is None:
              for layer in self.layers:
                  hidden_states = layer(
                      hidden_states, mixer_kwargs=mixer_kwargs
                  )
              hidden_states = pad_input(
                  hidden_states, indices, batch, seqlen
              )
          else:
              for layer in self.layers[:-1]:
                  hidden_states = layer(
                      hidden_states, mixer_kwargs=mixer_kwargs
                  )
              if key_padding_mask is not None:
                  subset_idx = torch.nonzero(
                      subset_mask[key_padding_mask], as_tuple=False
                  ).flatten()
                  subset_seqlens = (subset_mask & key_padding_mask).sum(
                      dim=-1, dtype=torch.int32
                  )
                  subset_cu_seqlens = F.pad(
                      torch.cumsum(
                          subset_seqlens, dim=0, dtype=torch.torch.int32
                      ),
                      (1, 0),
                  )
              else:
                  subset_idx = torch.nonzero(
                      subset_mask, as_tuple=False
                  ).flatten()
                  subset_seqlens = subset_mask.sum(
                      dim=-1, dtype=torch.int32
                  )
                  subset_cu_seqlens = F.pad(
                      torch.cumsum(
                          subset_seqlens, dim=0, dtype=torch.torch.int32
                      ),
                      (1, 0),
                  )
              (
                  hidden_states_subset,
                  hidden_states,
              ) = index_first_axis_residual(hidden_states, subset_idx)
              # It's ok to set max_seqlen_q to be much larger
              mixer_kwargs = {
                  "x_kv": hidden_states,
                  "cu_seqlens": subset_cu_seqlens,
                  "max_seqlen": max_seqlen_in_batch,
                  "cu_seqlens_k": cu_seqlens,
                  "max_seqlen_k": max_seqlen_in_batch,
              }
              hidden_states = self.layers[-1](
                  hidden_states_subset, mixer_kwargs=mixer_kwargs
              )
      return hidden_states
  
imports: import logging
  import re
  from collections import OrderedDict
  from collections.abc import Sequence
  from functools import partial
  from typing import Any, Mapping
  import torch
  import torch.nn
  import torch.nn.functional
  from einops import rearrange
  from transformers import BertConfig, PretrainedConfig
  from transformers.models.bert.modeling_bert import BaseModelOutputWithPoolingAndCrossAttentions, BertForPreTrainingOutput
  from flash_attn.bert_padding import index_first_axis, index_first_axis_residual, pad_input, unpad_input
  from flash_attn.modules.block import Block
  from flash_attn.modules.embedding import BertEmbeddings
  from flash_attn.modules.mha import MHA
  from flash_attn.modules.mlp import FusedMLP, Mlp
  from flash_attn.utils.pretrained import state_dict_from_pretrained
label_desc: Use image node_id 2 for calling the BertEncoder obj's underlying member method code with example usage: obj.forward(hidden_states, key_padding_mask, subset_mask) and returns: hidden_states