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

[JAX] Collective GEMM custom op with nvte_cublas_gemm (no comm. overlap) #1307

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

Conversation

denera
Copy link
Collaborator

@denera denera commented Nov 2, 2024

Description

Implements both old-style and new FFI-based XLA custom calls in C++, and the corresponding JAX primitive including custom partitioning rules.

Custom partitioning rules for a LHS:([B,] M, K) x RHS:([B,] K, N) = OUT:([B,] M, N) batched mat-mul operation where [B] is the batch dimension:

  • Preserve the partitioning of the [B] dimension for all operands.
  • Always all-gather LHS along the M dimension.
  • Error out if RHS is partitioned in both K and N dimensions.
  • Force the K dimension of LHS to match the partitioning of the K dimension of RHS.
  • If K dimension is partitioned but M dimension is not, jax.lax.psum (all-reduce) the output over the TP mesh resource.
  • If both the M and K dimensions are partitioned, jax.lax.psum_scatter (reduce-scatter) the output over the TP mesh resource.

In practice, the RHS matrix (typically the weight tensor) should be allocated with transposed contracting dimensions ([B,] N, K) for optimal GEMM heuristics in cuBlasLt. This layout is also mandatory for FP8 inputs.

This PR does NOT update fused ops or Flax/Praxis modules to use the new GEMM custom op over the existing XLA pattern matching approach.

Type of change

  • Documentation change (change only to the documentation, either a fix or a new content)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Infra/Build change
  • Code refractor

Changes

  • Added XLA custom calls for nvte_cublas_gemm.
  • Added JAX primitive for the new XLA custom call.
  • Added new serial unit test.
  • Add distributed unit test.

Checklist:

  • I have read and followed the contributing guidelines
  • The functionality is complete
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

@denera denera added the jax label Nov 2, 2024
@denera denera requested review from nouiz and phu0ngng November 2, 2024 02:30
@denera denera self-assigned this Nov 2, 2024
@denera denera changed the title [JAX] Collective GEMM custom op with nvte_cublas_gemm [JAX] Collective GEMM custom op with nvte_cublas_gemm (no comm. overlap) Nov 2, 2024
@nouiz
Copy link
Collaborator

nouiz commented Nov 4, 2024

Why? Normal JAX behavior is to do some gathering.

@huanghua1994
Copy link
Collaborator

It seems that currently the batch size is not handled in the C++ code. Since JAX is using row-major storage for tensor by default, probably the batch dimension should be combined with the m dimension for LHS or the n dimension for RHS?

@denera denera force-pushed the jax-collective-gemm branch from bb2be56 to fea0728 Compare November 6, 2024 02:13
@denera denera force-pushed the jax-collective-gemm branch 2 times, most recently from 6444211 to f440094 Compare November 14, 2024 18:14
Copy link

@abhinavgoel95 abhinavgoel95 left a comment

Choose a reason for hiding this comment

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

@denera I have some questions about the PR.


# Validate operand layouts
lhs_inner_dim, rhs_inner_dim = map(
lambda inner_dim, ndims: (ndims - inner_dim) if inner_dim < 0 else inner_dim,

Choose a reason for hiding this comment

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

@denera should be ndims + inner_dim when inner_dim is negative, right?

rhs_trans = contracting_dims[1] == rhs.ndim - 1
lhs = jnp.matrix_transpose(lhs) if lhs_trans and jax_dtype_is_fp8(lhs.dtype) else lhs
rhs = jnp.matrix_transpose(rhs) if not rhs_trans and jax_dtype_is_fp8(rhs.dtype) else rhs
contracting_dims = (1, 1)

Choose a reason for hiding this comment

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

@denera is there a need to hard-code this?

Copy link
Collaborator Author

@denera denera Nov 15, 2024

Choose a reason for hiding this comment

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

cuBlasLt GEMM requires non-transposed LHS and transposed RHS for FP8 GEMM, but the batcher is not the right place to check/force that. Also, leaving contracting_dims=(1, 1) out of the conditional for FP8 type is a mistake. Thanks for catching it!

grad=grad,
accumulate=accumulate,
use_split_accumulator=use_split_accumulator,
)(lhs_bdims, out_amax_bdims, out_scale_bdims, gelu_input_bdims, bias_bdims)

