This repository was archived by the owner on Jun 4, 2025. It is now read-only.
forked from huggingface/transformers
-
Notifications
You must be signed in to change notification settings - Fork 3
Merged
Changes from all commits
Commits
Show all changes
2 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
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,60 @@ | ||
| from typing import Any | ||
spacemanidol marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| import numpy | ||
|
|
||
| from sparseml.pytorch.utils import ModuleExporter | ||
| from transformers.modeling_outputs import SequenceClassifierOutput | ||
| from transformers.sparse import SparseMLTrainer | ||
|
|
||
|
|
||
| class SparseMLGLUETrainer(SparseMLTrainer): | ||
| """ | ||
| GLUE trainer with SparseML integration | ||
|
|
||
| :param recipe: recipe for model sparsification | ||
| :param teacher: teacher model for distillation | ||
| :param distill_hardness: ratio of loss by teacher targets (between 0 and 1) | ||
| :param distill_temperature: temperature for distillation | ||
| :param args, kwargs: arguments passed into parent class | ||
| """ | ||
|
|
||
| def compute_loss(self, model, inputs, return_outputs=False): | ||
| """ | ||
| Computing loss using teacher/student distillation | ||
| """ | ||
| if not self.recipes or self.teacher is None: | ||
| return super().compute_loss(model, inputs, return_outputs=return_outputs) | ||
|
|
||
| student_outputs = model(**inputs) | ||
| loss = student_outputs["loss"] | ||
|
|
||
| teacher_input_keys = ["input_ids", "token_type_ids", "attention_mask"] | ||
| teacher_inputs = {k: inputs[k] for k in teacher_input_keys} | ||
|
|
||
| steps_in_epoch = -1 # Unused | ||
| loss = self.manager.loss_update( | ||
| loss, | ||
| model, | ||
| self.optimizer, | ||
| self.state.epoch, | ||
| steps_in_epoch, | ||
| global_step=self.state.global_step, | ||
| student_outputs=student_outputs, | ||
| teacher_inputs=teacher_inputs, | ||
| ) | ||
| return (loss, student_outputs) if return_outputs else loss | ||
|
|
||
|
|
||
| class GLUEModuleExporter(ModuleExporter): | ||
| """ | ||
| Module exporter class for Sequence Classification | ||
| """ | ||
|
|
||
| @classmethod | ||
| def get_output_names(self, out: Any): | ||
| if not isinstance(out, SequenceClassifierOutput): | ||
| raise ValueError(f"Expected SequenceClassifierOutput, got {type(out)}") | ||
| expected = ["logits"] | ||
| if numpy.any([name for name in expected if name not in out]): | ||
| raise ValueError("Expected output names not found in model output") | ||
| return expected | ||
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.
Uh oh!
There was an error while loading. Please reload this page.