blib/lib/PDL/ML/Serve.pm

filter: Statementstmt 99.1% 123 / 124 Branchbran 71.4% 30 / 42 Conditioncond 68.1% 30 / 44 MC/DCmcdc 48.0% 12 / 25 Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 7 totaltotal 81.5% 207 / 254 CC 46 SCAR 23.7
CC
46
Cov
87%
sample_logits
20.0
step
9.2
allocate_blocks
9.0
CRAP 10.7
↑ Summary
1 package PDL::ML::Serve;
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 PDL::LiteF;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 our $VERSION = '0.012';
10  
11 3 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod undocumented
12 3 my ($class, %args) = @_;
13 3 ▶ my $num_blocks = $args{num_blocks} || 64;
Condition100%2/2
$args{'num_blocks'} || 64
l!l
21
MC/DC100%1/1
$args{'num_blocks'} || 64
$args{'num_blocks'}
Truth table100%2/2
A
A= $args{'num_blocks'}
Aresult
11
00
14 3 ▶ my $block_size = $args{block_size} || 16;
Condition100%2/2
$args{'block_size'} || 16
l!l
21
MC/DC100%1/1
$args{'block_size'} || 16
$args{'block_size'}
Truth table100%2/2
A
A= $args{'block_size'}
Aresult
11
00
15 return bless {
16 block_size => $block_size,
17 3 ▶ max_batch_size => $args{max_batch_size} || 8,
Condition100%2/2
$args{'max_batch_size'} || 8
l!l
12
MC/DC100%1/1
$args{'max_batch_size'} || 8
$args{'max_batch_size'}
Truth table100%2/2
A
A= $args{'max_batch_size'}
Aresult
11
00
18 free_blocks => [ 0 .. ($num_blocks - 1) ],
19 block_tables => {}, # req_id => [ phys_block_ids... ]
20 ref_counts => {}, # phys_block_id => count
21 prefix_cache => {}, # prefix_key => phys_block_id
22 waiting => [], # queued requests
23 running => [], # active requests
24 finished => [], # completed requests
25 }, $class;
26 }
27  
28 # --- PagedAttention KV-Cache Block Manager ---
29 11 ▶sub free_block_count { scalar @{ $_[0]->{free_blocks} } }
Subroutine free_block_count: called CC=1 SCAR=0.0
Pod undocumented
30  
31 5 ▶sub allocate_blocks {
Subroutine allocate_blocks: called CC=9 SCAR=22.0
Pod undocumented
32 5 my ($self, $req_id, $num_tokens, $prefix_key) = @_;
33 5 my $needed = int(($num_tokens + $self->{block_size} - 1) / $self->{block_size});
34 5 ▶ $needed = 1 if $needed < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $needed < 1 0 5 5
35  
36 5 my @table;
37 # Optional prefix-cache hit for block 0
38 5 ▶ if (defined $prefix_key && exists $self->{prefix_cache}{$prefix_key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prefix_key and exists $self->{"prefix_cache"}{$prefix_key}) 1 4 5
Condition100%3/3
defined $prefix_key and exists $self->{"prefix_cache"}{$prefix_key}
!ll&&!rl&&r
311
MC/DC100%2/2
defined $prefix_key and exists $self->{"prefix_cache"}{$prefix_key}
defined $prefix_key exists $self->{"prefix_cache"}{$prefix_key}
Truth table100%3/3
A and B
A= defined $prefix_keyB= exists $self->{"prefix_cache"}{$prefix_key}
ABresult
0X0
100
111
39 1 my $blk = $self->{prefix_cache}{$prefix_key};
40 1 $self->{ref_counts}{$blk}++;
41 1 push @table, $blk;
42 1 $needed--;
43 }
44  
45 5 ▶ return undef if $needed > $self->free_block_count;
Branch: 1 branch
BranchTrueFalseTotal
if $needed > $self->free_block_count 0 5 5
46  
47 5 for (1 .. $needed) {
48 7 my $blk = shift @{ $self->{free_blocks} };
49 7 $self->{ref_counts}{$blk} = 1;
50 7 push @table, $blk;
51 }
52 5 ▶ if (defined $prefix_key && !exists $self->{prefix_cache}{$prefix_key} && @table) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prefix_key and not exists $self->{"prefix_cache"}{$prefix_key} and @table) 1 4 5
Condition83%5/6
defined $prefix_key and not exists $self->{"prefix_cache"}{$prefix_key} and @table
!ll&&!rl&&r
401
defined $prefix_key and not exists $self->{"prefix_cache"}{$prefix_key}
!ll&&!rl&&r
311
MC/DC66%2/3
defined $prefix_key and not exists $self->{"prefix_cache"}{$prefix_key} and @table
defined $prefix_key not exists $self->{"prefix_cache"}{$prefix_key} @table
Truth table75%3/4
A and not(B) and C
A= defined $prefix_keyB= not exists $self->{"prefix_cache"}{$prefix_key}C= @table
ABCresult
0XX0
10X0
1100
1111
53 1 $self->{prefix_cache}{$prefix_key} = $table[0];
54 }
55 5 $self->{block_tables}{$req_id} = \@table;
56 5 return \@table;
57 }
58  
59 5 ▶sub free_request {
Subroutine free_request: called CC=6 SCAR=17.9
Pod undocumented
60 5 my ($self, $req_id) = @_;
61 5 ▶ my $table = delete $self->{block_tables}{$req_id} or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $table = delete $self->{"block_tables"}{$req_id} or return 0 5 0 5
62 5 for my $blk (@$table) {
63 8 $self->{ref_counts}{$blk}--;
64 8 ▶ if ($self->{ref_counts}{$blk} <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"ref_counts"}{$blk} <= 0) 7 1 8
65 7 delete $self->{ref_counts}{$blk};
66 # Evict from prefix cache if present
67 7 for my $k (keys %{ $self->{prefix_cache} }) {
68 2 ▶ delete $self->{prefix_cache}{$k} if $self->{prefix_cache}{$k} == $blk;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"prefix_cache"}{$k} == $blk 1 1 2
69 }
70 7 push @{ $self->{free_blocks} }, $blk;
71 }
72 }
73 5 return 1;
74 }
75  
76 # --- Continuous Batching Scheduler ---
77 3 ▶sub enqueue {
Subroutine enqueue: called CC=5 SCAR=17.3
Pod undocumented
78 3 my ($self, %req) = @_;
79 3 ▶ croak 'req id and prompt_tokens required' unless defined $req{id} && $req{prompt_tokens};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $req{"id"} and $req{"prompt_tokens"} 0 3 3
Condition33%1/3
defined $req{"id"} and $req{"prompt_tokens"}
!ll&&!rl&&r
003
MC/DC0%0/2
defined $req{"id"} and $req{"prompt_tokens"}
defined $req{"id"} $req{"prompt_tokens"}
Truth table33%1/3
A and B
A= defined $req{"id"}B= $req{"prompt_tokens"}
ABresult
0X0
100
111
80 3 ▶ $req{max_new_tokens} //= 16;
Condition50%1/2
$req{"max_new_tokens"} //= 16
l!l
30
MC/DC0%0/1
$req{"max_new_tokens"} //= 16
$req{"max_new_tokens"}
Truth table50%1/2
A
A= $req{"max_new_tokens"}
Aresult
11
00
81 3 ▶ $req{generated} //= [];
Condition50%1/2
$req{"generated"} //= []
l!l
03
MC/DC0%0/1
$req{"generated"} //= []
$req{"generated"}
Truth table50%1/2
A
A= $req{"generated"}
Aresult
11
00
82 3 $req{state} = 'waiting';
83 3 push @{ $self->{waiting} }, \%req;
84 3 return \%req;
85 }
86  
87 3 ▶sub step {
Subroutine step: called CC=9 SCAR=22.2
Pod undocumented
88 3 my ($self, $step_fn) = @_;
89 # Admit waiting requests if batch slots and KV blocks are available
90 3 ▶ while (@{ $self->{running} } < $self->{max_batch_size} && @{ $self->{waiting} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"running"};} < $self->{"max_batch_size"} and @{$self->{"waiting"};}) 3 3 6
Condition66%2/3
@{$self->{"running"};} < $self->{"max_batch_size"} and @{$self->{"waiting"};}
!ll&&!rl&&r
303
MC/DC50%1/2
@{$self->{"running"};} < $self->{"max_batch_size"} and @{$self->{"waiting"};}
@{$self->{"running"};} < $self->{"max_batch_size"} @{$self->{"waiting"};}
Truth table66%2/3
A and B
A= @{$self->{"running"};} < $self->{"max_batch_size"}B= @{$self->{"waiting"};}
ABresult
0X0
100
111
91 3 my $req = $self->{waiting}[0];
92 3 my $n_tok = scalar(@{ $req->{prompt_tokens} }) + $req->{max_new_tokens};
93 3 my $tbl = $self->allocate_blocks($req->{id}, $n_tok, $req->{prefix_key});
94 3 ▶ last unless defined $tbl;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tbl 0 3 3
95 3 shift @{ $self->{waiting} };
96 3 $req->{state} = 'running';
97 3 push @{ $self->{running} }, $req;
98 }
99  
100 3 my @still_running;
101 3 for my $req (@{ $self->{running} }) {
102 6 ▶ my $tok = $step_fn ? $step_fn->($req, $self->{block_tables}{ $req->{id} }) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$step_fn ? : 6 0 6
103 6 push @{ $req->{generated} }, $tok;
104 6 ▶ if (@{ $req->{generated} } >= $req->{max_new_tokens}
Branch: 1 branch
BranchTrueFalseTotal
if (@{$req->{'generated'};} >= $req->{'max_new_tokens'} or defined $req->{'eos_token_id'} and $tok == $req->{'eos_token_id'}) { } 3 3 6
Condition50%3/6
@{$req->{"generated"};} >= $req->{"max_new_tokens"} or defined $req->{"eos_token_id"} and $tok == $req->{"eos_token_id"}
l!l&&r!l&&!r
303
defined $req->{"eos_token_id"} and $tok == $req->{"eos_token_id"}
!ll&&!rl&&r
300
MC/DC33%1/3
@{$req->{"generated"};} >= $req->{"max_new_tokens"} or defined $req->{"eos_token_id"} and $tok == $req->{"eos_token_id"}
@{$req->{"generated"};} >= $req->{"max_new_tokens"} defined $req->{"eos_token_id"} $tok == $req->{"eos_token_id"}
Truth table50%2/4
A or B and C
A= @{$req->{"generated"};} >= $req->{"max_new_tokens"}B= defined $req->{"eos_token_id"}C= $tok == $req->{"eos_token_id"}
ABCresult
1XX1
0111
00X0
0100
105 || (defined $req->{eos_token_id} && $tok == $req->{eos_token_id}))
106 {
107 3 $req->{state} = 'finished';
108 3 $self->free_request($req->{id});
109 3 push @{ $self->{finished} }, $req;
110 } else {
111 3 push @still_running, $req;
112 }
113 }
114 3 $self->{running} = \@still_running;
115 3 return scalar(@still_running);
116 }
117  
118 # --- PDL Logits Sampler (greedy, temperature, top-k, top-p, repetition penalty) ---
119 3 ▶sub sample_logits {
Subroutine sample_logits: called CC=18 SCAR=30.0
Pod undocumented
120 3 my ($class_or_self, $logits, %opts) = @_;
121 3 ▶ my $temp = defined $opts{temperature} ? $opts{temperature} : 1.0;
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'temperature'} ? : 3 0 3
122 3 ▶ my $top_k = $opts{top_k} || 0;
Condition100%2/2
$opts{'top_k'} || 0
l!l
12
MC/DC100%1/1
$opts{'top_k'} || 0
$opts{'top_k'}
Truth table100%2/2
A
A= $opts{'top_k'}
Aresult
11
00
123 3 ▶ my $top_p = defined $opts{top_p} ? $opts{top_p} : 1.0;
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'top_p'} ? : 1 2 3
124 3 ▶ my $rep = $opts{repetition_penalty} || 1.0;
Condition100%2/2
$opts{'repetition_penalty'} || 1.0
l!l
12
MC/DC100%1/1
$opts{'repetition_penalty'} || 1.0
$opts{'repetition_penalty'}
Truth table100%2/2
A
A= $opts{'repetition_penalty'}
Aresult
11
00
125  
126 3 my $l = double($logits)->flat->copy;
127 3 ▶ if ($rep != 1.0 && $opts{seen_tokens}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rep != 1 and $opts{"seen_tokens"}) 1 2 3
Condition66%2/3
$rep != 1 and $opts{"seen_tokens"}
!ll&&!rl&&r
201
MC/DC50%1/2
$rep != 1 and $opts{"seen_tokens"}
$rep != 1 $opts{"seen_tokens"}
Truth table66%2/3
A and B
A= $rep != 1B= $opts{"seen_tokens"}
ABresult
0X0
100
111
128 1 for my $tid (@{ $opts{seen_tokens} }) {
129 1 ▶ next if $tid < 0 || $tid >= $l->nelem;
Branch: 1 branch
BranchTrueFalseTotal
if $tid < 0 or $tid >= $l->nelem 0 1 1
Condition33%1/3
$tid < 0 or $tid >= $l->nelem
l!l&&r!l&&!r
001
MC/DC0%0/2
$tid < 0 or $tid >= $l->nelem
$tid < 0 $tid >= $l->nelem
Truth table33%1/3
A or B
A= $tid < 0B= $tid >= $l->nelem
ABresult
1X1
011
000
130 1 my $v = $l->at($tid);
131 1 ▶ $l->set($tid, $v > 0 ? $v / $rep : $v * $rep);
Branch: 1 branch
BranchTrueFalseTotal
$v > 0 ? : 1 0 1
132 }
133 }
134  
135 3 ▶ return $l->maximum_ind->sclr if $temp <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $temp <= 0 2 1 3
136  
137 1 $l /= $temp;
138 1 my $shifted = $l - $l->max;
139 1 my $probs = exp($shifted);
140 1 $probs /= $probs->sum;
141  
142 1 my @idx = $probs->qsorti->list;
143 1 @idx = reverse @idx; # descending probability
144 1 ▶ @idx = @idx[ 0 .. ($top_k - 1) ] if $top_k > 0 && $top_k < @idx;
Branch: 1 branch
BranchTrueFalseTotal
if $top_k > 0 and $top_k < @idx 1 0 1
Condition33%1/3
$top_k > 0 and $top_k < @idx
!ll&&!rl&&r
001
MC/DC0%0/2
$top_k > 0 and $top_k < @idx
$top_k > 0 $top_k < @idx
Truth table33%1/3
A and B
A= $top_k > 0B= $top_k < @idx
ABresult
0X0
100
111
145  
146 1 my (@cand_ids, @cand_p);
147 1 my $cum = 0.0;
148 1 for my $i (@idx) {
149 1 my $p = $probs->at($i);
150 1 push @cand_ids, $i;
151 1 push @cand_p, $p;
152 1 $cum += $p;
153 1 ▶ last if $cum >= $top_p;
Branch: 1 branch
BranchTrueFalseTotal
if $cum >= $top_p 1 0 1
154 }
155  
156 1 my $r = rand($cum);
157 1 my $acc = 0.0;
158 1 for my $j (0 .. $#cand_ids) {
159 1 $acc += $cand_p[$j];
160 1 ▶ return $cand_ids[$j] if $r <= $acc;
Branch: 1 branch
BranchTrueFalseTotal
if $r <= $acc 1 0 1
161 }
162 0 return $cand_ids[-1];
163 }
164  
165 2 1;
166  
167 __END__
↑ Summary