-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquantizer.cpp
More file actions
53 lines (42 loc) · 1.57 KB
/
Copy pathquantizer.cpp
File metadata and controls
53 lines (42 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "quantizer.h"
#include <mlx/ops.h>
namespace mlx_transformer {
mlx::core::array Quantizer::dequantize_int4(
const mlx::core::array& quantized_weights,
const mlx::core::array& scales,
const mlx::core::array& zero_points) {
// Implementation of 4-bit dequantization
// This is a simplified version for phase 1
auto dequantized = mlx::core::multiply(quantized_weights, scales);
if (zero_points.size() > 0) {
dequantized = mlx::core::subtract(dequantized, zero_points);
}
return dequantized;
}
std::tuple<mlx::core::array, mlx::core::array, mlx::core::array> Quantizer::quantize_int4(
const mlx::core::array& weights,
bool use_zero_point,
bool per_channel) {
// Actual implementation would compute optimal scales and zero points
// This is a placeholder for Phase 1
auto shape = weights.shape();
auto dtype = weights.dtype();
// Create dummy scales (1.0) and zero_points (0.0) for now
mlx::core::array scales;
mlx::core::array zero_points;
mlx::core::array quantized_weights = weights;
if (per_channel) {
auto num_channels = shape[shape.size() - 1];
scales = mlx::core::ones({num_channels}, dtype);
if (use_zero_point) {
zero_points = mlx::core::zeros({num_channels}, dtype);
}
} else {
scales = mlx::core::ones({1}, dtype);
if (use_zero_point) {
zero_points = mlx::core::zeros({1}, dtype);
}
}
return {quantized_weights, scales, zero_points};
}
} // namespace mlx_transformer