microsoft / microsoft/onnxruntime
Add the top_k parameter in the GPT models in transformers optimizer tool to vary the answers
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 21.9k
- Forks
- 4.2k
- Avg merge
- 4d 11h
- Merged PRs (30d)
- 184
Description
Describe the feature request
I have exported GPT NEO with the optimizer tool (it is necessary to slightly modify some parts of the code because it is not designed for GPT NEO).
python convert_generation.py -m EleutherAI/gpt-neo-1.3B --decoder_onnx D:\Gpt\GPT_NEO_ONNXRUNTIME\EleutherAI\gpt-neo-1.3B_GPTNeoForCausalLM_past_fp32.onnx --output D:\Gpt\GPT_NEO\NEO_SAMPLING\gpt_neo_beam_search.onnx --cache_dir D:\Gpt\GPT_NEO_ONNXRUNTIME\cache --use_external_data_format --num_beams 1 --top_p 1.0 --temperature=0.9
I am exploring the Sampling option in C++, however, the responses of gpt neo do not vary (see explanation in the scenario description).
My understanding is that the control of the variation is with the top_k parameter, but this is not present in the optimization tool.
Describe scenario use case
GPT models are stateless. I'm trying to provide a context for the character (Anna) to have a memory about her personality.
So, I have this context file: initial_context.txt
This is a conversation between a human named William and an android named Anna.
- Anna is 23 years old.
- Anna has long red hair.
- Anna was created by Soul Shell Corporation.
- Anna lives in Los Angeles California.
- Anna has a strong and athletic body.
- Anna's life goal is to fight for justice.
- Anna likes ice cream.
William: Hello nice to meet you.
Anna: Nice to meet you too.
William: How is it going today?
Anna: Not so bad, thank you! How about you?
William: I am ok.
Anna: I am glad to hear that.
William: where do you live?
This is the program in python using GPT NEO and apply sampling with top_k=50.
import time
import torch
from transformers import GPTNeoForCausalLM, GPT2Tokenizer
model = GPTNeoForCausalLM.from_pretrained("EleutherAI/gpt-neo-1.3B", cache_dir="D:/Gpt/cache")
tokenizer = GPT2Tokenizer.from_pretrained("EleutherAI/gpt-neo-1.3B", cache_dir="D:/Gpt/cache")
device = torch.device('cpu')
print("=======Start===============\n")
condition = True
while condition:
prompt = ""
with open('D:/MoreThanWordsConsole/MoreThanWordsConsole/Mind/GPTNEO/Book/initial_context.txt') as f:
while True:
line = f.readline()
if not line:
break
prompt += line
print(prompt)
input_ids = tokenizer(prompt, return_tensors="pt").input_ids
s = input_ids[0].size(dim=0)
size = s + 20
start_time = time.time()
gen_tokens = model.generate(
input_ids,
do_sample=True,
num_beams=1,
top_k=50,
top_p=1.0,
temperature=0.9,
max_length=size,
early_stopping=False,
num_return_sequences=1,
)
duration = time.time() - start_time
gen_text = tokenizer.batch_decode(gen_tokens)[0]
print(gen_text)
print("--- %s seconds ---" % duration)
print('\n\n')
input_text = input("Continue (y/n): ")
if input_text == 'y' or input_text == 'Y':
condition = True
else:
condition = False
The answers are generally acceptable and mostly vary with the same input. A very desirable feature. For example:
- Anna: I Live in Los Angeles.
- Anna: I am living in Los Angeles California.
- Anna: I am living in an apartment.
If I change the value of top_k = 1, the answer will always be the same, it stops varying.
With GPT NEO optimized in C++ I always have the same response, that is, it behaves as if it had top_k = 1.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the convert_generation.py command and the C++ sampling path described in the issue; trace how top_p and temperature reach generation. Add top_k support to the optimizer tool and verify that top_k=1 is deterministic while larger values allow varied responses, using the GPT-Neo scenario as the behavioral check.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, python
- Domain
- ai, machine-learning, tooling
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100