| 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
MC/DC100%1/1
$args{'num_blocks'} || 64
$args{'num_blocks'}
Truth table100%2/2
A
A= $args{'num_blocks'}
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 3 | ▶ | my $block_size = $args{block_size} || 16; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args{'block_size'} || 16
MC/DC100%1/1
$args{'block_size'} || 16
$args{'block_size'}
Truth table100%2/2
A
A= $args{'block_size'}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC100%1/1
$args{'max_batch_size'} || 8
$args{'max_batch_size'}
Truth table100%2/2
A
A= $args{'max_batch_size'}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
defined $prefix_key and exists $self->{"prefix_cache"}{$prefix_key}
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}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition83%5/6
defined $prefix_key and not exists $self->{"prefix_cache"}{$prefix_key} and @table
defined $prefix_key and not exists $self->{"prefix_cache"}{$prefix_key}
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 5 | for my $blk (@$table) { | |||||||||||||||||||||||||||||||||||||||||
| 63 | 8 | $self->{ref_counts}{$blk}--; | |||||||||||||||||||||||||||||||||||||||||
| 64 | 8 | ▶ | if ($self->{ref_counts}{$blk} <= 0) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $req{"id"} and $req{"prompt_tokens"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 3 | ▶ | $req{max_new_tokens} //= 16; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$req{"max_new_tokens"} //= 16
MC/DC0%0/1
$req{"max_new_tokens"} //= 16
$req{"max_new_tokens"}
Truth table50%1/2
A
A= $req{"max_new_tokens"}
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 3 | ▶ | $req{generated} //= []; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$req{"generated"} //= []
MC/DC0%0/1
$req{"generated"} //= []
$req{"generated"}
Truth table50%1/2
A
A= $req{"generated"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
@{$self->{"running"};} < $self->{"max_batch_size"} and @{$self->{"waiting"};}
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"};}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | 6 | push @{ $req->{generated} }, $tok; | |||||||||||||||||||||||||||||||||||||||||
| 104 | 6 | ▶ | if (@{ $req->{generated} } >= $req->{max_new_tokens} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
@{$req->{"generated"};} >= $req->{"max_new_tokens"} or defined $req->{"eos_token_id"} and $tok == $req->{"eos_token_id"}
defined $req->{"eos_token_id"} and $tok == $req->{"eos_token_id"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | 3 | ▶ | my $top_k = $opts{top_k} || 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts{'top_k'} || 0
MC/DC100%1/1
$opts{'top_k'} || 0
$opts{'top_k'}
Truth table100%2/2
A
A= $opts{'top_k'}
| |||||||||||||||||||||||||||||||||||||||||||
| 123 | 3 | ▶ | my $top_p = defined $opts{top_p} ? $opts{top_p} : 1.0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | 3 | ▶ | my $rep = $opts{repetition_penalty} || 1.0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts{'repetition_penalty'} || 1.0
MC/DC100%1/1
$opts{'repetition_penalty'} || 1.0
$opts{'repetition_penalty'}
Truth table100%2/2
A
A= $opts{'repetition_penalty'}
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | 3 | my $l = double($logits)->flat->copy; | |||||||||||||||||||||||||||||||||||||||||
| 127 | 3 | ▶ | if ($rep != 1.0 && $opts{seen_tokens}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$rep != 1 and $opts{"seen_tokens"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | 1 | for my $tid (@{ $opts{seen_tokens} }) { | |||||||||||||||||||||||||||||||||||||||||
| 129 | 1 | ▶ | next if $tid < 0 || $tid >= $l->nelem; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$tid < 0 or $tid >= $l->nelem
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
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | 1 | my $v = $l->at($tid); | |||||||||||||||||||||||||||||||||||||||||
| 131 | 1 | ▶ | $l->set($tid, $v > 0 ? $v / $rep : $v * $rep); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | 3 | ▶ | return $l->maximum_ind->sclr if $temp <= 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
$top_k > 0 and $top_k < @idx
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||||||||||
| 162 | 0 | return $cand_ids[-1]; | |||||||||||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||
| 167 | __END__ | ||||||||||||||||||||||||||||||||||||||||||