-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Mac GPU Utilization Support #2789
Comments
Thanks for raising this issue @josebarross! @whoisjones can you check if this works? |
Hi everyone, not sure if (still) relevant. I tried setting the device, unsuccessfully, via .to():
Find the traceback below:
This seems to have already been fixed though Relevant env info: torch==1.13.0.dev20220630 # (but should work with 1.12 as well) Hope it helps! |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
I've run into the same issue as @deakkon. This is still a problem. |
@gojefferson just tried to figure this out--with the merge of #3350 I am able to leverage torch with m1 by setting import flair
import torch
mps_device = torch.device("mps")
flair.device = 'mps:0' then run your code like normal from flair.data import Sentence
from flair.nn import Classifier
# uses apple GPU
tagger = Classifier.load('ner')
sentence = Sentence("Hello there!")
tagger.predict(sentence) it works as-is! |
Changes to Enable Flair on MPS (Apple Silicon)I was working to train/test on data using flair for a project and wanted to use MPS/GPU on Apple Silicon to run it. Example code: # Move model to MPS device and ensure float32 precision
tagger.to(flair.device)
# Make all model parameters are in float32
for param in tagger.parameters():
param.data = param.data.float()
# Make all gradients are also calculated in float32
for param in tagger.parameters():
if param.grad is not None:
param.grad.data = param.grad.data.float()
# 6. Initialize the trainer
trainer = ModelTrainer(tagger, corpus)
# 7. Train the model without AMP
trainer.train(
f'resources/taggers/{runname}_mps',
learning_rate=0.05,
mini_batch_size=16,
save_model_each_k_epochs=2,
monitor_test=True,
max_epochs=5,
use_amp=False,
) Here are the changes I had to make: 1. trainer.pyLocation: Issue: Fix: Made # Original Code:
with torch.autocast(device_type=flair.device.type, enabled=use_amp):
loss, datapoint_count = self.model.forward_loss(batch_step)
# Modified Code:
if use_amp:
with torch.autocast(device_type=flair.device.type):
loss, datapoint_count = self.model.forward_loss(batch_step)
else:
loss, datapoint_count = self.model.forward_loss(batch_step) 2. training_utils.pyLocation: Issue: The Fix: Only pin memory for CUDA devices. # Original Code:
pin_memory = str(flair.device) != "cpu"
# Modified Code:
pin_memory = str(flair.device) == "cuda" |
I am seeing this error after making the changes you suggested. |
@aditya-malani The output of your model and the target labels (ground truth) may have different dimensions. That wouldn't be an issue caused by the above changes. |
Torch has just released MAC M1 support using mps device. I want to know if flair will support it. I tried setting the flair.device manually to mps but it failed when running. Thank you in advance
The text was updated successfully, but these errors were encountered: