11from i6_experiments .users .zeyer .external_models .huggingface import (
22 DownloadHuggingFaceRepoJob ,
33)
4- from .common import HF_CACHE_DIR
4+ from .common import HF_CACHE_DIR
5+ from pathlib import Path
6+ from sisyphus import Job , Task , tk
7+ import os
8+ import subprocess
9+ import json
10+ from i6_experiments .users .dorian_koch .jobs .hf import HfMergeShards
11+ import sys
12+ import moshi_finetune # needed to get moshi_finetune path for PYTHONPATH below
513
614# None of this is used anywhere i think
715
16+
817def download_moshi ():
918 # projects/moshi/moshi/moshi/models/loaders.py
1019 # untested code...
@@ -16,3 +25,173 @@ def download_moshi():
1625
1726def moshi_inference_server (model ):
1827 pass
28+
29+
30+ # runs moshi annotate.py
31+ class MoshiAnnotate (Job ):
32+ def __init__ (
33+ self ,
34+ * ,
35+ venv_python_path : tk .Path ,
36+ in_hf : tk .Path ,
37+ shard : int | None = None ,
38+ num_shards : int | None = None ,
39+ ):
40+ self .venv_python_path = venv_python_path
41+ self .in_hf = in_hf
42+ self .shard = shard
43+ self .num_shards = num_shards
44+ self .out_annotations = self .output_path ("annotations" , directory = True )
45+ self .rqmt = {
46+ "gpu" : 1 ,
47+ "cpu" : 6 ,
48+ "mem" : 8 ,
49+ "time" : 1 ,
50+ }
51+
52+ def tasks (self ):
53+ yield Task ("run" , rqmt = self .rqmt )
54+
55+ def run (self ):
56+ this_file_path = Path (__file__ ).resolve ()
57+ moshi_annotate_path = this_file_path .parent / "moshi_annotate_inference.py"
58+
59+ work_dir = os .path .join (os .getcwd (), "annotate_inference_workdir" )
60+ os .makedirs (work_dir , exist_ok = True )
61+
62+ command = [
63+ self .venv_python_path .get (),
64+ str (moshi_annotate_path ),
65+ "--in_hf" ,
66+ str (self .in_hf .get ()),
67+ ]
68+ # if self.out_dir is not None:
69+ # command += ["--out_dir", str(self.out_dir.get())]
70+ # else:
71+ command += ["--out_dir" , str (self .out_annotations .get ())]
72+ if self .shard is not None and self .num_shards is not None :
73+ command += [
74+ "--in_hf_shard" ,
75+ str (self .shard ),
76+ "--in_hf_num_shards" ,
77+ str (self .num_shards ),
78+ ]
79+ env = os .environ .copy ()
80+ env ["PYTHONUNBUFFERED" ] = "1"
81+ env ["HF_HOME" ] = HF_CACHE_DIR .get ()
82+ top_level_file = sys .modules ["moshi_finetune" ].__file__
83+ package_base_dir = str (Path (top_level_file ).parent .parent )
84+ env ["PYTHONPATH" ] = (
85+ f"{ package_base_dir } { os .pathsep } { env ['PYTHONPATH' ]} "
86+ if "PYTHONPATH" in env
87+ else package_base_dir
88+ )
89+
90+ print ("Env:" )
91+ for k , v in env .items ():
92+ if k not in ["PYTHONPATH" ]:
93+ continue
94+ print (f"{ k } : { v } " )
95+
96+ print (
97+ f"Running Moshi annotate with command: { ' ' .join (command )} " ,
98+ flush = True ,
99+ )
100+ print (f"Using HF cache directory: { HF_CACHE_DIR } " )
101+ subprocess .run (command , env = env , check = True )
102+
103+
104+ class MoshiFinetune (Job ):
105+ def __init__ (self , venv_python_path : tk .Path , train_data : tk .Path ):
106+ self .train_data = train_data
107+ self .venv_python_path = venv_python_path
108+ self .out_config = self .output_path ("config.yaml" )
109+ self .rqmt = {
110+ "gpu" : 1 ,
111+ "cpu" : 6 ,
112+ "mem" : 24 ,
113+ "time" : 1 ,
114+ }
115+
116+ def tasks (self ):
117+ yield Task ("write_config" , mini_task = True )
118+ yield Task ("run" , rqmt = self .rqmt )
119+
120+ def write_config (self ):
121+ run_dir = os .path .join (os .getcwd (), "run_dir/" )
122+ txt = f"""
123+ # data
124+ data:
125+ eval_data: '' # Optional Fill
126+ shuffle: true
127+ train_data: '{ self .train_data .get ()} ' # Fill
128+
129+ # model
130+ moshi_paths:
131+ hf_repo_id: "kyutai/moshiko-pytorch-bf16"
132+
133+ full_finetuning: false # Activate lora.enable if partial finetuning
134+ lora:
135+ enable: true # Set to False if full_finetuning is True
136+ rank: 128
137+ scaling: 2.
138+ ft_embed: false # Optional, set to True if you want to finetune the embedding layer
139+
140+ first_codebook_weight_multiplier: 100.
141+ text_padding_weight: .5
142+
143+ # optim
144+ duration_sec: 100
145+ batch_size: 16
146+ max_steps: 2000
147+ gradient_checkpointing: true
148+ optim:
149+ lr: 2e-6
150+ weight_decay: 0.1
151+ pct_start: 0.05
152+
153+ # other
154+ seed: 0
155+ log_freq: 1
156+ eval_freq: 100
157+ do_eval: false
158+ do_ckpt: true
159+ ckpt_freq: 100
160+
161+
162+ save_adapters: true # Must be False if full_finetuning is True
163+
164+ run_dir: "{ run_dir } " # Fill
165+ """
166+ with open (self .out_config , "w" ) as f :
167+ f .write (txt )
168+
169+ def run (self ):
170+ command = [
171+ self .venv_python_path .get (),
172+ "-m" ,
173+ "torch.distributed.run" ,
174+ "--nproc-per-node" ,
175+ str (self .rqmt ["gpu" ]),
176+ "-m" ,
177+ "moshi_finetune.train" ,
178+ self .out_config .get (),
179+ ]
180+
181+ env = os .environ .copy ()
182+ env ["PYTHONUNBUFFERED" ] = "1"
183+ env ["HF_HOME" ] = HF_CACHE_DIR .get ()
184+ top_level_file = sys .modules ["moshi_finetune" ].__file__
185+ package_base_dir = f"{ str (Path (top_level_file ).parent .parent )} { os .pathsep } { str (Path (top_level_file ).parent )} "
186+
187+ env ["PYTHONPATH" ] = (
188+ f"{ package_base_dir } { os .pathsep } { env ['PYTHONPATH' ]} "
189+ if "PYTHONPATH" in env
190+ else package_base_dir
191+ )
192+ print (
193+ f"Running Moshi training with command: { ' ' .join (command )} " ,
194+ flush = True ,
195+ )
196+ print (f"Using HF cache directory: { HF_CACHE_DIR } " )
197+ subprocess .run (command , env = env , check = True )
0 commit comments