class_docstr: This class defines the `MobileViTv2 block <>`_
class_docstr_tok: ["This","class","defines","the","`","MobileViTv2","block","<",">","`","_"]
code: def forward(self, x: torch.Tensor) -> torch.Tensor:
      B, C, H, W = x.shape
      patch_h, patch_w = self.patch_size
      new_h, new_w = (
          math.ceil(H / patch_h) * patch_h,
          math.ceil(W / patch_w) * patch_w,
      )
      num_patch_h, num_patch_w = (
          new_h // patch_h,
          new_w // patch_w,
      )  # n_h, n_w
      num_patches = num_patch_h * num_patch_w  # N
      if new_h != H or new_w != W:
          x = F.interpolate(
              x,
              size=(new_h, new_w),
              mode="bilinear",
              align_corners=True,
          )
  
      # Local representation
      x = self.conv_kxk(x)
      x = self.conv_1x1(x)
  
      # Unfold (feature map -> patches), [B, C, H, W] -> [B, C, P, N]
      C = x.shape[1]
      if self.coreml_exportable:
          x = F.unfold(
              x,
              kernel_size=(patch_h, patch_w),
              stride=(patch_h, patch_w),
          )
      else:
          x = x.reshape(
              B, C, num_patch_h, patch_h, num_patch_w, patch_w
          ).permute(0, 1, 3, 5, 2, 4)
      x = x.reshape(B, C, -1, num_patches)
  
      # Global representations
      x = self.transformer(x)
      x = self.norm(x)
  
      # Fold (patches -> feature map), [B, C, P, N] --> [B, C, H, W]
      if self.coreml_exportable:
          # adopted from https://github.com/apple/ml-cvnets/blob/main/cvnets/modules/mobilevit_block.py#L609-L624
          x = x.reshape(
              B, C * patch_h * patch_w, num_patch_h, num_patch_w
          )
          x = F.pixel_shuffle(x, upscale_factor=patch_h)
      else:
          x = x.reshape(
              B, C, patch_h, patch_w, num_patch_h, num_patch_w
          ).permute(0, 1, 4, 2, 5, 3)
          x = x.reshape(
              B, C, num_patch_h * patch_h, num_patch_w * patch_w
          )
  
      x = self.conv_proj(x)
      return x
  
imports: import math
  from typing import Callable, Tuple, Optional
  import torch
  import torch.nn.functional
  from torch import nn
  from timm.layers import to_2tuple, make_divisible, GroupNorm1, ConvMlp, DropPath, is_exportable
  from ._builder import build_model_with_cfg
  from ._features_fx import register_notrace_module
  from ._registry import register_model, generate_default_cfgs, register_model_deprecations
  from .byobnet import register_block, ByoBlockCfg, ByoModelCfg, ByobNet, LayerFn, num_groups
  from .vision_transformer import Block
label_desc: Use image node_id 2 for calling the MobileVitV2Block obj's underlying member method code with example usage: obj.forward(x) and returns: x