-
Notifications
You must be signed in to change notification settings - Fork 31.8k
Trainer multi label #7191
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
Merged
Trainer multi label #7191
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,7 +3,7 @@ | |
|
|
||
| import numpy as np | ||
|
|
||
| from .file_utils import is_tf_available, is_torch_available | ||
| from .file_utils import is_tf_available, is_torch_available, is_torch_tpu_available | ||
| from .tokenization_utils_base import ExplicitEnum | ||
|
|
||
|
|
||
|
|
@@ -132,9 +132,49 @@ class HPSearchBackend(ExplicitEnum): | |
| } | ||
|
|
||
|
|
||
| def nested_concat(tensors, new_tensors, dim=0): | ||
| "Concat the `new_tensors` to `tensors` on `dim`. Works for tensors or nested list/tuples of tensors." | ||
| if is_torch_available(): | ||
| assert type(tensors) == type( | ||
| new_tensors | ||
| ), f"Expected `tensors` and `new_tensors` to have the same type but found {type(tensors)} and {type(new_tensors)}." | ||
| if isinstance(tensors, (list, tuple)): | ||
| return type(tensors)(nested_concat(t, n, dim) for t, n in zip(tensors, new_tensors)) | ||
| return torch.cat((tensors, new_tensors), dim=dim) | ||
| else: | ||
| raise ImportError("Torch must be installed to use `nested_concat`") | ||
|
|
||
|
|
||
| def nested_numpify(tensors): | ||
| "Numpify `tensors` (even if it's a nested list/tuple of tensors)." | ||
| if isinstance(tensors, (list, tuple)): | ||
| return type(tensors)(nested_numpify(t) for t in tensors) | ||
| return tensors.cpu().numpy() | ||
|
|
||
|
|
||
| def nested_detach(tensors): | ||
| "Detach `tensors` (even if it's a nested list/tuple of tensors)." | ||
| if isinstance(tensors, (list, tuple)): | ||
| return type(tensors)(nested_detach(t) for t in tensors) | ||
| return tensors.detach() | ||
|
|
||
|
|
||
| def nested_xla_mesh_reduce(tensors, name): | ||
| if is_torch_tpu_available(): | ||
| import torch_xla.core.xla_model as xm | ||
|
|
||
| if isinstance(tensors, (list, tuple)): | ||
| return type(tensors)(nested_xla_mesh_reduce(t, f"{name}_{i}") for i, t in enumerate(tensors)) | ||
| return xm.mesh_reduce(name, tensors, torch.cat) | ||
| else: | ||
| raise ImportError("Torch xla must be installed to use `nested_xla_mesh_reduce`") | ||
|
|
||
|
Comment on lines
+162
to
+171
Member
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. Did you get a chance to test this on TPU?
Collaborator
Author
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. No, was planning to ask you about it this morning. |
||
|
|
||
| def distributed_concat(tensor: "torch.Tensor", num_total_examples: Optional[int] = None) -> "torch.Tensor": | ||
| if is_torch_available(): | ||
| try: | ||
| if isinstance(tensor, (tuple, list)): | ||
| return type(tensor)(distributed_concat(t, num_total_examples) for t in tensor) | ||
| output_tensors = [tensor.clone() for _ in range(torch.distributed.get_world_size())] | ||
| torch.distributed.all_gather(output_tensors, tensor) | ||
| concat = torch.cat(output_tensors, dim=0) | ||
|
|
||
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
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.
No need to test for old deprecated argument names since they have all been changed in the lib and the user can now set their own name if they have an old model they are still using.
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.
good! this will reduce cruft