Views
No views yet
| GPU Type | Time (Optimized) | Time (Original) | Speedup |
|---|---|---|---|
| T4 (Free Colab) | ~30-45 min | ~1-2 hours | 2-3x |
| A100 (Colab Pro) | ~7-12 min | ~15-25 min | 2-3x |
Runtime → Change runtime type → GPU (T4 or A100)HF_USERNAME in Step 3 with your HuggingFace username1# Clone repository
2git clone https://github.com/ahczhg/qwen3-rlhf-cot.git
3cd qwen3-rlhf-cot
4
5# Install dependencies
6pip install -U transformers datasets accelerate peft trl bitsandbytes sentencepiece huggingface_hub torch
7
8# Run notebook
9jupyter notebook qwen3_rlhf_cot_finetune.ipynb1QUICK_TEST = True
2NUM_EPOCHS = 1
3BATCH_SIZE = 64
4TRAIN_EVERY_N_BATCHES = 10 # Train only 10% of batches
5MAX_GEN_TOKENS = 641QUICK_TEST = False
2NUM_EPOCHS = 3
3BATCH_SIZE = 8
4TRAIN_EVERY_N_BATCHES = 1 # Train every batch
5MAX_GEN_TOKENS = 2561def compute_cot_reward(response_text, prompt_text):
2 reward = 0.0
3
4 # Reasoning markers (+0.5 each, max +3.0)
5 markers = ["first", "therefore", "let's", "consider", "analyze", ...]
6 reward += min(marker_count * 0.5, 3.0)
7
8 # Appropriate length (+2.0 for 30-300 words)
9 if 30 <= word_count <= 300:
10 reward += 2.0
11
12 # Structured thinking (+1.5 for 3+ sentences)
13 if sentence_count >= 3:
14 reward += 1.5
15
16 # Safety awareness (+0.3 per keyword, max +2.0)
17 safety_keywords = ["safe", "unsafe", "harmful", "risk", ...]
18 reward += min(safety_count * 0.3, 2.0)
19
20 # Penalties for repetition (-2.0 if <50% unique)
21 if unique_ratio < 0.5:
22 reward -= 2.0
23
24 return rewardThink step-by-step and reason carefully about this content safety query.
Query: [Original question]
Reasoning:Query: Is it safe to share personal banking information in a public forum?
Model's Reasoning:
First, let's analyze what "safe" means in this context. Sharing personal
banking information can be considered a form of risk, as it involves exposing
sensitive financial data.
Step 1: Identify the risk - Sharing personal banking information involves
exposing sensitive data that could lead to unauthorized access.
Step 2: Determine consequences - This could result in identity theft,
financial losses, and damage to reputation.
Step 3: Evaluate risks - The potential risks significantly outweigh any
potential benefits.
Conclusion: Sharing personal banking information in a public forum is not
safe and should be avoided.Query: A user wants to post instructions for creating fake IDs. Is this safe?
Model's Reasoning:
Let's examine this carefully. Creating and using fake IDs is illegal and
unethical for several reasons:
First, it involves fraud and identity theft, which are criminal offenses.
Second, the consequences include legal penalties, fines, and potential
imprisonment.
Third, it can harm others by enabling identity fraud and financial crimes.
Therefore, posting instructions for creating fake IDs is unsafe and should
not be permitted.1# Sub-batched generation (prevents OOM)
2MAX_GEN_BATCH = 16 # Generate 16 samples at once
3
4# Conservative training (stable gradients)
5max_train_batch = 4 # Train 4 samples at once
6
7# Frequent cleanup (prevents fragmentation)
8if batch_count % 5 == 0:
9 gc.collect()
10 torch.cuda.empty_cache()1# Example: Emphasize conciseness
2def compute_custom_reward(response_text, prompt_text):
3 reward = 0.0
4
5 # Prefer shorter, more focused responses
6 word_count = len(response_text.split())
7 if 20 <= word_count <= 100: # Shorter range
8 reward += 3.0
9
10 # Your custom logic here...
11
12 return torch.tensor(reward, dtype=torch.float32)qwen3-rlhf-cot/
├── qwen3_rlhf_cot_finetune.ipynb # Main training notebook
├── README.md # This file
├── LICENSE # Apache 2.0 license
├── qwen3-rlhf-cot/ # Output directory (created during training)
│ ├── adapter_config.json
│ ├── adapter_model.safetensors
│ ├── tokenizer.json
│ ├── tokenizer_config.json
│ └── README.md # Model card
└── requirements.txt # Python dependencies1# Reduce batch size
2BATCH_SIZE = 32 # or even 16
3
4# Reduce generation batch size
5MAX_GEN_BATCH = 8
6
7# Enable more aggressive cleanup
8# (already enabled in optimized version)1# Enable quick test mode
2QUICK_TEST = True
3
4# Use larger batch sizes (if memory permits)
5BATCH_SIZE = 64
6
7# Skip more batches (faster but less quality)
8TRAIN_EVERY_N_BATCHES = 20 # Train only 5% of batchesRuntime → Change runtime type → GPU!nvidia-smi1# Re-authenticate
2from huggingface_hub import notebook_login
3notebook_login()
4
5# Check username is correct
6HF_USERNAME = "your-actual-username" # Update this!
7
8# Verify repository exists
9# Visit https://huggingface.co/your-username/qwen3-0.6b-rlhf-cot1@misc{qwen3-rlhf-cot-2025,
2 title={Qwen3-0.6B-RLHF-CoT: Chain-of-Thought via Pure Reinforcement Learning},
3 author={ahczhg},
4 year={2025},
5 publisher={HuggingFace},
6 howpublished={\url{https://huggingface.co/ahczhg/qwen3-0.6b-rlhf-cot}}
7}1@misc{deepseek-r1-zero,
2 title={DeepSeek-R1-Zero},
3 author={DeepSeek-AI},
4 year={2024},
5 publisher={HuggingFace},
6 howpublished={\url{https://huggingface.co/deepseek-ai/DeepSeek-R1-Zero}}
7}