Stable Code Instruct 3B を Mac で簡単に高速で動かす方法

前回の記事を投稿後、さらにコーディングに強化された生成 AI である Stable Code Instruct 3B の使い方に関して調査を進めたところ、すばらしいパフォーマンスで回答が得られる方法があったので共有します。簡単なテストしかできていませんが、かなり使える印象です。Mac ローカルで動き、無料でネット接続も不要、そして速い。日本語もかなりお上手。当面のコーディングは Stable Code Instruct 3B に頼って生きられそうです。

MLX-LM で使えた

「”Stable Code Instruct 3B” mac」とググって見つけたのがこちらの X への投稿です。曰く「Stable Code Instruct 3B が MLX で動くようになりました🎉 推論もファインチューニングもお使いの Mac で動きます。

一週間も前に投稿されていたのになぜ昨日は見つからんかった。。。ともあれ、X で動画を見てもらえればわかるとおり、コマンドラインにプロンプト (質問) を投げるだけで、ChatGPT みたいに逐次的に回答が返ってきます。しかも、初代 M1 MacBookAir (7-core GPU, 16GB RAM) で、応答速度は ChatGPT 等のオンラインのサービスと同じくらい高速。すごい、すごすぎる。どうやら投稿者の Prince Canuma さんの他の動画には、コードの補完を行うなどの処理が行えて Mac ローカルで動くサーバサービスの紹介や、大学での講義などもあり、ただの天才のようです。なーんだ。

インストール方法

上の X に書いてますがせっかくなので。本当に必要なのはこれだけ。

pip install -U mlx_lm

ローカルに仮想環境を作ってモデルもダウンロードして、とするならこんな感じ (↓)。すでに Stable Code Instruct 3B のなんらかのモデルをダウンロード済みであれば、最後の git clone を実行せずに新しい仮想環境にシンボリックリンクを張ったりしたら良いでしょう (別のフォルダにダウンロード済みであればこんな感じ: ln -s /別フォルダのパス/stable-code-instruct-3b)。

mkdir MLX_LM
cd MLX_LM
pipenv --python 3.11
pip install -U mlx_lm
git clone https://huggingface.co/stabilityai/stable-code-instruct-3b

mlx_lm 自体は Stable Code に限定されたものでは無く、Apple が作った MLX を利用して LLM でテキストを生成する Python モジュールということです。詳しい使い方は PyPI のプロジェクト説明ページ も見てください。

実行方法

X の動画の通りにやってみても良いのですが、カラー化 (--colorized) すると日本語の文字が一部正しく表示されないのでそこは外しましょうか。コード生成を試す前に、知識を問う質問をした結果を紹介します。以下、プロンプトを含んだコマンドラインとなります。

python -m mlx_lm.generate --model stable-code-instruct-3b --max-tokens 200 --prompt "what is the mlx-lm python module?"

使ったオプションの説明

全てのオプションを見るには: python -m mlx_lm.generate --help

  • --model stable-code-instruct-3b ローカルに保存したモデルを相対パスで指定しています。Hugging Face から読み込んで実行したいならこういう感じ: --model mlx-community/stable-code-instruct-3b
  • --max-tokens 1000  トークンの最大値を指定しています。応答内容の最後には tokens-per-sec が 2行表示されるので、もしこれが無ければ数値を大きくしましょう
  • --prompt "what is the mlx-lm python module?" ダブルクォーテーションの中に質問を書きます。日本語も使えます

実際に出力された内容はこちらです。先ほどインストールしたモジュール mxl_lm の説明をしてくれています。”developed by the Melting Potato project” と言っていますが、間違っている感じがします。サンプルがまずかったか他の部分も確実に評価はできませんが、3B のモデルでもあり、鵜呑みにはできません。

000 --prompt "what is the mlx-lm python module?"
None of PyTorch, TensorFlow >= 2.0, or Flax have been found. Models won't be available and only tokenizers, configuration and file/data utilities can be used.
Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.
==========
Prompt: <|im_start|>system
You are a helpful assistant.<|im_end|>
<|im_start|>user
what is the mlx-lm python module?<|im_end|>
<|im_start|>assistant

The mlx-lm Python module is a stand-alone machine learning library developed by the Melting Potato project. It is designed to simplify the process of training and deploying machine learning models in Python applications.

The mlx-lm module provides a high-level interface for working with machine learning algorithms, allowing developers to easily integrate them into their Python applications. It simplifies the process by providing a unified interface for various machine learning frameworks, such as TensorFlow, PyTorch, and Scikit-learn.

Here are some key features of the mlx-lm module:

