🐳

「DeepSeek-R1-Distill-Qwen-1.5B」をM2 Pro Mac miniで動かしてみた

2025/01/25に公開

DeepSeek-R1-Distill-Qwen-1.5Bとは

「DeepSeek-R1-Distill-Qwen-1.5B」の前に、まず「DeepSeek-R1」について簡単に紹介します。DeepSeek-R1は、DeepSeekによって開発されたオープンで高性能な大規模言語モデル(LLM)です。
https://huggingface.co/deepseek-ai/DeepSeek-R1

DeepSeek-R1はパラメータ数が671Bの巨大なモデルですが、モデル圧縮手法の1つである蒸留を用いて小型化されたモデルも公開されています。

蒸留 (Distillation)とは、大規模な「教師モデル」の知識をより軽量な「生徒モデル」に移行することでモデルの圧縮をする手法です。深層学習モデルを軽量化するために使われています。
https://codecrafthouse.jp/p/2018/01/knowledge-distillation/

DeepSeek-R1の蒸留モデルは、既存のモデルであるQwenやLlamaをベースにDeepSeek-R1の推論パターンを学習させているようです。公開されてる6個の蒸留モデルのなかで一番パラメータ数が少ないモデルがDeepSeek-R1-Distill-Qwen-1.5Bです。今回はこのモデルをM2 Pro Mac miniで動かしてみました。

M2 Pro Mac miniで試す

今回使用しているMac miniのスペックは以下の通りです。

  • Apple M2 Pro
    • CPU: E-core: 4 / P-core: 8
    • GPU: 19-core
    • Nerual Engine: 16-core
  • RAM: 32GB

パッケージのインストール

以下のパッケージをインストールします。torchは2.3以上である必要がありました。

pip install transformers 'torch>=2.3'

今回試したコード

今回はvLLMやOllamaなどを使わずにHuggingFace Transformersで試してみます。
入力をモデルに渡す前に、Xで言われている「Think in English and response in Japanese:」を入力テキストの前に追加しています。また、温度パラメータは0.7に設定しました。

実際に使用したコードはGitHubで公開しています: Shakshi3104/deepseek-r1-distill-qwen-1.5b

# モデルの設定
model_name = "deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B"

model = AutoModelForCausalLM.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)

if torch.backends.mps.is_available():
    device = torch.device('mps')
    model.to(device)

generator = pipeline("text-generation", model=model, tokenizer=tokenizer,
                     device="mps" if torch.backends.mps.is_available() else "cpu")

# 入力
input_text = "日本の首都は?"
messages = [
    {"role": "user", "content": f"Think in English and response in Japanese:\n{input_text}"},
]

# 出力
outputs = generator(
    messages,
    max_new_tokens=1024,
    do_sample=True,
    temperature=0.7,
    pad_token_id=tokenizer.pad_token_id,
)

出力のかかる時間とメモリ使用量

出力にかかる時間は、大体1分から2分くらいでした。リアルタイムの会話に使うことは難しいそうですが、Mac miniで動かしてることを考えるとそれなりの速さなのかなと思います。

また、メモリについて、実行中は最大でも26GBくらいまで使用量が増えますが、メモリに乗り切っているっぽいので問題なく動作します。

出力の例

同じ入力に対して何回か実行してみた結果を載せておきます。

入力: 日本の首都は?

出力①

<think>
Okay, the user is asking about the capital of Japan. I should first recall the capital. I think it's Tokyo. Let me make sure. Yes, Tokyo is the capital of Japan. I should also mention the neighboring countries briefly, like the East and West Japan, but maybe just focus on Tokyo for simplicity. I should keep it clear and straightforward. Alright, time to present the answer.
</think>
日本の首都は Tokyo です。

出力②

<think>
Okay, the user is asking about the capital of Japan in English. I should provide the correct answer in Japanese. The capital is Tokyo. I'll make sure to spell it correctly in Japanese.
</think>
日本の首都は东京です。

