huggingface / transformers

🤗 Transformers: State-of-the-art Machine Learning for Pytorch, TensorFlow, and JAX.
https://huggingface.co/transformers
Apache License 2.0
133.79k stars 26.75k forks source link

Adding kNN language modeling and Machine Translation #8346

Open yjernite opened 3 years ago

yjernite commented 3 years ago

🌟 Adding the kNN-LM and kNN-MT models

Model description

The kNN Language Model and Machine Translation leverage additional data at test time by looking up examples in the data store that are similar to the test example and using them to inform the prediction. This leads to decreased perplexity for language modeling and increased BLEU for MT.

Ideally, we'd have a general KNNDecoder class which can use any model in the library along with a 🤗datasets data store, similarly to what RAG does currently.

Open source status

sshleifer commented 3 years ago

are you interested @stas00 ? this will be a tough one but cool!

stas00 commented 3 years ago

This definitely sounds interested, thank you for thinking of me, @sshleifer!

But I also want to make sure that since I will be shortly starting on a big project which could take time, if someone wants to work on this one by all means do that.

So I propose to assign me tentatively but if another developer/contributor wants to do it then they are welcome to assign themselves. Thank you!

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stas00 commented 3 years ago

@LysandreJik, can we have some kind of way to tell the stale bot off for feature requests?

Surely if an issue is a bug and it has become old and everybody lost interest in it, it's probably ok to close it automatically, but setting such a short life span for feature-request type of issue just calls for a wasted time to get the bot off one's back and then one is too busy to ping the issue and the feature request disappears.

Thank you!

stas00 commented 3 years ago

Looks like I never had the time to start on this one. I have unassigned myself for now as I honestly won't have time to work on it any time soon. And now it's open to others to work on.

ncoop57 commented 2 years ago

I think using this more efficient version of the KNN-LM might make a future KNNDecoder class more practical: https://github.com/jxhe/efficient-knnlm. Maybe it can be a flag, but anyways it does dimensionality reduction on the embeddings that are generated to reduce the size of the datastore.

I'm seeing the following task to implementing this in the library:

Is there any other steps anyone can think for implementing this?

urialon commented 2 years ago

Hi @yjernite @sshleifer @stas00 @ncoop57 !

I just released an implementation of kNN-LM and our new RetoMaton model (ICML'2022) based on the transformers library at https://github.com/neulab/knn-transformers .

All previous implementations that I know about of kNN-based models are:

What I really like about my implementation is that I used hooks (using layer.register_forward_hook) to avoid forking the entire library, making the KNNWrapper standalone, and it can simply wrap any language model by:

knn_wrapper = KNNWrapper(...) # or: RetomatonWrapper(...)
knn_wrapper.break_into(model)

That's it! The model now internally uses kNN-LM or RetoMaton. In theory, this implementation should also make it very easy to reproduce the kNN-MT experiments, but I haven't tried yet.

Let me know if anyone has questions or wants to implement this inside transformers.

Best, Uri