Skip to content

Conversation

@mohammad-albarham
Copy link

Hej,

I have mac with mps, the current yaml files does support the m-chip mac machines.

However, I solved this but edit the file and make it appropriate for mac with m-chip. The solutions is as follows:

I have created a file called conda-environment-files/conda-environment-gpu-mac-mps.yml. Then, I have followed the same instructions to create the environment, but I have an error which was expected because of conda and pip packages. The error is as follows:

OMP: Error #15: Initializing libomp.dylib, but found libomp.dylib already initialized.
OMP: Hint This means that multiple copies of the OpenMP runtime have been linked into the program. 
That is dangerous, since it can degrade performance or cause incorrect results. 
The best thing to do is to ensure that only a single OpenMP runtime is linked into the process, e.g. by avoiding static linking of the OpenMP runtime in any library. As an unsafe, unsupported, undocumented workaround you can set the environment variable KMP_DUPLICATE_LIB_OK=TRUE to allow the program to continue to execute, but that may cause crashes or silently produce incorrect results. For more information, please see http://openmp.llvm.org/

This can be solved easily by setting an environment variable to True as:

export KMP_DUPLICATE_LIB_OK=TRUE
source ~/.bashrc

or in the python file as:

import os
os.environ['KMP_DUPLICATE_LIB_OK'] = 'True'

After that, I have tested the gpu and it worked well. The following code was used to test the setup:

import torch
import time

# Check if MPS is available
device = torch.device("mps" if torch.backends.mps.is_available() else "cpu")

# Define a simple neural network for testing
class SimpleNN(torch.nn.Module):
    def __init__(self):
        super(SimpleNN, self).__init__()
        self.fc1 = torch.nn.Linear(784, 128)
        self.fc2 = torch.nn.Linear(128, 10)

    def forward(self, x):
        x = torch.relu(self.fc1(x))
        return self.fc2(x)

model = SimpleNN().to(device)
batch_size, input_shape = 64, (784,)
data = torch.randn(batch_size, *input_shape).to(device)

# Time the forward pass on the chosen device
start = time.time()
output = model(data)
end = time.time()

print(f"Forward pass took {end - start:.4f} seconds on {device}")

I got the following results:

MPS is available
tensor([1.], device='mps:0')
Forward pass took 0.1280 seconds on mps

So, simply we need to add the file to directory and set KMP_DUPLICATE_LIB_OK variable to True. This PR resolve #1

@mohammad-albarham
Copy link
Author

@georghess @Eiphodos @eriklandolsi

I can still keep testing this during the course to make sure that the solution is robust.

@eriklandolsi
Copy link
Contributor

@mohammad-albarham, good suggestion! If this solves your problem for now, you could just this solution throughout the course. Just note that we will make updates to master regarding HA1+2 and upcoming instructions for cloud compute, so if you have a local branch, you need to keep it in sync with master.

If this solution works for you throughout all home assignments, we could merge it to master closer to the end of the course. Until then, we will direct any other students with mac that are interested to this PR.

I will set a reminder to follow up in a few weeks time.

(Side note: George Hess is no longer part of the course staff)

@mohammad-albarham
Copy link
Author

@eriklandolsi Looks good to me. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Introduce "MPS" support.

2 participants