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

adding peft lora example notebook for ner #2126

Merged
merged 3 commits into from
Oct 10, 2024

Conversation

JINO-ROHIT
Copy link
Contributor

Hi @BenjaminBossan ,

Ive added a lora example for NER, which is mainly a clone of the huggingface NER notebook by Sylvain here https://huggingface.co/learn/nlp-course/chapter7/2#data-collation with couple changes here and there.

lmk what you think

Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

Copy link
Member

@BenjaminBossan BenjaminBossan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Context: #2119

Thanks @JINO-ROHIT for taking this task. Good find of a base notebook to adapt. @HarikrishnanK9 is this what you were looking for?

I haven't done a fine-grained analysis yet, but it looks like the compute_metrics are note being used. Therefore, we don't get the interesting metrics defined in there. Could you please check that?

@JINO-ROHIT
Copy link
Contributor Author

will do

@JINO-ROHIT
Copy link
Contributor Author

i added the eval metric part, the recall and precision seem quite low, seems to be the classes are imbalanced, would you like me to handle that?

Copy link
Member

@BenjaminBossan BenjaminBossan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for the updates. I checked the notebook and found the source for the low performance, namely the learning rate. Check my comment.

Also, when I first ran this notebook, I got an error:

ImportError: To be able to use evaluate-metric/seqeval, you need to install the following dependencies['seqeval'] using 'pip install seqeval' for instance'

So let's mention at the beginning which packages the user should install. We can assume that torch and PEFT (and all their dependencies) are installed, but besides seqeval, let's also mention datasets and evaluate.

Moreover, it would be nice to set the batch size to a higher value. The base model is quite small, so even with batch sizes of 32 or more, most users should be able to run the notebook. This will speed up training considerably. You can add a comment that users have to reduce the value if they run OOM.

],
"source": [
"# Configure LoRA (Low-Rank Adaptation) for fine-tuning\n",
"peft_config = LoraConfig(target_modules = [\"query\", \"key\"])\n",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"peft_config = LoraConfig(target_modules = [\"query\", \"key\"])\n",
"peft_config = LoraConfig(target_modules=[\"query\", \"key\"], task_type=TaskType.TOKEN_CLS))\n",

TaskType needs to be imported from PEFT.

" \"bert-finetuned-ner-lora\",\n",
" evaluation_strategy=\"epoch\",\n",
" save_strategy=\"epoch\",\n",
" learning_rate=2e-5,\n",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The learning rate should be higher for LoRA. This is probably why you find worse results than expected. I checked 2e-4, 2e-3, and 2e-2, and got best results for 2e-3. However, they were still slightly worse than full fine-tuning and I only ran for 3 epochs, so some further experiments could yield better results still.

"metadata": {},
"outputs": [],
"source": [
"# Import required libraries\n",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a few sentences to the beginning before starting with the code. Something along the lines of: "In this notebook, we learn how to ...". You can also add the link to the course here.

@JINO-ROHIT
Copy link
Contributor Author

oh yeah, i totally missed the lr part. The metrics look waaay better now, thanks!

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Copy link
Member

@BenjaminBossan BenjaminBossan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this notebook to show NER with PEFT, LGTM.

From my point of view, it's ready to merge. I'll give @HarikrishnanK9 some time to take a look to possibly give more feedback. If we don't hear back by Friday, I'll merge.

@JINO-ROHIT
Copy link
Contributor Author

gotcha

@HarikrishnanK9
Copy link

@JINO-ROHIT Thank You @BenjaminBossan Just go ahead with the merge.

@BenjaminBossan BenjaminBossan merged commit 5758a7e into huggingface:main Oct 10, 2024
14 checks passed
BenjaminBossan pushed a commit to BenjaminBossan/peft that referenced this pull request Oct 22, 2024
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.

4 participants