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└── latest5 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:11bFROM /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 anyderivative 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 orcounterclaim in a lawsuit) alleging that the Llama Materials or Llama 3.2 outputs or results, or any portionof any of the foregoing, constitutes infringement of intellectual property or other rights owned or licensableby you, then any licenses granted to you under this Agreement shall terminate as of the date such litigation orclaim is filed or instituted. You will indemnify and hold harmless Meta from and against any claim by any thirdparty 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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/887221.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

阻塞IO 非阻塞IO

网络IO流程三次握手流程阻塞IO非阻塞IO阻塞IO和非阻塞IO的区别 阻塞IO:io 未就绪的情况下,会阻塞线程等待 非阻塞IO:io 未就绪的情况下,立即返回socket默认的情况是阻塞的

How Do Recommendation Models Amplify Popularity Bias? An Analysis from the Spectral Perspective

目录概符号说明Popularity bias\(\mathbf{q}_1\) 和 \(\mathbf{r}\) 具有高相似度相似度随着维度降低而增加相似度随着训练的变化ReSN: Regulartion with Spectral NormLin S., Gao C., Chen J., Zhou S., Hu B., Feng Y., Chen C. and Wang C. How do recommendation models a…

(文末有福利!)深度剖析大语言模型推理:指标、优化与框架选择

在人工智能飞速发展的当下,大语言模型(LLM)已然成为焦点。从智能聊天机器人到内容创作辅助,大语言模型的应用无处不在。但你是否了解其背后的推理过程,以及如何让这些模型运行得又快又好?今天,我们就来深入探讨大语言模型推理的奥秘。一、🌟大语言模型推理指标详解 在…

Docker之网络模型

Docker的网络模型类型 说明None 不为容器配置任何网络功能,没有网络 --net=noneContainer 与另一个运行中的容器共享Network Namespace,--net=container:containerID,k8s中Pod容器之间用此网络。Host 与主机共享Network Namespace,--net=hostBridge Docker设计的NAT网络模型…

初来园子

金易安装与使用说明 金易介绍 【金易】是交易外汇黄金的专项EA,只能用来交易黄金,做日内超短线的交易,是我们EA开发研究院研发多年的EA。金易由一位金融高校毕业有着超过十年外汇黄金实盘交易,黄金交易年入超十万美金的操盘高手指导完成,有着成熟的交易模型,也有完善的系…

太好了,IDE支持满血版DeepSeek了,我们有救了!

近期通义灵码能力再升级全新上线模型选择功能,目前已经支持 Qwen2.5、DeepSeek-V3 和 R1系列模型,用户可以在 VSCode 和 JetBrains 里搜索并下载最新通义灵码插件,在输入框里选择模型,即可轻松切换模型。你好呀,我是歪歪。 今年 1 月,通义灵码 AI 程序员全面上线,同时支…

2.14

今天在Java Web开发中深入学习了如何进行Web应用的部署,这是将我们开发的成果转化为可运行的服务的关键步骤。 首先,关于将项目打包成war文件(Web Application Archive),这不仅仅是简单的文件压缩。在打包过程中,要确保所有的依赖项都被正确地包含在内。例如,如果我们在…

可持续建筑设计的实践与挑战:筑梦绿色未来之路

在21世纪的建筑设计领域,可持续性不再只是理念的探讨,而是实践中的行动指南。在实际项目中推进可持续设计,既是机遇也是挑战。本文旨在深入剖析可持续建筑设计在实践中的具体应用及其面临的重重考验,共同探讨如何跨越障碍,迈向更加绿色的未来。 实践之光:可持续设计的亮点…

ubuntu18.04安装k8s和kubeSphere

swapoff -a # 临时禁用交换分区 sed -i / swap / s/^\(.*\)$/#\1/g /etc/fstab # 永久禁用 1.安装KubeKeyexport KKZONE=cn curl -sfL https://get-kk.kubesphere.io | VERSION=v3.0.7 bash - chmod +x kk2.安装包括socat、conntrack、ebtables、ipset 等依赖程序s…

建筑节能技术与材料的进展:迈向绿色建筑新时代

在应对全球气候变化和资源日益紧张的背景下,建筑节能已成为推动建筑业可持续发展的重要方向。本文将为您详细介绍当前最前沿的节能技术与材料,探索它们如何助力构建低碳环保的绿色建筑。 1. 智能玻璃与动态遮阳系统智能玻璃,如电致变色玻璃和热致变色玻璃,能够根据外部环境…

建筑与艺术的结合 —— 当空间遇见灵魂的诗篇

在历史的长河中,建筑与艺术如同两条并行又交织的河流,共同塑造着人类文明的风貌。本文将带您走进这一奇妙的领域,探讨建筑如何以其独特的语言,讲述故事,激发情感,以及如何在形式与功能的融合中绽放出艺术之光。 1. 建筑:凝固的音乐,立体的诗德国诗人歌德曾言:“建筑是…

java学习-5

异常 java把异常当作对象来处理,并定义一个基类java.lang.Throwable作为所有异常的超类 在Java API中已经定义了许多异常类,这些异常类分为两大类,错误Error和异常ExceptionError和Exception的区别:Error通常是灾难性的致命错误,是程序无法控制和处理的,当出现这些异常时…