AI Model Wiki

    Skip to main content
    25 min
    Lesson 1 of 4

    Inference Optimization Techniques

    From the course AI Model Serving at Scale

    Optimize models for low-latency, high-throughput inference at production scale.

    Model Optimization

    Quantization

    from transformers import AutoModelForCausalLM
    import torch
    
    # INT8 quantization for 4x speedup
    model = AutoModelForCausalLM.from_pretrained(
        "meta-llama/Llama-3-8b",
        load_in_8bit=True,  # Automatic INT8 quantization
        device_map="auto"
    )
    
    # Custom quantization with GPTQ
    from auto_gptq import AutoGPTQForCausalLM
    
    model = AutoGPTQForCausalLM.from_quantized(
        "TheBloke/Llama-3-8B-GPTQ",
        use_safetensors=True,
        device="cuda:0"
    )
    

    TensorRT Compilation

    import tensorrt as trt
    import torch_tensorrt
    
    # Compile PyTorch model to TensorRT
    trt_model = torch_tensorrt.compile(
        model,
        inputs=[torch_tensorrt.Input(
            shape=[1, 512],  # batch_size, seq_len
            dtype=torch.long
        )],
        enabled_precisions={torch.float16},
        workspace_size=1 << 30  # 1GB workspace
    )
    
    # 2-5x inference speedup
    output = trt_model(input_ids)
    

    Batching Strategies

    Dynamic Batching

    class DynamicBatcher:
        def __init__(self, model, max_batch_size=32, max_wait_ms=10):
            self.model = model
            self.max_batch_size = max_batch_size
            self.max_wait_ms = max_wait_ms
            self.queue = []
        
        async def infer(self, input_data):
            # Add to queue
            future = asyncio.Future()
            self.queue.append((input_data, future))
            
            # Wait for batch or timeout
            await asyncio.sleep(self.max_wait_ms / 1000)
            
            # Process batch
            if len(self.queue) >= self.max_batch_size:
                await self._process_batch()
            
            return await future
        
        async def _process_batch(self):
            batch = self.queue[:self.max_batch_size]
            self.queue = self.queue[self.max_batch_size:]
            
            inputs = [item[0] for item in batch]
            futures = [item[1] for item in batch]
            
            # Batch inference
            outputs = self.model(inputs)
            
            # Resolve futures
            for future, output in zip(futures, outputs):
                future.set_result(output)
    

    Key Takeaways

    • •Quantization to INT8/INT4 reduces memory and increases throughput by 2-4x
    • •TensorRT compilation optimizes inference with kernel fusion and precision tuning
    • •Dynamic batching increases throughput by processing multiple requests together
    • •Continuous batching maximizes GPU utilization for variable-length requests
    0 of 4 lessons marked complete

    Progress is kept in this browser only.