Scropo commited on
Commit
53d6bfb
·
verified ·
1 Parent(s): c734022

Create sft.py

Browse files
Files changed (1) hide show
  1. sft.py +83 -0
sft.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2020-2025 The HuggingFace Team. All rights reserved.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ from datasets import load_dataset
16
+ from transformers import AutoModelForCausalLM, AutoTokenizer, Mxfp4Config
17
+
18
+ from trl import (
19
+ ModelConfig,
20
+ ScriptArguments,
21
+ SFTConfig,
22
+ SFTTrainer,
23
+ TrlParser,
24
+ get_peft_config,
25
+ )
26
+
27
+
28
+ def main(script_args, training_args, model_args):
29
+ # ------------------------
30
+ # Load model & tokenizer
31
+ # ------------------------
32
+ quantization_config = Mxfp4Config(dequantize=True)
33
+ model_kwargs = dict(
34
+ revision=model_args.model_revision,
35
+ trust_remote_code=model_args.trust_remote_code,
36
+ attn_implementation=model_args.attn_implementation,
37
+ torch_dtype=model_args.torch_dtype,
38
+ use_cache=False if training_args.gradient_checkpointing else True,
39
+ quantization_config=quantization_config,
40
+ )
41
+
42
+ model = AutoModelForCausalLM.from_pretrained(
43
+ model_args.model_name_or_path, **model_kwargs
44
+ )
45
+
46
+ tokenizer = AutoTokenizer.from_pretrained(
47
+ model_args.model_name_or_path,
48
+ )
49
+
50
+ # --------------
51
+ # Load dataset
52
+ # --------------
53
+ dataset = load_dataset(script_args.dataset_name, name=script_args.dataset_config)
54
+
55
+ # -------------
56
+ # Train model
57
+ # -------------
58
+ trainer = SFTTrainer(
59
+ model=model,
60
+ args=training_args,
61
+ train_dataset=dataset[script_args.dataset_train_split],
62
+ eval_dataset=dataset[script_args.dataset_test_split]
63
+ if training_args.eval_strategy != "no"
64
+ else None,
65
+ # You had `processing_class` here, but SFTTrainer expects `tokenizer`
66
+ tokenizer=tokenizer,
67
+ peft_config=get_peft_config(model_args),
68
+ )
69
+
70
+ trainer.train()
71
+ trainer.save_model(training_args.output_dir)
72
+ if training_args.push_to_hub:
73
+ # You had dataset_name here, but it's not a valid argument for push_to_hub in this context.
74
+ # It will use the hub_model_id from TrainingArguments.
75
+ trainer.push_to_hub()
76
+
77
+
78
+ if __name__ == "__main__":
79
+ parser = TrlParser((ScriptArguments, SFTConfig, ModelConfig))
80
+ script_args, training_args, model_args, _ = parser.parse_args_and_config(
81
+ return_remaining_strings=True
82
+ )
83
+ main(script_args, training_args, model_args)