code: def compute_multiblank_alphas_kernel(
      acts: torch.Tensor,
      denom: torch.Tensor,
      sigma: float,
      alphas: torch.Tensor,
      llForward: torch.Tensor,
      xlen: torch.Tensor,
      ylen: torch.Tensor,
      mlabels: torch.Tensor,
      minibatch: int,
      maxT: int,
      maxU: int,
      alphabet_size: int,
      blank_: int,
      big_blank_duration: torch.Tensor,
      num_big_blanks: int,
  ):
      """
      Compute alpha (forward variable) probabilities for multi-blank transducuer loss
          (https://arxiv.org/pdf/2211.03541).
  
      Args:
          acts: Tensor of shape [B, T, U, V + 1 + num_big_blanks] flattened.
              Represents the logprobs activation tensor.
          denom: Tensor of shape [B, T, U] flattened. Represents the denominator of
              the logprobs activation tensor across entire vocabulary.
          sigma: Hyper-parameter for logit-undernormalization technique for training
              multi-blank transducers.
          alphas: Zero tensor of shape [B, T, U]. Will be updated inside the kernel
              with the forward variable probabilities.
          llForward: Zero tensor of shape [B]. Represents the log-likelihood of the
              forward pass. Returned as the forward 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 standard blank token in the vocabulary.
          big_blank_durations: Vector of supported big blank durations of the model.
          num_big_blanks: Number of big blanks of the model.
  
      Updates:
          Kernel inplace updates the following inputs:
          -   alphas: forward variable scores.
          -   llForward: log-likelihood of forward 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
  
      # Initilize alpha[b, t=0, u=0] for all b in B
      if u == 0:
          alphas[offset] = 0
  
      # sync until all alphas are initialized
      cuda.syncthreads()
  
      # Ordinary alpha calculations, broadcast across B=b and U=u
      # Look up forward variable calculation from rnnt_numpy.forward_pass()
      # Note: because of the logit under-normalization, everytime logp() is called,
      # it is always followed by a `-sigma` term.
      for n in range(1, T + U - 1):
          t = n - u
  
          if u == 0:
              # for t in range(1, T) step to initialize alphas[b, t, 0]
              if t > 0 and t < T:
                  alphas[offset + t * maxU + u] = (
                      alphas[offset + (t - 1) * maxU + u]
                      + logp(
                          denom,
                          acts,
                          maxT,
                          maxU,
                          alphabet_size,
                          b,
                          t - 1,
                          0,
                          blank_,
                      )
                      - sigma
                  )
  
                  # Now add the weights for big blanks.
                  for i in range(num_big_blanks):
                      if t >= big_blank_duration[i]:
                          alphas[
                              offset + t * maxU + u
                          ] = rnnt_helper.log_sum_exp(
                              alphas[offset + t * maxU + u],
                              alphas[
                                  offset
                                  + (t - big_blank_duration[i]) * maxU
                                  + u
                              ]
                              + logp(
                                  denom,
                                  acts,
                                  maxT,
                                  maxU,
                                  alphabet_size,
                                  b,
                                  t - big_blank_duration[i],
                                  0,
                                  blank_ - 1 - i,
                              )
                              - sigma,
                          )
  
          elif u < U:
              # for u in range(1, U) step to initialize alphas[b, 0, u]
              if t == 0:
                  alphas[offset + u] = (
                      alphas[offset + u - 1]
                      + logp(
                          denom,
                          acts,
                          maxT,
                          maxU,
                          alphabet_size,
                          b,
                          0,
                          u - 1,
                          labels[u - 1],
                      )
                      - sigma
                  )
  
              # for t in range(1, T) for u in range(1, U) step to compute alphas[b, t, u]
              elif t > 0 and t < T:
                  no_emit = (
                      alphas[offset + (t - 1) * maxU + u]
                      + logp(
                          denom,
                          acts,
                          maxT,
                          maxU,
                          alphabet_size,
                          b,
                          t - 1,
                          u,
                          blank_,
                      )
                      - sigma
                  )
                  emit = (
                      alphas[offset + t * maxU + u - 1]
                      + logp(
                          denom,
                          acts,
                          maxT,
                          maxU,
                          alphabet_size,
                          b,
                          t,
                          u - 1,
                          labels[u - 1],
                      )
                      - sigma
                  )
  
                  alphas[
                      offset + t * maxU + u
                  ] = rnnt_helper.log_sum_exp(emit, no_emit)
  
                  # Now add the weights for big blanks.
                  for i in range(num_big_blanks):
                      if t >= big_blank_duration[i]:
                          # big-blank weight here is
                          # alpha(t - duration, u) * p(big-blank | t - duration, u)
                          #     / exp(sigma), in log domain
                          # do this all all big-blanks if the above condition is met
                          big_blank_no_emit = (
                              alphas[
                                  offset
                                  + (t - big_blank_duration[i]) * maxU
                                  + u
                              ]
                              + logp(
                                  denom,
                                  acts,
                                  maxT,
                                  maxU,
                                  alphabet_size,
                                  b,
                                  t - big_blank_duration[i],
                                  u,
                                  blank_ - 1 - i,
                              )
                              - sigma
                          )
                          alphas[
                              offset + t * maxU + u
                          ] = rnnt_helper.log_sum_exp(
                              alphas[offset + t * maxU + u],
                              big_blank_no_emit,
                          )
  
          # sync across all B=b and U=u
          cuda.syncthreads()
  
      # After final sync, alphas[b, T-1, U - 1] + logprobs[b, T-1, U-1, blank]
      # + denom[b, T-1, U-1] gives log-likelihood of forward pass.
      if u == 0:
          loglike = (
              alphas[offset + (T - 1) * maxU + U - 1]
              + logp(
                  denom,
                  acts,
                  maxT,
                  maxU,
                  alphabet_size,
                  b,
                  T - 1,
                  U - 1,
                  blank_,
              )
              - sigma
          )
  
          # Now add the weights for big blanks for the final weight computation.
          for i in range(num_big_blanks):
              if T >= big_blank_duration[i]:
                  big_blank_loglike = (
                      alphas[
                          offset
                          + (T - big_blank_duration[i]) * maxU
                          + U
                          - 1
                      ]
                      + logp(
                          denom,
                          acts,
                          maxT,
                          maxU,
                          alphabet_size,
                          b,
                          T - big_blank_duration[i],
                          U - 1,
                          blank_ - 1 - i,
                      )
                      - sigma
                  )
                  loglike = rnnt_helper.log_sum_exp(
                      loglike, big_blank_loglike
                  )
  
          llForward[b] = loglike
  
file_path: espnet/espnet2/asr/transducer/rnnt_multi_blank/utils/cuda_utils/gpu_rnnt_kernel.py
imports: import math
  import torch
  from numba import cuda
  from espnet2.asr.transducer.rnnt_multi_blank.utils import rnnt_helper
label_desc: Use image node_id 5 for calling a global function with example usage: compute_multiblank_alphas_kernel(acts, denom, sigma, alphas, llForward, xlen, ylen, mlabels, minibatch, maxT, maxU, alphabet_size, blank_, big_blank_duration, num_big_blanks) without return types