# Why Does a Model Output Garbage After FP16 Conversion?

> Why FP16's low top value breaks some models after conversion, how to find the bad layer, and the fix, explained simply for your AI interview.

**In short:** FP16 can only hold numbers up to 65,504, so some internal values of a model turn into infinity when converted. Infinity can turn into NaN in the next steps, and then the output breaks. Find the first bad layer, keep risky steps in full precision, or use BF16.

AI and LLM interview question Q342, Inference, performance and cost. Advanced, occasional (our estimate). From Venkat R. Murty's free Interview Kit: https://designervenkat.online/interview/ai/why-does-a-model-output-garbage-after-fp16-conversion

## Say it in 30 seconds

FP16 is a 16-bit number format, and its biggest value is 65,504. A model's inside numbers can be bigger than that. When I convert, those numbers turn into infinity, and infinity can make not-a-number values (NaN) that spread through the model. So the output turns to garbage. The fix: find the first bad layer, keep the risky steps in full precision, or use BF16, which has a wider range.

## Say it in 2 minutes

Floating point numbers are how a model stores its values. FP16 uses 16 bits, so it takes half the memory of FP32 and moves data faster. The cost is range. The biggest value FP16 can hold is 65,504, and values much bigger than that become infinity when converted.

Why does that break a model? Large language models have a few internal values far bigger than the rest. The massive activations paper says a few activations are far bigger than the rest, and gives one example about 100,000 times larger than typical ones. If a typical value is about 1, that one is above FP16's limit. Once it is infinity, the next arithmetic can produce NaN, and NaN spreads through the later layers. The answer comes out as garbage.

BF16 is the usual cure. It keeps the same range as FP32, so the big values survive, but it has less precision. Most models trained in BF16 cannot run in FP16's range, PyTorch's docs warn. So I would first find the first layer that shows infinity or NaN, keep softmax, layer norm and sums in FP32, and use BF16 if the chip supports it.

Last, I would test on long, messy real prompts and compare the answers with the full precision version. A short test would miss this bug.

## What is FP16, in plain words?

A model keeps its numbers in a floating point format, a bit like scientific notation. FP16 stores each number in 16 bits, and FP32, the full format, uses 32. Fewer bits means less memory and faster moves, which is why people convert.

The trade is range. FP16 can only hold numbers up to 65,504. Anything bigger turns into infinity, and everything built on it goes wrong.

Think of a bank's old teller system. Its amount field holds up to 65,504, so a 91,000 payment does not fit. The screen shows infinity, and every running total after it is wrong. Inside a model, the same thing happens to a few big internal numbers.

- **FP16:** A 16-bit number format: small and fast, with a narrow range.
- **BF16:** Another 16-bit format. It keeps FP32's wide range and gives up some precision.
- **Infinity:** What a number too big for FP16 becomes.
- **NaN:** Not a Number. The result of impossible maths, like infinity minus infinity.
- **Activation:** A number one layer of the model makes while it reads your text.

## Why does one big number break the whole answer?

FP16 has one sign bit, five exponent bits and ten fraction bits. The exponent bits set the size, so the largest value FP16 can hold is 65,504. Official sources give the limit, and note that BF16 keeps FP32's range:

> "Maximum normalized 65,504" (NVIDIA)
>
> "The dynamic range of bfloat16 and float32 are equivalent." (Google Cloud)
>
> "most bf16-pretrained models cannot operate in the fp16 numerical range of max 65504" (PyTorch)

Large language models do have very big internal values. The massive activations paper found that very few activations are far bigger than the rest, and it gives 100,000 times larger as one example. Its authors called them massive activations.

Do the sum. If a typical value is about 1, a value 100,000 times bigger is 100,000. That is above 65,504, so the conversion turns it into infinity. A paper on 8-bit inference, LLM.int8(), found highly systematic emergent features that dominate attention and transformer predictive performance, a related kind of large feature.

Then the damage spreads. The next step that touches infinity can produce NaN, and NaN passes into later layers. NVIDIA's guide warns that a loss scale that is too large can overflow during backpropagation, leaving gradients with infinities or NaNs that irreversibly damage the weights during the update. In a converted model, the output can show up as repeated symbols, random words, or one token again and again.

*Diagram, How a few huge values break the output: A few huge values pass through the conversion to FP16, where values well above 65,504 become infinity. Infinity can then turn into NaN in the next steps, and the output breaks.*
1. The big value goes into the conversion step.
2. Values well above 65,504 turn into infinity.
3. Infinity meets the next maths. It can give NaN, which spreads.

## How do you fix it?

BF16 usually avoids this, because it has FP32's range, so the big internal values fit. It pays for that with less precision, so the numbers are a little rougher. The fix is to find the break first, then change one thing at a time.

1. **Find the first bad layer:** Log the first layer that outputs an infinity or NaN. Only that layer shows where the range breaks.
2. **Keep risky steps in FP32:** Softmax, layer norm and sums can stay in full precision while the big matrix maths runs in FP16. PyTorch's autocast runs softmax, layer norm and sums in float32 on CUDA.
3. **Or use BF16 if the chip supports it:** The whole run keeps FP32's range, with a little less precision.
4. **Test on real prompts:** Run long, messy inputs through the fixed model and compare with the full precision answers. A short test misses this bug.

