(encoder_function, perf_inputs, mel_inputs,
                                target_space, hparams, attention_weights=None,
                                features=None, losses=None,
                                prepare_encoder_fn=None, **kwargs)
return encoder_output, encoder_decoder_attention_bias
Encode transformer inputs. Used for melody & performance autoencoder.

  Performance is mean-aggregated across time and combined with melody in a
  variety of different ways.

  Args:
    encoder_function: the encoder function
    perf_inputs: Transformer inputs [batch_size, input_length, 1, hidden_dim]
    which will be flattened along the two spatial dimensions.
    mel_inputs: Transformer inputs [batch_size, input_length, 1, hidden_dim]
    which will be flattened along the two spatial dimensions.
    target_space: scalar, target space ID.
    hparams: hyperparameters for model.
    attention_weights: weight to store attention to.
    features: optionally pass the entire features dictionary as well. This is
      needed now for "packed" datasets.
    losses: optional list onto which to append extra training losses
    prepare_encoder_fn: optional, alternative to transformer_prepare_encoder.
    **kwargs: additional arguments to pass to encoder_function

  Returns:
    Tuple of:
        encoder_output: Encoder representation.
            [batch_size, input_length, hidden_dim]
        encoder_decoder_attention_bias: Bias and mask weights for
            encoder-decoder attention. [batch_size, input_length]
