RayMelius Claude Opus 4.6 commited on
Commit
688b587
Β·
1 Parent(s): 643d6a4

Restructure fine-tune notebook: Kaggle+Colab resume, checkpoint cleanup

Browse files

- Move checkpoint detection to beginning for immediate resume visibility
- Add Kaggle dataset download for checkpoint resume across sessions
- Keep only last 3 checkpoints in persistent storage (Kaggle/Drive)
- Remove duplicate code cells, fix section numbering
- Add stockex-clearing-house-llm-fine-tuning.ipynb (Kaggle version)
- Replace ch_trader_finetune.ipynb with unified notebook

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>

notebooks/ch_trader_finetune.ipynb CHANGED
@@ -1,6 +1,4 @@
1
  {
2
- "nbformat": 4,
3
- "nbformat_minor": 5,
4
  "metadata": {
5
  "kernelspec": {
6
  "display_name": "Python 3",
@@ -9,337 +7,1284 @@
9
  },
10
  "language_info": {
11
  "name": "python",
12
- "version": "3.10.0"
 
 
 
 
 
 
 
 
13
  },
14
  "accelerator": "GPU",
15
  "colab": {
16
- "gpuType": "A100",
17
  "provenance": []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  }
19
  },
 
 
20
  "cells": [
21
  {
22
  "cell_type": "markdown",
23
- "id": "title",
24
- "metadata": {},
25
- "source": "# StockEx Clearing House β€” LLM Fine-Tuning\n\nFine-tunes a Qwen2.5 Instruct model with QLoRA to act as a clearing house trading agent.\n\n**Auto-selects model size based on available VRAM:**\n| GPU | VRAM | Model |\n|-----|------|-------|\n| T4 (free) | 15 GB | Qwen2.5-7B-Instruct |\n| A100 40 GB | 40 GB | Qwen2.5-14B-Instruct |\n| A100 80 GB | 80 GB | Qwen2.5-32B-Instruct |\n\n**Output model:** `RayMelius/stockex-ch-trader` on HuggingFace Hub\n\n**Required secret:** Add `HF_TOKEN` in Colab β†’ Secrets (πŸ”‘ icon in left sidebar)"
 
26
  },
27
  {
28
  "cell_type": "code",
29
- "execution_count": null,
30
- "id": "install",
31
- "metadata": {},
 
 
 
32
  "outputs": [],
33
- "source": "# ── Install dependencies ──────────────────────────────────��────────────────────\n# Reinstall bitsandbytes with proper CUDA support (fixes triton.ops error on Colab)\n!pip install -q -U bitsandbytes\n!pip install -q \\\n \"transformers>=4.46.3\" \\\n \"peft>=0.13.2\" \\\n \"trl>=0.12.1\" \\\n \"datasets>=3.1.0\" \\\n \"accelerate>=1.1.1\" \\\n huggingface_hub\nprint(\"Dependencies installed.\")"
34
  },
35
  {
36
  "cell_type": "code",
37
- "execution_count": null,
38
- "id": "imports",
39
- "metadata": {},
 
 
 
40
  "outputs": [],
41
- "source": [
42
- "import os, json, random, torch\n",
43
- "from datasets import Dataset\n",
44
- "from transformers import (\n",
45
- " AutoTokenizer, AutoModelForCausalLM,\n",
46
- " BitsAndBytesConfig, TrainingArguments,\n",
47
- ")\n",
48
- "from peft import LoraConfig, get_peft_model, TaskType\n",
49
- "from trl import SFTTrainer, SFTConfig\n",
50
- "from huggingface_hub import login\n",
51
- "\n",
52
- "print(f\"CUDA available: {torch.cuda.is_available()}\")\n",
53
- "if torch.cuda.is_available():\n",
54
- " print(f\"GPU: {torch.cuda.get_device_name(0)}\")\n",
55
- " print(f\"VRAM: {torch.cuda.get_device_properties(0).total_memory / 1e9:.1f} GB\")"
56
- ]
57
  },
58
  {
59
  "cell_type": "code",
60
- "execution_count": null,
61
- "id": "config",
62
- "metadata": {},
 
 
 
63
  "outputs": [],
64
- "source": "# ── Auto-select model based on available VRAM ─────────────────────────────────\nimport torch\n\nassert torch.cuda.is_available(), \"No GPU found β€” change runtime to GPU.\"\nvram_gb = torch.cuda.get_device_properties(0).total_memory / 1e9\ngpu_name = torch.cuda.get_device_name(0)\nprint(f\"GPU: {gpu_name} | VRAM: {vram_gb:.1f} GB\")\n\nif vram_gb >= 70:\n BASE_MODEL = \"Qwen/Qwen2.5-32B-Instruct\"\n BATCH_SIZE, GRAD_ACCUM, LR = 1, 16, 1e-4\nelif vram_gb >= 35:\n BASE_MODEL = \"Qwen/Qwen2.5-14B-Instruct\"\n BATCH_SIZE, GRAD_ACCUM, LR = 2, 8, 1e-4\nelse: # T4 / 15 GB\n BASE_MODEL = \"Qwen/Qwen2.5-7B-Instruct\"\n BATCH_SIZE, GRAD_ACCUM, LR = 4, 4, 2e-4\n\nprint(f\"Selected model: {BASE_MODEL}\")\n\n# ── Fixed config ───────────────────────────────────────────────────────────────\nOUTPUT_REPO = \"RayMelius/stockex-ch-trader\"\nOUTPUT_DIR = \"./stockex-ch-trader\"\nLORA_R = 16\nLORA_ALPHA = 32\nLORA_DROPOUT = 0.05\nNUM_EPOCHS = 3\nMAX_SEQ_LEN = 512\nDATASET_SIZE = 2500\n\n# ── HuggingFace login ─────────────────────────────────────────────────────────\nimport os\ntry:\n from google.colab import userdata\n HF_TOKEN = userdata.get(\"HF_TOKEN\")\n print(\"HF_TOKEN loaded from Colab Secrets\")\nexcept Exception:\n HF_TOKEN = os.getenv(\"HF_TOKEN\", \"\")\n\nif not HF_TOKEN:\n raise ValueError(\"HF_TOKEN not found. Add it in Colab β†’ Secrets (πŸ”‘).\")\n\nfrom huggingface_hub import login\nlogin(token=HF_TOKEN)\nprint(\"Logged in to HuggingFace Hub\")"
65
  },
66
  {
67
  "cell_type": "markdown",
68
- "id": "dataset-header",
69
- "metadata": {},
70
- "source": [
71
- "## 1. Synthetic Dataset Generation\n",
72
- "\n",
73
- "Each training example is a realistic clearing house trading scenario:\n",
74
- "- Member state: capital, holdings, obligation remaining\n",
75
- "- Market: BBO for each security\n",
76
- "- Target: a valid JSON trading decision that respects all constraints"
77
- ]
78
  },
79
  {
80
- "cell_type": "markdown",
81
- "id": "g7cean6ejyj",
82
- "source": "## 1b. Checkpoint Setup (Resumable Training)\n\nCheckpoints persist across session restarts for both **Kaggle** and **Colab**:\n\n| Platform | Restore from | Save to |\n|----------|-------------|---------|\n| **Kaggle** | `/kaggle/input/stockex-ch-checkpoints/` (dataset) | `/kaggle/working/stockex-ch-checkpoints/` (output) |\n| **Colab** | Google Drive `stockex-ch-checkpoints/` | Google Drive `stockex-ch-checkpoints/` |\n\nAdapters are also pushed to HF Hub every `SAVE_STEPS` steps as a backup.",
83
- "metadata": {}
 
 
 
 
 
84
  },
85
  {
86
- "cell_type": "code",
87
- "id": "egq0dp9csuo",
88
- "source": "import shutil, math\nfrom transformers.trainer_utils import get_last_checkpoint\n\n# ── Detect platform ───────────────────────────────────────────────────────────\nON_KAGGLE = os.path.isdir(\"/kaggle\")\nON_COLAB = not ON_KAGGLE\n\n# ── Platform-specific checkpoint dirs ─────────────────────────────────────────\nDRIVE_CKPT_DIR = None\nKAGGLE_INPUT_CKPT_DIR = \"/kaggle/input/stockex-ch-checkpoints\"\nKAGGLE_OUTPUT_CKPT_DIR = \"/kaggle/working/stockex-ch-checkpoints\"\n\nif ON_COLAB:\n try:\n from google.colab import drive\n drive.mount(\"/content/drive\", force_remount=False)\n DRIVE_CKPT_DIR = \"/content/drive/MyDrive/stockex-ch-checkpoints\"\n os.makedirs(DRIVE_CKPT_DIR, exist_ok=True)\n print(f\"[Colab] Google Drive mounted. Checkpoints β†’ {DRIVE_CKPT_DIR}\")\n except Exception:\n print(\"[Colab] Google Drive not available β€” checkpoints saved locally only.\")\nelif ON_KAGGLE:\n os.makedirs(KAGGLE_OUTPUT_CKPT_DIR, exist_ok=True)\n print(f\"[Kaggle] Checkpoints restore ← {KAGGLE_INPUT_CKPT_DIR}\")\n print(f\"[Kaggle] Checkpoints save β†’ {KAGGLE_OUTPUT_CKPT_DIR}\")\n\n# ── Restore checkpoint to local OUTPUT_DIR ────────────────────────────────────\nos.makedirs(OUTPUT_DIR, exist_ok=True)\n\n# Pick the right source directory for restoring\nrestore_dir = None\nif ON_KAGGLE and os.path.isdir(KAGGLE_INPUT_CKPT_DIR):\n restore_dir = KAGGLE_INPUT_CKPT_DIR\nelif ON_COLAB and DRIVE_CKPT_DIR and os.path.isdir(DRIVE_CKPT_DIR):\n restore_dir = DRIVE_CKPT_DIR\n\nif restore_dir:\n remote_ckpt = get_last_checkpoint(restore_dir)\n if remote_ckpt:\n local_ckpt_name = os.path.basename(remote_ckpt)\n local_ckpt_path = os.path.join(OUTPUT_DIR, local_ckpt_name)\n if not os.path.exists(local_ckpt_path):\n print(f\"Restoring checkpoint: {remote_ckpt}\")\n shutil.copytree(remote_ckpt, local_ckpt_path)\n else:\n print(f\"No checkpoint found in {restore_dir}\")\n\n# ── Detect latest local checkpoint ────────────────────────────────────────────\nRESUME_FROM = get_last_checkpoint(OUTPUT_DIR)\n\nSAVE_STEPS = 10\n\n# ── Resume summary ─────────────────────────────────────────────────────────────\nif RESUME_FROM:\n completed_steps = int(os.path.basename(RESUME_FROM).split(\"-\")[-1])\n train_size = int(DATASET_SIZE * 0.9)\n steps_per_epoch = math.ceil(train_size / (BATCH_SIZE * GRAD_ACCUM))\n total_steps = steps_per_epoch * NUM_EPOCHS\n remaining = max(0, total_steps - completed_steps)\n pct_done = 100 * completed_steps / total_steps\n epoch_done = completed_steps / steps_per_epoch\n\n print(\"\\n\" + \"=\" * 55)\n print(\" RESUMING FROM CHECKPOINT\")\n print(\"=\" * 55)\n print(f\" Checkpoint : {os.path.basename(RESUME_FROM)}\")\n print(f\" Steps done : {completed_steps:,} / {total_steps:,} ({pct_done:.1f}%)\")\n print(f\" Steps left : {remaining:,}\")\n print(f\" Epoch : {epoch_done:.2f} / {NUM_EPOCHS}\")\n print(f\" Epochs left : {NUM_EPOCHS - epoch_done:.2f}\")\n print(f\" Steps/epoch : {steps_per_epoch:,}\")\n print(f\" Save every : {SAVE_STEPS} steps\")\n print(\"=\" * 55 + \"\\n\")\nelse:\n train_size = int(DATASET_SIZE * 0.9)\n steps_per_epoch = math.ceil(train_size / (BATCH_SIZE * GRAD_ACCUM))\n total_steps = steps_per_epoch * NUM_EPOCHS\n print(\"\\n\" + \"=\" * 55)\n print(\" STARTING FRESH\")\n print(\"=\" * 55)\n print(f\" Total steps : {total_steps:,}\")\n print(f\" Steps/epoch : {steps_per_epoch:,}\")\n print(f\" Epochs : {NUM_EPOCHS}\")\n print(f\" Save every : {SAVE_STEPS} steps\")\n print(\"=\" * 55 + \"\\n\")",
89
- "metadata": {},
90
- "execution_count": null,
91
- "outputs": []
 
92
  },
93
  {
94
  "cell_type": "code",
95
- "execution_count": null,
96
- "id": "dataset-gen",
97
- "metadata": {},
 
 
 
98
  "outputs": [],
99
- "source": "# Securities from shared_data/securities.txt (symbol, start_price, current_price)\nSECURITIES = [\n {\"symbol\": \"ALPHA\", \"base\": 5.65},\n {\"symbol\": \"PEIR\", \"base\": 8.35},\n {\"symbol\": \"EXAE\", \"base\": 6.90},\n {\"symbol\": \"QUEST\", \"base\": 13.35},\n {\"symbol\": \"NBG\", \"base\": 8.00},\n {\"symbol\": \"EUROB\", \"base\": 3.45},\n {\"symbol\": \"AEG\", \"base\": 4.75},\n {\"symbol\": \"INTKA\", \"base\": 7.35},\n {\"symbol\": \"AAAK\", \"base\": 2.75},\n {\"symbol\": \"ATTIK\", \"base\": 4.90},\n]\n\nSTARTING_CAPITAL = 100_000.0\nDAILY_OBLIGATION = 10\n\n\ndef gen_bbo(base_price: float) -> dict:\n \"\"\"Generate a realistic bid/ask spread around a base price.\"\"\"\n drift = random.uniform(-0.05, 0.05)\n mid = round(base_price * (1 + drift), 2)\n spread = round(random.choice([0.05, 0.10, 0.15]), 2)\n best_bid = round(mid - spread / 2, 2)\n best_ask = round(mid + spread / 2, 2)\n return {\"best_bid\": best_bid, \"best_ask\": best_ask, \"mid\": mid}\n\n\ndef gen_holdings(bbos: dict) -> list:\n \"\"\"Randomly generate some holdings for a member.\"\"\"\n holdings = []\n n = random.randint(0, 4)\n for sym in random.sample(list(bbos.keys()), min(n, len(bbos))):\n qty = random.randint(50, 500)\n mid = bbos[sym][\"mid\"]\n avg_cost = round(mid * random.uniform(0.92, 1.08), 2)\n holdings.append({\"symbol\": sym, \"quantity\": qty, \"avg_cost\": avg_cost})\n return holdings\n\n\ndef build_prompt(member_id: str, capital: float, holdings: list,\n obligation_remaining: int, bbos: dict) -> str:\n market_lines = [\n f\" {sym}: Bid {bbo['best_bid']:.2f} / Ask {bbo['best_ask']:.2f}\"\n for sym, bbo in sorted(bbos.items())\n ]\n holding_lines = (\n [f\" {h['symbol']}: {h['quantity']} shares @ avg cost {h['avg_cost']:.2f}\"\n for h in holdings]\n if holdings else [\" None\"]\n )\n return (\n f\"You are simulating clearing house member {member_id} making ONE trading decision.\\n\\n\"\n f\"Member state:\\n\"\n f\" Available capital: EUR {capital:,.2f}\\n\"\n f\" Securities obligation remaining today: {obligation_remaining} more to trade\\n\"\n f\" Current holdings:\\n\" + \"\\n\".join(holding_lines) + \"\\n\\n\"\n f\"Current market (Bid/Ask):\\n\" + \"\\n\".join(market_lines) + \"\\n\\n\"\n f\"Rules:\\n\"\n f\"- Do not spend more than your available capital\\n\"\n f\"- Do not sell more shares than you hold\\n\"\n f\"- If you have no holdings, you must BUY\\n\"\n f\"- Choose a realistic price close to the BBO mid-price\\n\"\n f\"- Quantity should be between 10 and 200\\n\\n\"\n f\"Respond ONLY with valid JSON, no other text:\\n\"\n f'Example: {{\"symbol\": \"ALPHA\", \"side\": \"BUY\", \"quantity\": 50, \"price\": 5.65}}'\n )\n\n\ndef gen_decision(capital: float, holdings: list, bbos: dict) -> dict:\n \"\"\"Generate a rule-valid trading decision for the given state.\"\"\"\n holdings_value = sum(\n h[\"quantity\"] * bbos.get(h[\"symbol\"], {}).get(\"mid\", h[\"avg_cost\"])\n for h in holdings\n )\n net_worth = capital + holdings_value\n holdings_ratio = holdings_value / net_worth if net_worth > 0 else 0\n\n if not holdings:\n side = \"BUY\"\n elif holdings_ratio > 0.6:\n side = random.choices([\"SELL\", \"BUY\"], weights=[0.7, 0.3])[0]\n else:\n side = random.choices([\"BUY\", \"SELL\"], weights=[0.55, 0.45])[0]\n\n if side == \"BUY\":\n affordable = [sym for sym, bbo in bbos.items() if 10 * bbo[\"best_ask\"] <= capital]\n if not affordable:\n sym = min(bbos, key=lambda s: bbos[s][\"best_ask\"])\n else:\n held_syms = [h[\"symbol\"] for h in holdings]\n weights = [3 if s in held_syms else 1 for s in affordable]\n sym = random.choices(affordable, weights=weights)[0]\n ask = bbos[sym][\"best_ask\"]\n max_qty = min(200, int(capital / ask))\n qty = random.randint(10, max(10, max_qty))\n price = round(bbos[sym][\"mid\"] + random.uniform(-0.05, 0.05), 2)\n price = max(bbos[sym][\"best_bid\"], min(price, ask))\n return {\"symbol\": sym, \"side\": \"BUY\", \"quantity\": qty, \"price\": round(price, 2)}\n else:\n h = random.choice(holdings)\n sym = h[\"symbol\"]\n bbo = bbos[sym]\n qty = random.randint(10, min(200, h[\"quantity\"]))\n price = round(bbo[\"mid\"] + random.uniform(-0.05, 0.05), 2)\n price = max(bbo[\"best_bid\"] - 0.05, min(price, bbo[\"best_ask\"]))\n return {\"symbol\": sym, \"side\": \"SELL\", \"quantity\": qty, \"price\": round(price, 2)}\n\n\ndef generate_dataset(n: int) -> list:\n examples = []\n member_ids = [f\"USR{i:02d}\" for i in range(1, 11)]\n scenarios = [\n ((80_000, 100_000), (5, 10), \"fresh_member\"),\n ((50_000, 80_000), (0, 5), \"active_member\"),\n ((20_000, 50_000), (0, 2), \"low_capital\"),\n ((5_000, 20_000), (0, 10), \"very_low_capital\"),\n ((90_000, 100_000), (10, 10),\"start_of_day\"),\n ]\n for _ in range(n):\n cap_range, obl_range, _ = random.choice(scenarios)\n capital = round(random.uniform(*cap_range), 2)\n obligation = random.randint(*obl_range)\n member_id = random.choice(member_ids)\n bbos = {s[\"symbol\"]: gen_bbo(s[\"base\"]) for s in SECURITIES}\n holdings = gen_holdings(bbos)\n holdings_cost = sum(h[\"quantity\"] * h[\"avg_cost\"] for h in holdings)\n if holdings_cost > STARTING_CAPITAL - capital:\n scale = (STARTING_CAPITAL - capital) / max(holdings_cost, 1)\n for h in holdings:\n h[\"quantity\"] = max(10, int(h[\"quantity\"] * scale))\n prompt = build_prompt(member_id, capital, holdings, obligation, bbos)\n decision = gen_decision(capital, holdings, bbos)\n examples.append({\"prompt\": prompt, \"completion\": json.dumps(decision)})\n return examples\n\n\nprint(f\"Generating {DATASET_SIZE} training examples...\")\nraw_data = generate_dataset(DATASET_SIZE)\nprint(f\"Done. Example:\")\nprint(\"PROMPT:\\n\", raw_data[0][\"prompt\"])\nprint(\"\\nCOMPLETION:\", raw_data[0][\"completion\"])"
100
  },
101
  {
102
  "cell_type": "code",
103
- "execution_count": null,
104
- "id": "dataset-split",
105
- "metadata": {},
 
 
 
106
  "outputs": [],
107
- "source": [
108
- "# Train/val split (90/10)\n",
109
- "random.shuffle(raw_data)\n",
110
- "split = int(len(raw_data) * 0.9)\n",
111
- "train_data = raw_data[:split]\n",
112
- "val_data = raw_data[split:]\n",
113
- "\n",
114
- "train_dataset = Dataset.from_list(train_data)\n",
115
- "val_dataset = Dataset.from_list(val_data)\n",
116
- "print(f\"Train: {len(train_dataset)} | Val: {len(val_dataset)}\")"
117
- ]
118
  },
119
  {
120
  "cell_type": "markdown",
121
- "id": "model-header",
122
- "metadata": {},
123
- "source": [
124
- "## 2. Load Base Model (4-bit QLoRA)"
125
- ]
126
  },
127
  {
128
  "cell_type": "code",
129
- "execution_count": null,
130
- "id": "load-tokenizer",
131
- "metadata": {},
 
 
 
132
  "outputs": [],
133
- "source": "print(f\"Loading tokenizer: {BASE_MODEL}\")\ntokenizer = AutoTokenizer.from_pretrained(BASE_MODEL, trust_remote_code=True)\ntokenizer.pad_token = tokenizer.eos_token\ntokenizer.padding_side = \"right\"\ntokenizer.model_max_length = MAX_SEQ_LEN # replaces max_seq_length in SFTConfig\nprint(\"Tokenizer loaded\")"
134
  },
135
  {
136
  "cell_type": "code",
137
- "execution_count": null,
138
- "id": "format-dataset",
139
- "metadata": {},
 
 
 
140
  "outputs": [],
141
- "source": [
142
- "SYSTEM_PROMPT = (\n",
143
- " \"You are a StockEx clearing house trading agent. \"\n",
144
- " \"Given a member's financial state and live market data, \"\n",
145
- " \"you output a single valid JSON trading decision that respects all capital and holdings constraints. \"\n",
146
- " \"Never output anything other than the JSON object.\"\n",
147
- ")\n",
148
- "\n",
149
- "\n",
150
- "def format_chat(example):\n",
151
- " \"\"\"Apply the model's chat template to produce a training string.\"\"\"\n",
152
- " messages = [\n",
153
- " {\"role\": \"system\", \"content\": SYSTEM_PROMPT},\n",
154
- " {\"role\": \"user\", \"content\": example[\"prompt\"]},\n",
155
- " {\"role\": \"assistant\", \"content\": example[\"completion\"]},\n",
156
- " ]\n",
157
- " text = tokenizer.apply_chat_template(\n",
158
- " messages,\n",
159
- " tokenize=False,\n",
160
- " add_generation_prompt=False,\n",
161
- " )\n",
162
- " return {\"text\": text}\n",
163
- "\n",
164
- "\n",
165
- "train_dataset = train_dataset.map(format_chat)\n",
166
- "val_dataset = val_dataset.map(format_chat)\n",
167
- "\n",
168
- "print(\"Sample formatted text:\")\n",
169
- "print(train_dataset[0][\"text\"][:600], \"...\")"
170
- ]
171
  },
172
  {
173
  "cell_type": "code",
174
- "execution_count": null,
175
- "id": "load-model",
176
- "metadata": {},
 
 
 
177
  "outputs": [],
178
- "source": [
179
- "# 4-bit quantization config\n",
180
- "bnb_config = BitsAndBytesConfig(\n",
181
- " load_in_4bit=True,\n",
182
- " bnb_4bit_quant_type=\"nf4\",\n",
183
- " bnb_4bit_compute_dtype=torch.bfloat16,\n",
184
- " bnb_4bit_use_double_quant=True,\n",
185
- ")\n",
186
- "\n",
187
- "print(f\"Loading model: {BASE_MODEL} (4-bit)\")\n",
188
- "model = AutoModelForCausalLM.from_pretrained(\n",
189
- " BASE_MODEL,\n",
190
- " quantization_config=bnb_config,\n",
191
- " device_map=\"auto\",\n",
192
- " trust_remote_code=True,\n",
193
- " torch_dtype=torch.bfloat16,\n",
194
- ")\n",
195
- "model.config.use_cache = False\n",
196
- "model.config.pretraining_tp = 1\n",
197
- "print(f\"Model loaded. Parameters: {model.num_parameters()/1e9:.2f}B\")"
198
- ]
199
  },
200
  {
201
  "cell_type": "markdown",
202
- "id": "lora-header",
203
- "metadata": {},
204
- "source": [
205
- "## 3. LoRA Configuration"
206
- ]
207
  },
208
  {
209
  "cell_type": "code",
210
- "execution_count": null,
211
- "id": "lora-config",
212
- "metadata": {},
 
 
 
213
  "outputs": [],
214
- "source": [
215
- "lora_config = LoraConfig(\n",
216
- " r=LORA_R,\n",
217
- " lora_alpha=LORA_ALPHA,\n",
218
- " target_modules=[\n",
219
- " \"q_proj\", \"k_proj\", \"v_proj\", \"o_proj\",\n",
220
- " \"gate_proj\", \"up_proj\", \"down_proj\",\n",
221
- " ],\n",
222
- " lora_dropout=LORA_DROPOUT,\n",
223
- " bias=\"none\",\n",
224
- " task_type=TaskType.CAUSAL_LM,\n",
225
- ")\n",
226
- "\n",
227
- "trainable = sum(p.numel() for p in model.parameters() if p.requires_grad)\n",
228
- "total = sum(p.numel() for p in model.parameters())\n",
229
- "print(f\"Trainable parameters: {trainable/1e6:.1f}M / {total/1e6:.0f}M ({100*trainable/total:.2f}%)\")"
230
- ]
231
  },
232
  {
233
  "cell_type": "markdown",
234
- "id": "training-header",
235
- "metadata": {},
236
- "source": [
237
- "## 4. Train"
238
- ]
239
  },
240
  {
241
  "cell_type": "code",
242
- "execution_count": null,
243
- "id": "train",
244
- "metadata": {},
 
245
  "outputs": [],
246
- "source": "from transformers import TrainerCallback\n\n\nclass CheckpointSyncCallback(TrainerCallback):\n \"\"\"After every checkpoint: copy to persistent storage and push adapter to HF Hub.\"\"\"\n\n def on_save(self, args, state, control, **kwargs):\n ckpt_dir = os.path.join(args.output_dir, f\"checkpoint-{state.global_step}\")\n if not os.path.isdir(ckpt_dir):\n return\n\n # 1. Sync to persistent storage (platform-specific)\n if ON_COLAB and DRIVE_CKPT_DIR:\n dest = os.path.join(DRIVE_CKPT_DIR, f\"checkpoint-{state.global_step}\")\n if not os.path.exists(dest):\n shutil.copytree(ckpt_dir, dest)\n print(f\"[Checkpoint] Saved to Drive: {dest}\")\n elif ON_KAGGLE:\n dest = os.path.join(KAGGLE_OUTPUT_CKPT_DIR, f\"checkpoint-{state.global_step}\")\n if not os.path.exists(dest):\n shutil.copytree(ckpt_dir, dest)\n print(f\"[Checkpoint] Saved to output: {dest}\")\n\n # 2. Push adapter to HF Hub (lightweight β€” only LoRA weights)\n try:\n kwargs[\"model\"].push_to_hub(\n OUTPUT_REPO,\n commit_message=f\"Checkpoint step {state.global_step} \"\n f\"(epoch {state.epoch:.2f})\",\n token=HF_TOKEN,\n )\n print(f\"[Checkpoint] Pushed adapter step {state.global_step} β†’ HF Hub\")\n except Exception as e:\n print(f\"[Checkpoint] HF Hub push failed (non-fatal): {e}\")\n\n\nsft_config = SFTConfig(\n output_dir=OUTPUT_DIR,\n num_train_epochs=NUM_EPOCHS,\n per_device_train_batch_size=BATCH_SIZE,\n per_device_eval_batch_size=BATCH_SIZE,\n gradient_accumulation_steps=GRAD_ACCUM,\n gradient_checkpointing=True,\n optim=\"paged_adamw_32bit\",\n learning_rate=LR,\n lr_scheduler_type=\"cosine\",\n warmup_ratio=0.05,\n fp16=not torch.cuda.is_bf16_supported(),\n bf16=torch.cuda.is_bf16_supported(),\n logging_steps=10,\n eval_strategy=\"steps\",\n eval_steps=SAVE_STEPS,\n save_strategy=\"steps\",\n save_steps=SAVE_STEPS,\n save_total_limit=3, # keep only 3 latest local checkpoints\n load_best_model_at_end=True,\n metric_for_best_model=\"eval_loss\",\n greater_is_better=False,\n report_to=\"none\",\n dataset_text_field=\"text\",\n packing=False,\n)\n\ntrainer = SFTTrainer(\n model=model,\n args=sft_config,\n train_dataset=train_dataset,\n eval_dataset=val_dataset,\n peft_config=lora_config,\n processing_class=tokenizer,\n callbacks=[CheckpointSyncCallback()],\n)\n\nif RESUME_FROM:\n print(f\"β–Ά Resuming from checkpoint: {RESUME_FROM}\")\nelse:\n print(\"β–Ά Starting training from scratch\")\n\ntrainer.train(resume_from_checkpoint=RESUME_FROM)\nprint(\"βœ“ Training complete.\")"
247
  },
248
  {
249
  "cell_type": "markdown",
250
- "id": "save-header",
251
- "metadata": {},
252
- "source": [
253
- "## 5. Save & Push to HuggingFace Hub\n",
254
- "\n",
255
- "Merges LoRA adapters into the base model weights and pushes the full model."
256
- ]
257
  },
258
  {
259
  "cell_type": "code",
260
- "execution_count": null,
261
- "id": "save-model",
262
- "metadata": {},
 
 
263
  "outputs": [],
264
- "source": [
265
- "from peft import PeftModel\n",
266
- "\n",
267
- "# Save best adapter checkpoint locally\n",
268
- "trainer.model.save_pretrained(OUTPUT_DIR)\n",
269
- "tokenizer.save_pretrained(OUTPUT_DIR)\n",
270
- "print(f\"Adapter saved to {OUTPUT_DIR}\")\n",
271
- "\n",
272
- "# Reload base model in fp16 for merging (can't merge with 4-bit)\n",
273
- "print(\"Reloading base model in fp16 for adapter merge...\")\n",
274
- "del model\n",
275
- "torch.cuda.empty_cache()\n",
276
- "\n",
277
- "base_model = AutoModelForCausalLM.from_pretrained(\n",
278
- " BASE_MODEL,\n",
279
- " torch_dtype=torch.float16,\n",
280
- " device_map=\"auto\",\n",
281
- " trust_remote_code=True,\n",
282
- ")\n",
283
- "merged_model = PeftModel.from_pretrained(base_model, OUTPUT_DIR)\n",
284
- "merged_model = merged_model.merge_and_unload()\n",
285
- "print(\"Adapters merged.\")"
286
- ]
287
  },
288
  {
289
  "cell_type": "code",
290
- "execution_count": null,
291
- "id": "push-hub",
292
- "metadata": {},
 
 
293
  "outputs": [],
294
- "source": "print(f\"Pushing merged model to: {OUTPUT_REPO}\")\nmerged_model.push_to_hub(\n OUTPUT_REPO,\n token=HF_TOKEN,\n commit_message=\"StockEx CH Trader: QLoRA fine-tuned Qwen2.5-32B-Instruct\",\n)\ntokenizer.push_to_hub(\n OUTPUT_REPO,\n token=HF_TOKEN,\n commit_message=\"Tokenizer for StockEx CH Trader (Qwen2.5-32B-Instruct base)\",\n)\nprint(f\"βœ“ Model pushed to https://huggingface.co/{OUTPUT_REPO}\")"
295
  },
296
  {
297
  "cell_type": "markdown",
298
- "id": "test-header",
299
- "metadata": {},
300
- "source": [
301
- "## 6. Inference Test\n",
302
- "\n",
303
- "Verify the model generates valid JSON trading decisions."
304
- ]
305
  },
306
  {
307
  "cell_type": "code",
308
- "execution_count": null,
309
- "id": "inference-test",
310
- "metadata": {},
 
 
311
  "outputs": [],
312
- "source": "import re\nfrom transformers import pipeline\n\npipe = pipeline(\n \"text-generation\",\n model=merged_model,\n tokenizer=tokenizer,\n device_map=\"auto\",\n)\n\ntest_cases = [\n {\n \"desc\": \"New member, no holdings, must trade\",\n \"capital\": 100_000.0,\n \"holdings\": [],\n \"obligation\": 10,\n },\n {\n \"desc\": \"Experienced member with holdings, low obligation\",\n \"capital\": 65_000.0,\n \"holdings\": [\n {\"symbol\": \"ALPHA\", \"quantity\": 300, \"avg_cost\": 5.60},\n {\"symbol\": \"QUEST\", \"quantity\": 150, \"avg_cost\": 13.20},\n ],\n \"obligation\": 2,\n },\n {\n \"desc\": \"Low capital, large holdings\",\n \"capital\": 8_000.0,\n \"holdings\": [\n {\"symbol\": \"PEIR\", \"quantity\": 500, \"avg_cost\": 8.30},\n {\"symbol\": \"NBG\", \"quantity\": 200, \"avg_cost\": 7.95},\n ],\n \"obligation\": 5,\n },\n]\n\ntest_bbos = {s[\"symbol\"]: gen_bbo(s[\"base\"]) for s in SECURITIES}\n\nprint(\"=\" * 70)\nfor tc in test_cases:\n print(f\"\\nSCENARIO: {tc['desc']}\")\n prompt = build_prompt(\"USR01\", tc[\"capital\"], tc[\"holdings\"], tc[\"obligation\"], test_bbos)\n messages = [\n {\"role\": \"system\", \"content\": SYSTEM_PROMPT},\n {\"role\": \"user\", \"content\": prompt},\n ]\n output = pipe(\n messages,\n max_new_tokens=60,\n temperature=0.3,\n do_sample=True,\n pad_token_id=tokenizer.eos_token_id,\n )\n response = output[0][\"generated_text\"][-1][\"content\"].strip()\n print(f\"RESPONSE: {response}\")\n try:\n m = re.search(r\"\\{[^}]+\\}\", response)\n if m:\n d = json.loads(m.group())\n assert d[\"side\"] in (\"BUY\", \"SELL\")\n assert d[\"symbol\"] in [s[\"symbol\"] for s in SECURITIES]\n assert d[\"quantity\"] > 0\n assert d[\"price\"] > 0\n print(f\"βœ“ Valid JSON: {d}\")\n else:\n print(\"βœ— No JSON found in response\")\n except Exception as e:\n print(f\"βœ— Invalid: {e}\")\n print(\"-\" * 70)"
313
  },
314
  {
315
  "cell_type": "markdown",
316
- "id": "usage-header",
317
- "metadata": {},
318
- "source": [
319
- "## 7. Activate in StockEx\n",
320
- "\n",
321
- "The clearing house already uses `RayMelius/stockex-ch-trader` as default.\n",
322
- "\n",
323
- "To switch to this model in a running StockEx instance:\n",
324
- "\n",
325
- "**HuggingFace Spaces** β€” add to secrets:\n",
326
- "```\n",
327
- "HF_MODEL = RayMelius/stockex-ch-trader\n",
328
- "HF_TOKEN = <your token>\n",
329
- "```\n",
330
- "\n",
331
- "**Docker Compose** β€” already set in `docker-compose.yml`:\n",
332
- "```yaml\n",
333
- "environment:\n",
334
- " - HF_MODEL=RayMelius/stockex-ch-trader\n",
335
- " - HF_TOKEN=<your token>\n",
336
- "```\n",
337
- "\n",
338
- "To use a future CH-specific model later:\n",
339
- "```\n",
340
- "HF_MODEL = RayMelius/<new-ch-model>\n",
341
- "```"
342
- ]
343
  }
344
  ]
345
  }
 
