code: def compute_tdt_betas_kernel(
      acts: torch.Tensor,
      duration_acts: torch.Tensor,
      denom: torch.Tensor,
      sigma: float,
      betas: torch.Tensor,
      llBackward: torch.Tensor,
      xlen: torch.Tensor,
      ylen: torch.Tensor,
      mlabels: torch.Tensor,  # [B, U]
      minibatch: int,
      maxT: int,
      maxU: int,
      alphabet_size: int,
      blank_: int,
      durations: torch.Tensor,
      num_durations: int,
  ):
      """
      Compute beta (backward variable) probabilities over the transduction step.
  
      Args:
          acts: Tensor of shape [B, T, U, V] flattened. Represents the logprobs activation tensor for tokens.
          duration_acts: Tensor of shape [B, T, U, D] flattened. Represents the logprobs activation tensor for duations.
          denom: Tensor of shape [B, T, U] flattened. Represents the denominator of the logprobs activation tensor
              across entire vocabulary.
          betas: Zero tensor of shape [B, T, U]. Will be updated inside the kernel with the backward variable
              probabilities.
          llBackward: Zero tensor of shape [B]. Represents the log-likelihood of the backward pass.
              Returned as the backward pass loss that is reduced by the optimizer.
          xlen: Vector of length B which contains the actual acoustic sequence lengths in the padded
              activation tensor.
          ylen: Vector of length B which contains the actual target sequence lengths in the padded
              activation tensor.
          mlabels: Matrix of shape [B, U+1] (+1 here is due to <SOS> token - usually the RNNT blank).
              The matrix contains the padded target transcription that must be predicted.
          minibatch: Int representing the batch size.
          maxT: The maximum possible acoustic sequence length. Represents T in the logprobs tensor.
          maxU: The maximum possible target sequence length. Represents U in the logprobs tensor.
          alphabet_size: The vocabulary dimension V+1 (inclusive of RNNT blank).
          blank_: Index of the RNNT blank token in the vocabulary. Generally the first or last token in the vocab.
  
      Updates:
          Kernel inplace updates the following inputs:
          -   betas: backward variable scores.
          -   llBackward: log-likelihood of backward variable.
      """
      # // launch B blocks, each block has U threads
      b = cuda.blockIdx.x  # // batch id
      u = cuda.threadIdx.x  # label id, u
      T = xlen[b]  # select AM length of current sample
      U = (
          ylen[b] + 1
      )  # select target length of current sample, +1 for the blank token
  
      labels: torch.Tensor = mlabels[
          b
      ]  # mb label start point, equivalent to mlabels + b * (maxU - 1)
      offset = b * maxT * maxU  # pointer indexing offset
  
      # betas += offset # pointer offset, ignored since we explicitly add offset
  
      # Initilize beta[b, t=T-1, u=U-1] for all b in B with log_probs[b, t=T-1, u=U-1, blank]
      if u == 0:
          betas[offset + (T - 1) * maxU + U - 1] = (
              logp(
                  denom,
                  acts,
                  maxT,
                  maxU,
                  alphabet_size,
                  b,
                  T - 1,
                  U - 1,
                  blank_,
              )
              - sigma
              + logp_duration(
                  duration_acts,
                  maxT,
                  maxU,
                  num_durations,
                  b,
                  T - 1,
                  U - 1,
                  1,
              )
          )
  
      # sync until all betas are initialized
      cuda.syncthreads()
  
      # Ordinary beta calculations, broadcast across B=b and U=u
      # Look up backward variable calculation from rnnt_numpy.backward_pass()
      for n in range(T + U - 2, -1, -1):
          t = n - u
  
          if u == U - 1:
              # u == U - 1, we only consider blank emissions.
              if t >= 0 and t + 1 < T:
                  betas[offset + t * maxU + U - 1] = -INF
                  for i in range(1, num_durations):
                      # although similar, the computation for beta's is slightly more complex for boundary cases.
                      # the following two cases correspond to whether t is exactly certain duration away from T.
                      # and they have slightly different update rules.
  
                      if t + durations[i] < T:
                          betas[
                              offset + t * maxU + U - 1
                          ] = rnnt_helper.log_sum_exp(
                              betas[offset + t * maxU + U - 1],
                              betas[
                                  offset
                                  + (t + durations[i]) * maxU
                                  + U
                                  - 1
                              ]  # beta[t, U - 1] depends on the value beta[t + duration, U - 1] here.
                              + logp(
                                  denom,
                                  acts,
                                  maxT,
                                  maxU,
                                  alphabet_size,
                                  b,
                                  t,
                                  U - 1,
                                  blank_,
                              )  # log prob of blank
                              + logp_duration(
                                  duration_acts,
                                  maxT,
                                  maxU,
                                  num_durations,
                                  b,
                                  t,
                                  U - 1,
                                  i,
                              )  # log prob of duration (durations[i])
                              - sigma,  # for logit undernormalization
                          )
                      elif t + durations[i] == T:
                          betas[
                              offset + t * maxU + U - 1
                          ] = rnnt_helper.log_sum_exp(
                              betas[offset + t * maxU + U - 1],
                              # here we have one fewer term than the "if" block above. This could be seen as having "0" here since
                              # beta[t + duration, U - 1] isn't defined because t + duration is out of bound.
                              logp(
                                  denom,
                                  acts,
                                  maxT,
                                  maxU,
                                  alphabet_size,
                                  b,
                                  t,
                                  U - 1,
                                  blank_,
                              )  # log prob of blank
                              + logp_duration(
                                  duration_acts,
                                  maxT,
                                  maxU,
                                  num_durations,
                                  b,
                                  t,
                                  U - 1,
                                  i,
                              )  # log prob of duration (durations[i])
                              - sigma,  # for logit undernormalization. Basically every time sigma shows up is because of logit undernormalization.
                          )
  
          elif u < U - 1:
              if t == T - 1:
                  # t == T - 1, so we only consider non-blank with duration 0. (Note, we can't have blank emissions with duration = 0)
                  betas[offset + (T - 1) * maxU + u] = (
                      betas[offset + (T - 1) * maxU + u + 1]
                      + logp(
                          denom,
                          acts,
                          maxT,
                          maxU,
                          alphabet_size,
                          b,
                          T - 1,
                          u,
                          labels[u],
                      )  # non-blank log prob
                      + logp_duration(
                          duration_acts,
                          maxT,
                          maxU,
                          num_durations,
                          b,
                          T - 1,
                          u,
                          0,
                      )  # log prob of duration 0
                      - sigma
                  )
  
              elif t >= 0 and t < T - 1:
                  # now we need to consider both blank andnon-blanks. Similar to alphas, we first compute them separately with no_emit and emit.
                  no_emit = -INF
                  for i in range(1, num_durations):
                      if t + durations[i] < T:
                          no_emit = rnnt_helper.log_sum_exp(
                              no_emit,
                              betas[
                                  offset + (t + durations[i]) * maxU + u
                              ]
                              + logp(
                                  denom,
                                  acts,
                                  maxT,
                                  maxU,
                                  alphabet_size,
                                  b,
                                  t,
                                  u,
                                  blank_,
                              )
                              + logp_duration(
                                  duration_acts,
                                  maxT,
                                  maxU,
                                  num_durations,
                                  b,
                                  t,
                                  u,
                                  i,
                              )
                              - sigma,
                          )
  
                  emit = -INF
                  for i in range(0, num_durations):
                      if t + durations[i] < T:
                          emit = rnnt_helper.log_sum_exp(
                              emit,
                              betas[
                                  offset
                                  + (t + durations[i]) * maxU
                                  + u
                                  + 1
                              ]
                              + logp(
                                  denom,
                                  acts,
                                  maxT,
                                  maxU,
                                  alphabet_size,
                                  b,
                                  t,
                                  u,
                                  labels[u],
                              )
                              + logp_duration(
                                  duration_acts,
                                  maxT,
                                  maxU,
                                  num_durations,
                                  b,
                                  t,
                                  u,
                                  i,
                              )
                              - sigma,
                          )
  
                  # combining all blank emissions and all non-blank emissions.
                  betas[
                      offset + t * maxU + u
                  ] = rnnt_helper.log_sum_exp(emit, no_emit)
  
          # sync across all B=b and U=u
          cuda.syncthreads()
  
      # After final sync, betas[b, 0, 0] gives log-likelihood of backward pass, same with conventional Transducers.
      if u == 0:
          llBackward[b] = betas[offset]
  
file_path: NeMo/nemo/collections/asr/parts/numba/rnnt_loss/utils/cuda_utils/gpu_rnnt_kernel.py
imports: import math
  import torch
  from numba import cuda
  from nemo.collections.asr.parts.numba.rnnt_loss.utils import rnnt_helper
label_desc: Use image node_id 10 for calling a global function with example usage: compute_tdt_betas_kernel(acts, duration_acts, denom, sigma, betas, llBackward, xlen, ylen, mlabels, minibatch, maxT, maxU, alphabet_size, blank_, durations, num_durations) without return types