code: def average_by_duration(ds, xs, text_lengths, feats_lengths):
      """Average frame-level features into token-level according to durations
  
      Args:
          ds (Tensor): Batched token duration (B, T_text).
          xs (Tensor): Batched feature sequences to be averaged (B, T_feats).
          text_lengths (Tensor): Text length tensor (B,).
          feats_lengths (Tensor): Feature length tensor (B,).
  
      Returns:
          Tensor: Batched feature averaged according to the token duration (B, T_text).
  
      """
      device = ds.device
      args = [ds, xs, text_lengths, feats_lengths]
      args = [arg.detach().cpu().numpy() for arg in args]
      xs_avg = _average_by_duration(*args)
      xs_avg = torch.from_numpy(xs_avg).to(device)
      return xs_avg
  
file_path: espnet/espnet2/gan_tts/jets/alignments.py
imports: import numpy
  import torch
  import torch.nn
  import torch.nn.functional
  from numba import jit
  from scipy.stats import betabinom
label_desc: Use image node_id 4 for calling a global function with example usage: average_by_duration(ds, xs, text_lengths, feats_lengths) and returns: xs_avg