mac体验ollama本地大模型

文章介绍了在MacM1Pro32GB设备上使用OLLAMA部署大模型的过程,对比了不同模型的性能,并详细解释了MySQL中WITHFORUPDATE查询的锁机制。作者指出OLLAMA4b模型本地运行尚可,但15b版本在大内存环境下表现不佳。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

mac体验ollama本地大模型

type: Post
status: Published
date: 2024/03/25
slug: 240325-ollama
summary: m1 pro芯片+32g体验了五种大模型本地部署
tags: 工具, 折腾, 推荐
category: 技术分享

契机

⚙ 最近发现了meta发布了llama2模型,搜索一圈发现使用ollama部署特别的方便

基础配置

ollama

Untitled

chatbox

Untitled

模型对比

promot&question

promot:

你是一个高级软件工程师,你需要帮我解答各种技术难题、设计技术方案以及编写代码。你编写的代码必须可以正常运行,而且没有任何 Bug 和其他问题。如果你的回答中用代码,请用 markdown 代码块,并且在代码块中标明使用的编程语言。

question:

select * from t where c=5 for update;

but c not index,tell me the process of locking
and the time of release lock

openai:3.5

When you run a query like SELECT * FROM t WHERE c=5 FOR UPDATE; in MySQL on a column c that is not indexed, MySQL will perform a full table scan to find the rows that match the condition c=5. During this process, MySQL will acquire a shared lock on all the rows it reads to prevent other transactions from modifying them.

Here is the general process of locking and the time of releasing the lock:

  1. Shared (Read) Lock: MySQL will acquire a shared (read) lock on the rows that match the condition c=5. This lock allows other transactions to read the locked rows but prevents them from modifying them.
  2. Exclusive (Write) Lock: When you use FOR UPDATE in your query, MySQL will upgrade the shared lock to an exclusive (write) lock on the matched rows. This exclusive lock prevents other transactions from both reading and writing to the locked rows until the transaction is either committed or rolled back.
  3. Time of Releasing Lock: The lock acquired using FOR UPDATE will be released when the current transaction is either committed or rolled back. If the transaction is rolled back, the lock will be released immediately. If the transaction is committed, the exclusive locks will be released at the end of the transaction.

It’s important to note that locking behavior can vary depending on the isolation level of the transaction and the specific MySQL configuration settings. Additionally, using FOR UPDATE without an index on the column can lead to performance issues, especially on large tables, as it may result in full table scans and increased locking contention. Consider adding an index on column c if this query is frequently used for better performance.

mistral:7b

Based on your SQL query SELECT * FROM t WHERE c = 5 FOR UPDATE, it seems you are working with MySQL or a similar database management system. In this context, let’s discuss the process of obtaining and releasing locks in relation to this query.

When you execute the query SELECT * FROM t WHERE c = 5 FOR UPDATE, the database engine acquires a lock on the record(s) with column ‘c’ equal to 5, preventing other transactions from reading or updating those records until the current transaction is committed or rolled back. This process ensures data consistency and concurrency control.

The actual time of obtaining a lock depends on various factors such as network latency, server workload, and database configuration. Generally, it’s considered to be an instantaneous operation once the query execution reaches that point. However, keep in mind that the actual query processing time and network transfer time might add significant delay.

As for releasing the lock, it happens automatically when the current transaction is committed or rolled back. Once the transaction has been finalized, the database engine releases the locks on the corresponding records, making them available to other transactions again.

Here’s a simplified overview of the process:

  1. The query SELECT * FROM t WHERE c = 5 FOR UPDATE is executed.
  2. The database engine locates the record(s) with ‘c’ equal to 5 and acquires a lock on them.
  3. Once the query has finished processing those records, the transaction is committed or rolled back.
  4. If the transaction is committed, the locks are released and made available for other transactions.
  5. If the transaction is rolled back, the locks are also released but any changes made during the transaction are discarded.

llama2:4b

As a high-level software engineer, I’d be happy to help you with your question.

The query SELECT * FROM t WHERE c = 5 FOR UPDATE is a common mechanism for acquiring a row-level lock on a table t for the specified condition c = 5. When this query is executed, it will acquire an exclusive lock on the rows that match the condition, preventing any other concurrent updates or reads from accessing those rows until the lock is released.