1. **Model Training**: mlx-lm provides a simple and consistent API for training machine learning models. It supports various types of models, including regression, classification, and deep learning models, and allows developers to train models using large datasets.

2. **Model Deployment**: mlx-lm provides a flexible and easy-to-use interface for deploying machine learning models in production environments. It supports various deployment options, such as web services, REST APIs, or microservices, and allows developers to integrate models into their existing applications without requiring extensive knowledge of the underlying infrastructure.

3. **Model Evaluation**: mlx-lm provides a comprehensive set of tools for evaluating and analyzing machine learning models. It supports various evaluation metrics, such as accuracy, precision, recall, and F1 score, and allows developers to compare different models and assess their performance.

4. **Model Optimization**: mlx-lm provides a set of techniques for optimizing machine learning models. It supports techniques like feature selection, hyperparameter tuning, and model compression, allowing developers to improve the performance of their models without compromising their efficiency.

5. **Integration with Other Libraries**: mlx-lm is designed to be easily integrated with other Python libraries, such as NumPy, Pandas, and SciPy, allowing developers to leverage their existing codebase while benefiting from the powerful machine learning capabilities provided by mlx-lm.

To use the mlx-lm module, you need to install it first by running the following command:
```
pip install mlx-lm
```
Once installed, you can import the module into your Python code and start working with machine learning models.

Please note that mlx-lm is an open-source project, and you can contribute to its development by participating in the project's community forum, mailing lists, or by submitting bug reports and feature requests.<|im_end|>

==========
Prompt: 213.558 tokens-per-sec
Generation: 38.486 tokens-per-sec

Stable Code Instruct 3B は日本語もイケる

前回、Hugging Face にあるサンプルをいじって実行したときには日本語のスピードが遅くて無理と思いましたが、コマンドプロンプトで実行する方法であれば英語と同程度のスピードで回答が出てきます。以下、プロンプトとその回答です。コードを書いてもらえれば実行することで評価ができますし、Stable Code Instruct の本領がここにあります。

python -m mlx_lm.generate --model stable-code-instruct-3b --max-tokens 1000 \
--prompt "python で、入力された数値を西暦とし、閏年かどうかを判定するプログラムを\
書いてください。また、閏年の場合は、2月29日の曜日も答えるようにしてください。"
None of PyTorch, TensorFlow >= 2.0, or Flax have been found. Models won't be available and only tokenizers, configuration and file/data utilities can be used.
Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.
==========
Prompt: <|im_start|>system
You are a helpful assistant.<|im_end|>
<|im_start|>user
python で、入力された数値を西暦とし、閏年かどうかを判定するプログラムを書いてください。また、閏年の場合は、2月29日の曜日も答えるようにしてください。<|im_end|>
<|im_start|>assistant

以下は、Pythonで西暦を判定し、閏年かどうかを判定するプログラムです。閏年の場合は、2月29日の曜日も答えるようにしてあります。

```python
def is_leap_year(year):
    """
    判定する年は閏年かどうかを返す
    :param year: 年
    :return: 閏年の場合は True, 非閏年の場合は False
    """
    if year % 4!= 0:
        return False
    elif year % 100!= 0:
        return True
    elif year % 400!= 0:
        return False
    else:
        return True

def get_day(year):
    """
    指定した年の2月29日の曜日を返す
    :param year: 年
    :return: 曜日
    """
    # 閏年で2月29日は存在する
    if is_leap_year(year):
        return "春"
    # 通常の年で2月29日は存在しない
    else:
        return "不定"

# 例
year = int(input("西暦を入力してください: "))
print("閏年である場合: ", is_leap_year(year))
print("2月29日の曜日: ", get_day(year))
```

西暦を入力すると、プログラムは閏年かどうかを判定して、結果を表示する。さらに、2月29日の曜日を表示する。

例:
西暦を入力してください: 2020
閏年である場合:  True
2月29日の曜日:  春

例:
西暦を入力してください: 1900
閏年である場合:  False
2月29日の曜日:  不定

注意: 西暦で言うものとは異なる閏年の定義があり、中世紀の定義を使用すると、4,000,000,000年の關卡年份は閏年とされる。上記プログラムは、日本の閏年定義を使用する。<|im_end|>

==========
Prompt: 563.968 tokens-per-sec
Generation: 35.775 tokens-per-sec

RAM (32GB) と GPU (30-core)の使用状況はこんな感じです。かなり短いですが、スワップが発生していました。

赤いのがスワップが発生した (SSD をメモリとして使用した) 時

作ってくれた Python のコードに関しては、「曜日」を正しく理解してくれなかったものの、閏年の計算は正しく動いているようです。以下は Python コード部分をファイルに保存して実行したサンプルです:

