Skip to content

Commit

Permalink
feat: ✨ Add support for training on Apple M1/M2/M3 (mps) devices. (#311)
Browse files Browse the repository at this point in the history
  • Loading branch information
mzouink authored Oct 24, 2024
2 parents 2d11926 + e58a01d commit 193b71b
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
2 changes: 2 additions & 0 deletions dacapo/compute_context/local_torch.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ def device(self):
if free < self.oom_limit: # less than 1 GB free, decrease chance of OOM
return torch.device("cpu")
return torch.device("cuda")
elif torch.backends.mps.is_available():
return torch.device("mps")
else:
return torch.device("cpu")
return torch.device(self._device)
6 changes: 3 additions & 3 deletions dacapo/experiments/trainers/gunpowder_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,12 +309,12 @@ def iterate(self, num_iterations, model, optimizer, device):
param.grad = None

t_start_prediction = time.time()
predicted = model.forward(torch.as_tensor(raw[raw.roi]).to(device).float())
predicted = model.forward(torch.as_tensor(raw[raw.roi]).float().to(device))
predicted.retain_grad()
loss = self._loss.compute(
predicted,
torch.as_tensor(target[target.roi]).to(device).float(),
torch.as_tensor(weight[weight.roi]).to(device).float(),
torch.as_tensor(target[target.roi]).float().to(device),
torch.as_tensor(weight[weight.roi]).float().to(device),
)
loss.backward()
optimizer.step()
Expand Down
1 change: 1 addition & 0 deletions dacapo/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ def train_run(run: Run, do_validate=True):
compute_context = create_compute_context()
run.model = run.model.to(compute_context.device)
run.move_optimizer(compute_context.device)
logger.info(f"Training on {compute_context.device}")

array_store = create_array_store()
run.trainer.iteration = trained_until
Expand Down

0 comments on commit 193b71b

Please sign in to comment.