code: def sample_equal_sequence(
      batch,
      num_steps: int,
      is_training: bool,
      output_feature_lists_name: str,
      output_raw_timestamp_name: str,
      output_raw_duration_name: str,
      output_raw_string_name: str,
      p: float,
      t: int,
      preserve: bool,
      asr_input: bool,
      max_segments: int,
  ) -> tf.Tensor:
      """Samples at equal distance num_steps features + pad + random temporal crop."""
  
      if is_training and p > 0:
          # Random temporal cropping with proba p, preservering or not events.
          def random_crop(batch, preserve=True):
              # Load batch.
              sequence = batch[output_feature_lists_name]
              duration = batch[output_raw_duration_name]
              captions = batch[output_raw_string_name]
              start = batch[output_raw_timestamp_name + "_start"]
              end = batch[output_raw_timestamp_name + "_end"]
  
              # Sample start offset.
              sequence_length = tf.shape(input=sequence)[0]
              if preserve:
                  max_offset = (
                      tf.cast(
                          tf.math.reduce_min(end / t), dtype=tf.int32
                      )
                      - 1
                  )
              else:
                  max_offset = sequence_length
              max_offset = tf.minimum(max_offset, sequence_length)
              max_offset = tf.maximum(max_offset, 1)
              offset_start = tf.random.uniform(
                  (), maxval=max_offset, dtype=tf.int32
              )
  
              # Modify captions/start/end given the sampled start.
              start = tf.cast(start, tf.int32) - offset_start * t
              end = tf.cast(end, tf.int32) - offset_start * t
              idx_to_keep = tf.where(end > 0)[:, 0]
              captions = tf.gather(captions, idx_to_keep, axis=0)
              start = tf.gather(start, idx_to_keep, axis=0)
              start = tf.maximum(start, 0)
              end = tf.gather(end, idx_to_keep, axis=0)
  
              # Sample end offset.
              if preserve:
                  min_offset = (
                      tf.cast(
                          tf.math.reduce_max(start / t), dtype=tf.int32
                      )
                      + 1
                  )
              else:
                  min_offset = offset_start
              maxval = sequence_length
              if (
                  max_segments
              ):  # only consider a given maximum number of segments
                  maxval = tf.math.reduce_max(end[:max_segments] // t)
                  maxval = tf.minimum(maxval, sequence_length)
                  maxval = tf.maximum(maxval, min_offset + 1)
              min_offset = tf.minimum(min_offset, maxval - 1)
              offset_end = tf.random.uniform(
                  (), minval=min_offset, maxval=maxval, dtype=tf.int32
              )
  
              # Modify captions/start/end given the sampled end.
              idx_to_keep = tf.where(start < offset_end * t)[:, 0]
              captions = tf.gather(captions, idx_to_keep, axis=0)
              start = tf.gather(start, idx_to_keep, axis=0)
              end = tf.gather(end, idx_to_keep, axis=0)
              end = tf.minimum(end, offset_end * t)
  
              # Modify sequence and duration given the sampled offsets.
              sequence = sequence[offset_start : offset_end + 1]
              duration = (offset_end - offset_start + 1) * t
  
              # Correct dimensions and types.
              duration = tf.cast(duration, tf.int64)[None]
              start = tf.cast(start, tf.int64)
              end = tf.cast(end, tf.int64)
  
              if asr_input:
                  # Modify ASR given the sampled start.
                  asr = batch["asr_string"]
                  asr_st = batch["asr_start"]
                  asr_ed = batch["asr_end"]
                  asr_st = tf.cast(asr_st, tf.int32) - offset_start * t
                  asr_ed = tf.cast(asr_ed, tf.int32) - offset_start * t
                  asr_idx = tf.where(asr_ed > 0)[:, 0]
                  asr = tf.gather(asr, asr_idx, axis=0)
                  asr_st = tf.gather(asr_st, asr_idx, axis=0)
                  asr_ed = tf.gather(asr_ed, asr_idx, axis=0)
                  # Modify ASR given the sampled end.
                  asr_idx = tf.where(asr_st < offset_end * t)[:, 0]
                  asr = tf.gather(asr, asr_idx, axis=0)
                  asr_st = tf.gather(asr_st, asr_idx, axis=0)
                  asr_ed = tf.gather(asr_ed, asr_idx, axis=0)
                  asr_st = tf.cast(asr_st, tf.int64)
                  asr_ed = tf.cast(asr_ed, tf.int64)
                  return (
                      sequence,
                      duration,
                      captions,
                      start,
                      end,
                      asr,
                      asr_st,
                      asr_ed,
                  )
  
              return sequence, duration, captions, start, end
  
          def no_crop(batch):
              if asr_input:
                  return (
                      batch[output_feature_lists_name],
                      batch[output_raw_duration_name],
                      batch[output_raw_string_name],
                      batch[output_raw_timestamp_name + "_start"],
                      batch[output_raw_timestamp_name + "_end"],
                      batch["asr_string"],
                      batch["asr_start"],
                      batch["asr_end"],
                  )
              return (
                  batch[output_feature_lists_name],
                  batch[output_raw_duration_name],
                  batch[output_raw_string_name],
                  batch[output_raw_timestamp_name + "_start"],
                  batch[output_raw_timestamp_name + "_end"],
              )
  
          output = random_apply(
              func=functools.partial(random_crop, preserve=preserve),
              gunc=no_crop,
              p=p,
              x=batch,
          )
  
          # Update batch
          batch[output_feature_lists_name] = output[0]
          batch[output_raw_duration_name] = output[1]
          batch[output_raw_string_name] = output[2]
          batch[output_raw_timestamp_name + "_start"] = output[3]
          batch[output_raw_timestamp_name + "_end"] = output[4]
  
          if asr_input:
              batch["asr_string"] = output[5]
              batch["asr_start"] = output[6]
              batch["asr_end"] = output[7]
  
      sequence = batch[output_feature_lists_name]
      sequence_length = tf.shape(input=sequence)[0]
  
      # Pad or sample
      output = tf.cond(
          sequence_length < num_steps,
          lambda: processors.sample_or_pad_non_sorted_sequence(  # pylint: disable=g-long-lambda
              sequence, num_steps, 0, False
          ),
          lambda: processors.sample_linspace_sequence(
              sequence, num_steps, 1, 1
          ),
      )
  
      batch[output_feature_lists_name] = output
      return batch
  
imports: import functools
  from typing import Any, Mapping, Optional, Union
  from dmvr import builders
  from dmvr import processors
  from dmvr import tokenizers
  import gin
  from t5.data.preprocessors import DenoiseInputsFn
  from t5.data.preprocessors import DenoiseNoiseMaskFn
  from t5.data.preprocessors import DenoiseTargetsFn
  from t5.data.preprocessors import random_spans_noise_mask
  import tensorflow
label_desc: Use image node_id 9 for calling a global function with example usage: sample_equal_sequence(batch, num_steps, is_training, output_feature_lists_name, output_raw_timestamp_name, output_raw_duration_name, output_raw_string_name, p, t, preserve, asr_input, max_segments) and returns: batch, sequence,  duration,  captions,  start,  end, batch,  batch,  batch,  batch,  batch, sequence,  duration,  captions,  start,  end,  asr,  asr_st,  asr_ed, batch,  batch,  batch,  batch,  batch,  batch,  batch,  batch