OpenNMT / OpenNMT/CTranslate2

[Feature Request] add a classification / estimation layer on top of Encoders

Open
#1,678 1 comment 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
C++
Stars
4.7k
Forks
537
Avg merge
12h 12m
Merged PRs (30d)
4

Description

This has been a long time request.
Recently resurfaced with #1534 .
#1342 is also an example on how to perform classification but HF library is needed.

Technically this is not very difficult since it is just a matter of adding some extra Linear layer after the Encoder.
However this can take various form since all features are not identical.

I would like two use-cases to work to make sure we can check the output.

Use-case 1: XLM-Roberta classifier
it is just a matter of adding a new "Layer" (in CT2 taxonomy) named "classifier" which replicates this: https://github.com/huggingface/transformers/blob/main/src/transformers/models/xlm_roberta/modeling_xlm_roberta.py#L1167
which you can see here: https://github.com/huggingface/transformers/blob/main/src/transformers/models/xlm_roberta/modeling_xlm_roberta.py#L1434-L1453
is a very simple module.

Use-case 2: Unbabel Comet Estimator
This is again a very simple add-on.
A new "Layer" named "estimator" needs to be added based on this:
https://github.com/Unbabel/COMET/blob/master/comet/modules/feedforward.py#L26-L72
since we just need to use it for inference there are only 3 nn.Linear named:
estimator.ff.0
estimator.ff.3
estimator.ff.6
because 1, 2, 4, 5 are not learned weights.

Of course the converter for https://github.com/OpenNMT/CTranslate2/blob/master/python/ctranslate2/converters/transformers.py#L1970
would need to be modified to reflect the new layer.

If everything works fine for XLM-Roberta we can eventually adapt for other encoder-based models.

LMK if you need more info to implement.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Transformers converter at python/ctranslate2/converters/transformers.py around line 1970, then compare the linked XLM-Roberta classifier and COMET feedforward definitions. Trace how encoder layers are represented and converted before deciding how the classifier and estimator layers fit. Done means both referenced use cases can be converted and run for inference, with the new learned weights handled.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, huggingface, python, pytorch
Domain
machine-learning
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.