File size: 24,430 Bytes
e6a04c6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 |
{
"cells": [
{
"cell_type": "markdown",
"id": "5a611684",
"metadata": {
"id": "5a611684"
},
"source": [
"# NanoChat Easy - GRPO Training\n",
"\n"
]
},
{
"cell_type": "markdown",
"id": "80df0403",
"metadata": {
"id": "80df0403"
},
"source": [
"## Import model and tokenizer\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1dd76bde",
"metadata": {
"id": "1dd76bde",
"outputId": "b786d7ad-5aa8-4a13-eb1f-54a65aaf44ba"
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/fsx/benjamin_burtenshaw/nanochat_/.venv/lib/python3.10/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
" from .autonotebook import tqdm as notebook_tqdm\n",
"`torch_dtype` is deprecated! Use `dtype` instead!\n"
]
}
],
"source": [
"import torch\n",
"from torch.utils.data import DataLoader\n",
"from datasets import load_dataset\n",
"from transformers import AutoModelForCausalLM, AutoTokenizer, get_linear_schedule_with_warmup\n",
"\n",
"\n",
"model_id = \"karpathy/nanochat-d32\"\n",
"revision = \"refs/pr/1\"\n",
"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
"\n",
"\n",
"tokenizer = AutoTokenizer.from_pretrained(model_id, revision=revision)\n",
"model = AutoModelForCausalLM.from_pretrained(\n",
" model_id,\n",
" revision=revision,\n",
" torch_dtype=torch.bfloat16 if device.type == \"cuda\" else torch.float32,\n",
").to(device)\n",
"tokenizer.pad_token = tokenizer.eos_token\n",
"model.config.pad_token_id = tokenizer.pad_token_id"
]
},
{
"cell_type": "markdown",
"id": "6eb979a9",
"metadata": {
"id": "6eb979a9"
},
"source": [
"## Setup LoRA\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1973b450",
"metadata": {
"id": "1973b450",
"outputId": "354ceafb-b4cb-4423-f076-7800024171b7"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"trainable params: 1,179,648 || all params: 1,880,227,840 || trainable%: 0.0627\n"
]
}
],
"source": [
"from peft import LoraConfig, get_peft_model\n",
"\n",
"lora_config = LoraConfig(\n",
" r=1,\n",
" lora_alpha=2,\n",
" lora_dropout=0.00,\n",
" task_type=\"CAUSAL_LM\",\n",
" target_modules=[\"q_proj\", \"k_proj\", \"v_proj\", \"o_proj\", \"fc1\", \"fc2\"]\n",
")\n",
"\n",
"model = get_peft_model(model, lora_config)\n",
"model.print_trainable_parameters()\n"
]
},
{
"cell_type": "markdown",
"id": "3f3533dd",
"metadata": {
"id": "3f3533dd"
},
"source": [
"## Demo the model\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0f930711",
"metadata": {
"id": "0f930711",
"outputId": "f263ab12-9b2c-4ea3-da1c-4465032538d2"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"================================================================================\n",
"TEST 1: Plain Autoregressive Prompt\n",
"================================================================================\n",
"Prompt: The Eiffel Tower stands in Paris and\n",
"\n",
"Generated: is one of the most famous landmarks in the world. It is located on the Champ de Mars in the heart of the city. The tower was built for the 1889 World's Fair. It was designed by the French engineer Gustave Eiffel and took 2 years to build. The Eiffel Tower stands 324 meters\n",
"================================================================================\n"
]
}
],
"source": [
"print(\"=\" * 80)\n",
"print(\"TEST 1: Plain Autoregressive Prompt\")\n",
"print(\"=\" * 80)\n",
"prompt = \"The Eiffel Tower stands in Paris and\"\n",
"test_inputs = tokenizer(prompt, return_tensors=\"pt\").to(device)\n",
"\n",
"\n",
"with torch.no_grad():\n",
" test_outputs = model.generate(\n",
" **test_inputs,\n",
" max_new_tokens=64,\n",
" do_sample=False,\n",
" pad_token_id=tokenizer.pad_token_id,\n",
" )\n",
"\n",
"generated_tokens = test_outputs[0, test_inputs[\"input_ids\"].shape[1] :]\n",
"print(f\"Prompt: {prompt}\")\n",
"print(f\"\\nGenerated: {tokenizer.decode(generated_tokens, skip_special_tokens=True)}\")\n",
"print(\"=\" * 80)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fbf80e5f",
"metadata": {
"id": "fbf80e5f",
"outputId": "86af20b4-3b9f-4dad-ba09-5dbb0de0f18c"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"================================================================================\n",
"TEST 2: Chat Template\n",
"================================================================================\n",
"Formatted prompt: <|bos|><|user_start|>What is the capital of France?<|user_end|><|assistant_start|>\n",
"Input IDs: [65527, 65528, 1442, 309, 261, 3429, 281, 4215, 63, 65529, 65530]\n",
"\n",
"Generated: The capital of France is Paris.<|assistant_end|>\n",
"================================================================================\n"
]
}
],
"source": [
"print(\"=\" * 80)\n",
"print(\"TEST 2: Chat Template\")\n",
"print(\"=\"*80)\n",
"conversation = [\n",
" {\"role\": \"user\", \"content\": \"What is the capital of France?\"},\n",
"]\n",
"\n",
"inputs = tokenizer.apply_chat_template(\n",
" conversation, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors=\"pt\"\n",
").to(device)\n",
"\n",
"print(f\"Formatted prompt: {tokenizer.decode(inputs['input_ids'][0])}\")\n",
"print(f\"Input IDs: {inputs['input_ids'][0].tolist()}\")\n",
"\n",
"with torch.no_grad():\n",
" outputs = model.generate(\n",
" **inputs,\n",
" max_new_tokens=64,\n",
" do_sample=False\n",
" )\n",
"\n",
"generated_tokens = outputs[0, inputs[\"input_ids\"].shape[1] :]\n",
"print(f\"\\nGenerated: {tokenizer.decode(generated_tokens)}\")\n",
"print(\"=\" * 80)\n"
]
},
{
"cell_type": "markdown",
"id": "a102e248",
"metadata": {
"id": "a102e248"
},
"source": [
"## Dataset\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b07e3b95",
"metadata": {
"id": "b07e3b95",
"outputId": "3c42b4d4-6e4f-4622-94cd-adbe53efa238"
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Generating train split: 100%|ββββββββββ| 52736/52736 [00:00<00:00, 1058243.18 examples/s]\n"
]
}
],
"source": [
"raw_dataset = load_dataset(\"HuggingFaceH4/OpenR1-Math-220k-default-verified\", split=\"train\")\n",
"splits = raw_dataset.train_test_split(test_size=0.1, seed=42)\n",
"train_dataset = splits[\"train\"]\n",
"eval_dataset = splits[\"test\"]\n"
]
},
{
"cell_type": "markdown",
"id": "21ec9078",
"metadata": {
"id": "21ec9078"
},
"source": [
"## Training Configuration\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "17a49557",
"metadata": {
"id": "17a49557"
},
"outputs": [],
"source": [
"max_train_steps = 50\n",
"prompt_batch_size = 1\n",
"num_generations = 4\n",
"max_new_tokens = 128\n",
"temperature = 1.0\n",
"top_k = 50\n",
"learning_rate = 5e-6\n",
"weight_decay = 0.0\n",
"epsilon = 0.2\n",
"gradient_accumulation_steps = 1\n",
"warmup_ratio = 0.1\n",
"logging_frequency = 5\n",
"max_train_samples = 1000\n",
"max_eval_samples = 100\n"
]
},
{
"cell_type": "markdown",
"id": "a8a12581",
"metadata": {
"id": "a8a12581"
},
"source": [
"## Reward Functions\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3f07953f",
"metadata": {
"id": "3f07953f"
},
"outputs": [],
"source": [
"import re\n",
"import numpy as np\n",
"import torch.nn.functional as F\n",
"from contextlib import nullcontext\n",
"\n",
"\n",
"def think_format_reward(completions):\n",
" \"\"\"\n",
" Reward function that checks if the reasoning process is enclosed within <think> and </think> tags.\n",
" Returns 1.0 if the format is correct, otherwise 0.0.\n",
" \"\"\"\n",
" pattern = r\"^(?!.*<think>)(.*?)</think>.*$\"\n",
" matches = [re.match(pattern, content, re.DOTALL | re.MULTILINE) for content in completions]\n",
" return [1.0 if match else 0.0 for match in matches]\n",
"\n",
"\n",
"def accuracy_reward(completions, solutions):\n",
" \"\"\"\n",
" Reward function that checks if the completion matches the solution.\n",
" For simplicity, we'll do basic string matching here.\n",
" \"\"\"\n",
" rewards = []\n",
" for completion, solution in zip(completions, solutions):\n",
" # Simple string matching (normalized)\n",
" reward = 1.0 if solution.strip().lower() in completion.strip().lower() else 0.0\n",
" rewards.append(reward)\n",
" return rewards\n",
"\n",
"\n",
"def min_length_reward(completions, min_length=10):\n",
" \"\"\"\n",
" Reward function that checks if the completion is at least a certain length.\n",
" Returns 1.0 if the length is greater than or equal to the minimum length, otherwise 0.0.\n",
" \"\"\"\n",
" return [1.0 if len(completion) >= min_length else 0.0 for completion in completions]\n",
"\n",
"def combined_reward(completions, solutions):\n",
" \"\"\"\n",
" Combines format and accuracy rewards with equal weight.\n",
" \"\"\"\n",
" format_rewards = think_format_reward(completions)\n",
" accuracy_rewards = accuracy_reward(completions, solutions)\n",
" min_length_rewards = min_length_reward(completions)\n",
" return [np.mean([f, a, m]) for f, a, m in zip(format_rewards, accuracy_rewards, min_length_rewards)]"
]
},
{
"cell_type": "markdown",
"id": "b2299e86",
"metadata": {
"id": "b2299e86"
},
"source": [
"## Helper Functions\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b0f0e9e4",
"metadata": {
"id": "b0f0e9e4"
},
"outputs": [],
"source": [
"def per_token_log_probs(logits, labels):\n",
" logits = logits.float()\n",
" log_probs = F.log_softmax(logits, dim=-1)\n",
" return log_probs.gather(dim=-1, index=labels.unsqueeze(-1)).squeeze(-1)\n",
"\n",
"\n",
"def prepare_prompt(example, problem_key=\"problem\", solution_key=\"solution\"):\n",
" # Extract the messages (should be a list of dicts with 'role' and 'content')\n",
" prompt = example.get(problem_key, \"\")\n",
" messages = [{\"role\": \"user\", \"content\": prompt}]\n",
"\n",
" formatted = tokenizer.apply_chat_template(\n",
" messages,\n",
" add_generation_prompt=True,\n",
" truncation=True,\n",
" max_length=2048,\n",
" padding=False,\n",
" return_dict=True,\n",
" return_tensors=\"pt\",\n",
" )\n",
" return formatted[\"input_ids\"], formatted[\"attention_mask\"]\n",
"\n",
"\n",
"if device.type == \"cuda\":\n",
" autocast_ctx = torch.amp.autocast(device_type=\"cuda\", dtype=torch.bfloat16)\n",
"else:\n",
" autocast_ctx = nullcontext()\n"
]
},
{
"cell_type": "markdown",
"id": "2756b691",
"metadata": {
"id": "2756b691"
},
"source": [
"## Optimizer and Scheduler\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e0e05495",
"metadata": {
"id": "e0e05495"
},
"outputs": [],
"source": [
"optimizer = torch.optim.AdamW(model.parameters(), lr=learning_rate, weight_decay=weight_decay)\n",
"total_update_steps = max_train_steps // gradient_accumulation_steps\n",
"warmup_steps = max(1, int(total_update_steps * warmup_ratio))\n",
"scheduler = get_linear_schedule_with_warmup(optimizer, warmup_steps, total_update_steps)\n"
]
},
{
"cell_type": "markdown",
"id": "5e2c7a2c",
"metadata": {
"id": "5e2c7a2c"
},
"source": [
"# The Training Loop\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "260f574c",
"metadata": {
"id": "260f574c",
"outputId": "b762165f-ed4a-4b22-cbb7-2fa203696ac3"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"step=0005 | loss=0.0000 | avg_reward=0.4000 | lr=0.00e+00\n",
"Sample eval completion: 3^4 - 11 and 3^6 - 17\n",
"step=0010 | loss=0.0000 | avg_reward=0.3333 | lr=0.00e+00\n",
"Sample eval completion: 11. \n",
"\n",
"This statement refers to an optimization problem where we seek to find the smallest prime \\( p\n",
"step=0015 | loss=0.0000 | avg_reward=0.4667 | lr=0.00e+00\n",
"Sample eval completion: What number has two prime factors, 1 and itself, without additional restrictions? One possible combi\n",
"step=0020 | loss=-0.0983 | avg_reward=0.4500 | lr=0.00e+00\n",
"Sample eval completion: \\[\\begin{bmatrix} 2 & 3\\\\ 6 & 11\\end{bmatrix} \\]\\[3^{a}-2^{b}\\left(\\frac{1^{a}}{a}\\right) \\left(\\fra\n",
"step=0025 | loss=-0.0979 | avg_reward=0.3333 | lr=0.00e+00\n",
"Sample eval completion: Let's examine the smallest prime \\( p \\) for which there do not exist non-negative integers \\( a, b \n",
"step=0030 | loss=-0.0000 | avg_reward=0.3667 | lr=0.00e+00\n",
"Sample eval completion: \n",
"Since \\( p = 23^2 + 7 \\) or \\( p \\ge 23^3 + 63 \\), and \\( p > 23 \\), we find that \\( p \\ge 9223 \\).\n",
"step=0035 | loss=0.0431 | avg_reward=0.4167 | lr=0.00e+00\n",
"Sample eval completion: \\[11 \\] = \\((3^5)\\), for all \\( a, b \\).\n",
"[asy]\n",
"import random;\n",
"import numpy as np;\n",
"\n",
"unitsize(1cm);\n",
"\n",
"d\n",
"step=0040 | loss=-0.0702 | avg_reward=0.5000 | lr=0.00e+00\n",
"Sample eval completion: 3^4 - 7\n",
"step=0045 | loss=0.0000 | avg_reward=0.3333 | lr=0.00e+00\n",
"Sample eval completion: 7.\n",
"step=0050 | loss=0.0000 | avg_reward=0.4000 | lr=0.00e+00\n",
"Sample eval completion: Here is the answer:\n",
"\n",
"The smallest prime \\( p \\) (where \\( p > 3 \\)) for which there do not exist non\n",
"Training complete.\n"
]
}
],
"source": [
"\n",
"# Sample dataset if needed\n",
"if max_train_samples is not None and len(train_dataset) > max_train_samples:\n",
" train_dataset = train_dataset.select(range(max_train_samples))\n",
"if max_eval_samples is not None and len(eval_dataset) > max_eval_samples:\n",
" eval_dataset = eval_dataset.select(range(max_eval_samples))\n",
"\n",
"model.train()\n",
"train_index = 0\n",
"global_step = 0\n",
"running_reward = 0.0\n",
"running_loss = 0.0\n",
"\n",
"for step in range(1, max_train_steps + 1):\n",
" example = train_dataset[train_index % len(train_dataset)]\n",
" train_index += 1\n",
"\n",
" prompt_ids, prompt_mask = prepare_prompt(example)\n",
" prompt_ids = prompt_ids.to(device)\n",
" prompt_mask = prompt_mask.to(device)\n",
" prompt_length = prompt_ids.shape[1]\n",
"\n",
" prompt_repeat = prompt_ids.repeat(num_generations, 1)\n",
" mask_repeat = prompt_mask.repeat(num_generations, 1)\n",
"\n",
" # Generate completions\n",
" model.eval()\n",
" with torch.no_grad():\n",
" generated = model.generate(\n",
" input_ids=prompt_repeat,\n",
" attention_mask=mask_repeat,\n",
" max_new_tokens=max_new_tokens,\n",
" do_sample=True,\n",
" temperature=temperature,\n",
" top_k=top_k,\n",
" pad_token_id=tokenizer.pad_token_id,\n",
" )\n",
" model.train()\n",
"\n",
" sequences = generated\n",
" attention_mask = (sequences != tokenizer.pad_token_id).long()\n",
" completion_mask = attention_mask.clone()\n",
" completion_mask[:, :prompt_length] = 0\n",
"\n",
" completion_tokens = sequences[:, prompt_length:]\n",
" completion_texts = tokenizer.batch_decode(completion_tokens, skip_special_tokens=True)\n",
"\n",
" # Get solution\n",
" solution = example.get(\"solution\", example.get(\"answer\", \"\"))\n",
" solutions = [solution] * num_generations\n",
"\n",
" # Compute rewards\n",
" rewards = combined_reward(completion_texts, solutions)\n",
" rewards = torch.tensor(rewards, dtype=torch.float32, device=device)\n",
" running_reward += rewards.mean().item()\n",
"\n",
" rewards_view = rewards.view(prompt_batch_size, num_generations)\n",
" mean_rewards = rewards_view.mean(dim=1, keepdim=True)\n",
" std_rewards = rewards_view.std(dim=1, keepdim=True)\n",
" std_rewards = torch.where(std_rewards > 0, std_rewards, torch.ones_like(std_rewards))\n",
" advantages = ((rewards_view - mean_rewards) / std_rewards).view(-1)\n",
"\n",
" labels = sequences[:, 1:].clone()\n",
" labels[attention_mask[:, 1:] == 0] = tokenizer.pad_token_id\n",
"\n",
" # Compute old log probs\n",
" with torch.no_grad():\n",
" with (autocast_ctx if device.type == \"cuda\" else nullcontext()):\n",
" old_outputs = model(\n",
" input_ids=sequences,\n",
" attention_mask=attention_mask,\n",
" use_cache=False,\n",
" )\n",
" old_log_probs = per_token_log_probs(old_outputs.logits[:, :-1], labels)\n",
"\n",
" valid_mask = (completion_mask[:, 1:] == 1) & (labels != tokenizer.pad_token_id)\n",
"\n",
" # Compute loss\n",
" optimizer.zero_grad(set_to_none=True)\n",
" with (autocast_ctx if device.type == \"cuda\" else nullcontext()):\n",
" outputs = model(\n",
" input_ids=sequences,\n",
" attention_mask=attention_mask,\n",
" use_cache=False,\n",
" )\n",
" log_probs = per_token_log_probs(outputs.logits[:, :-1], labels)\n",
"\n",
" ratio = (log_probs - old_log_probs).exp()\n",
" ratio = torch.where(valid_mask, ratio, torch.ones_like(ratio))\n",
" clipped_ratio = ratio.clamp(1.0 - epsilon, 1.0 + epsilon)\n",
"\n",
" adv = advantages.unsqueeze(1)\n",
" loss_unclipped = ratio * adv\n",
" loss_clipped = clipped_ratio * adv\n",
" per_token_loss = -torch.min(loss_unclipped, loss_clipped)\n",
" per_token_loss = torch.where(valid_mask, per_token_loss, torch.zeros_like(per_token_loss))\n",
"\n",
" denom = valid_mask.sum().clamp(min=1)\n",
" loss = per_token_loss.sum() / denom\n",
"\n",
" loss.backward()\n",
" torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0)\n",
" optimizer.step()\n",
" scheduler.step()\n",
"\n",
" global_step += 1\n",
" running_loss += loss.item()\n",
"\n",
" if step % logging_frequency == 0:\n",
" avg_reward = running_reward / logging_frequency\n",
" avg_loss = running_loss / logging_frequency\n",
" current_lr = scheduler.get_last_lr()[0]\n",
" print(\n",
" f\"step={step:04d} | loss={avg_loss:.4f} | avg_reward={avg_reward:.4f} | lr={current_lr:.2e}\"\n",
" )\n",
" running_reward = 0.0\n",
" running_loss = 0.0\n",
"\n",
" # Sample evaluation\n",
" model.eval()\n",
" eval_example = eval_dataset[0]\n",
" prompt_ids, prompt_mask = prepare_prompt(eval_example)\n",
" with torch.no_grad():\n",
" eval_sequences = model.generate(\n",
" input_ids=prompt_ids.to(device),\n",
" attention_mask=prompt_mask.to(device),\n",
" max_new_tokens=max_new_tokens,\n",
" do_sample=True,\n",
" top_k=top_k,\n",
" temperature=temperature,\n",
" pad_token_id=tokenizer.pad_token_id,\n",
" )\n",
" model.train()\n",
" completion = eval_sequences[0, prompt_ids.shape[1] :]\n",
" print(\"Sample eval completion:\", tokenizer.decode(completion, skip_special_tokens=True)[:100])\n",
"\n",
"print(\"Training complete.\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2104662d",
"metadata": {
"id": "2104662d"
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.18"
},
"colab": {
"provenance": []
}
},
"nbformat": 4,
"nbformat_minor": 5
} |