1
  {
 
 
2
  "metadata": {
3
  "kernelspec": {
4
  "display_name": "Python 3",
 
7
  },
8
  "language_info": {
9
  "name": "python",
10
+ "version": "3.12.12",
11
+ "mimetype": "text/x-python",
12
+ "codemirror_mode": {
13
+ "name": "ipython",
14
+ "version": 3
15
+ },
16
+ "pygments_lexer": "ipython3",
17
+ "nbconvert_exporter": "python",
18
+ "file_extension": ".py"
19
  },
20
  "accelerator": "GPU",
21
  "colab": {
22
+ "gpuType": "T4",
23
  "provenance": []
24
+ },
25
+ "widgets": {
26
+ "application/vnd.jupyter.widget-state+json": {
27
+ "7d1727a12c9445f88824a433ec067d05": {
28
+ "model_module": "@jupyter-widgets/controls",
29
+ "model_name": "HBoxModel",
30
+ "model_module_version": "1.5.0",
31
+ "state": {
32
+ "_dom_classes": [],
33
+ "_model_module": "@jupyter-widgets/controls",
34
+ "_model_module_version": "1.5.0",
35
+ "_model_name": "HBoxModel",
36
+ "_view_count": null,
37
+ "_view_module": "@jupyter-widgets/controls",
38
+ "_view_module_version": "1.5.0",
39
+ "_view_name": "HBoxView",
40
+ "box_style": "",
41
+ "children": [
42
+ "IPY_MODEL_ddf4a278a228476aadf8e154bf92c19b",
43
+ "IPY_MODEL_607775cdd08f4dd4a3a0d50a788dc631",
44
+ "IPY_MODEL_f6a6a369e6004316827310f3ff71c597"
45
+ ],
46
+ "layout": "IPY_MODEL_030fdf253ff141c09913c8e99d81637f"
47
+ }
48
+ },
49
+ "ddf4a278a228476aadf8e154bf92c19b": {
50
+ "model_module": "@jupyter-widgets/controls",
51
+ "model_name": "HTMLModel",
52
+ "model_module_version": "1.5.0",
53
+ "state": {
54
+ "_dom_classes": [],
55
+ "_model_module": "@jupyter-widgets/controls",
56
+ "_model_module_version": "1.5.0",
57
+ "_model_name": "HTMLModel",
58
+ "_view_count": null,
59
+ "_view_module": "@jupyter-widgets/controls",
60
+ "_view_module_version": "1.5.0",
61
+ "_view_name": "HTMLView",
62
+ "description": "",
63
+ "description_tooltip": null,
64
+ "layout": "IPY_MODEL_41d79e9429514c37b51282c281978ccb",
65
+ "placeholder": "​",
66
+ "style": "IPY_MODEL_474c0b3c456b462c9d98a635c09daff1",
67
+ "value": "Map: 100%"
68
+ }
69
+ },
70
+ "607775cdd08f4dd4a3a0d50a788dc631": {
71
+ "model_module": "@jupyter-widgets/controls",
72
+ "model_name": "FloatProgressModel",
73
+ "model_module_version": "1.5.0",
74
+ "state": {
75
+ "_dom_classes": [],
76
+ "_model_module": "@jupyter-widgets/controls",
77
+ "_model_module_version": "1.5.0",
78
+ "_model_name": "FloatProgressModel",
79
+ "_view_count": null,
80
+ "_view_module": "@jupyter-widgets/controls",
81
+ "_view_module_version": "1.5.0",
82
+ "_view_name": "ProgressView",
83
+ "bar_style": "success",
84
+ "description": "",
85
+ "description_tooltip": null,
86
+ "layout": "IPY_MODEL_897d25d509bf4d46bdf1e9d6d6ae6caa",
87
+ "max": 2250,
88
+ "min": 0,
89
+ "orientation": "horizontal",
90
+ "style": "IPY_MODEL_292804d6b77f4de39c24b768a205ca80",
91
+ "value": 2250
92
+ }
93
+ },
94
+ "f6a6a369e6004316827310f3ff71c597": {
95
+ "model_module": "@jupyter-widgets/controls",
96
+ "model_name": "HTMLModel",
97
+ "model_module_version": "1.5.0",
98
+ "state": {
99
+ "_dom_classes": [],
100
+ "_model_module": "@jupyter-widgets/controls",
101
+ "_model_module_version": "1.5.0",
102
+ "_model_name": "HTMLModel",
103
+ "_view_count": null,
104
+ "_view_module": "@jupyter-widgets/controls",
105
+ "_view_module_version": "1.5.0",
106
+ "_view_name": "HTMLView",
107
+ "description": "",
108
+ "description_tooltip": null,
109
+ "layout": "IPY_MODEL_00baa69256d04592bd69f4c41e7fb572",
110
+ "placeholder": "​",
111
+ "style": "IPY_MODEL_beaa41bfdbac47b1a8ffddf18d49f74d",
112
+ "value": " 2250/2250 [00:00&lt;00:00, 2300.58 examples/s]"
113
+ }
114
+ },
115
+ "030fdf253ff141c09913c8e99d81637f": {
116
+ "model_module": "@jupyter-widgets/base",
117
+ "model_name": "LayoutModel",
118
+ "model_module_version": "1.2.0",
119
+ "state": {
120
+ "_model_module": "@jupyter-widgets/base",
121
+ "_model_module_version": "1.2.0",
122
+ "_model_name": "LayoutModel",
123
+ "_view_count": null,
124
+ "_view_module": "@jupyter-widgets/base",
125
+ "_view_module_version": "1.2.0",
126
+ "_view_name": "LayoutView",
127
+ "align_content": null,
128
+ "align_items": null,
129
+ "align_self": null,
130
+ "border": null,
131
+ "bottom": null,
132
+ "display": null,
133
+ "flex": null,
134
+ "flex_flow": null,
135
+ "grid_area": null,
136
+ "grid_auto_columns": null,
137
+ "grid_auto_flow": null,
138
+ "grid_auto_rows": null,
139
+ "grid_column": null,
140
+ "grid_gap": null,
141
+ "grid_row": null,
142
+ "grid_template_areas": null,
143
+ "grid_template_columns": null,
144
+ "grid_template_rows": null,
145
+ "height": null,
146
+ "justify_content": null,
147
+ "justify_items": null,
148
+ "left": null,
149
+ "margin": null,
150
+ "max_height": null,
151
+ "max_width": null,
152
+ "min_height": null,
153
+ "min_width": null,
154
+ "object_fit": null,
155
+ "object_position": null,
156
+ "order": null,
157
+ "overflow": null,
158
+ "overflow_x": null,
159
+ "overflow_y": null,
160
+ "padding": null,
161
+ "right": null,
162
+ "top": null,
163
+ "visibility": null,
164
+ "width": null
165
+ }
166
+ },
167
+ "41d79e9429514c37b51282c281978ccb": {
168
+ "model_module": "@jupyter-widgets/base",
169
+ "model_name": "LayoutModel",
170
+ "model_module_version": "1.2.0",
171
+ "state": {
172
+ "_model_module": "@jupyter-widgets/base",
173
+ "_model_module_version": "1.2.0",
174
+ "_model_name": "LayoutModel",
175
+ "_view_count": null,
176
+ "_view_module": "@jupyter-widgets/base",
177
+ "_view_module_version": "1.2.0",
178
+ "_view_name": "LayoutView",
179
+ "align_content": null,
180
+ "align_items": null,
181
+ "align_self": null,
182
+ "border": null,
183
+ "bottom": null,
184
+ "display": null,
185
+ "flex": null,
186
+ "flex_flow": null,
187
+ "grid_area": null,
188
+ "grid_auto_columns": null,
189
+ "grid_auto_flow": null,
190
+ "grid_auto_rows": null,
191
+ "grid_column": null,
192
+ "grid_gap": null,
193
+ "grid_row": null,
194
+ "grid_template_areas": null,
195
+ "grid_template_columns": null,
196
+ "grid_template_rows": null,
197
+ "height": null,
198
+ "justify_content": null,
199
+ "justify_items": null,
200
+ "left": null,
201
+ "margin": null,
202
+ "max_height": null,
203
+ "max_width": null,
204
+ "min_height": null,
205
+ "min_width": null,
206
+ "object_fit": null,
207
+ "object_position": null,
208
+ "order": null,
209
+ "overflow": null,
210
+ "overflow_x": null,
211
+ "overflow_y": null,
212
+ "padding": null,
213
+ "right": null,
214
+ "top": null,
215
+ "visibility": null,
216
+ "width": null
217
+ }
218
+ },
219
+ "474c0b3c456b462c9d98a635c09daff1": {
220
+ "model_module": "@jupyter-widgets/controls",
221
+ "model_name": "DescriptionStyleModel",
222
+ "model_module_version": "1.5.0",
223
+ "state": {
224
+ "_model_module": "@jupyter-widgets/controls",
225
+ "_model_module_version": "1.5.0",
226
+ "_model_name": "DescriptionStyleModel",
227
+ "_view_count": null,
228
+ "_view_module": "@jupyter-widgets/base",
229
+ "_view_module_version": "1.2.0",
230
+ "_view_name": "StyleView",
231
+ "description_width": ""
232
+ }
233
+ },
234
+ "897d25d509bf4d46bdf1e9d6d6ae6caa": {
235
+ "model_module": "@jupyter-widgets/base",
236
+ "model_name": "LayoutModel",
237
+ "model_module_version": "1.2.0",
238
+ "state": {
239
+ "_model_module": "@jupyter-widgets/base",
240
+ "_model_module_version": "1.2.0",
241
+ "_model_name": "LayoutModel",
242
+ "_view_count": null,
243
+ "_view_module": "@jupyter-widgets/base",
244
+ "_view_module_version": "1.2.0",
245
+ "_view_name": "LayoutView",
246
+ "align_content": null,
247
+ "align_items": null,
248
+ "align_self": null,
249
+ "border": null,
250
+ "bottom": null,
251
+ "display": null,
252
+ "flex": null,
253
+ "flex_flow": null,
254
+ "grid_area": null,
255
+ "grid_auto_columns": null,
256
+ "grid_auto_flow": null,
257
+ "grid_auto_rows": null,
258
+ "grid_column": null,
259
+ "grid_gap": null,
260
+ "grid_row": null,
261
+ "grid_template_areas": null,
262
+ "grid_template_columns": null,
263
+ "grid_template_rows": null,
264
+ "height": null,
265
+ "justify_content": null,
266
+ "justify_items": null,
267
+ "left": null,
268
+ "margin": null,
269
+ "max_height": null,
270
+ "max_width": null,
271
+ "min_height": null,
272
+ "min_width": null,
273
+ "object_fit": null,
274
+ "object_position": null,
275
+ "order": null,
276
+ "overflow": null,
277
+ "overflow_x": null,
278
+ "overflow_y": null,
279
+ "padding": null,
280
+ "right": null,
281
+ "top": null,
282
+ "visibility": null,
283
+ "width": null
284
+ }
285
+ },
286
+ "292804d6b77f4de39c24b768a205ca80": {
287
+ "model_module": "@jupyter-widgets/controls",
288
+ "model_name": "ProgressStyleModel",
289
+ "model_module_version": "1.5.0",
290
+ "state": {
291
+ "_model_module": "@jupyter-widgets/controls",
292
+ "_model_module_version": "1.5.0",
293
+ "_model_name": "ProgressStyleModel",
294
+ "_view_count": null,
295
+ "_view_module": "@jupyter-widgets/base",
296
+ "_view_module_version": "1.2.0",
297
+ "_view_name": "StyleView",
298
+ "bar_color": null,
299
+ "description_width": ""
300
+ }
301
+ },
302
+ "00baa69256d04592bd69f4c41e7fb572": {
303
+ "model_module": "@jupyter-widgets/base",
304
+ "model_name": "LayoutModel",
305
+ "model_module_version": "1.2.0",
306
+ "state": {
307
+ "_model_module": "@jupyter-widgets/base",
308
+ "_model_module_version": "1.2.0",
309
+ "_model_name": "LayoutModel",
310
+ "_view_count": null,
311
+ "_view_module": "@jupyter-widgets/base",
312
+ "_view_module_version": "1.2.0",
313
+ "_view_name": "LayoutView",
314
+ "align_content": null,
315
+ "align_items": null,
316
+ "align_self": null,
317
+ "border": null,
318
+ "bottom": null,
319
+ "display": null,
320
+ "flex": null,
321
+ "flex_flow": null,
322
+ "grid_area": null,
323
+ "grid_auto_columns": null,
324
+ "grid_auto_flow": null,
325
+ "grid_auto_rows": null,
326
+ "grid_column": null,
327
+ "grid_gap": null,
328
+ "grid_row": null,
329
+ "grid_template_areas": null,
330
+ "grid_template_columns": null,
331
+ "grid_template_rows": null,
332
+ "height": null,
333
+ "justify_content": null,
334
+ "justify_items": null,
335
+ "left": null,
336
+ "margin": null,
337
+ "max_height": null,
338
+ "max_width": null,
339
+ "min_height": null,
340
+ "min_width": null,
341
+ "object_fit": null,
342
+ "object_position": null,
343
+ "order": null,
344
+ "overflow": null,
345
+ "overflow_x": null,
346
+ "overflow_y": null,
347
+ "padding": null,
348
+ "right": null,
349
+ "top": null,
350
+ "visibility": null,
351
+ "width": null
352
+ }
353
+ },
354
+ "beaa41bfdbac47b1a8ffddf18d49f74d": {
355
+ "model_module": "@jupyter-widgets/controls",
356
+ "model_name": "DescriptionStyleModel",
357
+ "model_module_version": "1.5.0",
358
+ "state": {
359
+ "_model_module": "@jupyter-widgets/controls",
360
+ "_model_module_version": "1.5.0",
361
+ "_model_name": "DescriptionStyleModel",
362
+ "_view_count": null,
363
+ "_view_module": "@jupyter-widgets/base",
364
+ "_view_module_version": "1.2.0",
365
+ "_view_name": "StyleView",
366
+ "description_width": ""
367
+ }
368
+ },
369
+ "16be669fa1b34fc5929de11272273bbc": {
370
+ "model_module": "@jupyter-widgets/controls",
371
+ "model_name": "HBoxModel",
372
+ "model_module_version": "1.5.0",
373
+ "state": {
374
+ "_dom_classes": [],
375
+ "_model_module": "@jupyter-widgets/controls",
376
+ "_model_module_version": "1.5.0",
377
+ "_model_name": "HBoxModel",
378
+ "_view_count": null,
379
+ "_view_module": "@jupyter-widgets/controls",
380
+ "_view_module_version": "1.5.0",
381
+ "_view_name": "HBoxView",
382
+ "box_style": "",
383
+ "children": [
384
+ "IPY_MODEL_8e40f72fced7455496a0ac8adfcedc1e",
385
+ "IPY_MODEL_64442da9157d406da770b7752face424",
386
+ "IPY_MODEL_9522c3c9c20f4bddbeb9106e1eff2b04"
387
+ ],
388
+ "layout": "IPY_MODEL_ea1c64cc55ff4948b08c167aad9c02bf"
389
+ }
390
+ },
391
+ "8e40f72fced7455496a0ac8adfcedc1e": {
392
+ "model_module": "@jupyter-widgets/controls",
393
+ "model_name": "HTMLModel",
394
+ "model_module_version": "1.5.0",
395
+ "state": {
396
+ "_dom_classes": [],
397
+ "_model_module": "@jupyter-widgets/controls",
398
+ "_model_module_version": "1.5.0",
399
+ "_model_name": "HTMLModel",
400
+ "_view_count": null,
401
+ "_view_module": "@jupyter-widgets/controls",
402
+ "_view_module_version": "1.5.0",
403
+ "_view_name": "HTMLView",
404
+ "description": "",
405
+ "description_tooltip": null,
406
+ "layout": "IPY_MODEL_5cf321f500ca4848bdb76fe8a21fa12f",
407
+ "placeholder": "​",
408
+ "style": "IPY_MODEL_4ff9d8e7dc5c488189872b74d643d430",
409
+ "value": "Map: 100%"
410
+ }
411
+ },
412
+ "64442da9157d406da770b7752face424": {
413
+ "model_module": "@jupyter-widgets/controls",
414
+ "model_name": "FloatProgressModel",
415
+ "model_module_version": "1.5.0",
416
+ "state": {
417
+ "_dom_classes": [],
418
+ "_model_module": "@jupyter-widgets/controls",
419
+ "_model_module_version": "1.5.0",
420
+ "_model_name": "FloatProgressModel",
421
+ "_view_count": null,
422
+ "_view_module": "@jupyter-widgets/controls",
423
+ "_view_module_version": "1.5.0",
424
+ "_view_name": "ProgressView",
425
+ "bar_style": "success",
426
+ "description": "",
427
+ "description_tooltip": null,
428
+ "layout": "IPY_MODEL_9d97e9d42df4490cb49c423e479a105a",
429
+ "max": 250,
430
+ "min": 0,
431
+ "orientation": "horizontal",
432
+ "style": "IPY_MODEL_36c56cb7232849819c6d032c395acbe4",
433
+ "value": 250
434
+ }
435
+ },
436
+ "9522c3c9c20f4bddbeb9106e1eff2b04": {
437
+ "model_module": "@jupyter-widgets/controls",
438
+ "model_name": "HTMLModel",
439
+ "model_module_version": "1.5.0",
440
+ "state": {
441
+ "_dom_classes": [],
442
+ "_model_module": "@jupyter-widgets/controls",
443
+ "_model_module_version": "1.5.0",
444
+ "_model_name": "HTMLModel",
445
+ "_view_count": null,
446
+ "_view_module": "@jupyter-widgets/controls",
447
+ "_view_module_version": "1.5.0",
448
+ "_view_name": "HTMLView",
449
+ "description": "",
450
+ "description_tooltip": null,
451
+ "layout": "IPY_MODEL_19740a84d8e74481b4c819cb33abd260",
452
+ "placeholder": "​",
453
+ "style": "IPY_MODEL_77a8e0233da34523b09e7b5f5f0efc68",
454
+ "value": " 250/250 [00:00&lt;00:00, 3130.33 examples/s]"
455
+ }
456
+ },
457
+ "ea1c64cc55ff4948b08c167aad9c02bf": {
458
+ "model_module": "@jupyter-widgets/base",
459
+ "model_name": "LayoutModel",
460
+ "model_module_version": "1.2.0",
461
+ "state": {
462
+ "_model_module": "@jupyter-widgets/base",
463
+ "_model_module_version": "1.2.0",
464
+ "_model_name": "LayoutModel",
465
+ "_view_count": null,
466
+ "_view_module": "@jupyter-widgets/base",
467
+ "_view_module_version": "1.2.0",
468
+ "_view_name": "LayoutView",
469
+ "align_content": null,
470
+ "align_items": null,
471
+ "align_self": null,
472
+ "border": null,
473
+ "bottom": null,
474
+ "display": null,
475
+ "flex": null,
476
+ "flex_flow": null,
477
+ "grid_area": null,
478
+ "grid_auto_columns": null,
479
+ "grid_auto_flow": null,
480
+ "grid_auto_rows": null,
481
+ "grid_column": null,
482
+ "grid_gap": null,
483
+ "grid_row": null,
484
+ "grid_template_areas": null,
485
+ "grid_template_columns": null,
486
+ "grid_template_rows": null,
487
+ "height": null,
488
+ "justify_content": null,
489
+ "justify_items": null,
490
+ "left": null,
491
+ "margin": null,
492
+ "max_height": null,
493
+ "max_width": null,
494
+ "min_height": null,
495
+ "min_width": null,
496
+ "object_fit": null,
497
+ "object_position": null,
498
+ "order": null,
499
+ "overflow": null,
500
+ "overflow_x": null,
501
+ "overflow_y": null,
502
+ "padding": null,
503
+ "right": null,
504
+ "top": null,
505
+ "visibility": null,
506
+ "width": null
507
+ }
508
+ },
509
+ "5cf321f500ca4848bdb76fe8a21fa12f": {
510
+ "model_module": "@jupyter-widgets/base",
511
+ "model_name": "LayoutModel",
512
+ "model_module_version": "1.2.0",
513
+ "state": {
514
+ "_model_module": "@jupyter-widgets/base",
515
+ "_model_module_version": "1.2.0",
516
+ "_model_name": "LayoutModel",
517
+ "_view_count": null,
518
+ "_view_module": "@jupyter-widgets/base",
519
+ "_view_module_version": "1.2.0",
520
+ "_view_name": "LayoutView",
521
+ "align_content": null,
522
+ "align_items": null,
523
+ "align_self": null,
524
+ "border": null,
525
+ "bottom": null,
526
+ "display": null,
527
+ "flex": null,
528
+ "flex_flow": null,
529
+ "grid_area": null,
530
+ "grid_auto_columns": null,
531
+ "grid_auto_flow": null,
532
+ "grid_auto_rows": null,
533
+ "grid_column": null,
534
+ "grid_gap": null,
535
+ "grid_row": null,
536
+ "grid_template_areas": null,
537
+ "grid_template_columns": null,
538
+ "grid_template_rows": null,
539
+ "height": null,
540
+ "justify_content": null,
541
+ "justify_items": null,
542
+ "left": null,
543
+ "margin": null,
544
+ "max_height": null,
545
+ "max_width": null,
546
+ "min_height": null,
547
+ "min_width": null,
548
+ "object_fit": null,
549
+ "object_position": null,
550
+ "order": null,
551
+ "overflow": null,
552
+ "overflow_x": null,
553
+ "overflow_y": null,
554
+ "padding": null,
555
+ "right": null,
556
+ "top": null,
557
+ "visibility": null,
558
+ "width": null
559
+ }
560
+ },
561
+ "4ff9d8e7dc5c488189872b74d643d430": {
562
+ "model_module": "@jupyter-widgets/controls",
563
+ "model_name": "DescriptionStyleModel",
564
+ "model_module_version": "1.5.0",
565
+ "state": {
566
+ "_model_module": "@jupyter-widgets/controls",
567
+ "_model_module_version": "1.5.0",
568
+ "_model_name": "DescriptionStyleModel",
569
+ "_view_count": null,
570
+ "_view_module": "@jupyter-widgets/base",
571
+ "_view_module_version": "1.2.0",
572
+ "_view_name": "StyleView",
573
+ "description_width": ""
574
+ }
575
+ },
576
+ "9d97e9d42df4490cb49c423e479a105a": {
577
+ "model_module": "@jupyter-widgets/base",
578
+ "model_name": "LayoutModel",
579
+ "model_module_version": "1.2.0",
580
+ "state": {
581
+ "_model_module": "@jupyter-widgets/base",
582
+ "_model_module_version": "1.2.0",
583
+ "_model_name": "LayoutModel",
584
+ "_view_count": null,
585
+ "_view_module": "@jupyter-widgets/base",
586
+ "_view_module_version": "1.2.0",
587
+ "_view_name": "LayoutView",
588
+ "align_content": null,
589
+ "align_items": null,
590
+ "align_self": null,
591
+ "border": null,
592
+ "bottom": null,
593
+ "display": null,
594
+ "flex": null,
595
+ "flex_flow": null,
596
+ "grid_area": null,
597
+ "grid_auto_columns": null,
598
+ "grid_auto_flow": null,
599
+ "grid_auto_rows": null,
600
+ "grid_column": null,
601
+ "grid_gap": null,
602
+ "grid_row": null,
603
+ "grid_template_areas": null,
604
+ "grid_template_columns": null,
605
+ "grid_template_rows": null,
606
+ "height": null,
607
+ "justify_content": null,
608
+ "justify_items": null,
609
+ "left": null,
610
+ "margin": null,
611
+ "max_height": null,
612
+ "max_width": null,
613
+ "min_height": null,
614
+ "min_width": null,
615
+ "object_fit": null,
616
+ "object_position": null,
617
+ "order": null,
618
+ "overflow": null,
619
+ "overflow_x": null,
620
+ "overflow_y": null,
621
+ "padding": null,
622
+ "right": null,
623
+ "top": null,
624
+ "visibility": null,
625
+ "width": null
626
+ }
627
+ },
628
+ "36c56cb7232849819c6d032c395acbe4": {
629
+ "model_module": "@jupyter-widgets/controls",
630
+ "model_name": "ProgressStyleModel",
631
+ "model_module_version": "1.5.0",
632
+ "state": {
633
+ "_model_module": "@jupyter-widgets/controls",
634
+ "_model_module_version": "1.5.0",
635
+ "_model_name": "ProgressStyleModel",
636
+ "_view_count": null,
637
+ "_view_module": "@jupyter-widgets/base",
638
+ "_view_module_version": "1.2.0",
639
+ "_view_name": "StyleView",
640
+ "bar_color": null,
641
+ "description_width": ""
642
+ }
643
+ },
644
+ "19740a84d8e74481b4c819cb33abd260": {
645
+ "model_module": "@jupyter-widgets/base",
646
+ "model_name": "LayoutModel",
647
+ "model_module_version": "1.2.0",
648
+ "state": {
649
+ "_model_module": "@jupyter-widgets/base",
650
+ "_model_module_version": "1.2.0",
651
+ "_model_name": "LayoutModel",
652
+ "_view_count": null,
653
+ "_view_module": "@jupyter-widgets/base",
654
+ "_view_module_version": "1.2.0",
655
+ "_view_name": "LayoutView",
656
+ "align_content": null,
657
+ "align_items": null,
658
+ "align_self": null,
659
+ "border": null,
660
+ "bottom": null,
661
+ "display": null,
662
+ "flex": null,
663
+ "flex_flow": null,
664
+ "grid_area": null,
665
+ "grid_auto_columns": null,
666
+ "grid_auto_flow": null,
667
+ "grid_auto_rows": null,
668
+ "grid_column": null,
669
+ "grid_gap": null,
670
+ "grid_row": null,
671
+ "grid_template_areas": null,
672
+ "grid_template_columns": null,
673
+ "grid_template_rows": null,
674
+ "height": null,
675
+ "justify_content": null,
676
+ "justify_items": null,
677
+ "left": null,
678
+ "margin": null,
679
+ "max_height": null,
680
+ "max_width": null,
681
+ "min_height": null,
682
+ "min_width": null,
683
+ "object_fit": null,
684
+ "object_position": null,
685
+ "order": null,
686
+ "overflow": null,
687
+ "overflow_x": null,
688
+ "overflow_y": null,
689
+ "padding": null,
690
+ "right": null,
691
+ "top": null,
692
+ "visibility": null,
693
+ "width": null
694
+ }
695
+ },
696
+ "77a8e0233da34523b09e7b5f5f0efc68": {
697
+ "model_module": "@jupyter-widgets/controls",
698
+ "model_name": "DescriptionStyleModel",
699
+ "model_module_version": "1.5.0",
700
+ "state": {
701
+ "_model_module": "@jupyter-widgets/controls",
702
+ "_model_module_version": "1.5.0",
703
+ "_model_name": "DescriptionStyleModel",
704
+ "_view_count": null,
705
+ "_view_module": "@jupyter-widgets/base",
706
+ "_view_module_version": "1.2.0",
707
+ "_view_name": "StyleView",
708
+ "description_width": ""
709
+ }
710
+ },
711
+ "b2f6a30c7e70419c811e0da79bed8224": {
712
+ "model_module": "@jupyter-widgets/controls",
713
+ "model_name": "HBoxModel",
714
+ "model_module_version": "1.5.0",
715
+ "state": {
716
+ "_dom_classes": [],
717
+ "_model_module": "@jupyter-widgets/controls",
718
+ "_model_module_version": "1.5.0",
719
+ "_model_name": "HBoxModel",
720
+ "_view_count": null,
721
+ "_view_module": "@jupyter-widgets/controls",
722
+ "_view_module_version": "1.5.0",
723
+ "_view_name": "HBoxView",
724
+ "box_style": "",
725
+ "children": [
726
+ "IPY_MODEL_aea2b8a51c194dfab14d0870873652b7",
727
+ "IPY_MODEL_a1efc3457ce34a799f9a59c3cb58d55c",
728
+ "IPY_MODEL_8b4f2a3e789e42d2ac402f21c1fc15f5"
729
+ ],
730
+ "layout": "IPY_MODEL_53d3826130da44b3aba936cad515d67b"
731
+ }
732
+ },
733
+ "aea2b8a51c194dfab14d0870873652b7": {
734
+ "model_module": "@jupyter-widgets/controls",
735
+ "model_name": "HTMLModel",
736
+ "model_module_version": "1.5.0",
737
+ "state": {
738
+ "_dom_classes": [],
739
+ "_model_module": "@jupyter-widgets/controls",
740
+ "_model_module_version": "1.5.0",
741
+ "_model_name": "HTMLModel",
742
+ "_view_count": null,
743
+ "_view_module": "@jupyter-widgets/controls",
744
+ "_view_module_version": "1.5.0",
745
+ "_view_name": "HTMLView",
746
+ "description": "",
747
+ "description_tooltip": null,
748
+ "layout": "IPY_MODEL_ac3bf1ae42804678be05203c7d95ddd9",
749
+ "placeholder": "​",
750
+ "style": "IPY_MODEL_73e084e0709a446cbde83880da8fae2e",
751
+ "value": "Loading weights: 100%"
752
+ }
753
+ },
754
+ "a1efc3457ce34a799f9a59c3cb58d55c": {
755
+ "model_module": "@jupyter-widgets/controls",
756
+ "model_name": "FloatProgressModel",
757
+ "model_module_version": "1.5.0",
758
+ "state": {
759
+ "_dom_classes": [],
760
+ "_model_module": "@jupyter-widgets/controls",
761
+ "_model_module_version": "1.5.0",
762
+ "_model_name": "FloatProgressModel",
763
+ "_view_count": null,
764
+ "_view_module": "@jupyter-widgets/controls",
765
+ "_view_module_version": "1.5.0",
766
+ "_view_name": "ProgressView",
767
+ "bar_style": "success",
768
+ "description": "",
769
+ "description_tooltip": null,
770
+ "layout": "IPY_MODEL_2d6124a44ed048198d897c074561cd96",
771
+ "max": 339,
772
+ "min": 0,
773
+ "orientation": "horizontal",
774
+ "style": "IPY_MODEL_b30c7621e14748f7a7e1f6683fac0674",
775
+ "value": 339
776
+ }
777
+ },
778
+ "8b4f2a3e789e42d2ac402f21c1fc15f5": {
779
+ "model_module": "@jupyter-widgets/controls",
780
+ "model_name": "HTMLModel",
781
+ "model_module_version": "1.5.0",
782
+ "state": {
783
+ "_dom_classes": [],
784
+ "_model_module": "@jupyter-widgets/controls",
785
+ "_model_module_version": "1.5.0",
786
+ "_model_name": "HTMLModel",
787
+ "_view_count": null,
788
+ "_view_module": "@jupyter-widgets/controls",
789
+ "_view_module_version": "1.5.0",
790
+ "_view_name": "HTMLView",
791
+ "description": "",
792
+ "description_tooltip": null,
793
+ "layout": "IPY_MODEL_5969532875ce41b6820a5c57b53908d9",
794
+ "placeholder": "​",
795
+ "style": "IPY_MODEL_af8771c98d3a4470a6781f1035e0856e",
796
+ "value": " 339/339 [01:07&lt;00:00, 178.83it/s, Materializing param=model.norm.weight]"
797
+ }
798
+ },
799
+ "53d3826130da44b3aba936cad515d67b": {
800
+ "model_module": "@jupyter-widgets/base",
801
+ "model_name": "LayoutModel",
802
+ "model_module_version": "1.2.0",
803
+ "state": {
804
+ "_model_module": "@jupyter-widgets/base",
805
+ "_model_module_version": "1.2.0",
806
+ "_model_name": "LayoutModel",
807
+ "_view_count": null,
808
+ "_view_module": "@jupyter-widgets/base",
809
+ "_view_module_version": "1.2.0",
810
+ "_view_name": "LayoutView",
811
+ "align_content": null,
812
+ "align_items": null,
813
+ "align_self": null,
814
+ "border": null,
815
+ "bottom": null,
816
+ "display": null,
817
+ "flex": null,
818
+ "flex_flow": null,
819
+ "grid_area": null,
820
+ "grid_auto_columns": null,
821
+ "grid_auto_flow": null,
822
+ "grid_auto_rows": null,
823
+ "grid_column": null,
824
+ "grid_gap": null,
825
+ "grid_row": null,
826
+ "grid_template_areas": null,
827
+ "grid_template_columns": null,
828
+ "grid_template_rows": null,
829
+ "height": null,
830
+ "justify_content": null,
831
+ "justify_items": null,
832
+ "left": null,
833
+ "margin": null,
834
+ "max_height": null,
835
+ "max_width": null,
836
+ "min_height": null,
837
+ "min_width": null,
838
+ "object_fit": null,
839
+ "object_position": null,
840
+ "order": null,
841
+ "overflow": null,
842
+ "overflow_x": null,
843
+ "overflow_y": null,
844
+ "padding": null,
845
+ "right": null,
846
+ "top": null,
847
+ "visibility": null,
848
+ "width": null
849
+ }
850
+ },
851
+ "ac3bf1ae42804678be05203c7d95ddd9": {
852
+ "model_module": "@jupyter-widgets/base",
853
+ "model_name": "LayoutModel",
854
+ "model_module_version": "1.2.0",
855
+ "state": {
856
+ "_model_module": "@jupyter-widgets/base",
857
+ "_model_module_version": "1.2.0",
858
+ "_model_name": "LayoutModel",
859
+ "_view_count": null,
860
+ "_view_module": "@jupyter-widgets/base",
861
+ "_view_module_version": "1.2.0",
862
+ "_view_name": "LayoutView",
863
+ "align_content": null,
864
+ "align_items": null,
865
+ "align_self": null,
866
+ "border": null,
867
+ "bottom": null,
868
+ "display": null,
869
+ "flex": null,
870
+ "flex_flow": null,
871
+ "grid_area": null,
872
+ "grid_auto_columns": null,
873
+ "grid_auto_flow": null,
874
+ "grid_auto_rows": null,
875
+ "grid_column": null,
876
+ "grid_gap": null,
877
+ "grid_row": null,
878
+ "grid_template_areas": null,
879
+ "grid_template_columns": null,
880
+ "grid_template_rows": null,
881
+ "height": null,
882
+ "justify_content": null,
883
+ "justify_items": null,
884
+ "left": null,
885
+ "margin": null,
886
+ "max_height": null,
887
+ "max_width": null,
888
+ "min_height": null,
889
+ "min_width": null,
890
+ "object_fit": null,
891
+ "object_position": null,
892
+ "order": null,
893
+ "overflow": null,
894
+ "overflow_x": null,
895
+ "overflow_y": null,
896
+ "padding": null,
897
+ "right": null,
898
+ "top": null,
899
+ "visibility": null,
900
+ "width": null
901
+ }
902
+ },
903
+ "73e084e0709a446cbde83880da8fae2e": {
904
+ "model_module": "@jupyter-widgets/controls",
905
+ "model_name": "DescriptionStyleModel",
906
+ "model_module_version": "1.5.0",
907
+ "state": {
908
+ "_model_module": "@jupyter-widgets/controls",
909
+ "_model_module_version": "1.5.0",
910
+ "_model_name": "DescriptionStyleModel",
911
+ "_view_count": null,
912
+ "_view_module": "@jupyter-widgets/base",
913
+ "_view_module_version": "1.2.0",
914
+ "_view_name": "StyleView",
915
+ "description_width": ""
916
+ }
917
+ },
918
+ "2d6124a44ed048198d897c074561cd96": {
919
+ "model_module": "@jupyter-widgets/base",
920
+ "model_name": "LayoutModel",
921
+ "model_module_version": "1.2.0",
922
+ "state": {
923
+ "_model_module": "@jupyter-widgets/base",
924
+ "_model_module_version": "1.2.0",
925
+ "_model_name": "LayoutModel",
926
+ "_view_count": null,
927
+ "_view_module": "@jupyter-widgets/base",
928
+ "_view_module_version": "1.2.0",
929
+ "_view_name": "LayoutView",
930
+ "align_content": null,
931
+ "align_items": null,
932
+ "align_self": null,
933
+ "border": null,
934
+ "bottom": null,
935
+ "display": null,
936
+ "flex": null,
937
+ "flex_flow": null,
938
+ "grid_area": null,
939
+ "grid_auto_columns": null,
940
+ "grid_auto_flow": null,
941
+ "grid_auto_rows": null,
942
+ "grid_column": null,
943
+ "grid_gap": null,
944
+ "grid_row": null,
945
+ "grid_template_areas": null,
946
+ "grid_template_columns": null,
947
+ "grid_template_rows": null,
948
+ "height": null,
949
+ "justify_content": null,
950
+ "justify_items": null,
951
+ "left": null,
952
+ "margin": null,
953
+ "max_height": null,
954
+ "max_width": null,
955
+ "min_height": null,
956
+ "min_width": null,
957
+ "object_fit": null,
958
+ "object_position": null,
959
+ "order": null,
960
+ "overflow": null,
961
+ "overflow_x": null,
962
+ "overflow_y": null,
963
+ "padding": null,
964
+ "right": null,
965
+ "top": null,
966
+ "visibility": null,
967
+ "width": null
968
+ }
969
+ },
970
+ "b30c7621e14748f7a7e1f6683fac0674": {
971
+ "model_module": "@jupyter-widgets/controls",
972
+ "model_name": "ProgressStyleModel",
973
+ "model_module_version": "1.5.0",
974
+ "state": {
975
+ "_model_module": "@jupyter-widgets/controls",
976
+ "_model_module_version": "1.5.0",
977
+ "_model_name": "ProgressStyleModel",
978
+ "_view_count": null,
979
+ "_view_module": "@jupyter-widgets/base",
980
+ "_view_module_version": "1.2.0",
981
+ "_view_name": "StyleView",
982
+ "bar_color": null,
983
+ "description_width": ""
984
+ }
985
+ },
986
+ "5969532875ce41b6820a5c57b53908d9": {
987
+ "model_module": "@jupyter-widgets/base",
988
+ "model_name": "LayoutModel",
989
+ "model_module_version": "1.2.0",
990
+ "state": {
991
+ "_model_module": "@jupyter-widgets/base",
992
+ "_model_module_version": "1.2.0",
993
+ "_model_name": "LayoutModel",
994
+ "_view_count": null,
995
+ "_view_module": "@jupyter-widgets/base",
996
+ "_view_module_version": "1.2.0",
997
+ "_view_name": "LayoutView",
998
+ "align_content": null,
999
+ "align_items": null,
1000
+ "align_self": null,
1001
+ "border": null,
1002
+ "bottom": null,
1003
+ "display": null,
1004
+ "flex": null,
1005
+ "flex_flow": null,
1006
+ "grid_area": null,
1007
+ "grid_auto_columns": null,
1008
+ "grid_auto_flow": null,
1009
+ "grid_auto_rows": null,
1010
+ "grid_column": null,
1011
+ "grid_gap": null,
1012
+ "grid_row": null,
1013
+ "grid_template_areas": null,
1014
+ "grid_template_columns": null,
1015
+ "grid_template_rows": null,
1016
+ "height": null,
1017
+ "justify_content": null,
1018
+ "justify_items": null,
1019
+ "left": null,
1020
+ "margin": null,
1021
+ "max_height": null,
1022
+ "max_width": null,
1023
+ "min_height": null,
1024
+ "min_width": null,
1025
+ "object_fit": null,
1026
+ "object_position": null,
1027
+ "order": null,
1028
+ "overflow": null,
1029
+ "overflow_x": null,
1030
+ "overflow_y": null,
1031
+ "padding": null,
1032
+ "right": null,
1033
+ "top": null,
1034
+ "visibility": null,
1035
+ "width": null
1036
+ }
1037
+ },
1038
+ "af8771c98d3a4470a6781f1035e0856e": {
1039
+ "model_module": "@jupyter-widgets/controls",
1040
+ "model_name": "DescriptionStyleModel",
1041
+ "model_module_version": "1.5.0",
1042
+ "state": {
1043
+ "_model_module": "@jupyter-widgets/controls",
1044
+ "_model_module_version": "1.5.0",
1045
+ "_model_name": "DescriptionStyleModel",
1046
+ "_view_count": null,
1047
+ "_view_module": "@jupyter-widgets/base",
1048
+ "_view_module_version": "1.2.0",
1049
+ "_view_name": "StyleView",
1050
+ "description_width": ""
1051
+ }
1052
+ }
1053
+ }
1054
+ },
1055
+ "kaggle": {
1056
+ "accelerator": "gpu",
1057
+ "dataSources": [
1058
+ {
1059
+ "sourceType": "datasetVersion",
1060
+ "sourceId": 15056802,
1061
+ "datasetId": 9638685,
1062
+ "databundleVersionId": 15937545
1063
+ }
1064
+ ],
1065
+ "dockerImageVersionId": 31287,
1066
+ "isInternetEnabled": true,
1067
+ "language": "python",
1068
+ "sourceType": "notebook",
1069
+ "isGpuEnabled": true
1070
  }
1071
  },
1072
+ "nbformat_minor": 4,
1073
+ "nbformat": 4,
1074
  "cells": [
1075
  {
1076
  "cell_type": "markdown",
1077
+ "source": "# StockEx Clearing House β€” LLM Fine-Tuning\n\nFine-tunes a Qwen2.5 Instruct model with QLoRA to act as a clearing house trading agent.\n\n**Runs on both Kaggle and Colab.** Auto-selects model size based on available VRAM:\n\n| GPU | VRAM | Model |\n|-----|------|-------|\n| T4 (free) | 15 GB | Qwen2.5-7B-Instruct |\n| A100 40 GB | 40 GB | Qwen2.5-14B-Instruct |\n| A100 80 GB | 80 GB | Qwen2.5-32B-Instruct |\n\n**Resumable training:**\n- **Kaggle**: checkpoints saved/restored from dataset `xabonum/stockex-ch-checkpoints`\n- **Colab**: checkpoints saved/restored from Google Drive\n\n**Output model:** `RayMelius/stockex-ch-trader` on HuggingFace Hub\n\n**Required secret:** Add `HF_TOKEN` in Kaggle Secrets or Colab Secrets (πŸ”‘ icon in left sidebar)",
1078
+ "metadata": {
1079
+ "id": "title"
1080
+ }
1081
  },
1082
  {
1083
  "cell_type": "code",
1084
+ "source": "# ── Install dependencies ───────────────────────────────────────────────────────\n# Reinstall bitsandbytes with proper CUDA support (fixes triton.ops error on Colab)\n!pip install -q -U bitsandbytes\n!pip install -q \\\n \"transformers>=4.46.3\" \\\n \"peft>=0.13.2\" \\\n \"trl>=0.12.1\" \\\n \"datasets>=3.1.0\" \\\n \"accelerate>=1.1.1\" \\\n huggingface_hub\nprint(\"Dependencies installed.\")",
1085
+ "metadata": {
1086
+ "id": "install",
1087
+ "outputId": "a564dd39-8172-4745-e00c-c90fc17c6634",
1088
+ "trusted": true
1089
+ },
1090
  "outputs": [],
1091
+ "execution_count": null
1092
  },
1093
  {
1094
  "cell_type": "code",
1095
+ "source": "import os, json, random, torch\nfrom datasets import Dataset\nfrom transformers import (\n AutoTokenizer, AutoModelForCausalLM,\n BitsAndBytesConfig, TrainingArguments,\n)\nfrom peft import LoraConfig, get_peft_model, TaskType\nfrom trl import SFTTrainer, SFTConfig\nfrom huggingface_hub import login\n\nprint(f\"CUDA available: {torch.cuda.is_available()}\")\nif torch.cuda.is_available():\n print(f\"GPU: {torch.cuda.get_device_name(0)}\")\n print(f\"VRAM: {torch.cuda.get_device_properties(0).total_memory / 1e9:.1f} GB\")",
1096
+ "metadata": {
1097
+ "id": "imports",
1098
+ "outputId": "162eb31d-3a84-487c-c5b6-4f25c8d69485",
1099
+ "trusted": true
1100
+ },
1101
  "outputs": [],
1102
+ "execution_count": null
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1103
  },
1104
  {
1105
  "cell_type": "code",
1106
+ "source": "# ── Auto-select model based on available VRAM ─────────────────────────────────\nimport torch\n\nassert torch.cuda.is_available(), \"No GPU found β€” change runtime to GPU.\"\nvram_gb = torch.cuda.get_device_properties(0).total_memory / 1e9\ngpu_name = torch.cuda.get_device_name(0)\nprint(f\"GPU: {gpu_name} | VRAM: {vram_gb:.1f} GB\")\n\nif vram_gb >= 70:\n BASE_MODEL = \"Qwen/Qwen2.5-32B-Instruct\"\n BATCH_SIZE, GRAD_ACCUM, LR = 1, 16, 1e-4\nelif vram_gb >= 35:\n BASE_MODEL = \"Qwen/Qwen2.5-14B-Instruct\"\n BATCH_SIZE, GRAD_ACCUM, LR = 2, 8, 1e-4\nelse: # T4 / 15 GB\n BASE_MODEL = \"Qwen/Qwen2.5-7B-Instruct\"\n BATCH_SIZE, GRAD_ACCUM, LR = 4, 4, 2e-4\n\nprint(f\"Selected model: {BASE_MODEL}\")\n\n# ── Fixed config ───────────────────────────────────────────────────────────────\nOUTPUT_REPO = \"RayMelius/stockex-ch-trader\"\nOUTPUT_DIR = \"./stockex-ch-trader\"\nLORA_R = 16\nLORA_ALPHA = 32\nLORA_DROPOUT = 0.05\nNUM_EPOCHS = 3\nMAX_SEQ_LEN = 512\nDATASET_SIZE = 2500\n\n# ── HuggingFace login ─────────────────────────────────────────────────────────\nimport os\n\nHF_TOKEN = None\n\n# Kaggle\ntry:\n from kaggle_secrets import UserSecretsClient\n HF_TOKEN = UserSecretsClient().get_secret(\"HF_TOKEN\")\n print(\"HF_TOKEN loaded from Kaggle Secrets\")\nexcept:\n pass\n\n# Colab\nif not HF_TOKEN:\n try:\n from google.colab import userdata\n HF_TOKEN = userdata.get(\"HF_TOKEN\")\n print(\"HF_TOKEN loaded from Colab Secrets\")\n except:\n pass\n\n# Env fallback\nif not HF_TOKEN:\n HF_TOKEN = os.getenv(\"HF_TOKEN\")\n\nif not HF_TOKEN:\n raise ValueError(\"HF_TOKEN not found.\")\n\nfrom huggingface_hub import login\nlogin(token=HF_TOKEN)",
1107
+ "metadata": {
1108
+ "id": "config",
1109
+ "outputId": "c45e0d98-8928-4459-8449-63d498694d5d",
1110
+ "trusted": true
1111
+ },
1112
  "outputs": [],
1113
+ "execution_count": null
1114
  },
1115
  {
1116
  "cell_type": "markdown",
1117
+ "source": "## 1. Checkpoint Detection (Resumable Training)\n\nAutomatically detects and resumes from the latest checkpoint:\n- **Kaggle**: Downloads checkpoints from dataset `xabonum/stockex-ch-checkpoints`\n- **Colab**: Restores checkpoints from Google Drive\n\nOnly the **last 3 checkpoints** are kept in persistent storage.",
1118
+ "metadata": {
1119
+ "id": "dataset-header"
1120
+ }
 
 
 
 
 
 
1121
  },
1122
  {
1123
+ "cell_type": "code",
1124
+ "source": "import shutil, math, glob\nfrom transformers.trainer_utils import get_last_checkpoint\n\n# ── Detect environment ─────────────────────────────────────────────\nRUNNING_ON_KAGGLE = os.path.exists(\"/kaggle/working\")\nRUNNING_ON_COLAB = os.path.exists(\"/content\") and not RUNNING_ON_KAGGLE\n\nUSE_DRIVE = False\nDRIVE_CKPT_DIR = None\n\nif RUNNING_ON_COLAB:\n try:\n from google.colab import drive\n drive.mount(\"/content/drive\", force_remount=False)\n DRIVE_CKPT_DIR = \"/content/drive/MyDrive/stockex-ch-checkpoints\"\n USE_DRIVE = True\n print(f\"Colab: checkpoints will use {DRIVE_CKPT_DIR}\")\n except Exception as e:\n print(f\"Colab drive mount failed: {e} β€” saving locally only\")\n\nelif RUNNING_ON_KAGGLE:\n DRIVE_CKPT_DIR = \"/kaggle/working/stockex-ch-checkpoints\"\n USE_DRIVE = True\n os.makedirs(DRIVE_CKPT_DIR, exist_ok=True)\n\n # Download checkpoint dataset if available\n try:\n import subprocess\n result = subprocess.run(\n [\"kaggle\", \"datasets\", \"download\", \"-d\", \"xabonum/stockex-ch-checkpoints\",\n \"-p\", DRIVE_CKPT_DIR, \"--unzip\"],\n capture_output=True, text=True, timeout=120\n )\n if result.returncode == 0:\n print(f\"Kaggle: downloaded checkpoint dataset -> {DRIVE_CKPT_DIR}\")\n else:\n print(f\"Kaggle: no existing checkpoint dataset (starting fresh)\")\n print(f\" stderr: {result.stderr.strip()}\")\n except Exception as e:\n print(f\"Kaggle: checkpoint download failed: {e}\")\n\n print(f\"Kaggle: checkpoints will use {DRIVE_CKPT_DIR}\")\n\nelse:\n print(\"Unknown environment β€” saving locally only\")\n\n# ── Free GPU memory to prevent OOM on resume ────────────────────────\ntorch.cuda.empty_cache()\ntorch.cuda.reset_peak_memory_stats()\ntorch.cuda.ipc_collect()\nos.environ[\"PYTORCH_CUDA_ALLOC_CONF\"] = \"max_split_size_mb:128,garbage_collection_threshold:0.6\"\n\n# ── Restore checkpoint from persistent storage to local output dir ──\nos.makedirs(OUTPUT_DIR, exist_ok=True)\nif USE_DRIVE:\n os.makedirs(DRIVE_CKPT_DIR, exist_ok=True)\n drive_ckpt = get_last_checkpoint(DRIVE_CKPT_DIR)\n if drive_ckpt:\n local_ckpt_name = os.path.basename(drive_ckpt)\n local_ckpt_path = os.path.join(OUTPUT_DIR, local_ckpt_name)\n if not os.path.exists(local_ckpt_path):\n print(f\"Restoring checkpoint: {drive_ckpt} -> {local_ckpt_path}\")\n shutil.copytree(drive_ckpt, local_ckpt_path)\n\n# ── Detect latest local checkpoint ────────────────────────────────\nRESUME_FROM = get_last_checkpoint(OUTPUT_DIR)\nSAVE_STEPS = 10\n\n# ── Resume summary ─────────────────────────────────────────────────\ntrain_size = int(DATASET_SIZE * 0.9)\nsteps_per_epoch = math.ceil(train_size / (BATCH_SIZE * GRAD_ACCUM))\ntotal_steps = steps_per_epoch * NUM_EPOCHS\n\nif RESUME_FROM:\n completed_steps = int(os.path.basename(RESUME_FROM).split(\"-\")[-1])\n remaining = max(0, total_steps - completed_steps)\n pct_done = 100 * completed_steps / total_steps\n epoch_done = completed_steps / steps_per_epoch\n\n print(\"\\n\" + \"=\" * 55)\n print(\" RESUMING FROM CHECKPOINT\")\n print(\"=\" * 55)\n print(f\" Checkpoint : {os.path.basename(RESUME_FROM)}\")\n print(f\" Steps done : {completed_steps:,} / {total_steps:,} ({pct_done:.1f}%)\")\n print(f\" Steps left : {remaining:,}\")\n print(f\" Epoch : {epoch_done:.2f} / {NUM_EPOCHS}\")\n print(f\" Epochs left : {NUM_EPOCHS - epoch_done:.2f}\")\n print(f\" Steps/epoch : {steps_per_epoch:,}\")\n print(f\" Save every : {SAVE_STEPS} steps\")\n print(\"=\" * 55 + \"\\n\")\nelse:\n print(\"\\n\" + \"=\" * 55)\n print(\" STARTING FRESH\")\n print(\"=\" * 55)\n print(f\" Total steps : {total_steps:,}\")\n print(f\" Steps/epoch : {steps_per_epoch:,}\")\n print(f\" Epochs : {NUM_EPOCHS}\")\n print(f\" Save every : {SAVE_STEPS} steps\")\n print(\"=\" * 55 + \"\\n\")",
1125
+ "metadata": {
1126
+ "id": "egq0dp9csuo",
1127
+ "outputId": "f098a839-130c-4011-d0ff-a10289004957",
1128
+ "trusted": true
1129
+ },
1130
+ "outputs": [],
1131
+ "execution_count": null
1132
  },
1133
  {
1134
+ "cell_type": "markdown",
1135
+ "source": "## 2. Synthetic Dataset Generation\n\nEach training example is a realistic clearing house trading scenario:\n- Member state: capital, holdings, obligation remaining\n- Market: BBO for each security\n- Target: a valid JSON trading decision that respects all constraints",
1136
+ "metadata": {
1137
+ "trusted": true
1138
+ },
1139
+ "outputs": [],
1140
+ "execution_count": null
1141
  },
1142
  {
1143
  "cell_type": "code",
1144
+ "source": "# Securities from shared_data/securities.txt (symbol, start_price, current_price)\nSECURITIES = [\n {\"symbol\": \"ALPHA\", \"base\": 5.65},\n {\"symbol\": \"PEIR\", \"base\": 8.35},\n {\"symbol\": \"EXAE\", \"base\": 6.90},\n {\"symbol\": \"QUEST\", \"base\": 13.35},\n {\"symbol\": \"NBG\", \"base\": 8.00},\n {\"symbol\": \"EUROB\", \"base\": 3.45},\n {\"symbol\": \"AEG\", \"base\": 4.75},\n {\"symbol\": \"INTKA\", \"base\": 7.35},\n {\"symbol\": \"AAAK\", \"base\": 2.75},\n {\"symbol\": \"ATTIK\", \"base\": 4.90},\n]\n\nSTARTING_CAPITAL = 100_000.0\nDAILY_OBLIGATION = 10\n\n\ndef gen_bbo(base_price: float) -> dict:\n \"\"\"Generate a realistic bid/ask spread around a base price.\"\"\"\n drift = random.uniform(-0.05, 0.05)\n mid = round(base_price * (1 + drift), 2)\n spread = round(random.choice([0.05, 0.10, 0.15]), 2)\n best_bid = round(mid - spread / 2, 2)\n best_ask = round(mid + spread / 2, 2)\n return {\"best_bid\": best_bid, \"best_ask\": best_ask, \"mid\": mid}\n\n\ndef gen_holdings(bbos: dict) -> list:\n \"\"\"Randomly generate some holdings for a member.\"\"\"\n holdings = []\n n = random.randint(0, 4)\n for sym in random.sample(list(bbos.keys()), min(n, len(bbos))):\n qty = random.randint(50, 500)\n mid = bbos[sym][\"mid\"]\n avg_cost = round(mid * random.uniform(0.92, 1.08), 2)\n holdings.append({\"symbol\": sym, \"quantity\": qty, \"avg_cost\": avg_cost})\n return holdings\n\n\ndef build_prompt(member_id: str, capital: float, holdings: list,\n obligation_remaining: int, bbos: dict) -> str:\n market_lines = [\n f\" {sym}: Bid {bbo['best_bid']:.2f} / Ask {bbo['best_ask']:.2f}\"\n for sym, bbo in sorted(bbos.items())\n ]\n holding_lines = (\n [f\" {h['symbol']}: {h['quantity']} shares @ avg cost {h['avg_cost']:.2f}\"\n for h in holdings]\n if holdings else [\" None\"]\n )\n return (\n f\"You are simulating clearing house member {member_id} making ONE trading decision.\\n\\n\"\n f\"Member state:\\n\"\n f\" Available capital: EUR {capital:,.2f}\\n\"\n f\" Securities obligation remaining today: {obligation_remaining} more to trade\\n\"\n f\" Current holdings:\\n\" + \"\\n\".join(holding_lines) + \"\\n\\n\"\n f\"Current market (Bid/Ask):\\n\" + \"\\n\".join(market_lines) + \"\\n\\n\"\n f\"Rules:\\n\"\n f\"- Do not spend more than your available capital\\n\"\n f\"- Do not sell more shares than you hold\\n\"\n f\"- If you have no holdings, you must BUY\\n\"\n f\"- Choose a realistic price close to the BBO mid-price\\n\"\n f\"- Quantity should be between 10 and 200\\n\\n\"\n f\"Respond ONLY with valid JSON, no other text:\\n\"\n f'Example: {{\"symbol\": \"ALPHA\", \"side\": \"BUY\", \"quantity\": 50, \"price\": 5.65}}'\n )\n\n\ndef gen_decision(capital: float, holdings: list, bbos: dict) -> dict:\n \"\"\"Generate a rule-valid trading decision for the given state.\"\"\"\n holdings_value = sum(\n h[\"quantity\"] * bbos.get(h[\"symbol\"], {}).get(\"mid\", h[\"avg_cost\"])\n for h in holdings\n )\n net_worth = capital + holdings_value\n holdings_ratio = holdings_value / net_worth if net_worth > 0 else 0\n\n if not holdings:\n side = \"BUY\"\n elif holdings_ratio > 0.6:\n side = random.choices([\"SELL\", \"BUY\"], weights=[0.7, 0.3])[0]\n else:\n side = random.choices([\"BUY\", \"SELL\"], weights=[0.55, 0.45])[0]\n\n if side == \"BUY\":\n affordable = [sym for sym, bbo in bbos.items() if 10 * bbo[\"best_ask\"] <= capital]\n if not affordable:\n sym = min(bbos, key=lambda s: bbos[s][\"best_ask\"])\n else:\n held_syms = [h[\"symbol\"] for h in holdings]\n weights = [3 if s in held_syms else 1 for s in affordable]\n sym = random.choices(affordable, weights=weights)[0]\n ask = bbos[sym][\"best_ask\"]\n max_qty = min(200, int(capital / ask))\n qty = random.randint(10, max(10, max_qty))\n price = round(bbos[sym][\"mid\"] + random.uniform(-0.05, 0.05), 2)\n price = max(bbos[sym][\"best_bid\"], min(price, ask))\n return {\"symbol\": sym, \"side\": \"BUY\", \"quantity\": qty, \"price\": round(price, 2)}\n else:\n h = random.choice(holdings)\n sym = h[\"symbol\"]\n bbo = bbos[sym]\n qty = random.randint(10, min(200, h[\"quantity\"]))\n price = round(bbo[\"mid\"] + random.uniform(-0.05, 0.05), 2)\n price = max(bbo[\"best_bid\"] - 0.05, min(price, bbo[\"best_ask\"]))\n return {\"symbol\": sym, \"side\": \"SELL\", \"quantity\": qty, \"price\": round(price, 2)}\n\n\ndef generate_dataset(n: int) -> list:\n examples = []\n member_ids = [f\"USR{i:02d}\" for i in range(1, 11)]\n scenarios = [\n ((80_000, 100_000), (5, 10), \"fresh_member\"),\n ((50_000, 80_000), (0, 5), \"active_member\"),\n ((20_000, 50_000), (0, 2), \"low_capital\"),\n ((5_000, 20_000), (0, 10), \"very_low_capital\"),\n ((90_000, 100_000), (10, 10),\"start_of_day\"),\n ]\n for _ in range(n):\n cap_range, obl_range, _ = random.choice(scenarios)\n capital = round(random.uniform(*cap_range), 2)\n obligation = random.randint(*obl_range)\n member_id = random.choice(member_ids)\n bbos = {s[\"symbol\"]: gen_bbo(s[\"base\"]) for s in SECURITIES}\n holdings = gen_holdings(bbos)\n holdings_cost = sum(h[\"quantity\"] * h[\"avg_cost\"] for h in holdings)\n if holdings_cost > STARTING_CAPITAL - capital:\n scale = (STARTING_CAPITAL - capital) / max(holdings_cost, 1)\n for h in holdings:\n h[\"quantity\"] = max(10, int(h[\"quantity\"] * scale))\n prompt = build_prompt(member_id, capital, holdings, obligation, bbos)\n decision = gen_decision(capital, holdings, bbos)\n examples.append({\"prompt\": prompt, \"completion\": json.dumps(decision)})\n return examples\n\n\nprint(f\"Generating {DATASET_SIZE} training examples...\")\nraw_data = generate_dataset(DATASET_SIZE)\nprint(f\"Done. Example:\")\nprint(\"PROMPT:\\n\", raw_data[0][\"prompt\"])\nprint(\"\\nCOMPLETION:\", raw_data[0][\"completion\"])",
1145
+ "metadata": {
1146
+ "id": "dataset-gen",
1147
+ "outputId": "6b6ae157-9c91-42d5-a556-049f122bf7f1",
1148
+ "trusted": true
1149
+ },
1150
  "outputs": [],
1151
+ "execution_count": null
1152
  },
1153
  {
1154
  "cell_type": "code",
1155
+ "source": "# Train/val split (90/10)\nrandom.shuffle(raw_data)\nsplit = int(len(raw_data) * 0.9)\ntrain_data = raw_data[:split]\nval_data = raw_data[split:]\n\ntrain_dataset = Dataset.from_list(train_data)\nval_dataset = Dataset.from_list(val_data)\nprint(f\"Train: {len(train_dataset)} | Val: {len(val_dataset)}\")",
1156
+ "metadata": {
1157
+ "id": "dataset-split",
1158
+ "outputId": "3246d564-c102-4395-8fbc-c3f7979130fb",
1159
+ "trusted": true
1160
+ },
1161
  "outputs": [],
1162
+ "execution_count": null
 
 
 
 
 
 
 
 
 
 
1163
  },
1164
  {
1165
  "cell_type": "markdown",
1166
+ "source": "## 3. Load Base Model (4-bit QLoRA)",
1167
+ "metadata": {
1168
+ "id": "model-header"
1169
+ }
 
1170
  },
1171
  {
1172
  "cell_type": "code",
1173
+ "source": "print(f\"Loading tokenizer: {BASE_MODEL}\")\ntokenizer = AutoTokenizer.from_pretrained(BASE_MODEL, trust_remote_code=True)\ntokenizer.pad_token = tokenizer.eos_token\ntokenizer.padding_side = \"right\"\ntokenizer.model_max_length = MAX_SEQ_LEN # replaces max_seq_length in SFTConfig\nprint(\"Tokenizer loaded\")",
1174
+ "metadata": {
1175
+ "id": "load-tokenizer",
1176
+ "outputId": "a5008937-66ed-4028-a215-62e458cfc8dd",
1177
+ "trusted": true
1178
+ },
1179
  "outputs": [],
1180
+ "execution_count": null
1181
  },
1182
  {
1183
  "cell_type": "code",
1184
+ "source": "SYSTEM_PROMPT = (\n \"You are a StockEx clearing house trading agent. \"\n \"Given a member's financial state and live market data, \"\n \"you output a single valid JSON trading decision that respects all capital and holdings constraints. \"\n \"Never output anything other than the JSON object.\"\n)\n\n\ndef format_chat(example):\n \"\"\"Apply the model's chat template to produce a training string.\"\"\"\n messages = [\n {\"role\": \"system\", \"content\": SYSTEM_PROMPT},\n {\"role\": \"user\", \"content\": example[\"prompt\"]},\n {\"role\": \"assistant\", \"content\": example[\"completion\"]},\n ]\n text = tokenizer.apply_chat_template(\n messages,\n tokenize=False,\n add_generation_prompt=False,\n )\n return {\"text\": text}\n\n\ntrain_dataset = train_dataset.map(format_chat)\nval_dataset = val_dataset.map(format_chat)\n\nprint(\"Sample formatted text:\")\nprint(train_dataset[0][\"text\"][:600], \"...\")",
1185
+ "metadata": {
1186
+ "id": "format-dataset",
1187
+ "outputId": "ea4f1fa8-bf2b-4360-c426-28e5255dbbd3",
1188
+ "trusted": true
1189
+ },
1190
  "outputs": [],
1191
+ "execution_count": null
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1192
  },
1193
  {
1194
  "cell_type": "code",
1195
+ "source": "# 4-bit quantization config\nbnb_config = BitsAndBytesConfig(\n load_in_4bit=True,\n bnb_4bit_quant_type=\"nf4\",\n bnb_4bit_compute_dtype=torch.bfloat16,\n bnb_4bit_use_double_quant=True,\n)\n\nprint(f\"Loading model: {BASE_MODEL} (4-bit)\")\nmodel = AutoModelForCausalLM.from_pretrained(\n BASE_MODEL,\n quantization_config=bnb_config,\n device_map=\"auto\",\n trust_remote_code=True,\n dtype=torch.bfloat16,\n)\nmodel.config.use_cache = False\nmodel.config.pretraining_tp = 1\nprint(f\"Model loaded. Parameters: {model.num_parameters()/1e9:.2f}B\")\n\nfrom peft import prepare_model_for_kbit_training\n\nmodel = prepare_model_for_kbit_training(model)",
1196
+ "metadata": {
1197
+ "id": "load-model",
1198
+ "outputId": "7f46083d-c830-45f3-e887-fbe9cf30b4b2",
1199
+ "trusted": true
1200
+ },
1201
  "outputs": [],
1202
+ "execution_count": null
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1203
  },
1204
  {
1205
  "cell_type": "markdown",
1206
+ "source": "## 3b. LoRA Configuration",
1207
+ "metadata": {
1208
+ "id": "lora-header"
1209
+ }
 
1210
  },
1211
  {
1212
  "cell_type": "code",
1213
+ "source": "lora_config = LoraConfig(\n r=LORA_R,\n lora_alpha=LORA_ALPHA,\n target_modules=[\n \"q_proj\", \"k_proj\", \"v_proj\", \"o_proj\",\n \"gate_proj\", \"up_proj\", \"down_proj\",\n ],\n lora_dropout=LORA_DROPOUT,\n bias=\"none\",\n task_type=TaskType.CAUSAL_LM,\n)\n\ntrainable = sum(p.numel() for p in model.parameters() if p.requires_grad)\ntotal = sum(p.numel() for p in model.parameters())\nprint(f\"Trainable parameters: {trainable/1e6:.1f}M / {total/1e6:.0f}M ({100*trainable/total:.2f}%)\")",
1214
+ "metadata": {
1215
+ "id": "lora-config",
1216
+ "outputId": "8a2688ba-288b-4149-ff37-c8d0ee11f77f",
1217
+ "trusted": true
1218
+ },
1219
  "outputs": [],
1220
+ "execution_count": null
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1221
  },
1222
  {
1223
  "cell_type": "markdown",
1224
+ "source": "## 4. Train",
1225
+ "metadata": {
1226
+ "id": "training-header"
1227
+ }
 
1228
  },
1229
  {
1230
  "cell_type": "code",
1231
+ "source": "from transformers import TrainerCallback\nimport shutil, glob\n\nKAGGLE_DATASET_ID = \"xabonum/stockex-ch-checkpoints\"\nMAX_CHECKPOINTS_PERSISTENT = 3 # keep only last 3 in Kaggle dataset / Drive\n\n# ── Create Kaggle dataset metadata if needed ────────────────────────\nif USE_DRIVE and DRIVE_CKPT_DIR:\n metadata_path = os.path.join(DRIVE_CKPT_DIR, \"dataset-metadata.json\")\n if not os.path.exists(metadata_path):\n metadata = {\n \"title\": \"stockex-ch-checkpoints\",\n \"id\": KAGGLE_DATASET_ID,\n \"licenses\": [{\"name\": \"CC0-1.0\"}]\n }\n with open(metadata_path, \"w\") as f:\n json.dump(metadata, f, indent=2)\n print(\"Created dataset-metadata.json\")\n\n\ndef cleanup_old_checkpoints(ckpt_dir, keep=MAX_CHECKPOINTS_PERSISTENT):\n \"\"\"Keep only the last `keep` checkpoints in persistent storage.\"\"\"\n ckpts = sorted(\n glob.glob(os.path.join(ckpt_dir, \"checkpoint-*\")),\n key=lambda p: int(os.path.basename(p).split(\"-\")[-1])\n )\n while len(ckpts) > keep:\n old = ckpts.pop(0)\n shutil.rmtree(old)\n print(f\"[Checkpoint] Removed old: {os.path.basename(old)}\")\n\n\nclass CheckpointSyncCallback(TrainerCallback):\n \"\"\"Copy checkpoint to persistent storage, push LoRA to HF Hub, upload to Kaggle dataset.\"\"\"\n\n def on_save(self, args, state, control, **kwargs):\n ckpt_dir = os.path.join(args.output_dir, f\"checkpoint-{state.global_step}\")\n if not os.path.isdir(ckpt_dir):\n return\n\n # 1. Save to persistent folder (Drive / Kaggle working dir)\n if USE_DRIVE and DRIVE_CKPT_DIR:\n dest = os.path.join(DRIVE_CKPT_DIR, f\"checkpoint-{state.global_step}\")\n os.makedirs(DRIVE_CKPT_DIR, exist_ok=True)\n try:\n shutil.copytree(ckpt_dir, dest, dirs_exist_ok=True)\n print(f\"[Checkpoint] Saved -> {dest}\")\n except Exception as e:\n print(f\"[Checkpoint] Copy failed: {e}\")\n\n # Cleanup: keep only last 3 checkpoints in persistent storage\n try:\n cleanup_old_checkpoints(DRIVE_CKPT_DIR)\n except Exception as e:\n print(f\"[Checkpoint] Cleanup failed: {e}\")\n\n # 2. Push LoRA adapter to HF Hub\n try:\n kwargs[\"model\"].push_to_hub(\n OUTPUT_REPO,\n commit_message=f\"Checkpoint step {state.global_step} (epoch {state.epoch:.2f})\",\n token=HF_TOKEN,\n )\n print(f\"[Checkpoint] Pushed step {state.global_step} -> HF Hub\")\n except Exception as e:\n print(f\"[Checkpoint] HF push failed: {e}\")\n\n # 3. Upload to Kaggle dataset (Kaggle only)\n if RUNNING_ON_KAGGLE and USE_DRIVE:\n try:\n os.system(\n f\"kaggle datasets version -p {DRIVE_CKPT_DIR} \"\n f\"-m 'Checkpoint step {state.global_step}' --dir-mode zip\"\n )\n print(f\"[Checkpoint] Kaggle dataset updated for step {state.global_step}\")\n except Exception as e:\n print(f\"[Checkpoint] Kaggle dataset update failed: {e}\")\n\n\nsft_config = SFTConfig(\n output_dir=OUTPUT_DIR,\n num_train_epochs=NUM_EPOCHS,\n per_device_train_batch_size=BATCH_SIZE,\n per_device_eval_batch_size=BATCH_SIZE,\n gradient_accumulation_steps=GRAD_ACCUM,\n gradient_checkpointing=True,\n optim=\"paged_adamw_8bit\",\n learning_rate=LR,\n lr_scheduler_type=\"cosine\",\n warmup_ratio=0.05,\n fp16=not torch.cuda.is_bf16_supported(),\n bf16=torch.cuda.is_bf16_supported(),\n logging_steps=10,\n eval_strategy=\"steps\",\n eval_steps=SAVE_STEPS,\n save_strategy=\"steps\",\n save_steps=SAVE_STEPS,\n save_total_limit=3,\n load_best_model_at_end=True,\n metric_for_best_model=\"eval_loss\",\n greater_is_better=False,\n report_to=\"none\",\n dataset_text_field=\"text\",\n packing=False,\n)\n\ntrainer = SFTTrainer(\n model=model,\n args=sft_config,\n train_dataset=train_dataset,\n eval_dataset=val_dataset,\n peft_config=lora_config,\n processing_class=tokenizer,\n callbacks=[CheckpointSyncCallback()],\n)\n\nif RESUME_FROM:\n print(f\"Resuming from checkpoint: {RESUME_FROM}\")\nelse:\n print(f\"Starting training from scratch. Save every {SAVE_STEPS} steps.\")\n\ntrainer.train(resume_from_checkpoint=RESUME_FROM)\nprint(\"Training complete.\")",
1232
+ "metadata": {
1233
+ "trusted": true
1234
+ },
1235
  "outputs": [],
1236
+ "execution_count": null
1237
  },
1238
  {
1239
  "cell_type": "markdown",
1240
+ "source": "## 5. Save & Push to HuggingFace Hub\n\nMerges LoRA adapters into the base model weights and pushes the full model.",
1241
+ "metadata": {
1242
+ "id": "save-header"
1243
+ }
 
 
 
1244
  },
1245
  {
1246
  "cell_type": "code",
1247
+ "source": "from peft import PeftModel\n\n# Save best adapter checkpoint locally\ntrainer.model.save_pretrained(OUTPUT_DIR)\ntokenizer.save_pretrained(OUTPUT_DIR)\nprint(f\"Adapter saved to {OUTPUT_DIR}\")\n\n# Reload base model in fp16 for merging (can't merge with 4-bit)\nprint(\"Reloading base model in fp16 for adapter merge...\")\ndel model\ntorch.cuda.empty_cache()\n\nbase_model = AutoModelForCausalLM.from_pretrained(\n BASE_MODEL,\n torch_dtype=torch.float16,\n device_map=\"auto\",\n trust_remote_code=True,\n)\nmerged_model = PeftModel.from_pretrained(base_model, OUTPUT_DIR)\nmerged_model = merged_model.merge_and_unload()\nprint(\"Adapters merged.\")",
1248
+ "metadata": {
1249
+ "id": "save-model",
1250
+ "trusted": true
1251
+ },
1252
  "outputs": [],
1253
+ "execution_count": null
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1254
  },
1255
  {
1256
  "cell_type": "code",
1257
+ "source": "print(f\"Pushing merged model to: {OUTPUT_REPO}\")\nmerged_model.push_to_hub(\n OUTPUT_REPO,\n token=HF_TOKEN,\n commit_message=\"StockEx CH Trader: QLoRA fine-tuned Qwen2.5-32B-Instruct\",\n)\ntokenizer.push_to_hub(\n OUTPUT_REPO,\n token=HF_TOKEN,\n commit_message=\"Tokenizer for StockEx CH Trader (Qwen2.5-32B-Instruct base)\",\n)\nprint(f\"βœ“ Model pushed to https://huggingface.co/{OUTPUT_REPO}\")",
1258
+ "metadata": {
1259
+ "id": "push-hub",
1260
+ "trusted": true
1261
+ },
1262
  "outputs": [],
1263
+ "execution_count": null
1264
  },
1265
  {
1266
  "cell_type": "markdown",
1267
+ "source": "## 6. Inference Test\n\nVerify the model generates valid JSON trading decisions.",
1268
+ "metadata": {
1269
+ "id": "test-header"
1270
+ }
 
 
 
1271
  },
1272
  {
1273
  "cell_type": "code",
1274
+ "source": "import re\nfrom transformers import pipeline\n\npipe = pipeline(\n \"text-generation\",\n model=merged_model,\n tokenizer=tokenizer,\n device_map=\"auto\",\n)\n\ntest_cases = [\n {\n \"desc\": \"New member, no holdings, must trade\",\n \"capital\": 100_000.0,\n \"holdings\": [],\n \"obligation\": 10,\n },\n {\n \"desc\": \"Experienced member with holdings, low obligation\",\n \"capital\": 65_000.0,\n \"holdings\": [\n {\"symbol\": \"ALPHA\", \"quantity\": 300, \"avg_cost\": 5.60},\n {\"symbol\": \"QUEST\", \"quantity\": 150, \"avg_cost\": 13.20},\n ],\n \"obligation\": 2,\n },\n {\n \"desc\": \"Low capital, large holdings\",\n \"capital\": 8_000.0,\n \"holdings\": [\n {\"symbol\": \"PEIR\", \"quantity\": 500, \"avg_cost\": 8.30},\n {\"symbol\": \"NBG\", \"quantity\": 200, \"avg_cost\": 7.95},\n ],\n \"obligation\": 5,\n },\n]\n\ntest_bbos = {s[\"symbol\"]: gen_bbo(s[\"base\"]) for s in SECURITIES}\n\nprint(\"=\" * 70)\nfor tc in test_cases:\n print(f\"\\nSCENARIO: {tc['desc']}\")\n prompt = build_prompt(\"USR01\", tc[\"capital\"], tc[\"holdings\"], tc[\"obligation\"], test_bbos)\n messages = [\n {\"role\": \"system\", \"content\": SYSTEM_PROMPT},\n {\"role\": \"user\", \"content\": prompt},\n ]\n output = pipe(\n messages,\n max_new_tokens=60,\n temperature=0.3,\n do_sample=True,\n pad_token_id=tokenizer.eos_token_id,\n )\n response = output[0][\"generated_text\"][-1][\"content\"].strip()\n print(f\"RESPONSE: {response}\")\n try:\n m = re.search(r\"\\{[^}]+\\}\", response)\n if m:\n d = json.loads(m.group())\n assert d[\"side\"] in (\"BUY\", \"SELL\")\n assert d[\"symbol\"] in [s[\"symbol\"] for s in SECURITIES]\n assert d[\"quantity\"] > 0\n assert d[\"price\"] > 0\n print(f\"βœ“ Valid JSON: {d}\")\n else:\n print(\"βœ— No JSON found in response\")\n except Exception as e:\n print(f\"βœ— Invalid: {e}\")\n print(\"-\" * 70)",
1275
+ "metadata": {
1276
+ "id": "inference-test",
1277
+ "trusted": true
1278
+ },
1279
  "outputs": [],
1280
+ "execution_count": null
1281
  },
1282
  {
1283
  "cell_type": "markdown",
1284
+ "source": "## 7. Activate in StockEx\n\nThe clearing house already uses `RayMelius/stockex-ch-trader` as default.\n\nTo switch to this model in a running StockEx instance:\n\n**HuggingFace Spaces** β€” add to secrets:\n```\nHF_MODEL = RayMelius/stockex-ch-trader\nHF_TOKEN = <your token>\n```\n\n**Docker Compose** β€” already set in `docker-compose.yml`:\n```yaml\nenvironment:\n - HF_MODEL=RayMelius/stockex-ch-trader\n - HF_TOKEN=<your token>\n```",
1285
+ "metadata": {
1286
+ "id": "usage-header"
1287
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1288
  }
1289
  ]
1290
  }
notebooks/stockex-clearing-house-llm-fine-tuning.ipynb ADDED
@@ -0,0 +1,1290 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "metadata": {
3
+ "kernelspec": {
4
+ "display_name": "Python 3",
5
+ "language": "python",
6
+ "name": "python3"
7
+ },
8
+ "language_info": {
9
+ "name": "python",
10
+ "version": "3.12.12",
11
+ "mimetype": "text/x-python",
12
+ "codemirror_mode": {
13
+ "name": "ipython",
14
+ "version": 3
15
+ },
16
+ "pygments_lexer": "ipython3",
17
+ "nbconvert_exporter": "python",
18
+ "file_extension": ".py"
19
+ },
20
+ "accelerator": "GPU",
21
+ "colab": {
22
+ "gpuType": "T4",
23
+ "provenance": []
24
+ },
25
+ "widgets": {
26
+ "application/vnd.jupyter.widget-state+json": {
27
+ "7d1727a12c9445f88824a433ec067d05": {
28
+ "model_module": "@jupyter-widgets/controls",
29
+ "model_name": "HBoxModel",
30
+ "model_module_version": "1.5.0",
31
+ "state": {
32
+ "_dom_classes": [],
33
+ "_model_module": "@jupyter-widgets/controls",
34
+ "_model_module_version": "1.5.0",
35
+ "_model_name": "HBoxModel",
36
+ "_view_count": null,
37
+ "_view_module": "@jupyter-widgets/controls",
38
+ "_view_module_version": "1.5.0",
39
+ "_view_name": "HBoxView",
40
+ "box_style": "",
41
+ "children": [
42
+ "IPY_MODEL_ddf4a278a228476aadf8e154bf92c19b",
43
+ "IPY_MODEL_607775cdd08f4dd4a3a0d50a788dc631",
44
+ "IPY_MODEL_f6a6a369e6004316827310f3ff71c597"
45
+ ],
46
+ "layout": "IPY_MODEL_030fdf253ff141c09913c8e99d81637f"
47
+ }
48
+ },
49
+ "ddf4a278a228476aadf8e154bf92c19b": {
50
+ "model_module": "@jupyter-widgets/controls",
51
+ "model_name": "HTMLModel",
52
+ "model_module_version": "1.5.0",
53
+ "state": {
54
+ "_dom_classes": [],
55
+ "_model_module": "@jupyter-widgets/controls",
56
+ "_model_module_version": "1.5.0",
57
+ "_model_name": "HTMLModel",
58
+ "_view_count": null,
59
+ "_view_module": "@jupyter-widgets/controls",
60
+ "_view_module_version": "1.5.0",
61
+ "_view_name": "HTMLView",
62
+ "description": "",
63
+ "description_tooltip": null,
64
+ "layout": "IPY_MODEL_41d79e9429514c37b51282c281978ccb",
65
+ "placeholder": "​",
66
+ "style": "IPY_MODEL_474c0b3c456b462c9d98a635c09daff1",
67
+ "value": "Map: 100%"
68
+ }
69
+ },
70
+ "607775cdd08f4dd4a3a0d50a788dc631": {
71
+ "model_module": "@jupyter-widgets/controls",
72
+ "model_name": "FloatProgressModel",
73
+ "model_module_version": "1.5.0",
74
+ "state": {
75
+ "_dom_classes": [],
76
+ "_model_module": "@jupyter-widgets/controls",
77
+ "_model_module_version": "1.5.0",
78
+ "_model_name": "FloatProgressModel",
79
+ "_view_count": null,
80
+ "_view_module": "@jupyter-widgets/controls",
81
+ "_view_module_version": "1.5.0",
82
+ "_view_name": "ProgressView",
83
+ "bar_style": "success",
84
+ "description": "",
85
+ "description_tooltip": null,
86
+ "layout": "IPY_MODEL_897d25d509bf4d46bdf1e9d6d6ae6caa",
87
+ "max": 2250,
88
+ "min": 0,
89
+ "orientation": "horizontal",
90
+ "style": "IPY_MODEL_292804d6b77f4de39c24b768a205ca80",
91
+ "value": 2250
92
+ }
93
+ },
94
+ "f6a6a369e6004316827310f3ff71c597": {
95
+ "model_module": "@jupyter-widgets/controls",
96
+ "model_name": "HTMLModel",
97
+ "model_module_version": "1.5.0",
98
+ "state": {
99
+ "_dom_classes": [],
100
+ "_model_module": "@jupyter-widgets/controls",
101
+ "_model_module_version": "1.5.0",
102
+ "_model_name": "HTMLModel",
103
+ "_view_count": null,
104
+ "_view_module": "@jupyter-widgets/controls",
105
+ "_view_module_version": "1.5.0",
106
+ "_view_name": "HTMLView",
107
+ "description": "",
108
+ "description_tooltip": null,
109
+ "layout": "IPY_MODEL_00baa69256d04592bd69f4c41e7fb572",
110
+ "placeholder": "​",
111
+ "style": "IPY_MODEL_beaa41bfdbac47b1a8ffddf18d49f74d",
112
+ "value": " 2250/2250 [00:00&lt;00:00, 2300.58 examples/s]"
113
+ }
114
+ },
115
+ "030fdf253ff141c09913c8e99d81637f": {
116
+ "model_module": "@jupyter-widgets/base",
117
+ "model_name": "LayoutModel",
118
+ "model_module_version": "1.2.0",
119
+ "state": {
120
+ "_model_module": "@jupyter-widgets/base",
121
+ "_model_module_version": "1.2.0",
122
+ "_model_name": "LayoutModel",
123
+ "_view_count": null,
124
+ "_view_module": "@jupyter-widgets/base",
125
+ "_view_module_version": "1.2.0",
126
+ "_view_name": "LayoutView",
127
+ "align_content": null,
128
+ "align_items": null,
129
+ "align_self": null,
130
+ "border": null,
131
+ "bottom": null,
132
+ "display": null,
133
+ "flex": null,
134
+ "flex_flow": null,
135
+ "grid_area": null,
136
+ "grid_auto_columns": null,
137
+ "grid_auto_flow": null,
138
+ "grid_auto_rows": null,
139
+ "grid_column": null,
140
+ "grid_gap": null,
141
+ "grid_row": null,
142
+ "grid_template_areas": null,
143
+ "grid_template_columns": null,
144
+ "grid_template_rows": null,
145
+ "height": null,
146
+ "justify_content": null,
147
+ "justify_items": null,
148
+ "left": null,
149
+ "margin": null,
150
+ "max_height": null,
151
+ "max_width": null,
152
+ "min_height": null,
153
+ "min_width": null,
154
+ "object_fit": null,
155
+ "object_position": null,
156
+ "order": null,
157
+ "overflow": null,
158
+ "overflow_x": null,
159
+ "overflow_y": null,
160
+ "padding": null,
161
+ "right": null,
162
+ "top": null,
163
+ "visibility": null,
164
+ "width": null
165
+ }
166
+ },
167
+ "41d79e9429514c37b51282c281978ccb": {
168
+ "model_module": "@jupyter-widgets/base",
169
+ "model_name": "LayoutModel",
170
+ "model_module_version": "1.2.0",
171
+ "state": {
172
+ "_model_module": "@jupyter-widgets/base",
173
+ "_model_module_version": "1.2.0",
174
+ "_model_name": "LayoutModel",
175
+ "_view_count": null,
176
+ "_view_module": "@jupyter-widgets/base",
177
+ "_view_module_version": "1.2.0",
178
+ "_view_name": "LayoutView",
179
+ "align_content": null,
180
+ "align_items": null,
181
+ "align_self": null,
182
+ "border": null,
183
+ "bottom": null,
184
+ "display": null,
185
+ "flex": null,
186
+ "flex_flow": null,
187
+ "grid_area": null,
188
+ "grid_auto_columns": null,
189
+ "grid_auto_flow": null,
190
+ "grid_auto_rows": null,
191
+ "grid_column": null,
192
+ "grid_gap": null,
193
+ "grid_row": null,
194
+ "grid_template_areas": null,
195
+ "grid_template_columns": null,
196
+ "grid_template_rows": null,
197
+ "height": null,
198
+ "justify_content": null,
199
+ "justify_items": null,
200
+ "left": null,
201
+ "margin": null,
202
+ "max_height": null,
203
+ "max_width": null,
204
+ "min_height": null,
205
+ "min_width": null,
206
+ "object_fit": null,
207
+ "object_position": null,
208
+ "order": null,
209
+ "overflow": null,
210
+ "overflow_x": null,
211
+ "overflow_y": null,
212
+ "padding": null,
213
+ "right": null,
214
+ "top": null,
215
+ "visibility": null,
216
+ "width": null
217
+ }
218
+ },
219
+ "474c0b3c456b462c9d98a635c09daff1": {
220
+ "model_module": "@jupyter-widgets/controls",
221
+ "model_name": "DescriptionStyleModel",
222
+ "model_module_version": "1.5.0",
223
+ "state": {
224
+ "_model_module": "@jupyter-widgets/controls",
225
+ "_model_module_version": "1.5.0",
226
+ "_model_name": "DescriptionStyleModel",
227
+ "_view_count": null,
228
+ "_view_module": "@jupyter-widgets/base",
229
+ "_view_module_version": "1.2.0",
230
+ "_view_name": "StyleView",
231
+ "description_width": ""
232
+ }
233
+ },
234
+ "897d25d509bf4d46bdf1e9d6d6ae6caa": {
235
+ "model_module": "@jupyter-widgets/base",
236
+ "model_name": "LayoutModel",
237
+ "model_module_version": "1.2.0",
238
+ "state": {
239
+ "_model_module": "@jupyter-widgets/base",
240
+ "_model_module_version": "1.2.0",
241
+ "_model_name": "LayoutModel",
242
+ "_view_count": null,
243
+ "_view_module": "@jupyter-widgets/base",
244
+ "_view_module_version": "1.2.0",
245
+ "_view_name": "LayoutView",
246
+ "align_content": null,
247
+ "align_items": null,
248
+ "align_self": null,
249
+ "border": null,
250
+ "bottom": null,
251
+ "display": null,
252
+ "flex": null,
253
+ "flex_flow": null,
254
+ "grid_area": null,
255
+ "grid_auto_columns": null,
256
+ "grid_auto_flow": null,
257
+ "grid_auto_rows": null,
258
+ "grid_column": null,
259
+ "grid_gap": null,
260
+ "grid_row": null,
261
+ "grid_template_areas": null,
262
+ "grid_template_columns": null,
263
+ "grid_template_rows": null,
264
+ "height": null,
265
+ "justify_content": null,
266
+ "justify_items": null,
267
+ "left": null,
268
+ "margin": null,
269
+ "max_height": null,
270
+ "max_width": null,
271
+ "min_height": null,
272
+ "min_width": null,
273
+ "object_fit": null,
274
+ "object_position": null,
275
+ "order": null,
276
+ "overflow": null,
277
+ "overflow_x": null,
278
+ "overflow_y": null,
279
+ "padding": null,
280
+ "right": null,
281
+ "top": null,
282
+ "visibility": null,
283
+ "width": null
284
+ }
285
+ },
286
+ "292804d6b77f4de39c24b768a205ca80": {
287
+ "model_module": "@jupyter-widgets/controls",
288
+ "model_name": "ProgressStyleModel",
289
+ "model_module_version": "1.5.0",
290
+ "state": {
291
+ "_model_module": "@jupyter-widgets/controls",
292
+ "_model_module_version": "1.5.0",
293
+ "_model_name": "ProgressStyleModel",
294
+ "_view_count": null,
295
+ "_view_module": "@jupyter-widgets/base",
296
+ "_view_module_version": "1.2.0",
297
+ "_view_name": "StyleView",
298
+ "bar_color": null,
299
+ "description_width": ""
300
+ }
301
+ },
302
+ "00baa69256d04592bd69f4c41e7fb572": {
303
+ "model_module": "@jupyter-widgets/base",
304
+ "model_name": "LayoutModel",
305
+ "model_module_version": "1.2.0",
306
+ "state": {
307
+ "_model_module": "@jupyter-widgets/base",
308
+ "_model_module_version": "1.2.0",
309
+ "_model_name": "LayoutModel",
310
+ "_view_count": null,
311
+ "_view_module": "@jupyter-widgets/base",
312
+ "_view_module_version": "1.2.0",
313
+ "_view_name": "LayoutView",
314
+ "align_content": null,
315
+ "align_items": null,
316
+ "align_self": null,
317
+ "border": null,
318
+ "bottom": null,
319
+ "display": null,
320
+ "flex": null,
321
+ "flex_flow": null,
322
+ "grid_area": null,
323
+ "grid_auto_columns": null,
324
+ "grid_auto_flow": null,
325
+ "grid_auto_rows": null,
326
+ "grid_column": null,
327
+ "grid_gap": null,
328
+ "grid_row": null,
329
+ "grid_template_areas": null,
330
+ "grid_template_columns": null,
331
+ "grid_template_rows": null,
332
+ "height": null,
333
+ "justify_content": null,
334
+ "justify_items": null,
335
+ "left": null,
336
+ "margin": null,
337
+ "max_height": null,
338
+ "max_width": null,
339
+ "min_height": null,
340
+ "min_width": null,
341
+ "object_fit": null,
342
+ "object_position": null,
343
+ "order": null,
344
+ "overflow": null,
345
+ "overflow_x": null,
346
+ "overflow_y": null,
347
+ "padding": null,
348
+ "right": null,
349
+ "top": null,
350
+ "visibility": null,
351
+ "width": null
352
+ }
353
+ },
354
+ "beaa41bfdbac47b1a8ffddf18d49f74d": {
355
+ "model_module": "@jupyter-widgets/controls",
356
+ "model_name": "DescriptionStyleModel",
357
+ "model_module_version": "1.5.0",
358
+ "state": {
359
+ "_model_module": "@jupyter-widgets/controls",
360
+ "_model_module_version": "1.5.0",
361
+ "_model_name": "DescriptionStyleModel",
362
+ "_view_count": null,
363
+ "_view_module": "@jupyter-widgets/base",
364
+ "_view_module_version": "1.2.0",
365
+ "_view_name": "StyleView",
366
+ "description_width": ""
367
+ }
368
+ },
369
+ "16be669fa1b34fc5929de11272273bbc": {
370
+ "model_module": "@jupyter-widgets/controls",
371
+ "model_name": "HBoxModel",
372
+ "model_module_version": "1.5.0",
373
+ "state": {
374
+ "_dom_classes": [],
375
+ "_model_module": "@jupyter-widgets/controls",
376
+ "_model_module_version": "1.5.0",
377
+ "_model_name": "HBoxModel",
378
+ "_view_count": null,
379
+ "_view_module": "@jupyter-widgets/controls",
380
+ "_view_module_version": "1.5.0",
381
+ "_view_name": "HBoxView",
382
+ "box_style": "",
383
+ "children": [
384
+ "IPY_MODEL_8e40f72fced7455496a0ac8adfcedc1e",
385
+ "IPY_MODEL_64442da9157d406da770b7752face424",
386
+ "IPY_MODEL_9522c3c9c20f4bddbeb9106e1eff2b04"
387
+ ],
388
+ "layout": "IPY_MODEL_ea1c64cc55ff4948b08c167aad9c02bf"
389
+ }
390
+ },
391
+ "8e40f72fced7455496a0ac8adfcedc1e": {
392
+ "model_module": "@jupyter-widgets/controls",
393
+ "model_name": "HTMLModel",
394
+ "model_module_version": "1.5.0",
395
+ "state": {
396
+ "_dom_classes": [],
397
+ "_model_module": "@jupyter-widgets/controls",
398
+ "_model_module_version": "1.5.0",
399
+ "_model_name": "HTMLModel",
400
+ "_view_count": null,
401
+ "_view_module": "@jupyter-widgets/controls",
402
+ "_view_module_version": "1.5.0",
403
+ "_view_name": "HTMLView",
404
+ "description": "",
405
+ "description_tooltip": null,
406
+ "layout": "IPY_MODEL_5cf321f500ca4848bdb76fe8a21fa12f",
407
+ "placeholder": "​",
408
+ "style": "IPY_MODEL_4ff9d8e7dc5c488189872b74d643d430",
409
+ "value": "Map: 100%"
410
+ }
411
+ },
412
+ "64442da9157d406da770b7752face424": {
413
+ "model_module": "@jupyter-widgets/controls",
414
+ "model_name": "FloatProgressModel",
415
+ "model_module_version": "1.5.0",
416
+ "state": {
417
+ "_dom_classes": [],
418
+ "_model_module": "@jupyter-widgets/controls",
419
+ "_model_module_version": "1.5.0",
420
+ "_model_name": "FloatProgressModel",
421
+ "_view_count": null,
422
+ "_view_module": "@jupyter-widgets/controls",
423
+ "_view_module_version": "1.5.0",
424
+ "_view_name": "ProgressView",
425
+ "bar_style": "success",
426
+ "description": "",
427
+ "description_tooltip": null,
428
+ "layout": "IPY_MODEL_9d97e9d42df4490cb49c423e479a105a",
429
+ "max": 250,
430
+ "min": 0,
431
+ "orientation": "horizontal",
432
+ "style": "IPY_MODEL_36c56cb7232849819c6d032c395acbe4",
433
+ "value": 250
434
+ }
435
+ },
436
+ "9522c3c9c20f4bddbeb9106e1eff2b04": {
437
+ "model_module": "@jupyter-widgets/controls",
438
+ "model_name": "HTMLModel",
439
+ "model_module_version": "1.5.0",
440
+ "state": {
441
+ "_dom_classes": [],
442
+ "_model_module": "@jupyter-widgets/controls",
443
+ "_model_module_version": "1.5.0",
444
+ "_model_name": "HTMLModel",
445
+ "_view_count": null,
446
+ "_view_module": "@jupyter-widgets/controls",
447
+ "_view_module_version": "1.5.0",
448
+ "_view_name": "HTMLView",
449
+ "description": "",
450
+ "description_tooltip": null,
451
+ "layout": "IPY_MODEL_19740a84d8e74481b4c819cb33abd260",
452
+ "placeholder": "​",
453
+ "style": "IPY_MODEL_77a8e0233da34523b09e7b5f5f0efc68",
454
+ "value": " 250/250 [00:00&lt;00:00, 3130.33 examples/s]"
455
+ }
456
+ },
457
+ "ea1c64cc55ff4948b08c167aad9c02bf": {
458
+ "model_module": "@jupyter-widgets/base",
459
+ "model_name": "LayoutModel",
460
+ "model_module_version": "1.2.0",
461
+ "state": {
462
+ "_model_module": "@jupyter-widgets/base",
463
+ "_model_module_version": "1.2.0",
464
+ "_model_name": "LayoutModel",
465
+ "_view_count": null,
466
+ "_view_module": "@jupyter-widgets/base",
467
+ "_view_module_version": "1.2.0",
468
+ "_view_name": "LayoutView",
469
+ "align_content": null,
470
+ "align_items": null,
471
+ "align_self": null,
472
+ "border": null,
473
+ "bottom": null,
474
+ "display": null,
475
+ "flex": null,
476
+ "flex_flow": null,
477
+ "grid_area": null,
478
+ "grid_auto_columns": null,
479
+ "grid_auto_flow": null,
480
+ "grid_auto_rows": null,
481
+ "grid_column": null,
482
+ "grid_gap": null,
483
+ "grid_row": null,
484
+ "grid_template_areas": null,
485
+ "grid_template_columns": null,
486
+ "grid_template_rows": null,
487
+ "height": null,
488
+ "justify_content": null,
489
+ "justify_items": null,
490
+ "left": null,
491
+ "margin": null,
492
+ "max_height": null,
493
+ "max_width": null,
494
+ "min_height": null,
495
+ "min_width": null,
496
+ "object_fit": null,
497
+ "object_position": null,
498
+ "order": null,
499
+ "overflow": null,
500
+ "overflow_x": null,
501
+ "overflow_y": null,
502
+ "padding": null,
503
+ "right": null,
504
+ "top": null,
505
+ "visibility": null,
506
+ "width": null
507
+ }
508
+ },
509
+ "5cf321f500ca4848bdb76fe8a21fa12f": {
510
+ "model_module": "@jupyter-widgets/base",
511
+ "model_name": "LayoutModel",
512
+ "model_module_version": "1.2.0",
513
+ "state": {
514
+ "_model_module": "@jupyter-widgets/base",
515
+ "_model_module_version": "1.2.0",
516
+ "_model_name": "LayoutModel",
517
+ "_view_count": null,
518
+ "_view_module": "@jupyter-widgets/base",
519
+ "_view_module_version": "1.2.0",
520
+ "_view_name": "LayoutView",
521
+ "align_content": null,
522
+ "align_items": null,
523
+ "align_self": null,
524
+ "border": null,
525
+ "bottom": null,
526
+ "display": null,
527
+ "flex": null,
528
+ "flex_flow": null,
529
+ "grid_area": null,
530
+ "grid_auto_columns": null,
531
+ "grid_auto_flow": null,
532
+ "grid_auto_rows": null,
533
+ "grid_column": null,
534
+ "grid_gap": null,
535
+ "grid_row": null,
536
+ "grid_template_areas": null,
537
+ "grid_template_columns": null,
538
+ "grid_template_rows": null,
539
+ "height": null,
540
+ "justify_content": null,
541
+ "justify_items": null,
542
+ "left": null,
543
+ "margin": null,
544
+ "max_height": null,
545
+ "max_width": null,
546
+ "min_height": null,
547
+ "min_width": null,
548
+ "object_fit": null,
549
+ "object_position": null,
550
+ "order": null,
551
+ "overflow": null,
552
+ "overflow_x": null,
553
+ "overflow_y": null,
554
+ "padding": null,
555
+ "right": null,
556
+ "top": null,
557
+ "visibility": null,
558
+ "width": null
559
+ }
560
+ },
561
+ "4ff9d8e7dc5c488189872b74d643d430": {
562
+ "model_module": "@jupyter-widgets/controls",
563
+ "model_name": "DescriptionStyleModel",
564
+ "model_module_version": "1.5.0",
565
+ "state": {
566
+ "_model_module": "@jupyter-widgets/controls",
567
+ "_model_module_version": "1.5.0",
568
+ "_model_name": "DescriptionStyleModel",
569
+ "_view_count": null,
570
+ "_view_module": "@jupyter-widgets/base",
571
+ "_view_module_version": "1.2.0",
572
+ "_view_name": "StyleView",
573
+ "description_width": ""
574
+ }
575
+ },
576
+ "9d97e9d42df4490cb49c423e479a105a": {
577
+ "model_module": "@jupyter-widgets/base",
578
+ "model_name": "LayoutModel",
579
+ "model_module_version": "1.2.0",
580
+ "state": {
581
+ "_model_module": "@jupyter-widgets/base",
582
+ "_model_module_version": "1.2.0",
583
+ "_model_name": "LayoutModel",
584
+ "_view_count": null,
585
+ "_view_module": "@jupyter-widgets/base",
586
+ "_view_module_version": "1.2.0",
587
+ "_view_name": "LayoutView",
588
+ "align_content": null,
589
+ "align_items": null,
590
+ "align_self": null,
591
+ "border": null,
592
+ "bottom": null,
593
+ "display": null,
594
+ "flex": null,
595
+ "flex_flow": null,
596
+ "grid_area": null,
597
+ "grid_auto_columns": null,
598
+ "grid_auto_flow": null,
599
+ "grid_auto_rows": null,
600
+ "grid_column": null,
601
+ "grid_gap": null,
602
+ "grid_row": null,
603
+ "grid_template_areas": null,
604
+ "grid_template_columns": null,
605
+ "grid_template_rows": null,
606
+ "height": null,
607
+ "justify_content": null,
608
+ "justify_items": null,
609
+ "left": null,
610
+ "margin": null,
611
+ "max_height": null,
612
+ "max_width": null,
613
+ "min_height": null,
614
+ "min_width": null,
615
+ "object_fit": null,
616
+ "object_position": null,
617
+ "order": null,
618
+ "overflow": null,
619
+ "overflow_x": null,
620
+ "overflow_y": null,
621
+ "padding": null,
622
+ "right": null,
623
+ "top": null,
624
+ "visibility": null,
625
+ "width": null
626
+ }
627
+ },
628
+ "36c56cb7232849819c6d032c395acbe4": {
629
+ "model_module": "@jupyter-widgets/controls",
630
+ "model_name": "ProgressStyleModel",
631
+ "model_module_version": "1.5.0",
632
+ "state": {
633
+ "_model_module": "@jupyter-widgets/controls",
634
+ "_model_module_version": "1.5.0",
635
+ "_model_name": "ProgressStyleModel",
636
+ "_view_count": null,
637
+ "_view_module": "@jupyter-widgets/base",
638
+ "_view_module_version": "1.2.0",
639
+ "_view_name": "StyleView",
640
+ "bar_color": null,
641
+ "description_width": ""
642
+ }
643
+ },
644
+ "19740a84d8e74481b4c819cb33abd260": {
645
+ "model_module": "@jupyter-widgets/base",
646
+ "model_name": "LayoutModel",
647
+ "model_module_version": "1.2.0",
648
+ "state": {
649
+ "_model_module": "@jupyter-widgets/base",
650
+ "_model_module_version": "1.2.0",
651
+ "_model_name": "LayoutModel",
652
+ "_view_count": null,
653
+ "_view_module": "@jupyter-widgets/base",
654
+ "_view_module_version": "1.2.0",
655
+ "_view_name": "LayoutView",
656
+ "align_content": null,
657
+ "align_items": null,
658
+ "align_self": null,
659
+ "border": null,
660
+ "bottom": null,
661
+ "display": null,
662
+ "flex": null,
663
+ "flex_flow": null,
664
+ "grid_area": null,
665
+ "grid_auto_columns": null,
666
+ "grid_auto_flow": null,
667
+ "grid_auto_rows": null,
668
+ "grid_column": null,
669
+ "grid_gap": null,
670
+ "grid_row": null,
671
+ "grid_template_areas": null,
672
+ "grid_template_columns": null,
673
+ "grid_template_rows": null,
674
+ "height": null,
675
+ "justify_content": null,
676
+ "justify_items": null,
677
+ "left": null,
678
+ "margin": null,
679
+ "max_height": null,
680
+ "max_width": null,
681
+ "min_height": null,
682
+ "min_width": null,
683
+ "object_fit": null,
684
+ "object_position": null,
685
+ "order": null,
686
+ "overflow": null,
687
+ "overflow_x": null,
688
+ "overflow_y": null,
689
+ "padding": null,
690
+ "right": null,
691
+ "top": null,
692
+ "visibility": null,
693
+ "width": null
694
+ }
695
+ },
696
+ "77a8e0233da34523b09e7b5f5f0efc68": {
697
+ "model_module": "@jupyter-widgets/controls",
698
+ "model_name": "DescriptionStyleModel",
699
+ "model_module_version": "1.5.0",
700
+ "state": {
701
+ "_model_module": "@jupyter-widgets/controls",
702
+ "_model_module_version": "1.5.0",
703
+ "_model_name": "DescriptionStyleModel",
704
+ "_view_count": null,
705
+ "_view_module": "@jupyter-widgets/base",
706
+ "_view_module_version": "1.2.0",
707
+ "_view_name": "StyleView",
708
+ "description_width": ""
709
+ }
710
+ },
711
+ "b2f6a30c7e70419c811e0da79bed8224": {
712
+ "model_module": "@jupyter-widgets/controls",
713
+ "model_name": "HBoxModel",
714
+ "model_module_version": "1.5.0",
715
+ "state": {
716
+ "_dom_classes": [],
717
+ "_model_module": "@jupyter-widgets/controls",
718
+ "_model_module_version": "1.5.0",
719
+ "_model_name": "HBoxModel",
720
+ "_view_count": null,
721
+ "_view_module": "@jupyter-widgets/controls",
722
+ "_view_module_version": "1.5.0",
723
+ "_view_name": "HBoxView",
724
+ "box_style": "",
725
+ "children": [
726
+ "IPY_MODEL_aea2b8a51c194dfab14d0870873652b7",
727
+ "IPY_MODEL_a1efc3457ce34a799f9a59c3cb58d55c",
728
+ "IPY_MODEL_8b4f2a3e789e42d2ac402f21c1fc15f5"
729
+ ],
730
+ "layout": "IPY_MODEL_53d3826130da44b3aba936cad515d67b"
731
+ }
732
+ },
733
+ "aea2b8a51c194dfab14d0870873652b7": {
734
+ "model_module": "@jupyter-widgets/controls",
735
+ "model_name": "HTMLModel",
736
+ "model_module_version": "1.5.0",
737
+ "state": {
738
+ "_dom_classes": [],
739
+ "_model_module": "@jupyter-widgets/controls",
740
+ "_model_module_version": "1.5.0",
741
+ "_model_name": "HTMLModel",
742
+ "_view_count": null,
743
+ "_view_module": "@jupyter-widgets/controls",
744
+ "_view_module_version": "1.5.0",
745
+ "_view_name": "HTMLView",
746
+ "description": "",
747
+ "description_tooltip": null,
748
+ "layout": "IPY_MODEL_ac3bf1ae42804678be05203c7d95ddd9",
749
+ "placeholder": "​",
750
+ "style": "IPY_MODEL_73e084e0709a446cbde83880da8fae2e",
751
+ "value": "Loading weights: 100%"
752
+ }
753
+ },
754
+ "a1efc3457ce34a799f9a59c3cb58d55c": {
755
+ "model_module": "@jupyter-widgets/controls",
756
+ "model_name": "FloatProgressModel",
757
+ "model_module_version": "1.5.0",
758
+ "state": {
759
+ "_dom_classes": [],
760
+ "_model_module": "@jupyter-widgets/controls",
761
+ "_model_module_version": "1.5.0",
762
+ "_model_name": "FloatProgressModel",
763
+ "_view_count": null,
764
+ "_view_module": "@jupyter-widgets/controls",
765
+ "_view_module_version": "1.5.0",
766
+ "_view_name": "ProgressView",
767
+ "bar_style": "success",
768
+ "description": "",
769
+ "description_tooltip": null,
770
+ "layout": "IPY_MODEL_2d6124a44ed048198d897c074561cd96",
771
+ "max": 339,
772
+ "min": 0,
773
+ "orientation": "horizontal",
774
+ "style": "IPY_MODEL_b30c7621e14748f7a7e1f6683fac0674",
775
+ "value": 339
776
+ }
777
+ },
778
+ "8b4f2a3e789e42d2ac402f21c1fc15f5": {
779
+ "model_module": "@jupyter-widgets/controls",
780
+ "model_name": "HTMLModel",
781
+ "model_module_version": "1.5.0",
782
+ "state": {
783
+ "_dom_classes": [],
784
+ "_model_module": "@jupyter-widgets/controls",
785
+ "_model_module_version": "1.5.0",
786
+ "_model_name": "HTMLModel",
787
+ "_view_count": null,
788
+ "_view_module": "@jupyter-widgets/controls",
789
+ "_view_module_version": "1.5.0",
790
+ "_view_name": "HTMLView",
791
+ "description": "",
792
+ "description_tooltip": null,
793
+ "layout": "IPY_MODEL_5969532875ce41b6820a5c57b53908d9",
794
+ "placeholder": "​",
795
+ "style": "IPY_MODEL_af8771c98d3a4470a6781f1035e0856e",
796
+ "value": " 339/339 [01:07&lt;00:00, 178.83it/s, Materializing param=model.norm.weight]"
797
+ }
798
+ },
799
+ "53d3826130da44b3aba936cad515d67b": {
800
+ "model_module": "@jupyter-widgets/base",
801
+ "model_name": "LayoutModel",
802
+ "model_module_version": "1.2.0",
803
+ "state": {
804
+ "_model_module": "@jupyter-widgets/base",
805
+ "_model_module_version": "1.2.0",
806
+ "_model_name": "LayoutModel",
807
+ "_view_count": null,
808
+ "_view_module": "@jupyter-widgets/base",
809
+ "_view_module_version": "1.2.0",
810
+ "_view_name": "LayoutView",
811
+ "align_content": null,
812
+ "align_items": null,
813
+ "align_self": null,
814
+ "border": null,
815
+ "bottom": null,
816
+ "display": null,
817
+ "flex": null,
818
+ "flex_flow": null,
819
+ "grid_area": null,
820
+ "grid_auto_columns": null,
821
+ "grid_auto_flow": null,
822
+ "grid_auto_rows": null,
823
+ "grid_column": null,
824
+ "grid_gap": null,
825
+ "grid_row": null,
826
+ "grid_template_areas": null,
827
+ "grid_template_columns": null,
828
+ "grid_template_rows": null,
829
+ "height": null,
830
+ "justify_content": null,
831
+ "justify_items": null,
832
+ "left": null,
833
+ "margin": null,
834
+ "max_height": null,
835
+ "max_width": null,
836
+ "min_height": null,
837
+ "min_width": null,
838
+ "object_fit": null,
839
+ "object_position": null,
840
+ "order": null,
841
+ "overflow": null,
842
+ "overflow_x": null,
843
+ "overflow_y": null,
844
+ "padding": null,
845
+ "right": null,
846
+ "top": null,
847
+ "visibility": null,
848
+ "width": null
849
+ }
850
+ },
851
+ "ac3bf1ae42804678be05203c7d95ddd9": {
852
+ "model_module": "@jupyter-widgets/base",
853
+ "model_name": "LayoutModel",
854
+ "model_module_version": "1.2.0",
855
+ "state": {
856
+ "_model_module": "@jupyter-widgets/base",
857
+ "_model_module_version": "1.2.0",
858
+ "_model_name": "LayoutModel",
859
+ "_view_count": null,
860
+ "_view_module": "@jupyter-widgets/base",
861
+ "_view_module_version": "1.2.0",
862
+ "_view_name": "LayoutView",
863
+ "align_content": null,
864
+ "align_items": null,
865
+ "align_self": null,
866
+ "border": null,
867
+ "bottom": null,
868
+ "display": null,
869
+ "flex": null,
870
+ "flex_flow": null,
871
+ "grid_area": null,
872
+ "grid_auto_columns": null,
873
+ "grid_auto_flow": null,
874
+ "grid_auto_rows": null,
875
+ "grid_column": null,
876
+ "grid_gap": null,
877
+ "grid_row": null,
878
+ "grid_template_areas": null,
879
+ "grid_template_columns": null,
880
+ "grid_template_rows": null,
881
+ "height": null,
882
+ "justify_content": null,
883
+ "justify_items": null,
884
+ "left": null,
885
+ "margin": null,
886
+ "max_height": null,
887
+ "max_width": null,
888
+ "min_height": null,
889
+ "min_width": null,
890
+ "object_fit": null,
891
+ "object_position": null,
892
+ "order": null,
893
+ "overflow": null,
894
+ "overflow_x": null,
895
+ "overflow_y": null,
896
+ "padding": null,
897
+ "right": null,
898
+ "top": null,
899
+ "visibility": null,
900
+ "width": null
901
+ }
902
+ },
903
+ "73e084e0709a446cbde83880da8fae2e": {
904
+ "model_module": "@jupyter-widgets/controls",
905
+ "model_name": "DescriptionStyleModel",
906
+ "model_module_version": "1.5.0",
907
+ "state": {
908
+ "_model_module": "@jupyter-widgets/controls",
909
+ "_model_module_version": "1.5.0",
910
+ "_model_name": "DescriptionStyleModel",
911
+ "_view_count": null,
912
+ "_view_module": "@jupyter-widgets/base",
913
+ "_view_module_version": "1.2.0",
914
+ "_view_name": "StyleView",
915
+ "description_width": ""
916
+ }
917
+ },
918
+ "2d6124a44ed048198d897c074561cd96": {
919
+ "model_module": "@jupyter-widgets/base",
920
+ "model_name": "LayoutModel",
921
+ "model_module_version": "1.2.0",
922
+ "state": {
923
+ "_model_module": "@jupyter-widgets/base",
924
+ "_model_module_version": "1.2.0",
925
+ "_model_name": "LayoutModel",
926
+ "_view_count": null,
927
+ "_view_module": "@jupyter-widgets/base",
928
+ "_view_module_version": "1.2.0",
929
+ "_view_name": "LayoutView",
930
+ "align_content": null,
931
+ "align_items": null,
932
+ "align_self": null,
933
+ "border": null,
934
+ "bottom": null,
935
+ "display": null,
936
+ "flex": null,
937
+ "flex_flow": null,
938
+ "grid_area": null,
939
+ "grid_auto_columns": null,
940
+ "grid_auto_flow": null,
941
+ "grid_auto_rows": null,
942
+ "grid_column": null,
943
+ "grid_gap": null,
944
+ "grid_row": null,
945
+ "grid_template_areas": null,
946
+ "grid_template_columns": null,
947
+ "grid_template_rows": null,
948
+ "height": null,
949
+ "justify_content": null,
950
+ "justify_items": null,
951
+ "left": null,
952
+ "margin": null,
953
+ "max_height": null,
954
+ "max_width": null,
955
+ "min_height": null,
956
+ "min_width": null,
957
+ "object_fit": null,
958
+ "object_position": null,
959
+ "order": null,
960
+ "overflow": null,
961
+ "overflow_x": null,
962
+ "overflow_y": null,
963
+ "padding": null,
964
+ "right": null,
965
+ "top": null,
966
+ "visibility": null,
967
+ "width": null
968
+ }
969
+ },
970
+ "b30c7621e14748f7a7e1f6683fac0674": {
971
+ "model_module": "@jupyter-widgets/controls",
972
+ "model_name": "ProgressStyleModel",
973
+ "model_module_version": "1.5.0",
974
+ "state": {
975
+ "_model_module": "@jupyter-widgets/controls",
976
+ "_model_module_version": "1.5.0",
977
+ "_model_name": "ProgressStyleModel",
978
+ "_view_count": null,
979
+ "_view_module": "@jupyter-widgets/base",
980
+ "_view_module_version": "1.2.0",
981
+ "_view_name": "StyleView",
982
+ "bar_color": null,
983
+ "description_width": ""
984
+ }
985
+ },
986
+ "5969532875ce41b6820a5c57b53908d9": {
987
+ "model_module": "@jupyter-widgets/base",
988
+ "model_name": "LayoutModel",
989
+ "model_module_version": "1.2.0",
990
+ "state": {
991
+ "_model_module": "@jupyter-widgets/base",
992
+ "_model_module_version": "1.2.0",
993
+ "_model_name": "LayoutModel",
994
+ "_view_count": null,
995
+ "_view_module": "@jupyter-widgets/base",
996
+ "_view_module_version": "1.2.0",
997
+ "_view_name": "LayoutView",
998
+ "align_content": null,
999
+ "align_items": null,
1000
+ "align_self": null,
1001
+ "border": null,
1002
+ "bottom": null,
1003
+ "display": null,
1004
+ "flex": null,
1005
+ "flex_flow": null,
1006
+ "grid_area": null,
1007
+ "grid_auto_columns": null,
1008
+ "grid_auto_flow": null,
1009
+ "grid_auto_rows": null,
1010
+ "grid_column": null,
1011
+ "grid_gap": null,
1012
+ "grid_row": null,
1013
+ "grid_template_areas": null,
1014
+ "grid_template_columns": null,
1015
+ "grid_template_rows": null,
1016
+ "height": null,
1017
+ "justify_content": null,
1018
+ "justify_items": null,
1019
+ "left": null,
1020
+ "margin": null,
1021
+ "max_height": null,
1022
+ "max_width": null,
1023
+ "min_height": null,
1024
+ "min_width": null,
1025
+ "object_fit": null,
1026
+ "object_position": null,
1027
+ "order": null,
1028
+ "overflow": null,
1029
+ "overflow_x": null,
1030
+ "overflow_y": null,
1031
+ "padding": null,
1032
+ "right": null,
1033
+ "top": null,
1034
+ "visibility": null,
1035
+ "width": null
1036
+ }
1037
+ },
1038
+ "af8771c98d3a4470a6781f1035e0856e": {
1039
+ "model_module": "@jupyter-widgets/controls",
1040
+ "model_name": "DescriptionStyleModel",
1041
+ "model_module_version": "1.5.0",
1042
+ "state": {
1043
+ "_model_module": "@jupyter-widgets/controls",
1044
+ "_model_module_version": "1.5.0",
1045
+ "_model_name": "DescriptionStyleModel",
1046
+ "_view_count": null,
1047
+ "_view_module": "@jupyter-widgets/base",
1048
+ "_view_module_version": "1.2.0",
1049
+ "_view_name": "StyleView",
1050
+ "description_width": ""
1051
+ }
1052
+ }
1053
+ }
1054
+ },
1055
+ "kaggle": {
1056
+ "accelerator": "gpu",
1057
+ "dataSources": [
1058
+ {
1059
+ "sourceType": "datasetVersion",
1060
+ "sourceId": 15056802,
1061
+ "datasetId": 9638685,
1062
+ "databundleVersionId": 15937545
1063
+ }
1064
+ ],
1065
+ "dockerImageVersionId": 31287,
1066
+ "isInternetEnabled": true,
1067
+ "language": "python",
1068
+ "sourceType": "notebook",
1069
+ "isGpuEnabled": true
1070
+ }
1071
+ },
1072
+ "nbformat_minor": 4,
1073
+ "nbformat": 4,
1074
+ "cells": [
1075
+ {
1076
+ "cell_type": "markdown",
1077
+ "source": "# StockEx Clearing House β€” LLM Fine-Tuning\n\nFine-tunes a Qwen2.5 Instruct model with QLoRA to act as a clearing house trading agent.\n\n**Runs on both Kaggle and Colab.** Auto-selects model size based on available VRAM:\n\n| GPU | VRAM | Model |\n|-----|------|-------|\n| T4 (free) | 15 GB | Qwen2.5-7B-Instruct |\n| A100 40 GB | 40 GB | Qwen2.5-14B-Instruct |\n| A100 80 GB | 80 GB | Qwen2.5-32B-Instruct |\n\n**Resumable training:**\n- **Kaggle**: checkpoints saved/restored from dataset `xabonum/stockex-ch-checkpoints`\n- **Colab**: checkpoints saved/restored from Google Drive\n\n**Output model:** `RayMelius/stockex-ch-trader` on HuggingFace Hub\n\n**Required secret:** Add `HF_TOKEN` in Kaggle Secrets or Colab Secrets (πŸ”‘ icon in left sidebar)",
1078
+ "metadata": {
1079
+ "id": "title"
1080
+ }
1081
+ },
1082
+ {
1083
+ "cell_type": "code",
1084
+ "source": "# ── Install dependencies ───────────────────────────────────────────────────────\n# Reinstall bitsandbytes with proper CUDA support (fixes triton.ops error on Colab)\n!pip install -q -U bitsandbytes\n!pip install -q \\\n \"transformers>=4.46.3\" \\\n \"peft>=0.13.2\" \\\n \"trl>=0.12.1\" \\\n \"datasets>=3.1.0\" \\\n \"accelerate>=1.1.1\" \\\n huggingface_hub\nprint(\"Dependencies installed.\")",
1085
+ "metadata": {
1086
+ "id": "install",
1087
+ "outputId": "a564dd39-8172-4745-e00c-c90fc17c6634",
1088
+ "trusted": true
1089
+ },
1090
+ "outputs": [],
1091
+ "execution_count": null
1092
+ },
1093
+ {
1094
+ "cell_type": "code",
1095
+ "source": "import os, json, random, torch\nfrom datasets import Dataset\nfrom transformers import (\n AutoTokenizer, AutoModelForCausalLM,\n BitsAndBytesConfig, TrainingArguments,\n)\nfrom peft import LoraConfig, get_peft_model, TaskType\nfrom trl import SFTTrainer, SFTConfig\nfrom huggingface_hub import login\n\nprint(f\"CUDA available: {torch.cuda.is_available()}\")\nif torch.cuda.is_available():\n print(f\"GPU: {torch.cuda.get_device_name(0)}\")\n print(f\"VRAM: {torch.cuda.get_device_properties(0).total_memory / 1e9:.1f} GB\")",
1096
+ "metadata": {
1097
+ "id": "imports",
1098
+ "outputId": "162eb31d-3a84-487c-c5b6-4f25c8d69485",
1099
+ "trusted": true
1100
+ },
1101
+ "outputs": [],
1102
+ "execution_count": null
1103
+ },
1104
+ {
1105
+ "cell_type": "code",
1106
+ "source": "# ── Auto-select model based on available VRAM ─────────────────────────────────\nimport torch\n\nassert torch.cuda.is_available(), \"No GPU found β€” change runtime to GPU.\"\nvram_gb = torch.cuda.get_device_properties(0).total_memory / 1e9\ngpu_name = torch.cuda.get_device_name(0)\nprint(f\"GPU: {gpu_name} | VRAM: {vram_gb:.1f} GB\")\n\nif vram_gb >= 70:\n BASE_MODEL = \"Qwen/Qwen2.5-32B-Instruct\"\n BATCH_SIZE, GRAD_ACCUM, LR = 1, 16, 1e-4\nelif vram_gb >= 35:\n BASE_MODEL = \"Qwen/Qwen2.5-14B-Instruct\"\n BATCH_SIZE, GRAD_ACCUM, LR = 2, 8, 1e-4\nelse: # T4 / 15 GB\n BASE_MODEL = \"Qwen/Qwen2.5-7B-Instruct\"\n BATCH_SIZE, GRAD_ACCUM, LR = 4, 4, 2e-4\n\nprint(f\"Selected model: {BASE_MODEL}\")\n\n# ── Fixed config ───────────────────────────────────────────────────────────────\nOUTPUT_REPO = \"RayMelius/stockex-ch-trader\"\nOUTPUT_DIR = \"./stockex-ch-trader\"\nLORA_R = 16\nLORA_ALPHA = 32\nLORA_DROPOUT = 0.05\nNUM_EPOCHS = 3\nMAX_SEQ_LEN = 512\nDATASET_SIZE = 2500\n\n# ── HuggingFace login ─────────────────────────────────────────────────────────\nimport os\n\nHF_TOKEN = None\n\n# Kaggle\ntry:\n from kaggle_secrets import UserSecretsClient\n HF_TOKEN = UserSecretsClient().get_secret(\"HF_TOKEN\")\n print(\"HF_TOKEN loaded from Kaggle Secrets\")\nexcept:\n pass\n\n# Colab\nif not HF_TOKEN:\n try:\n from google.colab import userdata\n HF_TOKEN = userdata.get(\"HF_TOKEN\")\n print(\"HF_TOKEN loaded from Colab Secrets\")\n except:\n pass\n\n# Env fallback\nif not HF_TOKEN:\n HF_TOKEN = os.getenv(\"HF_TOKEN\")\n\nif not HF_TOKEN:\n raise ValueError(\"HF_TOKEN not found.\")\n\nfrom huggingface_hub import login\nlogin(token=HF_TOKEN)",
1107
+ "metadata": {
1108
+ "id": "config",
1109
+ "outputId": "c45e0d98-8928-4459-8449-63d498694d5d",
1110
+ "trusted": true
1111
+ },
1112
+ "outputs": [],
1113
+ "execution_count": null
1114
+ },
1115
+ {
1116
+ "cell_type": "markdown",
1117
+ "source": "## 1. Checkpoint Detection (Resumable Training)\n\nAutomatically detects and resumes from the latest checkpoint:\n- **Kaggle**: Downloads checkpoints from dataset `xabonum/stockex-ch-checkpoints`\n- **Colab**: Restores checkpoints from Google Drive\n\nOnly the **last 3 checkpoints** are kept in persistent storage.",
1118
+ "metadata": {
1119
+ "id": "dataset-header"
1120
+ }
1121
+ },
1122
+ {
1123
+ "cell_type": "code",
1124
+ "source": "import shutil, math, glob\nfrom transformers.trainer_utils import get_last_checkpoint\n\n# ── Detect environment ─────────────────────────────────────────────\nRUNNING_ON_KAGGLE = os.path.exists(\"/kaggle/working\")\nRUNNING_ON_COLAB = os.path.exists(\"/content\") and not RUNNING_ON_KAGGLE\n\nUSE_DRIVE = False\nDRIVE_CKPT_DIR = None\n\nif RUNNING_ON_COLAB:\n try:\n from google.colab import drive\n drive.mount(\"/content/drive\", force_remount=False)\n DRIVE_CKPT_DIR = \"/content/drive/MyDrive/stockex-ch-checkpoints\"\n USE_DRIVE = True\n print(f\"Colab: checkpoints will use {DRIVE_CKPT_DIR}\")\n except Exception as e:\n print(f\"Colab drive mount failed: {e} β€” saving locally only\")\n\nelif RUNNING_ON_KAGGLE:\n DRIVE_CKPT_DIR = \"/kaggle/working/stockex-ch-checkpoints\"\n USE_DRIVE = True\n os.makedirs(DRIVE_CKPT_DIR, exist_ok=True)\n\n # Download checkpoint dataset if available\n try:\n import subprocess\n result = subprocess.run(\n [\"kaggle\", \"datasets\", \"download\", \"-d\", \"xabonum/stockex-ch-checkpoints\",\n \"-p\", DRIVE_CKPT_DIR, \"--unzip\"],\n capture_output=True, text=True, timeout=120\n )\n if result.returncode == 0:\n print(f\"Kaggle: downloaded checkpoint dataset -> {DRIVE_CKPT_DIR}\")\n else:\n print(f\"Kaggle: no existing checkpoint dataset (starting fresh)\")\n print(f\" stderr: {result.stderr.strip()}\")\n except Exception as e:\n print(f\"Kaggle: checkpoint download failed: {e}\")\n\n print(f\"Kaggle: checkpoints will use {DRIVE_CKPT_DIR}\")\n\nelse:\n print(\"Unknown environment β€” saving locally only\")\n\n# ── Free GPU memory to prevent OOM on resume ────────────────────────\ntorch.cuda.empty_cache()\ntorch.cuda.reset_peak_memory_stats()\ntorch.cuda.ipc_collect()\nos.environ[\"PYTORCH_CUDA_ALLOC_CONF\"] = \"max_split_size_mb:128,garbage_collection_threshold:0.6\"\n\n# ── Restore checkpoint from persistent storage to local output dir ──\nos.makedirs(OUTPUT_DIR, exist_ok=True)\nif USE_DRIVE:\n os.makedirs(DRIVE_CKPT_DIR, exist_ok=True)\n drive_ckpt = get_last_checkpoint(DRIVE_CKPT_DIR)\n if drive_ckpt:\n local_ckpt_name = os.path.basename(drive_ckpt)\n local_ckpt_path = os.path.join(OUTPUT_DIR, local_ckpt_name)\n if not os.path.exists(local_ckpt_path):\n print(f\"Restoring checkpoint: {drive_ckpt} -> {local_ckpt_path}\")\n shutil.copytree(drive_ckpt, local_ckpt_path)\n\n# ── Detect latest local checkpoint ────────────────────────────────\nRESUME_FROM = get_last_checkpoint(OUTPUT_DIR)\nSAVE_STEPS = 10\n\n# ── Resume summary ─────────────────────────────────────────────────\ntrain_size = int(DATASET_SIZE * 0.9)\nsteps_per_epoch = math.ceil(train_size / (BATCH_SIZE * GRAD_ACCUM))\ntotal_steps = steps_per_epoch * NUM_EPOCHS\n\nif RESUME_FROM:\n completed_steps = int(os.path.basename(RESUME_FROM).split(\"-\")[-1])\n remaining = max(0, total_steps - completed_steps)\n pct_done = 100 * completed_steps / total_steps\n epoch_done = completed_steps / steps_per_epoch\n\n print(\"\\n\" + \"=\" * 55)\n print(\" RESUMING FROM CHECKPOINT\")\n print(\"=\" * 55)\n print(f\" Checkpoint : {os.path.basename(RESUME_FROM)}\")\n print(f\" Steps done : {completed_steps:,} / {total_steps:,} ({pct_done:.1f}%)\")\n print(f\" Steps left : {remaining:,}\")\n print(f\" Epoch : {epoch_done:.2f} / {NUM_EPOCHS}\")\n print(f\" Epochs left : {NUM_EPOCHS - epoch_done:.2f}\")\n print(f\" Steps/epoch : {steps_per_epoch:,}\")\n print(f\" Save every : {SAVE_STEPS} steps\")\n print(\"=\" * 55 + \"\\n\")\nelse:\n print(\"\\n\" + \"=\" * 55)\n print(\" STARTING FRESH\")\n print(\"=\" * 55)\n print(f\" Total steps : {total_steps:,}\")\n print(f\" Steps/epoch : {steps_per_epoch:,}\")\n print(f\" Epochs : {NUM_EPOCHS}\")\n print(f\" Save every : {SAVE_STEPS} steps\")\n print(\"=\" * 55 + \"\\n\")",
1125
+ "metadata": {
1126
+ "id": "egq0dp9csuo",
1127
+ "outputId": "f098a839-130c-4011-d0ff-a10289004957",
1128
+ "trusted": true
1129
+ },
1130
+ "outputs": [],
1131
+ "execution_count": null
1132
+ },
1133
+ {
1134
+ "cell_type": "markdown",
1135
+ "source": "## 2. Synthetic Dataset Generation\n\nEach training example is a realistic clearing house trading scenario:\n- Member state: capital, holdings, obligation remaining\n- Market: BBO for each security\n- Target: a valid JSON trading decision that respects all constraints",
1136
+ "metadata": {
1137
+ "trusted": true
1138
+ },
1139
+ "outputs": [],
1140
+ "execution_count": null
1141
+ },
1142
+ {
1143
+ "cell_type": "code",
1144
+ "source": "# Securities from shared_data/securities.txt (symbol, start_price, current_price)\nSECURITIES = [\n {\"symbol\": \"ALPHA\", \"base\": 5.65},\n {\"symbol\": \"PEIR\", \"base\": 8.35},\n {\"symbol\": \"EXAE\", \"base\": 6.90},\n {\"symbol\": \"QUEST\", \"base\": 13.35},\n {\"symbol\": \"NBG\", \"base\": 8.00},\n {\"symbol\": \"EUROB\", \"base\": 3.45},\n {\"symbol\": \"AEG\", \"base\": 4.75},\n {\"symbol\": \"INTKA\", \"base\": 7.35},\n {\"symbol\": \"AAAK\", \"base\": 2.75},\n {\"symbol\": \"ATTIK\", \"base\": 4.90},\n]\n\nSTARTING_CAPITAL = 100_000.0\nDAILY_OBLIGATION = 10\n\n\ndef gen_bbo(base_price: float) -> dict:\n \"\"\"Generate a realistic bid/ask spread around a base price.\"\"\"\n drift = random.uniform(-0.05, 0.05)\n mid = round(base_price * (1 + drift), 2)\n spread = round(random.choice([0.05, 0.10, 0.15]), 2)\n best_bid = round(mid - spread / 2, 2)\n best_ask = round(mid + spread / 2, 2)\n return {\"best_bid\": best_bid, \"best_ask\": best_ask, \"mid\": mid}\n\n\ndef gen_holdings(bbos: dict) -> list:\n \"\"\"Randomly generate some holdings for a member.\"\"\"\n holdings = []\n n = random.randint(0, 4)\n for sym in random.sample(list(bbos.keys()), min(n, len(bbos))):\n qty = random.randint(50, 500)\n mid = bbos[sym][\"mid\"]\n avg_cost = round(mid * random.uniform(0.92, 1.08), 2)\n holdings.append({\"symbol\": sym, \"quantity\": qty, \"avg_cost\": avg_cost})\n return holdings\n\n\ndef build_prompt(member_id: str, capital: float, holdings: list,\n obligation_remaining: int, bbos: dict) -> str:\n market_lines = [\n f\" {sym}: Bid {bbo['best_bid']:.2f} / Ask {bbo['best_ask']:.2f}\"\n for sym, bbo in sorted(bbos.items())\n ]\n holding_lines = (\n [f\" {h['symbol']}: {h['quantity']} shares @ avg cost {h['avg_cost']:.2f}\"\n for h in holdings]\n if holdings else [\" None\"]\n )\n return (\n f\"You are simulating clearing house member {member_id} making ONE trading decision.\\n\\n\"\n f\"Member state:\\n\"\n f\" Available capital: EUR {capital:,.2f}\\n\"\n f\" Securities obligation remaining today: {obligation_remaining} more to trade\\n\"\n f\" Current holdings:\\n\" + \"\\n\".join(holding_lines) + \"\\n\\n\"\n f\"Current market (Bid/Ask):\\n\" + \"\\n\".join(market_lines) + \"\\n\\n\"\n f\"Rules:\\n\"\n f\"- Do not spend more than your available capital\\n\"\n f\"- Do not sell more shares than you hold\\n\"\n f\"- If you have no holdings, you must BUY\\n\"\n f\"- Choose a realistic price close to the BBO mid-price\\n\"\n f\"- Quantity should be between 10 and 200\\n\\n\"\n f\"Respond ONLY with valid JSON, no other text:\\n\"\n f'Example: {{\"symbol\": \"ALPHA\", \"side\": \"BUY\", \"quantity\": 50, \"price\": 5.65}}'\n )\n\n\ndef gen_decision(capital: float, holdings: list, bbos: dict) -> dict:\n \"\"\"Generate a rule-valid trading decision for the given state.\"\"\"\n holdings_value = sum(\n h[\"quantity\"] * bbos.get(h[\"symbol\"], {}).get(\"mid\", h[\"avg_cost\"])\n for h in holdings\n )\n net_worth = capital + holdings_value\n holdings_ratio = holdings_value / net_worth if net_worth > 0 else 0\n\n if not holdings:\n side = \"BUY\"\n elif holdings_ratio > 0.6:\n side = random.choices([\"SELL\", \"BUY\"], weights=[0.7, 0.3])[0]\n else:\n side = random.choices([\"BUY\", \"SELL\"], weights=[0.55, 0.45])[0]\n\n if side == \"BUY\":\n affordable = [sym for sym, bbo in bbos.items() if 10 * bbo[\"best_ask\"] <= capital]\n if not affordable:\n sym = min(bbos, key=lambda s: bbos[s][\"best_ask\"])\n else:\n held_syms = [h[\"symbol\"] for h in holdings]\n weights = [3 if s in held_syms else 1 for s in affordable]\n sym = random.choices(affordable, weights=weights)[0]\n ask = bbos[sym][\"best_ask\"]\n max_qty = min(200, int(capital / ask))\n qty = random.randint(10, max(10, max_qty))\n price = round(bbos[sym][\"mid\"] + random.uniform(-0.05, 0.05), 2)\n price = max(bbos[sym][\"best_bid\"], min(price, ask))\n return {\"symbol\": sym, \"side\": \"BUY\", \"quantity\": qty, \"price\": round(price, 2)}\n else:\n h = random.choice(holdings)\n sym = h[\"symbol\"]\n bbo = bbos[sym]\n qty = random.randint(10, min(200, h[\"quantity\"]))\n price = round(bbo[\"mid\"] + random.uniform(-0.05, 0.05), 2)\n price = max(bbo[\"best_bid\"] - 0.05, min(price, bbo[\"best_ask\"]))\n return {\"symbol\": sym, \"side\": \"SELL\", \"quantity\": qty, \"price\": round(price, 2)}\n\n\ndef generate_dataset(n: int) -> list:\n examples = []\n member_ids = [f\"USR{i:02d}\" for i in range(1, 11)]\n scenarios = [\n ((80_000, 100_000), (5, 10), \"fresh_member\"),\n ((50_000, 80_000), (0, 5), \"active_member\"),\n ((20_000, 50_000), (0, 2), \"low_capital\"),\n ((5_000, 20_000), (0, 10), \"very_low_capital\"),\n ((90_000, 100_000), (10, 10),\"start_of_day\"),\n ]\n for _ in range(n):\n cap_range, obl_range, _ = random.choice(scenarios)\n capital = round(random.uniform(*cap_range), 2)\n obligation = random.randint(*obl_range)\n member_id = random.choice(member_ids)\n bbos = {s[\"symbol\"]: gen_bbo(s[\"base\"]) for s in SECURITIES}\n holdings = gen_holdings(bbos)\n holdings_cost = sum(h[\"quantity\"] * h[\"avg_cost\"] for h in holdings)\n if holdings_cost > STARTING_CAPITAL - capital:\n scale = (STARTING_CAPITAL - capital) / max(holdings_cost, 1)\n for h in holdings:\n h[\"quantity\"] = max(10, int(h[\"quantity\"] * scale))\n prompt = build_prompt(member_id, capital, holdings, obligation, bbos)\n decision = gen_decision(capital, holdings, bbos)\n examples.append({\"prompt\": prompt, \"completion\": json.dumps(decision)})\n return examples\n\n\nprint(f\"Generating {DATASET_SIZE} training examples...\")\nraw_data = generate_dataset(DATASET_SIZE)\nprint(f\"Done. Example:\")\nprint(\"PROMPT:\\n\", raw_data[0][\"prompt\"])\nprint(\"\\nCOMPLETION:\", raw_data[0][\"completion\"])",
1145
+ "metadata": {
1146
+ "id": "dataset-gen",
1147
+ "outputId": "6b6ae157-9c91-42d5-a556-049f122bf7f1",
1148
+ "trusted": true
1149
+ },
1150
+ "outputs": [],
1151
+ "execution_count": null
1152
+ },
1153
+ {
1154
+ "cell_type": "code",
1155
+ "source": "# Train/val split (90/10)\nrandom.shuffle(raw_data)\nsplit = int(len(raw_data) * 0.9)\ntrain_data = raw_data[:split]\nval_data = raw_data[split:]\n\ntrain_dataset = Dataset.from_list(train_data)\nval_dataset = Dataset.from_list(val_data)\nprint(f\"Train: {len(train_dataset)} | Val: {len(val_dataset)}\")",
1156
+ "metadata": {
1157
+ "id": "dataset-split",
1158
+ "outputId": "3246d564-c102-4395-8fbc-c3f7979130fb",
1159
+ "trusted": true
1160
+ },
1161
+ "outputs": [],
1162
+ "execution_count": null
1163
+ },
1164
+ {
1165
+ "cell_type": "markdown",
1166
+ "source": "## 3. Load Base Model (4-bit QLoRA)",
1167
+ "metadata": {
1168
+ "id": "model-header"
1169
+ }
1170
+ },
1171
+ {
1172
+ "cell_type": "code",
1173
+ "source": "print(f\"Loading tokenizer: {BASE_MODEL}\")\ntokenizer = AutoTokenizer.from_pretrained(BASE_MODEL, trust_remote_code=True)\ntokenizer.pad_token = tokenizer.eos_token\ntokenizer.padding_side = \"right\"\ntokenizer.model_max_length = MAX_SEQ_LEN # replaces max_seq_length in SFTConfig\nprint(\"Tokenizer loaded\")",
1174
+ "metadata": {
1175
+ "id": "load-tokenizer",
1176
+ "outputId": "a5008937-66ed-4028-a215-62e458cfc8dd",
1177
+ "trusted": true
1178
+ },
1179
+ "outputs": [],
1180
+ "execution_count": null
1181
+ },
1182
+ {
1183
+ "cell_type": "code",
1184
+ "source": "SYSTEM_PROMPT = (\n \"You are a StockEx clearing house trading agent. \"\n \"Given a member's financial state and live market data, \"\n \"you output a single valid JSON trading decision that respects all capital and holdings constraints. \"\n \"Never output anything other than the JSON object.\"\n)\n\n\ndef format_chat(example):\n \"\"\"Apply the model's chat template to produce a training string.\"\"\"\n messages = [\n {\"role\": \"system\", \"content\": SYSTEM_PROMPT},\n {\"role\": \"user\", \"content\": example[\"prompt\"]},\n {\"role\": \"assistant\", \"content\": example[\"completion\"]},\n ]\n text = tokenizer.apply_chat_template(\n messages,\n tokenize=False,\n add_generation_prompt=False,\n )\n return {\"text\": text}\n\n\ntrain_dataset = train_dataset.map(format_chat)\nval_dataset = val_dataset.map(format_chat)\n\nprint(\"Sample formatted text:\")\nprint(train_dataset[0][\"text\"][:600], \"...\")",
1185
+ "metadata": {
1186
+ "id": "format-dataset",
1187
+ "outputId": "ea4f1fa8-bf2b-4360-c426-28e5255dbbd3",
1188
+ "trusted": true
1189
+ },
1190
+ "outputs": [],
1191
+ "execution_count": null
1192
+ },
1193
+ {
1194
+ "cell_type": "code",
1195
+ "source": "# 4-bit quantization config\nbnb_config = BitsAndBytesConfig(\n load_in_4bit=True,\n bnb_4bit_quant_type=\"nf4\",\n bnb_4bit_compute_dtype=torch.bfloat16,\n bnb_4bit_use_double_quant=True,\n)\n\nprint(f\"Loading model: {BASE_MODEL} (4-bit)\")\nmodel = AutoModelForCausalLM.from_pretrained(\n BASE_MODEL,\n quantization_config=bnb_config,\n device_map=\"auto\",\n trust_remote_code=True,\n dtype=torch.bfloat16,\n)\nmodel.config.use_cache = False\nmodel.config.pretraining_tp = 1\nprint(f\"Model loaded. Parameters: {model.num_parameters()/1e9:.2f}B\")\n\nfrom peft import prepare_model_for_kbit_training\n\nmodel = prepare_model_for_kbit_training(model)",
1196
+ "metadata": {
1197
+ "id": "load-model",
1198
+ "outputId": "7f46083d-c830-45f3-e887-fbe9cf30b4b2",
1199
+ "trusted": true
1200
+ },
1201
+ "outputs": [],
1202
+ "execution_count": null
1203
+ },
1204
+ {
1205
+ "cell_type": "markdown",
1206
+ "source": "## 3b. LoRA Configuration",
1207
+ "metadata": {
1208
+ "id": "lora-header"
1209
+ }
1210
+ },
1211
+ {
1212
+ "cell_type": "code",
1213
+ "source": "lora_config = LoraConfig(\n r=LORA_R,\n lora_alpha=LORA_ALPHA,\n target_modules=[\n \"q_proj\", \"k_proj\", \"v_proj\", \"o_proj\",\n \"gate_proj\", \"up_proj\", \"down_proj\",\n ],\n lora_dropout=LORA_DROPOUT,\n bias=\"none\",\n task_type=TaskType.CAUSAL_LM,\n)\n\ntrainable = sum(p.numel() for p in model.parameters() if p.requires_grad)\ntotal = sum(p.numel() for p in model.parameters())\nprint(f\"Trainable parameters: {trainable/1e6:.1f}M / {total/1e6:.0f}M ({100*trainable/total:.2f}%)\")",
1214
+ "metadata": {
1215
+ "id": "lora-config",
1216
+ "outputId": "8a2688ba-288b-4149-ff37-c8d0ee11f77f",
1217
+ "trusted": true
1218
+ },
1219
+ "outputs": [],
1220
+ "execution_count": null
1221
+ },
1222
+ {
1223
+ "cell_type": "markdown",
1224
+ "source": "## 4. Train",
1225
+ "metadata": {
1226
+ "id": "training-header"
1227
+ }
1228
+ },
1229
+ {
1230
+ "cell_type": "code",
1231
+ "source": "from transformers import TrainerCallback\nimport shutil, glob\n\nKAGGLE_DATASET_ID = \"xabonum/stockex-ch-checkpoints\"\nMAX_CHECKPOINTS_PERSISTENT = 3 # keep only last 3 in Kaggle dataset / Drive\n\n# ── Create Kaggle dataset metadata if needed ────────────────────────\nif USE_DRIVE and DRIVE_CKPT_DIR:\n metadata_path = os.path.join(DRIVE_CKPT_DIR, \"dataset-metadata.json\")\n if not os.path.exists(metadata_path):\n metadata = {\n \"title\": \"stockex-ch-checkpoints\",\n \"id\": KAGGLE_DATASET_ID,\n \"licenses\": [{\"name\": \"CC0-1.0\"}]\n }\n with open(metadata_path, \"w\") as f:\n json.dump(metadata, f, indent=2)\n print(\"Created dataset-metadata.json\")\n\n\ndef cleanup_old_checkpoints(ckpt_dir, keep=MAX_CHECKPOINTS_PERSISTENT):\n \"\"\"Keep only the last `keep` checkpoints in persistent storage.\"\"\"\n ckpts = sorted(\n glob.glob(os.path.join(ckpt_dir, \"checkpoint-*\")),\n key=lambda p: int(os.path.basename(p).split(\"-\")[-1])\n )\n while len(ckpts) > keep:\n old = ckpts.pop(0)\n shutil.rmtree(old)\n print(f\"[Checkpoint] Removed old: {os.path.basename(old)}\")\n\n\nclass CheckpointSyncCallback(TrainerCallback):\n \"\"\"Copy checkpoint to persistent storage, push LoRA to HF Hub, upload to Kaggle dataset.\"\"\"\n\n def on_save(self, args, state, control, **kwargs):\n ckpt_dir = os.path.join(args.output_dir, f\"checkpoint-{state.global_step}\")\n if not os.path.isdir(ckpt_dir):\n return\n\n # 1. Save to persistent folder (Drive / Kaggle working dir)\n if USE_DRIVE and DRIVE_CKPT_DIR:\n dest = os.path.join(DRIVE_CKPT_DIR, f\"checkpoint-{state.global_step}\")\n os.makedirs(DRIVE_CKPT_DIR, exist_ok=True)\n try:\n shutil.copytree(ckpt_dir, dest, dirs_exist_ok=True)\n print(f\"[Checkpoint] Saved -> {dest}\")\n except Exception as e:\n print(f\"[Checkpoint] Copy failed: {e}\")\n\n # Cleanup: keep only last 3 checkpoints in persistent storage\n try:\n cleanup_old_checkpoints(DRIVE_CKPT_DIR)\n except Exception as e:\n print(f\"[Checkpoint] Cleanup failed: {e}\")\n\n # 2. Push LoRA adapter to HF Hub\n try:\n kwargs[\"model\"].push_to_hub(\n OUTPUT_REPO,\n commit_message=f\"Checkpoint step {state.global_step} (epoch {state.epoch:.2f})\",\n token=HF_TOKEN,\n )\n print(f\"[Checkpoint] Pushed step {state.global_step} -> HF Hub\")\n except Exception as e:\n print(f\"[Checkpoint] HF push failed: {e}\")\n\n # 3. Upload to Kaggle dataset (Kaggle only)\n if RUNNING_ON_KAGGLE and USE_DRIVE:\n try:\n os.system(\n f\"kaggle datasets version -p {DRIVE_CKPT_DIR} \"\n f\"-m 'Checkpoint step {state.global_step}' --dir-mode zip\"\n )\n print(f\"[Checkpoint] Kaggle dataset updated for step {state.global_step}\")\n except Exception as e:\n print(f\"[Checkpoint] Kaggle dataset update failed: {e}\")\n\n\nsft_config = SFTConfig(\n output_dir=OUTPUT_DIR,\n num_train_epochs=NUM_EPOCHS,\n per_device_train_batch_size=BATCH_SIZE,\n per_device_eval_batch_size=BATCH_SIZE,\n gradient_accumulation_steps=GRAD_ACCUM,\n gradient_checkpointing=True,\n optim=\"paged_adamw_8bit\",\n learning_rate=LR,\n lr_scheduler_type=\"cosine\",\n warmup_ratio=0.05,\n fp16=not torch.cuda.is_bf16_supported(),\n bf16=torch.cuda.is_bf16_supported(),\n logging_steps=10,\n eval_strategy=\"steps\",\n eval_steps=SAVE_STEPS,\n save_strategy=\"steps\",\n save_steps=SAVE_STEPS,\n save_total_limit=3,\n load_best_model_at_end=True,\n metric_for_best_model=\"eval_loss\",\n greater_is_better=False,\n report_to=\"none\",\n dataset_text_field=\"text\",\n packing=False,\n)\n\ntrainer = SFTTrainer(\n model=model,\n args=sft_config,\n train_dataset=train_dataset,\n eval_dataset=val_dataset,\n peft_config=lora_config,\n processing_class=tokenizer,\n callbacks=[CheckpointSyncCallback()],\n)\n\nif RESUME_FROM:\n print(f\"Resuming from checkpoint: {RESUME_FROM}\")\nelse:\n print(f\"Starting training from scratch. Save every {SAVE_STEPS} steps.\")\n\ntrainer.train(resume_from_checkpoint=RESUME_FROM)\nprint(\"Training complete.\")",
1232
+ "metadata": {
1233
+ "trusted": true
1234
+ },
1235
+ "outputs": [],
1236
+ "execution_count": null
1237
+ },
1238
+ {
1239
+ "cell_type": "markdown",
1240
+ "source": "## 5. Save & Push to HuggingFace Hub\n\nMerges LoRA adapters into the base model weights and pushes the full model.",
1241
+ "metadata": {
1242
+ "id": "save-header"
1243
+ }
1244
+ },
1245
+ {
1246
+ "cell_type": "code",
1247
+ "source": "from peft import PeftModel\n\n# Save best adapter checkpoint locally\ntrainer.model.save_pretrained(OUTPUT_DIR)\ntokenizer.save_pretrained(OUTPUT_DIR)\nprint(f\"Adapter saved to {OUTPUT_DIR}\")\n\n# Reload base model in fp16 for merging (can't merge with 4-bit)\nprint(\"Reloading base model in fp16 for adapter merge...\")\ndel model\ntorch.cuda.empty_cache()\n\nbase_model = AutoModelForCausalLM.from_pretrained(\n BASE_MODEL,\n torch_dtype=torch.float16,\n device_map=\"auto\",\n trust_remote_code=True,\n)\nmerged_model = PeftModel.from_pretrained(base_model, OUTPUT_DIR)\nmerged_model = merged_model.merge_and_unload()\nprint(\"Adapters merged.\")",
1248
+ "metadata": {
1249
+ "id": "save-model",
1250
+ "trusted": true
1251
+ },
1252
+ "outputs": [],
1253
+ "execution_count": null
1254
+ },
1255
+ {
1256
+ "cell_type": "code",
1257
+ "source": "print(f\"Pushing merged model to: {OUTPUT_REPO}\")\nmerged_model.push_to_hub(\n OUTPUT_REPO,\n token=HF_TOKEN,\n commit_message=\"StockEx CH Trader: QLoRA fine-tuned Qwen2.5-32B-Instruct\",\n)\ntokenizer.push_to_hub(\n OUTPUT_REPO,\n token=HF_TOKEN,\n commit_message=\"Tokenizer for StockEx CH Trader (Qwen2.5-32B-Instruct base)\",\n)\nprint(f\"βœ“ Model pushed to https://huggingface.co/{OUTPUT_REPO}\")",
1258
+ "metadata": {
1259
+ "id": "push-hub",
1260
+ "trusted": true
1261
+ },
1262
+ "outputs": [],
1263
+ "execution_count": null
1264
+ },
1265
+ {
1266
+ "cell_type": "markdown",
1267
+ "source": "## 6. Inference Test\n\nVerify the model generates valid JSON trading decisions.",
1268
+ "metadata": {
1269
+ "id": "test-header"
1270
+ }
1271
+ },
1272
+ {
1273
+ "cell_type": "code",
1274
+ "source": "import re\nfrom transformers import pipeline\n\npipe = pipeline(\n \"text-generation\",\n model=merged_model,\n tokenizer=tokenizer,\n device_map=\"auto\",\n)\n\ntest_cases = [\n {\n \"desc\": \"New member, no holdings, must trade\",\n \"capital\": 100_000.0,\n \"holdings\": [],\n \"obligation\": 10,\n },\n {\n \"desc\": \"Experienced member with holdings, low obligation\",\n \"capital\": 65_000.0,\n \"holdings\": [\n {\"symbol\": \"ALPHA\", \"quantity\": 300, \"avg_cost\": 5.60},\n {\"symbol\": \"QUEST\", \"quantity\": 150, \"avg_cost\": 13.20},\n ],\n \"obligation\": 2,\n },\n {\n \"desc\": \"Low capital, large holdings\",\n \"capital\": 8_000.0,\n \"holdings\": [\n {\"symbol\": \"PEIR\", \"quantity\": 500, \"avg_cost\": 8.30},\n {\"symbol\": \"NBG\", \"quantity\": 200, \"avg_cost\": 7.95},\n ],\n \"obligation\": 5,\n },\n]\n\ntest_bbos = {s[\"symbol\"]: gen_bbo(s[\"base\"]) for s in SECURITIES}\n\nprint(\"=\" * 70)\nfor tc in test_cases:\n print(f\"\\nSCENARIO: {tc['desc']}\")\n prompt = build_prompt(\"USR01\", tc[\"capital\"], tc[\"holdings\"], tc[\"obligation\"], test_bbos)\n messages = [\n {\"role\": \"system\", \"content\": SYSTEM_PROMPT},\n {\"role\": \"user\", \"content\": prompt},\n ]\n output = pipe(\n messages,\n max_new_tokens=60,\n temperature=0.3,\n do_sample=True,\n pad_token_id=tokenizer.eos_token_id,\n )\n response = output[0][\"generated_text\"][-1][\"content\"].strip()\n print(f\"RESPONSE: {response}\")\n try:\n m = re.search(r\"\\{[^}]+\\}\", response)\n if m:\n d = json.loads(m.group())\n assert d[\"side\"] in (\"BUY\", \"SELL\")\n assert d[\"symbol\"] in [s[\"symbol\"] for s in SECURITIES]\n assert d[\"quantity\"] > 0\n assert d[\"price\"] > 0\n print(f\"βœ“ Valid JSON: {d}\")\n else:\n print(\"βœ— No JSON found in response\")\n except Exception as e:\n print(f\"βœ— Invalid: {e}\")\n print(\"-\" * 70)",
1275
+ "metadata": {
1276
+ "id": "inference-test",
1277
+ "trusted": true
1278
+ },
1279
+ "outputs": [],
1280
+ "execution_count": null
1281
+ },
1282
+ {
1283
+ "cell_type": "markdown",
1284
+ "source": "## 7. Activate in StockEx\n\nThe clearing house already uses `RayMelius/stockex-ch-trader` as default.\n\nTo switch to this model in a running StockEx instance:\n\n**HuggingFace Spaces** β€” add to secrets:\n```\nHF_MODEL = RayMelius/stockex-ch-trader\nHF_TOKEN = <your token>\n```\n\n**Docker Compose** β€” already set in `docker-compose.yml`:\n```yaml\nenvironment:\n - HF_MODEL=RayMelius/stockex-ch-trader\n - HF_TOKEN=<your token>\n```",
1285
+ "metadata": {
1286
+ "id": "usage-header"
1287
+ }
1288
+ }
1289
+ ]
1290
+ }