目次
作業内容
大規模言語モデルについて動作検証を行うため、Command-R+(4bit)を導入する
参考:https://zenn.dev/romot/articles/d2cbe75743eebe
手順1:事前準備
1. OS確認、メモリ容量確認
ターミナルから下記のコマンド操作を行う
taiyo@mac-studio ~ % sw_vers
ProductName: macOS
ProductVersion: 14.4.1
BuildVersion: 23E224
taiyo@mac-studio ~ % top -l 1 | grep PhysMem
PhysMem: 9480M used (1685M wired, 0B compressor), 118G unused.
2. pip更新
ターミナルから下記のコマンド操作を行う
taiyo@mac-studio ~ % python3 -m pip install --upgrade pip
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: pip in /Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/site-packages (21.2.4)
Collecting pip
Downloading pip-24.0-py3-none-any.whl (2.1 MB)
|████████████████████████████████| 2.1 MB 5.5 MB/s
Installing collected packages: pip
WARNING: The scripts pip, pip3, pip3.10 and pip3.9 are installed in '/Users/taiyo/Library/Python/3.9/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed pip-24.0
WARNING: You are using pip version 21.2.4; however, version 24.0 is available.
You should consider upgrading via the '/Library/Developer/CommandLineTools/usr/bin/python3 -m pip install --upgrade pip' command.
手順2:Command-R+(4bit)の設定
1. mlx-lmをインストール
ターミナルから下記のコマンド操作を行う
taiyo@mac-studio ~ % pip3 install mlx-lm
Defaulting to user installation because normal site-packages is not writeable
Collecting mlx-lm
Downloading mlx_lm-0.12.1-py3-none-any.whl.metadata (5.0 kB)
Collecting mlx>=0.11 (from mlx-lm)
Downloading mlx-0.12.2-cp39-cp39-macosx_14_0_arm64.whl.metadata (4.9 kB)
Collecting numpy (from mlx-lm)
Downloading numpy-1.26.4-cp39-cp39-macosx_11_0_arm64.whl.metadata (61 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 61.1/61.1 kB 3.0 MB/s eta 0:00:00
Collecting transformers>=4.39.3 (from mlx-lm)
Downloading transformers-4.40.1-py3-none-any.whl.metadata (137 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 138.0/138.0 kB 6.2 MB/s eta 0:00:00
Collecting protobuf (from mlx-lm)
Downloading protobuf-5.26.1-cp37-abi3-macosx_10_9_universal2.whl.metadata (592 bytes)
Collecting pyyaml (from mlx-lm)
Downloading PyYAML-6.0.1-cp39-cp39-macosx_11_0_arm64.whl.metadata (2.1 kB)
Collecting jinja2 (from mlx-lm)
Downloading Jinja2-3.1.3-py3-none-any.whl.metadata (3.3 kB)
Collecting filelock (from transformers>=4.39.3->mlx-lm)
Downloading filelock-3.14.0-py3-none-any.whl.metadata (2.8 kB)
Collecting huggingface-hub<1.0,>=0.19.3 (from transformers>=4.39.3->mlx-lm)
Downloading huggingface_hub-0.23.0-py3-none-any.whl.metadata (12 kB)
Collecting packaging>=20.0 (from transformers>=4.39.3->mlx-lm)
Downloading packaging-24.0-py3-none-any.whl.metadata (3.2 kB)
Collecting regex!=2019.12.17 (from transformers>=4.39.3->mlx-lm)
Downloading regex-2024.4.28-cp39-cp39-macosx_11_0_arm64.whl.metadata (40 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 40.8/40.8 kB 2.5 MB/s eta 0:00:00
Collecting requests (from transformers>=4.39.3->mlx-lm)
Downloading requests-2.31.0-py3-none-any.whl.metadata (4.6 kB)
Collecting tokenizers<0.20,>=0.19 (from transformers>=4.39.3->mlx-lm)
Downloading tokenizers-0.19.1-cp39-cp39-macosx_11_0_arm64.whl.metadata (6.7 kB)
Collecting safetensors>=0.4.1 (from transformers>=4.39.3->mlx-lm)
Downloading safetensors-0.4.3-cp39-cp39-macosx_11_0_arm64.whl.metadata (3.8 kB)
Collecting tqdm>=4.27 (from transformers>=4.39.3->mlx-lm)
Downloading tqdm-4.66.4-py3-none-any.whl.metadata (57 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 57.6/57.6 kB 4.1 MB/s eta 0:00:00
Collecting MarkupSafe>=2.0 (from jinja2->mlx-lm)
Downloading MarkupSafe-2.1.5-cp39-cp39-macosx_10_9_universal2.whl.metadata (3.0 kB)
Collecting fsspec>=2023.5.0 (from huggingface-hub<1.0,>=0.19.3->transformers>=4.39.3->mlx-lm)
Downloading fsspec-2024.3.1-py3-none-any.whl.metadata (6.8 kB)
Collecting typing-extensions>=3.7.4.3 (from huggingface-hub<1.0,>=0.19.3->transformers>=4.39.3->mlx-lm)
Downloading typing_extensions-4.11.0-py3-none-any.whl.metadata (3.0 kB)
Collecting charset-normalizer<4,>=2 (from requests->transformers>=4.39.3->mlx-lm)
Downloading charset_normalizer-3.3.2-cp39-cp39-macosx_11_0_arm64.whl.metadata (33 kB)
Collecting idna<4,>=2.5 (from requests->transformers>=4.39.3->mlx-lm)
Downloading idna-3.7-py3-none-any.whl.metadata (9.9 kB)
Collecting urllib3<3,>=1.21.1 (from requests->transformers>=4.39.3->mlx-lm)
Downloading urllib3-2.2.1-py3-none-any.whl.metadata (6.4 kB)
Collecting certifi>=2017.4.17 (from requests->transformers>=4.39.3->mlx-lm)
Downloading certifi-2024.2.2-py3-none-any.whl.metadata (2.2 kB)
Downloading mlx_lm-0.12.1-py3-none-any.whl (68 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 68.8/68.8 kB 4.5 MB/s eta 0:00:00
Downloading mlx-0.12.2-cp39-cp39-macosx_14_0_arm64.whl (35.0 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 35.0/35.0 MB 10.0 MB/s eta 0:00:00
Downloading transformers-4.40.1-py3-none-any.whl (9.0 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 9.0/9.0 MB 7.7 MB/s eta 0:00:00
Downloading numpy-1.26.4-cp39-cp39-macosx_11_0_arm64.whl (14.0 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 14.0/14.0 MB 10.1 MB/s eta 0:00:00
Downloading PyYAML-6.0.1-cp39-cp39-macosx_11_0_arm64.whl (174 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 174.4/174.4 kB 7.6 MB/s eta 0:00:00
Downloading Jinja2-3.1.3-py3-none-any.whl (133 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 133.2/133.2 kB 6.5 MB/s eta 0:00:00
Downloading protobuf-5.26.1-cp37-abi3-macosx_10_9_universal2.whl (404 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 404.0/404.0 kB 8.1 MB/s eta 0:00:00
Downloading huggingface_hub-0.23.0-py3-none-any.whl (401 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 401.2/401.2 kB 9.5 MB/s eta 0:00:00
Downloading MarkupSafe-2.1.5-cp39-cp39-macosx_10_9_universal2.whl (18 kB)
Downloading packaging-24.0-py3-none-any.whl (53 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 53.5/53.5 kB 3.5 MB/s eta 0:00:00
Downloading regex-2024.4.28-cp39-cp39-macosx_11_0_arm64.whl (278 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 278.3/278.3 kB 9.0 MB/s eta 0:00:00
Downloading safetensors-0.4.3-cp39-cp39-macosx_11_0_arm64.whl (411 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 411.3/411.3 kB 8.3 MB/s eta 0:00:00
Downloading tokenizers-0.19.1-cp39-cp39-macosx_11_0_arm64.whl (2.4 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.4/2.4 MB 10.2 MB/s eta 0:00:00
Downloading tqdm-4.66.4-py3-none-any.whl (78 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 78.3/78.3 kB 4.8 MB/s eta 0:00:00
Downloading filelock-3.14.0-py3-none-any.whl (12 kB)
Downloading requests-2.31.0-py3-none-any.whl (62 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 62.6/62.6 kB 4.2 MB/s eta 0:00:00
Downloading certifi-2024.2.2-py3-none-any.whl (163 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 163.8/163.8 kB 6.6 MB/s eta 0:00:00
Downloading charset_normalizer-3.3.2-cp39-cp39-macosx_11_0_arm64.whl (120 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 120.4/120.4 kB 6.8 MB/s eta 0:00:00
Downloading fsspec-2024.3.1-py3-none-any.whl (171 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 172.0/172.0 kB 7.2 MB/s eta 0:00:00
Downloading idna-3.7-py3-none-any.whl (66 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 66.8/66.8 kB 4.9 MB/s eta 0:00:00
Downloading typing_extensions-4.11.0-py3-none-any.whl (34 kB)
Downloading urllib3-2.2.1-py3-none-any.whl (121 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 121.1/121.1 kB 6.6 MB/s eta 0:00:00
Installing collected packages: urllib3, typing-extensions, tqdm, safetensors, regex, pyyaml, protobuf, packaging, numpy, mlx, MarkupSafe, idna, fsspec, filelock, charset-normalizer, certifi, requests, jinja2, huggingface-hub, tokenizers, transformers, mlx-lm
WARNING: The script tqdm is installed in '/Users/taiyo/Library/Python/3.9/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
WARNING: The script f2py is installed in '/Users/taiyo/Library/Python/3.9/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
WARNING: The script normalizer is installed in '/Users/taiyo/Library/Python/3.9/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
WARNING: The script huggingface-cli is installed in '/Users/taiyo/Library/Python/3.9/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
WARNING: The script transformers-cli is installed in '/Users/taiyo/Library/Python/3.9/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
WARNING: The scripts mlx_lm.convert, mlx_lm.fuse, mlx_lm.generate, mlx_lm.lora, mlx_lm.merge and mlx_lm.server are installed in '/Users/taiyo/Library/Python/3.9/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed MarkupSafe-2.1.5 certifi-2024.2.2 charset-normalizer-3.3.2 filelock-3.14.0 fsspec-2024.3.1 huggingface-hub-0.23.0 idna-3.7 jinja2-3.1.3 mlx-0.12.2 mlx-lm-0.12.1 numpy-1.26.4 packaging-24.0 protobuf-5.26.1 pyyaml-6.0.1 regex-2024.4.28 requests-2.31.0 safetensors-0.4.3 tokenizers-0.19.1 tqdm-4.66.4 transformers-4.40.1 typing-extensions-4.11.0 urllib3-2.2.1
2. PATH環境変数を更新
ターミナルから下記のコマンド操作を行う
taiyo@mac-studio ~ % vim .zprofile
taiyo@mac-studio ~ % cat .zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"
export PATH="/Users/taiyo/Library/Python/3.9/bin:$PATH"
taiyo@mac-studio ~ % export PATH="/Users/taiyo/Library/Python/3.9/bin:$PATH"
taiyo@mac-studio ~ % pip --version
pip 24.0 from /Users/taiyo/Library/Python/3.9/lib/python/site-packages/pip (python 3.9)
3. Command-R+(4bit)の実行スクリプトを作成
ターミナルから下記のコマンド操作を行う
taiyo@mac-studio ~ % mkdir mlx-lm
taiyo@mac-studio ~ % vim mlx-lm/c4ai-command-r-v01-4bit.py
taiyo@mac-studio ~ % cat mlx-lm/c4ai-command-r-v01-4bit.py
import argparse
from mlx_lm import load, generate
def main():
parser = argparse.ArgumentParser(description='Generate text using Command-R(MLX LM)')
parser.add_argument('prompt', type=str, help='Input prompt for text generation')
args = parser.parse_args()
model, tokenizer = load("mlx-community/c4ai-command-r-v01-4bit")
prompt = []
prompt.append({'role': 'user', 'content': args.prompt})
inputs = tokenizer.apply_chat_template(prompt,
tokenize=False,
add_generation_prompt=True)
response = generate(
model,
tokenizer,
prompt=inputs,
verbose=True,
temp=0.2,
max_tokens=12800,
)
if __name__ == "__main__":
main()
4. Command-R+(4bit)をインストール
ターミナルから下記のコマンド操作を行う
taiyo@mac-studio ~ % python3 mlx-lm/c4ai-command-r-v01-4bit.py "こんにちは!元気ですか?"
/Users/taiyo/Library/Python/3.9/lib/python/site-packages/urllib3/__init__.py:35: NotOpenSSLWarning: urllib3 v2 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'LibreSSL 2.8.3'. See: https://github.com/urllib3/urllib3/issues/3020
warnings.warn(
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.
/Users/taiyo/Library/Python/3.9/lib/python/site-packages/huggingface_hub/file_download.py:1132: FutureWarning: `resume_download` is deprecated and will be removed in version 1.0.0. Downloads always resume when possible. If you want to force a new download, use `force_download=True`.
warnings.warn(
config.json: 100%|██████████████████████████████████████████████████████████████████| 2.60k/2.60k [00:00<00:00, 678kB/s]
model.safetensors.index.json: 100%|█████████████████████████████████████████████████| 75.2k/75.2k [00:00<00:00, 465kB/s]
configuration_cohere.py: 100%|█████████████████████████████████████████████████████| 7.37k/7.37k [00:00<00:00, 2.08MB/s]
special_tokens_map.json: 100%|██████████████████████████████████████████████████████████| 439/439 [00:00<00:00, 127kB/s]
tokenization_cohere_fast.py: 100%|█████████████████████████████████████████████████| 43.7k/43.7k [00:00<00:00, 1.74MB/s]
modeling_cohere.py: 100%|██████████████████████████████████████████████████████████| 60.6k/60.6k [00:00<00:00, 65.9kB/s]
tokenizer_config.json: 100%|███████████████████████████████████████████████████████| 8.19k/8.19k [00:00<00:00, 5.28MB/s]
tokenizer.json: 100%|██████████████████████████████████████████████████████████████| 12.8M/12.8M [00:04<00:00, 2.71MB/s]
model-00005-of-00005.safetensors: 100%|████████████████████████████████████████████| 1.31G/1.31G [10:56<00:00, 2.00MB/s]
model-00001-of-00005.safetensors: 100%|████████████████████████████████████████████| 5.37G/5.37G [32:38<00:00, 2.74MB/s]
model-00004-of-00005.safetensors: 100%|████████████████████████████████████████████| 5.37G/5.37G [32:40<00:00, 2.74MB/s]
model-00003-of-00005.safetensors: 100%|████████████████████████████████████████████| 5.30G/5.30G [33:19<00:00, 2.65MB/s]
model-00002-of-00005.safetensors: 100%|████████████████████████████████████████████| 5.34G/5.34G [33:41<00:00, 2.64MB/s]
Fetching 13 files: 100%|███████████████████████████████████████████████████████████████| 13/13 [33:42<00:00, 155.59s/it]
Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained./s]
==========
Prompt: <BOS_TOKEN><|START_OF_TURN_TOKEN|><|USER_TOKEN|>こんにちは!元気ですか?<|END_OF_TURN_TOKEN|>17<00:51, 5.09MB/s]
<|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>██████████████████████████████████████████▉| 5.34G/5.34G [33:41<00:00, 11.4MB/s]
はい、元気です!おっしゃる通りですね。日本語でお話しできることを光栄に思います。どのようなお手伝いも喜んでさせていただきますので、お気軽にご依頼ください。
==========
Prompt: 20.713 tokens-per-sec
Generation: 23.035 tokens-per-sec
手順3:動作確認
1. 『mpeg4フォーマットの動画ファイルから0.1秒毎にフレーム画像をjpeg形式ファイルで出力するスクリプトを作成してください。』
ターミナルから下記のコマンド操作を行う
taiyo@mac-studio ~ % python3 mlx-lm/c4ai-command-r-v01-4bit.py "mpeg4フォーマットの動画ファイルから0.1秒毎にフレーム画像をjpeg形式ファイルで出力するスクリプトを作成してください。"
/Users/taiyo/Library/Python/3.9/lib/python/site-packages/urllib3/__init__.py:35: NotOpenSSLWarning: urllib3 v2 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'LibreSSL 2.8.3'. See: https://github.com/urllib3/urllib3/issues/3020
warnings.warn(
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.
/Users/taiyo/Library/Python/3.9/lib/python/site-packages/huggingface_hub/file_download.py:1132: FutureWarning: `resume_download` is deprecated and will be removed in version 1.0.0. Downloads always resume when possible. If you want to force a new download, use `force_download=True`.
warnings.warn(
Fetching 13 files: 100%|████████████████████████████████████████████████████████████| 13/13 [00:00<00:00, 158505.67it/s]
Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.
==========
Prompt: <BOS_TOKEN><|START_OF_TURN_TOKEN|><|USER_TOKEN|>mpeg4フォーマットの動画ファイルから0.1秒毎にフレーム画像をjpeg形式ファイルで出力するスクリプトを作成してください。<|END_OF_TURN_TOKEN|>
<|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>
以下に、ffmpegというツールを使用して、MPEG-4形式の動画ファイルから0.1秒ごとにフレーム画像を出力するバッチファイルの例を示します。
@echo off
REM 入力動画ファイルを指定
set input="C:\path\to\your\video.mp4"
REM 出力先フォルダを指定
set output="C:\output\folder\"
REM ffmpegを実行して、0.1秒ごとのフレーム画像を出力
ffmpeg -i "%input%" -vf "select=not(mod(n\100))" "%output%frame-%03d.jpg"
このバッチファイルを実行すると、入力動画ファイルから0.1秒おきにフレーム画像が"C:\output\folder\"フォルダに"frame-001.jpg"、"frame-002.jpg"というように出力されます。
"C:\path\to\your\video.mp4"の部分には、実際の動画ファイルのパスを指定してください。
また、ffmpegのインストール先が標準のパスにない場合は、ffmpegの実行ファイルがある場所を指定する必要があります。その場合は、最初の行の@echo offの下に、以下の行を追加してください。
REM ffmpegのパスを指定
set ffmpeg_path="C:\path\to\ffmpeg\ffmpeg.exe"
"%ffmpeg_path%" -i ... として、ffmpegの実行ファイルへのパスを指定します。
このバッチファイルを実行するには、ファイル名を例えば"extract_frames.bat"として、コマンドプロンプトから"extract_frames.bat"と入力して実行してください。
==========
Prompt: 73.199 tokens-per-sec
Generation: 21.973 tokens-per-sec
2. 『整数型変数のみを使用して平方根を計算するC言語の関数プログラムを作成してください。』
ターミナルから下記のコマンド操作を行う
taiyo@mac-studio ~ % python3 mlx-lm/c4ai-command-r-v01-4bit.py "整数型変数のみを使用して平方根を計算するC言語の関数プログラムを作成してください。"
/Users/taiyo/Library/Python/3.9/lib/python/site-packages/urllib3/__init__.py:35: NotOpenSSLWarning: urllib3 v2 only supports OpenSSL 1.1.1+, currently the 'ssl' module is compiled with 'LibreSSL 2.8.3'. See: https://github.com/urllib3/urllib3/issues/3020
warnings.warn(
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.
/Users/taiyo/Library/Python/3.9/lib/python/site-packages/huggingface_hub/file_download.py:1132: FutureWarning: `resume_download` is deprecated and will be removed in version 1.0.0. Downloads always resume when possible. If you want to force a new download, use `force_download=True`.
warnings.warn(
Fetching 13 files: 100%|████████████████████████████████████████████████████████████| 13/13 [00:00<00:00, 102108.52it/s]
Special tokens have been added in the vocabulary, make sure the associated word embeddings are fine-tuned or trained.
==========
Prompt: <BOS_TOKEN><|START_OF_TURN_TOKEN|><|USER_TOKEN|>整数型変数のみを使用して平方根を計算するC言語の関数プログラムを作成してください。<|END_OF_TURN_TOKEN|>
<|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>
C言語で整数型変数のみを使用して平方根を計算する関数を作成するには、二分探索法のような近似法を使用する必要があります。 ここでは、与えられた整数値の平方根を計算する関数を持つプログラムを示します。
```c
#include <stdio.h>
int int_sqrt(int num) {
if (num == 0 || num == 1) {
return num;
}
int left = 1;
int right = num;
int ans;
while (left <= right) {
int mid = (left + right) / 2;
int square = mid * mid;
if (square == num) {
return mid;
} else if (square < num) {
left = mid + 1;
} else {
right = mid - 1;
}
}
// 整数部分の平方根は右に若干小さい値となる
ans = right;
return ans;
}
int main() {
int num;
printf("入力を入力してください: ");
scanf("%d", &num);
int result = int_sqrt(num);
printf("平方根: %d\n", result);
return 0;
}
```
このプログラムは、与えられた数値の整数部分の平方根を近似的に計算する `int_sqrt` 関数を定義しています。 この関数は、二分探索法を使用して、与えられた数値の平方が与えられた数に最も近い整数値を見つけようとします。 main関数では、ユーザーからの入力値のためにこの関数を呼び出し、結果を出力します。
ただし、浮動小数点数を使用せずに正確な平方根を計算することはできません。このアプローチは近似値を提供しますが、実際の数値の平方根には若干の誤差が生じます。
==========
Prompt: 83.797 tokens-per-sec
Generation: 22.232 tokens-per-sec