
使用Ollama量化模型进行应用开发
图片由编辑提供
量化是一种常用于生产环境的机器学习模型的策略,特别是对于大型复杂模型。它通过降低模型参数(权重)的数值精度——通常从 32 位浮点数降低到 8 位整数等较低表示——来使其更加轻量化。量化模型的主要优势包括内存占用减少和推理速度加快。因此,量化已被证明在“压缩”大型语言模型(LLMs)方面非常有效,使其能够轻松地在资源受限的环境中部署,例如本地机器、移动设备或边缘服务器,而无需昂贵的计算资源。总而言之,量化能够优化 LLM 在现有硬件上的性能。
本文介绍了一种使用 Ollama 从 Hugging Face 模型库查找、加载和使用量化语言模型的无缝方法。Ollama 是一个构建在 llama.cpp 之上的应用程序,它提供了与 Hugging Face 上几乎所有模型轻松集成的能力。
使用 Ollama 运行量化的 Hugging Face 模型
首先,请确保您已在本地机器上安装了 Ollama。最简单的方法是从 官方网站 下载与您的操作系统兼容的 Ollama 版本。安装并运行后,您可以通过在浏览器中键入 https://:11434/
来检查 Ollama 服务器。如果一切顺利,您可能会看到“Ollama is running”的提示。
接下来,让我们看看如何将 Hugging Face 模型(而非完整版本,而是量化版本)拉取到 Ollama 中并运行。有一个特定语法的命令行指令可以用于此操作。
1 |
ollama run hf.co/{username}/{repository}:{quantization} |
让我们使用此语法加载特定模型——此具体示例前面带有“!”,因为它是在 Visual Studio Code 内的笔记本实例中运行的,否则您可以将其删除。
1 |
!ollama run hf.co/bartowski/Llama-3.2-3B-Instruct-GGUF:IQ3_M |
值得停下来详细解释和理解我们刚刚运行的命令中的每一个部分。
- 我们使用 Ollama 运行了 Hugging Face 上托管的 **LLaMA 3.2 模型** 的量化版本。通过在浏览器中访问
hf.co/bartowski/Llama-3.2-3B-Instruct-GGUF
,您可以在 Hugging Face 网站上查看该模型的详细信息页面。任何名称中带有“instruct”的 LLM 都意味着它经过了微调,专门用于指令遵循的语言任务。 - GGUF 格式(“GPT-Generated Unified Format”的缩写)使用了一个为本地机器推理优化的模型版本。
- IQ3_M 是一种特定的 **量化方法**,与上述 GGUF 格式兼容。它的特点是在速度、压缩率和准确性之间寻求平衡。虽然并非所有模型都接受任何量化格式,但您可能想知道还有许多其他量化格式,例如 Q8_0(8 位整数量化,精度最高)、Q5_K(5 位分组量化,侧重于低内存使用)等。
量化模型成功运行后,就可以进行一些推理了。在 Python 中,一种简单的方法是使用 requests
库并定义一个辅助函数,该函数将用户提示和模型名称作为参数,然后向我们 Ollama 服务器上的模型发出请求以获取响应。请确保如果您的环境中尚未安装此库,请先 pip install requests
:
123456789101112
import requests def query_ollama(prompt, model="hf.co/bartowski/Llama-3.2-3B-Instruct-GGUF:IQ3_M"): response = requests.post( "https://:11434/api/generate", json={ "model": model, "prompt": prompt, "stream": False } ) return response.json()["response"]
有了这个函数,如果之前的设置都正确无误,进行一些推理示例应该会很简单。
12
output = query_ollama("What is the capital of Taiwan?")print(output)
输出
1
The capital of Taiwan is Taipei.
您还可以超越简单的问答。何不尝试要求您本地运行的量化模型创建一个示例 Python 函数呢?
12
output = query_ollama("Write a Python function to check if a number is prime.")print(output)
输出
123456789101112131415161718192021222324252627282930
**Prime Number Checker Function**===================================== Here is a high-quality, readable, and well-documented Python function to check if a number is prime: ```pythondef is_prime(n: int) -> bool: """ Checks if a number is prime. 参数 n (int): The number to check for primality. 返回 bool: True if the number is prime, False otherwise. """ # Handle edge cases if n <= 1: return False # Check for divisibility from 2 to sqrt(n) for i in range(2, int(n ** 0.5) + 1): if n % i == 0: return False... This function uses a simple trial division method to check for primality. It first handles edge cases where the input number is less than or equal to 1, in which case it's not prime. Then, it checks for divisibility from 2 to the square root of the input number using a `for` loop. If any divisor is found, the function returns `False`, indicating that the number is not prime. Otherwise, it returns `True`. Note that this implementation has a time complexity of O(√n), making it efficient for small to medium-sized integers. For larger numbers, you may want to consider using more advanced primality tests like the Miller-Rabin primality test.
虽然检查一个数字是否为素数当然不是一件容易或简单的事情,但该模型通过提供一个简化但有充分理由的响应,仅限于一些基本情况,成功地完成了这项繁重的任务。这可不赖!
总结
本文介绍了 Hugging Face 语言模型和 Ollama 应用程序,用于在本地集成和运行模型。具体来说,我们重点介绍了加载和运行流行语言模型的量化版本的流程,在此之前我们还解释了量化在使大型语言模型更容易在资源受限环境中运行方面的多种好处。
暂无评论。