code: def compute_gradients(
      policy, optimizer: LocalOptimizer, loss: TensorType
  ) -> ModelGradients:
      # Compute the gradients.
      variables = policy.model.trainable_variables
      if isinstance(policy.model, ModelV2):
          variables = variables()
      grads_and_vars = optimizer.compute_gradients(loss, variables)
  
      # Clip by global norm, if necessary.
      if policy.config.get("grad_clip") is not None:
          # Defuse inf gradients (due to super large losses).
          grads = [g for (g, v) in grads_and_vars]
          grads, _ = tf.clip_by_global_norm(
              grads, policy.config["grad_clip"]
          )
          # If the global_norm is inf -> All grads will be NaN. Stabilize this
          # here by setting them to 0.0. This will simply ignore destructive loss
          # calculations.
          policy.grads = []
          for g in grads:
              if g is not None:
                  policy.grads.append(
                      tf.where(tf.math.is_nan(g), tf.zeros_like(g), g)
                  )
              else:
                  policy.grads.append(None)
          clipped_grads_and_vars = list(zip(policy.grads, variables))
          return clipped_grads_and_vars
      else:
          return grads_and_vars
  
imports: import logging
  from typing import Dict, List
  import numpy
  from ray.rllib.models.modelv2 import ModelV2
  from ray.rllib.policy.eager_tf_policy import EagerTFPolicy
  from ray.rllib.policy.eager_tf_policy_v2 import EagerTFPolicyV2
  from ray.rllib.policy.policy import Policy, PolicyState
  from ray.rllib.policy.sample_batch import SampleBatch
  from ray.rllib.policy.tf_policy import TFPolicy
  from ray.rllib.utils.annotations import DeveloperAPI, override
  from ray.rllib.utils.framework import get_variable, try_import_tf
  from ray.rllib.utils.schedules import PiecewiseSchedule
  from ray.rllib.utils.tf_utils import make_tf_callable
  from ray.rllib.utils.typing import AlgorithmConfigDict, LocalOptimizer, ModelGradients, TensorType
label_desc: Use image node_id 1 for calling a global function with example usage: compute_gradients(policy, optimizer, loss) and returns: clipped_grads_and_vars, grads_and_vars