{ "cells": [ { "cell_type": "markdown", "id": "intro001", "metadata": {}, "source": [ "# KERN-Lab-0.5-624M in Google Colab\n", "\n", "Run the cells in order. A GPU runtime is optional (Runtime → Change runtime type → GPU). The model completes text prefixes. It is an experimental adaptation of Qwen3-0.6B-Base.\n", "\n", "The notebook downloads approximately 1.3 GB. Review the custom model Python file in the downloaded snapshot before running the loading cell. It reconstructs the sparse layer through Transformers AutoClass." ] }, { "cell_type": "code", "id": "install01", "metadata": {}, "execution_count": null, "outputs": [], "source": [ "%pip -q install \"torch>=2.7.0\" \"transformers==5.23.0\" \"huggingface_hub>=1.0\" \"safetensors>=0.4.0\" \"tokenizers>=0.20.0\"" ] }, { "cell_type": "code", "id": "download1", "metadata": {}, "execution_count": null, "outputs": [], "source": [ "from pathlib import Path\n", "from hashlib import file_digest\n", "from huggingface_hub import snapshot_download\n", "\n", "MODEL_REPO = \"Project-KERN/KERN-Lab-0.5-624M\" # @param {type: \"string\"}\n", "REVISION = \"\" # @param {type: \"string\"}\n", "LOCAL_MODEL_DIR = \"\" # @param {type: \"string\"}\n", "\n", "required = [\n", " \"config.json\", \"generation_config.json\", \"model.safetensors\",\n", " \"modeling_kern_qwen06b_v1.py\", \"tokenizer.json\", \"tokenizer_config.json\",\n", " \"merges.txt\", \"vocab.json\",\n", "]\n", "if LOCAL_MODEL_DIR.strip():\n", " model_dir = Path(LOCAL_MODEL_DIR).expanduser().resolve()\n", "else:\n", " repo_id = MODEL_REPO.strip()\n", " if not repo_id or repo_id.startswith(\"__\"):\n", " repo_id = input(\"Enter the official Hugging Face repository (owner/KERN-Lab-0.5-624M): \").strip()\n", " model_dir = Path(snapshot_download(\n", " repo_id=repo_id, revision=REVISION.strip() or None, allow_patterns=required,\n", " ))\n", "with (model_dir / \"model.safetensors\").open(\"rb\") as stream:\n", " actual_sha256 = file_digest(stream, \"sha256\").hexdigest()\n", "expected_sha256 = \"cc1ac73225ccc6013b1e73d91a6299078f7acd1b67940bf5277328dac43b0fd2\"\n", "if actual_sha256 != expected_sha256:\n", " raise ValueError(f\"Unexpected SafeTensors SHA-256: {actual_sha256}\")\n", "print(\"Approved weights verified.\")" ] }, { "cell_type": "code", "id": "load001", "metadata": {}, "execution_count": null, "outputs": [], "source": [ "import torch\n", "from transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer\n", "\n", "device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n", "dtype = torch.bfloat16 if device == \"cuda\" and torch.cuda.is_bf16_supported() else torch.float32\n", "config = AutoConfig.from_pretrained(model_dir, trust_remote_code=True, local_files_only=True)\n", "tokenizer = AutoTokenizer.from_pretrained(model_dir, config=config, local_files_only=True)\n", "model = AutoModelForCausalLM.from_pretrained(\n", " model_dir, config=config, trust_remote_code=True, local_files_only=True, dtype=dtype,\n", ").to(device).eval()\n", "print(f\"Loaded {type(model).__name__} on {device} ({dtype}).\")" ] }, { "cell_type": "code", "id": "gen0001", "metadata": {}, "execution_count": null, "outputs": [], "source": [ "prompt = \"The capital of France is\" # @param {type: \"string\"}\n", "new_tokens = 16 # @param {type: \"integer\"}\n", "\n", "inputs = tokenizer(prompt, return_tensors=\"pt\", add_special_tokens=False).to(device)\n", "with torch.inference_mode():\n", " output = model.generate(\n", " **inputs, max_new_tokens=int(new_tokens), do_sample=False, use_cache=True,\n", " pad_token_id=tokenizer.pad_token_id,\n", " )\n", "print(prompt + tokenizer.decode(output[0, inputs[\"input_ids\"].shape[-1]:], skip_special_tokens=True))" ] } ], "metadata": { "colab": { "name": "KERN_Lab_0.5_624M.ipynb", "provenance": [] }, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "name": "python" }, "accelerator": "GPU" }, "nbformat": 4, "nbformat_minor": 5 }