-
Notifications
You must be signed in to change notification settings - Fork 28
Implement swizzle gemm for flux dit and wan dit #204
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ | |
| ) | ||
| from diffsynth_engine.utils.gguf import gguf_inference | ||
| from diffsynth_engine.utils.fp8_linear import fp8_inference | ||
| from diffsynth_engine.utils.aiter_linear import use_swizzle_hipblaslt | ||
| from diffsynth_engine.utils.parallel import ( | ||
| cfg_parallel, | ||
| cfg_parallel_unshard, | ||
|
|
@@ -390,6 +391,7 @@ def forward( | |
| use_cfg = x.shape[0] > 1 | ||
| with ( | ||
| fp8_inference(fp8_linear_enabled), | ||
| use_swizzle_hipblaslt(swizzle=True, use_fp8_linear=fp8_linear_enabled), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| gguf_inference(), | ||
| cfg_parallel((x, context, timestep, clip_feature, y), use_cfg=use_cfg), | ||
| ): | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,110 @@ | ||||||||||||||||||||||||||||||||||
| import torch | ||||||||||||||||||||||||||||||||||
| import torch.nn as nn | ||||||||||||||||||||||||||||||||||
| import torch.nn.functional as F | ||||||||||||||||||||||||||||||||||
| from functools import lru_cache | ||||||||||||||||||||||||||||||||||
| from aiter import hipb_mm, hipb_create_extension, per_tensor_quant_hip | ||||||||||||||||||||||||||||||||||
| from aiter.tuned_gemm import tgemm | ||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||||||||||||||||||||||||
| from aiter.ops.shuffle import shuffle_weight | ||||||||||||||||||||||||||||||||||
| from diffsynth_engine.utils.platform import DTYPE_FP8 | ||||||||||||||||||||||||||||||||||
| from contextlib import contextmanager | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| @lru_cache(maxsize=1) | ||||||||||||||||||||||||||||||||||
| def init_hipblas(): | ||||||||||||||||||||||||||||||||||
| hipb_create_extension() | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| @contextmanager | ||||||||||||||||||||||||||||||||||
| def use_swizzle_hipblaslt(swizzle=True, use_fp8_linear=True, use_scale_for_fp8=False): | ||||||||||||||||||||||||||||||||||
| if not swizzle: | ||||||||||||||||||||||||||||||||||
| yield | ||||||||||||||||||||||||||||||||||
| return | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| # Preserve original F.linear | ||||||||||||||||||||||||||||||||||
| _original_linear = F.linear | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| def optimized_linear(input, weight, bias=None, otype=torch.bfloat16, | ||||||||||||||||||||||||||||||||||
| scaleA=None, scaleB=None, device="cuda"): | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| input_flat = input.reshape(-1, input.shape[-1]) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| init_hipblas() | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| weight_preshuffle = shuffle_weight(weight.contiguous(), layout=(16, 16), use_int4=False).to(device) | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+26
to
+33
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Suggested change
|
||||||||||||||||||||||||||||||||||
| output_flat = hipb_mm( | ||||||||||||||||||||||||||||||||||
| input_flat, | ||||||||||||||||||||||||||||||||||
| weight_preshuffle.t(), | ||||||||||||||||||||||||||||||||||
| bias=bias, | ||||||||||||||||||||||||||||||||||
| solution_index=-1, | ||||||||||||||||||||||||||||||||||
| out_dtype=otype, | ||||||||||||||||||||||||||||||||||
| scaleA=scaleA, | ||||||||||||||||||||||||||||||||||
| scaleB=scaleB, | ||||||||||||||||||||||||||||||||||
| scaleOut=None, | ||||||||||||||||||||||||||||||||||
| bpreshuffle=True | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| # Reshape output to match input dimensions | ||||||||||||||||||||||||||||||||||
| new_shape = input.shape[:-1] + (weight.shape[0],) | ||||||||||||||||||||||||||||||||||
| output = output_flat.view(new_shape) | ||||||||||||||||||||||||||||||||||
| return output | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+26
to
+49
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The def optimized_linear(input, weight, bias=None, otype=torch.bfloat16,
scaleA=None, scaleB=None):
input_flat = input.reshape(-1, input.shape[-1])
init_hipblas()
weight_preshuffle = shuffle_weight(weight.contiguous(), layout=(16, 16), use_int4=False).to(input.device)
output_flat = hipb_mm(
input_flat,
weight_preshuffle.t(),
bias=bias,
solution_index=-1,
out_dtype=otype,
scaleA=scaleA,
scaleB=scaleB,
scaleOut=None,
bpreshuffle=True
)
# Reshape output to match input dimensions
new_shape = input.shape[:-1] + (weight.shape[0],)
output = output_flat.view(new_shape)
return output |
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| def optimized_linear_fp8(input, weight, bias=None, otype=torch.bfloat16, | ||||||||||||||||||||||||||||||||||
| scaleA=None, scaleB=None, device="cuda"): | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| input_flat = input.reshape(-1, input.shape[-1]) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| if use_scale_for_fp8: | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| input_flat, a_scale = per_tensor_quant_hip(input_flat, quant_dtype=DTYPE_FP8) | ||||||||||||||||||||||||||||||||||
| weight = weight.to(DTYPE_FP8) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| init_hipblas() | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| weight_preshuffle = shuffle_weight(weight.contiguous(), layout=(16, 16)).to(device) | ||||||||||||||||||||||||||||||||||
| output_flat = hipb_mm( | ||||||||||||||||||||||||||||||||||
| input_flat, | ||||||||||||||||||||||||||||||||||
| weight_preshuffle.t(), | ||||||||||||||||||||||||||||||||||
| bias=bias, | ||||||||||||||||||||||||||||||||||
| solution_index=-1, | ||||||||||||||||||||||||||||||||||
| out_dtype=otype, | ||||||||||||||||||||||||||||||||||
| scaleA=a_scale, | ||||||||||||||||||||||||||||||||||
| scaleB=scaleB, | ||||||||||||||||||||||||||||||||||
| scaleOut=None, | ||||||||||||||||||||||||||||||||||
| bpreshuffle=True | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||
| input_flat = input_flat.to(DTYPE_FP8) | ||||||||||||||||||||||||||||||||||
| weight = weight.to(DTYPE_FP8) | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| init_hipblas() | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| weight_preshuffle = shuffle_weight(weight.contiguous(), layout=(16, 16)).to(device) | ||||||||||||||||||||||||||||||||||
| output_flat = hipb_mm( | ||||||||||||||||||||||||||||||||||
| input_flat, | ||||||||||||||||||||||||||||||||||
| weight_preshuffle.t(), | ||||||||||||||||||||||||||||||||||
| bias=bias, | ||||||||||||||||||||||||||||||||||
| solution_index=-1, | ||||||||||||||||||||||||||||||||||
| out_dtype=otype, | ||||||||||||||||||||||||||||||||||
| scaleA=scaleA, | ||||||||||||||||||||||||||||||||||
| scaleB=scaleB, | ||||||||||||||||||||||||||||||||||
| scaleOut=None, | ||||||||||||||||||||||||||||||||||
| bpreshuffle=True | ||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+52
to
+94
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function can be improved in two ways:
I've provided a suggestion that addresses both points. def optimized_linear_fp8(input, weight, bias=None, otype=torch.bfloat16,
scaleA=None, scaleB=None):
input_flat = input.reshape(-1, input.shape[-1])
a_scale_for_mm = scaleA
if use_scale_for_fp8:
input_flat, a_scale_for_mm = per_tensor_quant_hip(input_flat, quant_dtype=DTYPE_FP8)
else:
input_flat = input_flat.to(DTYPE_FP8)
weight = weight.to(DTYPE_FP8)
init_hipblas()
weight_preshuffle = shuffle_weight(weight.contiguous(), layout=(16, 16)).to(input.device)
output_flat = hipb_mm(
input_flat,
weight_preshuffle.t(),
bias=bias,
solution_index=-1,
out_dtype=otype,
scaleA=a_scale_for_mm,
scaleB=scaleB,
scaleOut=None,
bpreshuffle=True
) |
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| # Reshape output to match input dimensions | ||||||||||||||||||||||||||||||||||
| new_shape = input.shape[:-1] + (weight.shape[0],) | ||||||||||||||||||||||||||||||||||
| output = output_flat.view(new_shape) | ||||||||||||||||||||||||||||||||||
| return output | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+52
to
+100
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function has a couple of issues:
def optimized_linear_fp8(input, weight, bias=None, otype=torch.bfloat16,
scaleA=None, scaleB=None):
input_flat = input.reshape(-1, input.shape[-1])
device = input.device
if use_scale_for_fp8:
input_flat, a_scale = per_tensor_quant_hip(input_flat, quant_dtype=DTYPE_FP8)
scaleA = a_scale
else:
input_flat = input_flat.to(DTYPE_FP8)
weight = weight.to(DTYPE_FP8)
init_hipblas()
weight_preshuffle = shuffle_weight(weight.contiguous(), layout=(16, 16)).to(device)
output_flat = hipb_mm(
input_flat,
weight_preshuffle.t(),
bias=bias,
solution_index=-1,
out_dtype=otype,
scaleA=scaleA,
scaleB=scaleB,
scaleOut=None,
bpreshuffle=True
)
# Reshape output to match input dimensions
new_shape = input.shape[:-1] + (weight.shape[0],)
output = output_flat.view(new_shape)
return output |
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| if use_fp8_linear: | ||||||||||||||||||||||||||||||||||
| F.linear = optimized_linear_fp8 | ||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||
| F.linear = optimized_linear | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| yield | ||||||||||||||||||||||||||||||||||
| F.linear = _original_linear | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
swizzleparameter is hardcoded toTrue. This makes it difficult to disable this optimization without changing the code. Consider making this configurable, for example, through a class attributeself.swizzle_enabledthat can be set during model initialization.