-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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
[Kernel] Initial Machete W4A8 support + Refactors #9855
Merged
mgoin
merged 9 commits into
vllm-project:main
from
neuralmagic:lwilkinson/machete-w4a8-signed
Nov 18, 2024
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
09a7060
rebase and sign
LucasWilkinson 88426d0
fix format
LucasWilkinson 2f3a49e
format
LucasWilkinson 78dd9dd
minor cleanup
LucasWilkinson 30d0af3
review comments
LucasWilkinson f140152
review comments
LucasWilkinson 1993f3b
minor comment tweak
LucasWilkinson 563f80c
review comments
LucasWilkinson 70ad239
review comments
LucasWilkinson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
317 changes: 317 additions & 0 deletions
317
csrc/cutlass_extensions/epilogue/scaled_mm_epilogues_c2x.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,317 @@ | ||
#include "cutlass_extensions/epilogue/broadcast_load_epilogue_c2x.hpp" | ||
|
||
/* | ||
This file defines custom epilogues for fusing channel scales, token scales, | ||
bias, and activation zero-points onto a GEMM operation using the | ||
CUTLASS 2.x API, for sm80 (Ampere) NVIDIA GPUs. | ||
Epilogues must contain a public type named EVTCompute of type Sm80EVT, | ||
as well as a static prepare_args function that constructs an | ||
EVTCompute::Arguments struct. | ||
*/ | ||
|
||
namespace vllm::c2x { | ||
|
||
using namespace cute; | ||
|
||
/* | ||
* This class provides the common load descriptors for the | ||
* ScaledEpilogue[...] classes | ||
*/ | ||
template <typename ElementD, typename OutputTileThreadMap> | ||
struct ScaledEpilogueBase { | ||
protected: | ||
using Accum = cutlass::epilogue::threadblock::VisitorAccFetch; | ||
|
||
template <typename T> | ||
using ColOrScalarLoad = | ||
cutlass::epilogue::threadblock::VisitorColOrScalarBroadcast< | ||
OutputTileThreadMap, T, Stride<Int<1>, Int<0>, Int<0>>>; | ||
|
||
template <typename T> | ||
using RowOrScalarLoad = | ||
cutlass::epilogue::threadblock::VisitorRowOrScalarBroadcast< | ||
OutputTileThreadMap, T, Stride<Int<0>, Int<1>, Int<0>>>; | ||
|
||
template <typename T> | ||
using ColLoad = cutlass::epilogue::threadblock::VisitorColBroadcast< | ||
OutputTileThreadMap, T, Stride<Int<1>, Int<0>, Int<0>>>; | ||
|
||
template <typename T> | ||
using RowLoad = cutlass::epilogue::threadblock::VisitorRowBroadcast< | ||
OutputTileThreadMap, T, Stride<Int<0>, Int<1>, Int<0>>>; | ||
|
||
template <typename T> | ||
using RowOrZeroLoad = | ||
cutlass::epilogue::threadblock::VisitorRowOrZeroBroadcast< | ||
OutputTileThreadMap, T, Stride<Int<0>, Int<1>, Int<0>>>; | ||
|
||
// This utility function constructs the arguments for the load descriptors | ||
// from a tensor. It can handle both row and column, as well as row/column or | ||
// scalar cases. | ||
template <typename Descriptor, typename T> | ||
static auto args_from_tensor(torch::Tensor const& tensor) { | ||
using Arguments = typename Descriptor::Arguments; | ||
auto* data_ptr = static_cast<T*>(tensor.data_ptr()); | ||
if constexpr (std::is_same_v<Descriptor, ColOrScalarLoad<T>> || | ||
std::is_same_v<Descriptor, RowOrScalarLoad<T>>) { | ||
return Arguments{data_ptr, tensor.numel() != 1}; | ||
} else { | ||
// it would technically work but no use case as data_ptr is never nullptr | ||
static_assert(!std::is_same_v<Descriptor, RowOrZeroLoad<T>>); | ||
return Arguments{data_ptr}; | ||
} | ||
} | ||
|
||
// This overload handles the case where there might not be a tensor, in which | ||
// case a nullptr is passed and a constant (0) is used. | ||
template <typename Descriptor, typename T> | ||
static auto args_from_tensor(c10::optional<torch::Tensor> const& tensor) { | ||
static_assert(std::is_same_v<Descriptor, RowOrZeroLoad<T>>); | ||
using Arguments = typename Descriptor::Arguments; | ||
auto* data_ptr = tensor ? static_cast<T*>(tensor->data_ptr()) : nullptr; | ||
return Arguments{data_ptr}; | ||
} | ||
}; | ||
|
||
/* | ||
This epilogue function defines a quantized GEMM operation similar to | ||
torch._scaled_mm. | ||
A and B may be both either int8 or fp8_e4m3. A can be quantized per-tensor or | ||
per-row. B can be quantized per-tensor or per-column. | ||
Any combination of per-tensor and per-row or column is supported. | ||
A and B must have symmetric quantization (zero point == 0). | ||
So the GEMM operation is D = (a_scales * A) (b_scales * B), where the | ||
scales are applied elementwise with numpy-style broadcasting. | ||
ScaleA and ScaleB define the epilogue functions that apply the scales for | ||
the A and B operands respectively. These scales may be either per-tensor or | ||
per row or column. | ||
*/ | ||
template <typename ElementD, typename OutputTileThreadMap> | ||
struct ScaledEpilogue | ||
: private ScaledEpilogueBase<ElementD, OutputTileThreadMap> { | ||
private: | ||
using SUPER = ScaledEpilogueBase<ElementD, OutputTileThreadMap>; | ||
using Accum = typename SUPER::Accum; | ||
using ScaleA = typename SUPER::template ColOrScalarLoad<float>; | ||
using ScaleB = typename SUPER::template RowOrScalarLoad<float>; | ||
|
||
using Compute0 = cutlass::epilogue::threadblock::VisitorCompute< | ||
cutlass::multiplies, float, float, | ||
cutlass::FloatRoundStyle::round_to_nearest>; | ||
|
||
using EVTCompute0 = | ||
cutlass::epilogue::threadblock::Sm80EVT<Compute0, ScaleB, Accum>; | ||
|
||
using Compute1 = cutlass::epilogue::threadblock::VisitorCompute< | ||
cutlass::multiplies, ElementD, float, | ||
cutlass::FloatRoundStyle::round_to_nearest>; | ||
|
||
public: | ||
using EVTCompute = | ||
cutlass::epilogue::threadblock::Sm80EVT<Compute1, ScaleA, EVTCompute0>; | ||
using ArgumentType = typename EVTCompute::Arguments; | ||
|
||
static ArgumentType prepare_args(torch::Tensor const& a_scales, | ||
torch::Tensor const& b_scales) { | ||
auto a_args = SUPER::template args_from_tensor<ScaleA, float>(a_scales); | ||
auto b_args = SUPER::template args_from_tensor<ScaleB, float>(b_scales); | ||
|
||
typename EVTCompute0::Arguments evt0_args{b_args}; | ||
return ArgumentType{a_args, evt0_args}; | ||
} | ||
}; | ||
|
||
/* | ||
* This epilogue performs the same operation as ScaledEpilogue, but adds a bias. | ||
* This bias can also be used in the per-tensor azp case, where the activation | ||
* zero point (azp) is used to compute an azp correction term, | ||
* which is folded into the bias. | ||
* | ||
* The bias tensor must be per-output channel. | ||
* ScaleA and ScaleB can be per-tensor or per-token/per-channel. | ||
*/ | ||
template <typename ElementD, typename OutputTileThreadMap> | ||
struct ScaledEpilogueBias | ||
: protected ScaledEpilogueBase<ElementD, OutputTileThreadMap> { | ||
protected: | ||
using SUPER = ScaledEpilogueBase<ElementD, OutputTileThreadMap>; | ||
using Accum = typename SUPER::Accum; | ||
using ScaleA = typename SUPER::template ColOrScalarLoad<float>; | ||
using ScaleB = typename SUPER::template RowOrScalarLoad<float>; | ||
using Bias = typename SUPER::template RowLoad<ElementD>; | ||
using Compute0 = cutlass::epilogue::threadblock::VisitorCompute< | ||
cutlass::multiplies, float, float, | ||
cutlass::FloatRoundStyle::round_to_nearest>; | ||
|
||
using EVTCompute0 = | ||
cutlass::epilogue::threadblock::Sm80EVT<Compute0, ScaleB, Accum>; | ||
|
||
using Compute1 = cutlass::epilogue::threadblock::VisitorCompute< | ||
cutlass::multiply_add, ElementD, float, | ||
cutlass::FloatRoundStyle::round_to_nearest>; | ||
|
||
public: | ||
using EVTCompute = cutlass::epilogue::threadblock::Sm80EVT<Compute1, ScaleA, | ||
EVTCompute0, Bias>; | ||
using ArgumentType = typename EVTCompute::Arguments; | ||
static ArgumentType prepare_args(torch::Tensor const& a_scales, | ||
torch::Tensor const& b_scales, | ||
torch::Tensor const& bias) { | ||
auto a_args = SUPER::template args_from_tensor<ScaleA, float>(a_scales); | ||
auto b_args = SUPER::template args_from_tensor<ScaleB, float>(b_scales); | ||
auto bias_args = SUPER::template args_from_tensor<Bias, ElementD>(bias); | ||
|
||
typename EVTCompute0::Arguments evt0_args{b_args}; | ||
return ArgumentType{a_args, evt0_args, bias_args}; | ||
} | ||
}; | ||
|
||
/* | ||
* This epilogue directly supports per-tensor azp in int32 form. | ||
* As opposed to the per-token epilogue below, this epilogue only has an azp_adj | ||
* term, which should already be multiplied with the scalar azp. | ||
* The azp_adj term is a 1D tensor of shape (1,n), computed as azp * J @ B. | ||
* | ||
* This epilogue also supports bias, which remains per-channel. | ||
*/ | ||
template <typename ElementD, typename OutputTileThreadMap> | ||
struct ScaledEpilogueBiasAzp | ||
: protected ScaledEpilogueBase<ElementD, OutputTileThreadMap> { | ||
private: | ||
using SUPER = ScaledEpilogueBase<ElementD, OutputTileThreadMap>; | ||
using Accum = typename SUPER::Accum; | ||
using ScaleA = typename SUPER::template ColOrScalarLoad<float>; | ||
using ScaleB = typename SUPER::template RowOrScalarLoad<float>; | ||
using Bias = typename SUPER::template RowOrZeroLoad<ElementD>; | ||
|
||
// This is the full AZP term, azp * J @ B, shape (1,n) | ||
using AzpWithAdj = typename SUPER::template RowLoad<int32_t>; | ||
|
||
// Compute float(accum - azp_adj), both operands are int32_t | ||
using ComputeAzp = cutlass::epilogue::threadblock::VisitorCompute< | ||
cutlass::minus, float, int32_t, | ||
cutlass::FloatRoundStyle::round_to_nearest>; | ||
|
||
using EVTComputeAzp = | ||
cutlass::epilogue::threadblock::Sm80EVT<ComputeAzp, Accum, AzpWithAdj>; | ||
|
||
using ComputeScaleB = cutlass::epilogue::threadblock::VisitorCompute< | ||
cutlass::multiplies, float, float, | ||
cutlass::FloatRoundStyle::round_to_nearest>; | ||
|
||
using EVTComputeScaleB = | ||
cutlass::epilogue::threadblock::Sm80EVT<ComputeScaleB, ScaleB, | ||
EVTComputeAzp>; | ||
|
||
using ComputeScaleBiasA = cutlass::epilogue::threadblock::VisitorCompute< | ||
cutlass::multiply_add, ElementD, float, | ||
cutlass::FloatRoundStyle::round_to_nearest>; | ||
|
||
public: | ||
using EVTCompute = | ||
cutlass::epilogue::threadblock::Sm80EVT<ComputeScaleBiasA, ScaleA, | ||
EVTComputeScaleB, Bias>; | ||
|
||
using ArgumentType = typename EVTCompute::Arguments; | ||
|
||
static ArgumentType prepare_args(torch::Tensor const& a_scales, | ||
torch::Tensor const& b_scales, | ||
torch::Tensor const& azp_adj, | ||
c10::optional<torch::Tensor> const& bias) { | ||
auto a_args = SUPER::template args_from_tensor<ScaleA, float>(a_scales); | ||
auto b_args = SUPER::template args_from_tensor<ScaleB, float>(b_scales); | ||
auto bias_args = SUPER::template args_from_tensor<Bias, ElementD>(bias); | ||
auto azp_adj_args = | ||
SUPER::template args_from_tensor<AzpWithAdj, int32_t>(azp_adj); | ||
|
||
typename EVTComputeAzp::Arguments evt_azp_args{{}, azp_adj_args}; | ||
typename EVTComputeScaleB::Arguments evt_scale_b_args{b_args, evt_azp_args}; | ||
return ArgumentType{a_args, evt_scale_b_args, bias_args}; | ||
} | ||
}; | ||
|
||
/* | ||
* This epilogue supports per-token azp by computing and applying | ||
* the correction term using a rank-1 update. If the term were materialized, | ||
* it would require O(m*n) space, and this way it only requires O(m+n) space. | ||
* The azp term is a 1D tensor of shape (m,1), and represents the unscaled zero | ||
* point for each row of A. | ||
* The azp_adj term is a 1D tensor of shape (1,n), computed as J @ B. | ||
* | ||
* This epilogue also supports bias, which remains per-channel. | ||
*/ | ||
template <typename ElementD, typename OutputTileThreadMap> | ||
struct ScaledEpilogueBiasAzpToken | ||
: protected ScaledEpilogueBase<ElementD, OutputTileThreadMap> { | ||
private: | ||
using SUPER = ScaledEpilogueBase<ElementD, OutputTileThreadMap>; | ||
using Accum = typename SUPER::Accum; | ||
using ScaleA = typename SUPER::template ColOrScalarLoad<float>; | ||
using ScaleB = typename SUPER::template RowOrScalarLoad<float>; | ||
using Bias = typename SUPER::template RowOrZeroLoad<ElementD>; | ||
|
||
// Per-token azp term, shape (m,1) | ||
using Azp = typename SUPER::template ColLoad<int32_t>; | ||
|
||
// This is the AZP adjustment term, J @ B, shape (1,n) | ||
using AzpAdj = typename SUPER::template RowLoad<int32_t>; | ||
|
||
// Compute azp * azp_adj | ||
using ComputeAzp = cutlass::epilogue::threadblock::VisitorCompute< | ||
cutlass::multiplies, int32_t, int32_t, | ||
cutlass::FloatRoundStyle::round_to_nearest>; | ||
|
||
using EVTComputeAzp = | ||
cutlass::epilogue::threadblock::Sm80EVT<ComputeAzp, Azp, AzpAdj>; | ||
|
||
// Compute float(accum - azp*azp_adj), all operands are int32_t | ||
using ComputeAcc = cutlass::epilogue::threadblock::VisitorCompute< | ||
cutlass::minus, float, int32_t, | ||
cutlass::FloatRoundStyle::round_to_nearest>; | ||
|
||
using EVTComputeAcc = | ||
cutlass::epilogue::threadblock::Sm80EVT<ComputeAcc, Accum, EVTComputeAzp>; | ||
|
||
using ComputeScaleB = cutlass::epilogue::threadblock::VisitorCompute< | ||
cutlass::multiplies, float, float, | ||
cutlass::FloatRoundStyle::round_to_nearest>; | ||
|
||
using EVTComputeScaleB = | ||
cutlass::epilogue::threadblock::Sm80EVT<ComputeScaleB, ScaleB, | ||
EVTComputeAcc>; | ||
|
||
using ComputeScaleBiasA = cutlass::epilogue::threadblock::VisitorCompute< | ||
cutlass::multiply_add, ElementD, float, | ||
cutlass::FloatRoundStyle::round_to_nearest>; | ||
|
||
public: | ||
using EVTCompute = | ||
cutlass::epilogue::threadblock::Sm80EVT<ComputeScaleBiasA, ScaleA, | ||
EVTComputeScaleB, Bias>; | ||
|
||
using ArgumentType = typename EVTCompute::Arguments; | ||
|
||
static ArgumentType prepare_args(torch::Tensor const& a_scales, | ||
torch::Tensor const& b_scales, | ||
torch::Tensor const& azp_adj, | ||
torch::Tensor const& azp, | ||
c10::optional<torch::Tensor> const& bias) { | ||
auto a_args = SUPER::template args_from_tensor<ScaleA, float>(a_scales); | ||
auto b_args = SUPER::template args_from_tensor<ScaleB, float>(b_scales); | ||
auto bias_args = SUPER::template args_from_tensor<Bias, ElementD>(bias); | ||
auto azp_args = SUPER::template args_from_tensor<Azp, int32_t>(azp); | ||
auto azp_adj_args = | ||
SUPER::template args_from_tensor<AzpAdj, int32_t>(azp_adj); | ||
|
||
typename EVTComputeAzp::Arguments evt_azp_args{azp_args, azp_adj_args}; | ||
typename EVTComputeAcc::Arguments evt_acc_args{{}, evt_azp_args}; | ||
typename EVTComputeScaleB::Arguments evt_scale_b_args{b_args, evt_acc_args}; | ||
return ArgumentType{a_args, evt_scale_b_args, bias_args}; | ||
} | ||
}; | ||
|
||
}; // namespace vllm::c2x |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
nit : for big models, I have found it useful to have their realistic TPn counter-parts also (e.g. for the 70B case, add a 70B-TP4 case). That way we can just list that version in the 1GPU model benchmarking.
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.
You mean so you can list it as a string as opposed to using the
--tp-sizes
args?