-
Notifications
You must be signed in to change notification settings - Fork 15
Add cross_entropy example and unit test #320
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
Merged
+133
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
from __future__ import annotations | ||
|
||
import os | ||
|
||
import torch | ||
|
||
import helion | ||
from helion._testing import run_example | ||
import helion.language as hl | ||
|
||
# TritonBench configuration - adjust based on HELION_DEV_LOW_VRAM environment variable | ||
if os.environ.get("HELION_DEV_LOW_VRAM", "0") == "1": | ||
# Low memory configuration | ||
TRITONBENCH_ARGS = {"B": 4, "T": 512, "v_range": "10,15"} | ||
|
||
|
||
@helion.kernel(ignore_warnings=[helion.exc.TensorOperationInWrapper]) | ||
def cross_entropy( | ||
logits: torch.Tensor, # [N, V] input logits | ||
labels: torch.Tensor, # [N] target labels | ||
) -> torch.Tensor: | ||
n, v = logits.shape | ||
losses = torch.zeros([n], dtype=logits.dtype, device=logits.device) | ||
|
||
# Flatten logits once at the beginning | ||
logits_flat = logits.view(-1) | ||
|
||
for tile_n in hl.tile(n): | ||
# Get data for this tile | ||
labels_tile = labels[tile_n] # [tile_size] | ||
base_indices_tile = tile_n.index * v # [tile_size] | ||
|
||
# Compute the actual flat indices by adding the label offset | ||
flat_indices = base_indices_tile + labels_tile | ||
|
||
# Load the logits at the target indices | ||
logits_at_target = hl.load(logits_flat, [flat_indices]) | ||
|
||
# Compute log_softmax for numerical stability | ||
# Load the full rows for this tile | ||
logits_rows = logits[tile_n, :] # [tile_size, V] | ||
|
||
# Compute log-sum-exp | ||
max_logits = torch.amax(logits_rows, dim=-1, keepdim=True) | ||
shifted = logits_rows - max_logits | ||
exp_shifted = torch.exp(shifted) | ||
sum_exp = torch.sum(exp_shifted, dim=-1, keepdim=True) | ||
log_sum_exp = max_logits.squeeze(-1) + torch.log(sum_exp.squeeze(-1)) | ||
|
||
# Cross entropy loss: log_sum_exp - logit_at_target | ||
losses[tile_n] = log_sum_exp - logits_at_target | ||
|
||
return losses.mean() | ||
|
||
|
||
def main() -> None: | ||
"""Run cross entropy benchmark with different input sizes.""" | ||
# Test with moderate size | ||
n, v = 128, 1000 | ||
logits = torch.randn(n, v, device="cuda", dtype=torch.float32) | ||
labels = torch.randint(0, v, (n,), device="cuda", dtype=torch.long) | ||
|
||
run_example( | ||
cross_entropy, | ||
torch.nn.functional.cross_entropy, | ||
(logits, labels), | ||
kernel_name="helion", | ||
baseline_name="torch", | ||
rtol=1e-4, | ||
atol=1e-4, | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Where is this used?
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.
It’s for development on low-VRAM machines - switched to use HELION_DEV_LOW_VRAM to gate this