docstr: Test inference methods.
docstr_tok: ["Test","inference","methods","."]
code: def test_inference_methods(self):
      """Test inference methods."""
  
      config = (
          DTConfig()
          .environment(
              env="Pendulum-v1",
              clip_actions=True,
              normalize_actions=True,
          )
          .framework("torch")
          .training(
              train_batch_size=200,
              replay_buffer_config={
                  "capacity": 8,
              },
              model={
                  "max_seq_len": 3,
              },
              num_layers=1,
              num_heads=1,
              embed_dim=64,
              horizon=200,
          )
          .evaluation(
              target_return=-120,
          )
          .rollouts(
              num_rollout_workers=0,
          )
          .experimental(_disable_preprocessor_api=True)
      )
      algo = config.build()
  
      # Do a controlled fake rollout for 2 steps and check input_dict
      # first input_dict
      obs = np.array([0.0, 1.0, 2.0])
  
      input_dict = algo.get_initial_input_dict(obs)
      target = SampleBatch(
          {
              SampleBatch.OBS: np.array(
                  [
                      [0.0, 0.0, 0.0],
                      [0.0, 0.0, 0.0],
                      [0.0, 1.0, 2.0],
                  ],
                  dtype=np.float32,
              ),
              SampleBatch.ACTIONS: np.array(
                  [[0.0], [0.0]], dtype=np.float32
              ),
              SampleBatch.RETURNS_TO_GO: np.array(
                  [0.0, 0.0], dtype=np.float32
              ),
              SampleBatch.REWARDS: np.zeros((), dtype=np.float32),
              SampleBatch.T: np.array([-1, -1], dtype=np.int32),
          }
      )
      _assert_input_dict_equals(input_dict, target)
  
      # forward pass with first input_dict
      action, _, extra = algo.compute_single_action(
          input_dict=input_dict
      )
      assert action.shape == (1,)
      assert SampleBatch.RETURNS_TO_GO in extra
      assert np.isclose(extra[SampleBatch.RETURNS_TO_GO], -120.0)
  
      # second input_dict
      action = np.array([0.5])
      obs = np.array([3.0, 4.0, 5.0])
      reward = -10.0
  
      input_dict = algo.get_next_input_dict(
          input_dict,
          action,
          reward,
          obs,
          extra,
      )
      target = SampleBatch(
          {
              SampleBatch.OBS: np.array(
                  [
                      [0.0, 0.0, 0.0],
                      [0.0, 1.0, 2.0],
                      [3.0, 4.0, 5.0],
                  ],
                  dtype=np.float32,
              ),
              SampleBatch.ACTIONS: np.array(
                  [[0.0], [0.5]], dtype=np.float32
              ),
              SampleBatch.RETURNS_TO_GO: np.array(
                  [0.0, -120.0], dtype=np.float32
              ),
              SampleBatch.REWARDS: np.asarray(-10.0),
              SampleBatch.T: np.array([-1, 0], dtype=np.int32),
          }
      )
      _assert_input_dict_equals(input_dict, target)
  
      # forward pass with second input_dict
      action, _, extra = algo.compute_single_action(
          input_dict=input_dict
      )
      assert action.shape == (1,)
      assert SampleBatch.RETURNS_TO_GO in extra
      assert np.isclose(extra[SampleBatch.RETURNS_TO_GO], -110.0)
  
      # third input_dict
      action = np.array([-0.2])
      obs = np.array([6.0, 7.0, 8.0])
      reward = -20.0
  
      input_dict = algo.get_next_input_dict(
          input_dict,
          action,
          reward,
          obs,
          extra,
      )
      target = SampleBatch(
          {
              SampleBatch.OBS: np.array(
                  [
                      [0.0, 1.0, 2.0],
                      [3.0, 4.0, 5.0],
                      [6.0, 7.0, 8.0],
                  ],
                  dtype=np.float32,
              ),
              SampleBatch.ACTIONS: np.array(
                  [[0.5], [-0.2]], dtype=np.float32
              ),
              SampleBatch.RETURNS_TO_GO: np.array(
                  [-120, -110.0], dtype=np.float32
              ),
              SampleBatch.REWARDS: np.asarray(-20.0),
              SampleBatch.T: np.array([0, 1], dtype=np.int32),
          }
      )
      _assert_input_dict_equals(input_dict, target)
  
file_path: ray/rllib_contrib/dt/tests/test_dt.py
imports: import unittest
  from typing import Dict
  import gymnasium
  import numpy
  from rllib_dt.dt.dt import DTConfig
  import ray
  from ray.rllib import SampleBatch
  from ray.rllib.utils.framework import try_import_tf, try_import_torch
  from ray.rllib.utils.test_utils import check_train_results
label_desc: Use image node_id 4 for calling the TestDT obj's underlying member method code with example usage: obj.test_inference_methods() without return types