Views
No views yet
\boxed{} format.FreedomIntelligence/medical-o1-reasoning-SFTunsloth/OpenMathReasoning-minimlabonne/guanaco-llama2-1kunsloth library with the advanced reinforcement learning features of trl:bnb_4bit_use_double_quant=True), allowing for efficient training on limited GPU memory.r=24, lora_alpha=32, and lora_dropout=0.05 was applied. Key attention and feed-forward projection layers (q_proj, k_proj, v_proj, o_proj, gate_proj, up_proj, down_proj) were targeted for adaptation.recompute_grad=True for Unsloth-specific optimizations.adamw_8bit.max_grad_norm=1.0) and weight decay (0.01) were applied to prevent exploding gradients and overfitting.transformers, unsloth, trl, datasets, wandb (for experiment tracking), and vllm (used during the GRPO stage for efficient text generation).1import torch
2from transformers import AutoTokenizer, BitsAndBytesConfig
3from unsloth import FastLanguageModel
4
5# Configuration parameters (matching training)
6MAX_SEQ_LENGTH = 2048
7LOAD_IN_4BIT = True
8USE_DOUBLE_QUANT = True
9
10# Initialize BitsAndBytesConfig as used during training
11bnb_config = BitsAndBytesConfig(
12 load_in_4bit=LOAD_IN_4BIT,
13 bnb_4bit_quant_type="nf4",
14 bnb_4bit_compute_dtype=torch.bfloat16 if torch.cuda.is_bf16_supported() else torch.float16,
15 bnb_4bit_use_double_quant=USE_DOUBLE_QUANT,
16)
17
18# Replace with the actual path to your uploaded model on Hugging Face Hub
19model_id = "your-huggingface-username/Qwen3-8B-MultiStage-Finetune-Hybrid"
20
21# Load tokenizer
22tokenizer = AutoTokenizer.from_pretrained(model_id, use_fast=False)
23if tokenizer.pad_token is None:
24 tokenizer.pad_token = tokenizer.eos_token # Ensure pad token is set for generation
25
26# Load the model using Unsloth's optimized loading
27model = FastLanguageModel.from_pretrained(
28 model_id,
29 quantization_config=bnb_config,
30 max_seq_length=MAX_SEQ_LENGTH,
31 device_map="auto", # Automatically maps model to available GPUs
32)
33
34# Example for a general chat interaction
35messages = [
36 {"role": "system", "content": "You are a friendly and helpful assistant."},
37 {"role": "user", "content": "Tell me a short, funny story about a clumsy robot."},
38]
39
40# Apply the chat template and tokenize inputs
41input_ids = tokenizer.apply_chat_template(
42 messages,
43 tokenize=True,
44 add_generation_prompt=True, # Important: Add the prompt for the assistant's turn
45 return_tensors="pt"
46).to("cuda") # Move inputs to GPU
47
48# Generate outputs
49outputs = model.generate(
50 input_ids,
51 max_new_tokens=512, # Maximum tokens to generate
52 do_sample=True, # Enable sampling for more diverse outputs
53 temperature=0.7, # Control randomness
54 top_p=0.95 # Nucleus sampling
55)
56
57# Decode and print the generated text, skipping special tokens
58print("--- General Chat Example ---")
59print(tokenizer.decode(outputs[0], skip_special_tokens=True))
60
61# Example for a math problem (model is trained to provide a \boxed{} answer)
62math_messages = [
63 {"role": "system", "content": "You are a math solver. Provide your reasoning within \\ and the final answer in \\boxed{} format."},
64 {"role": "user", "content": "If a car travels at 80 km/h for 2.5 hours, and then at 60 km/h for another 1.5 hours, what is the total distance traveled?"},
65]
66
67# Apply math chat template and tokenize inputs
68math_input_ids = tokenizer.apply_chat_template(
69 math_messages,
70 tokenize=True,
71 add_generation_prompt=True,
72 return_tensors="pt"
73).to("cuda")
74
75# Generate outputs for the math problem
76math_outputs = model.generate(
77 math_input_ids,
78 max_new_tokens=512,
79 do_sample=True,
80 temperature=0.6, # Slightly lower temperature for more deterministic math outputs
81 top_p=0.9
82)
83
84print("\n--- Math Example ---")
85print(tokenizer.decode(math_outputs[0], skip_special_tokens=True))
86
87
88Limitations and Bias
89As a large language model, this fine-tuned Qwen-8B model inherits general limitations and potential biases from its extensive pre-training and fine-tuning data:
90
91Hallucinations: The model may generate information that is factually incorrect or nonsensical. Always cross-reference critical information.
92Factual Accuracy: While specialized in medical and mathematical domains, it should not be used as a substitute for professional medical advice, complex mathematical proofs, or any domain requiring absolute precision without independent verification.
93Bias: The model's outputs are influenced by the biases present in its training data (both the base model's pre-training and the fine-tuning datasets). This may manifest in stereotypical, harmful, or unfair content.
94Language Proficiency: Primarily trained on English text. While some Spanish content was present in the general chat dataset, its proficiency in Spanish or other languages is not guaranteed and may vary.
95Context Window: Limited by its max_seq_length (2048 tokens). Very long inputs or extensive multi-turn conversations might lead to degraded performance or truncation of context.
96Ethical Considerations
97Users should be aware of the following ethical considerations when deploying or using this model:
98
99Not for Critical Applications: This model is intended for research, experimentation, and exploratory applications. It is not designed or validated for use in critical systems where accuracy, reliability, and safety are paramount (e.g., medical diagnosis, financial advice, legal counsel, or decision-making systems impacting individuals).
100Responsible AI Use: Deploy and use this model responsibly, adhering to ethical AI guidelines and principles. Implement safeguards to monitor its outputs and prevent potential misuse, discrimination, or the generation of harmful content.
101Data Privacy and Security: Do not use this model with sensitive personal identifiable information (PII) or confidential data. Ensure compliance with all relevant data privacy regulations.
102Transparency: Be transparent with end-users when they are interacting with an AI system.
103Citation
104If you use this model or the training methodology, please consider citing the following key components:
105
106Code snippet
107
108@misc{qwen3,
109 author = {Qwen Team},
110 title = {Qwen3-8B},
111 year = {2024},
112 publisher = {Hugging Face},
113 howpublished = {\url{[https://huggingface.co/Qwen/Qwen3-8B](https://huggingface.co/Qwen/Qwen3-8B)}}
114}
115
116@misc{unsloth,
117 author = {Daniel Han},
118 title = {Unsloth: Fast LLM Fine-tuning},
119 year = {2023},
120 publisher = {GitHub},
121 howpublished = {\url{[https://github.com/unsloth/unsloth](https://github.com/unsloth/unsloth)}}
122}
123
124@misc{trl,
125 author = {Hugging Face Team},
126 title = {TRL: Transformer Reinforcement Learning},
127 year = {2023},
128 publisher = {GitHub},
129 howpublished = {\url{[https://github.com/huggingface/trl](https://github.com/huggingface/trl)}}
130}
131
132@misc{medical_dataset,
133 author = {FreedomIntelligence},
134 title = {medical-o1-reasoning-SFT},
135 year = {2024},
136 publisher = {Hugging Face},
137 howpublished = {\url{[https://huggingface.co/FreedomIntelligence/medical-o1-reasoning-SFT](https://huggingface.co/FreedomIntelligence/medical-o1-reasoning-SFT)}}
138}
139
140@misc{openmathreasoning_mini,
141 author = {unsloth},
142 title = {OpenMathReasoning-mini},
143 year = {2023},
144 publisher = {Hugging Face},
145 howpublished = {\url{[https://huggingface.co/datasets/unsloth/OpenMathReasoning-mini](https://huggingface.co/datasets/unsloth/OpenMathReasoning-mini)}}
146}
147
148@misc{guanaco_llama2_1k,
149 author = {mlabonne},
150 title = {guanaco-llama2-1k},
151 year = {2023},
152 publisher = {Hugging Face},
153 howpublished = {\url{[https://huggingface.co/datasets/mlabonne/guanaco-llama2-1k](https://huggingface.co/datasets/mlabonne/guanaco-llama2-1k)}}
154}