Skip to main content
Flash is now part of the Runpod SDK. This page documents the standalone runpod-flash package, which keeps working but no longer gets new features. For new projects, use the Runpod SDK. To see how Flash commands and imports map to the SDK, see What changed.
This tutorial shows you how to build a text generation script using Flash and Hugging Face’s transformers library. You’ll learn how to load a pretrained language model on a GPU worker and generate text from prompts.

Requirements

What you’ll build

By the end of this tutorial, you’ll have a working text generation application that:
  • Accepts text prompts as input.
  • Generates natural language completions using GPT-2.
  • Runs entirely on Runpod’s GPU infrastructure.
  • Returns generated text with execution metadata.

Step 1: Set up your project

Create a new directory for your project and set up a Python virtual environment:
Install Flash using uv:
Create a .env file with your Runpod API key:
Replace YOUR_API_KEY with your actual API key from the Runpod console.

Step 2: Understand the Hugging Face transformers library

Hugging Face transformers is a popular Python library for working with pretrained language models. It provides:
  • Thousands of pretrained models: GPT-2, GPT-3, BERT, T5, LLaMA, and more
  • Unified API: Same code works across different model architectures
  • Model hub integration: Download models directly from Hugging Face Hub
  • Production-ready: Used by companies and researchers worldwide
For this tutorial, we’ll use GPT-2, a 124M parameter language model from OpenAI. It’s small enough to load quickly but powerful enough to generate coherent text.

Step 3: Create your project file

Create a new file called text_generation.py:
Open this file in your code editor. The following steps walk through building the text generation application.

Step 4: Add imports and configuration

Add the necessary imports and Flash configuration:

Step 5: Define the text generation function

Add the endpoint function that will run on the GPU worker:
Configuration breakdown:
  • name="text-generation": Identifies your endpoint in the Runpod console
  • gpu=[GpuGroup.AMPERE_24, GpuGroup.ADA_24]: Allows workers to use L4, A5000, RTX 3090, or RTX 4090 GPUs (all have 24GB VRAM)
  • workers=3: Allows up to 3 parallel workers for concurrent requests
  • idle_timeout=600: Keeps workers active for 10 minutes after last use (reduces cold starts)
GPT-2 only requires about 2GB of VRAM, so 24GB GPUs are more than sufficient. For larger models like LLaMA or GPT-J, you might need 48GB or 80GB GPUs.
This function:
  • Loads the GPT-2 model from Hugging Face.
  • Moves the model to the GPU.
  • Tokenizes the input prompt.
  • Generates text from the prompt.
  • Decodes the generated tokens back to text.
  • Returns the generated text and other metadata.
Expand this section for a full breakdown:
Dependencies: The function requires three packages:
  • transformers: Hugging Face library for language models
  • torch: PyTorch for GPU computation
  • accelerate: Helper library for loading large models efficiently
Model loading:
These lines download and load the GPT-2 model from Hugging Face Hub. The first time this runs, it downloads ~500MB of model weights. Subsequent runs use the cached version.GPU acceleration:
This moves the model to GPU for faster inference. On Runpod workers, torch.cuda.is_available() returns True.Tokenization:
Converts your text prompt into token IDs that the model understands. The .to(device) moves these tokens to GPU memory.Generation parameters:
  • max_length=50: Maximum number of tokens to generate
  • temperature=0.7: Controls randomness (0.0 = deterministic, 1.0+ = very random)
  • do_sample=True: Use sampling instead of greedy decoding for more diverse outputs
  • num_return_sequences=1: Generate one completion per prompt
No gradient tracking:
Disables gradient computation, reducing memory usage and speeding up inference.

Step 6: Add the main function

Create the main function to test your text generator:
This main function:
  • Calls the remote function with await (runs asynchronously).
  • Waits for the GPU worker to complete text generation.
  • Displays the results in a formatted output.

Step 7: Run your first generation

Run the application:
First run output (takes 60-90 seconds):
Subsequent runs (takes 2-5 seconds):
Notice the dramatic speed improvement on subsequent runs—the endpoint is already provisioned, dependencies are installed, and the model is cached.

Step 8: Experiment with different prompts

Modify the main function to try different prompts:
Run it again:
You’ll see three different completions generated sequentially on the same GPU worker.

Troubleshooting

Model download fails

Issue: Error: Failed to download model from Hugging Face. Solutions:
  1. Check internet connectivity from workers (rare issue on Runpod).
  2. Try a different model that might be available faster.
  3. Increase execution timeout in configuration:

Out of memory error

Issue: RuntimeError: CUDA out of memory. Solutions:
  1. Use smaller models (GPT-2 instead of GPT-2 Large).
  2. Reduce max_length parameter.
  3. Use larger GPUs:

Slow generation

Issue: Text generation takes >30 seconds per request. Possible causes:
  1. Worker scaled down (cold start).
  2. Model not cached.
  3. Large max_length value.
Solutions:
  1. Increase idle_timeout to keep workers active:
  2. Set workers=(1, 3) to always have a warm worker ready.
  3. Reduce max_length to generate fewer tokens.

Generation quality is poor

Issue: Generated text is incoherent or repetitive. Solutions:
  1. Adjust temperature (try 0.7-0.9)
  2. Add top_p and top_k sampling:
  3. Try a larger model (GPT-2 Medium or Large).

Next steps

Now that you’ve built a text generation script with Flash, you can:

Explore other models

Try different models from Hugging Face:

Build a chat interface

Extend your app to handle multi-turn conversations:

Deploy as a Flash app

Convert your script to a production Flash app:
When deploying queue-based functions with flash deploy, each function must have its own unique endpoint configuration. If your script has multiple functions sharing the same config (like generate_text and chat in this tutorial), create separate endpoints for each function when converting to a Flash app. See understanding endpoint architecture for details.

Optimize performance

Last modified on October 2, 2026