Grok-1 Stop Signs
by fitzyracing1
README.md
# Grok-1 Stop Signs (MCP plugin)

Grok-1 with stop signs, as tools any agent can call. A small "driver" model drafts words,
and Grok-1 (the "checker") checks the whole draft in one pass each time a stop sign comes
up. Every word Grok-1 agrees with is kept; at the first disagreement Grok-1's own word is
used instead. Because the check is greedy, the output is identical to plain greedy
decoding, token for token, while the big model runs fewer times.
This is a free, Apache-2.0 MCP server (stdio and streamable HTTP) packaged as a Grok Build
/ Cursor plugin. It builds on:
- [fitzyracing1/grok-1-stopsigns](https://github.com/fitzyracing1/grok-1-stopsigns), stop
signs added to xAI's open [Grok-1](https://github.com/xai-org/grok-1) JAX code (vendored
at commit `82a1f0f`), and
- [fitzyracing1/stopsigns](https://github.com/fitzyracing1/stopsigns), the Hugging Face /
PyTorch version (pinned at `ddf8c28` for the `hf` backend).
Not made, endorsed or verified by xAI.
## Tools
| Tool | What it does |
|---|---|
| `generate(prompt, policy="every:4", max_new_tokens=32, backend="grok1-tiny", check_lossless=false, hf_driver, hf_checker)` | Generates text with stop signs. Returns the text plus stats: checker passes used, plain-greedy passes, passes saved, driver passes, accepted / rejected draft tokens, stop signs, wall time. With `check_lossless` it also runs plain greedy and reports whether the outputs are identical and the measured speedup. |
| `benchmark(policies, prompt, max_new_tokens=32, backend="grok1-tiny")` | One plain-greedy baseline, then each policy; every row is checked for identical output. |
| `list_policies()` | The stop-sign policies. |
| `list_backends()` | The backends, what they need, and whether they are tested. |
### Stop-sign policies
| Policy | Stops the draft when |
|---|---|
| `every:N` | N tokens have been drafted |
| `sentence` / `sentence:M` | a drafted token contains `. ? ! ; :` (or after M tokens, default 16) |
| `conf:T` | the driver's top-token probability drops below T (max 16 tokens) |
| `sentence,conf:0.5,every:8` | any of the listed policies fires |
### Backends
| Backend | Status | What it is |
|---|---|---|
| `grok1-tiny` (default) | Tested on CPU in CI | Grok-1's real architecture (8-expert top-2 MoE, GQA, RoPE) and real 131,072-token tokenizer, shrunk to 4 layers with **random weights**. The driver is the checker's first layer. No GPU, no download, no sign-in. The text is gibberish; the mechanics are real. |
| `hf` | Tested (CPU in CI; GPU by hand) | Any Hugging Face driver/checker pair that shares a tokenizer, e.g. `distilgpt2` → `gpt2-large`. Needs the `[hf]` extra; models download from huggingface.co on first use. Defaults: `STOPSIGNS_HF_DRIVER=distilgpt2`, `STOPSIGNS_HF_CHECKER=gpt2`. |
| `grok1` | **Untested** | Full-size Grok-1 (314B) from your own checkpoint: set `GROK1_CHECKPOINT_DIR` to the folder holding `ckpt-0`. Needs JAX with CUDA and about 8 large GPUs (`GROK1_MESH`, default `1,8`). The driver is Grok-1 cut to its first `GROK1_DRIVER_LAYERS` layers (default 8). |
## Install and run
Requires Python 3.10 to 3.12 (JAX 0.4.25, which Grok-1's code is pinned to, has no 3.13
wheels) and [uv](https://docs.astral.sh/uv/) or pip.
As a plugin, the bundled `mcp.json` starts the server with:
```bash
uv run --directory <plugin folder> --python 3.11 grok1-stopsigns-mcp
```
The first start installs the dependencies (JAX, Haiku, SentencePiece, the MCP SDK; about
150 MB) into the plugin folder, which can take a minute.
By hand:
```bash
git clone https://github.com/fitzyracing1/grok-1-stopsigns-plugin
cd grok-1-stopsigns-plugin
pip install -e . # add ".[hf]" for the Hugging Face backend
grok1-stopsigns-mcp # stdio
grok1-stopsigns-mcp --transport http --host 127.0.0.1 --port 8765
# streamable HTTP at http://127.0.0.1:8765/mcp
```
Any MCP client config:
```json
{
"mcpServers": {
"grok1-stopsigns": { "command": "grok1-stopsigns-mcp" }
}
}
```
The HTTP transport has no authentication. It binds to 127.0.0.1 by default; put it
behind your own auth before exposing it.
### Sample `generate` result (grok1-tiny, CPU)
`generate(prompt="The answer to life the universe and everything is of course", policy="every:4", max_new_tokens=32, check_lossless=true)`:
```json
{
"text": "cerealAddToAddToAddTo...ictionary Guilty Guilty Guilty ...",
"stats": {
"new_tokens": 32, "checker_passes": 9, "walk_checker_passes": 32,
"checker_passes_saved": 23, "checker_pass_reduction": 3.556,
"driver_passes": 34, "accepted_draft_tokens": 24, "rejected_draft_tokens": 3,
"bonus_tokens": 5, "stop_signs": 9, "acceptance_rate": 0.889, "wall_time_s": 4.37
},
"lossless": { "checked": true, "identical": true, "plain_greedy_wall_time_s": 3.75, "speedup": 0.858 }
}
```
Grok-1 ran 9 times instead of 32 and the output matched plain greedy exactly. It was still
slower in wall time, which is expected on a CPU with a tiny model (see limits).
## Honest limits
- **grok1-tiny is a demo.** Random weights, so the text is nonsense. It proves the output
is lossless and counts the checker passes saved; it says nothing about quality.
- **Fewer checker passes is not the same as faster.** On CPU, and with small models,
driving is usually slower than walking. The wall-clock win shows up when the checker is
much bigger than the driver and runs on a GPU: with the `hf` backend on an RTX 5070,
`distilgpt2` → `gpt2-large`, 128 tokens, `every:8` took 0.843 s vs 1.994 s (2.37x
faster, identical output). Tiny pairs like `distilgpt2` → `gpt2` were slower on the same
GPU.
- **Full-size Grok-1 is untested.** The `grok1` backend wires the 314B checkpoint into the
same code, but nobody has run it: it needs the ~300 GB checkpoint and about 8 large GPUs.
No speedup is claimed for it.
- The Grok-1 backends re-run the whole context each pass (no KV cache), like Grok-1's
reference code. The `hf` backend uses a KV cache.
- Greedy only; no sampling or temperature.
- One request at a time per server process (models are shared behind a lock).
## Network and credentials
- `grok1-tiny`: no network calls, no credentials.
- `hf`: downloads model weights from huggingface.co on first use. No token needed for
public models.
- `grok1`: reads a local checkpoint folder; no network.
- Installing fetches Python packages from PyPI (and, for `[hf]`, `stopsigns` from GitHub
at a pinned commit).
- No telemetry. The server reads only the environment variables listed above.
## Testing instructions for reviewers
No sign-in, API key, GPU or download beyond the Python packages is needed. Everything
below uses the default CPU backend `grok1-tiny`. The first call takes about 10 to 20
seconds while JAX compiles the tiny model; later calls take a few seconds. Expect
gibberish text: the weights are random. What to check is `lossless.identical: true` and
`checker_passes` lower than `walk_checker_passes`.
Example prompts:
1. "Use grok1-stopsigns to list the stop-sign policies and the backends, and tell me which
backend works on CPU."
2. "With grok1-stopsigns, generate 32 tokens from 'The answer to life the universe and
everything is of course' using policy every:4 with check_lossless on. How many Grok-1
checker passes did stop signs save, and was the output identical?"
3. "Benchmark the stop-sign policies every:2, every:4, every:8, sentence and conf:0.5 on
grok1-tiny. Which one saved the most checker passes, and were all of them lossless?"
4. "Generate 20 tokens from 'Stop signs let the driver go fast' with the policy
sentence,conf:0.5,every:6 and check that it matches plain greedy decoding."
## Development
```bash
pip install -e ".[test]" # add hf for the hf-backend test
python -m pytest -v # starts the server over stdio and HTTP, calls the tools
```
CI (`.github/workflows/ci.yml`) runs the same tests on CPU, plus the `hf` backend with
`sshleifer/tiny-gpt2` → `distilgpt2`.
## License
Apache-2.0. See [LICENSE](LICENSE) and [NOTICE](NOTICE). Grok-1 code © 2024 X.AI Corp.
(Apache-2.0); stop-sign code © 2026 fitzyracing1 (Apache-2.0 for the Grok-1 port, MIT for
`stopsigns`). The Grok-1 weights are not included.