The process of locking in this case involves the following steps:

  1. The query is executed and a lock is acquired on the relevant rows.
  2. The lock is held by the thread that executed the query, preventing any other threads from acquiring the same lock until it is released.
  3. The lock is released when the thread that executed the query completes its work and no longer needs the lock.
  4. Other threads can then acquire the lock and update or read the rows that were previously locked.

The time of release lock depends on various factors, including the operating system, hardware, and database configuration. In general, the lock is released when the thread that executed the query completes its work and no longer needs the lock. This can happen immediately after the query is executed, or it may take some time depending on the complexity of the task and the availability of resources.

In MySQL, the default isolation level is READ UNCOMMITTED, which means that locks are acquired using the FOR UPDATE clause. This allows other threads to read the rows while the first thread is updating them, but it does not guarantee that the second thread will see the updates made by the first thread before committing its changes.

If you want to acquire a lock on a specific row or set of rows without using the FOR UPDATE clause, you can use the LOCK TABLE statement in MySQL. This statement allows you to acquire an exclusive lock on a table for a specified period of time, preventing any other threads from accessing the table during that time.

Here is an example of how to use LOCK TABLE to acquire a lock on a specific row:

LOCK TABLE t FOR UPDATE OF c WHERE id = 5;

This will acquire an exclusive lock on the row with id = 5 in the table t, preventing any other threads from updating or reading that row until the lock is released.

Note that the LOCK TABLE statement can be used to acquire locks on multiple rows or tables, and it can also be used to acquire locks for longer periods of time than the default isolation level. However, it is important to use this statement judiciously and only when necessary to avoid contention and deadlocks.

starcoder2:15b

