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

Connecting fp16xq4 gemm kernels (optimized for A100) to MatMulNBits<fp16> operator #21083

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

Conversation

chenfucn
Copy link
Contributor

@chenfucn chenfucn commented Jun 18, 2024

Description

This change hookup two A100 specialized gemm kernels to MatMulNBits.Float16 operator.

Also created a hacky solution of quantized weight prepacking that works in GPU operators.

Motivation and Context

Using specialized A100 kernels to accelerate MatMulNBits operator.

Currently we have two kernels:

  1. blkq4_gemm_sm80 is optimized for throughput. It works well with large size GEMM problems and achieves more than 95% of peak device FLOPS. When the GEMM size is small, part of the device would remain idle.
  2. blkq4_small_gemm_sm80 is crafted specifically for very small size GEMM problems, trying to spread the computation to the entire device. This kernel achieves lower latency for small GEMMs.

blkq4_fp16_gemm_sm80_dispatch selectively dispatch to one of these two kernels based on GEMM size. In the future lots of tuning can be done here.

Manually ran the following tests on A100 devices:

GpuOpPrepackTests.MatmulNBits
MatMulNBits.Float16
MatMulNBits.Float16Large

Prepacking for GPU operators

Our kernels require weight prepacking. Current prepacking infrastructure works well with CPU operators. Unfortunately, using it in GPU operator causes memory bloat -- old GPU memory buffers holding not yet packed data are not released after prepacking. This is especially problematic for LLMs.

In this change we developed a prepacking logic in a new graph optimizer. This solves the memory bloat problem. However, it also introduces the following problems:

  1. Rewriting of the initializer tensors is restricted by operator shape inferencing rules. For example, there are 3 initializers for MatMulNBits, we can't combine them into a single tensor. And we have to remove part of the operator's shape inference logic, in case it freaks out about the shape of the packed tensors.
  2. This rewriting logic is tightly coupled to each GPU operators. It really should be defined together with the operators. Now we are forced to put them in a graph optimizer module that has nothing to do with the operators. This increases fragmentation and maintenance cost.
  3. The logic of prepacking depends on underlying GPU hardware. Currently this part is hard-coded for SM80.
    In the operator, we have to expose a property to remember whether the operator is prepacked by the optimizer. This property is exposed to the user. But it should never be set by user.

We really need to find a better solution for GPU operator prepacking next. These problems are significant.

@chenfucn chenfucn changed the title Add fp16xq4 matmul sm80 cuda kernel to ORT operator Connecting fp16xq4 gemm kernels (optimized for A100) to ORT operator Jun 18, 2024
@chenfucn chenfucn changed the title Connecting fp16xq4 gemm kernels (optimized for A100) to ORT operator Connecting fp16xq4 gemm kernels (optimized for A100) to MatMulNBits<fp16> operator Jun 18, 2024
@chenfucn chenfucn requested review from yufenglee and a team June 18, 2024 17:28
@chenfucn chenfucn force-pushed the cfu_transform_prepack branch from 45e0036 to 3fc508c Compare July 2, 2024 23:19
And to use graph transformer as prepack
@chenfucn chenfucn force-pushed the cfu_transform_prepack branch from 5bf1322 to d9fae0c Compare July 3, 2024 17:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant