Skip to content

Conversation

@pacman100
Copy link
Contributor

@pacman100 pacman100 commented Jun 28, 2023

What does this PR do?

  1. For many tasks, users leverage AutoModel to get embeddings followed by postprocessing using that. One of the most popular use case of it is Semantic Similarity via Bi-Encoder models.
  2. This PR enables that
  3. Fixes How to use PEFT to wrap an encoder? #348
  4. Adds an example on how to use it
  5. Also shows how to use PEFT with custom models on top of Transformers

ToDo:

  • Add tests
  • Add docs
  • Add a complete example

@review-notebook-app
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@HuggingFaceDocBuilderDev
Copy link

HuggingFaceDocBuilderDev commented Jun 28, 2023

The documentation is not available anymore as the PR was closed or merged.

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.

I'm still new to a lot of this, so I can't give a full review.

Here are some observations:

  • I couldn't discern how the notebook peft_prompt_tuning_seq2seq_with_generate.ipynb relates to embeddings. Could this be elaborated? Also, it contains a big error message in the first cell.
  • The notebook peft_lora_embedding_semantic_similarity.ipynb also contains an error message at the end. If it takes too long to run, could the number of epochs or the dataset size be reduced? Or at least the KeyboardInterrupt could be caught gracefully to avoid showing the error.
  • In general, at least for me personally, it would be helpful if the notebooks could add a few sentences to explain what is going on and what the use case is.
  • There are no tests for the new class. Are we okay with that? Will it be added later?
  • Similarly, there is also no documentation for the new class (doc entry, docstring), even though this seems to be a big addition.

@pacman100
Copy link
Contributor Author

pacman100 commented Jun 28, 2023

Hello @BenjaminBossan , the example is using PEFT Embedding Task to get embedding for queries and products and then uses them to get the cosine similarity between them to know whether the query and product are similar/relevant.

Also, the first cell didn't have any error, it is just using dataset from cache.

Yes, will be adding tests and docs.

Also working on a full example on this.

@BenjaminBossan
Copy link
Member

the example is using PEFT Embedding Task to get embedding for queries and products and then uses them to get the cosine similarity between them to know whether the query and product are similar/relevant.

Also, the first cell didn't have any error, it is just using dataset from cache.

You mean in peft_lora_embedding_semantic_similarity.ipynb? Yes, but my comment referred to peft_prompt_tuning_seq2seq_with_generate.ipynb, which had a bunch of changes in this PR, and I was not sure how those relate to the PR.

Yes, will be adding tests and docs.

Also working on a full example on this.

Fantastic. I'll do another review once they are added.

@pacman100
Copy link
Contributor Author

but my comment referred to peft_prompt_tuning_seq2seq_with_generate.ipynb, which had a bunch of changes in this PR, and I was not sure how those relate to the PR.

Those were due to make style and make quality

@BenjaminBossan
Copy link
Member

Those were due to make style and make quality

Ah I see, so just disregard my comments about that notebook.

@pacman100 pacman100 changed the title add support for getting embeddings using PEFT add support for Feature Extraction using PEFT Jul 1, 2023
Copy link
Member

@stevhliu stevhliu 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 a guide for such a cool use-case, this is great! 👏

Remember to add the docs to the toctree to properly build it!

pacman100 and others added 6 commits July 13, 2023 11:24
@pacman100
Copy link
Contributor Author

Remember to add the docs to the toctree to properly build it!

Done

Copy link
Contributor

@younesbelkada younesbelkada left a comment

Choose a reason for hiding this comment

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

Very nice addition and learned a lot! I left 2 questions !

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.

I focused on the documentation part for this review, will review the code once the tests pass.

Great docs overall, thanks for putting so much work into this. I have a few minor comments. Please take a look.


## Setup

