docstr: Tests the compute_gradients correctness.
  
  Tests that if we sum all the trainable variables the gradient of output w.r.t.
  the weights is all ones.
docstr_tok: ["Tests","the","compute_gradients","correctness",".","Tests","that","if","we","sum","all","the","trainable","variables","the","gradient","of","output","w.r.t",".","the","weights","is","all","ones","."]
code: def test_compute_gradients(self):
      """Tests the compute_gradients correctness.
  
      Tests that if we sum all the trainable variables the gradient of output w.r.t.
      the weights is all ones.
      """
      config = BaseTestingAlgorithmConfig()
  
      for fw in framework_iterator(config, frameworks=("torch", "tf2")):
          learner = config.build_learner(env=self.ENV)
  
          params = learner.get_parameters(
              learner.module[DEFAULT_POLICY_ID]
          )
  
          tape = None
          if fw == "torch":
              loss_per_module = {
                  ALL_MODULES: sum(param.sum() for param in params)
              }
          else:
              with tf.GradientTape() as tape:
                  loss_per_module = {
                      ALL_MODULES: sum(
                          tf.reduce_sum(param) for param in params
                      )
                  }
  
          gradients = learner.compute_gradients(
              loss_per_module, gradient_tape=tape
          )
  
          # Type should be a mapping from ParamRefs to gradients.
          self.assertIsInstance(gradients, dict)
  
          for grad in gradients.values():
              check(grad, np.ones(grad.shape))
  
imports: import gymnasium
  import numpy
  import tempfile
  import unittest
  import ray
  from ray.rllib.core.learner.learner import Learner
  from ray.rllib.core.testing.testing_learner import BaseTestingAlgorithmConfig
  from ray.rllib.policy.sample_batch import DEFAULT_POLICY_ID
  from ray.rllib.utils.numpy import convert_to_numpy
  from ray.rllib.utils.framework import try_import_tf, try_import_torch
  from ray.rllib.utils.test_utils import check, framework_iterator, get_cartpole_dataset_reader
  from ray.rllib.utils.metrics import ALL_MODULES
label_desc: Use image node_id 4 for calling the TestLearner obj's underlying member method code with example usage: obj.test_compute_gradients() without return types