如何在DigitalOcean的H100 GPU服务器上运行DeepSeek R1 模型
在 DigitalOcean,我们一直在关注开源大语言模型(LLMs)和商业封闭模型之间差距的不断缩小。其中一个最关键的能力就是“推理”,也就是用合乎逻辑、讲得通的方式思考问题。
以前,大语言模型的表现比较单一。只要给它们一个提示,它们就会直接给出答案,根本没有什么“二次思考”的过程,也没有什么机制能让模型在出错时自己纠正。这就让它们在遇到那些指令本身就可能有问题的情况时,很难进行深入推理、提出疑问或做出调整。比如说,对于推理能力较弱的模型来说,如果遇到复杂的语言数学题,没有用户提供明确的指示和操作,它们可能根本解决不了。
现在,全新一代的推理型大语言模型来了。以 OpenAI 的 O1 模型系列为开端,这些推理模型一经问世,就在社区内引起了轰动,因为它们在多种逻辑任务上(比如编程、数学,甚至科学推理)基本上缩小了人类与机器学习之间的差距。
跟之前每一次技术进步一样,开源社区也在努力追赶封闭模型的步伐。最近,第一个达到这种抽象推理水平的开源模型——Deepseek R1 系列大语言模型,已经向大家发布了。
在这篇两部分系列文章的第一部分中,我们将展示如何在 DigitalOcean 的 GPU Droplets 上,通过 Ollama 来运行这些模型。你会学到如何设置 GPU Droplet、安装 Ollama,以及如何开始用 Deepseek R1 进行推理。
先决条件
- DigitalOcean 账号:本教程将使用 DigitalOcean 的 GPU Droplets,提供包括NVIDIA H100(2.5美元/小时)和NVIDIA H200 等型号的GPU服务器。注册,可访问digitalocean.com,目前DigitalOcean也支持通过支付宝付费。
- Bash 命令行基础:我们会用终端来访问、下载和使用 Ollama,相关命令都会提供。
设置 GPU Droplet
第一步就是配置好你的机器。按照官方 DigitalOcean 文档的指引,新建一个 GPU Droplet。(其实非常简单,你只需要登录账号,进入后台,在左侧标签栏里就能找到“GPU Droplet”的选项。
我们建议在这个项目中选择“AI/ML Ready”操作系统,并使用一块 NVIDIA H100 GPU,除非你打算运行最大的 671B 参数的DeepSeek模型。
当你的机器启动完毕后,就可以进入下一步了。
安装 Ollama 和 DeepSeek R1
在本次演示中,我们会利用 Ollama 开发者的出色工作,快速将我们的模型上线。点击 GPU Droplet 详情页面右上角的按钮,打开一个 web 控制台窗口,然后切换到你想要工作的目录。
接着,在终端里粘贴下面这条命令:
curl -fsSL https://ollama.com/install.sh | sh
这条命令会在你的机器上安装 Ollama。安装过程可能需要几分钟,一旦完成,就一切就绪了!是不是很简单?
接下来,我们只需要执行一条命令,让 DeepSeek R1 模型在你的机器上运行。Ollama 提供了多种模型尺寸(阅读博客文章了解1.5b、7b、8b、14b、32b、70b 和 671b 不同版本的部署配置要求),这里我们推荐在单个H100 GPU 服务器上运行最大的可用版本是70b 的模型:
ollama run deepseek-r1:70b
第一次运行时,系统会花几分钟时间下载大约 40GB 的模型并加载它,然后你就能看到一个交互窗口了,可以和这个目前最强的开源推理模型“聊天”了。
使用 DeepSeek R1 进行推理
正如文章开头提到的,DeepSeek R1 能够处理数学、编程,甚至科学推理等复杂任务。那么,什么是推理模型呢?简单来说,它是一整套指导 AI 系统如何推理、做决策和解决问题的框架,或者说核心。它提供了让 AI 能够从现有信息中推断出结论、生成解决方案所需的算法、技术和启发式方法。它通过利用模型的迭代特性和元逻辑推理,更好地理解并推断出问题的答案。
接下来,让我们试试一个数学题,看看这个模型是怎么推理出答案的:
为了方便大家理解,题目翻译为中文是这样的:如果一个包裹的重量超过11磅8盎司,那么包裹上会贴有“heavy”的标签。Gloria为她的客户打包了6个花盆,每个花盆的重量为1磅12盎司,而包装材料重5盎司。那么,她的包裹会被标记为“heavy”吗?
## input
Answer this question: A package that is heavier than 11 lbs and 8 oz will have a label that says “heavy” on it. Gloria packed 6 flowerpots to send to her customers. Each of the flowerpots weighs 1 lb and 12 oz. The packing material weighs 5 oz. Will her package be labeled as “heavy”?
## output
<think>
First, I need to determine the total weight of Gloria's package to see if it exceeds the threshold for
being labeled as "heavy."
Gloria has packed 6 flowerpots, each weighing 1 pound and 12 ounces. Additionally, there is packing
material that weighs 5 ounces.
I'll start by calculating the total weight of all the flowerpots. Since each pot is 1 lb 12 oz, multiplying
this by 6 gives:
6 pots × (1 lb + 12 oz) = 6 lbs and 72 oz.
Next, I need to convert the 72 ounces into pounds because the threshold is given in pounds and ounces.
Knowing that 16 ounces make a pound, I divide 72 by 16:
72 oz ÷ 16 oz/lb = 4.5 lb.
So, the flowerpots weigh a total of 6 lbs + 4.5 lbs = 10.5 lbs.
Now, adding the weight of the packing material, which is 5 ounces. To maintain consistent units, I convert
5 ounces to pounds:
5 oz ÷ 16 oz/lb ≈ 0.3125 lb.
Adding this to the flowerpots' total weight:
10.5 lbs + 0.3125 lbs = 10.8125 lbs.
Finally, comparing the total package weight of approximately 10.81 lbs to the 11 lbs and 8 oz threshold,
which is equivalent to 11.5 lbs (since 8 oz = 0.5 lb). Since 10.81 lbs is less than 11.5 lbs, the package
will not be labeled as "heavy."
</think>
**Solution:**
To determine if Gloria's package will be labeled as “heavy,” we need to calculate its total weight and
compare it to the threshold of **11 lbs and 8 oz** (which is equivalent to **11.5 lbs**).
1. **Calculate the Weight of the Flowerpots:**
- Each flowerpot weighs **1 lb and 12 oz**.
- Convert **12 oz** to pounds:
\[
12\ \text{oz} = \frac{12}{16}\ \text{lbs} = 0.75\ \text{lbs}
\]
- So, each flowerpot weighs **1.75 lbs**.
- Gloria packed **6 flowerpots**, so:
\[
6 \times 1.75\ \text{lbs} = 10.5\ \text{lbs}
\]
2. **Add the Weight of Packing Material:**
- The packing material weighs **5 oz**.
- Convert **5 oz** to pounds:
\[
5\ \text{oz} = \frac{5}{16}\ \text{lbs} \approx 0.3125\ \text{lbs}
\]
- Total weight of the package:
\[
10.5\ \text{lbs} + 0.3125\ \text{lbs} = 10.8125\ \text{lbs}
\]
3. **Compare with the Threshold:**
- The threshold for a “heavy” label is **11 lbs and 8 oz**, which is **11.5 lbs**.
- Since **10.8125 lbs < 11.5 lbs**, the package does **not** meet the criteria for being labeled as
“heavy.”
**Final Answer:**
\[
\boxed{\text{No}}
\]
仔细看看这个模型是怎么推导出这个问题答案的:它先确定了关键——算出花盆和包装材料的总重量;然后把题目里的信息复述了一遍,并展示了如何分别计算花盆和包装材料的总重;接着把这些数值相加,再对比两个结果,从而得出了正确答案:不。
现在,我们来看看一个编程例子,我们问的问题是:如何编写一个 Python 函数,把 wav 文件转换成 mp3 文件?
## input
How can i write a Python function that converts wav files to mp3s?
## output
Okay, so I want to create a Python function that converts WAV files to MP3s. Hmm, where do I start? Well, I
know that WAV and MP3 are both audio formats, but they're different in terms of compression and quality.
WAV is uncompressed, which makes the files larger, while MP3 is compressed, making the files smaller but
with some loss in quality.
First, I need to figure out how to read a WAV file in Python. I've heard about libraries like `wave` or
`soundfile`, but I'm not sure if they can help me convert formats. Maybe those are more for manipulating
audio data rather than converting formats.
Then there's the conversion part. How do I actually convert a WAV file into an MP3? It seems like I might
need some external tool or library that handles the encoding. Oh, right! I think `ffmpeg` is a powerful
tool for audio and video processing. Maybe I can use it within Python.
So, perhaps I can write a function that uses `ffmpeg` to convert the WAV file to MP3. But how do I
interface with `ffmpeg` from Python? I recall there's a library called `subprocess` that allows you to run
shell commands from Python. That could work.
Wait, but is there a more direct way without using shell commands? Maybe there are pure Python libraries
for converting audio formats. I did some searching and found `pydub`. It seems like `pydub` can handle
format conversions and uses `ffmpeg` under the hood. That might be easier to use than dealing with
`subprocess` directly.
Alright, so I'll go with `pydub`. First, I need to install it using pip:
pip install pydub
But wait, does `pydub` require `ffmpeg` to be installed on my system? Yes, it does. So I need to make sure
that `ffmpeg` is installed and accessible from the command line.
Once that's set up, I can write a function that takes a WAV file path as input and outputs an MP3 file. Let
me sketch this out:
1. Import the necessary modules.
2. Define a function called `convert_wav_to_mp3`.
3. Use `pydub.AudioSegment` to read the WAV file.
4. Export the audio segment to MP3 format.
But I should also handle exceptions, like if the input file doesn't exist or if there's an error during
conversion. Maybe add some try-except blocks for better error handling.
Let me think about potential issues:
- What if the output path already exists? Should I overwrite it or raise an error?
- How do I handle different bitrates or quality settings in MP3?
- Are there any specific parameters I need to set for the conversion?
For now, I'll keep it simple and focus on a basic conversion. I can always add more features later.
So, putting it all together, here's what my function might look like:
from pydub import AudioSegment
def convert_wav_to_mp3(wav_file_path, mp3_file_path):
try:
# Load the WAV file
sound = AudioSegment.from_wav(wav_file_path)
# Export to MP3
sound.export(mp3_file_path, format="mp3")
print("Conversion successful!")
except Exception as e:
print(f"An error occurred: {e}")
I should test this function with a sample WAV file to see if it works. If it does, great! I can then think
about adding more features like handling multiple files or different quality settings.
Wait, but what if the user wants to convert all WAV files in a directory? Maybe I can modify the function
to accept a list of file paths or even search for WAV files recursively. That would make the function more
versatile.
Also, considering performance, converting large numbers of files might take some time. Maybe adding
progress indicators or parallel processing could improve the user experience, but that's probably beyond
the scope of this initial implementation.
In summary, to write a Python function that converts WAV files to MP3s:
1. Use `pydub` as it simplifies the conversion process using `ffmpeg`.
2. Ensure `ffmpeg` is installed and accessible.
3. Write a function that imports the WAV file and exports it in MP3 format.
4. Add error handling for potential issues like missing files or conversion errors.
I think this approach should work well for my needs, but I'll need to test it thoroughly to make sure there
are no unexpected issues.
</think>
To convert WAV files to MP3s using Python, you can utilize the `pydub` library, which simplifies audio
format conversions by leveraging `ffmpeg`. Below is a step-by-step guide and code implementation:
**Step 1: Install Required Libraries**
First, install `pydub` using pip. Note that `pydub` requires `ffmpeg` to be installed on your system.
pip install pydub
Ensure `ffmpeg` is accessible from the command line.
**Step 2: Implement the Conversion Function**
Here's a Python function that converts a WAV file to MP3:
from pydub import AudioSegment
def convert_wav_to_mp3(wav_file_path, mp3_file_path):
#Converts a WAV file to an MP3 file.
#Args:
#wav_file_path (str): Path to the input WAV file.
#mp3_file_path (str): Desired path for the output MP3 file.
#"""
try:
# Load the WAV file
sound = AudioSegment.from_wav(wav_file_path)
# Export to MP3 format
sound.export(mp3_file_path, format="mp3")
print("Conversion successful!")
except Exception as e:
print(f"An error occurred during conversion: {e}")
**Step 3: Example Usage**
To use the function:
# Example usage
convert_wav_to_mp3("input.wav", "output.mp3")
This will convert `input.wav` to `output.mp3`.
**Additional Considerations:**
1. **Error Handling**: The function includes basic error handling. Depending on your needs, you can extend
it by handling specific exceptions.
2. **Multiple Files**: To process multiple WAV files, modify the function to accept a list of file paths or
iterate through a directory.
3. **Quality Settings**: `pydub` allows specifying parameters like bitrate. You can enhance the function to
accept such options.
4. **Performance**: For large-scale conversions, consider adding progress indicators or parallel processing
for efficiency.
This implementation provides a robust foundation for converting WAV files to MP3s using Python.
这又是一个非常能说明模型推理能力的例子。在这个回答中,它首先给出了一个足够的答案;接着,它意识到这个答案可能没有涵盖所有用户可能遇到的边缘情况,比如目录下所有文件的转换问题。为了解决这个问题,它又提供了另一个更健全的方案,能更好地处理所有可能出现的状况。最后,它详细解释了这些考虑,作为对回答的总结。总体来说,这个解决方案既全面又令人印象深刻,几乎和人类的思考过程不相上下。
基于这些回答,我们建议大家用DeepSeek R1 来试试各种有挑战性的问题。这个模型非常强大,尤其是在 60b 参数级别及以上的版本。
结束语
在这篇文章中,我们展示了如何通过 DigitalOcean 的 GPU Droplets 和 Ollama 来运行 DeepSeek R1。正如上面所看到的,这种方式为我们提供了一个又快又强的推理工具,能帮助我们完成编程、数学计算等各种任务。 如果你的团队也正在寻找性能可靠、价格实惠的H100 GPU或H200 GPU 服务器,欢迎与DigitalOcean中国区独家战略合作伙伴卓普云联系。
扩展阅读:
- 显存、带宽与CUDA核心的优先级分析,Deepseek 大语言模型部署技术选型
- 个人和企业必看,DeepSeek从1.5B到671B模型的选型与部署指南