code: def compute_advantages(
      rollout: SampleBatch,
      last_r: float,
      gamma: float = 0.9,
      lambda_: float = 1.0,
      use_gae: bool = True,
      use_critic: bool = True,
      rewards: TensorType = None,
      vf_preds: TensorType = None,
  ):
      """Given a rollout, compute its value targets and the advantages.
  
      Args:
          rollout: SampleBatch of a single trajectory.
          last_r: Value estimation for last observation.
          gamma: Discount factor.
          lambda_: Parameter for GAE.
          use_gae: Using Generalized Advantage Estimation.
          use_critic: Whether to use critic (value estimates). Setting
              this to False will use 0 as baseline.
          rewards: Override the reward values in rollout.
          vf_preds: Override the value function predictions in rollout.
  
      Returns:
          SampleBatch with experience from rollout and processed rewards.
      """
      assert (
          SampleBatch.VF_PREDS in rollout or not use_critic
      ), "use_critic=True but values not found"
      assert (
          use_critic or not use_gae
      ), "Can't use gae without using a value function"
      last_r = convert_to_numpy(last_r)
  
      if rewards is None:
          rewards = rollout[SampleBatch.REWARDS]
      if vf_preds is None and use_critic:
          vf_preds = rollout[SampleBatch.VF_PREDS]
  
      if use_gae:
          vpred_t = np.concatenate([vf_preds, np.array([last_r])])
          delta_t = rewards + gamma * vpred_t[1:] - vpred_t[:-1]
          # This formula for the advantage comes from:
          # "Generalized Advantage Estimation": https://arxiv.org/abs/1506.02438
          rollout[Postprocessing.ADVANTAGES] = discount_cumsum(
              delta_t, gamma * lambda_
          )
          rollout[Postprocessing.VALUE_TARGETS] = (
              rollout[Postprocessing.ADVANTAGES] + vf_preds
          ).astype(np.float32)
      else:
          rewards_plus_v = np.concatenate([rewards, np.array([last_r])])
          discounted_returns = discount_cumsum(rewards_plus_v, gamma)[
              :-1
          ].astype(np.float32)
  
          if use_critic:
              rollout[Postprocessing.ADVANTAGES] = (
                  discounted_returns - vf_preds
              )
              rollout[Postprocessing.VALUE_TARGETS] = discounted_returns
          else:
              rollout[Postprocessing.ADVANTAGES] = discounted_returns
              rollout[Postprocessing.VALUE_TARGETS] = np.zeros_like(
                  rollout[Postprocessing.ADVANTAGES]
              )
  
      rollout[Postprocessing.ADVANTAGES] = rollout[
          Postprocessing.ADVANTAGES
      ].astype(np.float32)
  
      return rollout
  
file_path: ray/rllib/evaluation/postprocessing.py
imports: import numpy
  import scipy.signal
  from typing import Dict, Optional
  from ray.rllib.evaluation.episode import Episode
  from ray.rllib.policy.policy import Policy
  from ray.rllib.policy.sample_batch import SampleBatch
  from ray.rllib.utils.annotations import DeveloperAPI
  from ray.rllib.utils.nested_dict import NestedDict
  from ray.rllib.utils.numpy import convert_to_numpy
  from ray.rllib.utils.torch_utils import convert_to_torch_tensor
  from ray.rllib.utils.typing import AgentID
  from ray.rllib.utils.typing import TensorType
label_desc: Use image node_id 2 for calling a global function with example usage: compute_advantages(rollout, last_r, gamma, lambda_, use_gae, use_critic, rewards, vf_preds) and returns: rollout