*Diagram, What to try when a converted model breaks: A short order to try when a converted model breaks: find the first bad layer, then either keep risky steps in full precision or switch to BF16. Both paths end in the same retest on real prompts.*
1. Keep the sensitive steps in full precision.
2. Or switch the whole run to BF16.
3. Check the fixed run on real prompts.
4. Check the BF16 run the same way.

> **Tip:** Don't cast the whole model and hope. A clean short test can pass while long inputs still break.

## What does a real failure look like, in code?

A mid-size bank moves its support assistant to FP16 servers to cut memory costs. Short chats look fine. Long chats, where a customer pastes in several months of statements, come back as strings of symbols about one time in twenty.

1. **Find the layer:** The logs show the first infinity in one attention layer, only on the long inputs.
2. **Read the cause:** Those inputs push that layer's values past 65,504, so the conversion turns them into infinity.
3. **Fix and retest:** The team keeps that layer's sums in FP32, then reruns 500 saved long chats and matches the full precision answers.

The code below shows the same break in a few lines. A made-up layer produces one big value, and the conversion does the rest.

```python
# A made-up layer of a model after conversion to FP16 (a 16-bit float).
# FP16 cannot hold anything above 65,504, so a bigger value becomes infinity.
import math
import struct

def to_fp16(x):
    try:
        return struct.unpack("<e", struct.pack("<e", x))[0]
    except OverflowError:
        return float("inf")  # too big for FP16

# Most activations are small, but one value is far bigger.
activations = [0.8, -1.2, 0.3, 91000.0]
fp16 = [to_fp16(a) for a in activations]
print("FP16 copy:", fp16)

# A simplified step: subtract the average from every value.
mean = sum(fp16) / len(fp16)
print("Mean:", mean)
centred = [a - mean for a in fp16]
print("Centred:", centred)
print("Any NaN?", any(math.isnan(v) for v in centred))
```

Output:

```text
FP16 copy: [0.7998046875, -1.2001953125, 0.300048828125, inf]
Mean: inf
Centred: [-inf, -inf, -inf, nan]
Any NaN? True
```

## What does the interviewer want to hear?

**A strong answer covers**

- [ ] FP16 tops out at 65,504, so bigger values become infinity.
- [ ] Models with big internal values break under FP16, which BF16 avoids.
- [ ] Infinity can turn into NaN, and NaN spreads, so the output is garbage.
- [ ] Fixes: find the first bad layer, keep risky steps in FP32, or use BF16.
- [ ] Always test on long, real prompts after any format change.

**Common traps**

- Trap: "Just retrain it in FP16, the bug will go away." Better: Retraining is slow, and the number range still breaks the model. Find the first bad layer first.
- Trap: "FP16 is just less accurate, so there is a bit of noise." Better: The problem is range, not small rounding. Big values turn into infinity, and NaN spreads.
- Trap: "Just use FP16 everywhere, it is faster." Better: Speed only counts if the answers still match. Test on long real prompts first.

## Can you spot the problem?

**Spot the fix:** A converted model gives correct answers on short chats but garbage on long ones. Which first step makes the most sense?

A. Retrain the whole model in FP16 from scratch.
B. Log the first layer that outputs an infinity or NaN.
C. Add more instructions to the system prompt.
D. Lower the temperature so answers repeat less.

**Answer:** B. Only the first bad layer shows where the range breaks. Retraining is slow, and prompts or temperature do not change how big a number can get.

**Quick check:** Which format keeps the same range as FP32, at the cost of some precision?

A. FP16
B. BF16
C. INT8
D. A longer context window

**Answer:** B. BF16 keeps FP32's wide range and gives up some precision. FP16 is narrower, so it overflows sooner.

## Can you answer these from memory?

**Flash cards**

- **What is the largest value FP16 can hold?** 65,504. Values much bigger than that become infinity when converted.
- **What does BF16 keep from FP32?** The same wide range, with less precision.
- **What is NaN?** Not a Number. Impossible maths like infinity minus infinity gives it, and it spreads.
- **Why does FP16 break some models?** Their big internal values go past 65,504 once converted.
- **What do you check first?** The first layer that outputs infinity or NaN.
- **Which steps usually stay in FP32?** Softmax, layer norm and sums, as PyTorch's autocast does on CUDA.

## Likely follow-up questions

- Q311 What is quantization? https://designervenkat.online/interview/ai/what-is-quantization
- Q312 INT8 vs INT4 vs FP8? https://designervenkat.online/interview/ai/int8-vs-int4-vs-fp8
- Q313 GPTQ, AWQ and GGUF in one line each? https://designervenkat.online/interview/ai/gptq-awq-and-gguf-in-one-line-each
- Q074 What uses memory at inference time? https://designervenkat.online/interview/ai/what-uses-memory-at-inference-time
