Skip to content

Commit

Permalink
Another transmute tweak. (huggingface#610)
Browse files Browse the repository at this point in the history
* Another transmute tweak.

* Changelog tweak.
  • Loading branch information
LaurentMazare authored Aug 26, 2023
1 parent 06b37ea commit fdf15f0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ This documents the main changes to the `candle` crate.

## Unreleased
### Added
- Support more quantized types, e.g. Q2K, Q4K, Q5K...
[586](https://github.com/huggingface/candle/pull/586).
- Add pose estimation to the yolo example
[589](https://github.com/huggingface/candle/pull/589).
- Api to write GGUF files
Expand Down
39 changes: 19 additions & 20 deletions candle-core/src/quantized/k_quants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,6 @@ impl GgmlType for BlockQ3K {
let mut aux32: [i32; 8] = [0; 8];

let mut auxs: [u32; 4] = [0; 4];
let mut scales: &[i8; 16];

for (x, y) in xs.iter().zip(ys.iter()) {
let mut q3: &[u8] = &x.qs;
Expand Down Expand Up @@ -728,28 +727,28 @@ impl GgmlType for BlockQ3K {
auxs[0] = (auxs[0] & KMASK2) | (((tmp) & KMASK1) << 4);
auxs[1] = (auxs[1] & KMASK2) | (((tmp >> 2) & KMASK1) << 4);

scales = unsafe { std::mem::transmute::<&mut [u32; 4], &mut [i8; 16]>(&mut auxs) };

for scale in scales {
for l in 0..8 {
aux16[l] = q8[l] as i16 * a[l] as i16;
}
for l in 0..8 {
aux32[l] += (*scale as i32 - 32) * aux16[l] as i32;
}
q8 = &q8[8..];
a = &mut a[8..];
for aux in auxs {
for scale in aux.to_le_bytes() {
let scale = i8::from_be_bytes([scale]);
for l in 0..8 {
aux16[l] = q8[l] as i16 * a[l] as i16;
}
for l in 0..8 {
aux32[l] += (scale as i32 - 32) * aux16[l] as i32;
}
q8 = &q8[8..];
a = &mut a[8..];

for l in 0..8 {
aux16[l] = q8[l] as i16 * a[l] as i16;
}
for l in 0..8 {
aux32[l] += (*scale as i32 - 32) * aux16[l] as i32;
for l in 0..8 {
aux16[l] = q8[l] as i16 * a[l] as i16;
}
for l in 0..8 {
aux32[l] += (scale as i32 - 32) * aux16[l] as i32;
}
q8 = &q8[8..];
a = &mut a[8..];
}
q8 = &q8[8..];
a = &mut a[8..];
}

let d = x.d.to_f32() * y.d;
for l in 0..8 {
sums[l] += d * aux32[l] as f32;
Expand Down

0 comments on commit fdf15f0

Please sign in to comment.