You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@opennlp.apache.org by "krickert (via GitHub)" <gi...@apache.org> on 2026/06/12 04:06:09 UTC

[PR] OPENNLP-1838: Adopt BertTokenizer in opennlp-dl components (opennlp)

krickert opened a new pull request, #1075:
URL: https://github.com/apache/opennlp/pull/1075

   > [!NOTE]
   > **Stacked on #1072, #1073 and #1074.** This branch includes their commits because it depends on `BertTokenizer` from #1073. Once those merge, this PR will be rebased onto `main` and the diff collapses to the OPENNLP-1838 changes only. Draft until then.
   
   ## What
   
   - `SentenceVectorsDL`, `DocumentCategorizerDL` and `NameFinderDL` now tokenize with `BertTokenizer` (basic tokenization / normalization, then wordpiece) instead of raw `WordpieceTokenizer`.
   - Lower casing defaults follow each component's commonly used models:
     - `SentenceVectorsDL` and `DocumentCategorizerDL`: **true** (the README-recommended models are uncased)
     - `NameFinderDL`: **false** (recommended NER models such as `dslim/bert-base-NER` are cased; capitalization is a core signal for entity boundaries)
   - Overridable via the new tri-state `InferenceOptions.setLowerCase(boolean)` or the new `SentenceVectorsDL(model, vocab, lowerCase)` constructor. RoBERTa-style special-token detection is preserved.
   
   ## Why
   
   See [OPENNLP-1838](https://issues.apache.org/jira/browse/OPENNLP-1838) and OPENNLP-1837: without basic tokenization, uncased models receive `[UNK]` for every capitalized or accented word, severely degrading results.
   
   ## Validation
   
   - New `CreateTokenizerTest` covers lower-casing vs. case-preserving tokenizers, RoBERTa special-token selection, and the `InferenceOptions` default resolution.
   - Verified end-to-end with the sentence-transformers all-MiniLM-L6-v2 ONNX export: the existing `SentenceVectorsDLEval` pinned values still hold, and a capitalized variant of the eval sentence now produces vectors identical to the lowercase one (previously every capitalized word mapped to `[UNK]`). Added as an eval assertion.
   - Verified with the real `dslim/bert-base-NER` vocabulary that all `NameFinderDLEval` input sentences tokenize identically before and after this change, so the NER eval expectations remain valid.
   
   ## Note for eval-data owners
   
   `DocumentCategorizerDLEval` expectations will shift because lower casing changes the tokens sent to the uncased sentiment models (that is the intended fix). The pinned values need to be regenerated against the canonical eval-data ONNX files, which are not publicly available. Happy to update the pins if someone can run the eval and share the new values.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@opennlp.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] OPENNLP-1838: Adopt BertTokenizer in opennlp-dl components (opennlp)

Posted by "rzo1 (via GitHub)" <gi...@apache.org>.
rzo1 commented on PR #1075:
URL: https://github.com/apache/opennlp/pull/1075#issuecomment-4698862172

   50/51 did succeed. Onto evals are quite slow. Perhaps another investigstion target next time


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@opennlp.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] OPENNLP-1838: Adopt BertTokenizer in opennlp-dl components (opennlp)

Posted by "mawiesne (via GitHub)" <gi...@apache.org>.
mawiesne merged PR #1075:
URL: https://github.com/apache/opennlp/pull/1075


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@opennlp.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] OPENNLP-1838: Adopt BertTokenizer in opennlp-dl components (opennlp)

Posted by "mawiesne (via GitHub)" <gi...@apache.org>.
mawiesne commented on PR #1075:
URL: https://github.com/apache/opennlp/pull/1075#issuecomment-4694661295

   > I have triggered one for you: https://ci-builds.apache.org/job/OpenNLP/job/eval-tests-configurable/48/
   
   @rzo1 Something seems odd with this run:
   
   Run time is way too fast, confirmed by this output:
   
   ```
   [INFO] --- surefire:3.5.5:test (default-test) @ opennlp-eval-tests ---
   [INFO] Tests are skipped.
   ```
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@opennlp.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] OPENNLP-1838: Adopt BertTokenizer in opennlp-dl components (opennlp)

Posted by "rzo1 (via GitHub)" <gi...@apache.org>.
rzo1 commented on PR #1075:
URL: https://github.com/apache/opennlp/pull/1075#issuecomment-4689905028

   Will look into this once the other stacked onces are done.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@opennlp.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] OPENNLP-1838: Adopt BertTokenizer in opennlp-dl components (opennlp)

