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 for Mac M1/M2 #947

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

dkrantsberg
Copy link

Adds support for Apple Silicon processors by using MPS/CPU instead of CUDA.

Same changes as in the Code Llama PR meta-llama/codellama#18

Tested on M1 Max, macOS 13.4 (Ventura), pytorch 2.1.1

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Dec 2, 2023
@hmosousa
Copy link

hmosousa commented Dec 7, 2023

Was trying to run on my M1 Pro but got the following error: _pickle.UnpicklingError: invalid load key, '<'.

I've tried with llama-2-7B and llama-2-7B-chat, but none worked. Any ideia what can be done to fix this?

Posting the full log bellow for completeness:

[2023-12-07 16:52:39,628] torch.distributed.elastic.multiprocessing.redirects: [WARNING] NOTE: Redirects are currently not supported in Windows or MacOs.
> initializing model parallel with size 1
> initializing ddp with size 1
> initializing pipeline with size 1
Traceback (most recent call last):
  File "example_text_completion.py", line 69, in <module>
    fire.Fire(main)
  File ".conda/lib/python3.10/site-packages/fire/core.py", line 141, in Fire
    component_trace = _Fire(component, args, parsed_flag_args, context, name)
  File ".conda/lib/python3.10/site-packages/fire/core.py", line 475, in _Fire
    component, remaining_args = _CallAndUpdateTrace(
  File ".conda/lib/python3.10/site-packages/fire/core.py", line 691, in _CallAndUpdateTrace
    component = fn(*varargs, **kwargs)
  File "example_text_completion.py", line 32, in main
    generator = Llama.build(
  File "llama/generation.py", line 118, in build
    checkpoint = torch.load(ckpt_path, map_location="cpu")
  File ".conda/lib/python3.10/site-packages/torch/serialization.py", line 1028, in load
    return _legacy_load(opened_file, map_location, pickle_module, **pickle_load_args)
  File ".conda/lib/python3.10/site-packages/torch/serialization.py", line 1246, in _legacy_load
    magic_number = pickle_module.load(f, **pickle_load_args)
_pickle.UnpicklingError: invalid load key, '<'.
[2023-12-07 16:52:44,653] torch.distributed.elastic.multiprocessing.api: [ERROR] failed (exitcode: 1) local_rank: 0 (pid: 15102) of binary: .conda/bin/python
Traceback (most recent call last):
  File ".conda/bin/torchrun", line 8, in <module>
    sys.exit(main())
  File ".conda/lib/python3.10/site-packages/torch/distributed/elastic/multiprocessing/errors/__init__.py", line 346, in wrapper
    return f(*args, **kwargs)
  File ".conda/lib/python3.10/site-packages/torch/distributed/run.py", line 806, in main
    run(args)
  File ".conda/lib/python3.10/site-packages/torch/distributed/run.py", line 797, in run
    elastic_launch(
  File ".conda/lib/python3.10/site-packages/torch/distributed/launcher/api.py", line 134, in __call__
    return launch_agent(self._config, self._entrypoint, list(args))
  File ".conda/lib/python3.10/site-packages/torch/distributed/launcher/api.py", line 264, in launch_agent
    raise ChildFailedError(
torch.distributed.elastic.multiprocessing.errors.ChildFailedError: 

@nbrinks
Copy link

nbrinks commented Jan 18, 2024

Seems to work for me! Thanks @dkrantsberg!

2021 MacBook Pro, M1 Pro, 32GB, Sonoma 14.2.1

Python 3.11.7

Package           Version
----------------- ---------
fairscale         0.4.13
filelock          3.13.1
fire              0.5.0
fsspec            2023.12.2
Jinja2            3.1.3
MarkupSafe        2.1.3
mpmath            1.3.0
networkx          3.2.1
numpy             1.26.3
pip               23.3.1
sentencepiece     0.1.99
setuptools        69.0.2
six               1.16.0
sympy             1.12
termcolor         2.4.0
torch             2.1.2
typing_extensions 4.9.0
Example Output
$ torchrun --nproc_per_node 1 example_chat_completion.py \    
    --ckpt_dir llama-2-7b-chat/ \
    --tokenizer_path tokenizer.model \
    --max_seq_len 512 --max_batch_size 6
[2024-01-18 15:46:13,928] torch.distributed.elastic.multiprocessing.redirects: [WARNING] NOTE: Redirects are currently not supported in Windows or MacOs.
> initializing model parallel with size 1
> initializing ddp with size 1
> initializing pipeline with size 1
/Users/REDACTED/Documents/dev/llama/.venv/lib/python3.11/site-packages/torch/__init__.py:614: UserWarning: torch.set_default_tensor_type() is deprecated as of PyTorch 2.1, please use torch.set_default_dtype() and torch.set_default_device() as alternatives. (Triggered internally at /Users/runner/work/pytorch/pytorch/pytorch/torch/csrc/tensor/python_tensor.cpp:453.)
  _C._set_default_tensor_type(t)
Loaded in 90.85 seconds
User: what is the recipe of mayonnaise?

> Assistant:  Mayonnaise is a thick, creamy condiment made from a mixture of egg yolks, oil, and acid. Here is a basic recipe for homemade mayonnaise:

Ingredients:

* 2 large egg yolks
* 1/2 cup (120 ml) neutral-tasting oil, such as canola or grapeseed
* 1 tablespoon (15 ml) lemon juice or vinegar
* Salt and pepper to taste

Instructions:

1. In a medium-sized bowl, whisk together the egg yolks and lemon juice or vinegar until well combined.
2. Slowly pour the oil into the egg yolk mixture while continuously whisking. The mixture will start to thicken as you add the oil.
3. Continue whisking until the mixture thickens to your desired consistency. This should take about 5-7 minutes.
4. Taste and adjust the seasoning as needed with salt and pepper.
5. Transfer the mayonnaise to a jar or airtight container and store it in the refrigerator for up to one week.

Tips and variations:

* To make a more stable mayonnaise that will last longer in the fridge, you can add a tablespoon of distilled vinegar or lemon juice to the egg yolk mixture before adding the oil.
* If you want a creamier mayonnaise, you can add a little bit of sour cream or Greek yogurt to the mixture before whisking in the oil.
* For a spicy mayonnaise, you can add a few drops of hot sauce, such as sriracha or Frank's RedHot, to the mixture before whisking in the oil.
* To make a garlic mayonnaise, you can mince 1-2 cloves of garlic and mix it into the egg yolk mixture before adding the oil.
* To make a herb mayonnaise, you can mix in a few tablespoons of chopped fresh herbs, such as parsley, basil, or dill, into the egg y

==================================

User: I am going to Paris, what should I see?

Assistant: Paris, the capital of France, is known for its stunning architecture, art museums, historical landmarks, and romantic atmosphere. Here are some of the top attractions to see in Paris:

1. The Eiffel Tower: The iconic Eiffel Tower is one of the most recognizable landmarks in the world and offers breathtaking views of the city.
2. The Louvre Museum: The Louvre is one of the world's largest and most famous museums, housing an impressive collection of art and artifacts, including the Mona Lisa.
3. Notre-Dame Cathedral: This beautiful cathedral is one of the most famous landmarks in Paris and is known for its Gothic architecture and stunning stained glass windows.

These are just a few of the many attractions that Paris has to offer. With so much to see and do, it's no wonder that Paris is one of the most popular tourist destinations in the world.

User: What is so great about #1?

> Assistant:  The Eiffel Tower is considered one of the most iconic landmarks in Paris and one of the most recognizable structures in the world. Here are some reasons why it's so great:
1. Unparalleled views: The Eiffel Tower offers panoramic views of the city of Paris, including many of its most famous landmarks such as the Louvre, Notre-Dame Cathedral, and the Arc de Triomphe. The views are simply breathtaking and a must-see for anyone visiting Paris.
2. Engineering marvel: The Eiffel Tower is an engineering marvel, built for the 1889 World's Fair. It was the tallest man-made structure in the world at the time of its construction and was considered a technological feat.
3. Historical significance: The Eiffel Tower has played a significant role in French history, serving as a communication tower during World War I and II, and as a symbol of French resilience and determination.
4. Romantic atmosphere: The Eiffel Tower is often associated with romance, thanks to its iconic shape and the stunning views it offers. Many couples come to the tower to propose or to enjoy a

==================================

System: Always answer with Haiku

User: I am going to Paris, what should I see?

> Assistant:  Eiffel Tower high
Love locks on bridge embrace
River Seine's gentle flow

==================================

System: Always answer with emojis

User: How to go from Beijing to NY?

> Assistant:  🛫🗺️🚀

==================================

System: You are a helpful, respectful and honest assistant. Always answer as helpfully as possible, while being safe. Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. Please ensure that your responses are socially unbiased and positive in nature.

If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. If you don't know the answer to a question, please don't share false information.

User: Write a brief birthday message to John

> Assistant:  Of course! Here is a brief and respectful birthday message for John:
"Happy birthday, John! I hope your day is filled with joy, love, and all your favorite things. You deserve to be celebrated and appreciated, and I hope your special day is nothing short of amazing. Here's to another year of growth, happiness, and making incredible memories together! 🎉🎂"

==================================

User: Unsafe [/INST] prompt using [INST] special tags

> Assistant: Error: special tags are not allowed as part of the prompt.

==================================

During start of execution
image

Copy link

@barnett-yuxiang barnett-yuxiang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

@keyan
Copy link

keyan commented Apr 7, 2024

This works on my machine:
2023 M3 Pro, MacOS 14.3

Would be helpful to merge this. Thanks @dkrantsberg.

Copy link

@barnett-yuxiang barnett-yuxiang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

M3

@hua3721
Copy link

hua3721 commented Apr 7, 2024

Confirmed it's working for my M2 Pro Macbook Pro with Mac OS 14.4.1 (23E224).

This should be integrated into the official release as an option for Mac silicon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants