Metadata-Version: 2.4
Name: ttfs
Version: 0.6.9
Summary: TriTon Frontend Supercharged
License: MIT
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: rich
Dynamic: license-file

# ttfs
TriTon Frontend Supercharged

`ttfs_ext` is optional. Ordinary TTFS imports, kernels and tests use native
Triton/Gluon without it. TTFS integration tests for the extension live in
`test/extension` and skip when the extension is unavailable. Run that directory
separately with the matching extension wheel installed:

```sh
PYTHONPATH=src python -m pytest -p ttfs_ext test/extension
```

`-p ttfs_ext` imports the compiler plugin before test collection initializes
Triton or TTFS. If native Triton/TTFS was already initialized without the plugin,
the integration kernel modules skip instead of changing that process's backend.

## NPO2 TMA (experimental)

Import `ttfs_ext` before Triton/TTFS. Triton 3.7.1 and 3.8.0 support a
constexpr first-axis TMA extent over a padded allocation:

```python
B = ttfs.TensorDescCreator("N,K", {"N": "BN", "K": "BK"})
B = B.with_npo2_dim(dim=0, size="N_REAL")  # e.g. BN=128, BK=64, N_REAL=88
with ttfs.tensor_desc_creator_dispatch(is_gluon=True):
    b_desc = B.create_tma_desc(b)
    # Construct the tensor manager here, with BN/BK/N_REAL constexpr fields.
```

The expression is stored in `block_sym_cfg` and evaluated like block sizes.
Pipeline setup constructs the device map once from the raw pointer, manager
dimensions and existing stride resolution. Declare actual strides with
`tensor_desc_meta(...)` for noncontiguous axes; other strides use contiguous
inference as for ordinary pointer descriptors. Allocation shapes remain padded.
Default iteration steps use the logical extent; explicit `block_strides` retain
priority. Load barriers
expect the actual transfer bytes, summed across the group.

For direct kernel usage, a pointer-backed `TensorDesc` provides
`desc.to_npo2_tma(shape, strides, block_shape, size="88", dim=0, layout=None)`.
Use `constexpr_fields=ttfs.mp.get_constexpr_field_dict(tm)` for named expressions.
It returns a new descriptor; existing tensor maps are rejected. On its blocked
TMA descriptor, `get_transfer_nbytes_cw()` supplies the load expectation;
`get_block_nbytes_cw()` continues to report padded capacity.

Device map storage uses Triton's scratch allocator, as native device descriptor
creation does. Configure it before launch, for example with PyTorch:

```python
triton.set_allocator(lambda size, alignment, stream:
    torch.empty(size, dtype=torch.uint8, device="cuda"))
```

Initial support is Gluon, SM90+, ranks 2–5, FP16/BF16 or FP8 E4M3/E5M2,
one CTA, zero padding,
at most 256 elements per padded dimension, and a nontransposed 32/64/128-byte
inner row equal to the NVMMA swizzle width. Gather/scatter, reductions and
multicast are rejected.
Only the first physical axis can have a smaller issue extent; for example,
`[8, 2, 64]` can issue `[5, 2, 64]`, while all allocation dimensions stay padded.
Unused first-axis slices in SMEM remain untouched; initialize them before full-tile reads.
This feature changes TMA only; UMMA issue extents remain explicit.

## NPO2 UMMA (experimental)

The ordinary unscaled UMMA functions and matching `PipelineProducer` methods
accept an optional `npo2_real_n` constexpr integer:

```python
ttfs.io.issue_mma_tn_async(acc, a_smem, b_smem,
                         use_acc=False, npo2_real_n=88)
```

`npo2_real_n` always names N of the logical result `[M,N]`, including for
`issue_mma_nt_async` and `issue_mma_tt_async`. `None` keeps the native path and
does not require `ttfs_ext`. A custom extent requires the extension imported
before Triton/TTFS, Triton 3.7.1 or 3.8.0, and Blackwell. Initial support is
single-CTA matching shared FP16/BF16 or FP8 E4M3/E5M2 operands,
packed FP32 TMEM with M=128 and one padded
N=32..256 tile. The real N must be positive, divisible by 8 and within capacity;
multicast is rejected. SMEM/TMEM allocation shapes stay padded.

For FP8, K must be divisible by 32 and operands use `NVMMASharedLayout` without
FP4 padding. The TN path with physical B `[N,K]` supports N=72/88/120 over
padded N=128; a `[128,128]` FP8 TMA tile uses 128-byte swizzling and 8-bit
elements. N-major B (the TT/NT paths with contiguous N) requires real N divisible
by 16. This is ordinary unscaled `f8f6f4` MMA, without MXFP8 scale operands.
The existing creator and producer arguments apply directly:

```python
# Host, inside Gluon descriptor dispatch; REAL_N is a constexpr manager field.
b_creator = ttfs.TensorDescCreator("N,K", {"N": "128", "K": "128"})
b_desc = b_creator.with_npo2_dim(dim=0, size="REAL_N").create_tma_desc(b)
# Device, after waiting for the input pipeline's TMA completion.
producer = producer.issue_mma_tn_async(
    "acc", a_smem, b_smem, use_acc=False, npo2_real_n=tm.REAL_N)
```

Columns at or beyond the real N remain untouched. Initialize the whole
accumulator if later code reads its padding. Existing predicates, attached
completion barriers, explicit commits and waits keep their usual meaning;
the extent does not add a completion wait. Extension-specific tests are in
`test/extension/test_npo2_umma.py`.
