class_docstr: This class defines the `ByteFormer <https://arxiv.org/pdf/2306.00238.pdf>`_
  architecture.
class_docstr_tok: ["This","class","defines","the","`","ByteFormer","<","https",":","\/\/arxiv.org\/pdf\/2306.00238.pdf",">","`","_","architecture","."]
docstr: Apply the portion of the network used to reduce sequence lengths before
  the transformer backbone.
  
  Args:
      x: The input token embeddings of shape [batch_size, sequence_length,
          embed_dim].
      x_mask: The input mask of shape [batch_size, sequence_length].
  
  Returns:
      New versions of @x and @x_mask, downsampled along the sequence
      dimension by the token reduction net.
docstr_tok: ["Apply","the","portion","of","the","network","used","to","reduce","sequence","lengths","before","the","transformer","backbone",".","Args",":","x",":","The","input","token","embeddings","of","shape","[","batch_size",",","sequence_length",",","embed_dim","]",".","x_mask",":","The","input","mask","of","shape","[","batch_size",",","sequence_length","]",".","Returns",":","New","versions","of","@","x","and","@","x_mask",",","downsampled","along","the","sequence","dimension","by","the","token","reduction","net","."]
code: def apply_token_reduction_net(
      self, x: Tensor, x_mask: Tensor
  ) -> Tuple[Tensor, Tensor]:
      """
      Apply the portion of the network used to reduce sequence lengths before
      the transformer backbone.
  
      Args:
          x: The input token embeddings of shape [batch_size, sequence_length,
              embed_dim].
          x_mask: The input mask of shape [batch_size, sequence_length].
  
      Returns:
          New versions of @x and @x_mask, downsampled along the sequence
          dimension by the token reduction net.
      """
      B, N, C = x.shape
      if self.token_reduction_net is None:
          return x, x_mask
  
      x = self.token_reduction_net(x.permute(0, 2, 1)).permute(0, 2, 1)
      if x_mask is not None:
          x_mask = unfold_tokens(
              x_mask.reshape(B, N, 1).float(), self.conv_kernel_size
          )
          # The mask is now [B * N, kernel_size, 1]. It contains values in {0, -inf}.
          x_mask = x_mask.max(dim=1).values.view(x.shape[0], x.shape[1])
  
          assert x.shape[:2] == x_mask.shape
      return x, x_mask
  
imports: import argparse
  import math
  from typing import Dict, Optional, Tuple, Union
  import numpy
  import torch
  from torch import Tensor, nn
  from torch.nn import init
  from cvnets.layers import LinearLayer, embedding, get_normalization_layer, normalization, positional_embedding, token_merging
  from cvnets.models import MODEL_REGISTRY, BaseAnyNNModel
  from cvnets.models.classification.config.byteformer import get_configuration
  from cvnets.modules import WindowedTransformerEncoder
label_desc: Use image node_id 4 for calling the ByteFormer obj's underlying member method code with example usage: obj.apply_token_reduction_net(x, x_mask) and returns: x,  x_mask, x,  x_mask