Choose a reason for hiding this comment

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

@denera denera force-pushed the jax-collective-gemm branch 3 times, most recently from f057def to 718c03d Compare November 21, 2024 11:38
@denera denera marked this pull request as ready for review December 3, 2024 15:05
@denera denera force-pushed the jax-collective-gemm branch from 09e2316 to 39bd494 Compare December 3, 2024 15:06
Comment on lines +116 to +117
self._amax_list[FP8MetaPackage.OUTPUT_IDX] = output_amax
self._scale_list[FP8MetaPackage.OUTPUT_IDX] = output_scale
Copy link
Collaborator

@phu0ngng phu0ngng Dec 5, 2024

Choose a reason for hiding this comment

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

Hi,
For the delayed scaling FP8 recipe, the output amax and scale from GEMM are not used anywhere else afterward, so I think we don't need to output and store them.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The FP8 GEMM+RS overlap needs output amax/scale when the communication buffer type is FP8 -- i.e. the overlap algorithms/kernels communicate FP8 GEMM output and fuse BF16 upcast into the sum-reduce.

This PR does not implement TP overlap, but PR #1337 extends the same operations to support TP overlap, so I'm including the output amax/scale infrastructure here.

)
return a, a_q, jnp.reciprocal(a_scale), b, b_q, jnp.reciprocal(b_scale), bias

@pytest.mark.parametrize("m,n,k", GEMM_CASES)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Need to provide a list for test parameter b (batch size)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Actually this PR is not supposed to modify test_custom_call_compute.py. These changes are erroneous and need to be removed. Thank you for catching it!

def test_gemm(self, b, m, n, k, use_bias, do_gelu):
a, b, bias = self._generate_inputs(b, m, n, k, jnp.bfloat16)

primitive_out = gemm(a, b, bias=bias if use_bias else None, layout="NT", do_gelu=do_gelu)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we really need to provide or use layout parameter here? On one hand, user or other functions in TE is unlikely to use this argument (I think C/C++ code would need it but not python code), on the other hand does it make dist-mem sharding complicated?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Changes to this file are erroneous and I just pushed up a commit to restore the original.

All testing for the new collective GEMM custom op are written in test_distributed_gemm.py instead.

denera and others added 9 commits December 5, 2024 21:33
Signed-off-by: Alp Dener <[email protected]>

Added XLA FFI custom op for TE GEMM

Signed-off-by: Alp Dener <[email protected]>

finished GEMM custom op primitive and serial unit test

Signed-off-by: Alp Dener <[email protected]>

fixed GEMM custom op batcher

Signed-off-by: Alp Dener <[email protected]>

fixed output dtype error and contracting dimensions options

Signed-off-by: Alp Dener <[email protected]>

AG overlap working but executes scatter to match outer LHS dim

Signed-off-by: Alp Dener <[email protected]>

both all-gather and all-reduce are now working

Signed-off-by: Alp Dener <[email protected]>

code style

Signed-off-by: Alp Dener <[email protected]>

changed kwargs in abstract to be explicit

Signed-off-by: Alp Dener <[email protected]>

added fwd/bwd implementation for non-fp8 gemm

Signed-off-by: Alp Dener <[email protected]>
@denera denera force-pushed the jax-collective-gemm branch from 07a2fb3 to f68d71e Compare December 5, 2024 21:33
resources.update(dict(dp_resource="dp"))
if parallel_dist == "FSDP_TP":
fsdp = True
mesh_shape.update(dict(tp=NUM_DEVICES // 2, dp=1, zp=NUM_DEVICES // 2))
Copy link
Collaborator

Choose a reason for hiding this comment

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

This mesh shape calculation is incorrect. Suggested revision:

    if parallel_dist in ["DP_TP", "FSDP_TP"]:
        batched = True
        tp = NUM_DEVICES // 2
        dp = NUM_DEVICES // tp
        mesh_shape.update(dict(tp=tp, dp=dp))
        resources.update(dict(dp_resource="dp"))
        if parallel_dist == "FSDP_TP":
            fsdp = True
            dp = 1
            zp = NUM_DEVICES // tp
            mesh_shape.update(dict(tp=tp, dp=1, zp=zp))
            resources.update(dict(fsdp_resource="zp"))

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

Successfully merging this pull request may close these issues.

5 participants