Skip to content

armgit / laya

Public Imported from GitHub
0

Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.

feat: increase token budget to 1024 tokens in Kaggle notebook

Nandakishora29dcf8cf181
diff --git a/notebooks/laya_finetune_typed_decisions_2xT4_kaggle.ipynb b/notebooks/laya_finetune_typed_decisions_2xT4_kaggle.ipynb
index c39021e6..6fc86180 100644
--- a/notebooks/laya_finetune_typed_decisions_2xT4_kaggle.ipynb
+++ b/notebooks/laya_finetune_typed_decisions_2xT4_kaggle.ipynb
@@ -158,7 +158,7 @@
"metadata": {},
"source": [
"%%writefile /kaggle/working/train_ddp.py\n",
- "import os, sys, time, json, random\n",
+ "import os, sys, time, json, random, math\n",
"import torch\n",
"import torch.distributed as dist\n",
"from torch.nn.parallel import DistributedDataParallel as DDP\n",
@@ -191,6 +191,25 @@
" \"label\": torch.tensor([it[\"label\"] for it in items])\n",
" }\n",
"\n",
+ "def fit_one_temp(sel):\n",
+ " if len(sel) < 10:\n",
+ " return 1.0\n",
+ " kmax = max(len(z) for z, _ in sel)\n",
+ " Z = torch.full((len(sel), kmax), -1e4)\n",
+ " T = torch.zeros((len(sel), kmax))\n",
+ " for i, (z, t) in enumerate(sel):\n",
+ " Z[i, :len(z)] = torch.tensor(z)\n",
+ " T[i, :len(t)] = torch.tensor(t, dtype=torch.float32)\n",
+ " log_t = torch.zeros(1, requires_grad=True)\n",
+ " opt = torch.optim.LBFGS([log_t], lr=0.1, max_iter=100)\n",
+ " def closure():\n",
+ " opt.zero_grad()\n",
+ " loss = -(T * torch.log_softmax(Z / log_t.exp(), -1)).sum(-1).mean()\n",
+ " loss.backward()\n",
+ " return loss\n",
+ " opt.step(closure)\n",
+ " return float(torch.clamp(log_t.exp(), 0.1, 10.0).item())\n",
+ "\n",
"def main():\n",
" dist.init_process_group(\"nccl\")\n",
" rank = dist.get_rank()\n",
@@ -221,17 +240,17 @@
" ddp_model = DDP(model, device_ids=[local_rank], find_unused_parameters=True)\n",
" \n",
" all_items = torch.load(\"/kaggle/working/train_items.pt\", weights_only=False)\n",
- " # Split items evenly across ranks\n",
" my_items = all_items[rank::world_size]\n",
" \n",
- " EPOCHS = 2\n",
- " MICRO_BATCH = 10 # 10 sequences per forward pass per GPU\n",
- " GRAD_ACCUM = 2 # Effective batch across 2 GPUs = 40 sequences\n",
- " GROUP_SIZE = 4\n",
- " LR_ENCODER = 1.5e-5\n",
- " LR_HEAD = 1.5e-4\n",
- " SIGMA_START = 0.5\n",
- " SIGMA_END = 0.2\n",
+ " # 4 Epochs with Cosine Annealing and soft-teacher guidance\n",
+ " EPOCHS = 4\n",
+ " MICRO_BATCH = 8 # 8 sequences per GPU: fits comfortably up to 1024 tokens!\n",
+ " GRAD_ACCUM = 4 # Effective batch across 2 GPUs = 64 sequences (8 * 2 * 4)\n",
+ " GROUP_SIZE = 4 # GRPO baseline samples\n",
+ " LR_ENCODER = 2.0e-5 # Encoder adaptation rate\n",
+ " LR_HEAD = 1.2e-4 # Head adaptation rate\n",
+ " SIGMA_START = 0.5 # Exploration noise\n",
+ " SIGMA_END = 0.15\n",
"\n",
" enc_params = [p for n, p in ddp_model.named_parameters() if \"encoder.\" in n]\n",
" head_params = [p for n, p in ddp_model.named_parameters() if \"encoder.\" not in n]\n",
@@ -241,10 +260,12 @@
" {\"params\": head_params, \"lr\": LR_HEAD}\n",
" ], weight_decay=0.01)\n",
" \n",
+ " total_steps = (len(my_items) // (MICRO_BATCH * GRAD_ACCUM)) * EPOCHS\n",
+ " scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=max(1, total_steps), eta_min=1e-6)\n",
" scaler = torch.amp.GradScaler(\"cuda\", enabled=True)\n",
" \n",
" if rank == 0:\n",
- " print(f\"Starting 2xT4 DDP training: {len(all_items)} total items | {len(my_items)} per rank\")\n",
+ " print(f\"Starting 2xT4 DDP training: {len(all_items)} total items | {len(my_items)} per rank | {EPOCHS} epochs\")\n",
" t0 = time.time()\n",
" \n",
" for epoch in range(EPOCHS):\n",
@@ -276,20 +297,25 @@
" logits = logits.float()\n",
" mask = batch[\"marker_mask\"].to(device)\n",
" k = mask.sum(-1, keepdim=True).float()\n",
+ " target = batch[\"target\"].to(device)\n",
" \n",
+ " # 1. Sample G noisy logit distributions with zero-mean projection\n",
" eps = torch.randn((GROUP_SIZE,) + logits.shape, device=device) * sigma * mask\n",
" eps = (eps - eps.sum(-1, keepdim=True) / k) * mask\n",
" z = logits.detach().unsqueeze(0) + eps\n",
" q = torch.softmax(z.masked_fill(~mask, -1e4), -1)\n",
" \n",
- " target = batch[\"target\"].to(device)\n",
+ " # 2. Evaluate proper scoring reward (w_sph=0.75 for soft target matching)\n",
" with torch.no_grad():\n",
- " r = proper_reward(q, target.unsqueeze(0), batch[\"qtype\"].to(device), mask)\n",
+ " r = proper_reward(q, target.unsqueeze(0), batch[\"qtype\"].to(device), mask, w_sph=0.75, w_rps=1.0)\n",
" adv = r - r.mean(0, keepdim=True)\n",
" adv = adv / (adv.std() + 1e-6)\n",
" \n",
+ " # 3. Policy gradient loss + auxiliary soft cross-entropy guidance (0.5 weight)\n",
" logp = -(((z - logits.unsqueeze(0)) ** 2) * mask).sum(-1) / (2 * sigma ** 2)\n",
- " loss = -(adv * logp).mean() / GRAD_ACCUM + 0.0 * act.sum()\n",
+ " loss_rl = -(adv * logp).mean()\n",
+ " loss_ce = -(target * torch.log_softmax(logits.masked_fill(~mask, -1e4), -1)).sum(-1).mean()\n",
+ " loss = (loss_rl + 0.5 * loss_ce) / GRAD_ACCUM + 0.0 * act.sum()\n",
" \n",
" scaler.scale(loss).backward()\n",
" accum_step += 1\n",
@@ -299,26 +325,57 @@
" torch.nn.utils.clip_grad_norm_(ddp_model.parameters(), 1.0)\n",
" scaler.step(optimizer)\n",
" scaler.update()\n",
+ " scheduler.step()\n",
" optimizer.zero_grad(set_to_none=True)\n",
" \n",
" epoch_loss += loss.item() * GRAD_ACCUM\n",
" n_batches += 1\n",
" \n",
" if rank == 0 and (n_batches % 50) == 0:\n",
- " print(f\" Epoch {epoch+1}/{EPOCHS} | Step {n_batches} | Loss: {loss.item()*GRAD_ACCUM:.4f} | Reward: {r.mean().item():.3f}\")\n",
+ " cur_lr = scheduler.get_last_lr()[0]\n",
+ " print(f\" Epoch {epoch+1}/{EPOCHS} | Step {n_batches} | Loss: {loss.item()*GRAD_ACCUM:.4f} | Reward: {r.mean().item():.3f} | LR: {cur_lr:.2e}\")\n",
"\n",
" if rank == 0:\n",
" print(f\"=== Epoch {epoch+1}/{EPOCHS} Completed in {time.time()-t0:.1f}s | Avg Loss: {epoch_loss/max(1, n_batches):.4f} ===\")\n",
"\n",
" dist.barrier()\n",
+ " \n",
+ " # Post-training temperature calibration on rank 0\n",
" if rank == 0:\n",
+ " print(\"\\nFitting post-training calibration temperatures...\")\n",
+ " model.eval()\n",
+ " calib_items = all_items[:150]\n",
+ " calib_batch = collate_train_batch(calib_items, tok.pad_token_id)\n",
+ " with torch.no_grad():\n",
+ " with torch.autocast(\"cuda\", dtype=torch.float16):\n",
+ " logits_cal, _ = model(\n",
+ " calib_batch[\"input_ids\"].to(device),\n",
+ " calib_batch[\"attention_mask\"].to(device),\n",
+ " calib_batch[\"marker_pos\"].to(device),\n",
+ " calib_batch[\"marker_mask\"].to(device),\n",
+ " calib_batch[\"qtype\"].to(device)\n",
+ " )\n",
+ " logits_cal = logits_cal.float().cpu().numpy()\n",
+ " target_cal = calib_batch[\"target\"].numpy()\n",
+ " qtypes_cal = calib_batch[\"qtype\"].numpy()\n",
+ " \n",
+ " fitted_temps = [1.0, 1.0, 1.0]\n",
+ " for qt in range(3):\n",
+ " sel = [(logits_cal[idx, :len(calib_items[idx][\"markers\"])], target_cal[idx, :len(calib_items[idx][\"markers\"])])\n",
+ " for idx in range(len(calib_items)) if qtypes_cal[idx] == qt]\n",
+ " if sel:\n",
+ " fitted_temps[qt] = fit_one_temp(sel)\n",
+ " print(\"Fitted calibration temperatures (choice, score, noul):\", [round(t, 3) for t in fitted_temps])\n",
+ " \n",
" os.makedirs(output_dir, exist_ok=True)\n",
" sd = {k: v.half().contiguous().cpu() for k, v in model.state_dict().items()}\n",
" save_file(sd, os.path.join(output_dir, \"model.safetensors\"))\n",
" model.encoder.config.save_pretrained(os.path.join(output_dir, \"encoder\"))\n",
" tok.save_pretrained(os.path.join(output_dir, \"tokenizer\"))\n",
+ " \n",
" cfg[\"fine_tuned\"] = True\n",
" cfg[\"model_name\"] = \"laya-typed-decisions\"\n",
+ " cfg[\"temperature\"] = fitted_temps\n",
" with open(os.path.join(output_dir, \"rl_agent_config.json\"), \"w\") as f:\n",
" json.dump(cfg, f, indent=2)\n",
" print(f\"Model successfully saved to {output_dir}!\")\n",