Encode transformer inputs. Used for melody & performance autoencoder.
Encode transformer inputs . Used for melody & performance autoencoder .
def mel_perf_transformer_encode(encoder_function, perf_inputs, mel_inputs,
                                target_space, hparams, attention_weights=None,
                                features=None, losses=None,
                                prepare_encoder_fn=None, **kwargs):
  """Encode transformer inputs. Used for melody & performance autoencoder.

  Performance is mean-aggregated across time and combined with melody in a
  variety of different ways.

  Args:
    encoder_function: the encoder function
    perf_inputs: Transformer inputs [batch_size, input_length, 1, hidden_dim]
    which will be flattened along the two spatial dimensions.
    mel_inputs: Transformer inputs [batch_size, input_length, 1, hidden_dim]
    which will be flattened along the two spatial dimensions.
    target_space: scalar, target space ID.
    hparams: hyperparameters for model.
    attention_weights: weight to store attention to.
    features: optionally pass the entire features dictionary as well. This is
      needed now for "packed" datasets.
    losses: optional list onto which to append extra training losses
    prepare_encoder_fn: optional, alternative to transformer_prepare_encoder.
    **kwargs: additional arguments to pass to encoder_function

  Returns:
    Tuple of:
        encoder_output: Encoder representation.
            [batch_size, input_length, hidden_dim]
        encoder_decoder_attention_bias: Bias and mask weights for
            encoder-decoder attention. [batch_size, input_length]
  """
  perf_inputs = common_layers.flatten4d3d(perf_inputs)
  mel_inputs = common_layers.flatten4d3d(mel_inputs)

  if not prepare_encoder_fn:
    prepare_encoder_fn = transformer_prepare_encoder
  perf_encoder_input, perf_self_attention_bias, perf_encdec_attention_bias = (
      prepare_encoder_fn(
          perf_inputs, target_space, hparams, features=features,
          reuse_target_embedding=tf.AUTO_REUSE))

  mlperf_log.transformer_print(
      key=mlperf_log.MODEL_HP_LAYER_POSTPROCESS_DROPOUT,
      value=hparams.layer_prepostprocess_dropout,
      hparams=hparams)

  perf_encoder_input = tf.nn.dropout(perf_encoder_input,
                                     1.0 - hparams.layer_prepostprocess_dropout)

  perf_attn_bias_for_padding = None
  # Otherwise the encoder will just use encoder_self_attention_bias.
  if hparams.unidirectional_encoder:
    perf_attn_bias_for_padding = perf_encdec_attention_bias

  # do the same thing for melody
  mel_encoder_input, mel_self_attention_bias, mel_encdec_attention_bias = (
      prepare_encoder_fn(
          mel_inputs, target_space, hparams, features=features,
          reuse_target_embedding=tf.AUTO_REUSE))

  mlperf_log.transformer_print(
      key=mlperf_log.MODEL_HP_LAYER_POSTPROCESS_DROPOUT,
      value=hparams.layer_prepostprocess_dropout,
      hparams=hparams)

  mel_encoder_input = tf.nn.dropout(mel_encoder_input,
                                    1.0 - hparams.layer_prepostprocess_dropout)

  mel_attn_bias_for_padding = None
  # Otherwise the encoder will just use encoder_self_attention_bias.
  if hparams.unidirectional_encoder:
    mel_attn_bias_for_padding = mel_encdec_attention_bias

  # use the proper encoder function for perf/melody
  perf_encoder_output = encoder_function(
      perf_encoder_input,
      perf_self_attention_bias,
      hparams,
      name="perf_encoder",
      nonpadding=features_to_nonpadding(features, "inputs"),
      save_weights_to=attention_weights,
      make_image_summary=not common_layers.is_xla_compiled(),
      losses=losses,
      attn_bias_for_padding=perf_attn_bias_for_padding,
      **kwargs)
  # same thing for melody
  mel_encoder_output = encoder_function(
      mel_encoder_input,
      mel_self_attention_bias,
      hparams,
      name="mel_encoder",
      nonpadding=features_to_nonpadding(features, "inputs"),
      save_weights_to=attention_weights,
      make_image_summary=not common_layers.is_xla_compiled(),
      losses=losses,
      attn_bias_for_padding=mel_attn_bias_for_padding,
      **kwargs)

  # concatenate the global mean vector/bias term with the full melody encoding
  perf_mean_vector = tf.math.reduce_mean(
      perf_encoder_output, axis=1, keep_dims=True)

  # different methods of aggregating over the performance + melody vectors!
  if hparams.aggregation == "sum":
    # add both mean performance and melody vectors together
    perf_mean_bias = tf.math.reduce_mean(perf_encdec_attention_bias,
                                         axis=-1, keep_dims=True)
    encoder_output = mel_encoder_output + perf_mean_vector
    encoder_decoder_attention_bias = mel_encdec_attention_bias + perf_mean_bias
  elif hparams.aggregation == "concat":
    # concatenate melody with mean-aggregated performance embedding
    stop_token = tf.zeros((1, 1, 384))
    encoder_output = tf.concat(
        [mel_encoder_output, stop_token, perf_mean_vector], axis=1)
    perf_mean_bias = tf.math.reduce_mean(perf_encdec_attention_bias,
                                         axis=-1, keep_dims=True)
    stop_bias = tf.zeros((1, 1, 1, 1))
    encoder_decoder_attention_bias = tf.concat(
        [mel_encdec_attention_bias, stop_bias, perf_mean_bias], axis=-1)
  elif hparams.aggregation == "tile":
    # tile performance embedding across each dimension of melody embedding!
    dynamic_val = tf.shape(mel_encoder_output)[1]
    shp = tf.convert_to_tensor([1, dynamic_val, 1], dtype=tf.int32)
    tiled_mean = tf.tile(perf_mean_vector, shp)

    encoder_output = tf.concat([mel_encoder_output, tiled_mean], axis=-1)
    encoder_decoder_attention_bias = mel_encdec_attention_bias
  else:
    NotImplementedError("aggregation method must be in [sum, concat, tile].")

  return encoder_output, encoder_decoder_attention_bias
