人工智能 | MetaLlama大模型

llama 大模型介绍

我们介绍 LLaMA,这是一个基础语言模型的集合,参数范围从 7B 到 65B。我们在数万亿个Token上训练我们的模型,并表明可以专门使用公开可用的数据集来训练最先进的模型,而无需诉诸专有的和无法访问的数据集。特别是,LLaMA-13B 在大多数基准测试中都优于 GPT-3 (175B),

llama2 大模型介绍

我们开发并发布了 Llama 2,这是一组经过预训练和微调的大型语言模型 (LLM),其参数规模从 70 亿到 700 亿不等。我们经过微调的大语言模型(称为 Llama 2-Chat)针对对话用例进行了优化。我们的模型在我们测试的大多数基准上都优于开源聊天模型,并且根据我们对有用性和安全性的人工评估,可能是闭源模型的合适替代品

llama 大语言模型提供的主要模型列表

Code Llama 模型

Code Llama 是一个基于 Llama 2 的大型代码语言模型系列,在开放模型、填充功能、对大输入上下文的支持以及编程任务的零样本指令跟踪能力中提供最先进的性能。我们提供多种风格来覆盖广泛的应用程序:基础模型 (Code Llama)、Python 专业化 (Code Llama - Python) 和指令跟随模型 (Code Llama - Instruct),每个模型都有 7B、13B 和 34B 参数。所有模型均在 16k 个标记序列上进行训练,并在最多 100k 个标记的输入上显示出改进。7B 和 13B Code Llama 和 Code Llama - 指令变体支持基于周围内容的填充。Code Llama 是通过使用更高的代码采样对 Llama 2 进行微调而开发的。与 Llama 2 一样,我们对模型的微调版本应用了大量的安全缓解措施。有关模型训练、架构和参数、评估、负责任的人工智能和安全性的详细信息,请参阅我们的研究论文。Llama 材料(包括 Code Llama)的代码生成功能生成的输出可能受第三方许可的约束,包括但不限于开源许可。

Code Llama 提供的主要模型列表

申请模型

申请地址 https://ai.meta.com/resources/models-and-libraries/llama-downloads/

申请通过后,在 hugging face 上如果邮箱一致,会提示已经授权

使用模型

  • 使用官方的 Api
  • 使用第三方封装 Api llama.cpp-python ollama
  • 使用 langchain
  • 使用 hugging face 的 transformers

llama

https://github.com/facebookresearch/llama

torchrun --nproc_per_node 1 example_text_completion.py \
    --ckpt_dir llama-2-7b/ \
    --tokenizer_path tokenizer.model \
    --max_seq_len 128 --max_batch_size 4

NCCL 错误

RuntimeError: Distributed package doesn’t have NCCL built in

windows 和 mac 上基本跑不起来,因为 torchrun 依赖 NCCL

https://pytorch.org/docs/stable/distributed.html

llama.cpp

https://github.com/ggerganov/llama.cpp

Port of Facebook’s LLaMA model in C/C++

因为很多同学受限于个人电脑的环境,没法运行完整的 llama 模型。llama.cpp 提供了一个非常好的移植版本,可以降低电脑的硬件要求,方便个人电脑运行与测试。

下载

git clone https://github.com/ggerganov/llama.cpp.git
cd llama.cpp

make

模型转换

通过对模型进行转化,可以降低资源消耗。

# obtain the original LLaMA model weights and place them in ./models
ls ./models
65B 30B 13B 7B tokenizer_checklist.chk tokenizer.model
  # [Optional] for models using BPE tokenizers
  ls ./models
  65B 30B 13B 7B vocab.json

# install Python dependencies
python3 -m pip install -r requirements.txt

# convert the 7B model to ggml FP16 format
python3 convert.py models/7B/

  # [Optional] for models using BPE tokenizers
  python convert.py models/7B/ --vocabtype bpe

# quantize the model to 4-bits (using q4_0 method)
./quantize ./models/7B/ggml-model-f16.gguf ./models/7B/ggml-model-q4_0.gguf q4_0

# update the gguf filetype to current if older version is unsupported by another application
./quantize ./models/7B/ggml-model-q4_0.gguf ./models/7B/ggml-model-q4_0-v2.gguf COPY


# run the inference
./main -m ./models/7B/ggml-model-q4_0.gguf -n 128

此步可以省略,直接下载别人转换好的量化模型即可。

https://huggingface.co/TheBloke/Llama-2-7b-Chat-GGUF

运行

命令行交互模式

./main -m ./models/llama-2-7b.Q4_0.gguf -i   -n 256 --color

开启 server 模式,访问 http://127.0.0.1:8080/

./server -m ./models/llama-2-7b.Q4_0.gguf

llama-cpp-python

https://github.com/abetlen/llama-cpp-python

pip install llama-cpp-python

mac m1 上构建的时候需要加上特殊的参数

CMAKE_ARGS="-DLLAMA_METAL=on -DCMAKE_OSX_ARCHITECTURES=arm64" FORCE_CMAKE=1 pip install -U llama-cpp-python --no-cache-dir --force-reinstall

