blib/lib/PDL/ML/Quantize.pm

filter: Statementstmt 96.1% 272 / 283 Branchbran 71.0% 98 / 138 Conditioncond 56.4% 22 / 39 MC/DCmcdc 26.0% 6 / 23 Subroutinesub 100% 28 / 28 Podpod 0.0% 0 / 16 totaltotal 80.8% 426 / 527 CC 92 SCAR 21.8
CC
92
Cov
85%
_f32_to_fp8_e4m3
19.0
quantize_q4_0
15.8
quantize_q8_0
11.3
CRAP 8.8
↑ Summary
1 package PDL::ML::Quantize;
2  
3 2 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use PDL::LiteF;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 our $VERSION = '0.012';
11 2 our @EXPORT_OK = qw(
12 to_bf16 from_bf16
13 to_fp16 from_fp16
14 quantize_int8 dequantize_int8
15 quantize_fp8_e4m3 dequantize_fp8_e4m3
16 quantize_nvfp4 dequantize_nvfp4
17 quantize_mxfp4 dequantize_mxfp4
18 quantize_q4_0 dequantize_q4_0
19 quantize_q8_0 dequantize_q8_0
20 );
21  
22 # --- BF16 (IEEE 754 bfloat16: upper 16 bits of float32) ---
23 1 ▶sub to_bf16 {
Subroutine to_bf16: called CC=2 SCAR=6.9
Pod undocumented
24 1 my ($p) = @_;
25 1 my $f = float($p)->sever;
26 1 my @vals = $f->flat->list;
27 1 my @u16;
28 1 for my $v (@vals) {
29 16 my $bits = unpack('L', pack('f', $v));
30 # Round-to-nearest-even
31 16 my $lsb = ($bits >> 16) & 1;
32 16 $bits += 0x7FFF + $lsb;
33 16 push @u16, ($bits >> 16) & 0xFFFF;
34 }
35 1 return ushort(\@u16)->reshape($f->dims);
36 }
37  
38 1 ▶sub from_bf16 {
Subroutine from_bf16: called CC=1 SCAR=0.0
Pod undocumented
39 1 my ($u) = @_;
40 1 my @u16 = ushort($u)->flat->list;
41 1 my @f32 = map { unpack('f', pack('L', ($_ & 0xFFFF) << 16)) } @u16;
42 1 return float(\@f32)->reshape($u->dims);
43 }
44  
45 # --- FP16 (IEEE 754 binary16) ---
46 18 ▶sub _f32_to_f16_bits {
Subroutine _f32_to_f16_bits: called CC=5 SCAR=16.6
47 18 my ($v) = @_;
48 18 my $b = unpack('L', pack('f', $v));
49 18 my $sign = ($b >> 16) & 0x8000;
50 18 my $exp = (($b >> 23) & 0xFF) - 127 + 15;
51 18 my $mant = $b & 0x007FFFFF;
52 18 ▶ return $sign | 0x7E00 if (($b >> 23) & 0xFF) == 0xFF && $mant != 0; # NaN
Branch: 1 branch
BranchTrueFalseTotal
if ($b >> 23 & 255) == 255 and $mant != 0 0 18 18
Condition33%1/3
($b >> 23 & 255) == 255 and $mant != 0
!ll&&!rl&&r
1800
MC/DC0%0/2
($b >> 23 & 255) == 255 and $mant != 0
($b >> 23 & 255) == 255 $mant != 0
Truth table33%1/3
A and B
A= ($b >> 23 & 255) == 255B= $mant != 0
ABresult
0X0
100
111
53 18 ▶ return $sign | 0x7C00 if $exp >= 31; # Inf / overflow
Branch: 1 branch
BranchTrueFalseTotal
if $exp >= 31 0 18 18
54 18 ▶ return $sign if $exp <= 0; # Zero / underflow
Branch: 1 branch
BranchTrueFalseTotal
if $exp <= 0 1 17 18
55 17 return $sign | ($exp << 10) | (($mant + 0x1000) >> 13);
56 }
57  
58 20 ▶sub _f16_bits_to_f32 {
Subroutine _f16_bits_to_f32: called CC=6 SCAR=20.2
59 20 my ($h) = @_;
60 20 my $sign = ($h & 0x8000) << 16;
61 20 my $exp = ($h >> 10) & 0x1F;
62 20 my $mant = $h & 0x03FF;
63 20 ▶ return unpack('f', pack('L', $sign)) if $exp == 0 && $mant == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $exp == 0 and $mant == 0 1 19 20
Condition66%2/3
$exp == 0 and $mant == 0
!ll&&!rl&&r
1901
MC/DC50%1/2
$exp == 0 and $mant == 0
$exp == 0 $mant == 0
Truth table66%2/3
A and B
A= $exp == 0B= $mant == 0
ABresult
0X0
100
111
64 19 ▶ if ($exp == 31) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exp == 31) 0 19 19
65 0 return unpack('f', pack('L', $sign | 0x7F800000 | ($mant << 13)));
66 }
67 19 ▶ if ($exp == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exp == 0) 0 19 19
68 # subnormal
69 0 my $val = ($mant / 1024.0) * (2.0 ** -14);
70 0 ▶ return ($h & 0x8000) ? -$val : $val;
Branch: 1 branch
BranchTrueFalseTotal
$h & 32768 ? : 0 0 0
71 }
72 19 my $f32 = $sign | (($exp - 15 + 127) << 23) | ($mant << 13);
73 19 return unpack('f', pack('L', $f32));
74 }
75  
76 1 ▶sub to_fp16 {
Subroutine to_fp16: called CC=1 SCAR=0.0
Pod undocumented
77 1 my ($p) = @_;
78 1 my $f = float($p)->sever;
79 1 my @h = map { _f32_to_f16_bits($_) } $f->flat->list;
80 1 return ushort(\@h)->reshape($f->dims);
81 }
82  
83 1 ▶sub from_fp16 {
Subroutine from_fp16: called CC=1 SCAR=0.0
Pod undocumented
84 1 my ($u) = @_;
85 1 my @f = map { _f16_bits_to_f32($_) } ushort($u)->flat->list;
86 1 return float(\@f)->reshape($u->dims);
87 }
88  
89 # --- Symmetric INT8 ---
90 1 ▶sub quantize_int8 {
Subroutine quantize_int8: called CC=2 SCAR=7.0
Pod undocumented
91 1 my ($p) = @_;
92 1 my $f = float($p);
93 1 my $amax = $f->abs->max->sclr;
94 1 ▶ my $scale = ($amax > 0) ? ($amax / 127.0) : 1.0;
Branch: 1 branch
BranchTrueFalseTotal
$amax > 0 ? : 1 0 1
95 1 my $q = sbyte(($f / $scale)->rint->clip(-127, 127));
96 return {
97 1 format => 'int8',
98 data => $q,
99 scale => float($scale),
100 shape => [ $f->dims ],
101 };
102 }
103  
104 1 ▶sub dequantize_int8 {
Subroutine dequantize_int8: called CC=1 SCAR=0.0
Pod undocumented
105 1 my ($qt) = @_;
106 1 return float($qt->{data}) * float($qt->{scale});
107 }
108  
109 # --- FP8 E4M3 (1 sign, 4 exp bias=7, 3 mant, max=448.0) ---
110 17 ▶sub _f32_to_fp8_e4m3 {
Subroutine _f32_to_fp8_e4m3: called CC=11 SCAR=29.5
111 17 my ($v) = @_;
112 17 ▶ my $sign = $v < 0 ? 0x80 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$v < 0 ? : 6 11 17
113 17 my $av = abs($v);
114 17 ▶ return $sign if $av == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $av == 0 1 16 17
115 16 ▶ $av = 448.0 if $av > 448.0;
Branch: 1 branch
BranchTrueFalseTotal
if $av > 448 0 16 16
116 16 my $exp = int(log($av) / log(2));
117 16 my $biased = $exp + 7;
118 16 ▶ if ($biased <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($biased <= 0) 0 16 16
119 0 my $m = int($av / (2.0 ** -9) + 0.5);
120 0 ▶ $m = 7 if $m > 7;
Branch: 1 branch
BranchTrueFalseTotal
if $m > 7 0 0 0
121 0 return $sign | $m;
122 }
123 16 ▶ $biased = 15 if $biased > 15;
Branch: 1 branch
BranchTrueFalseTotal
if $biased > 15 0 16 16
124 16 my $m = int((($av / (2.0 ** $exp)) - 1.0) * 8.0 + 0.5);
125 0 ▶ if ($m >= 8) { $m = 0; $biased++; }
Branch: 1 branch
BranchTrueFalseTotal
if ($m >= 8) 0 16 16
126 0 ▶ if ($biased > 15 || ($biased == 15 && $m > 6)) { $biased = 15; $m = 6; }
Branch: 1 branch
BranchTrueFalseTotal
if ($biased > 15 or $biased == 15 and $m > 6) 0 16 16
Condition50%3/6
$biased > 15 or $biased == 15 and $m > 6
l!l&&r!l&&!r
0016
$biased == 15 and $m > 6
!ll&&!rl&&r
1060
MC/DC0%0/3
$biased > 15 or $biased == 15 and $m > 6
$biased > 15 $biased == 15 $m > 6
Truth table50%2/4
A or B and C
A= $biased > 15B= $biased == 15C= $m > 6
ABCresult
1XX1
0111
00X0
0100
127 16 return $sign | ($biased << 3) | ($m & 0x7);
128 }
129  
130 18 ▶sub _fp8_e4m3_to_f32 {
Subroutine _fp8_e4m3_to_f32: called CC=5 SCAR=16.4
131 18 my ($b) = @_;
132 18 ▶ my $sign = ($b & 0x80) ? -1.0 : 1.0;
Branch: 1 branch
BranchTrueFalseTotal
$b & 128 ? : 6 12 18
133 18 my $exp = ($b >> 3) & 0x0F;
134 18 my $mant = $b & 0x07;
135 18 ▶ return 0.0 if $exp == 0 && $mant == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $exp == 0 and $mant == 0 1 17 18
Condition66%2/3
$exp == 0 and $mant == 0
!ll&&!rl&&r
1701
MC/DC50%1/2
$exp == 0 and $mant == 0
$exp == 0 $mant == 0
Truth table66%2/3
A and B
A= $exp == 0B= $mant == 0
ABresult
0X0
100
111
136 17 ▶ if ($exp == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exp == 0) 0 17 17
137 0 return $sign * ($mant / 8.0) * (2.0 ** -6);
138 }
139 17 return $sign * (1.0 + $mant / 8.0) * (2.0 ** ($exp - 7));
140 }
141  
142 1 ▶sub quantize_fp8_e4m3 {
Subroutine quantize_fp8_e4m3: called CC=2 SCAR=7.0
Pod undocumented
143 1 my ($p) = @_;
144 1 my $f = float($p);
145 1 my $amax = $f->abs->max->sclr;
146 1 ▶ my $scale = ($amax > 0) ? ($amax / 448.0) : 1.0;
Branch: 1 branch
BranchTrueFalseTotal
$amax > 0 ? : 1 0 1
147 1 my @q = map { _f32_to_fp8_e4m3($_ / $scale) } $f->flat->list;
148 return {
149 1 format => 'fp8_e4m3',
150 data => byte(\@q)->reshape($f->dims),
151 scale => float($scale),
152 shape => [ $f->dims ],
153 };
154 }
155  
156 1 ▶sub dequantize_fp8_e4m3 {
Subroutine dequantize_fp8_e4m3: called CC=1 SCAR=0.0
Pod undocumented
157 1 my ($qt) = @_;
158 1 my $scale = $qt->{scale}->sclr;
159 1 my @f = map { _fp8_e4m3_to_f32($_) * $scale } $qt->{data}->flat->list;
160 1 return float(\@f)->reshape(@{ $qt->{shape} });
161 }
162  
163 # --- FP4 E2M1 table (shared by NVFP4 and MXFP4) ---
164 2 my @FP4_E2M1 = (0.0, 0.5, 1.0, 1.5, 2.0, 3.0, 4.0, 6.0);
165  
166 32 ▶sub _f32_to_fp4_e2m1 {
Subroutine _f32_to_fp4_e2m1: called CC=4 SCAR=13.9
167 32 my ($v) = @_;
168 32 ▶ my $sign = $v < 0 ? 0x8 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$v < 0 ? : 12 20 32
169 32 my $av = abs($v);
170 32 my ($best_i, $best_d) = (0, 1e30);
171 32 for my $i (0..7) {
172 256 my $d = abs($av - $FP4_E2M1[$i]);
173 256 ▶ if ($d < $best_d) { $best_d = $d; $best_i = $i; }
Branch: 1 branch
BranchTrueFalseTotal
if ($d < $best_d) 138 118 256
174 }
175 32 return $sign | $best_i;
176 }
177  
178 32 ▶sub _fp4_e2m1_to_f32 {
Subroutine _fp4_e2m1_to_f32: called CC=2 SCAR=6.9
179 32 my ($n) = @_;
180 32 my $v = $FP4_E2M1[$n & 0x7];
181 32 ▶ return ($n & 0x8) ? -$v : $v;
Branch: 1 branch
BranchTrueFalseTotal
$n & 8 ? : 12 20 32
182 }
183  
184 # --- NVFP4 (Blackwell / Thor: E2M1 nibbles + FP8 E4M3 per-16-elem scales + FP32 tensor scale) ---
185 1 ▶sub quantize_nvfp4 {
Subroutine quantize_nvfp4: called CC=10 SCAR=23.2
Pod undocumented
186 1 my ($p) = @_;
187 1 my $f = float($p);
188 1 my @vals = $f->flat->list;
189 1 my $n = scalar @vals;
190 1 my $blk = 16;
191 1 my $global_amax = $f->abs->max->sclr;
192 1 ▶ my $tensor_scale = ($global_amax > 0) ? ($global_amax / (6.0 * 448.0)) : 1.0;
Branch: 1 branch
BranchTrueFalseTotal
$global_amax > 0 ? : 1 0 1
193  
194 1 my (@packed, @block_scales);
195 1 ▶ for (my $i = 0; $i < $n; $i += $blk) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 1 1 2
196 1 ▶ my $end = ($i + $blk - 1 < $n) ? ($i + $blk - 1) : ($n - 1);
Branch: 1 branch
BranchTrueFalseTotal
$i + $blk - 1 < $n ? : 1 0 1
197 1 my $bamax = 0;
198 1 for my $j ($i .. $end) {
199 16 my $av = abs($vals[$j]);
200 16 ▶ $bamax = $av if $av > $bamax;
Branch: 1 branch
BranchTrueFalseTotal
if $av > $bamax 3 13 16
201 }
202 1 ▶ my $bscale_f = ($bamax > 0) ? ($bamax / (6.0 * $tensor_scale)) : 1.0;
Branch: 1 branch
BranchTrueFalseTotal
$bamax > 0 ? : 1 0 1
203 1 my $bscale_fp8 = _f32_to_fp8_e4m3($bscale_f);
204 1 my $eff_scale = _fp8_e4m3_to_f32($bscale_fp8) * $tensor_scale;
205 1 ▶ $eff_scale = 1.0 if $eff_scale == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $eff_scale == 0 0 1 1
206 1 push @block_scales, $bscale_fp8;
207  
208 1 ▶ for (my $j = $i; $j <= $end; $j += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j <= $end) 8 1 9
209 8 my $lo = _f32_to_fp4_e2m1($vals[$j] / $eff_scale);
210 8 ▶ my $hi = ($j + 1 <= $end) ? _f32_to_fp4_e2m1($vals[$j + 1] / $eff_scale) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$j + 1 <= $end ? : 8 0 8
211 8 push @packed, ($lo & 0xF) | (($hi & 0xF) << 4);
212 }
213 }
214 return {
215 1 format => 'nvfp4',
216 block_size => $blk,
217 data => byte(\@packed),
218 block_scales => byte(\@block_scales),
219 tensor_scale => float($tensor_scale),
220 nelem => $n,
221 shape => [ $f->dims ],
222 };
223 }
224  
225 1 ▶sub dequantize_nvfp4 {
Subroutine dequantize_nvfp4: called CC=6 SCAR=18.0
Pod undocumented
226 1 my ($qt) = @_;
227 1 my @packed = $qt->{data}->list;
228 1 my @bscales = $qt->{block_scales}->list;
229 1 my $tscale = $qt->{tensor_scale}->sclr;
230 1 ▶ my $blk = $qt->{block_size} || 16;
Condition50%1/2
$qt->{'block_size'} || 16
l!l
10
MC/DC0%0/1
$qt->{'block_size'} || 16
$qt->{'block_size'}
Truth table50%1/2
A
A= $qt->{'block_size'}
Aresult
11
00
231 1 my $n = $qt->{nelem};
232 1 my @out;
233 1 my $pidx = 0;
234 1 ▶ for (my $b = 0; $b < @bscales; $b++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b < @bscales) 1 1 2
235 1 my $eff = _fp8_e4m3_to_f32($bscales[$b]) * $tscale;
236 1 ▶ for (my $k = 0; $k < $blk && @out < $n; $k += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k < $blk and @out < $n) 8 1 9
Condition66%2/3
$k < $blk and @out < $n
!ll&&!rl&&r
108
MC/DC50%1/2
$k < $blk and @out < $n
$k < $blk @out < $n
Truth table66%2/3
A and B
A= $k < $blkB= @out < $n
ABresult
0X0
100
111
237 8 my $byte = $packed[$pidx++];
238 8 push @out, _fp4_e2m1_to_f32($byte & 0xF) * $eff;
239 8 ▶ push @out, _fp4_e2m1_to_f32(($byte >> 4) & 0xF) * $eff if @out < $n;
Branch: 1 branch
BranchTrueFalseTotal
if @out < $n 8 0 8
240 }
241 }
242 1 return float(\@out)->reshape(@{ $qt->{shape} });
243 }
244  
245 # --- MXFP4 (OCP Microscaling: E2M1 + E8M0 per-32-elem power-of-two scale) ---
246 1 ▶sub quantize_mxfp4 {
Subroutine quantize_mxfp4: called CC=10 SCAR=23.2
Pod undocumented
247 1 my ($p) = @_;
248 1 my $f = float($p);
249 1 my @vals = $f->flat->list;
250 1 my $n = scalar @vals;
251 1 my $blk = 32;
252 1 my (@packed, @e8m0);
253 1 ▶ for (my $i = 0; $i < $n; $i += $blk) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 1 1 2
254 1 ▶ my $end = ($i + $blk - 1 < $n) ? ($i + $blk - 1) : ($n - 1);
Branch: 1 branch
BranchTrueFalseTotal
$i + $blk - 1 < $n ? : 0 1 1
255 1 my $bamax = 0;
256 1 for my $j ($i .. $end) {
257 16 my $av = abs($vals[$j]);
258 16 ▶ $bamax = $av if $av > $bamax;
Branch: 1 branch
BranchTrueFalseTotal
if $av > $bamax 3 13 16
259 }
260 1 ▶ my $exp = ($bamax > 0) ? int(log($bamax / 6.0) / log(2) + 0.999999) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$bamax > 0 ? : 1 0 1
261 1 my $biased = $exp + 127;
262 1 ▶ $biased = 0 if $biased < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $biased < 0 0 1 1
263 1 ▶ $biased = 254 if $biased > 254;
Branch: 1 branch
BranchTrueFalseTotal
if $biased > 254 0 1 1
264 1 push @e8m0, $biased;
265 1 my $eff = 2.0 ** ($biased - 127);
266 1 ▶ for (my $j = $i; $j <= $end; $j += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j <= $end) 8 1 9
267 8 my $lo = _f32_to_fp4_e2m1($vals[$j] / $eff);
268 8 ▶ my $hi = ($j + 1 <= $end) ? _f32_to_fp4_e2m1($vals[$j + 1] / $eff) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$j + 1 <= $end ? : 8 0 8
269 8 push @packed, ($lo & 0xF) | (($hi & 0xF) << 4);
270 }
271 }
272 return {
273 1 format => 'mxfp4',
274 block_size => $blk,
275 data => byte(\@packed),
276 scales => byte(\@e8m0),
277 nelem => $n,
278 shape => [ $f->dims ],
279 };
280 }
281  
282 1 ▶sub dequantize_mxfp4 {
Subroutine dequantize_mxfp4: called CC=6 SCAR=18.0
Pod undocumented
283 1 my ($qt) = @_;
284 1 my @packed = $qt->{data}->list;
285 1 my @e8m0 = $qt->{scales}->list;
286 1 ▶ my $blk = $qt->{block_size} || 32;
Condition50%1/2
$qt->{'block_size'} || 32
l!l
10
MC/DC0%0/1
$qt->{'block_size'} || 32
$qt->{'block_size'}
Truth table50%1/2
A
A= $qt->{'block_size'}
Aresult
11
00
287 1 my $n = $qt->{nelem};
288 1 my @out;
289 1 my $pidx = 0;
290 1 ▶ for (my $b = 0; $b < @e8m0; $b++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b < @e8m0) 1 1 2
291 1 my $eff = 2.0 ** ($e8m0[$b] - 127);
292 1 ▶ for (my $k = 0; $k < $blk && @out < $n; $k += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k < $blk and @out < $n) 8 1 9
Condition66%2/3
$k < $blk and @out < $n
!ll&&!rl&&r
018
MC/DC50%1/2
$k < $blk and @out < $n
$k < $blk @out < $n
Truth table66%2/3
A and B
A= $k < $blkB= @out < $n
ABresult
0X0
100
111
293 8 my $byte = $packed[$pidx++];
294 8 push @out, _fp4_e2m1_to_f32($byte & 0xF) * $eff;
295 8 ▶ push @out, _fp4_e2m1_to_f32(($byte >> 4) & 0xF) * $eff if @out < $n;
Branch: 1 branch
BranchTrueFalseTotal
if @out < $n 8 0 8
296 }
297 }
298 1 return float(\@out)->reshape(@{ $qt->{shape} });
299 }
300  
301 # --- GGML Q4_0 (32-elem block: FP16 scale + 16 packed 4-bit unsigned nibbles offset by 8) ---
302 1 ▶sub quantize_q4_0 {
Subroutine quantize_q4_0: called CC=15 SCAR=27.6
Pod undocumented
303 1 my ($p) = @_;
304 1 my $f = float($p);
305 1 my @vals = $f->flat->list;
306 1 my $n = scalar @vals;
307 1 my $blk = 32;
308 1 my (@packed, @scales);
309 1 ▶ for (my $i = 0; $i < $n; $i += $blk) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 1 1 2
310 1 ▶ my $end = ($i + $blk - 1 < $n) ? ($i + $blk - 1) : ($n - 1);
Branch: 1 branch
BranchTrueFalseTotal
$i + $blk - 1 < $n ? : 0 1 1
311 1 my $bamax = 0;
312 1 for my $j ($i .. $end) {
313 16 my $av = abs($vals[$j]);
314 16 ▶ $bamax = $av if $av > $bamax;
Branch: 1 branch
BranchTrueFalseTotal
if $av > $bamax 3 13 16
315 }
316 1 ▶ my $d = ($bamax > 0) ? ($bamax / 7.0) : 1.0;
Branch: 1 branch
BranchTrueFalseTotal
$bamax > 0 ? : 1 0 1
317 1 my $d16 = _f32_to_f16_bits($d);
318 1 push @scales, $d16;
319 1 ▶ my $eff = _f16_bits_to_f32($d16) || 1.0;
Condition50%1/2
_f16_bits_to_f32($d16) || 1.0
l!l
10
MC/DC0%0/1
_f16_bits_to_f32($d16) || 1.0
_f16_bits_to_f32($d16)
Truth table50%1/2
A
A= _f16_bits_to_f32($d16)
Aresult
11
00
320 1 ▶ for (my $j = $i; $j <= $end; $j += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j <= $end) 8 1 9
321 8 ▶ my $q0 = int($vals[$j] / $eff + ($vals[$j] >= 0 ? 0.5 : -0.5)) + 8;
Branch: 1 branch
BranchTrueFalseTotal
$vals[$j] >= 0 ? : 4 4 8
322 8 ▶ $q0 = 0 if $q0 < 0; $q0 = 15 if $q0 > 15;
Branch: 2 branches
BranchTrueFalseTotal
if $q0 < 0 0 8 8
if $q0 > 15 0 8 8
323 8 my $q1 = 8;
324 8 ▶ if ($j + 1 <= $end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j + 1 <= $end) 8 0 8
325 8 ▶ $q1 = int($vals[$j + 1] / $eff + ($vals[$j + 1] >= 0 ? 0.5 : -0.5)) + 8;
Branch: 1 branch
BranchTrueFalseTotal
$vals[$j + 1] >= 0 ? : 6 2 8
326 8 ▶ $q1 = 0 if $q1 < 0; $q1 = 15 if $q1 > 15;
Branch: 2 branches
BranchTrueFalseTotal
if $q1 < 0 0 8 8
if $q1 > 15 0 8 8
327 }
328 8 push @packed, ($q0 & 0xF) | (($q1 & 0xF) << 4);
329 }
330 }
331 return {
332 1 format => 'q4_0',
333 block_size => $blk,
334 data => byte(\@packed),
335 scales => ushort(\@scales),
336 nelem => $n,
337 shape => [ $f->dims ],
338 };
339 }
340  
341 1 ▶sub dequantize_q4_0 {
Subroutine dequantize_q4_0: called CC=6 SCAR=18.0
Pod undocumented
342 1 my ($qt) = @_;
343 1 my @packed = $qt->{data}->list;
344 1 my @scales = $qt->{scales}->list;
345 1 ▶ my $blk = $qt->{block_size} || 32;
Condition50%1/2
$qt->{'block_size'} || 32
l!l
10
MC/DC0%0/1
$qt->{'block_size'} || 32
$qt->{'block_size'}
Truth table50%1/2
A
A= $qt->{'block_size'}
Aresult
11
00
346 1 my $n = $qt->{nelem};
347 1 my @out;
348 1 my $pidx = 0;
349 1 ▶ for (my $b = 0; $b < @scales; $b++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b < @scales) 1 1 2
350 1 my $eff = _f16_bits_to_f32($scales[$b]);
351 1 ▶ for (my $k = 0; $k < $blk && @out < $n; $k += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k < $blk and @out < $n) 8 1 9
Condition66%2/3
$k < $blk and @out < $n
!ll&&!rl&&r
018
MC/DC50%1/2
$k < $blk and @out < $n
$k < $blk @out < $n
Truth table66%2/3
A and B
A= $k < $blkB= @out < $n
ABresult
0X0
100
111
352 8 my $byte = $packed[$pidx++];
353 8 push @out, (($byte & 0xF) - 8) * $eff;
354 8 ▶ push @out, ((($byte >> 4) & 0xF) - 8) * $eff if @out < $n;
Branch: 1 branch
BranchTrueFalseTotal
if @out < $n 8 0 8
355 }
356 }
357 1 return float(\@out)->reshape(@{ $qt->{shape} });
358 }
359  
360 # --- GGML Q8_0 (32-elem block: FP16 scale + 32 int8 values) ---
361 1 ▶sub quantize_q8_0 {
Subroutine quantize_q8_0: called CC=11 SCAR=24.2
Pod undocumented
362 1 my ($p) = @_;
363 1 my $f = float($p);
364 1 my @vals = $f->flat->list;
365 1 my $n = scalar @vals;
366 1 my $blk = 32;
367 1 my (@qvals, @scales);
368 1 ▶ for (my $i = 0; $i < $n; $i += $blk) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 1 1 2
369 1 ▶ my $end = ($i + $blk - 1 < $n) ? ($i + $blk - 1) : ($n - 1);
Branch: 1 branch
BranchTrueFalseTotal
$i + $blk - 1 < $n ? : 0 1 1
370 1 my $bamax = 0;
371 1 for my $j ($i .. $end) {
372 16 my $av = abs($vals[$j]);
373 16 ▶ $bamax = $av if $av > $bamax;
Branch: 1 branch
BranchTrueFalseTotal
if $av > $bamax 3 13 16
374 }
375 1 ▶ my $d = ($bamax > 0) ? ($bamax / 127.0) : 1.0;
Branch: 1 branch
BranchTrueFalseTotal
$bamax > 0 ? : 1 0 1
376 1 my $d16 = _f32_to_f16_bits($d);
377 1 push @scales, $d16;
378 1 ▶ my $eff = _f16_bits_to_f32($d16) || 1.0;
Condition50%1/2
_f16_bits_to_f32($d16) || 1.0
l!l
10
MC/DC0%0/1
_f16_bits_to_f32($d16) || 1.0
_f16_bits_to_f32($d16)
Truth table50%1/2
A
A= _f16_bits_to_f32($d16)
Aresult
11
00
379 1 for my $j ($i .. $end) {
380 16 ▶ my $q = int($vals[$j] / $eff + ($vals[$j] >= 0 ? 0.5 : -0.5));
Branch: 1 branch
BranchTrueFalseTotal
$vals[$j] >= 0 ? : 10 6 16
381 16 ▶ $q = -127 if $q < -127; $q = 127 if $q > 127;
Branch: 2 branches
BranchTrueFalseTotal
if $q < -127 0 16 16
if $q > 127 0 16 16
382 16 push @qvals, $q;
383 }
384 }
385 return {
386 1 format => 'q8_0',
387 block_size => $blk,
388 data => sbyte(\@qvals),
389 scales => ushort(\@scales),
390 nelem => $n,
391 shape => [ $f->dims ],
392 };
393 }
394  
395 1 ▶sub dequantize_q8_0 {
Subroutine dequantize_q8_0: called CC=5 SCAR=16.1
Pod undocumented
396 1 my ($qt) = @_;
397 1 my @qvals = $qt->{data}->list;
398 1 my @scales = $qt->{scales}->list;
399 1 ▶ my $blk = $qt->{block_size} || 32;
Condition50%1/2
$qt->{'block_size'} || 32
l!l
10
MC/DC0%0/1
$qt->{'block_size'} || 32
$qt->{'block_size'}
Truth table50%1/2
A
A= $qt->{'block_size'}
Aresult
11
00
400 1 my $n = $qt->{nelem};
401 1 my @out;
402 1 my $idx = 0;
403 1 ▶ for (my $b = 0; $b < @scales; $b++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b < @scales) 1 1 2
404 1 my $eff = _f16_bits_to_f32($scales[$b]);
405 1 ▶ for (my $k = 0; $k < $blk && $idx < $n; $k++, $idx++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k < $blk and $idx < $n) 16 1 17
Condition66%2/3
$k < $blk and $idx < $n
!ll&&!rl&&r
0116
MC/DC50%1/2
$k < $blk and $idx < $n
$k < $blk $idx < $n
Truth table66%2/3
A and B
A= $k < $blkB= $idx < $n
ABresult
0X0
100
111
406 16 push @out, $qvals[$idx] * $eff;
407 }
408 }
409 1 return float(\@out)->reshape(@{ $qt->{shape} });
410 }
411  
412 2 1;
413  
414 __END__
↑ Summary