Posted by "rzo1 (via GitHub)" <gi...@apache.org>.
rzo1 commented on PR #1075:
URL: https://github.com/apache/opennlp/pull/1075#issuecomment-4693598461

   > Happy to update the pins if someone can run the eval and share the new values.
   
   You find the eval data here: https://nightlies.apache.org/opennlp/
   
   You can also trigger a eval build using your ASF credentials here: https://ci-builds.apache.org/job/OpenNLP/job/eval-tests-configurable/
   
   I have triggered one for you: https://ci-builds.apache.org/job/OpenNLP/job/eval-tests-configurable/48/


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@opennlp.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] OPENNLP-1838: Adopt BertTokenizer in opennlp-dl components (opennlp)

Posted by "krickert (via GitHub)" <gi...@apache.org>.
krickert commented on PR #1075:
URL: https://github.com/apache/opennlp/pull/1075#issuecomment-4694708476

   Thanks @rzo1 @mawiesne.  Eval build #48 didn't actually exercise the new DL evals. `opennlp-eval-tests` sets surefire `<skip>true</skip>` in the base build config, and the `eval-tests` profile adds includes but never overrides
   the skip - so the module compiles the eval sources and runs nothing. 
   
   You can see it in the build log: the module finishes in ~1.2s and no `*DLEval` class appears anywhere in the console output. The 53 minutes of eval work all happened in the classic suites.
   
   This also means the `DocumentCategorizerDLEval` pin shift described in the PR text has not been observed by CI yet. It turns out the doccat ONNX eval models *are* in the nightlies eval data (`onnx/doccat/`), so I'm running the DL evals locally against it right now and will push the regenerated pinned values to this branch shortly - please
   hold off merging until that commit lands.
   
   The surefire skip issue itself is a pre-existing bug on `main` (it affects every eval class in the module, not just the DL ones). 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@opennlp.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] OPENNLP-1838: Adopt BertTokenizer in opennlp-dl components (opennlp)

Posted by "rzo1 (via GitHub)" <gi...@apache.org>.
rzo1 commented on PR #1075:
URL: https://github.com/apache/opennlp/pull/1075#issuecomment-4694727357

   Not related to this PR, see https://ci-builds.apache.org/job/OpenNLP/job/eval-tests/ (a few minutes only for the last days)
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@opennlp.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] OPENNLP-1838: Adopt BertTokenizer in opennlp-dl components (opennlp)

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #1075:
URL: https://github.com/apache/opennlp/pull/1075#discussion_r3404715497


##########
opennlp-core/opennlp-ml/opennlp-dl/src/main/java/opennlp/dl/AbstractDL.java:
##########
@@ -122,6 +123,50 @@ protected WordpieceTokenizer createTokenizer(
     return new WordpieceTokenizer(vocab.keySet());
   }
 
