Skip to content
This repository was archived by the owner on Jan 21, 2025. It is now read-only.

Update attention.py #394

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions mesh_tensorflow/transformer/attention.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,20 @@ def attention(q,
"Dividing attention z-loss loss by num_microbatches={}".format(
context.num_microbatches))
z_loss /= context.num_microbatches
if context.train:
mtf.scalar_summary("attention_z_loss", z_loss)
mtf.scalar_summary("attention_z_loss", z_loss)
z_loss *= z_loss_coeff
context.losses.append(mtf.cast(z_loss, v.dtype))

weights = mtf.softmax(logits, memory_length_dim, extra_logit=extra_logit)
weights = mtf.cast(weights, v.dtype)
weights = mtf.dropout(
weights, context.train, 1.0 - dropout_rate,
noise_shape=weights.shape - dropout_broadcast_dims)
if context:
weights = mtf.dropout(
weights, context.train, 1.0 - dropout_rate,
noise_shape=weights.shape - dropout_broadcast_dims)
else:
weights = mtf.dropout(
weights, False, 1.0 - dropout_rate,
noise_shape=weights.shape - dropout_broadcast_dims)
outputs_shape = q.shape - key_dim + value_dim
outputs = mtf.einsum([weights, v], outputs_shape)
outputs = mtf.reshape(outputs, orig_q_shape - key_dim + value_dim)
Expand Down