code: def test_custom_vjp(self):
      x = random.normal(random.key(0), (1, 4))
      y, variables = init(mlp_custom_grad)(random.key(1), x)
      param_shapes = unfreeze(
          jax.tree_util.tree_map(jnp.shape, variables["params"])
      )
      loss_fn = lambda p, x: jnp.mean(apply(mlp_custom_grad)(p, x) ** 2)
      grad = jax.grad(loss_fn)(variables, x)
      grad_shapes = unfreeze(
          jax.tree_util.tree_map(jnp.shape, grad["params"])
      )
      self.assertEqual(y.shape, (1, 1))
      expected_param_shapes = {
          "hidden_0": {"kernel": (4, 8), "bias": (8,)},
          "out": {"kernel": (8, 1), "bias": (1,)},
      }
      self.assertEqual(param_shapes, expected_param_shapes)
      self.assertEqual(grad_shapes, expected_param_shapes)
      for g in jax.tree_util.tree_leaves(grad):
          self.assertTrue(np.all(g == np.sign(g)))
  
file_path: flax/tests/core/design/core_custom_vjp_test.py
imports: from functools import partial
  from typing import Callable, Sequence
  import jax
  import numpy
  from absl.testing import absltest
  from jax import numpy
  from jax import random
  from flax.core import Array, Scope, apply, init, lift, nn, unfreeze
label_desc: Use image node_id 1 for calling the CustomVJPTest obj's underlying member method code with example usage: obj.test_custom_vjp() without return types