MCPcopy Create free account
hub / github.com/OpenBMB/AgentCPM-GUI / train

Function train

sft/finetune.py:178–307  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

176
177
178def train():
179 global local_rank
180 parser = transformers.HfArgumentParser(
181 (ModelArguments, DataArguments, TrainingArguments, LoraArguments)
182 )
183
184 (
185 model_args,
186 data_args,
187 training_args,
188 lora_args,
189 ) = parser.parse_args_into_dataclasses()
190
191 if getattr(training_args, "deepspeed", None) :
192 training_args.distributed_state.distributed_type = DistributedType.DEEPSPEED
193
194 compute_dtype = (
195 torch.float16
196 if training_args.fp16
197 else (torch.bfloat16 if training_args.bf16 else torch.float32)
198 )
199
200 local_rank = training_args.local_rank
201 world_size = int(os.environ.get("WORLD_SIZE", 1))
202 ddp = world_size != 1
203 device_map = None
204 if lora_args.q_lora:
205 device_map = {"": int(os.environ.get("LOCAL_RANK") or 0)} if ddp else None
206 if len(training_args.fsdp) > 0 or deepspeed.is_deepspeed_zero3_enabled():
207 logging.warning(
208 "FSDP or ZeRO3 are not incompatible with QLoRA."
209 )
210
211 model = AutoModel.from_pretrained(
212 model_args.model_name_or_path,
213 trust_remote_code=True,
214 torch_dtype=compute_dtype,
215 device_map=device_map,
216 _attn_implementation="flash_attention_2"
217 )
218
219 tokenizer = AutoTokenizer.from_pretrained(
220 model_args.model_name_or_path, trust_remote_code=True
221 )
222
223 if not training_args.tune_vision:
224 model.vpm.requires_grad_(False)
225 if not training_args.tune_llm:
226 model.llm.requires_grad_(False)
227
228 if training_args.use_lora:
229 if training_args.use_lora and training_args.tune_llm:
230 raise ValueError("The model cannot simultaneously adjust LLM parameters and apply LoRA.")
231
232 rank0_print("Currently using LoRA for fine-tuning the MiniCPM-V model.")
233 for name, param in model.llm.named_parameters():
234 param.requires_grad = False
235 modules_to_save = ['embed_tokens','resampler']

Callers 1

finetune.pyFile · 0.85

Calls 6

CPMTrainerClass · 0.90
rank0_printFunction · 0.85
get_parameter_numberFunction · 0.85
build_transformFunction · 0.85

Tested by

no test coverage detected