liveBook · Manning

Code compilation

As explained in LLMs in Production

Compilation is the transformation of high-level program or model code into lower-level code optimized for execution on target hardware.

Compilation lowers high-level model code into machine-level code that better matches target hardware, improving inference speed and potentially reducing cost. A practical pipeline converts PyTorch or TensorFlow models into an intermediate representation such as TorchScript, MLIR, or ONNX, then generates hardware-specific code for GPUs, TPUs, or CPUs. section 6.1.1

The intermediate representation separates framework tooling from hardware-specific compilation:

  • Framework developers provide conversion into shared intermediate representations. section 6.1.1
  • Hardware vendors provide compilation from those representations to their processors. section 6.1.1
  • Existing tools such as llama.cpp and pre-quantized model formats reduce repeated optimization work. section 6.1.1

TensorRT compilation

A TensorRT workflow loads GPT-2 with PyTorch, moves it to CUDA, traces it with TorchScript, and supplies input shapes and tensor types for compilation. Inputs can use static dimensions or bounded dynamic shapes. section 6.1.1

Figure 6.2 An example of an unoptimized network compared to the same network optimized using graph optimization. CBR is a NVIDIA fused layer kernel that simply stands for Convolution, Bias, and ReLU. See the following NVIDIA blog post for reference: https://mng.bz/PNvw.
figure

Graph optimization can replace an unoptimized network with fused operations such as NVIDIA's convolution-bias-ReLU kernel. section 6.1.1

How LLMs in Production covers it

How deep it goes
Explains the framework-to-intermediate-representation-to-machine-code pipeline and demonstrates TensorRT configuration.
Examples
Uses GPT-2 TensorRT tracing, ONNX export commands, llama.cpp, and graph-optimized fused kernels.
What it assumes
Assumes familiarity with high-level model frameworks and hardware targets while explaining intermediate representations from first principles.
What you can do after
Apply it via TensorRT or ONNX export tooling to produce hardware-oriented model artifacts.
How it connects
Connects PyTorch, TensorFlow, TorchScript, MLIR, ONNX, TensorRT, quantization, and hardware-specific inference.

Also explained in

Compare how 7 books explain code compilation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of LLMs in Production. The section links point to the parts of the book it draws on.