-
Notifications
You must be signed in to change notification settings - Fork 193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add TVD Loss Kernel #324
base: main
Are you sure you want to change the base?
Add TVD Loss Kernel #324
Conversation
@ByronHsu @qingquansong @lancerts Please let me know if any changes are required. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot for the contribution! 😄
pytest.param( | ||
torch.bfloat16, | ||
1e-8, | ||
5e-2, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you help to experiment what is the lowest rtol
that will not fail this test for bf16? Thanks!
from liger_kernel.transformers.tvd import LigerTVDLoss | ||
|
||
|
||
class TorchTVDLoss(torch.nn.Module): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel it'll be very helpful if we can add ignore index along with this PR to make TVD complete, similar to how JSD is doing it -- https://github.com/linkedin/Liger-Kernel/blob/main/src/liger_kernel/ops/jsd.py
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 which would be very helpful to cover broader use cases
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the efforts! Could you also add this to the init function in transformers folder as well same as JSD? https://github.com/linkedin/Liger-Kernel/blob/main/src/liger_kernel/transformers/__init__.py#L10
# TVD(P || Q) = 0.5 * |P - Q| | ||
tv_loss = 0.5 * tl.abs(p - q) | ||
|
||
grad_res = tl.where(p > q, 0.5, -0.5) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since we're doing gradients calculation in forward pass already, we can divide gradients by BT (BT * V) based on the reduction mode here to avoid extra calculations in backward pass and saving reduction mode in ctx
Summary
Resolves #281. Implements the TVD (Total Variation Distance) kernel by computing both the loss and gradient in the forward pass.
Testing Done
Implemented tests to verify that the results of the forward and backward passes match the Torch implementation. Additionally, added a script to benchmark the memory usage and speed of the Liger implementation compared to Torch, with the results shown below.
make test
to ensure correctnessmake checkstyle
to ensure code stylemake test-convergence
to ensure convergence