|
| 1 | +# Batch Inference |
| 2 | + |
| 3 | +Run prompts, embeddings, and model scoring over large datasets, then stream the results to durable storage. Daft is a reliable engine to express batch inference pipelines and scale them from your laptop to a distributed cluster. |
| 4 | + |
| 5 | +## When to use Daft for batch inference |
| 6 | + |
| 7 | +- **You need to run models over your data:** Express inference on a column (e.g., [`llm_generate`](#example-text-generation-with-openai), [`embed_text`](../modalities/text.md#how-to-use-the-embed_text-function), [`embed_image`](../api/ai.md)) and let Daft handle batching, concurrency, and backpressure. |
| 8 | +- **You have data that are large objects in cloud storage:** Daft has [record-setting](https://www.daft.ai/blog/announcing-daft-02) performance when reading and writing from S3, and provides flexible APIs for working with [URLs and Files](../modalities/urls.md). |
| 9 | +- **You're working with multimodal data:** Daft supports datatypes like [images](../modalities/images.md) and [video](../modalities/videos.md), and supports the ability to define [custom data sources and sinks](../connectors/custom.md) and [custom functions over this data](../custom-code/udfs.md). |
| 10 | +- **You want end-to-end pipelines where data sizes expand and shrink:** For example, downloading images from URLs, decoding them, then embedding them; [Daft streams across stages to keep memory well-behaved](https://www.daft.ai/blog/processing-300k-images-without-oom). |
| 11 | + |
| 12 | +If you’re new to Daft, see the [quickstart](../quickstart.md) first. For distributed execution, see our docs on [Scaling Out and Deployment](../distributed.md). |
| 13 | + |
| 14 | +## Core idea |
| 15 | + |
| 16 | +Daft provides first-class APIs for model inference. Under the hood, Daft pipelines data operations so that reading, inference, and writing overlap automatically, and is optimized for throughput. |
| 17 | + |
| 18 | +## Example: Text generation with OpenAI |
| 19 | + |
| 20 | +=== "🐍 Python" |
| 21 | +```python |
| 22 | +import daft |
| 23 | +from daft.functions import llm_generate |
| 24 | + |
| 25 | +( |
| 26 | + daft.read_huggingface("fka/awesome-chatgpt-prompts") |
| 27 | + .with_column( # Generate model outputs in a new column |
| 28 | + "output", |
| 29 | + llm_generate( |
| 30 | + daft.col("prompt"), |
| 31 | + model="gpt-4o", # Any chat/completions-capable model |
| 32 | + provider="openai", # Switch providers by changing this; e.g. to "vllm" |
| 33 | + api_key="...", # Pass via environment variable or secret manager |
| 34 | + temperature=0.2, |
| 35 | + max_tokens=256, |
| 36 | + ), |
| 37 | + ) |
| 38 | + .write_parquet("output.parquet/", write_mode="overwrite") # Write to Parquet as the pipeline runs |
| 39 | +) |
| 40 | +``` |
| 41 | + |
| 42 | +What this does: |
| 43 | + |
| 44 | +- Uses [`llm_generate()`](../../api/functions/llm_generate) to express inference. |
| 45 | +- Streams rows through OpenAI concurrently while reading from Hugging Face and writing to Parquet. |
| 46 | +- Requires no explicit async, batching, rate limiting, or retry code in your script. |
| 47 | + |
| 48 | +## Example: Local text embedding with LM Studio |
| 49 | + |
| 50 | +=== "🐍 Python" |
| 51 | +```python |
| 52 | +import daft |
| 53 | +from daft.ai.provider import load_provider |
| 54 | +from daft.functions.ai import embed_text |
| 55 | + |
| 56 | +provider = load_provider("lm_studio") |
| 57 | +model = "text-embedding-nomic-embed-text-v1.5" |
| 58 | + |
| 59 | +( |
| 60 | + daft.read_huggingface("Open-Orca/OpenOrca") |
| 61 | + .with_column("embedding", embed_text(daft.col("response"), provider=provider, model=model)) |
| 62 | + .show() |
| 63 | +) |
| 64 | +``` |
| 65 | + |
| 66 | +Notes: |
| 67 | + |
| 68 | +- [LM Studio](https://lmstudio.ai/) is a local AI model platform that lets you run Large Language Models like Qwen, Mistral, Gemma, or gpt-oss on your own machine. By using Daft with LM Studio, you can perform inference with any model locally, and utilize accelerators like [Apple's Metal Performance Shaders (MPS)](https://developer.apple.com/documentation/metalperformanceshaders). |
| 69 | + |
| 70 | +## Scaling out on Ray |
| 71 | + |
| 72 | +Turn on distributed execution with a single line; then run the same script on a Ray cluster. |
| 73 | + |
| 74 | +```python |
| 75 | +import daft |
| 76 | +daft.context.set_runner_ray() # Enable Daft's distributed runner |
| 77 | +``` |
| 78 | + |
| 79 | +Daft partitions the data, schedules remote execution, and orchestrates your workload across the cluster-no pipeline rewrites. |
| 80 | + |
| 81 | +## Patterns that work well |
| 82 | + |
| 83 | +- **Read → Preprocess → Infer → Write**: Daft parallelizes and pipelines automatically to maximize throughput and resource utilization. |
| 84 | +- **Provider-agnostic pipelines**: Switch between OpenAI and local LLMs by changing a single parameter. |
| 85 | + |
| 86 | +## Case Studies |
| 87 | + |
| 88 | +For inspiration and real-world scale: |
| 89 | + |
| 90 | +- [Processing 24 trillion tokens with 0 crashes—How Essential AI built Essential-Web v1.0 with Daft](https://www.daft.ai/blog/how-essential-ai-built-essential-web-v1-with-daft) |
| 91 | +- [Processing 300K Images Without OOMs](https://www.daft.ai/blog/processing-300k-images-without-oom) |
| 92 | +- [Embedding millions of text documents with Qwen3, achieving near 100% GPU utilization](https://www.daft.ai/blog/embedding-millions-of-text-documents-with-qwen3) |
| 93 | + |
| 94 | +## Next Steps |
| 95 | + |
| 96 | +Ready to explore Daft further? Check out these topics: |
| 97 | + |
| 98 | +- [AI functions](../api/ai.md) |
| 99 | +- Reading from and writing to common data sources: |
| 100 | + - [S3](../connectors/aws.md) |
| 101 | + - [Hugging Face 🤗](../connectors/huggingface.md) |
| 102 | + - [Turbopuffer](../connectors/turbopuffer.md) |
| 103 | +- [Scaling out and deployment](../distributed.md) |
0 commit comments