salesforce / CodeT5

Home of CodeT5: Open Code LLMs for Code Understanding and Generation
https://arxiv.org/abs/2305.07922
BSD 3-Clause "New" or "Revised" License
2.68k stars 394 forks source link

How to use fine-tuned model with my own dataset and task? #32

Closed blurLake closed 2 years ago

blurLake commented 2 years ago

Hi,

I fine-tuned a model using my own dataset, task and subtask. Say the task is called "own_task" and subtask is "c" since it is about c scripts. Now I have models saved in saved_models/own_task/c/codet5_small/checkpoint-best-ppl/ and checkpoint-last/ as pytorch_model.bin. Then I try to load this model using

from transformers import T5Config, RobertaTokenizer, T5ForConditionalGeneration
model = T5ForConditionalGeneration.from_pretrained("dir_saved_model")

It fails with error about missing config.json file. Is this the correct way to load the model? Do I need to generate a config.json for the model manually, or it can be done automatically?

Thank you very much.

blurLake commented 2 years ago

My task is a generation task, so I reused run_gen.py.

yuewang-cuhk commented 2 years ago

Hi, if the model folder does not contain config.json, you load the model via

from transformers import T5Config, RobertaTokenizer, T5ForConditionalGeneration
config = T5Config.from_pretrained('Salesforce/codet5-small')
model = T5ForConditionalGeneration.from_pretrained("dir_saved_model",  config=config)

Or you can revise the way you save the model in run_gen.py here like this: model.save_pretrained(output_model_file). Then the output_model_file will contain config.json.