Skip to main content

Overview

llama.cpp includes an extensive test suite covering unit tests, integration tests, and backend-specific tests. This guide covers how to build, run, and debug tests effectively.
Before submitting a pull request, you should execute the full CI locally to ensure your changes don’t break existing functionality.

Quick Start

Build and Run All Tests

Run Specific Tests

Test Categories

llama.cpp has several categories of tests:
C++ Unit Tests - Test individual components and functionsExamples:
  • test-tokenizer-0 - Tokenizer validation
  • test-sampling - Sampling algorithms
  • test-grammar-parser - Grammar parsing
  • test-arg-parser - Command-line argument parsing
  • test-rope - Rotary position embeddings
  • test-quantize-fns - Quantization functions
Location: tests/test-*.cpp
Backend Ops Tests - Verify consistency across different backends (CPU, CUDA, Metal, etc.)The test-backend-ops tool checks that different backend implementations of ggml operators produce consistent results.
This test requires access to at least two different ggml backends to verify consistency.
Python-based Server Tests - Test the HTTP API server using pytestLocation: tools/server/tests/See Server Testing section for details.
End-to-End Tests - Test complete workflows with real modelsExamples:
  • test-chat - Chat template functionality
  • test-chat-template - Chat template parsing
  • test-llama-archs - Model architecture loading
  • test-thread-safety - Multi-threaded inference

Running the Full CI Locally

Before submitting a PR, execute the full CI locally:
The CI runs comprehensive tests on different hardware configurations. Running it locally helps catch issues before submitting your PR.

Testing Modified Code

Testing ggml Modifications

If you modified the ggml source, you must run test-backend-ops:
1

Build with multiple backends

2

Run backend operations test

This verifies that different backends produce consistent results for ggml operations.
3

Add test cases for new operators

If you added a new ggml operator, add corresponding test cases to tests/test-backend-ops.cpp:

Testing Performance Impact

Verify your changes don’t negatively impact performance:

Testing Perplexity

Ensure your changes don’t affect model quality:

Debugging Tests

Using the debug-test.sh Script

The scripts/debug-test.sh script provides an easy way to debug specific tests:

Manual Debugging Process

For more control, follow these steps:
1

Create debug build directory

2

Configure with debug symbols

3

Build test binaries

4

Find test commands

This outputs test commands like:
5

Run with GDB

In GDB:

Debugging with Valgrind

Server Testing

The server has its own comprehensive test suite using Python and pytest.

Setup Server Tests

1

Install dependencies

2

Build the server

3

Run tests

Server Test Configuration

Environment variables for customizing server tests:

Running Specific Server Tests

Debugging Server Tests

Debug the server while running tests:
The DEBUG_EXTERNAL=1 environment variable tells the test suite to connect to an externally-started server instead of spawning its own.

Test Structure and CMake

Understanding Test Registration

Tests are registered in tests/CMakeLists.txt using helper functions:

Adding a New Test

1

Create test source file

2

Register in CMakeLists.txt

3

Build and run

Common Test Patterns

Testing with Models

Many tests require model files:

Assertion Helpers

Use the testing helpers from tests/testing.h:

Continuous Integration

llama.cpp uses GitHub Actions for CI/CD. The CI runs:
  • Unit tests on multiple platforms (Linux, macOS, Windows)
  • Backend-specific tests (CUDA, Metal, SYCL)
  • Integration tests with real models
  • Performance benchmarks
  • Code style checks
Some tests are disabled on certain platforms. Check .github/workflows/ for platform-specific configurations.

Best Practices

Test Before Submitting

Always run the full CI locally before opening a PR to catch issues early.

Add Tests for New Features

Every new feature should include corresponding tests to prevent regressions.

Test Multiple Backends

If modifying ggml operations, test on CPU, CUDA, and Metal backends.

Check Performance

Use llama-bench and llama-perplexity to verify no performance degradation.

Troubleshooting

Test Failures

Tests fail on CI but pass locally
  • Ensure you’re testing the same commit
  • Check if it’s a platform-specific issue
  • Verify model files are the same version
Timeout errors
  • Increase test timeout in CMakeLists.txt
  • Check for infinite loops or deadlocks
  • Run with smaller models for unit tests
Flaky tests
  • Check for race conditions in multi-threaded code
  • Ensure tests don’t depend on external state
  • Use fixed random seeds for reproducibility

Getting Help

Next Steps

Contributing

Learn the full contribution workflow and guidelines

Adding Models

Understand how to add new model architectures