omurberaisik commited on
Commit
0cb061b
·
verified ·
1 Parent(s): ce4b4f4

Update notebook.ipynb

Browse files
Files changed (1) hide show
  1. notebook.ipynb +174 -0
notebook.ipynb ADDED
@@ -0,0 +1,174 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "metadata": {},
6
+ "source": [
7
+ "# NoTokenLM-MicroGen \u2014 Use Notebook\n",
8
+ "\n",
9
+ "This is the notebook to use if you just want to **run one of the MicroGen models**. Every model in the series ships as a real `transformers`-compatible checkpoint, so loading one is a single `from_pretrained(..., trust_remote_code=True)` call \u2014 no manual architecture code, no private repos.\n",
10
+ "\n",
11
+ "Because this single repository hosts six different checkpoints side by side (one per subfolder), Hugging Face's default \"Use this model\" widget doesn't represent it correctly \u2014 this notebook is the intended entry point instead.\n",
12
+ "\n",
13
+ "## What this notebook does\n",
14
+ "1. Lets you pick a model from a dropdown\n",
15
+ "2. Loads it directly from the Hub with `AutoModelForCausalLM.from_pretrained(..., trust_remote_code=True)`\n",
16
+ "3. Runs text generation on your own prompt\n",
17
+ "\n",
18
+ "No token is required (the repo is public) \u2014 a GPU (T4 is enough) will make generation faster, but these models are small enough to run fine on CPU too.\n",
19
+ "\n",
20
+ "> These models have no tokenizer \u2014 input and output are raw UTF-8 bytes. `model.generate_bytes(prompt, ...)` handles that for you."
21
+ ]
22
+ },
23
+ {
24
+ "cell_type": "code",
25
+ "execution_count": null,
26
+ "metadata": {},
27
+ "outputs": [],
28
+ "source": [
29
+ "!pip install -q -U transformers torch huggingface_hub\n",
30
+ "\n",
31
+ "import torch\n",
32
+ "from transformers import AutoModelForCausalLM\n",
33
+ "\n",
34
+ "device = 'cuda' if torch.cuda.is_available() else 'cpu'\n",
35
+ "print(f'Device: {device}')\n",
36
+ "if device == 'cuda':\n",
37
+ " print(torch.cuda.get_device_name(0))"
38
+ ]
39
+ },
40
+ {
41
+ "cell_type": "markdown",
42
+ "metadata": {},
43
+ "source": [
44
+ "## Model registry\n",
45
+ "\n",
46
+ "Used only to show languages/context length and to offer default prompts \u2014 not needed for loading, that part is fully generic."
47
+ ]
48
+ },
49
+ {
50
+ "cell_type": "code",
51
+ "execution_count": null,
52
+ "metadata": {},
53
+ "outputs": [],
54
+ "source": [
55
+ "PUBLIC_REPO = \"omurberaisik/NoTokenLM-MicroGen\"\n",
56
+ "\n",
57
+ "MODEL_INFO = {\n",
58
+ " \"2.5\": dict(languages=[\"en\"], prompts=[\"The \", \"I think that \", \"Once upon a time \"]),\n",
59
+ " \"2.6\": dict(languages=[\"en\"], prompts=[\"The \", \"I think that \", \"Once upon a time \"]),\n",
60
+ " \"2.7\": dict(languages=[\"en\"], prompts=[\"The \", \"I think that \", \"Once upon a time \"]),\n",
61
+ " \"3.5\": dict(languages=[\"en\"], prompts=[\"The \", \"I think that \", \"Once upon a time \"]),\n",
62
+ " \"3.6\": dict(languages=[\"en\"], prompts=[\"The \", \"I think that \", \"Once upon a time \"]),\n",
63
+ " \"3.7\": dict(languages=[\"en\", \"es\", \"id\", \"it\"],\n",
64
+ " prompts=[(\"en\", \"The \"), (\"en\", \"I think that \"),\n",
65
+ " (\"es\", \"El \"), (\"id\", \"Saya \"), (\"it\", \"Il \")]),\n",
66
+ "}"
67
+ ]
68
+ },
69
+ {
70
+ "cell_type": "markdown",
71
+ "metadata": {},
72
+ "source": [
73
+ "## Pick a model and load it"
74
+ ]
75
+ },
76
+ {
77
+ "cell_type": "code",
78
+ "execution_count": null,
79
+ "metadata": {},
80
+ "outputs": [],
81
+ "source": [
82
+ "SELECTED_VERSION = \"3.6\" #@param [\"2.5\", \"2.6\", \"2.7\", \"3.5\", \"3.6\", \"3.7\"]\n",
83
+ "\n",
84
+ "model = AutoModelForCausalLM.from_pretrained(\n",
85
+ " PUBLIC_REPO,\n",
86
+ " subfolder=SELECTED_VERSION,\n",
87
+ " trust_remote_code=True,\n",
88
+ ")\n",
89
+ "model.to(device)\n",
90
+ "model.eval()\n",
91
+ "\n",
92
+ "n_params = sum(p.numel() for p in model.parameters())\n",
93
+ "info = MODEL_INFO[SELECTED_VERSION]\n",
94
+ "print(f\"Loaded NoTokenLM-MicroGen-{SELECTED_VERSION}: {n_params:,} parameters\")\n",
95
+ "print(f\"Languages: {info['languages']}\")\n",
96
+ "print(f\"Context length: {model.config.max_len} bytes\")"
97
+ ]
98
+ },
99
+ {
100
+ "cell_type": "markdown",
101
+ "metadata": {},
102
+ "source": [
103
+ "## Generate text\n",
104
+ "\n",
105
+ "Edit `MY_PROMPT` and re-run. `temperature` controls randomness \u2014 lower is more predictable, higher is more chaotic; these models were evaluated at 0.5."
106
+ ]
107
+ },
108
+ {
109
+ "cell_type": "code",
110
+ "execution_count": null,
111
+ "metadata": {},
112
+ "outputs": [],
113
+ "source": [
114
+ "MY_PROMPT = \"The \" #@param {type:\"string\"}\n",
115
+ "TEMPERATURE = 0.5 #@param {type:\"number\"}\n",
116
+ "N_NEW_BYTES = 200 #@param {type:\"integer\"}\n",
117
+ "\n",
118
+ "out = model.generate_bytes(MY_PROMPT, n_new_bytes=N_NEW_BYTES, temperature=TEMPERATURE)\n",
119
+ "print(f\"Model: NoTokenLM-MicroGen-{SELECTED_VERSION} ({n_params:,} params)\")\n",
120
+ "print(f\"Prompt: {MY_PROMPT!r}\")\n",
121
+ "print(f\"Output: {out!r}\")"
122
+ ]
123
+ },
124
+ {
125
+ "cell_type": "markdown",
126
+ "metadata": {},
127
+ "source": [
128
+ "## Try the model's default prompt set\n",
129
+ "\n",
130
+ "Each model has a small built-in prompt set (the same ones used for the README examples). Run this to see how the currently loaded model does on all of them at once."
131
+ ]
132
+ },
133
+ {
134
+ "cell_type": "code",
135
+ "execution_count": null,
136
+ "metadata": {},
137
+ "outputs": [],
138
+ "source": [
139
+ "for item in info[\"prompts\"]:\n",
140
+ " if isinstance(item, tuple):\n",
141
+ " lang, prompt = item\n",
142
+ " tag = f\"[{lang}] \"\n",
143
+ " else:\n",
144
+ " prompt = item\n",
145
+ " tag = \"\"\n",
146
+ " out = model.generate_bytes(prompt, n_new_bytes=200, temperature=0.5)\n",
147
+ " print(f\"{tag}{prompt!r} -> {out!r}\\n\")"
148
+ ]
149
+ },
150
+ {
151
+ "cell_type": "markdown",
152
+ "metadata": {},
153
+ "source": [
154
+ "---\n",
155
+ "\n",
156
+ "### A note on what to expect\n",
157
+ "\n",
158
+ "These are small, from-scratch, byte-level models with no instruction tuning. From 2.7 onward they tend to produce grammatically plausible continuations, but they are not chat assistants and will not reliably stay on topic for long. See the [model card](https://huggingface.co/omurberaisik/NoTokenLM-MicroGen) for a full, honest rundown of each version's limitations."
159
+ ]
160
+ }
161
+ ],
162
+ "metadata": {
163
+ "kernelspec": {
164
+ "display_name": "Python 3",
165
+ "name": "python3"
166
+ },
167
+ "language_info": {
168
+ "name": "python"
169
+ },
170
+ "accelerator": "GPU"
171
+ },
172
+ "nbformat": 4,
173
+ "nbformat_minor": 0
174
+ }