1
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?

singularityによるms-swiftを活用したmegatronのモデルloraマージとHggingface形式に変換

1
Last updated at Posted at 2025-11-06

1.概要

松尾研LLMコンペ2025にPhase2としてRAMENチームで開発に取り組んできました。

以前、記載したsingularityで表題のモデルマージとHggingface形式変換して、評価を実施してきました。

このスクリプトによりモデル作業の効率化することが出来ました。

本記事は、megatronで学習した際にHggingface形式に戻す際に参考になればと考えています。

singularityについては、以下の記事を参照して下さい。

2.ms-swiftとmegatronとは

ms-swiftとは、中国のアリババ社が開発している事後学習用のフレームワークです。

megatronとは、Megatron-LMでTransformerモデルの学習を効率化するフレームワークです。

2つのフレームワークを活用することにより、事後学習をより高速に学習が可能となります。

詳細は、以下を参照して下さい。

3.変換手順

3.1Hggingface形式からmegatron形式へ変換方法

※ファイル名をmegatron_mcore_export.shとする

!/bin/bash
CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7 

ulimit -s unlimited
ulimit -v unlimited
ulimit -n 65536
ulimit -u 32768

export MODEL_NAME=Qwen3-235B-A22B
export CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7

# megatronのモデルに変換する
numactl --interleave=all \
swift export \
    --model /home/xxxxxxxxx/xxxxx/xxxxxxx/models/${MODEL_NAME} \    #変換したいモデルパス
    --model_type qwen3_moe \                       #変換したいモデル形式 ここではqwen3_moe 
    --to_mcore true \
    --torch_dtype bfloat16 \
    --output_dir ${MODEL_NAME}-mcore \               #変換したいモデル形式 ここではPhi-4

singularityで実行する方法

# nodeに入る
NODE="ノード番号"; srun --partition xxx --nodelist=$NODE --gpus-per-node=1 --time=12:00:00 --pty bash -i

singularity shell -w --nv -B /home {singularity環境フルパス}
cd ソースコードのパス
bash megatron_mcore_export.sh

singularity環境については、以下を参照ください。

3.2フルパラで学習したmegatron形式のモデルをHggingface形式へ変換方法

※ファイル名をmerge_megatron_full_hf.shとする

!/bin/bash
ulimit -s unlimited
ulimit -v unlimited
ulimit -n 65536
ulimit -u 32768

export CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7

# fullの場合
numactl --interleave=all \
swift export \
    --mcore_model /home/xxxxxxxxx/xxxxx/xxxxxxx/models/Qwen3-235B-mcore/v3-xxxxxxxx-xxxxxx \  #学習したフルパラのモデルパス
    --to_hf true \
    --torch_dtype bfloat16 \
    --output_dir ./hf_output/Qwen3-235B_xxxxxxxxx/ \ #変換先のパス

singularityで実行する方法

# nodeに入る
NODE="ノード番号"; srun --partition xxx --nodelist=$NODE --gpus-per-node=1 --time=12:00:00 --pty bash -i

singularity shell -w --nv -B /home {singularity環境フルパス}
cd ソースコードのパス
bash merge_megatron_full_hf.sh

3.3loraで学習したmegatron形式のモデルをHggingface形式へ変換方法

※ファイル名をmerge_megatron_lora_hf.shとする

!/bin/bash
ulimit -s unlimited
ulimit -v unlimited
ulimit -n 65536
ulimit -u 32768

export CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7

# LoRAの場合
 numactl --interleave=all \
 swift export \
     --mcore_model /home/xxxxxxx/xxxx/xxx/xxxx/Qwen3-235B-mcore/ \ #学習前のモデルパス
     --mcore_adapters /home/xxxxxxx/xxxx/xxx/xxxx/models/Qwen3-235B-mcore/v2-xxxxxxxx-xxxxxx \ #学習したloraのモデルパス
     --to_hf true \
     --torch_dtype bfloat16 \
     --output_dir ./hf_output/output_Qwen3-235B-lora-xxxxxxxxxx/ \ #変換先のパス

singularityで実行する方法

# nodeに入る
NODE="ノード番号"; srun --partition xxx --nodelist=$NODE --gpus-per-node=1 --time=12:00:00 --pty bash -i

singularity shell -w --nv -B /home {singularity環境フルパス}
cd ソースコードのパス
bash merge_megatron_lora_hf.sh

4.まとめ

singularityを使って、ms-swiftのフレームワークで様々な形式に変換することが出来ました。

これにより、事後学習が時間がかかっている部分が円滑に学習出来ることも学ぶ事が出来ました。

プロジェクトのクレジット

本プロジェクトは、国立研究開発法人新エネルギー・産業技術開発機構(NEDO)の

「日本語版医療特化型LLMの社会実装に向けた安全性検証・実証」における

基盤モデルの開発プロジェクトの一環として行われました。

1
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
1
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?