This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
pip install -e .- Install package in development modepython test_batch_invariance.py- Run batch invariance test comparing standard PyTorch vs batch-invariant operationspython deterministic_vllm_inference.py- Test deterministic inference with vLLM (requires vLLM server running)
black .- Format code with Black (line length 100)isort .- Sort importsflake8- Run lintingpytest- Run tests (when available)
batch_invariant_ops.py - Main implementation containing:
- Triton kernels for batch-invariant operations (matmul, log_softmax, mean)
- PyTorch library overrides using
torch.library.Library - Context manager
set_batch_invariant_mode()for enabling/disabling batch-invariant behavior
Kernel Replacement Strategy: Uses torch.library.Library to override CUDA implementations of specific PyTorch operations (mm, addmm, _log_softmax, mean.dim) with Triton-based batch-invariant versions.
Context Management: The library provides both context manager (set_batch_invariant_mode()) and explicit enable/disable functions for controlling when batch-invariant operations are active.
Triton Implementation: All custom kernels use Triton for GPU acceleration with deterministic execution patterns that ensure batch size doesn't affect individual element computations.
- Matrix multiplication (
torch.mm,torch.addmm) - Uses persistent matmul kernel - Log softmax (
torch.log_softmax) - Custom implementation along last dimension - Mean reduction (
torch.mean) - Supports single and multiple dimension reduction
The library validates batch invariance by comparing:
- Small batch computation (e.g., single row matrix multiplication)
- Large batch computation with slicing (e.g., full matrix multiplication, then slice)
These should produce identical results with batch-invariant mode enabled, but may differ with standard PyTorch due to floating-point precision and execution order differences.