flux: [BUG] Incorrect results from flux.AGKernel for some problem shapes

Describe the bug I am seeing different using torch.distributed.all_gather_into_tensor followed by torch.mm as compared to the flux fused AllGather GEMM. The problem seems to be limited to certain problem shapes.

To Reproduce Here is my short script to repro the issue. For this rerpo, I am running on 2 A100s, torch 2.3, CUDA 12.5, Flux is at 66e271642c85db202a445de8d372490ff0a4f436, and I’m building Flux with bash build.sh --arch 80 --package and installing the wheel.

import multiprocessing as mp
import torch

import flux

def test(tp_group, m, n, k, transpose_weight, local_copy, dtype):
    world_size = torch.distributed.get_world_size(tp_group)

    a = (-2 * torch.rand(m, k, dtype=dtype, device="cuda") + 1) / .1
    b = ((-2 * torch.rand(k, n, dtype=dtype, device="cuda") + 1) / .1)

    # Massage b depending on transpose_weight
    b_ag_gemm = b
    if not transpose_weight:
        b_ag_gemm = b.t().contiguous()

    # Run fused AllGather Gemm
    ag_gemm_op = flux.AGKernel(tp_group,
                               1,
                               8192,
                               n,
                               k,
                               dtype,
                               dtype,
                               transpose_weight=transpose_weight,
                               local_copy=local_copy)

    torch.distributed.barrier()
    ag_gemm_output = ag_gemm_op.forward(a, b_ag_gemm)

    # Run a torch AllGather followed by a GEMM

    a_gathered = torch.zeros(m * world_size, k, dtype=dtype, device="cuda")
    torch.distributed.all_gather_into_tensor(a_gathered, a, 0)
    torch_output = torch.mm(a_gathered, b)
    torch.distributed.barrier()

    if not torch.allclose(torch_output, ag_gemm_output, atol=1e-1, rtol=1e-1):
        difference = (torch_output - ag_gemm_output).to(dtype=torch.float32)
        print(f"""Error:  Max diff.
        Process: {torch.distributed.get_rank(tp_group)}.
        Arguments: {m}, {n}, {k}, {local_copy}, {transpose_weight}
        Torch output norm: {torch.norm(torch_output.to(dtype=torch.float32))}.
        AGGemm output norm: {torch.norm(ag_gemm_output.to(dtype=torch.float32))},
        Norm of difference: {torch.norm(difference)}""")


@torch.no_grad()
def initialize_process(rank, world_size):
    # Assign GPU to this process
    torch.cuda.set_device(rank)

    # Create a torch communicator
    torch.distributed.init_process_group(
        backend='nccl',
        init_method='tcp://localhost:12345',
        world_size=world_size,
        rank=rank
    )
    tp_group = torch.distributed.new_group(ranks=list(range(world_size)), backend="nccl")

    # Initialize pynvshmem using the torch communicator
    flux.init_flux_shm(tp_group)
    torch.cuda.synchronize()

    # These are OK
    test(tp_group, 16, 4096, 4096, False, False, torch.float16);
    test(tp_group, 16, 6144, 4096, False, False, torch.float16);

    # This produces a wrong answer
    test(tp_group, 16, 3072, 4096, False, False, torch.float16);

    # Clean up
    torch.distributed.destroy_process_group()

def main():
    torch.set_printoptions(precision=4)
    torch.set_printoptions(sci_mode=True)

    world_size = 2  # Number of processes to create
    processes = []

    for rank in range(world_size):
        p = mp.Process(target=initialize_process, args=(rank, world_size))
        p.start()
        processes.append(p)

    for p in processes:
        p.join()

if __name__ == "__main__":
    main()

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 3
  • Comments: 15 (7 by maintainers)

Most upvoted comments

Adding the reset signal fixes the correctness issues. My integration PR is up-to-date and fuses both GEMM+RS and AG+GEMM –https://github.com/vllm-project/vllm/pull/5917.

Would it be better for each worker to keep track of the current epoch (number of times the barrier has been used) and use that for the barrier state instead of making sure the barrier is zeroed out every time? That way you wouldn’t have to reset the signal buffer.

Do you mean a counter? It is possible, but would require some changes to the code and kernel as well IIRC. We tried that but end up using the reset method for simplicity. BTW there are also other tricks to hide this reset operation, such as double buffer etc.

This is good to close now, thanks!

Creating an op each time won’t help with performance. The slowdown is due to an unnecessary CUDA stream sync in the reset-signal. I’ve removed it in the new MR(https://github.com/bytedance/flux/pull/19). Are there still correctness issues after adding the reset_signal? Also, if possible, could you please push/update the integration codebase of both AG+RS? I’ll work on resolving the correctness/performance issues on my side as well. @tlrmchlsmth Thanks a lot!