+  /**
+   * Creates a {@link BertTokenizer} that performs the full BERT tokenization
+   * pipeline: basic tokenization (text normalization) followed by wordpiece.
+   * The special tokens are selected based on the vocabulary: if it contains
+   * RoBERTa-style tokens, those are used, otherwise the BERT defaults.
+   *
+   * @param vocab The vocabulary map.
+   * @param lowerCase {@code true} for uncased models (lower casing and accent
+   *     stripping), {@code false} for cased models.
+   * @return A configured {@link BertTokenizer}.
+   */
+  protected BertTokenizer createTokenizer(
+      final Map<String, Integer> vocab, final boolean lowerCase) {
+    if (vocab.containsKey(
+            WordpieceTokenizer.ROBERTA_CLS_TOKEN)
+        && vocab.containsKey(
+            WordpieceTokenizer.ROBERTA_SEP_TOKEN)) {
+      final String unk = vocab.containsKey(
+          WordpieceTokenizer.ROBERTA_UNK_TOKEN)
+          ? WordpieceTokenizer.ROBERTA_UNK_TOKEN
+          : WordpieceTokenizer.BERT_UNK_TOKEN;
+      return new BertTokenizer(
+          vocab.keySet(),
+          lowerCase,
+          WordpieceTokenizer.ROBERTA_CLS_TOKEN,
+          WordpieceTokenizer.ROBERTA_SEP_TOKEN,
+          unk);

Review Comment:
   `createTokenizer(vocab, lowerCase)` falls back to `WordpieceTokenizer.BERT_UNK_TOKEN` when the vocabulary has RoBERTa CLS/SEP tokens but is missing `ROBERTA_UNK_TOKEN`. If the vocabulary also doesn't contain `[UNK]`, this produces a tokenizer that can emit an unknown token not present in the vocabulary, failing later during id mapping with a less-direct error. Prefer validating that at least one supported UNK token is present and throw a clear `IllegalArgumentException` otherwise.



##########
opennlp-eval-tests/src/test/java/opennlp/dl/vectors/SentenceVectorsDLEval.java:
##########
@@ -61,6 +61,13 @@ public void generateVectorsTest() throws Exception {
       Assertions.assertEquals(0.20219636, vectors[1], 0.00001);
       Assertions.assertEquals(0.41306049, vectors[2], 0.00001);
       Assertions.assertEquals(384, vectors.length);
+
+      // The uncased model lower cases during tokenization, so a capitalized
+      // variant must produce the same vectors. Prior to BERT basic
+      // tokenization, every capitalized word was mapped to [UNK].
+      final float[] capitalized = sv.getVectors("George Washington was President");
+
+      Assertions.assertArrayEquals(vectors, capitalized);

Review Comment:
   `Assertions.assertArrayEquals(vectors, capitalized)` asserts exact float equality, which can be brittle across ONNX Runtime versions/platforms even when the inputs are intended to be equivalent. Use the `assertArrayEquals(float[], float[], float delta)` overload with a small tolerance (similar to the scalar assertions above).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@opennlp.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] OPENNLP-1838: Adopt BertTokenizer in opennlp-dl components (opennlp)

Posted by "mawiesne (via GitHub)" <gi...@apache.org>.
mawiesne commented on PR #1075:
URL: https://github.com/apache/opennlp/pull/1075#issuecomment-4691988448

   @krickert Ready for rebase, as all other changes in in main now.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@opennlp.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] OPENNLP-1838: Adopt BertTokenizer in opennlp-dl components (opennlp)

Posted by "krickert (via GitHub)" <gi...@apache.org>.
krickert commented on PR #1075:
URL: https://github.com/apache/opennlp/pull/1075#issuecomment-4692614068

   > @krickert Ready for rebase, as all other changes in in main now.
   
   Rebased, ready for review


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@opennlp.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] OPENNLP-1838: Adopt BertTokenizer in opennlp-dl components (opennlp)

Posted by "mawiesne (via GitHub)" <gi...@apache.org>.
mawiesne commented on PR #1075:
URL: https://github.com/apache/opennlp/pull/1075#issuecomment-4694921686

   @krickert Could you quickly rebase on top of recent main to include the eval tests fix on your branch. I'll start a on ASF Jenkins shortly after, so we have multiple sources of eval result data.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@opennlp.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] OPENNLP-1838: Adopt BertTokenizer in opennlp-dl components (opennlp)

Posted by "mawiesne (via GitHub)" <gi...@apache.org>.
mawiesne commented on PR #1075:
URL: https://github.com/apache/opennlp/pull/1075#issuecomment-4695106504

   In roughly 4 hours time, this build should kick in for your fork + branch:
   
   https://ci-builds.apache.org/job/OpenNLP/job/eval-tests-configurable/50
   
   Note: URL will only work once build no. 49 has terminated.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@opennlp.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] OPENNLP-1838: Adopt BertTokenizer in opennlp-dl components (opennlp)

Posted by "krickert (via GitHub)" <gi...@apache.org>.
krickert commented on PR #1075:
URL: https://github.com/apache/opennlp/pull/1075#issuecomment-4698766265

   @mawiesne for some reason the URL isn't working for me but I'm on a phone browser.  Did this run get stuck too?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@opennlp.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] OPENNLP-1838: Adopt BertTokenizer in opennlp-dl components (opennlp)

Posted by "krickert (via GitHub)" <gi...@apache.org>.
krickert commented on PR #1075:
URL: https://github.com/apache/opennlp/pull/1075#issuecomment-4699037561

   Looks like it passed.  Squash and merge ok?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@opennlp.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] OPENNLP-1838: Adopt BertTokenizer in opennlp-dl components (opennlp)

Posted by "mawiesne (via GitHub)" <gi...@apache.org>.
mawiesne commented on PR #1075:
URL: https://github.com/apache/opennlp/pull/1075#issuecomment-4695061597

   @krickert I just notized you're working via a branch on a fork. Forget my last question / comment related to a rebase...
   
   For the records, here's a eval run kicked for fixed _main_ branch:
   https://ci-builds.apache.org/job/OpenNLP/job/eval-tests-configurable/49/
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@opennlp.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org