Ollama模型迁移
技术背景
在前面的一些文章中,我们介绍过使用Ollama在Linux平台加载DeepSeek蒸馏模型,使用Ollama在Windows平台部署DeepSeek本地模型。除了使用Ollama与模型文件交互,还可以使用llama.cpp和KTransformer这样的。也有一些ChatBox、AnythingLLM和PageAssist这样的客户端工具推荐。
这里我们考虑到这样的一个使用场景,在我们使用ollama pull或者是从ModelScope、Hugging Face等平台下载好了一个本地模型之后,例如已经加载到Ollama里面,那么如何把这个模型导出到其他硬件里面去呢?
Ollama模型路径
如果没有去手动配置Ollama的Model路径,在Linux上默认的Ollama模型路径为:
/usr/share/ollama/.ollama/models/
是一个需要root权限的文件夹。在Windows平台上这个路径一般是:
C:\Users\user_name\.ollama\models
Ollama路径含义
在models这个路径下有两个子目录:
drwxr-xr-x 2 ollama ollama 4096 2月 20 14:47 blobs/
drwxr-xr-x 3 ollama ollama 4096 2月 20 14:53 manifests/
其中blobs里面存放的是一系列的模型文件,以sha256开头,后面跟一长串的哈希值:
$ ll blobs/
总用量 106751780
drwxr-xr-x 2 ollama ollama 4096 2月 20 14:47 ./
drwxr-xr-x 4 ollama ollama 4096 2月 5 12:24 ../
-rw-r--r-- 1 ollama ollama 412 2月 14 15:43 sha256-066df13e4debff21aeb0bb1ce8db63b9f19498d06b477ba3a4fa066adafd2949
-rwxr-xr-x 1 root root 7712 2月 20 14:46 sha256-0b4284c1f87029e67654c7953afa16279961632cf73dcfe33374c4c2f298fa35
-rwxr-xr-x 1 root root 5963057248 2月 20 14:46 sha256-11f274007f093fefeec994a5dbbb33d0733a4feb87f7ab66dcd7c1069fef0068
这些文件其实就是模型的GGUF文件,可以直接cp为gguf的后缀来使用。但关键的是有时候一个模型可能对应多个GGUF文件,所以需要一个索引文件。manifests目录存放的是每个模型的索引:
$ tree manifests/
manifests/
└── registry.ollama.ai
└── library
├── deepseek-r1
│ ├── 14b
│ ├── 32b
│ ├── 32b-q2k
│ ├── 32b-q40
│ └── 70b-q2k
├── llama3-vision
│ └── 11b
└── nomic-embed-text-v1.5
└── latest
5 directories, 7 files
这就是Ollama模型存储的一个基本结构。
查找对应模型文件
其实在上一个章节的这个索引文件里面就明文包含了模型文件的对照路径,例如打开一个llama3的索引文件:
{"schemaVersion":2,"mediaType":"application/vnd.docker.distribution.manifest.v2+json","config":{"mediaType":"application/vnd.docker.container.image.v1+json","digest":"sha256:fbd313562bb706ac00f1a18c0aad8398b3c22d5cd78c47ff6f7246c4c3438576","size":572},"layers":[{"mediaType":"application/vnd.ollama.image.model","digest":"sha256:11f274007f093fefeec994a5dbbb33d0733a4feb87f7ab66dcd7c1069fef0068","size":5963057248},{"mediaType":"application/vnd.ollama.image.projector","digest":"sha256:ece5e659647a20a5c28ab9eea1c12a1ad430bc0f2a27021d00ad103b3bf5206f","size":1938763584,"from":"/Users/ollama/.ollama/models/blobs/sha256-ece5e659647a20a5c28ab9eea1c12a1ad430bc0f2a27021d00ad103b3bf5206f"},{"mediaType":"application/vnd.ollama.image.template","digest":"sha256:715415638c9c4c0cb2b78783da041b97bd1205f8b9f9494bd7e5a850cb443602","size":269},{"mediaType":"application/vnd.ollama.image.license","digest":"sha256:0b4284c1f87029e67654c7953afa16279961632cf73dcfe33374c4c2f298fa35","size":7712},{"mediaType":"application/vnd.ollama.image.params","digest":"sha256:fefc914e46e6024467471837a48a24251db2c6f3f58395943da7bf9dc6f70fb6","size":32}]}
这里面带sha256的,都是模型所需要用到的gguf文件。相比于这种直接打开索引文件来检索的方法,Ollama其实还有一种更加优雅的查看模型存储地址的方法:
$ ollama show --modelfile llama3-vision:11b
# Modelfile generated by "ollama show"
# To build a new Modelfile based on this, replace FROM with:
# FROM llama3-vision:11b
FROM /usr/share/ollama/.ollama/models/blobs/sha256-11f274007f093fefeec994a5dbbb33d0733a4feb87f7ab66dcd7c1069fef0068
FROM /usr/share/ollama/.ollama/models/blobs/sha256-ece5e659647a20a5c28ab9eea1c12a1ad430bc0f2a27021d00ad103b3bf5206f
TEMPLATE """{{- range $index, $_ := .Messages }}<|start_header_id|>{{ .Role }}<|end_header_id|>
{{ .Content }}
{{- if gt (len (slice $.Messages $index)) 1 }}<|eot_id|>
{{- else if ne .Role "assistant" }}<|eot_id|><|start_header_id|>assistant<|end_header_id|>
{{ end }}
{{- end }}"""
PARAMETER temperature 0.6
PARAMETER top_p 0.9
LICENSE "LLAMA 3.2 COMMUNITY LICENSE AGREEMENT
Llama 3.2 Version Release Date: September 25, 2024
“Agreement” means the terms and conditions for use, reproduction, distribution
and modification of the Llama Materials set forth herein.
“Documentation” means the specifications, manuals and documentation accompanying Llama 3.2
distributed by Meta at https://llama.meta.com/doc/overview.
“Licensee” or “you” means you, or your employer or any other person or entity (if you are
entering into this Agreement on such person or entity’s behalf), of the age required under
applicable laws, rules or regulations to provide legal consent and that has legal authority
to bind your employer or such other person or entity if you are entering in this Agreement
on their behalf.
“Llama 3.2” means the foundational large language models and software and algorithms, including
machine-learning model code, trained model weights, inference-enabling code, training-enabling code,
fine-tuning enabling code and other elements of the foregoing distributed by Meta at
https://www.llama.com/llama-downloads.
“Llama Materials” means, collectively, Meta’s proprietary Llama 3.2 and Documentation (and
any portion thereof) made available under this Agreement.
“Meta” or “we” means Meta Platforms Ireland Limited (if you are located in or,
if you are an entity, your principal place of business is in the EEA or Switzerland)
and Meta Platforms, Inc. (if you are located outside of the EEA or Switzerland).
By clicking “I Accept” below or by using or distributing any portion or element of the Llama Materials,
you agree to be bound by this Agreement.
1. License Rights and Redistribution.
a. Grant of Rights. You are granted a non-exclusive, worldwide,
non-transferable and royalty-free limited license under Meta’s intellectual property or other rights
owned by Meta embodied in the Llama Materials to use, reproduce, distribute, copy, create derivative works
of, and make modifications to the Llama Materials.
b. Redistribution and Use.
i. If you distribute or make available the Llama Materials (or any derivative works thereof),
or a product or service (including another AI model) that contains any of them, you shall (A) provide
a copy of this Agreement with any such Llama Materials; and (B) prominently display “Built with Llama”
on a related website, user interface, blogpost, about page, or product documentation. If you use the
Llama Materials or any outputs or results of the Llama Materials to create, train, fine tune, or
otherwise improve an AI model, which is distributed or made available, you shall also include “Llama”
at the beginning of any such AI model name.
ii. If you receive Llama Materials, or any derivative works thereof, from a Licensee as part
of an integrated end user product, then Section 2 of this Agreement will not apply to you.
iii. You must retain in all copies of the Llama Materials that you distribute the
following attribution notice within a “Notice” text file distributed as a part of such copies:
“Llama 3.2 is licensed under the Llama 3.2 Community License, Copyright Meta Platforms,
Inc. All Rights Reserved.”
iv. Your use of the Llama Materials must comply with applicable laws and regulations
(including trade compliance laws and regulations) and adhere to the Acceptable Use Policy for
the Llama Materials (available at https://www.llama.com/llama3_2/use-policy), which is hereby
incorporated by reference into this Agreement.
2. Additional Commercial Terms. If, on the Llama 3.2 version release date, the monthly active users
of the products or services made available by or for Licensee, or Licensee’s affiliates,
is greater than 700 million monthly active users in the preceding calendar month, you must request
a license from Meta, which Meta may grant to you in its sole discretion, and you are not authorized to
exercise any of the rights under this Agreement unless or until Meta otherwise expressly grants you such rights.
3. Disclaimer of Warranty. UNLESS REQUIRED BY APPLICABLE LAW, THE LLAMA MATERIALS AND ANY OUTPUT AND
RESULTS THEREFROM ARE PROVIDED ON AN “AS IS” BASIS, WITHOUT WARRANTIES OF ANY KIND, AND META DISCLAIMS
ALL WARRANTIES OF ANY KIND, BOTH EXPRESS AND IMPLIED, INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES
OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. YOU ARE SOLELY RESPONSIBLE
FOR DETERMINING THE APPROPRIATENESS OF USING OR REDISTRIBUTING THE LLAMA MATERIALS AND ASSUME ANY RISKS ASSOCIATED
WITH YOUR USE OF THE LLAMA MATERIALS AND ANY OUTPUT AND RESULTS.
4. Limitation of Liability. IN NO EVENT WILL META OR ITS AFFILIATES BE LIABLE UNDER ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, TORT, NEGLIGENCE, PRODUCTS LIABILITY, OR OTHERWISE, ARISING OUT OF THIS AGREEMENT,
FOR ANY LOST PROFITS OR ANY INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL, EXEMPLARY OR PUNITIVE DAMAGES, EVEN
IF META OR ITS AFFILIATES HAVE BEEN ADVISED OF THE POSSIBILITY OF ANY OF THE FOREGOING.
5. Intellectual Property.
a. No trademark licenses are granted under this Agreement, and in connection with the Llama Materials,
neither Meta nor Licensee may use any name or mark owned by or associated with the other or any of its affiliates,
except as required for reasonable and customary use in describing and redistributing the Llama Materials or as
set forth in this Section 5(a). Meta hereby grants you a license to use “Llama” (the “Mark”) solely as required
to comply with the last sentence of Section 1.b.i. You will comply with Meta’s brand guidelines (currently accessible
at https://about.meta.com/brand/resources/meta/company-brand/). All goodwill arising out of your use of the Mark
will inure to the benefit of Meta.
b. Subject to Meta’s ownership of Llama Materials and derivatives made by or for Meta, with respect to any
derivative works and modifications of the Llama Materials that are made by you, as between you and Meta,
you are and will be the owner of such derivative works and modifications.
c. If you institute litigation or other proceedings against Meta or any entity (including a cross-claim or
counterclaim in a lawsuit) alleging that the Llama Materials or Llama 3.2 outputs or results, or any portion
of any of the foregoing, constitutes infringement of intellectual property or other rights owned or licensable
by you, then any licenses granted to you under this Agreement shall terminate as of the date such litigation or
claim is filed or instituted. You will indemnify and hold harmless Meta from and against any claim by any third
party arising out of or related to your use or distribution of the Llama Materials.
6. Term and Termination. The term of this Agreement will commence upon your acceptance of this Agreement or access
to the Llama Materials and will continue in full force and effect until terminated in accordance with the terms
and conditions herein. Meta may terminate this Agreement if you are in breach of any term or condition of this
Agreement. Upon termination of this Agreement, you shall delete and cease use of the Llama Materials. Sections 3,
4 and 7 shall survive the termination of this Agreement.
7. Governing Law and Jurisdiction. This Agreement will be governed and construed under the laws of the State of
California without regard to choice of law principles, and the UN Convention on Contracts for the International
Sale of Goods does not apply to this Agreement. The courts of California shall have exclusive jurisdiction of
any dispute arising out of this Agreement. "
使用这个show指令也能够查看到模型存储地址,除了模型地址之外,还有整个的LICENSE。但是这里我们也发现了一个非常重要的问题:通过ollama show列举出来的GGUF模型文件不全!!!。因此,还是建议直接打开manifests中的配置文件进行查看,再迁移相关的模型文件。
模型迁移和加载
在前面了解清楚Ollama的模型文件的存储结构之后,我们要迁移模型,思路就很清晰了:直接把模型文件和索引文件拷贝到相应的路径下就可以了。例如,把sha256的gguf文件拷贝目标设备的blobs路径下,再按照manifests的目录结构,把索引文件拷贝到manifests的路径下。其中如果是Linux操作系统,如下结构是固定的:
manifests/
└── registry.ollama.ai
└── library
然后在library下创建一个模型名称命名的目录,例如model-name/,然后把配置文件config拷贝到这个路径下,那么加载模型的时候需要使用的指令就是:
$ ollama run model-name:config
需要注意的是,刚把模型迁移过来的时候,直接使用PageAssist等工具可能找不到本地刚迁移过来的模型,需要先ollama run一次,才能够在模型列表里面找到:
$ ollama list
NAME ID SIZE MODIFIED
llama3-vision:11b 085a1fdae525 7.9 GB About an hour ago
到这里,Ollama的模型迁移就完成了。
总结概要
为了方便本地大模型部署和迁移,本文提供了一个关于Ollama的模型本地迁移的方法。由于直接从Ollama Hub下载下来的模型,或者是比较大的GGUF模型文件,往往会被切分成多个,而文件名在Ollama的路径中又被执行了sha256散列变换。因此我们需要从索引文件中获取相应的文件名,再进行模型本地迁移。
版权声明
本文首发链接为:https://www.cnblogs.com/dechinphy/p/ollama.html
作者ID:DechinPhy
更多原著文章:https://www.cnblogs.com/dechinphy/
请博主喝咖啡:https://www.cnblogs.com/dechinphy/gallery/image/379634.html
Ollama模型迁移的更多相关文章
- 使用 Azure PowerShell 将 IaaS 资源从经典部署模型迁移到 Azure Resource Manager
以下步骤演示了如何使用 Azure PowerShell 命令将基础结构即服务 (IaaS) 资源从经典部署模型迁移到 Azure Resource Manager 部署模型. 也可根据需要通过 Az ...
- 规划将 IaaS 资源从经典部署模型迁移到 Azure Resource Manager
尽管 Azure 资源管理器提供了许多精彩功能,但请务必计划迁移,以确保一切顺利进行. 花时间进行规划可确保执行迁移活动时不会遇到问题. Note 以下指导的主要参与者为 Azure 客户顾问团队,以 ...
- 有关从经典部署模型迁移到 Azure Resource Manager 部署模型的常见问题
此迁移计划是否影响 Azure 虚拟机上运行的任何现有服务或应用程序? 不可以. VM(经典)是公开上市的完全受支持的服务. 你可以继续使用这些资源来拓展你在 Azure 上的足迹. 如果我近期不打算 ...
- 使用 Azure CLI 将 IaaS 资源从经典部署模型迁移到 Azure Resource Manager 部署模型
以下步骤演示如何使用 Azure 命令行接口 (CLI) 命令将基础结构即服务 (IaaS) 资源从经典部署模型迁移到 Azure Resource Manager 部署模型. 本文中的操作需要 Az ...
- 用tensorlayer导入Slim模型迁移学习
上一篇博客[用tensorflow迁移学习猫狗分类]笔者讲到用tensorlayer的[VGG16模型]迁移学习图像分类,那麽问题来了,tensorlayer没提供的模型怎么办呢?别担心,tensor ...
- 如何将同一云服务下的虚拟机从经典部署模型迁移到 Azure Resource Manager
适用场景 用户希望将特定云服务下的所有虚拟机从经典部署模型(以下简称:ASM)迁移到 Azure Resource Manager(以下简称:ARM). Note 如果云服务下使用 VNET 也希望将 ...
- 如何将同一 VNET 下的虚拟机从经典部署模型迁移到 Azure Resource Manager
本文内容 适用场景 解决方案 适用场景 用户拥有多个云服务但是在同一个 VNET 下,希望将这些虚拟机从经典部署模型(以下简称:ASM)迁移到 Azure Resource Manager(以下简称: ...
- 基于深度学习和迁移学习的识花实践——利用 VGG16 的深度网络结构中的五轮卷积网络层和池化层,对每张图片得到一个 4096 维的特征向量,然后我们直接用这个特征向量替代原来的图片,再加若干层全连接的神经网络,对花朵数据集进行训练(属于模型迁移)
基于深度学习和迁移学习的识花实践(转) 深度学习是人工智能领域近年来最火热的话题之一,但是对于个人来说,以往想要玩转深度学习除了要具备高超的编程技巧,还需要有海量的数据和强劲的硬件.不过 Tens ...
- 【Python-Django模型迁移】用户数据库模型的迁移(对其他数据库迁移同样适用)!!!
迁移用户模型类 1. 指定用户模型类 文档 思考:为什么Django默认用户模型类是User? 阅读源代码:'django.conf.global_settings’ AUTH_USER_MODEL ...
- Django模型迁移提示版本不匹配解决办法
Django迁移模型时提示django.core.exceptions.ImproperlyConfigured:mysqlclient 1.3.7 or newer is required; you ...
随机推荐
- Java根据前端返回的字段名进行查询数据
在Java后端开发中,我们经常需要根据前端传递的参数(如字段名)来动态查询数据库中的数据.这种需求通常出现在需要实现通用查询功能或者复杂查询接口的场景中.为了实现这个功能,我们需要结合Java的反射机 ...
- 禁用SAP Hana错误密码锁定用户功能
背景 公司项目适配多种数据库其中包含SAP Hana,由于有同事的数据库连接工具保存了某个在用的数据库的旧密码,导致时不时会被锁用户.通过查询官方文档已解决,这里统一记录一下. 禁用密码锁定方法 以下 ...
- uni-app上下级页面数据双向通信
前情 最近在做小程序项目,选用是当前比较火的uniapp技术栈,经常会遇到页面间消息传递的需求. 为什么要这么做? uniapp页面间数据通信方式有很多:通过url传参,状态管理库vuex/pinia ...
- IO介绍-中
系统接口 块设备接口 块设备:数据的存取和传输都是以数据块为单位的设备.典型的块设备是磁盘.该设备的基本特征是传输速率高,另一特征是可寻址,即能指定数据的输入源地址及输出的目标地址,可随机读写.磁盘设 ...
- VLC web(http)控制 (1) 设置与登录
VLC3.0 web控制设置步骤: 1.打开偏好设置并显示全部: 2.进入主界面选项,选中Web: 3.进入Lua中设置HTTP密码: 重启软件生效. 这时就可以通过http://127. ...
- 小程序,用户授权手机号,node需要检验和解析
1. 第一步需要先在小程序api文档中下载对应语言的解密算法,解压之后就可以看到 https://developers.weixin.qq.com/miniprogram/dev/framework/ ...
- 所有 HTML attribute - prop 对照表
attr global tags prop aria-activedescendant true all aria-atomic true all aria-autocomplete true ...
- 【Javaweb】在项目中添加MyBatis依赖等
pom.xml 仓库 如果你没有配置阿里云仓库镜像源,可以到这里来找 https://mvnrepository.com/ 如果你配置了阿里云仓库镜像源,可以来这里找 https://develope ...
- javax.management.InstanceNotFoundException: org.springframework.boot:type=Admin,name=SpringApplicati
把这两个勾选去掉
- git Already up-to-date解决办法,强制覆盖本地代码
1.拉取最新云端代码强制覆盖本地代码 git fetch --all git reset --hard origin/master git pull 2.git将分支合并到主master,出现这个结果 ...