Skip to content
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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open

Conversation

saurabhkoshatwar
Copy link

@saurabhkoshatwar saurabhkoshatwar commented Oct 26, 2024

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.

tvd_speed

tvd_memory

  • Hardware Type: Nvidia H100 (80GB PCIe)
  • run make test to ensure correctness
  • run make checkstyle to ensure code style
  • run make test-convergence to ensure convergence

@saurabhkoshatwar
Copy link
Author

saurabhkoshatwar commented Oct 26, 2024

@ByronHsu @qingquansong @lancerts Please let me know if any changes are required.

@ByronHsu ByronHsu mentioned this pull request Oct 31, 2024
Copy link
Collaborator

@yundai424 yundai424 left a 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,
Copy link
Collaborator

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!

Copy link
Author

@saurabhkoshatwar saurabhkoshatwar Nov 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yundai424 5e-2 is the lowest that does not fail, and with a lower step, approx 5.2e-18 is the lowest rtol that does not fail.

Test passed with rtol=5.0e-02
Testing with rtol=4.5e-02...
Test passed with rtol=4.5e-02
Testing with rtol=4.0e-02...
Test passed with rtol=4.0e-02
Testing with rtol=3.5e-02...
Test passed with rtol=3.5e-02
Testing with rtol=3.0e-02...
Test passed with rtol=3.0e-02
Testing with rtol=2.5e-02...
Test passed with rtol=2.5e-02
Testing with rtol=2.0e-02...
Test passed with rtol=2.0e-02
Testing with rtol=1.5e-02...
Test passed with rtol=1.5e-02
Testing with rtol=1.0e-02...
Test passed with rtol=1.0e-02
Testing with rtol=5.0e-03...
Test passed with rtol=5.0e-03
Testing with rtol=5.2e-18...
Test passed with rtol=5.2e-18
Testing with rtol=-5.0e-03...
FAILED

from liger_kernel.transformers.tvd import LigerTVDLoss


class TorchTVDLoss(torch.nn.Module):
Copy link
Collaborator

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

Copy link
Collaborator

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

Copy link
Collaborator

@qingquansong qingquansong left a 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add TVD (Total variation distance) Kernel
5 participants