Skip to content

Commit 42c1898

Browse files
authored
fix: seq2seq decode returns primary choice, strips slash-separated alternates (#805)
1 parent b32c99a commit 42c1898

3 files changed

Lines changed: 27 additions & 1 deletion

File tree

‎lib/interscript/ml/byt5_onnx.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def translate(text, max_seq_length: 256)
2424

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

3030
def id

‎lib/interscript/ml/imf.rb‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ def encode(text)
2828
text.bytes.map { |b| b + BYTE_OFFSET } + [EOS_ID]
2929
end
3030

31+
# Seq2seq output contract: models trained on multi-reference
32+
# corpora emit '/'-separated alternates per token; the runtime
33+
# returns the primary choice only.
34+
def first_alternates(text)
35+
text.split.map { |tok| tok.split('/', 2).first }.join(' ')
36+
end
37+
3138
def decode(token_ids)
3239
out = +""
3340
token_ids.each do |token|

‎spec/first_alternates_spec.rb‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require "spec_helper"
2+
require "interscript/ml/imf"
3+
4+
RSpec.describe Interscript::ML::IMF do
5+
describe ".first_alternates" do
6+
it "keeps the primary choice per token" do
7+
expect(described_class.first_alternates("وِكَالَةُ/وِكَالَةُ اَلْأَرْصَادِ")).to eq("وِكَالَةُ اَلْأَرْصَادِ")
8+
end
9+
10+
it "leaves plain text untouched" do
11+
expect(described_class.first_alternates("مرحبا بالعالم")).to eq("مرحبا بالعالم")
12+
end
13+
14+
it "handles edges" do
15+
expect(described_class.first_alternates("")).to eq("")
16+
expect(described_class.first_alternates("a/b c/d")).to eq("a c")
17+
end
18+
end
19+
end

0 commit comments

Comments
 (0)