Skip to content

Commit b49fcf1

Browse files
committed
style: standardrb autofix for the ml loader files
1 parent 9293c8e commit b49fcf1

2 files changed

Lines changed: 16 additions & 16 deletions

File tree

‎lib/interscript/ml.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module Interscript::ML
1919
# Expected to be loaded before IMF/Byt5Onnx/PlaneModel entry points.
2020
def self.require_optional!(gem_name, feature: gem_name)
2121
require feature
22-
rescue LoadError => e
22+
rescue LoadError
2323
raise Interscript::ExternalUtilError,
2424
"the neural layer needs the #{gem_name} gem: gem install #{gem_name} (or use a bundle including it)"
2525
end

‎lib/interscript/ml/byt5_onnx.rb‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require 'tmpdir'
1+
require "tmpdir"
22

33
module Interscript::ML
44
# Byte-level seq2seq (ByT5 family) inference over ONNX sessions from an
@@ -11,24 +11,24 @@ def initialize(zip_path)
1111
Interscript::ML.require_optional!("onnxruntime")
1212
@manifest = IMF.manifest(zip_path)
1313
graphs = IMF.verify_and_read(zip_path)
14-
@tmpdir = Dir.mktmpdir('secryst-imf')
15-
@encoder = build_session(graphs, 'encoder.onnx')
16-
@kv = @manifest['decoder'] == 'kv' && graphs.key?('decoder-kv.onnx')
17-
@decoder = build_session(graphs, @kv ? 'decoder-kv.onnx' : 'decoder.onnx')
14+
@tmpdir = Dir.mktmpdir("secryst-imf")
15+
@encoder = build_session(graphs, "encoder.onnx")
16+
@kv = @manifest["decoder"] == "kv" && graphs.key?("decoder-kv.onnx")
17+
@decoder = build_session(graphs, @kv ? "decoder-kv.onnx" : "decoder.onnx")
1818
@pasts = @kv ? zero_pasts : {}
1919
end
2020

2121
def translate(text, max_seq_length: 256)
2222
ids = IMF.encode(text)
23-
return '' if ids.length == 1 # only the trailing EOS: empty input
23+
return "" if ids.length == 1 # only the trailing EOS: empty input
2424

25-
hidden = @encoder.predict({ input_ids: [ids] })['last_hidden_state']
25+
hidden = @encoder.predict({input_ids: [ids]})["last_hidden_state"]
2626
tokens = @kv ? greedy_kv(hidden, max_seq_length) : greedy_plain(hidden, max_seq_length)
2727
IMF.first_alternates(IMF.decode(tokens))
2828
end
2929

3030
def id
31-
@manifest['id']
31+
@manifest["id"]
3232
end
3333

3434
private
@@ -43,7 +43,7 @@ def build_session(graphs, name)
4343

4444
def zero_pasts
4545
@decoder.inputs.each_with_object({}) do |meta, pasts|
46-
next unless meta[:name].start_with?('past_')
46+
next unless meta[:name].start_with?("past_")
4747
shape = meta[:shape] # [batch, heads, past_seq, d_kv]; dynamic dims are Strings
4848
heads = shape[1].is_a?(Integer) ? shape[1] : 4
4949
d_kv = shape[3].is_a?(Integer) ? shape[3] : 8
@@ -71,13 +71,13 @@ def greedy_kv(hidden, max_seq_length)
7171
generated = []
7272
max_seq_length.times do
7373
results = @decoder.predict(
74-
{ input_ids: [current], encoder_hidden_states: hidden }.merge(pasts)
74+
{input_ids: [current], encoder_hidden_states: hidden}.merge(pasts)
7575
)
76-
token = argmax(results['logits'].first.last)
76+
token = argmax(results["logits"].first.last)
7777
break if token == IMF::EOS_ID
7878

7979
generated << token
80-
pasts = pasts.keys.to_h { |name| [name, results[name.sub('past_', 'present_')]] }
80+
pasts = pasts.keys.to_h { |name| [name, results[name.sub("past_", "present_")]] }
8181
current = [token]
8282
end
8383
generated
@@ -88,13 +88,13 @@ def greedy_plain(hidden, max_seq_length)
8888
generated = []
8989
max_seq_length.times do
9090
logits = @decoder.predict(
91-
{ input_ids: [decoder_ids], encoder_hidden_states: hidden }
92-
)['logits']
91+
{input_ids: [decoder_ids], encoder_hidden_states: hidden}
92+
)["logits"]
9393
token = argmax(logits.first.last)
9494
break if token == IMF::EOS_ID
9595

9696
generated << token
97-
decoder_ids = decoder_ids + [token]
97+
decoder_ids += [token]
9898
end
9999
generated
100100
end

0 commit comments

Comments
 (0)