Views
No views yet
1
2import torch
3from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
4
5torch.random.manual_seed(0)
6
7model = AutoModelForCausalLM.from_pretrained(
8 "styalai/competition-math-phinetune-v1", q
9 device_map="cuda",
10 torch_dtype="auto",
11 trust_remote_code=True,
12)
13tokenizer = AutoTokenizer.from_pretrained("styalai/competition-math-phinetune-v1")
14
15messages = [
16 {"role": "user", "content": "What about solving an 2x + 3 = 7 equation?"},
17]
18
19pipe = pipeline(
20 "text-generation",
21 model=model,
22 tokenizer=tokenizer,
23)
24
25generation_args = {
26 "max_new_tokens": 500,
27 "return_full_text": False,
28 "temperature": 0.0,
29 "do_sample": False,
30}
31
32output = pipe(messages, **generation_args)
33print(output[0]['generated_text'])1project_name = 'competition-math-phinetune-v1' # @param {type:"string"}
2model_name = "styalai/phi-ne-tuning-1-4" #'microsoft/Phi-3-mini-4k-instruct' # @param {type:"string"}
3
4#@markdown ---
5#@markdown #### Push to Hub?
6#@markdown Use these only if you want to push your trained model to a private repo in your Hugging Face Account
7#@markdown If you dont use these, the model will be saved in Google Colab and you are required to download it manually.
8#@markdown Please enter your Hugging Face write token. The trained model will be saved to your Hugging Face account.
9#@markdown You can find your token here: https://huggingface.co/settings/tokens
10push_to_hub = True # @param ["False", "True"] {type:"raw"}
11hf_token = "hf_****" #@param {type:"string"}
12#repo_id = "styalai/phine_tuning_1" #@param {type:"string"}
13
14#@markdown ---
15#@markdown #### Hyperparameters
16learning_rate = 3e-4 # @param {type:"number"}
17num_epochs = 1 #@param {type:"number"}
18batch_size = 1 # @param {type:"slider", min:1, max:32, step:1}
19block_size = 1024 # @param {type:"number"}
20trainer = "sft" # @param ["default", "sft"] {type:"raw"}
21warmup_ratio = 0.1 # @param {type:"number"}
22weight_decay = 0.01 # @param {type:"number"}
23gradient_accumulation = 4 # @param {type:"number"}
24mixed_precision = "fp16" # @param ["fp16", "bf16", "none"] {type:"raw"}
25peft = True # @param ["False", "True"] {type:"raw"}
26quantization = "int4" # @param ["int4", "int8", "none"] {type:"raw"}
27lora_r = 16 #@param {type:"number"}
28lora_alpha = 32 #@param {type:"number"}
29lora_dropout = 0.05 #@param {type:"number"}
30
31code for the creation of the dataset :
32from datasets import load_dataset
33dataset = load_dataset("camel-ai/math")#, streaming=True)
34
35import pandas as pd
36data = {"text":[]}
37
38msg1 = dataset["train"]["message_1"]
39msg2 = dataset["train"]["message_2"]
40
41for i in range(3500):
42 user = "<|user|>"+ msg1[i] +"<|end|>\n"
43 phi = "<|assistant|>"+ msg2[i] +"<|end|>"
44 prompt = user+phi
45 data["text"].append(prompt)
46
47data = pd.DataFrame.from_dict(data)
48print(data)
49#os.mkdir("/kaggle/working/data")
50data.to_csv('data/dataset.csv', index=False, escapechar='\\')
51
52!autotrain llm \
53--train \
54--username "styalai" \
55--merge-adapter \
56--model ${MODEL_NAME} \
57--project-name ${PROJECT_NAME} \
58--data-path data/ \
59--text-column text \
60--lr ${LEARNING_RATE} \
61--batch-size ${BATCH_SIZE} \
62--epochs ${NUM_EPOCHS} \
63--block-size ${BLOCK_SIZE} \
64--warmup-ratio ${WARMUP_RATIO} \
65--lora-r ${LORA_R} \
66--lora-alpha ${LORA_ALPHA} \
67--lora-dropout ${LORA_DROPOUT} \
68--weight-decay ${WEIGHT_DECAY} \
69--gradient-accumulation ${GRADIENT_ACCUMULATION} \
70--quantization ${QUANTIZATION} \
71--mixed-precision ${MIXED_PRECISION} \
72$( [[ "$PEFT" == "True" ]] && echo "--peft" ) \
73$( [[ "$PUSH_TO_HUB" == "True" ]] && echo "--push-to-hub --token ${HF_TOKEN}" )q