Metadata-Version: 2.1
Name: nextok
Version: 0.1.0
Summary: Fast cross-platform LLM token sampler for CPUs (temperature, top-k/p, min-p, penalties, logit bias)
Keywords: llm,sampling,top-p,top-k,inference,simd
Author: sjjeong94
License: MIT
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: MacOS
Classifier: Operating System :: Microsoft :: Windows
Classifier: Operating System :: POSIX :: Linux
Classifier: Programming Language :: C++
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.10
Requires-Dist: numpy>=1.21
Description-Content-Type: text/markdown

# nextok

Fast, cross-platform LLM token sampler for CPUs, with a NumPy-friendly Python API.

Supported options: temperature, top-k, top-p, min-p, repetition / frequency / presence
penalty, logit_bias, seed. Exact semantics: [docs/semantics.md](docs/semantics.md).
Benchmarks: [docs/performance.md](docs/performance.md). For example, at vocab 128k a
top-p=0.95 draw takes ~50 µs on one core, ~190× faster than a sort-based sampler.
Roadmap: [plan.md](plan.md).

```bash
pip install nextok   # Python >= 3.10; wheels for Linux, macOS and Windows (x86-64 / arm64)
```

```python
import numpy as np
import nextok

sampler = nextok.Sampler(vocab_size=32000)
seq = sampler.new_sequence(prompt_ids)
token = sampler.sample(logits, temperature=0.8, top_p=0.95, seed=42, seq=seq)
```

## CPU backends

SIMD kernels are built with [Highway](https://github.com/google/highway) for every
supported instruction set (SSE2 through AVX-512/AVX10 on x86, NEON/SVE on ARM), and
the best one the CPU supports is picked at runtime:

```python
>>> nextok.cpu_info()
{'backend': 'AVX2', 'available': ['AVX2', 'SSE4', 'SSSE3', 'SSE2', 'scalar']}
```

Set `NEXTOK_BACKEND=<name>` (for example `scalar`) to override the choice.

## Threads

`Sampler(vocab_size, num_threads=0)` samples the rows of a batch in parallel
(`num_threads=0` means up to min(physical cores, 8) threads). A single row stays on
the calling thread unless `num_threads > 1` is given explicitly. In that case a large
row (vocab >= 64k) is also split across threads, which helps when calls come back to
back. Waking idle workers can cost more than the split saves when calls are spaced
out. Results never depend on the thread count.

## Development

Requires [uv](https://docs.astral.sh/uv/), CMake >= 3.18, and a C++17 compiler.

```bash
uv sync              # create .venv, build the extension (editable), install dev deps
uv run pytest        # run tests; edits to C++ sources trigger a rebuild automatically
uv build             # build sdist + wheel into dist/

# Native C ABI test
cmake -S . -B build/native -DNEXTOK_BUILD_TESTS=ON && cmake --build build/native && ctest --test-dir build/native

# Benchmarks
cmake -S . -B build/bench -DNEXTOK_BUILD_BENCH=ON && cmake --build build/bench
./build/bench/bench_sampler --reference   # C++ latency per preset, vs. scalar reference
./build/bench/bench_sampler --backend SSE4  # force a kernel backend
./build/bench/bench_sampler --threads 4 --batch 16  # parallel batch
uv run python bench/bench.py              # Python latency vs. a NumPy sampler
```

Highway is downloaded at configure time (pinned release). To build offline, point
`FETCHCONTENT_SOURCE_DIR_HIGHWAY` at a local checkout, or use an installed copy with
`-DNEXTOK_USE_SYSTEM_HWY=ON`. `-DNEXTOK_SIMD=OFF` builds the scalar backend only.

### Releasing

`.github/workflows/wheels.yml` builds the sdist and wheels with cibuildwheel. That means
abi3 wheels for CPython >= 3.12, per-version wheels for 3.10–3.11, manylinux_2_28 and
musllinux for x86_64 and aarch64, macOS arm64 and x86_64, and Windows AMD64. Every wheel
runs the test suite before it is uploaded. Pushing a `v*` tag publishes to PyPI through
Trusted Publishing. Before the first release:

1. Create a GitHub environment named `pypi`. Adding required reviewers is recommended.
2. Register this repository as a trusted publisher of project `nextok` on pypi.org
   (workflow `wheels.yml`, environment `pypi`).
3. Bump `NEXTOK_VERSION_MAJOR/MINOR/PATCH` in `include/nextok.h`, the single source of the
   version (CMake and `pyproject.toml` read it from there). Then tag:
   `git tag v0.1.0 && git push --tags`.

The Python version is pinned in `.python-version`; uv prefers its managed interpreters,
which include the headers needed to build the extension.
