Uncategorized

pytorch – Can we launch accelerate from a Python script instead of CLI?


I’m following an HuggingFace course where it is presented the following code.

from accelerate import Accelerator
from transformers import AdamW, AutoModelForSequenceClassification, get_scheduler

accelerator = Accelerator()

model = AutoModelForSequenceClassification.from_pretrained(checkpoint, num_labels=2)
optimizer = AdamW(model.parameters(), lr=3e-5)

train_dl, eval_dl, model, optimizer = accelerator.prepare(
    train_dataloader, eval_dataloader, model, optimizer
)

num_epochs = 3
num_training_steps = num_epochs * len(train_dl)
lr_scheduler = get_scheduler(
    "linear",
    optimizer=optimizer,
    num_warmup_steps=0,
    num_training_steps=num_training_steps,
)

progress_bar = tqdm(range(num_training_steps))

model.train()
for epoch in range(num_epochs):
    for batch in train_dl:
        outputs = model(**batch)
        loss = outputs.loss
        accelerator.backward(loss)

        optimizer.step()
        lr_scheduler.step()
        optimizer.zero_grad()
        progress_bar.update(1)

To take advantage of the accelerate library, can I simply execute this script as a typical Python program (python train.py), or is it mandatory to use the accelerate CLI commands (accelerate launch train.py) to enable the distributed training?

I did not try this as I do not have multiple GPUs to test the distributed training.



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *