code: def test_custom_loss_gradient_modular(
      get_default_mnist_subset, image_dl_estimator, device_type
  ):
      (_, _), (x_test_mnist, y_test_mnist) = get_default_mnist_subset
      classifier, _ = image_dl_estimator(functional=False)
      loss_function = torch.norm
      poison_image = x_test_mnist[0:1]
      target_image = x_test_mnist[1:2]
  
      layer = -2
      layer_name = classifier.layer_names[layer]
      custom_grad = classifier.custom_loss_gradient(
          loss_function, poison_image, target_image, layer_name
      )
      device = torch.device(
          "cuda" if torch.cuda.is_available() else "cpu"
      )
      target_image = torch.tensor(target_image).to(device)
      poison_image = torch.tensor(poison_image).to(device)
      poison_image.requires_grad = True
  
      new_model = nn.Sequential(
          *list(classifier.model.children())[: layer + 1]
      )
      f_x = new_model(poison_image)
      f_t = new_model(target_image)
      diff = f_x - f_t
      loss = loss_function(diff, p=2)
      self_grad = torch.autograd.grad(loss, [poison_image])[0]
  
      np.testing.assert_array_almost_equal(
          custom_grad, self_grad, decimal=4
      )
  
imports: import numpy
  import pytest
  import torch
  import torch.nn.functional
  import torch.nn
  import torch.optim
  import sklearn.datasets
  from art.estimators.classification.pytorch import PyTorchClassifier
  from art.defences.preprocessor.spatial_smoothing import SpatialSmoothing
  from art.defences.preprocessor.spatial_smoothing_pytorch import SpatialSmoothingPyTorch
  from art.attacks.evasion.fast_gradient import FastGradientMethod
  from art.attacks.evasion.projected_gradient_descent.projected_gradient_descent import ProjectedGradientDescent
  from tests.attacks.utils import backend_test_defended_images
  from tests.utils import ARTTestException
label_desc: Use image node_id 12 for calling a global function with example usage: test_custom_loss_gradient_modular(get_default_mnist_subset, image_dl_estimator, device_type) without return types