docstr: Compare output of vectorized chamfer loss with naive implementation
  when batch_reduction in ["mean", "sum"] and
  point_reduction in ["mean", "sum"].
docstr_tok: ["Compare","output","of","vectorized","chamfer","loss","with","naive","implementation","when","batch_reduction","in","[","``","mean","''",",","``","sum","''","]","and","point_reduction","in","[","``","mean","''",",","``","sum","''","]","."]
code: def test_chamfer_joint_reduction(self):
      """
      Compare output of vectorized chamfer loss with naive implementation
      when batch_reduction in ["mean", "sum"] and
      point_reduction in ["mean", "sum"].
      """
      N, max_P1, max_P2 = 7, 10, 18
      device = get_random_cuda_device()
  
      points_normals = TestChamfer.init_pointclouds(
          N, max_P1, max_P2, device
      )
      p1 = points_normals.p1
      p2 = points_normals.p2
      p1_normals = points_normals.n1
      p2_normals = points_normals.n2
      weights = points_normals.weights
  
      P1 = p1.shape[1]
      P2 = p2.shape[1]
  
      pred_loss, pred_loss_norm = TestChamfer.chamfer_distance_naive(
          p1, p2, x_normals=p1_normals, y_normals=p2_normals
      )
  
      # batch_reduction = "sum", point_reduction = "sum".
      loss, loss_norm = chamfer_distance(
          p1,
          p2,
          x_normals=p1_normals,
          y_normals=p2_normals,
          weights=weights,
          batch_reduction="sum",
          point_reduction="sum",
      )
      pred_loss[0] *= weights.view(N, 1)
      pred_loss[1] *= weights.view(N, 1)
      pred_loss_sum = pred_loss[0].sum(1) + pred_loss[1].sum(
          1
      )  # point sum
      pred_loss_sum = pred_loss_sum.sum()  # batch sum
      self.assertClose(loss, pred_loss_sum)
  
      pred_loss_norm[0] *= weights.view(N, 1)
      pred_loss_norm[1] *= weights.view(N, 1)
      pred_loss_norm_sum = pred_loss_norm[0].sum(1) + pred_loss_norm[
          1
      ].sum(
          1
      )  # point sum.
      pred_loss_norm_sum = pred_loss_norm_sum.sum()  # batch sum
      self.assertClose(loss_norm, pred_loss_norm_sum)
  
      # batch_reduction = "mean", point_reduction = "sum".
      loss, loss_norm = chamfer_distance(
          p1,
          p2,
          x_normals=p1_normals,
          y_normals=p2_normals,
          weights=weights,
          batch_reduction="mean",
          point_reduction="sum",
      )
      pred_loss_sum /= weights.sum()
      self.assertClose(loss, pred_loss_sum)
  
      pred_loss_norm_sum /= weights.sum()
      self.assertClose(loss_norm, pred_loss_norm_sum)
  
      # batch_reduction = "sum", point_reduction = "mean".
      loss, loss_norm = chamfer_distance(
          p1,
          p2,
          x_normals=p1_normals,
          y_normals=p2_normals,
          weights=weights,
          batch_reduction="sum",
          point_reduction="mean",
      )
      pred_loss_mean = (
          pred_loss[0].sum(1) / P1 + pred_loss[1].sum(1) / P2
      )
      pred_loss_mean = pred_loss_mean.sum()
      self.assertClose(loss, pred_loss_mean)
  
      pred_loss_norm_mean = (
          pred_loss_norm[0].sum(1) / P1 + pred_loss_norm[1].sum(1) / P2
      )
      pred_loss_norm_mean = pred_loss_norm_mean.sum()
      self.assertClose(loss_norm, pred_loss_norm_mean)
  
      # batch_reduction = "mean", point_reduction = "mean". This is the default.
      loss, loss_norm = chamfer_distance(
          p1,
          p2,
          x_normals=p1_normals,
          y_normals=p2_normals,
          weights=weights,
          batch_reduction="mean",
          point_reduction="mean",
      )
      pred_loss_mean /= weights.sum()
      self.assertClose(loss, pred_loss_mean)
  
      pred_loss_norm_mean /= weights.sum()
      self.assertClose(loss_norm, pred_loss_norm_mean)
  
      # Error when batch_reduction is not in ["mean", "sum"] or None.
      with self.assertRaisesRegex(
          ValueError, "batch_reduction must be one of"
      ):
          chamfer_distance(
              p1, p2, weights=weights, batch_reduction="max"
          )
  
      # Error when point_reduction is not in ["mean", "sum"] or None.
      with self.assertRaisesRegex(
          ValueError, "point_reduction must be one of"
      ):
          chamfer_distance(
              p1, p2, weights=weights, point_reduction="max"
          )
  
imports: import unittest
  from collections import namedtuple
  import numpy
  import torch
  import torch.nn.functional
  from pytorch3d.loss import chamfer_distance
  from pytorch3d.structures.pointclouds import Pointclouds
  from .common_testing import get_random_cuda_device, TestCaseMixin
label_desc: Use image node_id 18 for calling the TestChamfer obj's underlying member method code with example usage: obj.test_chamfer_joint_reduction() without return types