Kristian Rickert created OPENNLP-1836:
-----------------------------------------

             Summary: SentenceVectorsDL sends an all-zero attention mask and 
incorrect token type ids to the ONNX model
                 Key: OPENNLP-1836
                 URL: https://issues.apache.org/jira/browse/OPENNLP-1836
             Project: OpenNLP
          Issue Type: Bug
          Components: dl
    Affects Versions: 3.0.0-M3
            Reporter: Kristian Rickert
            Assignee: Kristian Rickert


SentenceVectorsDL encodes its model inputs incorrectly and has two robustness
issues in the same code path.

1. Wrong input encoding

The private tokenize method never fills the attention_mask array (it remains
all zeros) and fills token_type_ids with 1:

    final long[] mask = new long[ids.length];   // all zeros
    ...
    Arrays.fill(types, 1);

For BERT-style encoders the convention is the opposite: attention_mask must be
1 for every real token and token_type_ids must be 0 for single-segment input.
With an all-zero mask the encoder attends to nothing, so the produced
"sentence vectors" do not reflect the input sentence the way the underlying
sentence-transformers model intends. DocumentCategorizerDL in the same module
already uses the correct encoding (mask=1, types=0), so SentenceVectorsDL is
also inconsistent with its sibling class.

2. NPE on a vocabulary mismatch

tokenize() looks up token ids with vocab.get(token) and unboxes the result.
If the tokenizer emits a token that is missing from the vocabulary map (e.g.
a vocabulary file that does not match the model and lacks the [UNK] entry),
the call fails with an opaque NullPointerException.

3. Native resource leak

getVectors() never closes the three OnnxTensor inputs nor the
OrtSession.Result, leaking native memory on every call.

Proposed fix
------------

* Fill attention_mask with 1 and leave token_type_ids at 0 (single segment),
  matching DocumentCategorizerDL.
* Throw a descriptive IllegalArgumentException when a token cannot be mapped,
  stating that the vocabulary file does not match the model.
* Close all OnnxTensors and the OrtSession.Result deterministically.
* Add a unit test for the encoding (tokenize is made package-private static;
  it requires no ONNX session) and update the eval test expectations.

Verification of the eval test values
------------------------------------

SentenceVectorsDLEval pins exact vector values for the sentence
"george washington was president". Running the current (unfixed) code against
the public sentence-transformers/all-MiniLM-L6-v2 ONNX export reproduces the
pinned values exactly (0.39994872, -0.055101186, 0.2817594), confirming the
eval data model is the same export. Re-running with the corrected encoding
yields the new expected values (0.044745024, 0.20219636, 0.41306049,
dimension 384), which the patched class reproduces precisely.

Compatibility note
------------------

This is a behavioral fix: vectors produced by the previous encoding are not
comparable with the corrected output. Users who persisted vectors from
SentenceVectorsDL need to re-embed their data. This should be called out in
the release notes.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to