code: def _for_loop(
      *,
      dim,
      steps_num,
      current_state,
      drift_fn,
      volatility_fn,
      grad_volatility_fn,
      wiener_mean,
      watch_params,
      num_samples,
      times,
      dt,
      sqrt_dt,
      time_indices,
      keep_mask,
      random_type,
      seed,
      normal_draws,
      input_gradients,
      stratonovich_order,
      aux_normal_draws,
  ):
      """Sample paths using custom for_loop."""
      num_time_points = time_indices.shape.as_list()[-1]
      if num_time_points == 1:
          iter_nums = steps_num
      else:
          iter_nums = time_indices
  
      def step_fn(i, current_state):
          # Unpack current_state
          current_state = current_state[0]
          _, _, next_state, _ = _milstein_step(
              dim=dim,
              i=i,
              written_count=0,
              current_state=current_state,
              result=tf.expand_dims(current_state, axis=1),
              drift_fn=drift_fn,
              volatility_fn=volatility_fn,
              grad_volatility_fn=grad_volatility_fn,
              wiener_mean=wiener_mean,
              num_samples=num_samples,
              times=times,
              dt=dt,
              sqrt_dt=sqrt_dt,
              keep_mask=keep_mask,
              random_type=random_type,
              seed=seed,
              normal_draws=normal_draws,
              input_gradients=input_gradients,
              stratonovich_order=stratonovich_order,
              aux_normal_draws=aux_normal_draws,
              record_samples=False,
          )
          return [next_state]
  
      result = custom_loops.for_loop(
          body_fn=step_fn,
          initial_state=[current_state],
          params=watch_params,
          num_iterations=iter_nums,
      )[0]
      if num_time_points == 1:
          return tf.expand_dims(result, axis=1)
      return tf.transpose(result, (1, 0, 2))
  
imports: import functools
  import math
  from typing import Callable, List, Optional
  import numpy
  import tensorflow.compat.v2
  from tf_quant_finance import types
  from tf_quant_finance import utils
  from tf_quant_finance.math import custom_loops
  from tf_quant_finance.math import gradient
  from tf_quant_finance.math import random_ops
  from tf_quant_finance.models import utils
label_desc: Use image node_id 4 for calling a global function with example usage: _for_loop() and returns: tf, list, tf