blib/lib/Langertha/VLLMHook/Config.pm

filter: Statementstmt 86.5% 45 / 52 Branchbran 69.2% 18 / 26 Conditioncond 61.5% 8 / 13 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 85.7% 6 / 7 Podpod 100% 3 / 3 totaltotal 76.1% 83 / 109 CC 20 SCAR 20.9
CC
20
Cov
78%
_build_data
12.0
xargs
10.1
worker
8.3
CRAP 8.1
« blib/lib/Langertha/ServerTool.pm ↑ Summary blib/lib/Langertha/Engine/Perplexity.pm »
1 package Langertha::VLLMHook::Config;
2 # ABSTRACT: Loader for vLLM-Hook model_configs/*.json files
3 2 our $VERSION = '0.503';
4 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use JSON::MaybeXS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 with 'Langertha::Role::JSON';
9  
10  
11 2 has file => (
12 is => 'ro',
13 isa => 'Str',
14 predicate => 'has_file',
15 );
16  
17  
18 2 has data => (
19 is => 'ro',
20 isa => 'HashRef',
21 lazy_build => 1,
22 );
23 0 ▶sub _build_data {
Subroutine _build_data: not called CC=3 SCAR=24.8
24 0 my ( $self ) = @_;
25 0 ▶ croak "Langertha::VLLMHook::Config requires 'file' or 'data'"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_file 0 0 0
26 unless $self->has_file;
27 0 ▶ open my $fh, '<:raw', $self->file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<:raw", $self->file or croak(q[Langertha::VLLMHook::Config cannot open '] . $self->file . "': $!") 0 0 0
28 or croak "Langertha::VLLMHook::Config cannot open '".$self->file."': $!";
29 0 local $/;
30 0 my $content = <$fh>;
31 0 close $fh;
32 0 return $self->json->decode($content);
33 }
34  
35  
36  
37 5 ▶sub worker {
Subroutine worker: called CC=8 SCAR=21.2
Pod documented
38 5 my ( $self ) = @_;
39 5 my $cfg = $self->data;
40 5 ▶ return 'steer' if ref $cfg->{steering} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $$cfg{"steering"} eq "HASH" 1 4 5
41 4 ▶ return 'hidden_states' if ref $cfg->{hidden_states} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $$cfg{"hidden_states"} eq "HASH" 2 2 4
42 return 'qk' if ref $cfg->{params} eq 'HASH'
43 2 ▶ && exists $cfg->{params}{important_heads};
Branch: 1 branch
BranchTrueFalseTotal
if ref $$cfg{"params"} eq "HASH" and exists $$cfg{"params"}{"important_heads"} 1 1 2
Condition66%2/3
ref $$cfg{"params"} eq "HASH" and exists $$cfg{"params"}{"important_heads"}
!ll&&!rl&&r
101
MC/DC50%1/2
ref $$cfg{"params"} eq "HASH" and exists $$cfg{"params"}{"important_heads"}
ref $$cfg{"params"} eq "HASH" exists $$cfg{"params"}{"important_heads"}
Truth table66%2/3
A and B
A= ref $$cfg{"params"} eq "HASH"B= exists $$cfg{"params"}{"important_heads"}
ABresult
0X0
100
111
44 1 ▶ my $provider = $cfg->{model_info} ? ( $cfg->{model_info}{provider} // '' ) : '';
Branch: 1 branch
BranchTrueFalseTotal
$$cfg{'model_info'} ? : 1 0 1
Condition50%1/2
$$cfg{"model_info"}{"provider"} // ""
l!l
01
MC/DC0%0/1
$$cfg{"model_info"}{"provider"} // ""
$$cfg{"model_info"}{"provider"}
Truth table50%1/2
A
A= $$cfg{"model_info"}{"provider"}
Aresult
11
00
45 1 ▶ return 'qk' if $provider =~ /attn/;
Branch: 1 branch
BranchTrueFalseTotal
if $provider =~ /attn/ 0 1 1
46 1 return undef;
47 }
48  
49  
50 2 ▶sub model_id {
Subroutine model_id: called CC=2 SCAR=7.1
Pod documented
51 2 my ( $self ) = @_;
52 2 ▶ my $info = $self->data->{model_info} or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $info = $self->data->{"model_info"} or return undef 2 0 2
53 2 return $info->{model_id};
54 }
55  
56  
57 5 ▶sub xargs {
Subroutine xargs: called CC=10 SCAR=23.2
Pod documented
58 5 my ( $self ) = @_;
59 5 my $cfg = $self->data;
60  
61 5 ▶ if ( ref $cfg->{steering} eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $$cfg{"steering"} eq "HASH") 1 4 5
62 1 return { steer => $self->encode_json_text( $cfg->{steering} ) };
63 }
64  
65 4 ▶ if ( ref $cfg->{hidden_states} eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $$cfg{"hidden_states"} eq "HASH") 2 2 4
66 2 my $layers = $cfg->{hidden_states}{layers};
67 2 ▶ if ( ref $layers eq 'ARRAY' && @$layers ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $layers eq "ARRAY" and @$layers) 1 1 2
Condition66%2/3
ref $layers eq "ARRAY" and @$layers
!ll&&!rl&&r
011
MC/DC50%1/2
ref $layers eq "ARRAY" and @$layers
ref $layers eq "ARRAY" @$layers
Truth table66%2/3
A and B
A= ref $layers eq "ARRAY"B= @$layers
ABresult
0X0
100
111
68 1 return { output_hidden_states => $self->encode_json_text($layers) };
69 }
70 1 return { output_hidden_states => JSON::MaybeXS::true() };
71 }
72  
73 2 ▶ if ( ref $cfg->{params} eq 'HASH' && $cfg->{params}{important_heads} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $$cfg{"params"} eq "HASH" and $$cfg{"params"}{"important_heads"}) 1 1 2
Condition66%2/3
ref $$cfg{"params"} eq "HASH" and $$cfg{"params"}{"important_heads"}
!ll&&!rl&&r
101
MC/DC50%1/2
ref $$cfg{"params"} eq "HASH" and $$cfg{"params"}{"important_heads"}
ref $$cfg{"params"} eq "HASH" $$cfg{"params"}{"important_heads"}
Truth table66%2/3
A and B
A= ref $$cfg{"params"} eq "HASH"B= $$cfg{"params"}{"important_heads"}
ABresult
0X0
100
111
74 1 my %layer_to_heads;
75 1 for my $pair ( @{ $cfg->{params}{important_heads} } ) {
76 4 my ( $layer, $head ) = @$pair;
77 4 push @{ $layer_to_heads{$layer} }, $head;
78 }
79 1 ▶ my $mode = $cfg->{hookq} ? ( $cfg->{hookq}{hookq_mode} // 'last_token' ) : 'last_token';
Branch: 1 branch
BranchTrueFalseTotal
$$cfg{'hookq'} ? : 1 0 1
Condition50%1/2
$$cfg{"hookq"}{"hookq_mode"} // "last_token"
l!l
10
MC/DC0%0/1
$$cfg{"hookq"}{"hookq_mode"} // "last_token"
$$cfg{"hookq"}{"hookq_mode"}
Truth table50%1/2
A
A= $$cfg{"hookq"}{"hookq_mode"}
Aresult
11
00
80 return {
81 1 output_qk => $self->encode_json_text( \%layer_to_heads ),
82 hookq_mode => $mode,
83 };
84 }
85  
86 1 return {};
87 }
88  
89 2 __PACKAGE__->meta->make_immutable;
90  
91  
92 2 1;
93  
94 __END__
95  
96 =pod
97  
98 =encoding UTF-8
99  
100 =head1 NAME
101  
102 Langertha::VLLMHook::Config - Loader for vLLM-Hook model_configs/*.json files
103  
104 =head1 VERSION
105  
106 version 0.503
107  
108 =head1 SYNOPSIS
109  
110 use Langertha::VLLMHook::Config;
111 use Langertha::Engine::VLLMHook;
112  
113 my $cfg = Langertha::VLLMHook::Config->new(
114 file => 'model_configs/hidden_states/Qwen2.5-3B-Instruct.json',
115 );
116  
117 my $engine = Langertha::Engine::VLLMHook->new(
118 url => 'http://localhost:8770/v1',
119 vllm_xargs => $cfg->xargs,
120 );
121  
122 =head1 DESCRIPTION
123  
124 Parses the JSON model configuration files shipped with vLLM-Hook
125 (L<https://github.com/IBM/vLLM-Hook>) and turns them into the C<vllm_xargs>
126 HashRef that L<Langertha::Engine::VLLMHook> sends on the wire.
127  
128 Three config shapes are recognised, mirroring vLLM-Hook's own
129 C<HookClient._load_config> / C<_build_extra_body>:
130  
131 =over 4
132  
133 =item * B<Attention tracker / CoRer> — has C<params.important_heads> (and
134 optionally C<hookq.hookq_mode>). Maps to worker C<qk>, emitting C<output_qk>.
135  
136 =item * B<Hidden states> — has a top-level C<hidden_states> block. Maps to
137 worker C<hidden_states>, emitting C<output_hidden_states>.
138  
139 =item * B<Activation steering> — has a top-level C<steering> block. Maps to
140 worker C<steer>, emitting C<steer>.
141  
142 =back
143  
144 Nested values in the produced C<xargs> are already JSON-encoded as strings,
145 because vLLM's C<vllm_xargs> only accepts scalar values — so the result can be
146 handed straight to L<Langertha::Engine::VLLMHook/vllm_xargs>.
147  
148 =head2 file
149  
150 Path to a vLLM-Hook C<model_configs/*.json> file. Either C<file> or C<data>
151 must be supplied. When C<file> is set, C<data> is loaded from it lazily.
152  
153 =head2 data
154  
155 The parsed config as a HashRef. Built from C<file> when not supplied directly.
156 Pass C<data> explicitly to construct a config from an in-memory structure
157 (useful for tests).
158  
159 =head2 worker
160  
161 my $worker = $cfg->worker; # 'qk' | 'hidden_states' | 'steer' | undef
162  
163 Returns the vLLM-Hook worker name implied by the config (the value you would
164 set as C<VLLM_HOOK_WORKER> when starting C<vllm serve>). Determined from the
165 structural keys present, falling back to C<model_info.provider>. Returns
166 C<undef> when the shape is not recognised.
167  
168 =head2 model_id
169  
170 my $id = $cfg->model_id;
171  
172 Returns C<model_info.model_id> from the config, or C<undef> when absent.
173  
174 =head2 xargs
175  
176 my $xargs = $cfg->xargs;
177  
178 Returns the C<vllm_xargs> HashRef ready for L<Langertha::Engine::VLLMHook>.
179 Nested values (the layer/heads map, the layers list, the full steering dict)
180 are returned as JSON strings, since C<vllm_xargs> only carries scalars. Returns
181 an empty HashRef for an unrecognised config.
182  
183 =head1 SEE ALSO
184  
185 =over
186  
187 =item * L<Langertha::Engine::VLLMHook> - Engine that consumes this config
188  
189 =item * L<https://github.com/IBM/vLLM-Hook> - vLLM-Hook plugin
190  
191 =item * L<Langertha::Role::JSON> - Provides the JSON encoder used here
192  
193 =back
194  
195 =head1 SUPPORT
196  
197 =head2 Issues
198  
199 Please report bugs and feature requests on GitHub at
200 L<https://github.com/Getty/langertha/issues>.
201  
202 =head2 IRC
203  
204 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
205  
206 =head1 CONTRIBUTING
207  
208 Contributions are welcome! Please fork the repository and submit a pull request.
209  
210 =head1 AUTHOR
211  
212 Torsten Raudssus <getty@cpan.org>
213  
214 =head1 COPYRIGHT AND LICENSE
215  
216 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
217  
218 This is free software; you can redistribute it and/or modify it under
219 the same terms as the Perl 5 programming language system itself.
220  
221 =cut
« blib/lib/Langertha/ServerTool.pm ↑ Summary blib/lib/Langertha/Engine/Perplexity.pm »