TabPFN Documentation

repository·main·Indexed 27 days ago

https://github.com/priorlabs/tabpfn

TabPFN is a tabular foundation model for fast, high-performance classification and regression on small to medium-sized datasets, performing inference in a single forward pass. It provides TabPFNClassifier and TabPFNRegressor, supports model versions including TabPFN-3, and offers extensions for interpretability, unsupervised learning, and embeddings. The library requires Python 3.10+ and supports GPU acceleration, including FlashAttention-3 for Hopper-class GPUs.

Tokens
4.1K
Snippets
10
Records
29
Agent score
93%

What's inside TabPFN

  1. Cross-compile FlashAttention-3 for a non-Hopper node

    main

    If you are on a CPU-only node or a node without a Hopper GPU, you can build a transferable .whl file to be installed on an H100 node later.

    Requirements for compatibility between build and runtime nodes:

    • CUDA toolkit major version: Must be $\ge$ 12.3 (12.4+ recommended). The build node needs the toolkit; the H100 node only needs a compatible driver.
    • PyTorch & Python versions: The build must use the same torch wheel and Python minor version as the runtime environment.
    • glibc: Ensure build and runtime nodes share a compatible glibc version (ideally the same OS image).

    Build process: Set TORCH_CUDA_ARCH_LIST="9.0a" to target the Hopper architecture explicitly and adjust MAX_JOBS based on available RAM (approximately 32 GB is required).

    Installation on the H100 node: Once the wheel is generated, transfer it to the H100 node and install it using pip.

    # On the build node
    export TORCH_CUDA_ARCH_LIST="9.0a"
    export MAX_JOBS=4
    
    git clone https://github.com/Dao-AILab/flash-attention.git
    cd flash-attention/hopper
    pip wheel . --no-build-isolation -w /tmp/fa3-wheel/
    
    # On the H100 node
    pip install /tmp/fa3-wheel/flash_attn_3-*.whl
    python -c "from flash_attn_interface import flash_attn_func; print('ok')"
  2. Optimize TabPFN performance and usage

    main

    Follow these best practices for efficient TabPFN usage:

    • Batch Prediction: Always use batch prediction. Calling .predict() on individual samples is significantly slower than calling it once on a large array. If the test set is too large, split it into chunks (e.g., 1000 samples each).
    • Avoid Preprocessing: Do not apply data scaling or one-hot encoding before feeding data to the model.
    • Dataset Size Limits: TabPFN-3 supports up to:
      • 1,000,000 rows × 200 features
      • 100,000 rows × 2,000 features
      • 1,000 rows × 20,000 features (Note: Higher feature counts reduce the supported row capacity).
  3. Install FlashAttention-3 on a Hopper machine

    main

    Since FA3 is not available on PyPI, you must build it from source. To perform an in-place installation directly on a Hopper-class machine, clone the repository and run the setup script.

    Verification: After installation, verify the package is working by attempting to import flash_attn_func from flash_attn_interface.

    git clone https://github.com/Dao-AILab/flash-attention.git
    cd flash-attention/hopper
    python setup.py install
    from flash_attn_interface import flash_attn_func  # noqa: F401
  4. Install TabPFN

    main

    Install the core TabPFN package using pip. TabPFN requires Python 3.10 or higher. The installation automatically includes a compatible PyTorch build.

    Note for Apple Silicon/MPS users: For optimal performance with flash attention, consider installing a PyTorch version later than 2.13.0.dev20260510 to avoid GPU-CPU-GPU roundtrips required by MLX.

    pip install tabpfn
  5. Use TabPFN in offline environments

    main

    TabPFN requires model weights which are normally downloaded automatically. For offline use, you can use the provided script or manual download.

    Using the Download Script

    If you have the TabPFN repository, run:

    python scripts/download_all_models.py

    This downloads main classifiers, regressors, and ensemble variants to your default cache.

    Manual Download

    1. Download the .ckpt files for the Classifier or Regressor from HuggingFace.
    2. Use one of these methods to make them available:
      • Pass the path directly: TabPFNClassifier(model_path="/path/to/model.ckpt")
      • Set the TABPFN_MODEL_CACHE_DIR environment variable.
      • Place files in the default OS cache directory:
        • Windows: %APPDATA%\tabpfn\
        • macOS: ~/Library/Caches/tabpfn/
        • Linux: ~/.cache/tabpfn/
    # After installing TabPFN
    python scripts/download_all_models.py
  6. Configure FlashAttention-3 (Hopper) backend

    main

    TabPFN v3 can dispatch attention operations to the FlashAttention-3 (FA3) backend instead of PyTorch's SDPA for improved performance on Hopper-class GPUs (H100, H200).

    FA3 is automatically used only when all of the following conditions are met:

    • The flash_attn_interface Python package is installed and importable.
    • The attention is performed on a CUDA tensor on a Hopper-class device (compute capability 9.0+).
    • The data type is torch.float16 or torch.bfloat16.
    • The head dimension is one of {64, 96, 128, 192, 256}.
    • The sequence length (max(seq_q, seq_kv)) is greater than or equal to the threshold _FA3_MIN_SEQLEN_FOR_SPEEDUP (currently set to 10_000).
  7. Use TabPFN for Classification and Regression

    main

    Use TabPFNClassifier for classification tasks and TabPFNRegressor for regression tasks. The default model is TabPFN-3. Note that the model checkpoint is downloaded automatically upon the first call to .fit().

    Hardware Recommendations:

    • GPU: Highly recommended. Even older GPUs with ~8GB VRAM work well. 16GB is recommended for larger datasets.
    • CPU: Only feasible for small datasets (≲1000 samples).
    from tabpfn import TabPFNClassifier, TabPFNRegressor
    
    # Classification
    clf = TabPFNClassifier()
    clf.fit(X_train, y_train)  # downloads checkpoint on first use
    predictions = clf.predict(X_test)
    
    # Regression
    reg = TabPFNRegressor()
    reg.fit(X_train, y_train)  # downloads checkpoint on first use
    predictions = reg.predict(X_test)
  8. Install TabPFN Extensions

    main

    Install tabpfn-extensions to access additional tools including:

    • interpretability: SHAP-based explanations and feature importance.
    • unsupervised: Outlier detection and synthetic data generation.
    • embeddings: Extraction of internal learned embeddings.
    • many_class: Handling multi-class problems exceeding built-in class limits.
    pip install tabpfn-extensions
  9. Configure TabPFN authentication and environment variables

    main

    TabPFN uses Pydantic settings for configuration via environment variables or .env files.

    Authentication

    • TABPFN_TOKEN: Provide a PriorLabs authentication token (obtain from https://ux.priorlabs.ai). Essential for headless/CI environments.
    • TABPFN_NO_BROWSER: Set to true to disable the automatic browser-based login flow.

    Model Configuration

    • TABPFN_MODEL_CACHE_DIR: Custom directory for caching downloaded models. Defaults to platform-specific cache directories.
    • TABPFN_ALLOW_CPU_LARGE_DATASET: Set to true to allow running TabPFN on CPU with datasets >1000 samples (Note: this is very slow).
    • TABPFN_MPS_MEMORY_FRACTION: Fraction of recommended max MPS memory for Apple Silicon (default: 0.7). Set this before importing TabPFN.
    • TABPFN_MAX_BATCHED_TEST_ROWS: Maximum test rows per forward pass during fit_mode="fit_with_cache" inference (default: 32768). Set to 0 to disable chunking.

    PyTorch Settings

    • PYTORCH_CUDA_ALLOC_CONF: Configure PyTorch CUDA memory allocation (e.g., max_split_size_mb:512).
    export TABPFN_MODEL_CACHE_DIR="/path/to/models"
    export TABPFN_ALLOW_CPU_LARGE_DATASET=true
    export PYTORCH_CUDA_ALLOC_CONF="max_split_size_mb:512"
  10. Save and load trained TabPFN models

    main

    To persist a fitted estimator (including its state) and reload it later, use save_fitted_tabpfn_model and load_fitted_tabpfn_model from tabpfn.model_loading.

    To store only the foundation model weights (without the fitted state) to create fresh estimators later, use save_tabpfn_model and reload with load_model_criterion_config.

    from tabpfn import TabPFNRegressor
    from tabpfn.model_loading import (
        load_fitted_tabpfn_model,
        save_fitted_tabpfn_model,
    )
    
    # Train the regressor on GPU
    reg = TabPFNRegressor(device="cuda")
    reg.fit(X_train, y_train)
    save_fitted_tabpfn_model(reg, "my_reg.tabpfn_fit")
    
    # Later or on a CPU-only machine
    reg_cpu = load_fitted_tabpfn_model("my_reg.tabpfn_fit", device="cpu")
  11. Use specific TabPFN model versions

    main

    To use model versions other than the default TabPFN-3 (such as TabPFN-2.6 or the original TabPFN-2), use the create_default_for_version method with a ModelVersion constant.

    Note on Licensing: TabPFN-3 and prior releases (2.5, 2.6) use non-commercial licenses. The code and TabPFN-2 weights are under the Prior Labs License (Apache 2.0 with additional attribution).

    from tabpfn import TabPFNClassifier, TabPFNRegressor
    from tabpfn.constants import ModelVersion
    
    # Use TabPFN-2.6
    classifier = TabPFNClassifier.create_default_for_version(ModelVersion.V2_6)
    regressor = TabPFNRegressor.create_default_for_version(ModelVersion.V2_6)
    
    # Use TabPFN-2 (Apache 2.0 compatible weights)
    tabpfn_v2 = TabPFNRegressor.create_default_for_version(ModelVersion.V2)
  12. Configure preprocessing with PreprocessorConfig

    main

    Use PreprocessorConfig to define the settings for your preprocessing pipeline. For specific model versions, you can use the following preset configurations:

    • v2_5_classifier_preprocessor_configs (Classifier)
    • v2_5_regressor_preprocessor_configs (Regressor)
    • v2_classifier_preprocessor_configs (Classifier)
    • v2_regressor_preprocessor_configs (Regressor)