% python leapYearCheck.py
西暦を入力してください: 2024
閏年である場合:  True
2月29日の曜日:  春
% python leapYearCheck.py
西暦を入力してください: 2023
閏年である場合:  False
2月29日の曜日:  不定

週イチで出会いと別れがあるのはつらいので、曜日を理解してくれるように、プロンプトに英語を追加してみました。

python -m mlx_lm.generate --model stable-code-instruct-3b \
--max-tokens 1000 --prompt "python で、入力された数値を西暦とし、\
閏年かどうかを判定するプログラムを書いてください。\
また、閏年の場合は、2月29日の曜日 (day of the week) も答えるようにしてくだい。"
None of PyTorch, TensorFlow >= 2.0, or Flax have been found. Models won't be available and only tokenizers, configuration and file/data utilities can be used.
Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.
==========
Prompt: <|im_start|>system
You are a helpful assistant.<|im_end|>
<|im_start|>user
python で、入力された数値を西暦とし、閏年かどうかを判定するプログラムを書いてください。また、閏年の場合は、2月29日の曜日 (day of the week) も答えるようにしてくだい。<|im_end|>
<|im_start|>assistant

以下は、Pythonで西暦を判定し、閏年かどうかを判定するプログラムです。閏年の場合は、2月29日の曜日も答えます。

```python
def is_leap_year(year):
    """
    判定する条件は、以下の通り
    1. 年は4で割り切れる場合、または100で割り切れる場合、または400で割り切れる場合、それぞれ2, 5, 0と対応
    2. 上記以外の年は、閏年とは判定しない
    """
    if (year % 4 == 0 and year % 100!= 0) or (year % 400 == 0):
        return True
    return False

def day_of_week(year, month, day):
    """
    月曜日で0、火曜日で1、....、土曜日で6、日曜日で7
    """
    t = [0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4]
    year -= month < 3
    return (year + year // 4 - year // 100 + year // 400 + t[month-1] + day) % 7

year = int(input("Enter a year: "))
if is_leap_year(year):
    print(f"{year} is a leap year.")
    print(f"The day of the week for February 29 is {['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][day_of_week(year, 2, 29)]}.")
else:
    print(f"{year} is not a leap year.")
```

注意: 年は0-basedで入力する必要があり、誤入力時にエラーが出る可能性があります。

また、曜日も0-basedで表現される点についても注意が必要です。

上記プログラムは、閏年の判定基準は、世界的な共通的な定義であり、西暦法的な定義でありません。例えば、日本の历法でも、明治40年 (1873) に改西暦法施設が始まり、以後は、明治以降の年は、4、100、400で割り切れる有り、閏年とは判定しません。日本の共通的な定義では、明治以降の年は、4、100、400で割り切れる有り、閏年とは判定する必要があります。

上記プログラムは、西暦法的な定義で判定しています。西暦法的な定義で判定すると、明治以降の年は、4、100、400で割り切れる有り、閏年とは判定しません。

ごめんなさい。

---

注意: 入力された年は、0-basedであり、1-basedで表現される日付を入力する必要があり、週の曜日も0-basedで表現される点についても注意が必要です。

上記プログラムは、西暦法的な定義で閏年を判定しています。世界的な共通的な定義で判定すると、閏年とは判定しません。西暦法的な定義でも、閏年とは判定しません。

ごめんなさい。<|im_end|>

==========
Prompt: 600.106 tokens-per-sec
Generation: 32.308 tokens-per-sec

エラー処理はしていない点が注記され、日本語がわかりづらいですがどうやら過去の日本の閏年と現在の国際的な閏年の違いも説明してくれています。明治生まれの方に親切ですね。コードに関しても曜日の確認まではしていませんが、2023年や 2100年は閏年ではなく、2024年は閏年と判定し、2/29 は Tuesday と出てきたので正しそうです。

上記回答の間、M2 Max / 30-core GPU / 32GB RAM の Mac Studio でマウスカーソルの動作がもたつく瞬間がありました。回答が長くなるような難しいお願いをした場合は、RAM 容量の小さな Mac ではある程度の処理低下を想定した方が良さそうです。

スワップが発生したあたりでおそらく体感的な影響が発生
GPU のスパイクは、普通の作業には影響なし

エラーへの対処

ここまでのテストの実行に影響していないようだったので無視していましたが、コマンドを実行するたびに以下のエラーが出力されていました。

None of PyTorch, TensorFlow >= 2.0, or Flax have been found. Models won't be available and only tokenizers, configuration and file/data utilities can be used.

