# Cookbook

> More than fifty complete inferlets you can run, read, and copy.

Source: https://pie-project.org/docs/cookbook  

Every recipe below is a complete inferlet in the Pie repository. Clone the repository to run any recipe by name, or copy its source as a starting point for your own inferlet:

```bash
git clone https://github.com/pie-project/pie && cd pie/examples
pie run top-a-sampling-py -- --prompt "Once upon a time" --a 0.2
```

The colored dot marks which layer of the stack the recipe uses: ● uses the [Program](https://pie-project.org/docs/program) layer, and ● uses [Invent](https://pie-project.org/docs/invent) primitives such as multi-lane passes, custom attention masks, or device loops. The language tags link to each implementation on GitHub.

## Basics

- **quickstart** (program layer; [Python](https://github.com/pie-project/pie/tree/main/examples/quickstart-py), [JS](https://github.com/pie-project/pie/tree/main/examples/quickstart-js)): Greedy completion, one pass per token, driven from the host.
- **text-completion** (invent layer; [Rust](https://github.com/pie-project/pie/tree/main/examples/text-completion), [Python](https://github.com/pie-project/pie/tree/main/examples/text-completion-py), [JS](https://github.com/pie-project/pie/tree/main/examples/text-completion-js)): Greedy completion with the decode loop carried on the device.
- **chat-completion** (program layer; [Rust](https://github.com/pie-project/pie/tree/main/examples/chat-completion)): A chat reply with temperature and top-p, streamed.
- **naive-baseline** (program layer; [Rust](https://github.com/pie-project/pie/tree/main/examples/naive-baseline), [Python](https://github.com/pie-project/pie/tree/main/examples/naive-baseline-py), [JS](https://github.com/pie-project/pie/tree/main/examples/naive-baseline-js)): Plain sampling, the performance control for the other recipes.

## Sampling

- **top-a-sampling** (invent layer; [Rust](https://github.com/pie-project/pie/tree/main/examples/top-a-sampling), [Python](https://github.com/pie-project/pie/tree/main/examples/top-a-sampling-py), [JS](https://github.com/pie-project/pie/tree/main/examples/top-a-sampling-js)): Keep tokens above a · p_max², a floor that tracks confidence.
- **xtc-sampling** (invent layer; [Rust](https://github.com/pie-project/pie/tree/main/examples/xtc-sampling)): Exclude top choices, for more varied text.
- **tail-free-sampling** (invent layer; [Rust](https://github.com/pie-project/pie/tree/main/examples/tail-free-sampling)): Cut the tail where the sorted probabilities flatten.
- **locally-typical-sampling** (invent layer; [Rust](https://github.com/pie-project/pie/tree/main/examples/locally-typical-sampling)): Keep tokens close to the expected surprise.
- **mirostat-v2-sampling** (invent layer; [Rust](https://github.com/pie-project/pie/tree/main/examples/mirostat-v2-sampling)): Steer the surprise of the output toward a target.
- **eta-epsilon-sampling** (invent layer; [Rust](https://github.com/pie-project/pie/tree/main/examples/eta-epsilon-sampling)): Epsilon and eta truncation.
- **entropy-adaptive-temperature** (invent layer; [Rust](https://github.com/pie-project/pie/tree/main/examples/entropy-adaptive-temperature)): Raise or lower the temperature with the entropy.
- **repetition-penalty** (invent layer; [Rust](https://github.com/pie-project/pie/tree/main/examples/repetition-penalty)): Frequency, presence and repetition penalties.
- **dry-repetition-penalty** (invent layer; [Rust](https://github.com/pie-project/pie/tree/main/examples/dry-repetition-penalty)): Penalize tokens that would extend a repeated sequence.

## Guidance

- **classifier-free-guidance** (invent layer; [Rust](https://github.com/pie-project/pie/tree/main/examples/classifier-free-guidance)): Two lanes per pass: with and without the prompt.
- **contrastive-decoding** (invent layer; [Rust](https://github.com/pie-project/pie/tree/main/examples/contrastive-decoding)): Contrastive decoding with a single model.
- **context-aware-decoding** (invent layer; [Rust](https://github.com/pie-project/pie/tree/main/examples/context-aware-decoding)): Trust the given context over the model’s prior.

## Search and branching

- **beam-search** (invent layer; [Rust](https://github.com/pie-project/pie/tree/main/examples/beam-search), [Python](https://github.com/pie-project/pie/tree/main/examples/beam-search-py), [JS](https://github.com/pie-project/pie/tree/main/examples/beam-search-js)): Beam search on the device, with ancestry masks.
- **consensus-decoding** (program layer; [Rust](https://github.com/pie-project/pie/tree/main/examples/consensus-decoding)): Generate N answers from one prefix and pick the most central.
- **prefix-tree-kv-cache** (program layer; [Rust](https://github.com/pie-project/pie/tree/main/examples/prefix-tree-kv-cache), [Python](https://github.com/pie-project/pie/tree/main/examples/prefix-tree-kv-cache-py), [JS](https://github.com/pie-project/pie/tree/main/examples/prefix-tree-kv-cache-js)): A tree of prompts sharing their common prefixes.

## Speculative decoding

- **mtp-speculative-decoding** (invent layer; [Rust](https://github.com/pie-project/pie/tree/main/examples/mtp-speculative-decoding)): Draft from the model’s own MTP head; verify and commit on the device.
- **cacheback-speculative-decoding** (invent layer; [Rust](https://github.com/pie-project/pie/tree/main/examples/cacheback-speculative-decoding)): Prompt-lookup drafts from earlier in the context.
- **constrained-speculative-decoding** (invent layer; [Rust](https://github.com/pie-project/pie/tree/main/examples/constrained-speculative-decoding)): Speculation under a JSON Schema.
- **dflash-speculative-bench** (invent layer; [Rust](https://github.com/pie-project/pie/tree/main/examples/dflash-speculative-bench)): A block drafter’s draft-then-verify loop.

## KV cache

- **attention-sink** (invent layer; [Rust](https://github.com/pie-project/pie/tree/main/examples/attention-sink)): Attention sinks plus a sliding window.
- **snapkv-eviction** (invent layer; [Rust](https://github.com/pie-project/pie/tree/main/examples/snapkv-eviction)): Use the prompt’s attention to shrink the cache.
- **tova-attention** (invent layer; [Rust](https://github.com/pie-project/pie/tree/main/examples/tova-attention)): Drop the entry attended to least.
- **trackb-h2o** (invent layer; [Rust](https://github.com/pie-project/pie/tree/main/examples/trackb-h2o)): Keep the heavy hitters.
- **trackb-snapkv** (invent layer; [Rust](https://github.com/pie-project/pie/tree/main/examples/trackb-snapkv)): SnapKV from an observation window.

## Constrained output

- **json-schema-constrained-decoding** (program layer; [Rust](https://github.com/pie-project/pie/tree/main/examples/json-schema-constrained-decoding), [Python](https://github.com/pie-project/pie/tree/main/examples/json-schema-constrained-decoding-py), [JS](https://github.com/pie-project/pie/tree/main/examples/json-schema-constrained-decoding-js)): JSON that always matches a schema.
- **asap-grammar-aligned-decoding** (invent layer; [Rust](https://github.com/pie-project/pie/tree/main/examples/asap-grammar-aligned-decoding)): Grammar-constrained sampling without the distribution bias.
- **token-healing** (program layer; [Rust](https://github.com/pie-project/pie/tree/main/examples/token-healing)): Repair prompts that end mid-token.

## Watermarking

- **greenlist-watermarking** (invent layer; [Rust](https://github.com/pie-project/pie/tree/main/examples/greenlist-watermarking)): Bias a keyed greenlist of tokens.
- **gumbel-watermark** (invent layer; [Rust](https://github.com/pie-project/pie/tree/main/examples/gumbel-watermark)): A distortion-free watermark (Aaronson).
- **synthid-tournament-sampling** (invent layer; [Rust](https://github.com/pie-project/pie/tree/main/examples/synthid-tournament-sampling)): SynthID-Text tournament sampling.

## Images, video and diffusion

- **text-to-media** (program layer; [Rust](https://github.com/pie-project/pie/tree/main/examples/text-to-media)): Text to image or video with any diffusion model.
- **image-captioning** (program layer; [Rust](https://github.com/pie-project/pie/tree/main/examples/image-captioning)): Describe an image.
- **diffusion-baseline** (invent layer; [Rust](https://github.com/pie-project/pie/tree/main/examples/diffusion-baseline)): A text diffusion model’s reference sampler.
