ArticleslgStudy

computer science

Mixed-precision arithmetic

Mixed-precision arithmetic is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Mixed-precision arithmetic rather than just read about it. In short: Mixed-precision arithmetic is a form of floating-point arithmetic that uses numbers with varying widths in a single operation. Overview A common usage of mixed-precision arithmetic is for operating on inaccurate numbers with a small width and expanding them to a larger, more accurate representation.

Mixed-precision arithmetic — main illustration
Mixed-precision arithmetic — illustration

Key takeaways

  • Mixed-precision arithmetic belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Mixed-precision arithmetic to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Mixed-precision arithmetic from memory before moving on to harder problems.

Reference excerpt

Mixed-precision arithmetic is a form of floating-point arithmetic that uses numbers with varying widths in a single operation.

Overview A common usage of mixed-precision arithmetic is for operating on inaccurate numbers with a small width and expanding them to a larger, more accurate representation. For example, two half-precision or bfloat16 (16-bit) floating-point numbers may be multiplied together to result in a more accurate single-precision (32-bit) float. In this way, mixed-precision arithmetic approximates arbitrary-precision arithmetic, albeit with a low number of possible precisions. Iterative algorithms (like gradient descent) are good candidates for mixed-precision arithmetic. In an iterative algorithm like square root, a coarse integral guess can be made and refined over many iterations until the error in precision makes it such that the smallest addition or subtraction to the guess is still too coarse to be an acceptable answer. When this happens, the precision can be increased to something more precise, which allows for smaller increments to be used for the approximation. Supercomputers such as Summit utilize mixed-precision arithmetic to be more efficient with regards to memory and processing time, as well as power consumption.

Floating point format A floating-point number is typically packed into a single bit-string, as the sign bit, the exponent field, and the significand or mantissa, from left to right. As an example, a IEEE 754 standard 32-bit float ("FP32", "float32", or "binary32") is packed as follows:

The IEEE 754 binary floats are:

Machine learning Mixed-precision arithmetic is used in the field of machine learning, since gradient descent algorithms can use coarse and efficient half-precision floats for certain tasks, but can be more accurate if they use more precise but slower single-precision floats. Some platforms, including Nvidia, Intel, and AMD CPUs and GPUs, provide mixed-precision arithmetic for this purpose, using coarse floats when possible, but expanding them to higher precision when necessary.

Automatic mixed precision PyTorch implements automatic mixed-precision (AMP), which performs autocasting, gradient scaling, and loss scaling.

The weights are stored in a master copy at a high precision, usually in FP32. Autocasting means automatically converting a floating-point number between different precisions, such as from FP32 to FP16, during training. For example, matrix multiplications can often be performed in FP16 without loss of accuracy, even if the master copy weights are stored in FP32. Low-precision weights are used during forward pass. Gradient scaling means multiplying gradients by a constant factor during training, typically before the weight optimizer update. This is done to prevent the gradients from underflowing to zero when using low-precision data types like FP16. Mathematically, if the unscaled gradient is g {\displaystyle \mathbf {g} } , the scaled gradient is s g {\displaystyle s\mathbf {g} } where s {\displaystyle s} is the scaling factor. Within the optimizer update, the scaled gradient is cast to a higher precision before it is scaled down (no longer underflowing, as it is in a higher precision) to update the weights. Loss scaling means multiplying the loss function by a constant factor during training, typically before backpropagation. This is done to prevent the gradients from underflowing to zero when using low-precision data types. If the unscaled loss is L {\displaystyle {\mathcal {L}}} , the scaled loss is k L {\displaystyle k{\mathcal {L}}} where k {\displaystyle k} is the scaling factor. Since gradient scaling and loss scaling are mathematically equivalent by ∂ ( k L ) ∂ w = k ∂ L ∂ w {\displaystyle {\frac {\partial (k{\mathcal {L}})}{\partial \mathbf {w} }}=k{\frac {\partial {\mathcal {L}}}{\partial \mathbf {w} }}} , loss scaling is an implementation of gradient scaling. PyTorch AMP uses exponential backoff to automatically adjust the scale factor for loss scaling. That is, it periodically increase the scale factor. Whenever the gradients contain a NaN (indicating overflow), the weight update is skipped, and the scale factor is decreased.

References

Worked examples

Example 1 — a first encounter with Mixed-precision arithmetic

Start with the simplest possible case. Write down what Mixed-precision arithmetic claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Mixed-precision arithmetic before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Mixed-precision arithmetic ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Mixed-precision arithmetic

In research
Mixed-precision arithmetic appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Mixed-precision arithmetic in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Mixed-precision arithmetic is common in secondary-school and first-year university syllabi. It links to neighbouring topics Computer arithmetic, Floating point, so understanding it makes those chapters shorter.
In everyday life
Look for Mixed-precision arithmetic outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Mixed-precision arithmetic in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Mixed-precision arithmetic means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Mixed-precision arithmetic out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Mixed-precision arithmetic in simple terms?

Mixed-precision arithmetic is a form of floating-point arithmetic that uses numbers with varying widths in a single operation. Overview A common usage of mixed-precision arithmetic is for operating on inaccurate numbers with a small width and expanding them to a larger, more accurate representation.

Why does Mixed-precision arithmetic matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Mixed-precision arithmetic?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Mixed-precision arithmetic.

Tags

  • Computer arithmetic
  • Floating point

Keep exploring