-
Notifications
You must be signed in to change notification settings - Fork 67
Signed Quantized Weights #649
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
Merged
Merged
Changes from 12 commits
Commits
Show all changes
45 commits
Select commit
Hold shift + click to select a range
811af52
load rht quantized weights as signed codes
eugenebokhan ac85838
feed a8w4 weights to mxu as int4b tensors
eugenebokhan 3be7cad
split mxu fragment headers
eugenebokhan fb7d80c
use mpp matmul modes
eugenebokhan e1e6bfb
wip
eugenebokhan 52c6a58
merge main
eugenebokhan d751cb5
adapt gemv for signed weights
eugenebokhan c2854ae
Merge branch 'main' into signed-quantized-weights
eugenebokhan 2b5b0f9
add ActivationTransform kernel
eugenebokhan 3ef7f3c
add matmul gemv/gemm path routing
eugenebokhan 00c98be
migrate RHT to ActivationTransform
eugenebokhan ec9c484
Merge branch 'main' into signed-quantized-weights
eugenebokhan e8e6335
merge main
eugenebokhan 6503dd6
make activation transform outputs optional
eugenebokhan 67e150c
fix qlora output rht allocation size
eugenebokhan b1370a9
assert activation transform row width
eugenebokhan c50e9ad
drop native int8 shortcut from path select
eugenebokhan ec7ef4b
specialize signed weight codes
eugenebokhan 9525e38
clean up mxu fragment headers
eugenebokhan 7acfd8f
restore hadamard coverage as transform test
eugenebokhan 124782d
add signed weight gemv benchmark
eugenebokhan b383122
apply metal formatting
eugenebokhan 35326da
Merge branch 'main' into signed-quantized-weights
eugenebokhan f26111d
remove dead code and comments
eugenebokhan 09168e7
simplify matmul and transform plumbing
eugenebokhan 1c9dd16
consolidate activation transform tests
eugenebokhan 17109ff
decouple signed codes from prepared a
eugenebokhan 6c3fb9d
add signed weight gemm benchmark
eugenebokhan df5ad6e
rename gemm tiling selectors
eugenebokhan 6b87248
restore fragment matmul inlining
eugenebokhan 933996b
rename
eugenebokhan 03bf585
keep sign mask literal in w4 dequantize
eugenebokhan 3e54b85
bench bf16 paths with unsigned codes
eugenebokhan 9ac1655
replace activation transform flags with enum
eugenebokhan c76bdc7
drop signed weight investigation benches
eugenebokhan f870170
drop redundant quant argument helper
eugenebokhan e1c1ad2
simplify gemm tiling selection
eugenebokhan c053e40
carry b prologue and signed codes in matmul shape
eugenebokhan d122007
fold cpu hadamard into activation transform
eugenebokhan 258ad32
share quant b variant helper in tests
eugenebokhan 28e8922
add in-place mode to activation transform
eugenebokhan b951468
guard mxu tiling lookup on full precision a
eugenebokhan 70385d8
reuse metal quant runner in signed parity test
eugenebokhan e464090
keep bf16 bench paths comparable to main
eugenebokhan 7eec560
Merge branch 'main' into signed-quantized-weights
eugenebokhan 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
This file contains hidden or 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
30 changes: 30 additions & 0 deletions
30
crates/backend-uzu/src/backends/common/gpu_types/activation_transform.rs
This file contains hidden or 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,30 @@ | ||
| use bitflags::bitflags; | ||
|
|
||
| bitflags! { | ||
| #[repr(transparent)] | ||
| #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
| pub struct ActivationTransformOp: u32 { | ||
| const INPUT_RHT = 1 << 0; | ||
| const OUTPUT_RHT = 1 << 1; | ||
| const QUANTIZE = 1 << 2; | ||
| const GROUP_SUMS = 1 << 3; | ||
| } | ||
| } | ||
|
|
||
| impl ActivationTransformOp { | ||
| pub fn validate(self) -> Self { | ||
| assert!( | ||
| self.contains(Self::INPUT_RHT) ^ self.contains(Self::OUTPUT_RHT), | ||
| "exactly one of INPUT_RHT / OUTPUT_RHT is required, got {self:?}" | ||
| ); | ||
| assert!( | ||
| !self.contains(Self::QUANTIZE) || self.contains(Self::INPUT_RHT), | ||
| "QUANTIZE requires INPUT_RHT, got {self:?}" | ||
| ); | ||
| assert!( | ||
| !self.contains(Self::GROUP_SUMS) || self.contains(Self::QUANTIZE), | ||
| "GROUP_SUMS requires QUANTIZE, got {self:?}" | ||
| ); | ||
| self | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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
121 changes: 121 additions & 0 deletions
121
crates/backend-uzu/src/backends/common/kernel/activation_transform.rs
This file contains hidden or 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,121 @@ | ||
| use crate::backends::common::{ | ||
| Allocation, Backend, Encoder, Kernels, | ||
| gpu_types::{ActivationTransformOp, HADAMARD_TRANSFORM_BLOCK_SIZE}, | ||
| kernel::ActivationTransformKernel, | ||
| }; | ||
|
|
||
| pub struct ActivationTransform<B: Backend> { | ||
| kernel: <B::Kernels as Kernels>::ActivationTransformKernel, | ||
| ops: ActivationTransformOp, | ||
| group_size: u32, | ||
| } | ||
|
|
||
| impl<B: Backend> ActivationTransform<B> { | ||
| pub fn new( | ||
| context: &B::Context, | ||
| data_type: crate::data_type::DataType, | ||
| ops: ActivationTransformOp, | ||
| group_size: u32, | ||
| ) -> Result<Self, B::Error> { | ||
| let ops = ops.validate(); | ||
| let kernel = <B::Kernels as Kernels>::ActivationTransformKernel::new(context, data_type, ops)?; | ||
| Ok(Self { | ||
| kernel, | ||
| ops, | ||
| group_size, | ||
| }) | ||
| } | ||
|
|
||
| pub fn input_rht( | ||
| context: &B::Context, | ||
| data_type: crate::data_type::DataType, | ||
| ) -> Result<Self, B::Error> { | ||
| Self::new(context, data_type, ActivationTransformOp::INPUT_RHT, HADAMARD_TRANSFORM_BLOCK_SIZE as u32) | ||
| } | ||
|
|
||
| pub fn output_rht( | ||
| context: &B::Context, | ||
| data_type: crate::data_type::DataType, | ||
| ) -> Result<Self, B::Error> { | ||
| Self::new(context, data_type, ActivationTransformOp::OUTPUT_RHT, HADAMARD_TRANSFORM_BLOCK_SIZE as u32) | ||
| } | ||
|
|
||
| pub fn quantize( | ||
| context: &B::Context, | ||
| data_type: crate::data_type::DataType, | ||
| group_size: u32, | ||
| emit_group_sums: bool, | ||
| ) -> Result<Self, B::Error> { | ||
| let ops = if emit_group_sums { | ||
| ActivationTransformOp::INPUT_RHT | ActivationTransformOp::QUANTIZE | ActivationTransformOp::GROUP_SUMS | ||
| } else { | ||
| ActivationTransformOp::INPUT_RHT | ActivationTransformOp::QUANTIZE | ||
| }; | ||
| Self::new(context, data_type, ops, group_size) | ||
| } | ||
|
|
||
| /// FP Hadamard (input- or output-order depending on construction). | ||
| /// `input` and `output` must be distinct buffers. The two scratch buffers are | ||
| /// placeholders for the quantized outputs this mode does not write. | ||
| pub fn encode_fp( | ||
| &self, | ||
| input: &Allocation<B>, | ||
| output: &mut Allocation<B>, | ||
| q_scratch: &mut Allocation<B>, | ||
| scales_scratch: &mut Allocation<B>, | ||
| rht_factors: &Allocation<B>, | ||
| element_count: u32, | ||
| batch_size: u32, | ||
| encoder: &mut Encoder<B>, | ||
| ) { | ||
| assert!(!self.ops.contains(ActivationTransformOp::QUANTIZE)); | ||
| self.kernel.encode( | ||
| input, | ||
| output, | ||
| q_scratch, | ||
| scales_scratch, | ||
| None::<&mut Allocation<B>>, | ||
| rht_factors, | ||
| batch_size, | ||
| element_count, | ||
| self.group_size, | ||
| encoder, | ||
| ); | ||
| } | ||
|
|
||
| /// Input RHT + symmetric int8 quantization. | ||
| pub fn encode_quantize( | ||
| &self, | ||
| input: &Allocation<B>, | ||
| fp_scratch: &mut Allocation<B>, | ||
| q_out: &mut Allocation<B>, | ||
| scales_out: &mut Allocation<B>, | ||
| group_sums_out: Option<&mut Allocation<B>>, | ||
| rht_factors: &Allocation<B>, | ||
| batch_size: u32, | ||
| element_count: u32, | ||
| encoder: &mut Encoder<B>, | ||
| ) { | ||
| assert!(self.ops.contains(ActivationTransformOp::QUANTIZE)); | ||
| self.kernel.encode( | ||
| input, | ||
| fp_scratch, | ||
| q_out, | ||
| scales_out, | ||
| group_sums_out, | ||
| rht_factors, | ||
| batch_size, | ||
| element_count, | ||
| self.group_size, | ||
| encoder, | ||
| ); | ||
| } | ||
|
|
||
| pub fn ops(&self) -> ActivationTransformOp { | ||
| self.ops | ||
| } | ||
|
|
||
| pub fn emit_group_sums(&self) -> bool { | ||
| self.ops.contains(ActivationTransformOp::GROUP_SUMS) | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
21 changes: 21 additions & 0 deletions
21
crates/backend-uzu/src/backends/common/kernel/matmul/routing.rs
This file contains hidden or 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,21 @@ | ||
| use crate::backends::common::gpu_types::gemm::GemmDTransform; | ||
|
|
||
| #[derive(Debug, Clone, Copy)] | ||
| pub struct MatmulShape { | ||
| pub m: u32, | ||
| pub n: u32, | ||
| pub k: u32, | ||
| pub b_transpose: bool, | ||
| pub b_leading_dimension: Option<u32>, | ||
| pub is_quant: bool, | ||
| pub b_bits: Option<u32>, | ||
| pub b_group_size: Option<u32>, | ||
| pub gathered: bool, | ||
| pub d_transform: GemmDTransform, | ||
| } | ||
|
|
||
| #[derive(Debug, Clone, Copy, PartialEq, Eq)] | ||
| pub enum MatmulPath { | ||
| Gemv, | ||
| Gemm, | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.