Metadata-Version: 2.1
Name: build-slm
Version: 1.0.0
Summary: Research-Grade TPU-Native Language Model Training and Inference Framework
Author: Niranjan Kumar K.
License: Apache-2.0
Project-URL: Homepage, https://github.com/niranjankumark/slm
Project-URL: Documentation, https://github.com/niranjankumark/slm/blob/main/docs/USER_GUIDE.md
Project-URL: Bug Tracker, https://github.com/niranjankumark/slm/issues
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.10
Description-Content-Type: text/markdown
Provides-Extra: tpu
Requires-Dist: jax[tpu] ; extra == 'tpu'
Provides-Extra: gpu
Requires-Dist: jax[cuda] ; extra == 'gpu'
Provides-Extra: cpu
Requires-Dist: jax ; extra == 'cpu'
Provides-Extra: dev
Requires-Dist: twine ; extra == 'dev'
Requires-Dist: build ; extra == 'dev'
Requires-Dist: wheel ; extra == 'dev'
Requires-Dist: pytest ; extra == 'dev'

# KLM: Research-Grade TPU-Native Language Model Framework

**Package name:** `klm` &nbsp;|&nbsp; **Author:** Niranjan Kumar K. &nbsp;|&nbsp; **Version:** 1.0.0 &nbsp;|&nbsp; **License:** Apache-2.0

An advanced, scalable, research-grade language model training and inference framework optimized exclusively for Google Colab TPU accelerators using JAX and XLA, featuring a 100% backward-compatible pure-Python CPU reference implementation.

> **Backward compatibility:** `import slm` and `from slm import *` continue to work — the `slm` shim re-exports everything from `klm`.

See [the user guide](docs/USER_GUIDE.md), [the Google Colab TPU guide](docs/COLAB_TPU_GUIDE.md), [the benchmark report](docs/BENCHMARK_REPORT.md), and [the automated test report](docs/TEST_REPORT.md).

---

## Highlights

- **TPU-Native JAX/XLA Engine**: High-throughput training on Google Colab TPU (v2-8, v3-8, v4-8) with bfloat16 mixed precision and float32 master weights.
- **Advanced Architecture**: Causal multi-head attention, vectorized RoPE, numerically stable RMSNorm in float32, SwiGLU feed-forward networks, and pre-normalization.
- **Sequence Packing**: Static-shape sequence multi-packing with 2D block-diagonal causal masks and padding-aware loss masks.
- **Multi-Device TPU Scaling**: Automatic topology inspection, JAX device mesh, and `NamedSharding` across TPU cores.
- **Dedicated Instruction Tuning**: SFT pipeline supporting QA, Alpaca-style instruction-response, multi-turn, reasoning, and math datasets with **assistant-only loss masking**.
- **Inference Optimization**: Stateful KV-caching for O(1) decoding, greedy search, temperature, top-k, nucleus top-p, and repetition penalties.
- **Data Quality Intelligence**: Multi-stage cleaning, unicode NFKC, mojibake repair, HTML/boilerplate stripping, SimHash deduplication, 10-domain classification, and zero-leakage disjoint splitting.
- **Comprehensive Evaluation**: Top-1, Top-5, Top-10 token accuracy, word accuracy, perplexity, Expected Calibration Error (ECE), and long-context position-bucket evaluation.
- **Safe Migration & Recovery**: Atomic checkpoint saving with SHA-256 parameter checksums and backward-compatible v4-to-v5 `.kni` migration.

---

## Installation

```bash
# Base install (pure Python stdlib — no external dependencies)
pip install klm

# With TPU acceleration (Google Colab TPU)
pip install "klm[tpu]"

# With GPU acceleration (CUDA)
pip install "klm[gpu]"

# With CPU-only JAX
pip install "klm[cpu]"
```

---

## Quick Start

```python
from klm import *

# Initialize model (backend="auto" uses TPU if present, falls back to CPU)
model = klm(
    vocab_size=512,
    context_length=128,
    d_model=64,
    heads=4,
    layers=2,
    ff_dim=128,
    learning_rate=3e-4,
    weight_decay=0.01,
)

# Train on a folder of documents
report = model.train(
    "folder_path",
    epochs=3,
    batch_size=4,
    gradient_accumulation_steps=2,
    checkpoint_path="models/my_model.kni",
    use_advanced_cleaning=True,
)

# Fast KV-cached text generation
output = model.predict("The research showed that", max_tokens=80, temperature=0.7, top_p=0.9)
print(output)
```

Use `model.save("models/name")` and `model.load("models/name")` to store or restore a model. The only accepted model extension is `.kni`; it is added automatically when omitted.

---

## Backward Compatibility

The `slm` package name is preserved as a shim. Existing code using `from slm import *` or `import slm` continues to work without any changes.

```python
# Legacy code — still works unchanged
from slm import *
model = slm(vocab_size=512)
```