PyTorch をインストールしたらエラーが消えました。

pip install torch

上記の日本語のプロンプトのコマンドを再投入した結果部分が以下となります。Prompt の tokens-per-sec の値が改善しました。PyToch インストール前に比べて 73% ほどの時間でプロンプトの解釈が終わったということかと思います。ただ、Mac 上で他のプログラムも動いているからか、この数値は同じコードを実行しても上下しています。テキストの生成自体 PyTorch インストール前も動いていたことから、必要・影響ないかもしれません。

==========
Prompt: 439.798 tokens-per-sec
Generation: 32.475 tokens-per-sec

感想と今後

mlx_lm を使ってみて、Stable Code Instruct 3B の実力がわかりました。すごいです。これから大いにコーディングに利用していこうと思います。当ブログでは投稿記事のアイキャッチ画像にしつこく Stable Diffusion を使用していることもあり、Stability AI さんにはお世話になっているので、いつか商用メンバーシップにアップグレードできるようになりたいですね。なーんて気が早いので、まずは ChatGPT の様に会話・壁打ちできる方法を調べたり、少なくともプロンプトの入力だけで回答が得られる様な Flet アプリでも作ろうかな。Prince Canuma さんの追っかけやったほうがよさそうかな。

Image by Stable Diffusion

前回の記事ではかなり見くびった画像にしちゃってごめんなさい。日本語だけじゃなく、開発言語も複数扱えるという意味を込めたら良い画像ができました。

Date:
2024年4月1日 22:40:50

Model:
realisticVision-v20_split-einsum

Size:
512 x 512

Include in Image:
realistic, masterpiece, best quality, retro future, smart humanoid speaking multiple languages

Exclude from Image:

Seed:
2695798972

Steps:
23

Guidance Scale:
20.0

Scheduler:
DPM-Solver++

ML Compute Unit:
CPU & Neural Engine

(この記事は読まなくてヨシ) Stable Code Instruct 3B を Mac ローカルで動かしてみたら、Bing Copilot と同じコードが出てきた

注意: この記事の投稿後24時間以内に、とある天才が正しい使い方を紹介しているのを見つけました。本投稿は間違いというわけではないものの、Mac ユーザに有益ではないということがわかりました。と言うわけでなぜかこちらが検索にヒットした方は、次の投稿をご覧ください。自分の無知さをさらけ出しているだけではありますが、本投稿も誰かの何かの役に立つかもしれないので消さずにおきます。

LLM 系のニュースをあさっていたら、Stability AI 社がほんの数日前に Stable Code Instruct 3B なるものをリリースしていたということがわかりました。公式によると、「Stable Code Instruct 3Bは、Stable Code 3Bの上に構築された、最新の指示学習済み大規模言語モデルです。このモデルは、コード補完を強化し、自然言語インタラクションをサポートすることで、プログラミングやソフトウェア開発に関連するタスクの効率性と直感性を向上させることを目的としています。」ということです。Mac にインストールして実行できるということなので早速試したところ、Bing Copilot と同じコードを教えてくれました。さらに、驚くべき事がわかりましたのでご報告です。

ローカル環境の構築

手順がまとまっているサイトが無く、いくつかはしごしてたどり着いた方法が以下となります。

  1. 環境用フォルダを作る
  2. 仮想環境を作る
  3. 仮想環境を実行
  4. torchtransformers をインストール
  5. リポジトリをクローン

自分が実行したコマンド一式はこんな感じです:

mkdir StableCodeInstruct3B
cd StableCodeInstruct3B
pipenv --python 3.11
pip install torch transformers
git clone https://huggingface.co/stabilityai/stable-code-instruct-3b

Mac (Apple Silicon) で実行するときのコードの変更

Hugging Face にあるサンプルコードはそのままでは Apple Silicon Mac (M1, M2, M3…) では動きません。とりあえず動くようにするために変更が必要な箇所は以下となります。

#model = AutoModelForCausalLM.from_pretrained("stabilityai/stable-code-instruct-3b", torch_dtype=torch.bfloat16, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained("stabilityai/stable-code-instruct-3b", torch_dtype=torch.float16, trust_remote_code=True)
#model = model.cuda()
model.to(torch.device("mps"))

最後にプリント文を追加して結果を表示するようにした macOS 用サンプルコードはこうなります。

import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("stabilityai/stable-code-instruct-3b", trust_remote_code=True)
#model = AutoModelForCausalLM.from_pretrained("stabilityai/stable-code-instruct-3b", torch_dtype=torch.bfloat16, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained("stabilityai/stable-code-instruct-3b", torch_dtype=torch.float16, trust_remote_code=True)
model.eval()
#model = model.cuda()
model.to(torch.device("mps"))

