-
Notifications
You must be signed in to change notification settings - Fork 3k
[Embedding] Add inf-cl in embedding trainer #9673
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
Merged
ZHUI
merged 7 commits into
PaddlePaddle:develop
from
jie-z-0607:add_inf-cl_in_embedding
Dec 25, 2024
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
dd7fc8a
add inf-cl in embedding trainer
jie-z-0607 3b06655
add annotations and fix import
jie-z-0607 e3c55c3
rename inf_cl_loss and fix warning
jie-z-0607 6b6a108
rename simple_inf_cl
jie-z-0607 d69ac4a
Change inf_cl location
jie-z-0607 f05dd61
Change import location
jie-z-0607 8f55e52
Change error information
jie-z-0607 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| # Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from typing import List, Optional | ||
|
|
||
| import paddle | ||
| import paddle.nn as nn | ||
|
|
||
| from .ring import cal_inf_loss | ||
|
|
||
| __all__ = ["Simple_Inf_cl_loss", "Matryoshka_Inf_cl_loss"] | ||
|
|
||
|
|
||
| class Simple_Inf_cl_loss(nn.Layer): | ||
| def __init__(self, inf_cl_head_dim=64): | ||
| super().__init__() | ||
| self.head_dim = inf_cl_head_dim | ||
|
|
||
| def forward(self, q_reps, p_reps): | ||
| group_size = p_reps.shape[0] // q_reps.shape[0] | ||
| labels = paddle.arange(q_reps.shape[0], dtype="int64") | ||
| labels = labels * group_size | ||
| loss = cal_inf_loss(q_reps, p_reps, labels=labels, scale=None, head_dim=self.head_dim) | ||
| return loss | ||
|
|
||
|
|
||
| class Matryoshka_Inf_cl_loss(nn.Layer): | ||
| def __init__(self, embedding_matryoshka_dims: Optional[List[int]] = None, inf_cl_head_dim=64): | ||
| super().__init__() | ||
| if embedding_matryoshka_dims is None: | ||
| self.embedding_matryoshka_dims = [] | ||
| else: | ||
| self.embedding_matryoshka_dims = embedding_matryoshka_dims | ||
| self.loss_fn = Simple_Inf_cl_loss(inf_cl_head_dim) | ||
|
|
||
| def forward(self, q_reps, p_reps): | ||
| if len(self.embedding_matryoshka_dims) > 0: | ||
| loss = 0.0 | ||
| for dim in self.embedding_matryoshka_dims: | ||
| reduced_q_reps = q_reps[:, :dim] | ||
| reduced_q_reps = nn.functional.normalize(reduced_q_reps, axis=-1) | ||
|
|
||
| reduced_p_reps = p_reps[:, :dim] | ||
| reduced_p_reps = nn.functional.normalize(reduced_p_reps, axis=-1) | ||
|
|
||
| dim_loss = self.loss_fn(reduced_q_reps, reduced_p_reps) | ||
| loss += dim_loss | ||
| else: | ||
| loss = self.loss_fn(q_reps, p_reps) | ||
| return loss | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -18,6 +18,10 @@ | |||||
| from paddle.base import core | ||||||
| from paddle.distributed import fleet | ||||||
|
|
||||||
| from ops.src.paddlenlp_kernel.triton.inf_cl.inf_cl_loss import ( | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个没有默认安装,需要 try except一下 |
||||||
| Matryoshka_Inf_cl_loss, | ||||||
| Simple_Inf_cl_loss, | ||||||
| ) | ||||||
| from paddlenlp.trainer import Trainer | ||||||
| from paddlenlp.transformers.contrastive_loss import ( | ||||||
| MatryoshkaContrastiveLoss, | ||||||
|
|
@@ -44,11 +48,19 @@ def __init__(self, model_args, **kwargs): | |||||
| self.accum_rng_states["hybrid"] = [] | ||||||
|
|
||||||
| if model_args.embedding_matryoshka_dims is not None and len(model_args.embedding_matryoshka_dims) > 0: | ||||||
| self.loss_fn = MatryoshkaContrastiveLoss( | ||||||
| model_args.embedding_temperature, model_args.embedding_matryoshka_dims | ||||||
| ) | ||||||
| if model_args.loss_type == "inf_cl": | ||||||
| self.embedding_negatives_cross_device = False | ||||||
| self.loss_fn = Matryoshka_Inf_cl_loss(model_args.embedding_matryoshka_dims, model_args.inf_cl_head_dim) | ||||||
| elif model_args.loss_type == "contrastive": | ||||||
| self.loss_fn = MatryoshkaContrastiveLoss( | ||||||
| model_args.embedding_temperature, model_args.embedding_matryoshka_dims | ||||||
| ) | ||||||
| else: | ||||||
| self.loss_fn = SimpleContrastiveLoss(model_args.embedding_temperature) | ||||||
| if model_args.loss_type == "inf_cl": | ||||||
| self.embedding_negatives_cross_device = False | ||||||
| self.loss_fn = Simple_Inf_cl_loss(model_args.inf_cl_head_dim) | ||||||
| elif model_args.loss_type == "contrastive": | ||||||
| self.loss_fn = SimpleContrastiveLoss(model_args.embedding_temperature) | ||||||
|
|
||||||
| def clear_memory(self): | ||||||
| self.accum_q_features.clear() | ||||||
|
|
||||||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
加一些注释