Skip to content

[FEA] HSTU kernel fusion and wgrad/dgrad overlap #5

Closed

Description

@shijieliu

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

  1. added
    enhancement
    on Apr 23, 2025
  2. JacoCheung commented on Apr 30, 2025

    @JacoCheung
    Collaborator
    • For gemm, we may need to manually search for the best algorithm.

    slack msg

  3. JacoCheung commented on May 6, 2025

    @JacoCheung
    Collaborator

    We now have several optimization potentials for fusion :

    Opt id Strategy Feasibility/simplicity Max Gain
    ( overhead reduce)
    1 Remove q,k,v,u contiguous that follows split. Low, see issue ~6%
    2 Remove q,k,v,u grad contiguous Low, see issue ~7%
    3 Fuse dbias with dw/dx Med
    (we need to customize addmm with nvmath-python)
    ~2%
    4 Fuse grad_add with input layernorm High
    (Passing an cumulative grad to layer norm dx)
    ~2%

    Regarding the gemm autotune, I think it's not practial given that the input is jagged ( the m is variable).

    The projected gain is derived under such assumption.

  4. shijieliu commented on May 7, 2025

    @shijieliu
    CollaboratorAuthor

    @JacoCheung Good work.

    How about the SOL calculation? What's the effeciency of triton implemented fused layernorm/addmm, etc?

  5. shijieliu commented on May 8, 2025

    @shijieliu
    CollaboratorAuthor
    1. sync with jerry about fusing contiguous with attention
    2. POC wgrad/dgrad overlap.
  6. added this to the 25.05 milestone on May 8, 2025
  7. JacoCheung commented on May 8, 2025

    @JacoCheung
    Collaborator
    1. sync with jerry about fusing contiguous with attention

    Reply from jerry: it should be simply implemented (both forward and backward)

  8. JacoCheung commented on May 13, 2025

    @JacoCheung
    Collaborator

    Regarding the contiguous issues, the ETA is next week.

  9. shijieliu commented on May 14, 2025

    @shijieliu
    CollaboratorAuthor

    @shijieliu will study why doing wgrad/dgrd overlap will cause larger kernel bubble

  10. JacoCheung commented on May 15, 2025

    @JacoCheung
    Collaborator

    There is an unexpected D2D of the second GEMM+Residual which take half more than cublas gemm itself time. Filed an issue here

  11. removed this from the 25.05 milestone on Jun 4, 2025
  12. shijieliu commented on Jun 18, 2025

    @shijieliu
    CollaboratorAuthor

    close as done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions