0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[DeepSeek-R1] 30分でAWSのGPU EC2インスタンスでDeepSeekを動かす

Posted at

はじめに

DeepSeek を AWS の GPU インスタンスで動かす手順をまとめました。
作業が速い人なら 20 分程度で完了するかもしれません。

output.gif

EC2 インスタンスについて

g4dn が最もコストパフォーマンスが良いため、こちらを利用します。
g4ad の VRAM は 8GB ですが、g4dn は 16GB 以上あります。
Spot Instance なら $0.22/hour でコストを抑えられます。

g4dn
image.png

インスタンスの起動 (5分)

AWS にログインし、EC2 の起動画面から g4dn.xlarge を作成します。

image.png
image.png

ストレージは 50GB 程度あれば十分です。

スポットインスタンスで起動しますが、通常のインスタンスでも問題ありません。

image.png

まもなくすると起動します。
image.png

起動後、SSH でログインします。

Host deepseek
 HostName %IPアドレス%
 User ec2-user

ログイン後、以下のコマンドでパッケージを更新します。

% ssh deepseek
The authenticity of host '#.#.#.# (#.#.#.#)' can't be established.
ED25519 key fingerprint is SHA256:#####.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '#.#.#.#' (ED25519) to the list of known hosts.
   ,     #_
   ~\_  ####_        Amazon Linux 2023
  ~~  \_#####\
  ~~     \###|
  ~~       \#/ ___   https://aws.amazon.com/linux/amazon-linux-2023
   ~~       V~' '->
    ~~~         /
      ~~._.   _/
         _/ _/
       _/m/'

$ yum update -y

無事ログインできました。

NVIDIA ドライバーのインストール (10分)

公式ドキュメントを参考にドライバーをインストールします。

S3 にアクセスするため、aws configure の設定または AWS コンソールでの権限付与が必要です。
また、kernel-modules-extra をインストールしないと Unable to load the kernel module 'nvidia.ko' のエラーが発生するため注意してください。

$ sudo yum install -y gcc make kernel-devel-$(uname -r) kernel-modules-extra
$ sudo reboot
$ aws configure
AWS Access Key ID [None]: #####
AWS Secret Access Key [None]: ####
Default region name [None]: 
Default output format [None]: 

$ aws s3 cp --recursive s3://ec2-linux-nvidia-drivers/latest/ .
$ chmod +x NVIDIA-Linux-x86_64*.run
$ sudo /bin/sh ./NVIDIA-Linux-x86_64*.run

確認画面はOKで抜けちゃって大丈夫です。
下記の画面が出ればインストール完了です。

image.png

下記のコマンドで正しくドライバーがインストールできてるか確認できます。

$ nvidia-smi -q | head

==============NVSMI LOG==============

Timestamp                                 : Thu Feb  6 16:00:32 2025
Driver Version                            : 550.144.03
CUDA Version                              : 12.4

Attached GPUs                             : 1
GPU 00000000:00:1E.0
    Product Name                          : Tesla T4

とりあえずドキュメントに書いてあるので実行しときます。

$ sudo touch /etc/modprobe.d/nvidia.conf
$ echo "options nvidia NVreg_EnableGpuFirmware=0" | sudo tee --append /etc/modprobe.d/nvidia.conf
$ sudo reboot

Ollamaのインストール (5分)

公式ドキュメント通りにインストールします。

$ curl -fsSL https://ollama.com/install.sh | sh
>>> Installing ollama to /usr/local
>>> Downloading Linux amd64 bundle
######################################################################## 100.0%
>>> Creating ollama user...
>>> Adding ollama user to render group...
>>> Adding ollama user to video group...
>>> Adding current user to ollama group...
>>> Creating ollama systemd service...
>>> Enabling and starting ollama service...
Created symlink /etc/systemd/system/default.target.wants/ollama.service → /etc/systemd/system/ollama.service.
>>> NVIDIA GPU installed.

DeepSeek R1の実行 (5分)

CyberAgent の日本語モデルを試したい場合は次のステップへ。

下記を実行してpromptいれると実行されます。

$ ollama run deepseek-r1
pulling manifest 
pulling 96c415656d37... 100% ▕██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▏ 4.7 GB                         
pulling 369ca498f347... 100% ▕██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▏  387 B                         
pulling 6e4c38e1172f... 100% ▕██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▏ 1.1 KB                         
pulling f4d24e9138dd... 100% ▕██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▏  148 B                         
pulling 40fb844194b2... 100% ▕██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▏  487 B                         
verifying sha256 digest 
writing manifest 
success 
>>> hello
<think>

</think>

Hello! How can I assist you today? 😊

