Skip to content

Commit ea80920

Browse files
Merge pull request #2437 from AI-Hypercomputer:qinwen/add_grad_bf16
PiperOrigin-RevId: 815880783
2 parents 13872bd + cb80139 commit ea80920

3 files changed

Lines changed: 6 additions & 0 deletions

File tree

src/MaxText/configs/base.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ gcs_metrics: False
8383
# If true save config to GCS in {base_output_directory}/{run_name}/
8484
save_config_to_gcs: False
8585

86+
# Gradient dtype
87+
grad_dtype: "float32"
88+
8689
# Activation dtypes.
8790
dtype: "bfloat16"
8891
# Used to configure quantization in the transformer layers, defaults to null implying bf16.

src/MaxText/pyconfig.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,7 @@ def user_init(raw_keys):
691691

692692
# Type conversions
693693
raw_keys["dtype"] = jax.numpy.dtype(raw_keys["dtype"])
694+
raw_keys["grad_dtype"] = jax.numpy.dtype(raw_keys["grad_dtype"])
694695
raw_keys["weight_dtype"] = jax.numpy.dtype(raw_keys["weight_dtype"])
695696
raw_keys["mu_dtype"] = set_mu_dtype(raw_keys)
696697
raw_keys["logical_axis_rules"] = _lists_to_tuples(raw_keys["logical_axis_rules"])

src/MaxText/train.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,8 @@ def reshape_to_microbatch_accumulations(batch_arr):
255255
extra_dpo_args = [reference_params]
256256
grad_func = jax.value_and_grad(_loss_fn, argnums=4, has_aux=True)
257257
(loss, aux), raw_grads = grad_func(model, config, data, dropout_rng, state.params, *extra_dpo_args, is_train=True)
258+
259+
raw_grads = jax.tree_util.tree_map(lambda x: x.astype(config.grad_dtype) if x.dtype == jnp.float32 else x, raw_grads)
258260
intermediate_outputs = aux["intermediate_outputs"]
259261
total_weights = aux["total_weights"]
260262
moe_lb_loss = aux["moe_lb_loss"]

0 commit comments

Comments
 (0)