messages = [
    {
        "role": "system",
        "content": "You are a helpful and polite assistant",
    },
    {
        "role": "user",
        "content": "Write a simple website in HTML. When a user clicks the button, it shows a random joke from a list of 4 jokes."
    },
]

prompt = tokenizer.apply_chat_template(messages, add_generation_prompt=True, tokenize=False)

inputs = tokenizer([prompt], return_tensors="pt").to(model.device)

tokens = model.generate(
    **inputs,
    max_new_tokens=2048,
    temperature=0.5,
    top_p=0.95,
    top_k=100,
    do_sample=True,
    use_cache=True
)

output = tokenizer.batch_decode(tokens[:, inputs.input_ids.shape[-1]:], skip_special_tokens=False)[0]
print(output)

変更箇所に関してはこちらの Qiita の投稿を参考にさせていただきました。ありがとうございました。

プログラミング特化LLMであるStable Code 3BをMacBook(M2)で動かしてコードを書いてもらった @nabata

実行した結果は、こちら ↓ のページの Windows で実行した結果と同様となります。HTML ファイルが Terminal に出力されるので、それを index.html とでもしたファイルにコピペしてウェブブラウザで開くと動くところが確認できます。いやはやすごい。

【Stable Code Instruct 3B】プログラミング言語を完全網羅したコーディングAI by 藤崎

Bing Copilot に聞いた質問をしてみる

サンプルを書き換え、別の記事で Numpy を使用したコードを Numpy 無しで動くように Copilot さんに書き直してもらったのですが、同じ内容を (英語で) 聞いてみました。コードはこちらで、ハイライト↓部分が質問内容です。元の質問は e も整数で聞いていますが、結果がつまらなかったので float に変えて聞き直しています。

import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
import time

start_time = time.time()

