code: def mul_adaptive_logsoftmax(
      hidden,
      target,
      n_token,
      d_embed,
      d_proj,
      cutoffs,
      params,
      tie_projs,
      initializer=None,
      proj_initializer=None,
      div_val=1,
      perms=None,
      proj_same_dim=True,
      scope="adaptive_softmax",
      **kwargs,
  ):
      def _logit(x, W, b, proj):
          y = x
          if x.shape.ndims == 3:
              if proj is not None:
                  y = tf.einsum("ibd,ed->ibe", y, proj)
              return tf.einsum("ibd,nd->ibn", y, W) + b
          else:
              if proj is not None:
                  y = tf.einsum("id,ed->ie", y, proj)
              return tf.einsum("id,nd->in", y, W) + b
  
      params_W, params_projs = params[0], params[1]
  
      with tf.variable_scope(scope):
          if len(cutoffs) == 0:
              softmax_b = tf.get_variable(
                  "bias", [n_token], initializer=tf.zeros_initializer()
              )
              output = _logit(hidden, params_W, softmax_b, params_projs)
              nll = tf.nn.sparse_softmax_cross_entropy_with_logits(
                  labels=target, logits=output
              )
              nll = tf.reduce_mean(nll)
          else:
              total_loss, total_cnt = 0, 0
              cutoff_ends = [0] + cutoffs + [n_token]
              for i in range(len(cutoff_ends) - 1):
                  with tf.variable_scope("cutoff_{}".format(i)):
                      l_idx, r_idx = cutoff_ends[i], cutoff_ends[i + 1]
  
                      cur_d_embed = d_embed // (div_val**i)
  
                      if div_val == 1:
                          cur_W = params_W[l_idx:r_idx]
                      else:
                          cur_W = params_W[i]
                      cur_b = tf.get_variable(
                          "b",
                          [r_idx - l_idx],
                          initializer=tf.zeros_initializer(),
                      )
                      if tie_projs[i]:
                          if div_val == 1:
                              cur_proj = params_projs
                          else:
                              cur_proj = params_projs[i]
                      else:
                          if (
                              div_val == 1 or not proj_same_dim
                          ) and d_proj == cur_d_embed:
                              cur_proj = None
                          else:
                              cur_proj = tf.get_variable(
                                  "proj",
                                  [cur_d_embed, d_proj],
                                  initializer=proj_initializer,
                              )
  
                      if i == 0:
                          cluster_W = tf.get_variable(
                              "cluster_W",
                              [len(cutoffs), d_embed],
                              initializer=tf.zeros_initializer(),
                          )
                          cluster_b = tf.get_variable(
                              "cluster_b",
                              [len(cutoffs)],
                              initializer=tf.zeros_initializer(),
                          )
                          cur_W = tf.concat([cur_W, cluster_W], 0)
                          cur_b = tf.concat([cur_b, cluster_b], 0)
  
                          head_logit = _logit(
                              hidden, cur_W, cur_b, cur_proj
                          )
  
                          head_target = kwargs.get("head_target")
                          head_nll = tf.nn.sparse_softmax_cross_entropy_with_logits(
                              labels=head_target, logits=head_logit
                          )
  
                          masked_loss = head_nll * perms[i]
                          total_loss += tf.reduce_sum(masked_loss)
                          total_cnt += tf.reduce_sum(perms[i])
  
                          # head_logprob = tf.nn.log_softmax(head_logit)
  
                          # final_logprob = head_logprob * perms[i][:, :, None]
                          # final_target = tf.one_hot(target, tf.shape(head_logprob)[2])
                          # total_loss -= tf.einsum('ibn,ibn->', final_logprob, final_target)
                          # total_cnt += tf.reduce_sum(perms[i])
                      else:
                          cur_head_nll = tf.einsum(
                              "ib,ibk->k", head_nll, perms[i]
                          )
  
                          cur_hidden = tf.einsum(
                              "ibd,ibk->kd", hidden, perms[i]
                          )
                          tail_logit = _logit(
                              cur_hidden, cur_W, cur_b, cur_proj
                          )
  
                          tail_target = tf.einsum(
                              "ib,ibk->k",
                              tf.to_float(target - l_idx),
                              perms[i],
                          )
                          tail_nll = tf.nn.sparse_softmax_cross_entropy_with_logits(
                              labels=tf.to_int32(tail_target),
                              logits=tail_logit,
                          )
  
                          sum_nll = cur_head_nll + tail_nll
                          mask = tf.reduce_sum(perms[i], [0, 1])
  
                          masked_loss = sum_nll * mask
                          total_loss += tf.reduce_sum(masked_loss)
                          total_cnt += tf.reduce_sum(mask)
  
              nll = total_loss / total_cnt
  
      return nll, output
  
file_path: delta/gcompiler/example/python/nlp_transformer/model.py
imports: import tensorflow
label_desc: Use image node_id 9 for calling a global function with example usage: mul_adaptive_logsoftmax(hidden, target, n_token, d_embed, d_proj, cutoffs, params, tie_projs, initializer, proj_initializer, div_val, perms, proj_same_dim, scope) and returns: nll,  output, unknown, unknown