code: def test_optimize_rng_use_under_jit(
      self, module_fn: ModuleFn, shape, dtype
  ):
      rng = jax.random.PRNGKey(42)
      if jnp.issubdtype(dtype, jnp.integer):
          x = jax.random.randint(rng, shape, 0, np.prod(shape), dtype)
      else:
          x = jax.random.uniform(rng, shape, dtype)
  
      def g(x):
          return module_fn()(x)
  
      f = hk.transform_with_state(hk.experimental.optimize_rng_use(g))
  
      module_type = descriptors.module_type(module_fn)
      atol = CUSTOM_ATOL.get(module_type, DEFAULT_ATOL)
      assert_allclose = functools.partial(
          np.testing.assert_allclose, atol=atol
      )
  
      params, state = jax.jit(f.init)(rng, x)
      jax.tree_util.tree_map(
          assert_allclose, (params, state), f.init(rng, x)
      )
  
      if module_type in (
          hk.nets.VectorQuantizer,
          hk.nets.VectorQuantizerEMA,
      ):
          # For stochastic modules just test apply runs.
          jax.device_get(jax.jit(f.apply)(params, state, rng, x))
  
      else:
          jax.tree_util.tree_map(
              assert_allclose,
              jax.jit(f.apply)(params, state, rng, x),
              f.apply(params, state, rng, x),
          )
  
file_path: dm-haiku/haiku/_src/integration/hk_transforms_test.py
imports: import functools
  from absl.testing import absltest
  from absl.testing import parameterized
  import haiku
  from haiku._src import stateful
  from haiku._src import test_utils
  from haiku._src.integration import descriptors
  import jax
  import jax.numpy
  import numpy
label_desc: Use image node_id 4 for calling the HaikuTransformsTest obj's underlying member method code with example usage: obj.test_optimize_rng_use_under_jit(module_fn, shape, dtype) and returns: module_fn