第20章 Replicate:基于 Docker 容器运行与部署 AI 模型服务
Replicate Replicate 是一项商业服务,可以把用其 Cog 构建工具打包的模型上传上去,以 Docker 容器的方式运行。目前已有多个模型作为公开模型上传。对于公开模型,Replicate 只按实际使用的 GPU 时间收费;对于私有模型,则会额外收取启动和闲置时间的费用。已有若干为 LMQL/LMTP 封装好的模型公开发布,本章将介绍如何构建、运行和部署更多此类模型。在 Replicate 上运行 🤗 Transformers 模型 要在 Replicate 上运行 🤗 Transformers 模型,你需要:导出环境变量 REPLICATE_API_TOKEN,其值为用于请求鉴权的凭证。把模型的 transport= 参数设为 replicate:ORG/MODEL,与上传模型时使用的名称一致。把模型的 tokenizer= 参数设为一个 Hugging Face Transformers 名称,从中可以下载所用 tokenizer 的正确配置。例如:lmqlargmax """Review: We had a great stay. Hiking in the mountains was fabulous and the food is really good.\n Q: What is the underlying sentiment of this review and why?\n A:[ANALYSIS]\n Q: Summarizing the above analysis in a single word -- of the options "positive", "negative", and "neutral" -- how is the review best described?\n A:[CLASSIFICATION]""" from lmql.model( # 模型名实际不会被使用:endpoint 完全接管了模型选择 "meta-llama/Llama-2-13b-chat-hf", # 这里使用来自 https://replicate.com/charles-dyfis-net/llama-2-13b-hf--lmtp-8bit 的模型 endpoint="replicate:charles-dyfis-net/llama-2-13b-hf--lmtp-8bit", # 选择与 meta-llama/Llama-2-13b-hf 使用相同 tokenizer、但在 Hugging Face 上无需门控许可的模型 tokenizer="AyyYOO/Luna-AI-Llama2-Uncensored-FP16-sharded", ) where STOPS_AT(ANALYSIS, "\n") and len(TOKENS(ANALYSIS)) < 200 distribution CLASSIFICATION in [" positive", " negative", " neutral"] 把 🤗 模型上传到 Replicate 你也可以把自己的 LMQL 模型上传并部署到 Replicate。首先安装 Cog。此外,LMQL 还提供了大幅自动化构建和上传流程的脚本(参见 LMQL 源码中的 scripts/replicate-build 目录)。在 Replicate 网站上创建对应的模型。把 config.toml.example 复制为 config.toml 并进行自定义。修改 dest_prefix,把 YOURACCOUNT 替换为你实际用于上传模型的 Replicate 账户名。配置文件中应为每个要构建和上传的模型添加一个 [models.MODELNAME] 小节,且 MODELNAME 需与你在 Replicate 账户中创建的模型名称一致。huggingface.repo 应填写你想封装的 Hugging Face 模型名;如果需要固定版本,还需设置 huggingface.version。config 小节可用于设置任何你想传入 model_args 字典的值。在 scripts/replicate-build 目录下运行 ./build 脚本。这会为配置文件中定义的每个模型创建一个 work/ 子目录。进入 work/MODELNAME 目录,运行生成的 ./push 脚本来构建并上传模型,或运行 cog predict 在本地测试模型。