iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🤗

Running Stockmark-100B Locally with MLX

に公開

Introduction

I tried playing around with Stockmark-100B on a local MacBook Pro using MLX.
Regarding the Instruct model, I haven't been able to get the Adapter working properly with MLX yet. If I find a solution, I'd like to write an additional article about it.

In this post, I'll explain the quantization process in MLX in some detail.

Environment

  • Apple M3 MAX (128GB)
    • The maximum Unified Memory consumption of the python process during inference was roughly as follows:
      • 4bit : 51GB
      • 8bit : 96GB
  • Python 3.10
    • Please note that it likely won't work with version 3.9 or earlier.

Libraries

I am using the following. Standard pip installations without specific versions work fine.

mlx_lm

https://github.com/ml-explore/mlx
https://huggingface.co/docs/hub/mlx

MLX is a machine learning framework (especially for Deep Learning) provided by Apple. By running models on this framework, you can leverage Unified Memory and the GPU for high-speed training and inference. mlx_lm allows you to run Hugging Face LLMs using MLX.

Model Used

Stockmark-100B

https://stockmark.co.jp/news/20240516
https://huggingface.co/stockmark/stockmark-100b-instruct-v0.1
This is a 100B model developed from scratch by Stockmark, which reportedly significantly reduces hallucinations.

Since it was just released, the model wasn't registered in the MLX-community yet, so I performed a full quantization and registered it myself. Please feel free to use it.
https://huggingface.co/mlx-community/stockmark-100b-4bit
https://huggingface.co/mlx-community/stockmark-100b-8bit

Running Quantized Models via Command Line

Let's play around with the 4-bit quantized model. Activate the virtual environment where mlx_lm is installed, and the model will run by executing the following code.
Enter the following in the terminal to run the model.

Terminal Input
python -m mlx_lm.generate --model mlx-community/stockmark-100b-4bit --prompt “生成AIとは?” --temp 0.0 --max-tokens 500

The response is as follows. Since repetition_penalty cannot be applied with mlx_lm.generate, it starts repeating the same answer halfway through. However, the content itself doesn't seem to be incorrect, and it provides smooth sentences, so I can feel its strong basic performance within the scope of my testing.

Output
==========
Prompt: “生成AIとは?”

“生成AIとは?”
生成AIとは、人工知能の一種で、新しいデータやコンテンツを生成することができる技術です。
生成AIは、機械学習やディープラーニングなどの技術を用いて、大量のデータから学習し、新しいデータやコンテンツを生成することができます。
生成AIは、文章、画像、音楽、動画、プログラムコードなど、様々な種類のデータを生成することができます。
生成AIは、ビジネス、教育、エンターテイメントなど、様々な分野で活用されています。
例えば、生成AIは、文章生成AIを使って、文章を生成することができます。
また、画像生成AIを使って、画像を生成することができます。
さらに、音楽生成AIを使って、音楽を生成することができます。
生成AIは、ビジネスにおいて、顧客対応やマーケティング、データ分析などに活用されています。
例えば、生成AIは、顧客対応において、顧客の問い合わせやクレームに対して、自動的に回答することができます。
また、マーケティングにおいて、生成AIは、顧客の嗜好や行動を分析し、最適なマーケティング戦略を提案することができます。
さらに、データ分析において、生成AIは、大量のデータを分析し、新しい知見を得ることができます。
生成AIは、ビジネスにおいて、様々な分野で活用されています。
生成AIは、ビジネスにおいて、顧客対応やマーケティング、データ分析などに活用されています。
生成AIは、ビジネスにおいて、顧客対応やマーケティング、データ分析などに活用されています。生成AIは、ビジネスにおいて、顧客対応やマーケティング、データ分析などに活用されています。
生成AIは、ビジネスにおいて、顧客対応やマーケティング、データ分析などに活用されています。生成AIは、ビジネスにおいて、顧客対応やマーケティング、データ分析などに活用されています。生成AIは、ビジネスにおいて、顧客対応やマーケティング、データ分析などに活用されています。生成AIは、ビジネスにおいて、顧客対応やマーケティング、データ分析などに活用されています。生成AIは、ビジネスにおいて、顧客対応やマーケティング、データ分析などに活用されています。生成AIは、ビジネスにおいて、顧客対応やマーケティング、データ分析などに活用されています。生成AIは、ビジネスにおいて、顧客対応やマーケティング、データ分析などに活用されています。生成AIは、ビジネスにおいて、顧客対応やマーケティング、データ分析などに活用されています。生成AIは、ビジネスにおいて、顧客対応やマーケティング、データ分析などに活用されています。生成AIは、ビジネスにおいて、顧客対応やマーケティング、データ分析などに活用されています。生成AIは、ビジネスにおいて、顧客対応やマーケティング、データ分析などに活用されています。生成AIは、ビジネスにおいて、顧客対応やマーケティング、データ分析などに活用されています。生成AIは、ビジネスにおいて、顧客対応やマーケティング、データ分析などに活用されています。生成AI
==========
Prompt: 5.910 tokens-per-sec
Generation: 5.311 tokens-per-sec

How to Quantize (External Storage Edition)

When performing quantization, you need to download the pre-quantized model once. For example, the Stockmark-100B model I quantized this time is approximately 250GB in size.

By default, Hugging Face models are saved in a hidden folder directly under the user directory:

/.cache/huggingface/hub

In most configurations, the boot drive capacity is not that large, and it is common not to have hundreds of gigabytes of free space. In such cases, you can specify the location of the hub folder by entering the following in the terminal:

$ export HF_HUB_CACHE="(folder_name)"

Note that the above setting will revert when you restart the terminal. If you want to revert it without restarting, enter the following in the terminal:

$ unset HF_HUB_CACHE

After specifying the hub directory as described above, you can download the model, perform quantization, and upload it to the community by running the following command (there is a snippet on the mlx-community top page):

Terminal Input
python -m mlx_lm.convert --hf-path [Hugging Face path of the model to quantize]  -q --q-bits [number of quantization bits] --upload-repo [Hugging Face path for uploading the quantized model]

For example, the execution command for the 8-bit quantization I uploaded this time is as follows:

Terminal Input
python -m mlx_lm.convert --hf-path stockmark/stockmark-100b --q-bits 8 -q --upload-repo mlx-community/stockmark-100b-8bit

If the model you want to use is not available, please try quantizing it yourself by referring to the steps above!

About the Instruct Model

The stockmark-100b-instruct-v0.1 model is provided in the "adapter" format. Normally, in this case, both the adapter and the base model are specified and used, making it a format that reduces unnecessary downloads. However, it appears that the adapter format is unfortunately not supported for MLX models.

I am currently investigating how to handle this and will share any progress I make.
Also, if anyone has any information, I would be very grateful if you could let me know.

Discussion