<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by GusLovesMath on Medium]]></title>
        <description><![CDATA[Stories by GusLovesMath on Medium]]></description>
        <link>https://medium.com/@guslovesmath?source=rss-dbab5ab3abb8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*eHYAvVyLc-lSxOEBCtVs_A.png</url>
            <title>Stories by GusLovesMath on Medium</title>
            <link>https://medium.com/@guslovesmath?source=rss-dbab5ab3abb8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 08:51:46 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@guslovesmath/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Efficiently Running Meta-Llama-3 on Mac Silicon (M1, M2, M3)]]></title>
            <link>https://guslovesmath.medium.com/efficiently-running-meta-llama-3-on-mac-silicon-m1-m2-m3-61585c9bc741?source=rss-dbab5ab3abb8------2</link>
            <guid isPermaLink="false">https://medium.com/p/61585c9bc741</guid>
            <category><![CDATA[large-language-models]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[apple]]></category>
            <category><![CDATA[llama-3]]></category>
            <category><![CDATA[mac]]></category>
            <dc:creator><![CDATA[GusLovesMath]]></dc:creator>
            <pubDate>Fri, 03 May 2024 22:27:06 GMT</pubDate>
            <atom:updated>2024-08-29T16:18:50.341Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KupPVa3P1tC1NAifMyNkEg.jpeg" /></figure><p><strong><em>Link to Jupyter Notebook: </em></strong><a href="https://github.com/GusGitMath/Llama3_MacSilicon"><strong><em>GitHub</em></strong></a><strong><em> page</em></strong></p><p><em>Training LLMs locally on Apple silicon: </em><a href="https://github.com/GusGitMath/Local_LLM_Training_Apple_Silicon"><strong><em>GitHub</em></strong></a><strong><em> page</em></strong>.</p><h3>Introduction</h3><p>In the rapidly advancing field of artificial intelligence, the Meta-Llama-3 model stands out for its versatility and robust performance, making it ideally suited for Apple’s innovative silicon architecture. The use of the <strong>MLX framework</strong>, optimized specifically for Apple’s hardware, enhances the model’s capabilities, offering developers an efficient tool to leverage machine learning on Mac devices. <strong>This tutorial not only guides you through running Meta-Llama-3 but also introduces methods to utilize other powerful applications like OpenELM, Gemma, and Mistral</strong>.</p><h3><strong>Getting Started</strong></h3><p>To get started with running Meta-Llama-3 on your Mac silicon device, ensure you&#39;re using a MacBook with an M1, M2, or M3 chip. You <strong>should</strong> set up a Python virtual environment. Then run the following commands in your terminal or in Jupyter Notebook to install the required packages:</p><pre>pip install — upgrade ipywidgets<br>pip install torch<br>pip install mlx-lm</pre><p>These commands will equip your environment with the tools needed to utilize the Meta-Llama-3 model as well as other LLMs like Gemma.</p><h4>Section 1: Loading the Meta-Llama-3 Model</h4><p>Here we will load the Meta-Llama-3 model using the MLX framework, which is tailored for Apple’s silicon architecture. MLX enhances performance and efficiency on Mac devices. Here is how you can load the model:</p><pre>from mlx_lm import load<br><br># Define your model to import<br>model_name = &quot;mlx-community/Meta-Llama-3-8B-Instruct-4bit&quot;<br><br># Loading model<br>model, tokenizer = load(model_name)</pre><p>In this code snippet, the load function is designed to get and prepare both the model and its tokenizer from the MLX community repository hosted on Hugging Face. The specified model, mlx-community/Meta-Llama-3–8B-Instruct-4bit, is a 4-bit quantized version, which means it uses less memory and computes faster while maintaining adequate accuracy.</p><p>The<strong> </strong><a href="https://huggingface.co/mlx-community"><strong>MLX Community on Hugging Face</strong></a><strong>, </strong>hosts a variety of models optimized for Mac silicon, including models like Gemma, OpenELM, and Mistral, each designed for specific applications ranging from natural language understanding to more complex inference tasks. To explore these models, you can copy the name from there and test it in your setup.</p><h4>Section 2: Generating Basic Responses</h4><p>Once the Meta-Llama-3 model is loaded, you can generate responses using the generate function. Here’s a simple example to get you started:</p><pre># Generate a response from the model<br>response = generate(model, tokenizer, prompt=&quot;hello&quot;)<br><br># Output the response<br>print(response)</pre><p>Using the prompt “hello” with the Meta-Llama-3 model and parameters we used above, you should get a strange story about Sarah in a 1950s U.S. town, exploring identity themes.</p><h4>Section 3: Advanced Example — Solving Mathematical Problems</h4><p>To showcase a more complex application of the Meta-Llama-3 model, let’s explore how it can solve a mathematical problem involving calculus. This example will involve setting a specific user scenario and processing complex user input through the model. Below is the complete code to manage this task:</p><pre># Define the role of the chatbot<br>chatbot_role = &quot;You are a math professor chatbot. You answer with clear step by step solutions.&quot;<br><br># Define a mathematical problem<br>math_question = &quot;Please compute the integral of f(x) = exp(-x^2) over the x domain of (- infinity, infinity), use LaTeX.&quot;<br><br># Set up the chat scenario with roles<br>messages = [<br>    {&quot;role&quot;: &quot;system&quot;, &quot;content&quot;: chatbot_role},<br>    {&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: math_question}<br>]<br><br># Apply the chat template to format the input for the model<br>input_ids = tokenizer.apply_chat_template(messages, add_generation_prompt=True)<br><br># Decode the tokenized input back to text format to be used as a prompt for the model<br>prompt = tokenizer.decode(input_ids)<br><br># Generate a response using the model<br>response = generate(model, tokenizer, max_tokens=512, prompt=prompt)</pre><p><strong>Process walkthrough:</strong></p><ul><li><strong>1. Scenario Configuration:</strong> The messages variable simulates a chat where the model adopts a math professor’s role (system<em>,</em> content) to guide its response style, while user<em>,</em> content presents the actual query (math_question<em>)</em>.</li><li><strong>2. Tokenization and Template Application: </strong>Using tokenizer.apply_chat_template, we format the roles and contents into a structure that the model can interpret.</li><li><strong>3. Prompt Preparation:</strong> The formatted input is then decoded back to text (prompt), setting the stage for model interaction.</li><li><strong>4. Response Generation:</strong> The generate<em> </em>function processes the prompt to produce a detailed response, controlled by max_tokens.</li></ul><p><strong>Output:</strong></p><pre>from IPython.display import Markdown<br><br># Printing models response using Markdown cell formatting<br>Markdown(response)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6AYo7VtuFkrLRnVzbcuFTw.png" /><figcaption>The output of Llama3’s response, formatted in LaTeX as our system request.</figcaption></figure><h4>Conclusion</h4><p>This tutorial showcased the capabilities of the Meta-Llama-3 model using Apple’s silicon chips and the MLX framework, demonstrating how to handle tasks from basic interactions to complex mathematical problems efficiently. Explore the <a href="https://huggingface.co/mlx-community"><strong>MLX Community</strong></a><strong> </strong>hub, experiment with different models and settings, and leverage the power of this setup to enhance your machine learning projects on Mac devices.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=61585c9bc741" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>