docstr: Test Nonlocal model builder.
docstr_tok: ["Test","Nonlocal","model","builder","."]
code: def test_build_nonlocal(self):
      """
      Test Nonlocal model builder.
      """
      for (
          dim_in,
          dim_inner,
          pool,
          norm,
          instantiation,
      ) in itertools.product(
          (4, 8),
          (2, 4),
          (None, nn.MaxPool3d(2)),
          (None, nn.BatchNorm3d),
          ("dot_product", "softmax"),
      ):
          model = NonLocal(
              conv_theta=nn.Conv3d(
                  dim_in, dim_inner, kernel_size=1, stride=1, padding=0
              ),
              conv_phi=nn.Conv3d(
                  dim_in, dim_inner, kernel_size=1, stride=1, padding=0
              ),
              conv_g=nn.Conv3d(
                  dim_in, dim_inner, kernel_size=1, stride=1, padding=0
              ),
              conv_out=nn.Conv3d(
                  dim_inner, dim_in, kernel_size=1, stride=1, padding=0
              ),
              pool=pool,
              norm=norm(dim_in) if norm is not None else None,
              instantiation=instantiation,
          )
  
          # Test forwarding.
          for input_tensor in TestNonlocal._get_inputs(
              input_dim=dim_in
          ):
              if input_tensor.shape[1] != dim_in:
                  with self.assertRaises(RuntimeError):
                      output_tensor = model(input_tensor)
                  continue
              else:
                  output_tensor = model(input_tensor)
  
              input_shape = input_tensor.shape
              output_shape = output_tensor.shape
  
              self.assertEqual(
                  input_shape,
                  output_shape,
                  "Input shape {} is different from output shape {}".format(
                      input_shape, output_shape
                  ),
              )
  
file_path: pytorchvideo/tests/test_layers_nonlocal_net.py
imports: import itertools
  import unittest
  from typing import Iterable
  import numpy
  import torch
  from pytorchvideo.layers.nonlocal_net import create_nonlocal, NonLocal
  from torch import nn
label_desc: Use image node_id 2 for calling the TestNonlocal obj's underlying member method code with example usage: obj.test_build_nonlocal() without return types