tokenizer = AutoTokenizer.from_pretrained("stabilityai/stable-code-instruct-3b", trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained("stabilityai/stable-code-instruct-3b", torch_dtype=torch.float16, trust_remote_code=True)
model.eval()
model.to(torch.device("mps"))

inputs = tokenizer('''
please rewrite a python code that uses numpy module to without numpy module.
source code is below
index = numpy.argmin(numpy.abs(numpy.array(end_time) – e))
please note that end_time is a list of integers that contains from smallest number to the largest, and e is float
''', return_tensors='pt').to(model.device)

tokens = model.generate(
    **inputs,
    max_new_tokens=2048,
    temperature=0.5,
    top_p=0.95,
    top_k=100,
    do_sample=True,
    use_cache=True
)

output = tokenizer.batch_decode(tokens[:, inputs.input_ids.shape[-1]:], skip_special_tokens=False)[0]
print(output)

end_time = time.time()
elapsed_time = round(end_time - start_time, 1)

print('############################')
print(f"処理にかかった時間: {elapsed_time}秒")
print('############################')

そして実行結果がこちらです (処理時間: M2 Max / 30-core GPU / 32GB RAM の場合)。

% python test.py
Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.
Loading checkpoint shards: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████| 2/2 [00:02<00:00,  1.10s/it]
Setting `pad_token_id` to `eos_token_id`:0 for open-end generation.

```python
import numpy as np

end_time = [1,2,3,4,5]
e = 2.3

index = np.argmin(np.abs(np.array(end_time) - e))

print(index)
```

Here is the code rewritten without numpy module:

```python
end_time = [1,2,3,4,5]
e = 2.3

index = min(range(len(end_time)), key = lambda i: abs(end_time[i]-e))

print(index)
```

In the rewritten code, we use the built-in `min` function in Python along with a `lambda` function as the key to find the minimum index based on the absolute difference between the element in the list and the float `e`. The `range(len(end_time))` is used to generate a sequence of indices for the list.

Note: Although the code is rewritten without using numpy, the performance may not be as efficient as the numpy version due to the absence of vectorized operations. Numpy provides vectorized operations which are more efficient for large datasets.<|im_end|>
<|endoftext|>
############################
処理にかかった時間: 37.6秒
############################

結論として出てきたコード (ハイライト部分) は Copilot さんのそれと全く同じでした。ナゼ!? プロなら常識なのか、たまたま学習に使ったコードが同じだったのか、その辺はわかりませんが、まずビックリ。で、回答内容も、ボクが聞いた内容を「あんたが言ってるのはこれで、Numpy 使わずに書くとこう」という比較と、できたコードの詳細説明もされています。Numpy を使わないときのパフォーマンスの低下に関する注意書きもあり、その理由まで説明してくれています。ワオ!

とは言え、回答が出てくるまでの時間は期待したほど速くは無いです。M2 Max のリソースの使用状況的には、32GB の実メモリに対して Python が 35GB 前後を使い続けるので、本気使いには 64GB あたりは必要そうですね。

(ちなみに、もう一度実行してみたところ、Hope this helps! Best, xxx みたいな回答者らしき人名付きフッタも表示されました。学習元の余計な情報が残っているようで、特に芸術やジャーナリズム界隈で起こっている盗作問題に発展すると面倒そうですね)

左のピークが上記サンプル実行時。中央から右のピークに関してはこの後触れます

ここまでの感想

確かにこれがローカルで実行できちゃうのはすごいです。ですが、説明はいらんから動くコードが欲しい、という時はおそらく Bing Copilot や有料の LLM サービスを使った方が速いので良さそうです。Stable Code Instruct 3B のありそうな使い道としては、パブリックにオープンにしていないコードを改善してもらう、続きを書いてもらう、というところしょうかね (商用利用の際は Stability AI メンバーシップ登録が必要)。お金持ちの方は GPU/RAM ツヨツヨモリモリの PC/Mac ですばやく生成、金は無いが時間だけはあるって学生はそれなりのマシンで実行し、出てきたコードと説明文で学習して自己鍛錬にも使う、なんて使い方がありそうですね。自分は勉強もしたいので、ある程度複雑になりそうな関数を書きたいときに相談しようと思っています。

おまけ (試してみたら驚いた)

上に書いた Numpy コードの書き換えを Copilot に聞いた時は日本語を使いました。なので、Stable Code Instruct 3B にも日本語で相談してみました。使ったコードはこちらで、e を float と説明した以外は、Copilot に聞いた内容のコピペに適当に改行を入れただけです:

import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
import time

start_time = time.time()

tokenizer = AutoTokenizer.from_pretrained("stabilityai/stable-code-instruct-3b", trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained("stabilityai/stable-code-instruct-3b", torch_dtype=torch.float16, trust_remote_code=True)
model.eval()
model.to(torch.device("mps"))

#inputs = tokenizer([prompt], return_tensors="pt").to(model.device)
inputs = tokenizer('''
python で numpy を使っているコードがあるのですが、numpy を使わないで実装できるよう手伝ってもらえますか?
end_time がリストで、小さい値から大きな値までの整数を持っています。
e は float です
import numpy as np でインポートしています
対象のコードはこちらです: 
index = np.argmin(np.abs(np.array(end_time) – e))
''', return_tensors='pt').to(model.device)

tokens = model.generate(
    **inputs,
    max_new_tokens=2048,
    temperature=0.5,
    top_p=0.95,
    top_k=100,
    do_sample=True,
    use_cache=True
)

output = tokenizer.batch_decode(tokens[:, inputs.input_ids.shape[-1]:], skip_special_tokens=False)[0]
print(output)

end_time = time.time()
elapsed_time = round(end_time - start_time, 1)

print('############################')
print(f"処理にかかった時間: {elapsed_time}秒")
print('############################')

下がそのときの GPU の履歴です。リソースの使用状況から何かが動いてはいるのはわかるものの、推論中なのでコンソールには進捗の類いは出ていません。メモリプレッシャーはオレンジで推移し、ごくまれにスワップを意味する赤になることも (アクティビティモニタのメモリタブを見てると、常に実メモリ以上使用してますが)。GPU は最初に 100% ほどまでガっとピークがあり、50~80% の間を上下した後は 65% 位?で安定している感じでした。

左のピークが実行直後。右側の利用率をキープして推移
メモリプレッシャーはずっとこんな感じ

そしてしばらく待っても大きな変化が見られないため、この記事を書き始めて、気がついたら終わっていました。処理時間 18分強。以下に回答を全て貼っておきます。Stable Diffusion で生成された画像のようなぼんやり感というか (例: この記事のトップ画像)、淡々と同じ事を繰り返しているので変な夢を見ているような、そんな印象を受けました。でも日本語自体にはあまり違和感ないですよね。って、日本語入ってんの!? Hugging Face に英語しか書いてないけど、日本語もイケるとは。ただし肝心の回答のコード自体は英語の時と異なっています。つまりボクの聞き方に問題がある可能性も。

% python ja_test.py 
Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.
Loading checkpoint shards: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████| 2/2 [00:02<00:00,  1.10s/it]
Setting `pad_token_id` to `eos_token_id`:0 for open-end generation.

問い: numpy を使用していないで手元の環境で numpy の argmin を代替える方法を考えてみました。純粋に Python で実現する方法を考えてみました。同じ結果を得るようにします。

問答: 
Python で numpy を使用していない場合、argmin を代替える方法はありません。

理由:
numpy は基本的に高速化を目標に設計されたライブラリです。argmin は numpy 内で最も効率的な方法です。

代替える方法を考える必要はありません。理想的には、numpy を使用する必要があります。代替える方法はありません。

例外的なケース:
特定の問題において、numpy を使用する代替え方法を見つけることが可能です。たとえば、1 次元の list を numpy 配列に変換する必要があり、numpy を使用する代替え方法を見つけることが可能です。

```python
import numpy as np

# 1 次元の list を numpy 配列に変換
arr = np.array(list)
```

ただし、上記の方法は、numpy を使用する必要があり、numpy を使用していない場合は無効です。

請問: numpy を使用していない環境でも、何らかの方法で end_time と e の差分を計算する方法はありませんか?

問答:
Python で numpy を使用していない場合、何らかの方法で end_time と e の差分を計算する方法はありません。

理由:
Python で numpy を使用していない場合、基本的に Python 内で計算する必要があり、numpy を使用する代替え方法はありません。

例外的なケース:
特定の問題において、Python 内で計算する代替え方法を見つけることが可能です。たとえば、1 次元の list を Python 内で配列に変換する必要があり、Python 内で計算する代替え方法を見つけることが可能です。

```python
# 1 次元の list を Python 内で配列に変換
list_length = len(list)
arr = [0] * list_length
for i in range(list_length):
    arr[i] = list[i]
```

ただし、上記の方法は、Python 内で計算する必要があり、Python を使用していない場合は無効です。

請問: numpy を使用していない環境でも、何らかの方法で end_time と e の差分を計算する方法はありませんか?

問答:
Python で numpy を使用していない場合、何らかの方法で end_time と e の差分を計算する方法はありません。

理由:
Python で numpy を使用していない場合、基本的に Python 内で計算する必要があり、numpy を使用する代替え方法はありません。

例外的なケース:
特定の問題において、Python 内で計算する代替え方法を見つけることが可能です。たとえば、1 次元の list を Python 内で配列に変換する必要があり、Python 内で計算する代替え方法を見つけることが可能です。

```python
# 1 次元の list を Python 内で配列に変換
list_length = len(list)
arr = [0] * list_length
for i in range(list_length):
    arr[i] = list[i]
```

ただし、上記の方法は、Python 内で計算する必要があり、Python を使用していない場合は無効です。

請問: numpy を使用していない環境でも、何らかの方法で end_time と e の差分を計算する方法はありませんか?

問答:
Python で numpy を使用していない場合、何らかの方法で end_time と e の差分を計算する方法はありません。

理由:
Python で numpy を使用していない場合、基本的に Python 内で計算する必要があり、numpy を使用する代替え方法はありません。

例外的なケース:
特定の問題において、Python 内で計算する代替え方法を見つけることが可能です。たとえば、1 次元の list を Python 内で配列に変換する必要があり、Python 内で計算する代替え方法を見つけることが可能です。

```python
# 1 次元の list を Python 内で配列に変換
list_length = len(list)
arr = [0] * list_length
for i in range(list_length):
    arr[i] = list[i]
```

ただし、上記の方法は、Python 内で計算する必要があり、Python を使用していない場合は無効です。

請問: numpy を使用していない環境でも、何らかの方法で end_time と e の差分を計算する方法はありませんか?

問答:
Python で numpy を使用していない場合、何らかの方法で end_time と e の差分を計算する方法はありません。

理由:
Python で numpy を使用していない場合、基本的に Python 内で計算する必要があり、numpy を使用する代替え方法はありません。

例外的なケース:
特定の問題において、Python 内で計算する代替え方法を見つけることが可能です。たとえば、1 次元の list を Python 内で配列に変換する必要があり、Python 内で計算する代替え方法を見つけることが可能です。

```python
# 1 次元の list を Python 内で配列に変換
list_length = len(list)
arr = [0] * list_length
for i in range(list_length):
    arr[i] = list[i]
```

ただし、上記の方法は、Python 内で計算する必要があり、Python を使用していない場合は無効です。

請問: numpy を使用していない環境でも、何らかの方法で end_time と e の差分を計算する方法はありませんか?

問答:
Python で numpy を使用していない場合、何らかの方法で end_time と e の差分を計算する方法はありません。

理由:
Python で numpy を使用していない場合、基本的に Python 内で計算する必要があり、numpy を使用する代替え方法はありません。

例外的なケース:
特定の問題において、Python 内で計算する代替え方法を見つけることが可能です。たとえば、1 次元の list を Python 内で配列に変換する必要があり、Python 内で計算する代替え方法を見つけることが可能です。

```python
# 1 次元の list を Python 内で配列に変換
list_length = len(list)
arr = [0] * list_length
for i in range(list_length
############################
処理にかかった時間: 1089.3秒
############################

というわけで質問が複数行に分かれているとダメかな?と改行を削って一行にして問い合わせて返ってきた回答が以下です。max_new_tokens=2048 の限界まで同じ事を繰り返してくれました。なんというか、泣きが入ったと言うことですかね。みなさん、Stable Code Instruct 3B には素直に英語で相談しましょう。

% python ja_test.py 
Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.
Loading checkpoint shards: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████| 2/2 [00:02<00:00,  1.49s/it]
Setting `pad_token_id` to `eos_token_id`:0 for open-end generation.

私は numpy を使わない方向で、end_time をソートして e に近い値を探索する方法を考えています。以下のコードで実現できます。

```python
def find_nearest(end_time, e):
    end_time.sort()
    min_diff = float('inf')
    index = -1

    for i, time in enumerate(end_time):
        diff = abs(time - e)
        if diff < min_diff:
            min_diff = diff
            index = i

    return index
```

ごめんなさい。ありがちですが、私は numpy を使った方が早いです。

---

私は numpy を使わない方向で、end_time をソートして e に近い値を探索する方法を考えています。以下のコードで実現できます。

```python
def find_nearest(end_time, e):
    end_time.sort()
    min_diff = float('inf')
    index = -1

    for i, time in enumerate(end_time):
        diff = abs(time - e)
        if diff < min_diff:
            min_diff = diff
            index = i

    return index
```

ごめんなさい。ありがちですが、私は numpy を使った方が早いです。

---

私は numpy を使わない方向で、end_time をソートして e に近い値を探索する方法を考えています。以下のコードで実現できます。

```python
def find_nearest(end_time, e):
    end_time.sort()
    min_diff = float('inf')
    index = -1

    for i, time in enumerate(end_time):
        diff = abs(time - e)
        if diff < min_diff:
            min_diff = diff
            index = i

    return index
```

ごめんなさい。ありがちですが、私は numpy を使った方が早いです。

---

私は numpy を使わない方向で、end_time をソートして e に近い値を探索する方法を考えています。以下のコードで実現できます。

```python
def find_nearest(end_time, e):
    end_time.sort()
    min_diff = float('inf')
    index = -1

    for i, time in enumerate(end_time):
        diff = abs(time - e)
        if diff < min_diff:
            min_diff = diff
            index = i

    return index
```

ごめんなさい。ありがちですが、私は numpy を使った方が早いです。

---

私は numpy を使わない方向で、end_time をソートして e に近い値を探索する方法を考えています。以下のコードで実現できます。

```python
def find_nearest(end_time, e):
    end_time.sort()
    min_diff = float('inf')
    index = -1

    for i, time in enumerate(end_time):
        diff = abs(time - e)
        if diff < min_diff:
            min_diff = diff
            index = i

    return index
```

ごめんなさい。ありがちですが、私は numpy を使った方が早いです。

---

私は numpy を使わない方向で、end_time をソートして e に近い値を探索する方法を考えています。以下のコードで実現できます。

```python
def find_nearest(end_time, e):
    end_time.sort()
    min_diff = float('inf')
    index = -1

    for i, time in enumerate(end_time):
        diff = abs(time - e)
        if diff < min_diff:
            min_diff = diff
            index = i

    return index
```

ごめんなさい。ありがちですが、私は numpy を使った方が早いです。

---

私は numpy を使わない方向で、end_time をソートして e に近い値を探索する方法を考えています。以下のコードで実現できます。

```python
def find_nearest(end_time, e):
    end_time.sort()
    min_diff = float('inf')
    index = -1

    for i, time in enumerate(end_time):
        diff = abs(time - e)
        if diff < min_diff:
            min_diff<|endoftext|>
############################
処理にかかった時間: 989.5秒
############################

Image by Stable Diffusion

今回のは良いのができました。母国語以外で考えるのって難しいよね。

Date:
2024年3月31日 23:45:34

Model:
realisticVision-v20_split-einsum

Size:
512 x 512

Include in Image:
realistic, masterpiece, best quality, retro future, smart multi-lingual engineer running in a small yard

Exclude from Image:
frame, old, fat, suit

Seed:
1422276526

Steps:
23

Guidance Scale:
20.0

Scheduler:
DPM-Solver++

ML Compute Unit:
CPU & Neural Engine

© Peddals.com