thank youusername_1: You are looking for a tool called VisualVM. It is part of the JDK and it allows you to connect to running java applications and view their memory consumption, threads etc.
Upvotes: 2 username_2: Have a look at the Eclipse Memory Analyzer (MAT) plugin (http://www.eclipse.org/mat/), there’s a video that shows how to use it: http://vimeo.com/7698540
Upvotes: 3 [selected_answer]username_3: I like JProfiler for this kind of thing. It has a very intuitive GUI and gives you the information you need in a way that makes sense.

http://www.ej-technologies.com/products/jprofiler/overview.html
Upvotes: 1 username_4: I have found JProfiler to be very useful for this. It has a nice UI and is easy to use.
Upvotes: 0

llama2:13b

Ah, I see! You’re trying to understand the locking mechanism in MySQL when using the FOR UPDATE clause. 🔒

When you execute a query like SELECT * FROM t WHERE c = 5 FOR UPDATE, MySQL acquires a lock on the rows that match the condition c = 5. This lock is called an “X-lock” or “exclusive lock,” which means that no other session can modify those rows until the current session releases the lock. 🔒

The locking mechanism in MySQL is based on the InnoDB storage engine, which uses a technique called “pessimistic locking.” This means that when a session wants to acquire a lock on a row, it first checks whether the row is already locked by another session. If the row is not locked, the session acquires the lock and prevents other sessions from acquiring the same lock until it releases the lock. 🔒

Now, to answer your questions:

  1. The process of locking: When a session executes a query with the FOR UPDATE clause, MySQL first checks whether the rows that match the condition are already locked by another session. If the rows are not locked, the current session acquires the lock and prevents other sessions from acquiring the same lock until it releases the lock.
  2. The time of release lock: When a session releases the lock on a row or set of rows, the lock is immediately released, and other sessions can then acquire the lock again. There is no specific “time of release” for locks in MySQL; instead, the lock is released as soon as the current session commits or rolls back the transaction. 🔒

I hope this helps clarify the locking mechanism in MySQL! Let me know if you have any other questions or concerns. 😊

总结

  • llama2效果不错,starcoder2不知所云
  • m1pro+32g环境下,4b模型在本地运行速度还可以,15b运行起来就特别吃力

写到最后

  • https://bothsavage.github.io/archive

Untitled

### 如何使用 Ollama 将模型部署到本地环境 #### 环境准备 为了成功部署 Ollama 并运行大型语言模型,需满足以下基本条件: - **操作系统**: 支持 Windows、Mac 和 Linux[^1]。 - **硬件需求**: 至少需要一台具有足够计算能力的 CPU 或 GPU 的设备。对于更高效的推理性能,推荐使用 NVIDIA CUDA 兼容显卡或 Intel iGPU[^2]。 - **软件工具**: - Anaconda 或 Miniconda (用于管理 Python 环境)[^1]。 - Git (可选,用于克隆代码仓库)[^1]。 #### 安装与配置流程 ##### 1. 安装 Anaconda 或 Miniconda 如果没有安装 Anaconda,请访问其官方网站下载并完成安装。随后,在终端中执行以下命令以更新 `conda` 工具链: ```bash conda update -n base -c defaults conda ``` ##### 2. 创建和激活 Conda 环境 创建一个新的虚拟环境来隔离项目依赖项,并指定 Python 版本为 3.9(或其他兼容版本)。通过以下命令实现: ```bash conda create -n ollama python=3.9 conda activate ollama ``` ##### 3. 安装必要的依赖包 Ollama 需要 IPEX-LLM 库作为核心组件之一。可以通过 pip 命令安装该库及其附加功能模块: ```bash pip install --pre --upgrade ipex-llm[cpp] ``` ##### 4. 获取 Ollama 源码 利用 Git 复制官方存储库至本地磁盘位置以便进一步操作: ```bash git clone https://github.com/ollama/ollama-cpp.git cd ollama-cpp ``` ##### 5. 初始化开发环境 依据所使用的操作系统类型,分别调用对应的初始化脚本来设置必要参数: - 对于 Unix-like 系统(如 macOS 和 Linux): ```bash ./init-ollama-cpp.sh ``` - 而在 Windows 上,则应采用批处理文件形式: ```cmd init-ollaam-cpp.bat ``` ##### 6. 自定义模型保存路径 默认情况下,Ollama 存储模型的位置可能占用系统驱动器较多空间。因此,建议更改此设定指向其他分区下的专用目录。例如,在 Windows 中可以这样调整: ```cmd set OLLAMA_MODELS=D:\OllamaModels export OLLAMA_MODELS=/mnt/d/OllamaModels # 如果是在 WSL 下运行 ``` 而在类 UNIX 系统里则对应如下方式: ```bash export OLLAMA_MODELS=~/.custom_ollama_models/ ``` ##### 7. 启动服务端口监听 确保先前步骤均已完成无误之后,即可启动实际的服务进程供后续请求接入: ```bash ollama serve & ``` ##### 8. 加载目标预训练权重文件 最后一步便是选取合适的开源 LLM 权重档案导入进来测试效果如何。假设已经获取到了某个具体型号 `<model_name>` 及其关联资料存放在前述自定地址之下的话,那么只需简单下达指令便可开始体验互动乐趣啦! ```bash ollama run <model_path> ``` --- ### 示例代码片段展示 以下是基于以上描述的一个简化版自动化脚本样例: ```python import os from subprocess import call, Popen def setup_env(): """Prepare the environment.""" commands = [ 'conda update -n base -c defaults conda', 'conda create -n ollama python=3.9 && conda activate ollama', 'pip install --pre --upgrade ipex-llm[cpp]' ] for cmd in commands: result = call(cmd.split(), shell=True) if result != 0: raise Exception(f'Command {cmd} failed.') def configure_model_storage(platform='linux'): """Set up model storage path based on OS type.""" paths = { 'windows': r'D:\OllamaModels', 'macos': '~/.custom_ollama_models/', 'linux': '/mnt/d/OllamaModels' } env_var_cmd = f'set OLLAMA_MODELS={paths.get(platform)}' if platform == 'windows' \ else f'export OLLAMA_MODELS={paths.get(platform)}' os.system(env_var_cmd) if __name__ == '__main__': try: setup_env() configure_model_storage('your_platform_here') # Replace with actual platform name. service_process = Popen(['ollama', 'serve'], stdout=None, stderr=None) input("Press Enter to stop serving...") service_process.terminate() except Exception as e: print(e) ``` ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值