You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[FEA] HSTU kernel fusion and wgrad/dgrad overlap #5
Is your feature request related to a problem? Please describe.
Achieve SOL performance by doing kernel fusion for hstu structure and possible wgrad/dgrad overlap.
Describe the solution you'd like
fusion
Meta has released triton-based kernel fusions. We can incorporate their code to ours and investigate the perf:
kernel.
op included
HSTU Layer
_HSTUPreprocessAndAttentionFunction
triton@_weighted_layer_norm_fwd
ln
triton@triton_addmm_fwd
QKVU linear_
bias
dlrm v3 only apply silu on U
SILU
triton@_hstu_attn_fwd
hstu_hma
HSTUComputeOutputFunction
triton@_ln_mul_dropout_fwd
ln
mul
dropout
triton@triton_addmm_fwd
linear
residual add
fusion PoC
async wgrad
The wgrad/dgrad of GEMM backward is a successfuly widely used in hugectr.
We can check if it brings any gain in HSTU.
PoC
To enable async wgrad/dgrad, we need to customize our HSTU layer rather than building on torch nor megatron. To attain this goal, we need to introduce multiple stream. A minimal example would be
class LinearWithAsyncWgrad(torch.autograd.Function):
@staticmethod
def forward(ctx, x, w, b, stream = None):
ctx.save_for_backward(x, w, b)
ctx.stream = stream
return x * w + b
@staticmethod
def backward(ctx, grad_output):
x, w, b = ctx.saved_tensors
dgrad = mm(grad_output, w.t())
bgrad = grad_output.sum(dim=0)
with torch.cuda.stream(ctx.stream):
wgrad = mm(x.t(), grad_output)
torch.cuda.current_stream().wait_stream(ctx.stream)
return grad_output, wgrad, dgrad, bgrad
def linear_with_async_wgrad(
x,
w,
b,
wgrad_stream = None
):
return LinearWithAsyncWgrad.apply(x, w, b, wgrad_stream)
Describe alternatives you've considered
N/A
Additional context
Regarding Fusion, we will need to leverage triton kernel first, building upon the above idea.
Activity
JacoCheung commentedon Apr 30, 2025
slack msg
JacoCheung commentedon May 6, 2025
We now have several optimization potentials for fusion :
( overhead reduce)
(we need to customize addmm with nvmath-python)
(Passing an cumulative grad to layer norm dx)
Regarding the gemm autotune, I think it's not practial given that the input is jagged ( the
mis variable).The projected gain is derived under such assumption.
shijieliu commentedon May 7, 2025
@JacoCheung Good work.
How about the SOL calculation? What's the effeciency of triton implemented fused layernorm/addmm, etc?
shijieliu commentedon May 8, 2025
JacoCheung commentedon May 8, 2025
Reply from jerry: it should be simply implemented (both forward and backward)
JacoCheung commentedon May 13, 2025
Regarding the contiguous issues, the ETA is next week.
shijieliu commentedon May 14, 2025
@shijieliu will study why doing wgrad/dgrd overlap will cause larger kernel bubble
JacoCheung commentedon May 15, 2025
There is an unexpected D2D of the second GEMM+Residual which take half more than cublas gemm itself time. Filed an issue here
shijieliu commentedon Jun 18, 2025
close as done.
support fill_tables API for BatchedDynamicEmbeddingTablesV2 (#5)
[Feature] dynamicemb table fusion and expansion (#343)
feat(autosched): fire-order auto-scheduler with overlap matrix
feat(autosched): fire-order auto-scheduler with overlap matrix
feat(autosched): fire-order auto-scheduler with overlap matrix