>>> what is 3 + 3 * 2 =?
<think>
Okay, so the user just asked me to solve "3 + 3 * 2". Hmm, that's a basic arithmetic problem. Let me think about how to approach this.

First, I remember that in math problems, there's an order of operations, often abbreviated as PEMDAS—Parentheses, Exponents, Multiplication and Division (from left to right), Addition and 
Subtraction (from left to right). 

Looking at the problem "3 + 3 * 2", I don't see any parentheses or exponents, so I move on to multiplication and division. In this case, there's a multiplication: 3 multiplied by 2.

So, I should do that first. Let me calculate 3 times 2, which equals 6.

Now, the problem simplifies to "3 + 6". That seems straightforward enough. Adding those together gives me 9.

Wait, let me make sure I didn't skip anything. Was there any part where I might have made a mistake? No, it looks like everything follows the order of operations correctly.

So, putting it all together: 3 plus (3 times 2) equals 9.
</think>

To solve \(3 + 3 \times 2\), we follow the order of operations (PEMDAS/BODMAS):

1. **Multiplication** comes before addition:
   - Calculate \(3 \times 2 = 6\).
   
2. Then, perform the addition:
   - Add 3 to the result: \(3 + 6 = 9\).

So, the final answer is:

\[
\boxed{9}
\]

>>> Send a message (/? for help)

簡単ですね。
nvidia-smiコマンドでちゃんとGPUメモリを使ってるのがわかります。

$ nvidia-smi
+-----------------------------------------------------------------------------------------+
| NVIDIA-SMI 550.144.03             Driver Version: 550.144.03     CUDA Version: 12.4     |
|-----------------------------------------+------------------------+----------------------+
| GPU  Name                 Persistence-M | Bus-Id          Disp.A | Volatile Uncorr. ECC |
| Fan  Temp   Perf          Pwr:Usage/Cap |           Memory-Usage | GPU-Util  Compute M. |
|                                         |                        |               MIG M. |
|=========================================+========================+======================|
|   0  Tesla T4                       On  |   00000000:00:1E.0 Off |                    0 |
| N/A   31C    P0             72W /   70W |   11875MiB /  15360MiB |     92%      Default |
|                                         |                        |                  N/A |
+-----------------------------------------+------------------------+----------------------+
                                                                                         
+-----------------------------------------------------------------------------------------+
| Processes:                                                                              |
|  GPU   GI   CI        PID   Type   Process name                              GPU Memory |
|        ID   ID                                                               Usage      |
|=========================================================================================|
|    0   N/A  N/A      4565      C   ...rs/cuda_v12_avx/ollama_llama_server      11872MiB |
+-----------------------------------------------------------------------------------------+

CyberAgentモデルの実行 (5分)

モデル消してgguf形式でollamaで実行できるモデルをダウンロードして実行します。

$ ollama rm deepseek-r1
$ ollama run hf.co/mmnga/cyberagent-DeepSeek-R1-Distill-Qwen-14B-Japanese-gguf:Q5_K_M
pulling manifest 
pulling 2f3f35d28b8d... 100% ▕██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▏  10 GB                         
pulling 8aa4c0321ccd... 100% ▕██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▏  493 B                         
pulling c737442a210a... 100% ▕██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▏  170 B                         
pulling 6e7b0c17f887... 100% ▕██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▏  193 B                         
verifying sha256 digest 
writing manifest 
success 
>>> 次を計算してください 3 + 3 * 2 = ?
<think>
まず、問題の式は3 + 3 * 2です。ここで重要なのは乗算と加算の優先順位です。一般的に数学では、乗算や除算よりも加算や減算を後に計算します。つまり、この式ではまず3 * 2を計算してから、その結果を3に足す必
要があります。

まず、3 * 2を計算すると6になります。次に、3と計算した6を足すので、3 + 6 = 9となります。これで答えは9です。
</think>

**解答:**

問題の式は **3 + 3 × 2** です。  
数学では「乗算」よりも「加算」が優先されるため、まず **3 × 2** を計算します。

1. 乗算を先に計算:  
   \( 3 \times 2 = 6 \)

2. 加算で結果を足す:  
   \( 3 + 6 = 9 \)

**答え:9**

>>> Send a message (/? for help)

思考が日本語になりましたね。

アニメーションGIFにするとこんな感じです。
それなりに早く出力されますね。

DeepSeekはいろんな派生モデルあるので色々試してみると良いかもです。

output.gif

最後に

BitStarでは絶賛OpenAIをはじめとした生成AIを使ったデータ分析/プロダクト開発に力を入れています。
エンジニアを募集中です。詳しくは下記を参照ください。

image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?