Skip to content
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

Properly implement cooldown step parsing and explicitly support WSD schedules #71

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions src/contrastors/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class TrainArgs(BaseModel):
warmup_steps: Optional[int] = None
warmup_pct: Optional[float] = None
cooldown_steps: Optional[int] = None
cooldown_pct: Optional[float] = None
checkpoint: Optional[str] = None
wandb: bool
wandb_project_name: str
Expand Down
6 changes: 5 additions & 1 deletion src/contrastors/models/encoder/modeling_nomic_bert.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,11 @@ def forward(
residual = None

batch, seqlen = hidden_states.shape[:2]
hidden_states, indices, cu_seqlens, max_seqlen_in_batch = unpad_input(hidden_states, attention_mask)
try:
hidden_states, indices, cu_seqlens, max_seqlen_in_batch = unpad_input(hidden_states, attention_mask)
except:
# probably on a newer flash-attention version, which now outputs the amt of tokens used by the batch, which we don't need so just drop it
hidden_states, indices, cu_seqlens, max_seqlen_in_batch, _ = unpad_input(hidden_states, attention_mask)
for i, layer in enumerate(self.layers):
if self.gradient_checkpointing and self.training:

Expand Down
24 changes: 20 additions & 4 deletions src/contrastors/trainers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,20 +211,27 @@ def get_optimizer(self, config, ds_config=None):
return optimizer

def get_scheduler(self, config, optimizer, ds_config):
total_num_steps = self.total_num_steps * config.num_epochs

if hasattr(config, "warmup_steps") and getattr(config, "warmup_steps") is not None:
total_num_steps = self.total_num_steps * config.num_epochs
warmup_steps = config.warmup_steps

elif hasattr(config, "warmup_pct") and getattr(config, "warmup_pct") is not None:
total_num_steps = self.total_num_steps * config.num_epochs
warmup_steps = int(total_num_steps * config.warmup_pct)

else:
warmup_steps = 0

if hasattr(config, "cooldown_steps") and getattr(config, "cooldown_steps") is not None:
cooldown_steps = config.cooldown_steps
elif hasattr(config, "cooldown_pct") and getattr(config, "cooldown_pct") is not None:
cooldown_steps = int(total_num_steps * config.cooldown_pct)
else:
cooldown_steps = 0


self.print("*" * 50 + " SCHEDULER " + "*" * 50)
self.print(f"Using {config.schedule_type} learning rate schedule")
self.print(f"Warmup steps: {warmup_steps}")
self.print(f"Cooldown steps: {cooldown_steps}")
self.print(f"Total num steps: {total_num_steps}")

if ds_config:
Expand All @@ -240,11 +247,20 @@ def get_scheduler(self, config, optimizer, ds_config):
scheduler["params"]["total_num_steps"] = total_num_steps
return None

if config.schedule_type == "warmup_stable_decay":
scheduler_specific_kwargs = {
"num_stable_steps": total_num_steps - warmup_steps - cooldown_steps,
"num_decay_steps": cooldown_steps
}
else:
scheduler_specific_kwargs = {}

scheduler = get_scheduler(
name=config.schedule_type,
optimizer=optimizer,
num_warmup_steps=warmup_steps,
num_training_steps=(total_num_steps if config.schedule_type != "inverse_sqrt" else None),
scheduler_specific_kwargs=scheduler_specific_kwargs
)

return scheduler
Expand Down