Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contrastive Search #2547

Closed
wants to merge 2 commits into from
Closed

Conversation

KexinFeng
Copy link
Contributor

@KexinFeng KexinFeng commented Apr 17, 2023

Description

This PR succeeds PR #2509. The model tracing is shown therein.

It implements contrastive search algo based on torchscript gpt2 model. The onnx model support waits for the issue huggingface/optimum#972 to be solved.

Benchmarked with huggingface transformers' output.

Ref.
https://huggingface.co/blog/introducing-csearch

Demo output

In the demo TestLMSearch.java, we feed in batch sequence input, using right padding with the space token ' ' (id = 220).

["DeepMind Company is",  
 "Memories follow me left and right. I can"]

Output (topk = 3, maxLength = 50):

'DeepMind Company is      \xa0\na small startup that aims to build a better understanding 
of neural networks and how they work. We are currently developing a simple way to create
 a new type of machine learning model that can be used'

"Memories follow me left and right. I can't remember what happened last night, but I know 
that it was very sad. I don't know how long I've been here, but I'm sure there's something
 wrong with me. I'm"

The output successfully avoids the repetitive token output, as expected in Ref. https://huggingface.co/blog/introducing-csearch.

Model tracing

The onnx model gpt2.onnx is loaded from https://huggingface.co/docs/optimum/main/en/exporters/onnx/usage_guides/export_a_model#exporting-a-model-using-past-keysvalues-in-the-decoder.
See also https://github.com/huggingface/optimum/releases.

The gpt2.pt is traced with the following scripts: https://gist.github.com/KexinFeng/4876c6bfb27f40abffe4d5a92c02acff

@KexinFeng KexinFeng requested review from zachgk, frankfliu and a team as code owners April 17, 2023 06:37
@KexinFeng KexinFeng marked this pull request as draft April 17, 2023 06:37
@KexinFeng KexinFeng changed the title POC Contrastive Search Contrastive Search Apr 17, 2023
@KexinFeng KexinFeng marked this pull request as ready for review May 2, 2023 02:20
@KexinFeng
Copy link
Contributor Author

Merged in #2637

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant