Text Generation
Transformers
English
qwen2
code-generation
python
fine-tuning
Qwen
tools
agent-framework
multi-agent
conversational
Eval Results (legacy)
Instructions to use my-ai-stack/Stack-2-9-finetuned with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use my-ai-stack/Stack-2-9-finetuned with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="my-ai-stack/Stack-2-9-finetuned") messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("my-ai-stack/Stack-2-9-finetuned") model = AutoModelForCausalLM.from_pretrained("my-ai-stack/Stack-2-9-finetuned") messages = [ {"role": "user", "content": "Who are you?"}, ] inputs = tokenizer.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=40) print(tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps
- vLLM
How to use my-ai-stack/Stack-2-9-finetuned with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "my-ai-stack/Stack-2-9-finetuned" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "my-ai-stack/Stack-2-9-finetuned", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/my-ai-stack/Stack-2-9-finetuned
- SGLang
How to use my-ai-stack/Stack-2-9-finetuned with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "my-ai-stack/Stack-2-9-finetuned" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "my-ai-stack/Stack-2-9-finetuned", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "my-ai-stack/Stack-2-9-finetuned" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "my-ai-stack/Stack-2-9-finetuned", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Docker Model Runner
How to use my-ai-stack/Stack-2-9-finetuned with Docker Model Runner:
docker model run hf.co/my-ai-stack/Stack-2-9-finetuned
walidsobhie-code Claude Opus 4.6 commited on
Commit Β·
78417b9
1
Parent(s): f268fb6
fix: update Kaggle notebook with self-contained cells
Browse files- Each cell defines its own paths (no NameError)
- Auto-downloads model only if not exists
- 9 steps: clone β install β model β config β train β merge β done
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- kaggle_train_stack29.ipynb +64 -49
kaggle_train_stack29.ipynb
CHANGED
|
@@ -16,10 +16,9 @@
|
|
| 16 |
"---\n",
|
| 17 |
"\n",
|
| 18 |
"**Instructions:**\n",
|
| 19 |
-
"1.
|
| 20 |
-
"2.
|
| 21 |
-
"3.
|
| 22 |
-
"4. Run cells in order\n",
|
| 23 |
"\n",
|
| 24 |
"---"
|
| 25 |
]
|
|
@@ -30,7 +29,7 @@
|
|
| 30 |
"metadata": {},
|
| 31 |
"outputs": [],
|
| 32 |
"source": [
|
| 33 |
-
"# Check GPU\n",
|
| 34 |
"!nvidia-smi"
|
| 35 |
]
|
| 36 |
},
|
|
@@ -40,13 +39,12 @@
|
|
| 40 |
"metadata": {},
|
| 41 |
"outputs": [],
|
| 42 |
"source": [
|
| 43 |
-
"# STEP
|
| 44 |
"import os\n",
|
| 45 |
"import shutil\n",
|
| 46 |
"\n",
|
| 47 |
"REPO_DIR = \"/kaggle/working/stack-2.9\"\n",
|
| 48 |
"\n",
|
| 49 |
-
"# Remove old if exists\n",
|
| 50 |
"if os.path.exists(REPO_DIR):\n",
|
| 51 |
" shutil.rmtree(REPO_DIR)\n",
|
| 52 |
"\n",
|
|
@@ -62,7 +60,7 @@
|
|
| 62 |
"metadata": {},
|
| 63 |
"outputs": [],
|
| 64 |
"source": [
|
| 65 |
-
"# STEP
|
| 66 |
"!pip install -q torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118\n",
|
| 67 |
"!pip install -q transformers peft accelerate datasets pyyaml tqdm scipy bitsandbytes\n",
|
| 68 |
"print(\"β
Dependencies installed\")"
|
|
@@ -74,22 +72,37 @@
|
|
| 74 |
"metadata": {},
|
| 75 |
"outputs": [],
|
| 76 |
"source": [
|
| 77 |
-
"# STEP
|
| 78 |
-
"
|
| 79 |
"\n",
|
| 80 |
-
"
|
| 81 |
"MODEL_DIR = os.path.join(REPO_DIR, \"base_model_qwen7b\")\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
"\n",
|
| 83 |
-
"if
|
| 84 |
-
" print(
|
|
|
|
|
|
|
| 85 |
" print(\"This takes ~10-15 minutes...\")\n",
|
| 86 |
-
" tokenizer = AutoTokenizer.from_pretrained(
|
| 87 |
" tokenizer.save_pretrained(MODEL_DIR)\n",
|
| 88 |
-
" model = AutoModelForCausalLM.from_pretrained(
|
| 89 |
" model.save_pretrained(MODEL_DIR)\n",
|
| 90 |
" print(\"β
Model downloaded!\")\n",
|
| 91 |
-
"else:\n",
|
| 92 |
-
" print(\"β
Model already exists\")\n",
|
| 93 |
"\n",
|
| 94 |
"!ls -lh {MODEL_DIR} | head -5"
|
| 95 |
]
|
|
@@ -100,30 +113,34 @@
|
|
| 100 |
"metadata": {},
|
| 101 |
"outputs": [],
|
| 102 |
"source": [
|
| 103 |
-
"# STEP
|
| 104 |
"import yaml\n",
|
| 105 |
-
"\n",
|
| 106 |
-
"config_path = os.path.join(REPO_DIR, \"stack/training/train_config_local.yaml\")\n",
|
| 107 |
-
"\n",
|
| 108 |
-
"with open(config_path, 'r') as f:\n",
|
| 109 |
-
" config = yaml.safe_load(f)\n",
|
| 110 |
-
"\n",
|
| 111 |
-
"# Update for Kaggle GPU\n",
|
| 112 |
-
"config['model']['name'] = MODEL_DIR\n",
|
| 113 |
-
"config['hardware']['device'] = \"cuda\"\n",
|
| 114 |
-
"config['hardware']['num_gpus'] = 1\n",
|
| 115 |
-
"\n",
|
| 116 |
-
"OUTPUT_DIR = os.path.join(REPO_DIR, \"training_output\")\n",
|
| 117 |
-
"config['output']['lora_dir'] = os.path.join(OUTPUT_DIR, \"lora\")\n",
|
| 118 |
-
"config['output']['merged_dir'] = os.path.join(OUTPUT_DIR, \"merged\")\n",
|
| 119 |
"\n",
|
| 120 |
"os.makedirs(OUTPUT_DIR, exist_ok=True)\n",
|
| 121 |
-
"updated_config = os.path.join(OUTPUT_DIR, \"train_config.yaml\")\n",
|
| 122 |
"\n",
|
| 123 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
" yaml.dump(config, f)\n",
|
| 125 |
"\n",
|
| 126 |
-
"print(f\"β
Config saved to: {
|
| 127 |
"print(f\" Device: {config['hardware']['device']}\")"
|
| 128 |
]
|
| 129 |
},
|
|
@@ -133,7 +150,7 @@
|
|
| 133 |
"metadata": {},
|
| 134 |
"outputs": [],
|
| 135 |
"source": [
|
| 136 |
-
"# STEP
|
| 137 |
"import sys\n",
|
| 138 |
"sys.path.insert(0, os.path.join(REPO_DIR, \"stack/training\"))\n",
|
| 139 |
"\n",
|
|
@@ -142,10 +159,10 @@
|
|
| 142 |
"print(\"=\"*60)\n",
|
| 143 |
"\n",
|
| 144 |
"from train_lora import train_lora\n",
|
| 145 |
-
"trainer = train_lora(
|
| 146 |
"\n",
|
| 147 |
"print(\"=\"*60)\n",
|
| 148 |
-
"print(\"TRAINING COMPLETED\")\n",
|
| 149 |
"print(\"=\"*60)"
|
| 150 |
]
|
| 151 |
},
|
|
@@ -155,7 +172,7 @@
|
|
| 155 |
"metadata": {},
|
| 156 |
"outputs": [],
|
| 157 |
"source": [
|
| 158 |
-
"# STEP
|
| 159 |
"import sys\n",
|
| 160 |
"sys.path.insert(0, os.path.join(REPO_DIR, \"stack/training\"))\n",
|
| 161 |
"from merge_adapter import merge_adapter\n",
|
|
@@ -175,7 +192,7 @@
|
|
| 175 |
"\n",
|
| 176 |
"merge_adapter(merge_cfg_path, os.path.join(OUTPUT_DIR, \"lora\"), merged_dir)\n",
|
| 177 |
"\n",
|
| 178 |
-
"print(f\"β
|
| 179 |
"!ls -lh {merged_dir}"
|
| 180 |
]
|
| 181 |
},
|
|
@@ -185,15 +202,13 @@
|
|
| 185 |
"metadata": {},
|
| 186 |
"outputs": [],
|
| 187 |
"source": [
|
| 188 |
-
"# STEP
|
| 189 |
-
"
|
| 190 |
-
"
|
| 191 |
-
"\n",
|
| 192 |
-
"print(\"
|
| 193 |
-
"print(f\"
|
| 194 |
-
"print(\"\\
|
| 195 |
-
"print(\"1. Click 'Output' tab in Kaggle\")\n",
|
| 196 |
-
"print(\"2. Download the files from training_output/merged/\")"
|
| 197 |
]
|
| 198 |
}
|
| 199 |
],
|
|
|
|
| 16 |
"---\n",
|
| 17 |
"\n",
|
| 18 |
"**Instructions:**\n",
|
| 19 |
+
"1. Enable GPU: Settings β Accelerator β GPU P100\n",
|
| 20 |
+
"2. Run cells in order from the top\n",
|
| 21 |
+
"3. Model auto-downloads if not present\n",
|
|
|
|
| 22 |
"\n",
|
| 23 |
"---"
|
| 24 |
]
|
|
|
|
| 29 |
"metadata": {},
|
| 30 |
"outputs": [],
|
| 31 |
"source": [
|
| 32 |
+
"# STEP 1: Check GPU\n",
|
| 33 |
"!nvidia-smi"
|
| 34 |
]
|
| 35 |
},
|
|
|
|
| 39 |
"metadata": {},
|
| 40 |
"outputs": [],
|
| 41 |
"source": [
|
| 42 |
+
"# STEP 2: Clone repo\n",
|
| 43 |
"import os\n",
|
| 44 |
"import shutil\n",
|
| 45 |
"\n",
|
| 46 |
"REPO_DIR = \"/kaggle/working/stack-2.9\"\n",
|
| 47 |
"\n",
|
|
|
|
| 48 |
"if os.path.exists(REPO_DIR):\n",
|
| 49 |
" shutil.rmtree(REPO_DIR)\n",
|
| 50 |
"\n",
|
|
|
|
| 60 |
"metadata": {},
|
| 61 |
"outputs": [],
|
| 62 |
"source": [
|
| 63 |
+
"# STEP 3: Install dependencies\n",
|
| 64 |
"!pip install -q torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118\n",
|
| 65 |
"!pip install -q transformers peft accelerate datasets pyyaml tqdm scipy bitsandbytes\n",
|
| 66 |
"print(\"β
Dependencies installed\")"
|
|
|
|
| 72 |
"metadata": {},
|
| 73 |
"outputs": [],
|
| 74 |
"source": [
|
| 75 |
+
"# STEP 4: Setup paths (MODEL_DIR, OUTPUT_DIR)\n",
|
| 76 |
+
"import os\n",
|
| 77 |
"\n",
|
| 78 |
+
"REPO_DIR = \"/kaggle/working/stack-2.9\"\n",
|
| 79 |
"MODEL_DIR = os.path.join(REPO_DIR, \"base_model_qwen7b\")\n",
|
| 80 |
+
"OUTPUT_DIR = os.path.join(REPO_DIR, \"training_output\")\n",
|
| 81 |
+
"\n",
|
| 82 |
+
"print(f\"REPO_DIR: {REPO_DIR}\")\n",
|
| 83 |
+
"print(f\"MODEL_DIR: {MODEL_DIR}\")\n",
|
| 84 |
+
"print(f\"OUTPUT_DIR: {OUTPUT_DIR}\")"
|
| 85 |
+
]
|
| 86 |
+
},
|
| 87 |
+
{
|
| 88 |
+
"cell_type": "code",
|
| 89 |
+
"execution_count": null,
|
| 90 |
+
"metadata": {},
|
| 91 |
+
"outputs": [],
|
| 92 |
+
"source": [
|
| 93 |
+
"# STEP 5: Download model (if not exists)\n",
|
| 94 |
+
"from transformers import AutoModelForCausalLM, AutoTokenizer\n",
|
| 95 |
"\n",
|
| 96 |
+
"if os.path.exists(os.path.join(MODEL_DIR, \"config.json\")):\n",
|
| 97 |
+
" print(\"β
Model already exists, skipping download!\")\n",
|
| 98 |
+
"else:\n",
|
| 99 |
+
" print(\"β¬οΈ Downloading model (Qwen2.5-Coder-7B)...\")\n",
|
| 100 |
" print(\"This takes ~10-15 minutes...\")\n",
|
| 101 |
+
" tokenizer = AutoTokenizer.from_pretrained(\"Qwen/Qwen2.5-Coder-7B\", trust_remote_code=True)\n",
|
| 102 |
" tokenizer.save_pretrained(MODEL_DIR)\n",
|
| 103 |
+
" model = AutoModelForCausalLM.from_pretrained(\"Qwen/Qwen2.5-Coder-7B\", trust_remote_code=True)\n",
|
| 104 |
" model.save_pretrained(MODEL_DIR)\n",
|
| 105 |
" print(\"β
Model downloaded!\")\n",
|
|
|
|
|
|
|
| 106 |
"\n",
|
| 107 |
"!ls -lh {MODEL_DIR} | head -5"
|
| 108 |
]
|
|
|
|
| 113 |
"metadata": {},
|
| 114 |
"outputs": [],
|
| 115 |
"source": [
|
| 116 |
+
"# STEP 6: Create config\n",
|
| 117 |
"import yaml\n",
|
| 118 |
+
"import os\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
"\n",
|
| 120 |
"os.makedirs(OUTPUT_DIR, exist_ok=True)\n",
|
|
|
|
| 121 |
"\n",
|
| 122 |
+
"config = {\n",
|
| 123 |
+
" 'model': {'name': MODEL_DIR, 'trust_remote_code': True, 'torch_dtype': 'float16'},\n",
|
| 124 |
+
" 'data': {'input_path': './data/final/train.jsonl', 'max_length': 2048},\n",
|
| 125 |
+
" 'lora': {'r': 16, 'alpha': 32, 'dropout': 0.05,\n",
|
| 126 |
+
" 'target_modules': ['q_proj', 'k_proj', 'v_proj', 'o_proj'],\n",
|
| 127 |
+
" 'bias': 'none', 'task_type': 'CAUSAL_LM'},\n",
|
| 128 |
+
" 'training': {'num_epochs': 1, 'batch_size': 2, 'gradient_accumulation': 4,\n",
|
| 129 |
+
" 'learning_rate': 2e-4, 'warmup_steps': 50, 'weight_decay': 0.01,\n",
|
| 130 |
+
" 'max_grad_norm': 1.0, 'logging_steps': 5, 'eval_steps': 100,\n",
|
| 131 |
+
" 'save_steps': 200, 'save_total_limit': 2, 'fp16': True, 'bf16': False,\n",
|
| 132 |
+
" 'gradient_checkpointing': True},\n",
|
| 133 |
+
" 'output': {'lora_dir': os.path.join(OUTPUT_DIR, 'lora'),\n",
|
| 134 |
+
" 'merged_dir': os.path.join(OUTPUT_DIR, 'merged')},\n",
|
| 135 |
+
" 'quantization': {'enabled': False},\n",
|
| 136 |
+
" 'hardware': {'device': 'cuda', 'num_gpus': 1, 'use_4bit': False, 'use_8bit': False}\n",
|
| 137 |
+
"}\n",
|
| 138 |
+
"\n",
|
| 139 |
+
"config_path = os.path.join(OUTPUT_DIR, \"train_config.yaml\")\n",
|
| 140 |
+
"with open(config_path, 'w') as f:\n",
|
| 141 |
" yaml.dump(config, f)\n",
|
| 142 |
"\n",
|
| 143 |
+
"print(f\"β
Config saved to: {config_path}\")\n",
|
| 144 |
"print(f\" Device: {config['hardware']['device']}\")"
|
| 145 |
]
|
| 146 |
},
|
|
|
|
| 150 |
"metadata": {},
|
| 151 |
"outputs": [],
|
| 152 |
"source": [
|
| 153 |
+
"# STEP 7: Train LoRA\n",
|
| 154 |
"import sys\n",
|
| 155 |
"sys.path.insert(0, os.path.join(REPO_DIR, \"stack/training\"))\n",
|
| 156 |
"\n",
|
|
|
|
| 159 |
"print(\"=\"*60)\n",
|
| 160 |
"\n",
|
| 161 |
"from train_lora import train_lora\n",
|
| 162 |
+
"trainer = train_lora(config_path)\n",
|
| 163 |
"\n",
|
| 164 |
"print(\"=\"*60)\n",
|
| 165 |
+
"print(\"TRAINING COMPLETED!\")\n",
|
| 166 |
"print(\"=\"*60)"
|
| 167 |
]
|
| 168 |
},
|
|
|
|
| 172 |
"metadata": {},
|
| 173 |
"outputs": [],
|
| 174 |
"source": [
|
| 175 |
+
"# STEP 8: Merge model\n",
|
| 176 |
"import sys\n",
|
| 177 |
"sys.path.insert(0, os.path.join(REPO_DIR, \"stack/training\"))\n",
|
| 178 |
"from merge_adapter import merge_adapter\n",
|
|
|
|
| 192 |
"\n",
|
| 193 |
"merge_adapter(merge_cfg_path, os.path.join(OUTPUT_DIR, \"lora\"), merged_dir)\n",
|
| 194 |
"\n",
|
| 195 |
+
"print(f\"β
Merged model saved to: {merged_dir}\")\n",
|
| 196 |
"!ls -lh {merged_dir}"
|
| 197 |
]
|
| 198 |
},
|
|
|
|
| 202 |
"metadata": {},
|
| 203 |
"outputs": [],
|
| 204 |
"source": [
|
| 205 |
+
"# STEP 9: Done!\n",
|
| 206 |
+
"print(\"=\"*60)\n",
|
| 207 |
+
"print(\"π TRAINING COMPLETE!\")\n",
|
| 208 |
+
"print(\"=\"*60)\n",
|
| 209 |
+
"print(f\"LoRA adapter: {os.path.join(OUTPUT_DIR, 'lora')}\")\n",
|
| 210 |
+
"print(f\"Merged model: {os.path.join(OUTPUT_DIR, 'merged')}\")\n",
|
| 211 |
+
"print(\"\\nπ₯ Download from: Kaggle β Output tab\")"
|
|
|
|
|
|
|
| 212 |
]
|
| 213 |
}
|
| 214 |
],
|