Skip to content

Commit ee89d7b

Browse files
nshazeerCopybara-Service
authored andcommitted
Get bf16 activations working with mixture-of-experts.
PiperOrigin-RevId: 216935230
1 parent c8d7157 commit ee89d7b

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

  • tensor2tensor/mesh_tensorflow/research

tensor2tensor/mesh_tensorflow/research/moe.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,10 @@ def _top_2_gating(
475475
inputs, experts_dim, use_bias=False,
476476
expert_dims=outer_expert_dims), experts_dim)
477477

478+
# The internals of this function run in float32.
479+
# bfloat16 seems to reduce quality.
480+
raw_gates = mtf.to_float(raw_gates)
481+
478482
expert_capacity_f = float(expert_capacity_dim.size)
479483

480484
# FIND TOP 2 EXPERTS PER POSITON
@@ -591,6 +595,9 @@ def _top_2_gating(
591595
* mtf.one_hot(index_2, experts_dim)
592596
* mtf.one_hot(mtf.to_int32(position_in_expert_2), expert_capacity_dim))
593597

598+
combine_tensor = mtf.cast(combine_tensor, inputs.dtype)
599+
loss = mtf.cast(loss, inputs.dtype)
600+
594601
dispatch_tensor = mtf.cast(
595602
mtf.cast(combine_tensor, tf.bool), combine_tensor.dtype)
596603

0 commit comments

Comments
 (0)