Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion angle_emb/angle.py
Original file line number Diff line number Diff line change
Expand Up @@ -1072,6 +1072,23 @@ def fit(self,
if bf16 is None:
bf16 = False

# PyTorch's GradScaler (used for fp16 training) cannot unscale gradients when the
# model parameters are already stored in float16. The standard fix is to promote the
# model to float32 first: GradScaler then works correctly with float32 master weights
# and float16 activations. Training a float16-weight model without GradScaler
# (i.e. just disabling fp16) is numerically unstable due to float16 precision limits
# in the Adam moment accumulators.
if fp16:
backbone_dtype = next(self.backbone.parameters()).dtype
if backbone_dtype == torch.float16:
logger.warning(
'Model parameters are in float16 but fp16 training requires float32 '
'master weights. Casting backbone to float32 so that the GradScaler '
'can operate correctly (float32 params, float16 activations). '
'If memory is a concern, consider using bf16=True instead.'
)
self.backbone = self.backbone.float()

# init argument_kwargs
if argument_kwargs is None:
argument_kwargs = {}
Expand Down Expand Up @@ -1113,7 +1130,7 @@ def fit(self,
train_dataset=train_ds,
eval_dataset=valid_ds,
loss_kwargs=loss_kwargs,
tokenizer=self.tokenizer,
processing_class=self.tokenizer,
args=TrainingArguments(
per_device_train_batch_size=batch_size,
gradient_accumulation_steps=gradient_accumulation_steps,
Expand Down
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ classifiers = [
"Programming Language :: Python :: 3.12",
]
dependencies = [
"bitsandbytes>=0.48.1",
"bitsandbytes==0.49.2",
"boltons>=25.0.0",
"datasets>=4.2.0",
"einops>=0.8.1",
"peft>=0.17.1",
"peft==0.18.1",
"prettytable>=3.16.0",
"scikit-learn>=1.7.2",
"scipy>=1.15.3",
"transformers>=4.57.0",
"transformers==5.3.0",
]

[project.optional-dependencies]
Expand Down
Loading
Loading