启动 Api 模式

pip install llama-cpp-python[server]
python  -m llama_cpp.server --model models/llama-2-7b.Q4_0.gguf
python  -m llama_cpp.server --model models/llama-2-7b.Q4_0.gguf --n_gpu_layers 1

访问 http://localhost:8000/docs 可以看到 api 的文档,与 openai 兼容。

ollama

  • 官网 https://ollama.ai/
  • github https://github.com/jmorganca/ollama
  • docker https://ollama.ai/blog/ollama-is-now-available-as-an-official-docker-image
(base) hogwarts: ~ seveniruby$ ollama serve codellama:7b
2023/10/08 02:31:04 images.go:987: total blobs: 6
2023/10/08 02:31:04 images.go:994: total unused blobs removed: 0
2023/10/08 02:31:04 routes.go:535: Listening on 127.0.0.1:11434

api 文档 https://github.com/jmorganca/ollama/blob/main/docs/api.md

基于 langchain 使用 llama

使用 langchain 调用

def test_llama_cpp_local():
    """
    使用本地模型
    :return:
    """
    llm = Llama(model_path="/Users/seveniruby/projects/llama.cpp/models/llama-2-7b.Q4_0.gguf")
    output = llm("Q: 法国的首都在哪里\n A: ", echo=True, max_tokens=6, temperature=0)
    debug(json.dumps(output, indent=2, ensure_ascii=False))

输出

{
  "id": "cmpl-6d3e491e-716f-4e6c-b167-4f52e3f9786f",
  "object": "text_completion",
  "created": 1696709780,
  "model": "/Users/seveniruby/projects/llama.cpp/models/llama-2-7b.Q4_0.gguf",
  "choices": [
    {
      "text": "Q: 法国的首都在哪里\n A: 巴黎。\n",
      "index": 0,
      "logprobs": null,
      "finish_reason": "length"
    }
  ],
  "usage": {
    "prompt_tokens": 18,
    "completion_tokens": 6,
    "total_tokens": 24
  }
}

使用 langchain 结合 api 服务

def test_langchain_llm():
    llm = OpenAI(
        openai_api_key=None,
        openai_api_base='http://127.0.0.1:8000/v1',
        stop=["Q:", "\n"]
    )

    debug(llm)
    prompt = "Q: 中国的首都在哪里?A: "
    output = llm(prompt)
    debug(output)

基于 langchain 与 hugging face

def test_pipeline():
    pipe = pipeline(
        "text-generation",
        model="meta-llama/Llama-2-7b-hf",
        torch_dtype=torch.float16,
        device='mps',  # 按需改成你的cuda或者cpu
        revision='main',
    )
    debug(pipe)

debug(pipe('法国的首都在哪里'))

在这里插入图片描述

推荐学习

【霍格沃兹测试开发】7天软件测试快速入门带你从零基础/转行/小白/就业/测试用例设计实战

【霍格沃兹测试开发】最新版!Web 自动化测试从入门到精通/ 电子商务产品实战/Selenium (上集)

【霍格沃兹测试开发】最新版!Web 自动化测试从入门到精通/ 电子商务产品实战/Selenium (下集)

【霍格沃兹测试开发】明星讲师精心打造最新Python 教程软件测试开发从业者必学(上集)

【霍格沃兹测试开发】明星讲师精心打造最新Python 教程软件测试开发从业者必学(下集)

【霍格沃兹测试开发】精品课合集/ 自动化测试/ 性能测试/ 精准测试/ 测试左移/ 测试右移/ 人工智能测试

【霍格沃兹测试开发】腾讯/ 百度/ 阿里/ 字节测试专家技术沙龙分享合集/ 精准化测试/ 流量回放/Diff

【霍格沃兹测试开发】Pytest 用例结构/ 编写规范 / 免费分享

【霍格沃兹测试开发】JMeter 实时性能监控平台/ 数据分析展示系统Grafana/Docker 安装

【霍格沃兹测试开发】接口自动化测试的场景有哪些?为什么要做接口自动化测试?如何一键生成测试报告?

【霍格沃兹测试开发】面试技巧指导/ 测试开发能力评级/1V1 模拟面试实战/ 冲刺年薪百万!

【霍格沃兹测试开发】腾讯软件测试能力评级标准/ 要评级表格的联系我

【霍格沃兹测试开发】Pytest 与Allure2 一键生成测试报告/ 测试用例断言/ 数据驱动/ 参数化

【霍格沃兹测试开发】App 功能测试实战快速入门/adb 常用命令/adb 压力测试

【霍格沃兹测试开发】阿里/ 百度/ 腾讯/ 滴滴/ 字节/ 一线大厂面试真题讲解,卷完拿高薪Offer !

【霍格沃兹测试开发】App自动化测试零基础快速入门/Appium/自动化用例录制/参数配置

【霍格沃兹测试开发】如何用Postman 做接口测试,从入门到实战/ 接口抓包(最新最全教程)

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值