From fc8f2c94b721fb0cc909cc9577372ba7b6906ac6 Mon Sep 17 00:00:00 2001 From: Benoit Favre <benoit.favre@lis-lab.fr> Date: Fri, 24 Mar 2023 15:38:24 +0100 Subject: [PATCH] add open assistant --- RESULTS | 24 ++++++++++++------------ deft.py | 26 ++++++++++++++------------ run_open_assistant.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 24 deletions(-) create mode 100644 run_open_assistant.py diff --git a/RESULTS b/RESULTS index f154b91..c84599f 100644 --- a/RESULTS +++ b/RESULTS @@ -1,23 +1,27 @@ -Résultats sur le dev : +Résultats sur le dev (exact match) : prompt_0 => - bn/par exact +# modèles off-the-shelf (avec instruction finetuning) bloomz-560m 0.0737 -en/bloomz-560m 0.1442 bloomz-3b 0.1442 -en/bloomz-3b 0.1153 ?? bloomz-7b1 0.1602 bloomz-7b1-mt 0.1762 flan-t5-xxl 0.1794 flan-ul2 0.1570 tk-instruct-3b-def 0.1346 tk-instruct-11b-def 0.1826 -en/tk-instruct-11b-def 0.1442 - +oasst-sft-1-pythia-12b 0.0705 opt-iml-1.3b 0.0673 opt-iml-30b 0.1442 +# trad automatique anglais (pas l'air de marcher) +en/bloomz-3b 0.1153 ?? +en/bloomz-560m 0.1442 +en/tk-instruct-11b-def 0.1442 + +# modèles llama (+adaptation lora) + (int8) llama_7B 0.0576 llama_7B+alpaca_fr 0.1185 @@ -33,16 +37,15 @@ llama_30B+deft 0.2467 llama_65B 0.1730 llama_65B+deft 0.3044 - (fp16) llama_30B 0.1891 llama_65B 0.2179 +(api) openai/code-cushman-001 0.1121 openai/code-davinci-002 0.3108 ai21/j1-jumbo 0.0833 - - +cohere_command-xlarge-beta 0.1057 => autres prompts code-cushman-001 0.1346 @@ -54,9 +57,6 @@ text-curie-001 0.1217 text-davinci-003 0.2884 -cohere_command-xlarge-beta 0.1057 - - FrenchMedMCQA: A French Multiple-Choice Question Answering Dataset for Medical domain https://hal.science/hal-03824241v2/preview/LOUHI_2022___QA_22.pdf#page=2 diff --git a/deft.py b/deft.py index 7c46cae..e9386c7 100644 --- a/deft.py +++ b/deft.py @@ -2,11 +2,11 @@ import re import json lm_templates = [ -'''Ceci est une question de QCM de l\'examen de pharmacie. Réponds avec la ou les lettres correspondant à la bonne réponse.\n\n%s\n\nRéponse : (''', -'''Corrigé du QCM de pharma.\n%s\nRéponse(s) : (''', -'''Alice est une excellente pharmacienne. Elle répond aux questions de Pierre qui est interne en pharmacie.\nPierre : ma question est la suivante : %s\n Alice : je connais la bonne réponse et c'est (''', -'''Correction du QCM de l\'examen de pharmacie. %s\nRéponse(s) : (''', -'''Alice est une intelligence artificielle experte en pharmacie. Elle répond aux questions de Bob avec précision.\nBob: %s\n Alice: (''', +'''Ceci est une question de QCM de l\'examen de pharmacie. Réponds avec la ou les lettres correspondant à la bonne réponse.\n\n%s''', +#'''Corrigé du QCM de pharma.\n%s\nRéponse(s) : (''', +#'''Alice est une excellente pharmacienne. Elle répond aux questions de Pierre qui est interne en pharmacie.\nPierre : ma question est la suivante : %s\n Alice : je connais la bonne réponse et c'est (''', +#'''Correction du QCM de l\'examen de pharmacie. %s\nRéponse(s) : (''', +#'''Alice est une intelligence artificielle experte en pharmacie. Elle répond aux questions de Bob avec précision.\nBob: %s\n Alice: (''', ] lm_templates_en = [ '''This is a multiple choice question from the pharma exam. Reply with the letter or the letters corresponding to the correct answer.\n\n%s\n\nAnswer : (''', @@ -14,9 +14,11 @@ lm_templates_en = [ letters = 'abcdefghijklmnopqrstuvwxyz' -def linearize_instance(instance, include_correct_answers=False, add_left_parenthesis=False): +def linearize_instance(instance, include_correct_answers=False, add_left_parenthesis=False, bare=False): result = instance['question'] + '\n' + '\n'.join('(%s) %s.' % (k, v) for k, v in instance['answers'].items()) - if include_correct_answers: + if bare: + return result + elif include_correct_answers: result += '\nRéponse(s) : ' + ' '.join('(%s)' % a for a in instance['correct_answers']) else: result += '\nRéponse(s) :' + (' (' if add_left_parenthesis else '') @@ -28,9 +30,9 @@ def linearize_instance(instance, include_correct_answers=False, add_left_parenth # result += '\nRéponse(s) : ' + ' '.join('(%s)' % a for a in instance['correct_answers']) # return result -def get_prompt(prompt, instance, few_shots=[]): - shots = [linearize_instance(shot, include_correct_answers=True) for shot in few_shots] - return prompt % ('\n\n'.join(shots + [linearize_instance(instance)]),) +def get_prompt(prompt, instance, few_shots=[], **kwargs): + shots = [linearize_instance(shot, include_correct_answers=True, **kwargs) for shot in few_shots] + return prompt % ('\n\n'.join(shots + [linearize_instance(instance, **kwargs)]),) def extract_answer(answer, num_answers=5): answer = re.sub('Ceci est une question de QCM.*', '', answer).strip().lower() @@ -53,7 +55,7 @@ def hamming(preds, refs): return corrects / total_refs -def run_inference(generator, corpus_path, template): +def run_inference(generator, corpus_path, template, **kwargs): with open(corpus_path) as fp: dev_corpus = json.loads(fp.read()) @@ -63,7 +65,7 @@ def run_inference(generator, corpus_path, template): results = [] for instance in dev_corpus: - prompt = get_prompt(template, instance) + prompt = get_prompt(template, instance, **kwargs) print(prompt) generated = generator(prompt) print(generated) diff --git a/run_open_assistant.py b/run_open_assistant.py new file mode 100644 index 0000000..48b90de --- /dev/null +++ b/run_open_assistant.py @@ -0,0 +1,29 @@ +from transformers import GPTNeoXForCausalLM, AutoTokenizer + +pythia_model = 'EleutherAI/pythia-12b-deduped' + +def main(result_path: str, corpus_path: str, model: str = 'OpenAssistant/oasst-sft-1-pythia-12b', template_id: str = '0'): + checkpoint = model + + #tokenizer = AutoTokenizer.from_pretrained(checkpoint) + #llm = AutoModelForCausalLM.from_pretrained(checkpoint, device_map="auto", load_in_8bit=True) + llm = GPTNeoXForCausalLM.from_pretrained(checkpoint, device_map='auto') + tokenizer = AutoTokenizer.from_pretrained(checkpoint) + + def generate(input_string): + #print('###' + input_string + '###') + prompt = "<|prompter|>%s<|endoftext|><|assistant|>" % input_string + inputs = tokenizer(prompt, return_tensors="pt").input_ids.to("cuda") + outputs = llm.generate(inputs, max_new_tokens=32, pad_token_id=tokenizer.eos_token_id) + + generated = tokenizer.decode(outputs[0], skip_special_tokens=True) + #print('@@@' + generated + '@@@') + return generated[len(input_string):] + + import deft + results = deft.run_inference(generate, corpus_path, deft.template_from_id(template_id)) #, bare=True) + deft.write_results(results, result_path) + +if __name__ == '__main__': + import fire + fire.Fire(main) -- GitLab