liveBook · Manning

Chapter 4: Running inference

What you will learn in chapter 4 of Domain-Specific Small Language Models

This chapter introduction previews content generation with SLMs, inference cost estimation, and performance and cost improvements.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This chapter introduction previews content generation with SLMs, inference cost estimation, and performance and cost improvements.

4.1 How to generate content

This section introduces GPT-Neo as a reference model and explains why the chapter’s examples extend beyond it.

Key terms: GPT-Neo, GPT-3 architecture, Hugging Face Transformers, Accelerate, domain-specific models

4.1.1 Text completion

This section shows how to load GPT-Neo for text completion, check its memory and device placement, and generate completions for one or more prompts.

Key terms: GPTNeoForCausalLM, GPT2Tokenizer, device_map, auto, Accelerate, offloads, accelerate estimate-memory, Total Size, float32, hf_device_map

4.1.2 Few-shot learning

This section introduces few-shot learning as using examples in a prompt to guide a GPT-based model on new inputs.

Key terms: few-shot learning, prompt, few-shot text classification

4.1.3 Code generation

This section shows that a language model can generate code and explains why its output still needs review and model customization.

Key terms: code generation, Pile dataset, max_length, developer review, lint, fine-tuning, LoRA, RAG, hallucinations

4.1.4 Evaluating the generated content

This section explains why to evaluate generated content before deployment and how to use LM-Eval to run a task-based evaluation.

Key terms: LLM evaluation, output quality, Evaluation Harness, LM-Eval, custom tasks, task versioning, main.py, model, model_args, pretrained

4.2 Calculating inference cost

This section shows how to estimate GPU memory for LLM inference and identifies serving-performance measures beyond model size.

Key terms: trainable parameters, in-memory size, precision, FP32, FP16, INT8, mixed precision, hardware constraints, GPU power utilization, time to first token

4.3 Areas for improvement (cost savings and performance)

This introduction connects inference-serving metrics to finding cost and performance improvements before the chapter examines specific approaches.

Key terms: metrics, inference serving, computational power

4.3.1 Getting the most from your GPU

This section uses GPU specifications and attention arithmetic intensity to diagnose GPT-Neo inference performance, and explains why KV caching speeds up token generation.

Key terms: compute throughput, memory, memory bandwidth, ops/byte ratio, prefill, autoregressive sampling, key–value (KV) cache, use_cache, arithmetic intensity, memory bound

4.3.2 Batching

This section explains how batching improves LLM inference throughput, how GPU memory limits batch size, and when the added latency makes batching unsuitable.

Key terms: static batching, dynamic batching, arithmetic intensity, memory-bound, GPU memory, VRAM, KV cache, prefill phase, sequence length, throughput

4.3.3 Estimating the generation time

This section estimates LLM generation time from GPU memory bandwidth and prefill time, then identifies limits of the estimate and possible inference improvements.

Key terms: trainable parameters, bytes per parameter, memory bandwidth, full precision, prefill phase, forward pass, generation time, overhead, operator fusion, quantization

4.3.4 Optimizing GPU use with DeepSpeed

This section shows how to run GPT-Neo inference with DeepSpeed and evaluate its latency benefit against a possible accuracy tradeoff.

Key terms: DeepSpeed, model parallelism, inference-specific kernels, kernel injection, GPT-Neo, half precision, CUDA, replace_method, replace_with_kernel_inject, latency

4.4 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 3: End-to-end transformer fine-tuning Chapter 5: Exploring ONNX →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.