def mel_perf_transformer_encode ( encoder_function , perf_inputs , mel_inputs , target_space , hparams , attention_weights = None , features = None , losses = None , prepare_encoder_fn = None , * * kwargs ) : perf_inputs = common_layers . flatten4d3d ( perf_inputs ) mel_inputs = common_layers . flatten4d3d ( mel_inputs ) if not prepare_encoder_fn : prepare_encoder_fn = transformer_prepare_encoder perf_encoder_input , perf_self_attention_bias , perf_encdec_attention_bias = ( prepare_encoder_fn ( perf_inputs , target_space , hparams , features = features , reuse_target_embedding = tf . AUTO_REUSE ) ) mlperf_log . transformer_print ( key = mlperf_log . MODEL_HP_LAYER_POSTPROCESS_DROPOUT , value = hparams . layer_prepostprocess_dropout , hparams = hparams ) perf_encoder_input = tf . nn . dropout ( perf_encoder_input , 1.0 - hparams . layer_prepostprocess_dropout ) perf_attn_bias_for_padding = None # Otherwise the encoder will just use encoder_self_attention_bias. if hparams . unidirectional_encoder : perf_attn_bias_for_padding = perf_encdec_attention_bias # do the same thing for melody mel_encoder_input , mel_self_attention_bias , mel_encdec_attention_bias = ( prepare_encoder_fn ( mel_inputs , target_space , hparams , features = features , reuse_target_embedding = tf . AUTO_REUSE ) ) mlperf_log . transformer_print ( key = mlperf_log . MODEL_HP_LAYER_POSTPROCESS_DROPOUT , value = hparams . layer_prepostprocess_dropout , hparams = hparams ) mel_encoder_input = tf . nn . dropout ( mel_encoder_input , 1.0 - hparams . layer_prepostprocess_dropout ) mel_attn_bias_for_padding = None # Otherwise the encoder will just use encoder_self_attention_bias. if hparams . unidirectional_encoder : mel_attn_bias_for_padding = mel_encdec_attention_bias # use the proper encoder function for perf/melody perf_encoder_output = encoder_function ( perf_encoder_input , perf_self_attention_bias , hparams , name = "perf_encoder" , nonpadding = features_to_nonpadding ( features , "inputs" ) , save_weights_to = attention_weights , make_image_summary = not common_layers . is_xla_compiled ( ) , losses = losses , attn_bias_for_padding = perf_attn_bias_for_padding , * * kwargs ) # same thing for melody mel_encoder_output = encoder_function ( mel_encoder_input , mel_self_attention_bias , hparams , name = "mel_encoder" , nonpadding = features_to_nonpadding ( features , "inputs" ) , save_weights_to = attention_weights , make_image_summary = not common_layers . is_xla_compiled ( ) , losses = losses , attn_bias_for_padding = mel_attn_bias_for_padding , * * kwargs ) # concatenate the global mean vector/bias term with the full melody encoding perf_mean_vector = tf . math . reduce_mean ( perf_encoder_output , axis = 1 , keep_dims = True ) # different methods of aggregating over the performance + melody vectors! if hparams . aggregation == "sum" : # add both mean performance and melody vectors together perf_mean_bias = tf . math . reduce_mean ( perf_encdec_attention_bias , axis = - 1 , keep_dims = True ) encoder_output = mel_encoder_output + perf_mean_vector encoder_decoder_attention_bias = mel_encdec_attention_bias + perf_mean_bias elif hparams . aggregation == "concat" : # concatenate melody with mean-aggregated performance embedding stop_token = tf . zeros ( ( 1 , 1 , 384 ) ) encoder_output = tf . concat ( [ mel_encoder_output , stop_token , perf_mean_vector ] , axis = 1 ) perf_mean_bias = tf . math . reduce_mean ( perf_encdec_attention_bias , axis = - 1 , keep_dims = True ) stop_bias = tf . zeros ( ( 1 , 1 , 1 , 1 ) ) encoder_decoder_attention_bias = tf . concat ( [ mel_encdec_attention_bias , stop_bias , perf_mean_bias ] , axis = - 1 ) elif hparams . aggregation == "tile" : # tile performance embedding across each dimension of melody embedding! dynamic_val = tf . shape ( mel_encoder_output ) [ 1 ] shp = tf . convert_to_tensor ( [ 1 , dynamic_val , 1 ] , dtype = tf . int32 ) tiled_mean = tf . tile ( perf_mean_vector , shp ) encoder_output = tf . concat ( [ mel_encoder_output , tiled_mean ] , axis = - 1 ) encoder_decoder_attention_bias = mel_encdec_attention_bias else : NotImplementedError ( "aggregation method must be in [sum, concat, tile]." ) return encoder_output , encoder_decoder_attention_bias