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

Support to Apple’s Metal Performance Shaders (MPS) backend #3

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
35 changes: 22 additions & 13 deletions tendims.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,29 @@ def __init__(self, models_dir = './models/lstm_trained_models',
#load models
self.dim2model = {}
self.dim2embedding = {}

#choose device
self.device = torch.device('cpu')
if self.is_cuda:
print(f'Torch version: {torch.__version__}')
print(f'Torch CUDA available : {torch.cuda.is_available()}')
if torch.cuda.is_available():
print(f'Torch current device : {torch.cuda.current_device()}')
print(f'Torch device count : {torch.cuda.device_count()}')
print(f'Torch device name : {torch.cuda.get_device_name(0)}')
print('Instantiating the TenDimensionsClassifiers to CUDA.')
self.device = torch.device('cuda')
elif hasattr(torch.backends, 'mps') and torch.backends.mps.is_available():
print(f'Torch MPS available : {torch.backends.mps.is_available()}')
print('Instantiating the TenDimensionsClassifiers to MPS.')
self.device = torch.device('mps')
else:
print('Cuda not available. Instantiated the TenDimensionsClassifier with CUDA=False')
self.is_cuda = False

for dim in self.dimensions_list:
model = LSTMClassifier(embedding_dim=300, hidden_dim=300)
if self.is_cuda:
print(f'Torch version: {torch.__version__}')
print(f'Torch CUDA available : {torch.cuda.is_available()}')
if torch.cuda.is_available():
print(f'Torch current device : {torch.cuda.current_device()}')
print(f'Torch device count : {torch.cuda.device_count()}')
print(f'Torch device name : {torch.cuda.get_device_name(0)}')
model.cuda()
else:
print('Cuda not available. Instantiated the TenDimensionsClassifier with CUDA=False')
self.is_cuda = False
model.to(self.device)
model.eval()
for modelname in os.listdir(self.models_dir):
if ('-best.lstm' in modelname) & (dim in modelname):
Expand Down Expand Up @@ -97,7 +106,7 @@ def compute_score(self, text, dimensions=None):
input_ = em.obtain_vectors_from_sentence(tokenize(text), True)
input_ = torch.tensor(input_).float().unsqueeze(0)
if self.is_cuda:
input_ = input_.cuda()
input_ = input_.to(self.device)
output = model(input_)
score = torch.sigmoid(output).item()
dimension_scores[dim] = score
Expand Down Expand Up @@ -146,4 +155,4 @@ def compute_score_split(self, text, dimensions=None, min_tokens=3):
if len(dimension_scores) == 1:
return list(dimension_scores.values())[0]
else:
return dimension_scores
return dimension_scores