Start by installing 🤗 PEFT from [source](https://moon-ci-docs.huggingface.co/docs/peft/pr_647/en/install#source), and then navigate to the directory containing the training scripts for fine-tuning DreamBooth with LoRA:
Copy link
Member

Choose a reason for hiding this comment

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

Should this be linking to moon-ci? Also, is it necessary to install from source? We plan to have the release very soon.

pacman100 and others added 5 commits July 13, 2023 15:08
Co-authored-by: Younes Belkada <49240599+younesbelkada@users.noreply.github.com>
Co-authored-by: Benjamin Bossan <BenjaminBossan@users.noreply.github.com>
Copy link
Contributor

@younesbelkada younesbelkada left a comment

Choose a reason for hiding this comment

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

Still looking great, thanks !

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.

Overall, this looks good to me, nice work.

I have one concern left, however. According to the coverage report. the following lines are not covered by tests:

peft/src/peft/peft_model.py

Lines 1614 to 1644 in e2f7ff8

batch_size = input_ids.shape[0]
if attention_mask is not None:
# concat prompt attention mask
prefix_attention_mask = torch.ones(batch_size, peft_config.num_virtual_tokens).to(attention_mask.device)
attention_mask = torch.cat((prefix_attention_mask, attention_mask), dim=1)
if kwargs.get("position_ids", None) is not None:
warnings.warn("Position ids are not supported for parameter efficient tuning. Ignoring position ids.")
kwargs["position_ids"] = None
if kwargs.get("token_type_ids", None) is not None:
warnings.warn("Token type ids are not supported for parameter efficient tuning. Ignoring token type ids")
kwargs["token_type_ids"] = None
kwargs.update(
{
"attention_mask": attention_mask,
"output_attentions": output_attentions,
"output_hidden_states": output_hidden_states,
"return_dict": return_dict,
}
)
if peft_config.peft_type == PeftType.PREFIX_TUNING:
past_key_values = self.get_prompt(batch_size)
return self.base_model(input_ids=input_ids, past_key_values=past_key_values, **kwargs)
else:
if inputs_embeds is None:
inputs_embeds = self.word_embeddings(input_ids)
prompts = self.get_prompt(batch_size=batch_size)
prompts = prompts.to(inputs_embeds.dtype)
inputs_embeds = torch.cat((prompts, inputs_embeds), dim=1)
return self.base_model(inputs_embeds=inputs_embeds, **kwargs)

I double-checked locally and indeed I never hit those lines. When inspecting, only LoRA and IA³ are being tested. So should the tests be extended to include prompt learning or what could be done here?

@pacman100
Copy link
Contributor Author

I have one concern left, however. According to the coverage report. the following lines are not covered by tests:

latest commit should fix this

@pacman100 pacman100 requested a review from BenjaminBossan July 13, 2023 12:29
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 the fixes, this LGTM now.

@pacman100 pacman100 merged commit 92d38b5 into main Jul 13, 2023
@pacman100 pacman100 deleted the smangrul/add-emb-task branch July 13, 2023 12:43
@AngledLuffa
Copy link

This is great! Thanks for the support and for knocking off that old issue of mine.

Guy-Bilitski pushed a commit to Guy-Bilitski/peft that referenced this pull request May 13, 2025
* add support for embedding with peft

* add example and resolve code quality issues

* update notebook example post fixing the loss

* adding full example with inference notebook

* quality ✨

* add tests, docs, guide and rename task_type to be inline with Hub

* fixes

* fixes

* Apply suggestions from code review

Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com>

* Update peft_model.py

* fixes

* final fixes

* Update _toctree.yml

* fixes and make style and make quality

* deberta exception with checkpointing

* Update docs/source/task_guides/semantic-similarity-lora.md

Co-authored-by: Younes Belkada <49240599+younesbelkada@users.noreply.github.com>

* Update docs/source/task_guides/semantic-similarity-lora.md

Co-authored-by: Benjamin Bossan <BenjaminBossan@users.noreply.github.com>

* resolve comments

* testing prompt learning methods

* Update testing_common.py

* fix the tests

---------

Co-authored-by: Steven Liu <59462357+stevhliu@users.noreply.github.com>
Co-authored-by: Younes Belkada <49240599+younesbelkada@users.noreply.github.com>
Co-authored-by: Benjamin Bossan <BenjaminBossan@users.noreply.github.com>
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.

How to use PEFT to wrap an encoder?

7 participants