[Autodiff] Support backward in nn.Modules containing Helion kernels - #3473
Open
hinriksnaer wants to merge 1 commit into
Open
[Autodiff] Support backward in nn.Modules containing Helion kernels#3473hinriksnaer wants to merge 1 commit into
hinriksnaer wants to merge 1 commit into
Conversation
hinriksnaer
added a commit
that referenced
this pull request
Aug 27, 2026
When `HELION_EXPERIMENTAL_DIFFERENTIABLE=1` is set, Helion kernels
automatically participate in the autograd graph. No manual
`torch.autograd.Function` boilerplate needed:
```python
import helion
import helion.language as hl
@helion.kernel
def square_plus(x: Tensor) -> Tensor:
out = torch.empty_like(x)
for tile in hl.tile(x.size()):
v = x[tile]
out[tile] = v * v + v
return out
class MyModel(nn.Module):
def __init__(self):
super().__init__()
self.fc1 = nn.Linear(128, 256)
self.fc2 = nn.Linear(256, 32)
def forward(self, x):
x = self.fc1(x)
x = square_plus(x)
return self.fc2(x)
model = MyModel().cuda()
optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)
x = torch.randn(16, 128, device="cuda")
target = torch.randn(16, 32, device="cuda")
for step in range(100):
optimizer.zero_grad()
loss = F.mse_loss(model(x), target)
loss.backward()
optimizer.step()
```
```bash
HELION_EXPERIMENTAL_DIFFERENTIABLE=1 python train.py
```
`Kernel.__call__` detects tensors with `requires_grad=True` and routes
through a thin `torch.autograd.Function` wrapper. The backward pass
calls
`helion.experimental.backward()`, which auto-generates a backward Helion
kernel from the forward kernel's FX graph. Both forward and backward are
real Helion kernels compiled through the normal pipeline.
stack-info: PR: #3473, branch: hinriksnaer/stack/24
hinriksnaer
force-pushed
the
hinriksnaer/stack/24
branch
from
August 27, 2026 16:17
26a8b9e to
facfddd
Compare
When `HELION_EXPERIMENTAL_DIFFERENTIABLE=1` is set, Helion kernels
automatically participate in the autograd graph. No manual
`torch.autograd.Function` boilerplate needed:
```python
import helion
import helion.language as hl
@helion.kernel
def square_plus(x: Tensor) -> Tensor:
out = torch.empty_like(x)
for tile in hl.tile(x.size()):
v = x[tile]
out[tile] = v * v + v
return out
class MyModel(nn.Module):
def __init__(self):
super().__init__()
self.fc1 = nn.Linear(128, 256)
self.fc2 = nn.Linear(256, 32)
def forward(self, x):
x = self.fc1(x)
x = square_plus(x)
return self.fc2(x)
model = MyModel().cuda()
optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)
x = torch.randn(16, 128, device="cuda")
target = torch.randn(16, 32, device="cuda")
for step in range(100):
optimizer.zero_grad()
loss = F.mse_loss(model(x), target)
loss.backward()
optimizer.step()
```
```bash
HELION_EXPERIMENTAL_DIFFERENTIABLE=1 python train.py
```
`Kernel.__call__` detects tensors with `requires_grad=True` and routes
through a thin `torch.autograd.Function` wrapper. The backward pass
calls
`helion.experimental.backward()`, which auto-generates a backward Helion
kernel from the forward kernel's FX graph. Both forward and backward are
real Helion kernels compiled through the normal pipeline.
stack-info: PR: #3473, branch: hinriksnaer/stack/24
hinriksnaer
marked this pull request as draft
August 27, 2026 16:21
hinriksnaer
force-pushed
the
hinriksnaer/stack/24
branch
from
August 27, 2026 16:21
facfddd to
5ed77f5
Compare
hinriksnaer
marked this pull request as ready for review
August 27, 2026 16:21
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
When
HELION_EXPERIMENTAL_DIFFERENTIABLE=1is set, Helion kernelsautomatically participate in the autograd graph. No manual
torch.autograd.Functionboilerplate needed:Kernel.__call__detects tensors withrequires_grad=Trueand routesthrough a thin
torch.autograd.Functionwrapper. The backward passcalls
helion.experimental.backward(), which auto-generates a backward Helionkernel from the forward kernel's FX graph. Both forward and backward are
real Helion kernels compiled through the normal pipeline.