Fix optim import and ensure PyTorch AMP GradScaler backwards compatibility
Browse files
train.py
CHANGED
|
@@ -21,6 +21,8 @@ Key Features:
|
|
| 21 |
* Tokenizer files saved alongside ONNX models for standalone portability.
|
| 22 |
"""
|
| 23 |
|
|
|
|
|
|
|
| 24 |
import os
|
| 25 |
import sys
|
| 26 |
import re
|
|
@@ -30,14 +32,15 @@ import shutil
|
|
| 30 |
import tempfile
|
| 31 |
import urllib.request
|
| 32 |
import argparse
|
| 33 |
-
from typing import Tuple, Dict, Any
|
| 34 |
|
| 35 |
import numpy as np
|
| 36 |
import pandas as pd
|
| 37 |
import torch
|
| 38 |
import torch.nn as nn
|
| 39 |
-
|
| 40 |
from torch.optim import AdamW
|
|
|
|
| 41 |
from transformers import (
|
| 42 |
AutoTokenizer,
|
| 43 |
AutoModelForSequenceClassification,
|
|
@@ -151,7 +154,7 @@ def train_epoch(
|
|
| 151 |
dataloader: DataLoader,
|
| 152 |
optimizer: optim.Optimizer,
|
| 153 |
scheduler: Any,
|
| 154 |
-
scaler:
|
| 155 |
criterion: nn.Module,
|
| 156 |
device: torch.device,
|
| 157 |
) -> float:
|
|
@@ -167,7 +170,12 @@ def train_epoch(
|
|
| 167 |
labels = batch["labels"].to(device)
|
| 168 |
|
| 169 |
if use_amp:
|
| 170 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
outputs = model(input_ids=input_ids, attention_mask=attention_mask)
|
| 172 |
loss = criterion(outputs.logits, labels)
|
| 173 |
scaler.scale(loss).backward()
|
|
@@ -378,7 +386,10 @@ def main():
|
|
| 378 |
scheduler = get_linear_schedule_with_warmup(
|
| 379 |
optimizer, num_warmup_steps=warmup_steps, num_training_steps=total_steps
|
| 380 |
)
|
| 381 |
-
|
|
|
|
|
|
|
|
|
|
| 382 |
|
| 383 |
# Training Loop with Early Stopping monitoring Macro F1
|
| 384 |
best_val_macro_f1 = -1.0
|
|
|
|
| 21 |
* Tokenizer files saved alongside ONNX models for standalone portability.
|
| 22 |
"""
|
| 23 |
|
| 24 |
+
from __future__ import annotations
|
| 25 |
+
|
| 26 |
import os
|
| 27 |
import sys
|
| 28 |
import re
|
|
|
|
| 32 |
import tempfile
|
| 33 |
import urllib.request
|
| 34 |
import argparse
|
| 35 |
+
from typing import Tuple, Dict, Any, List
|
| 36 |
|
| 37 |
import numpy as np
|
| 38 |
import pandas as pd
|
| 39 |
import torch
|
| 40 |
import torch.nn as nn
|
| 41 |
+
import torch.optim as optim
|
| 42 |
from torch.optim import AdamW
|
| 43 |
+
from torch.utils.data import Dataset, DataLoader
|
| 44 |
from transformers import (
|
| 45 |
AutoTokenizer,
|
| 46 |
AutoModelForSequenceClassification,
|
|
|
|
| 154 |
dataloader: DataLoader,
|
| 155 |
optimizer: optim.Optimizer,
|
| 156 |
scheduler: Any,
|
| 157 |
+
scaler: Any,
|
| 158 |
criterion: nn.Module,
|
| 159 |
device: torch.device,
|
| 160 |
) -> float:
|
|
|
|
| 170 |
labels = batch["labels"].to(device)
|
| 171 |
|
| 172 |
if use_amp:
|
| 173 |
+
try:
|
| 174 |
+
autocast_ctx = torch.amp.autocast(device_type="cuda", dtype=torch.float16)
|
| 175 |
+
except (AttributeError, TypeError):
|
| 176 |
+
autocast_ctx = torch.cuda.amp.autocast()
|
| 177 |
+
|
| 178 |
+
with autocast_ctx:
|
| 179 |
outputs = model(input_ids=input_ids, attention_mask=attention_mask)
|
| 180 |
loss = criterion(outputs.logits, labels)
|
| 181 |
scaler.scale(loss).backward()
|
|
|
|
| 386 |
scheduler = get_linear_schedule_with_warmup(
|
| 387 |
optimizer, num_warmup_steps=warmup_steps, num_training_steps=total_steps
|
| 388 |
)
|
| 389 |
+
try:
|
| 390 |
+
scaler = torch.amp.GradScaler("cuda", enabled=(device.type == "cuda"))
|
| 391 |
+
except (AttributeError, TypeError):
|
| 392 |
+
scaler = torch.cuda.amp.GradScaler(enabled=(device.type == "cuda"))
|
| 393 |
|
| 394 |
# Training Loop with Early Stopping monitoring Macro F1
|
| 395 |
best_val_macro_f1 = -1.0
|