NVIDIA / NVIDIA/Megatron-LM

[QUESTION] KL Divergence Computation in DSA Uses a Mathematically Non-Equivalent Approximation

Open
#4,055 2 comments 1 reaction 1 assignee Claimed by @kunlunl View on GitHub
community-request question waiting-on-customer
Dominant language
Python
Stars
17.9k
Forks
4.5k
Avg merge
4d 6h
Merged PRs (30d)
271

Description

**Your question**
Ask a clear and concise question about Megatron-LM. Tag the [@mcore-oncall](https://github.com/orgs/NVIDIA/teams/mcore-oncall)
to get oncall's attention to this issue.

Hello, thank you for the excellent work on this project.

I noticed that in https://github.com/NVIDIA/Megatron-LM/blob/main/megatron/core/transformer/experimental_attention_variant/dsa.py#L242, an epsilon value of 1e-10 is added to both P and Q during the computation of KL divergence. I understand this likely
serves as a safeguard against NaN values arising from numerical instability, which is a reasonable concern.

However, I wanted to point out that log(P + ε) - log(Q + ε) is not mathematically equivalent to log(P) - log(Q). Specifically:

$$\log\frac{P + \varepsilon}{Q + \varepsilon} \neq \log\frac{P}{Q}$$

I would be grateful if the maintainers could share any insight on the following:

1. Has the impact of this approximation on training dynamics and convergence been evaluated?
2. Were alternative approaches considered, such as clamping P and Q via torch.clamp prior to the logarithm?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.