Bastile
Drop-in monkey-patch that replaces HuggingFace Qwen3 ops with optimized CuTile kernels for training on NVIDIA Blackwell GPUs.
Requires: NVIDIA Blackwell (B200 / B100) + CUDA Toolkit 13.1+
Benchmarks
Qwen3-8B (36 layers, 4096 hidden, 32 heads) — single B200, batch_size=1, bf16, AdamW:
Throughput (tokens/sec)
Peak GPU Memory (GB)
Latency (ms/iter)
Bastile's fused linear cross-entropy avoids materializing the full [batch * seq_len, vocab_size] logits tensor, which is the dominant memory cost at longer sequences. This is where the memory savings and throughput gains compound.
Installation
pip install bastile
Prerequisites:
- NVIDIA Blackwell GPU (B200, B100, GB200 or RTX 50 Series Chip)
- CUDA Toolkit 13.1+
- PyTorch 2.9+ with CUDA support
# Inside a CUDA 13.1 container:
pip install bastile
Quick Start
import bastile
# Apply all patches BEFORE loading / creating the model
bastile.apply()
from transformers import Qwen3ForCausalLM
model = Qwen3ForCausalLM.from_pretrained("Qwen/Qwen3-8B")
model.train()
# Train as usual — Bastile automatically uses optimized kernels
Selective patching:
bastile.apply(
rms_norm=True, # CuTile RMSNorm
swiglu=True, # CuTile SwiGLU MLP
rope=True, # CuTile RoPE
fused_linear_cross_entropy=True, # CuTile fused linear + CE
)
Reset to original HuggingFace implementations:
bastile.reset()
Kernel Implementations
Bastile patches 4 operations in transformers.models.qwen3.modeling_qwen3. Each has a full forward and backward pass:
| Operation | Backend | Source | What it replaces |
|---|---|---|---|
| RMSNorm | CuTile | ops/rms_norm.py |
Qwen3RMSNorm |
| SwiGLU MLP | CuTile | ops/swiglu.py |
Qwen3MLP |
| RoPE | CuTile | ops/rope.py |
apply_rotary_pos_emb |
| Fused Linear Cross-Entropy | CuTile | ops/fused_linear_cross_entropy.py |
Qwen3ForCausalLM.forward |
RMSNorm — CuTile
Native CuTile RMSNorm with persistent forward and backward kernels. Uses gather/scatter memory access with SM-aware tile sizing.
src/bastile/ops/rms_norm.py → patches Qwen3RMSNorm
SwiGLU MLP — CuTile
Native CuTile kernels using cuda.tile with gather/scatter memory access. Uses flush_to_zero and approximate reciprocal for fast sigmoid on Blackwell. Full backward with recomputation (no extra activation memory).
src/bastile/ops/swiglu.py → patches Qwen3MLP
RoPE — CuTile
CuTile rotary position embedding with compiler-optimized occupancy. In-place rotation on reshaped tensors to minimize memory traffic. Backward reuses the forward kernel by negating sin (inverse rotation identity).
src/bastile/ops/rope.py → patches apply_rotary_pos_emb
Fused Linear Cross-Entropy — CuTile
Replaces the standard lm_head(hidden_states) → logits → cross_entropy(logits, labels) pipeline with a chunked approach. This never materializes the full logits tensor ([batch * seq, 151936] for Qwen3), instead computing cross-entropy in chunks. This is the single biggest memory saver at long sequence lengths.
src/bastile/ops/fused_linear_cross_entropy.py → patches Qwen3ForCausalLM.forward
API Reference
import bastile
bastile.apply() # Patch all ops
bastile.apply(rope=False) # Patch everything except RoPE
bastile.reset() # Restore original implementations
bastile.get_patched_ops() # List currently active patches
bastile.warmup_all_kernels() # Pre-compile kernels (avoids JIT lag)
bastile.clear_autotune_cache() # Clear kernel caches
Running Benchmarks
# Qwen3-8B sequence length sweep (parallel on 3 GPUs)
make bench-8b
# Qwen3-8B sweep (sequential, single GPU)
make bench-8b-seq
# Qwen3-8B FSDP multi-GPU benchmark
make bench-fsdp
# Kernel micro-benchmarks
make bench-rmsnorm
make bench-lce
Why CuTile instead of Triton?
Bastile uses NVIDIA's CuTile (cuda.tile) instead of Triton. On Blackwell (sm_100), CuTile generates native PTX through NVIDIA's own compiler toolchain, while Triton's code generation for sm_100 is still maturing. In our benchmarks, Triton-based kernels (Liger) often underperform raw PyTorch on B200, whereas CuTile kernels consistently match or beat it.
License
MIT
Metadata
Release files for bastile 0.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| bastile-0.1.3.tar.gz | 17.6 kB | With provenance | Details for bastile-0.1.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| bastile-0.1.3-py3-none-any.whl | Python 3 | none | any | Details for bastile-0.1.3-py3-none-any.whl |
Total release size: 38.8 kB
Release files / bastile-0.1.3.tar.gz
| Download URL | bastile-0.1.3.tar.gz |
|---|---|
| Size | 17.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d2cf40bb79a7f348f2bd9f693e4f0a916f88089ac3dbe5caf1ee3d875acb2ec4
|
|
BLAKE2b-256 checksum How to use checksums |
3dce9f8cc9da67dd889be46bfb72dc66a1ebddf0b21910cc81a6b3d51d278b4f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Mar 2, 2026.
Transparency logRelease files / bastile-0.1.3-py3-none-any.whl
| Download URL | bastile-0.1.3-py3-none-any.whl |
|---|---|
| Size | 21.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f4e5d183d50c6c3839c508aa0f864ef45049bddbfe0d7f83c5de11e8d17ce5cf
|
|
BLAKE2b-256 checksum How to use checksums |
a011c6ec0b926faee0c593c0d21c2af5e5bc8a21eb963233e099f37afb1acc12
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Mar 2, 2026.
Transparency logSecurity for bastile 0.1.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for bastile-0.1.3.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Mar 2, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.