liveBook · Manning

Mixed precision

As explained in Deep Learning with R, Third Edition

Mixed-precision training is model training that uses numerical values with different floating-point precisions to balance computational efficiency and numerical accuracy.

Mixed-precision training combines numerical formats during model training: it uses fast 16-bit computation where reduced precision is safe, while retaining 32-bit values for gradients, weights, and updates that need stability. This usually provides much of the speed benefit of float16 without meaningfully reducing model quality. section 18.3.3

Enable mixed precision in a mini-GPT

A Keras model can enable the policy before constructing its layers: config_set_dtype_policy("mixed_float16"). Most of the forward pass then computes in float16, while layer variables remain float32. An eight-layer decoder model can therefore use faster computation without storing and updating its weights in half precision. section 16.2.1 section 18.3.3

Apply the technique with these practical choices:

  • Keep numerically unstable operations, such as softmax or cross-entropy, in float32 by passing dtype="float32" to the relevant layer. section 18.3.3
  • Use loss scaling when small float16 gradients risk rounding to zero; multiplying the loss makes those gradients more representable. section 18.3.4
  • Prefer automatic scaling when the appropriate factor may change during training: optimizer_adam(learning_rate = 1e-3) |> optimizer_loss_scale(). section 18.3.4
Listing 16.6 Creating a mini-GPT functional model
config_set_dtype_policy("mixed_float16")  #1
vocab_size <- tokenizer$vocabulary_size()
hidden_dim <- 512
intermediate_dim <- 2056
num_heads <- 8
num_layers <- 8

inputs <- keras_input(shape = c(NA), dtype = "int32", name = "inputs")
embedding <-
  layer_positional_embedding(, sequence_length, vocab_size, hidden_dim)

x <- inputs |>
  embedding() |>
  layer_layer_normalization()

for (i in seq_len(num_layers)) {
  x <- x |>
    layer_transformer_decoder(hidden_dim, intermediate_dim, num_heads)
}

outputs <- x |> embedding(reverse = TRUE)
mini_gpt <- keras_model(inputs, outputs)

Inspect a complete R/Keras mini-GPT configuration that enables mixed_float16 before defining the model. section 16.2.1

How Deep Learning with R, Third Edition covers it

How deep it goes
Explains dtype roles, unstable operations, loss scaling, and Keras configuration without deriving hardware-level implementation details.
Examples
Uses a mixed_float16 mini-GPT configuration, float32 layer opt-outs, and fixed or automatic optimizer loss scaling.
What it assumes
Assumes familiarity with Keras layers, optimizers, gradients, and model training while explaining precision hazards directly.
What you can do after
Operate/configure Keras mixed precision and protect training with float32 exceptions or loss scaling.
How it connects
Connects float16, bfloat16, gradient descent, loss scaling, softmax, cross-entropy, quantization, GPUs, and TPUs.

Also explained in

Compare how 3 books explain mixed precision →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with R, Third Edition. The section links point to the parts of the book it draws on.