class_docstr: This class tests the crypten.nn package.
class_docstr_tok: ["This","class","tests","the","crypten.nn","package","."]
docstr: Tests crypten.nn.Conv module.
code: def test_conv(self):
      """
      Tests crypten.nn.Conv module.
      """
  
      # try different dimensionalities:
      for dim in range(1, 3):
          for compute_gradients in [True, False]:
              # fixed attributes of convolution:
              stride = tuple([1] * dim)
              padding = tuple([0] * dim)
              dilation = tuple([1] * dim)
              groups = 1
  
              # generate input:
              in_channels, out_channels = 4, 5
              size = tuple([1, in_channels] + ([16] * dim))
              input = get_random_test_tensor(size=size, is_float=True)
              input.requires_grad = True
              encr_input = crypten.cryptensor(input)
              encr_input.requires_grad = compute_gradients
  
              # generate kernel:
              kernel_shape = tuple(
                  [out_channels, in_channels] + ([3] * dim)
              )
              kernel = get_random_test_tensor(
                  size=kernel_shape, is_float=True
              )
              kernel.requires_grad = True
              encr_kernel = crypten.cryptensor(kernel)
              encr_kernel.requires_grad = compute_gradients
  
              # create "encrypted" CrypTen module:
              module = crypten.nn.Conv(
                  stride, padding, dilation, groups
              )
              module.encrypt()
  
              # compute PyTorch output:
              func = getattr(torch.nn.functional, f"conv{dim}d", None)
              reference = func(
                  input,
                  kernel,
                  None,
                  stride=stride,
                  padding=padding,
                  dilation=dilation,
                  groups=groups,
              )
  
              # compare model outputs:
              encr_output = module((encr_input, encr_kernel))
              self._check(encr_output, reference, "Conv forward failed")
  
              # test backward pass:
              reference.backward(torch.ones(reference.size()))
              encr_output.backward(
                  encr_output.new(torch.ones(encr_output.size()))
              )
              if compute_gradients:
                  self._check(
                      encr_input.grad,
                      input.grad,
                      "Conv backward on input failed",
                  )
                  self._check(
                      encr_kernel.grad,
                      kernel.grad,
                      "Conv backward on weight failed",
                  )
              else:
                  self.assertIsNone(encr_input.grad)
                  self.assertIsNone(encr_kernel.grad)
  
imports: import copy
  import itertools
  import logging
  import unittest
  import crypten
  import crypten.communicator
  import torch
  import torch.nn.functional
  from crypten.common.rng import generate_random_ring_element
  from crypten.common.tensor_types import is_float_tensor
  from crypten.config import cfg
  from crypten.encoder import FixedPointEncoder
  from test.multiprocess_test_case import get_random_linear, get_random_test_tensor, MultiProcessTestCase, onehot
label_desc: Use image node_id 10 for calling the TestNN obj's underlying member method code with example usage: obj.test_conv() without return types