「東京」と返してくれますが、ローマ字や簡体字で返ってきてます。

入力: たまごっちとは何ですか?

出力①

<think>
Alright, the user is asking "たまごっちとは何ですか?" which translates to "What is a tabaigari?" in English. I need to figure out what a tabaigari is.
I recall that tabaigari is a traditional Japanese fish. It's one of the larger fish, usually around 13-16 centimeters long. It's not as colorful as some other fish, so it's more of a grayish fish.
Tabaigari is commonly used for catching small fish like crabs, clams, or crinets. It's popular because it's versatile and can be caught with various nets and methods. People use it in both freshwater and marine environments.
I should explain it clearly, maybe mention its size, how it's caught, and its use in different contexts. Keeping the explanation simple and conversational will help the user understand.
</think>
たまごっちは日本語で「 tabaigari 」と reptable. たまごっちは Japanese tradition で広いFishです。它的長さは13-16cm左右です。 tabaigari は 大SCALEで、 Crab、Clams、Crinets 等の小FishをCatch するためのFishです。它的常用方法は various Net と method で catch します。

出力②

Okay, so I need to figure out what "たまごっち" means in Japanese. I'm not entirely familiar with this word, so I should start by breaking it down. The name "たまご" sounds familiar, maybe it's something related to water? I think "たまご" is a term used in Japanese, perhaps for water or plants. 
Then there's the "っち" part. I know "っち" in English can mean "child" or "girl," but in Japanese, it has different meanings. I believe it's a gender-neutral term and is used in various contexts, maybe for talking about people without specifying gender. So combining "たまご" and "っち," I'm guessing it's a term used to refer to water or plants, but not specifically referring to a person. 
I remember that in Japanese, "たまご" is used in contexts like describing plants, the ground, or water bodies. So "たまごっち" could be used to describe a water body such as a pond, river, lake, or even a body of water in general. It's a term that's often used in literature, art, or everyday conversation to refer to the natural environment around a person or place. 
I should also consider if there's a more specific meaning. Maybe it's used in a particular type of context, like describing a garden or a water feature. But without more context, it's safer to assume it's a general term for water bodies. 
So putting it all together, "たまごっち" is a term used in Japanese to refer to water or a body of water, not specifically to a person or object. It's versatile and can be applied in various situations where water is involved. 
I should also think about synonyms or related terms. In Japanese, "水" is a common term for water, so "たまご" might be a variation or a specific term for a body of water. I don't think there's a direct equivalent in English for "たまごっち," which is why it's used in Japanese literature and art to denote the natural surroundings.
Additionally, I can think about how it's used in sentences. For example, a person might say, "We should water the plants," and in that context, "たまご" would refer to the plants. But since the word is "たまごっち," it's more about the water itself rather than specific plants. 
In summary, "たまごっち" is a term in Japanese that refers to water or a body of water, often used in contexts where the natural surroundings around a person or place are important. It's a versatile term with various applications in Japanese culture and literature.
</think>
たまごっちは日本語で"水"の意味を有するtermです。"たまご"は水や植物の意味を有し、"っち"は人間の特定を指摘する genders-neutralのtermです。"たまごっち"は水体の意味を有し、水体や水の全体を指摘するtermです。例に"たまご"は水体や植物を指摘するtermで、"たまごっち"は水体や水の全体を指摘するtermです。

固有名詞はうまく返せないのかも知れません。

おわりに

今回は、DeepSeek-R1の蒸留モデルである「DeepSeek-R1-Distill-Qwen-1.5B」をM2 ProのMac miniで動かしてみました。蒸留モデルの出力はまだ実用レベルではないかも知れません。今後、研究が進み、パラメータ数の多いLLMモデルと遜色ない小さなモデルが出てくると信じています。新たな小規模モデルが出てきたときに試してみたいと思います。

参考

Discussion