[GH-ISSUE #109] [Question] The clamp value in per_token_cast_to_fp8 #62

Closed
opened 2026-10-26 21:11:35 +00:00 by bsakaguchi · 11 comments
Owner

Originally created by @chenyu-jiang on GitHub (Jun 9, 2025).
Original GitHub issue: https://github.com/deepseek-ai/DeepGEMM/issues/109

Hi,

Thank you for the amazing work! May I ask a question about the fp8 format used:

In your test cases, e.g., per_token_cast_to_fp8, the amax value is clamped to 1e-4. May I know what is the purpose of such clamping? Is it just to avoid dividing by zero? Why is the value set to 1e-4 instead of lower (e.g., 1e-12)?

Originally created by @chenyu-jiang on GitHub (Jun 9, 2025). Original GitHub issue: https://github.com/deepseek-ai/DeepGEMM/issues/109 Hi, Thank you for the amazing work! May I ask a question about the fp8 format used: In your test cases, e.g., [per_token_cast_to_fp8](https://github.com/deepseek-ai/DeepGEMM/blob/8dfa3298274bfe6b242f6f8a3e6f3eff2707dd9f/tests/test_core.py#L21), the amax value is clamped to 1e-4. May I know what is the purpose of such clamping? Is it just to avoid dividing by zero? Why is the value set to 1e-4 instead of lower (e.g., 1e-12)?
Author
Owner

@LyricZhao commented on GitHub (Jun 9, 2025):

Nothing special, just a heuristic number to avoid dividing by zero.

But there is a kindly reminder: this value can ensure the scaling factor >= 1e-4 / 448, which is greater than the FP32's max subnormal value. So that you can directly use bit operations to do round-to-power-of-2-scaling casts.

<!-- gh-comment-id:2955372978 --> @LyricZhao commented on GitHub (Jun 9, 2025): Nothing special, just a heuristic number to avoid dividing by zero. But there is a kindly reminder: this value can ensure the scaling factor `>= 1e-4 / 448`, which is greater than the FP32's max subnormal value. So that you can directly use bit operations to do round-to-power-of-2-scaling casts.
Author
Owner

@chenyu-jiang commented on GitHub (Jun 9, 2025):

Thanks for the prompt response and the enlightening reminder!

I tried to use such clamping in training, but found that 1e-4 clamping would result in larger quantization error for some gradients during backward. May I know if this optimization is only targeted for inference? Should we use a lower value during training?

<!-- gh-comment-id:2955550865 --> @chenyu-jiang commented on GitHub (Jun 9, 2025): Thanks for the prompt response and the enlightening reminder! I tried to use such clamping in training, but found that 1e-4 clamping would result in larger quantization error for some gradients during backward. May I know if this optimization is only targeted for inference? Should we use a lower value during training?
Author
Owner

@LyricZhao commented on GitHub (Jun 10, 2025):

1e-4 is for both training and inference.

<!-- gh-comment-id:2957439083 --> @LyricZhao commented on GitHub (Jun 10, 2025): `1e-4` is for both training and inference.
Author
Owner

@chenyu-jiang commented on GitHub (Jun 10, 2025):

Thanks for the clarification!

<!-- gh-comment-id:2958247547 --> @chenyu-jiang commented on GitHub (Jun 10, 2025): Thanks for the clarification!
Author
Owner

@chenyu-jiang commented on GitHub (Jun 24, 2025):

Hi, sorry for bothering again. I noticed that:

1e-4 / 448 = 2.23214286e-7, while the smallest normal value of FP32 is 2^(-126) ~= 1.1754944e-38.

So it seems that we can use a much smaller clamping value (e.g., 1e-12) while still making sure the scale stays in the range of normal values.

Could you let me know if the understanding is correct? Thanks.

<!-- gh-comment-id:2998679456 --> @chenyu-jiang commented on GitHub (Jun 24, 2025): Hi, sorry for bothering again. I noticed that: 1e-4 / 448 = 2.23214286e-7, while the smallest normal value of FP32 is 2^(-126) ~= 1.1754944e-38. So it seems that we can use a much smaller clamping value (e.g., 1e-12) while still making sure the scale stays in the range of normal values. Could you let me know if the understanding is correct? Thanks.
Author
Owner

@weiyx16 commented on GitHub (Aug 14, 2025):

We don't have enough experiments on this value. It's free to adjust it to your case and I'll appreciate your results~

<!-- gh-comment-id:3186732531 --> @weiyx16 commented on GitHub (Aug 14, 2025): We don't have enough experiments on this value. It's free to adjust it to your case and I'll appreciate your results~
Author
Owner

@weiyx16 commented on GitHub (Aug 18, 2025):

We tried to change the amax clamp value from 1e-4 to 1e-12 on a pre-training task using a 9B MoE (total parameters) and around 400 Billion tokens. We didn't observe a huge difference in this new setting (around 0.001-0.002 loss difference).
A hypothesis is that clamping at 1e-4 leads to values lower than 1e-4/(448/2^-9) ~ 4.4e-10 underflow, and it's enough for most cases. And clamping at 1e-4 makes the max value imprecise: e.g. 1e-5 will be mapped to 1e-5*448/1e-4 = 44.8 ~ 44, but this error is smaller than the micro-scaling case with E8M0 Scaling Factor.
Anyway, it's free to clamp lower for your case.

<!-- gh-comment-id:3194955525 --> @weiyx16 commented on GitHub (Aug 18, 2025): We tried to change the amax clamp value from 1e-4 to 1e-12 on a pre-training task using a 9B MoE (total parameters) and around 400 Billion tokens. We didn't observe a huge difference in this new setting (around 0.001-0.002 loss difference). A hypothesis is that clamping at 1e-4 leads to values lower than 1e-4/(448/2^-9) ~ 4.4e-10 underflow, and it's enough for most cases. And clamping at 1e-4 makes the max value imprecise: e.g. 1e-5 will be mapped to 1e-5*448/1e-4 = 44.8 ~ 44, but this error is smaller than the micro-scaling case with E8M0 Scaling Factor. Anyway, it's free to clamp lower for your case.
Author
Owner

@chenyu-jiang commented on GitHub (Aug 18, 2025):

Thanks for the insights!

In our case, clamping to 1e-4 leads to loss divergence during initial training phases, while 1e-12 clamping seems to work fine. We also hypothesize that this is caused by the underflow of certain values. I guess this could be specific to certain model or data?

<!-- gh-comment-id:3195007768 --> @chenyu-jiang commented on GitHub (Aug 18, 2025): Thanks for the insights! In our case, clamping to 1e-4 leads to loss divergence during initial training phases, while 1e-12 clamping seems to work fine. We also hypothesize that this is caused by the underflow of certain values. I guess this could be specific to certain model or data?
Author
Owner

@weiyx16 commented on GitHub (Aug 19, 2025):

Thanks for sharing your results! It may be highly entangled with the training cases. Could you share the model size and training task, like pre-training, sft or rl? Really appreciate it.

<!-- gh-comment-id:3199354199 --> @weiyx16 commented on GitHub (Aug 19, 2025): Thanks for sharing your results! It may be highly entangled with the training cases. Could you share the model size and training task, like pre-training, sft or rl? Really appreciate it.
Author
Owner

@chenyu-jiang commented on GitHub (Aug 19, 2025):

It was small MoE model with 2.7B active parameters trained from scratch.

<!-- gh-comment-id:3199844508 --> @chenyu-jiang commented on GitHub (Aug 19, 2025): It was small MoE model with 2.7B active parameters trained from scratch.
Author
Owner

@weiyx16 commented on GitHub (Aug 21, 2025):

Thanks for the information~

<!-- gh-comment-id:3208853331 --> @weiyx16 commented on GitHub (Aug 21, 2025): Thanks for the information~
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
deepseek-ai/DeepGEMM#62
No description provided.