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

Fuse cumsum into stacked FP8 Grouped Gemm #3814

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions fbgemm_gpu/experimental/gen_ai/bench/quantize_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,55 @@ def cuda(self) -> bool:
return True


class FP8StackedGroupedGemm(QuantizeOpBase):
"""
FP8 grouped matmul with rowwise scaling and stacked inputs.
"""

def preprocess(self, x, w):
m_values = [i.shape[0] for i in x]
m_sizes = torch.tensor(m_values).to(dtype=torch.int64, device=x[0].device)
# Quantize weights.
wq, w_scale = zip(*[quantize_fp8_row(i) for i in w])
# Group weights as single tensor.
wq = torch.stack(wq, dim=0).contiguous()
w_scale = torch.stack(w_scale, dim=0).contiguous()
# Also view input as flattened.
x = torch.concat(x, dim=0).contiguous()
# Return processed tensors.
return x, wq, w_scale, m_sizes

def quantize(self, x, wq, w_scale, m_sizes):
B = x.shape[0]
xq, x_scale = triton_quantize_fp8_row(x)
x_scale = x_scale.view(B, -1)
return xq, wq, x_scale, w_scale, m_sizes

def compute(self, xq, wq, x_scale, w_scale, m_sizes):
return torch.ops.fbgemm.f8f8bf16_rowwise_grouped_stacked(
xq, wq, x_scale, w_scale, m_sizes
)

def quantize_and_compute(self, x, wq, w_scale, m_sizes):
xq, wq, x_scale, w_scale, m_sizes = self.quantize(x, wq, w_scale, m_sizes)
return self.compute(xq, wq, x_scale, w_scale, m_sizes)

@property
def name(self) -> str:
if torch.version.cuda:
return "cutlass_grouped_stacked"
else:
return "ck_grouped_stacked"

@property
def hip(self) -> bool:
return True

@property
def cuda(self) -> bool:
return True


@register_quantize_op
class BF16GroupedGemm(QuantizeOpBase):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ at::Tensor f8f8bf16_rowwise_batched_impl(
int B = XQ.size(0);
int M = XQ.size(1);
int N = WQ.size(1);
int K = XQ.size(2);
int K = WQ.size(2);

int StrideA = K;
int StrideB = K;
Expand Down
Loading
Loading