Metadata-Version: 2.4
Name: libsdca
Version: 0.4.1
Summary: Accurate and fast classification losses for SDCA and neural networks: cross-entropy, top-k, multiclass and multilabel SVM, and distillation losses as fused PyTorch and JAX layers and SDCA solvers
Author: Max Lapin
License-Expression: MIT
Project-URL: Homepage, https://mlapin.github.io/libsdca/
Project-URL: Technical Report, https://mlapin.github.io/libsdca/report/
Project-URL: Documentation, https://github.com/mlapin/libsdca#documentation
Project-URL: Source, https://github.com/mlapin/libsdca
Project-URL: Changelog, https://github.com/mlapin/libsdca/blob/v0.4.1/CHANGELOG.md
Project-URL: Issues, https://github.com/mlapin/libsdca/issues
Keywords: sdca,stochastic dual coordinate ascent,svm,support vector machine,multiclass svm,linear svm,kernel svm,top-k,top-k svm,hinge loss,softmax,cross-entropy,log-softmax,softmax collapse,softmax cancellation,numerically stable cross-entropy,numerical stability,knowledge distillation,multilabel,loss functions,classification,machine learning,optimization,scikit-learn,pytorch,jax,cuda,triton,liblinear,libsvm
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: GPU :: NVIDIA CUDA
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: MacOS
Classifier: Operating System :: POSIX :: Linux
Classifier: Programming Language :: C++
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Scientific/Engineering :: Mathematics
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Typing :: Typed
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy>=1.23
Requires-Dist: scipy-openblas32>=0.3.34.481.4
Provides-Extra: torch
Requires-Dist: torch>=2.1; extra == "torch"
Requires-Dist: setuptools<81,>=77; extra == "torch"
Requires-Dist: ziglang<0.18,>=0.13; sys_platform == "linux" and extra == "torch"
Provides-Extra: jax
Requires-Dist: jax>=0.4.35; extra == "jax"
Provides-Extra: jax-cuda
Requires-Dist: jax[cuda13]>=0.7.2; sys_platform == "linux" and extra == "jax-cuda"
Requires-Dist: jax>=0.4.35; sys_platform != "linux" and extra == "jax-cuda"
Requires-Dist: triton>=3.7; sys_platform == "linux" and extra == "jax-cuda"
Requires-Dist: jax-triton>=0.5; (sys_platform == "linux" and python_version >= "3.12") and extra == "jax-cuda"
Provides-Extra: sklearn
Requires-Dist: scikit-learn>=1.6; extra == "sklearn"
Provides-Extra: test
Requires-Dist: pytest>=9; extra == "test"
Requires-Dist: pytest-xdist>=3.6; extra == "test"
Requires-Dist: scipy; extra == "test"
Requires-Dist: mpmath; extra == "test"
Provides-Extra: test-torch
Requires-Dist: pytest>=9; extra == "test-torch"
Requires-Dist: pytest-xdist>=3.6; extra == "test-torch"
Requires-Dist: scipy; extra == "test-torch"
Requires-Dist: mpmath; extra == "test-torch"
Requires-Dist: hypothesis>=6.136; extra == "test-torch"
Requires-Dist: torch>=2.1; extra == "test-torch"
Requires-Dist: setuptools<81,>=77; extra == "test-torch"
Requires-Dist: scikit-learn>=1.6; extra == "test-torch"
Provides-Extra: test-jax
Requires-Dist: pytest>=9; extra == "test-jax"
Requires-Dist: pytest-xdist>=3.6; extra == "test-jax"
Requires-Dist: scipy; extra == "test-jax"
Requires-Dist: mpmath; extra == "test-jax"
Requires-Dist: hypothesis>=6.136; extra == "test-jax"
Requires-Dist: jax>=0.4.35; extra == "test-jax"
Requires-Dist: optax>=0.2.9; extra == "test-jax"
Provides-Extra: coverage
Requires-Dist: coverage[toml]>=7; extra == "coverage"
Dynamic: license-file
Dynamic: requires-dist

# libsdca for Python

[Documentation](https://github.com/mlapin/libsdca/blob/v0.4.1/docs/README.md) ·
[Shared reference](https://github.com/mlapin/libsdca/blob/v0.4.1/docs/API.md)

An accurate and fast implementation of classification losses: cross-entropy, multiclass and
multilabel SVM hinges, top-*k* losses and knowledge distillation. The losses come as PyTorch
and JAX loss layers and as NumPy functions. The package also has SDCA solvers for linear and
kernel classifiers, with a scikit-learn estimator
([project page](https://mlapin.github.io/libsdca/),
[technical report](https://mlapin.github.io/libsdca/report/)).

Prebuilt wheels are available for Linux `x86_64` and macOS `arm64`. The NumPy interface
needs no compiler. It requires NumPy and, on Linux, `scipy-openblas32`, which `pip` installs
as a dependency. PyTorch, scikit-learn and JAX are optional.

## Installation

```bash
pip install libsdca              # NumPy interface
pip install 'libsdca[sklearn]'   # scikit-learn estimator SDCAClassifier
pip install 'libsdca[torch]'     # PyTorch loss layers
pip install 'libsdca[jax]'       # JAX CPU loss functions
pip install 'libsdca[jax-cuda]'  # JAX CUDA losses (Linux, NVIDIA driver 580+)
```

| Task | Entry point | Described in |
| --- | --- | --- |
| Train a scikit-learn classifier | `libsdca.sklearn.SDCAClassifier` | [Classifier](https://github.com/mlapin/libsdca/blob/v0.4.1/docs/python/classifier.md) |
| Train with full control (warm starts, label sets, target probabilities, history) | `libsdca.fit`, which returns a `Model` | [Training](https://github.com/mlapin/libsdca/blob/v0.4.1/docs/python/training.md) |
| Evaluate losses and gradients on NumPy arrays | `libsdca.loss_and_grad` | [Losses and gradients](https://github.com/mlapin/libsdca/blob/v0.4.1/docs/python/losses-and-gradients.md) |
| Take a single SDCA step on the dual variables | `libsdca.coordinate_update` | [Losses and gradients](https://github.com/mlapin/libsdca/blob/v0.4.1/docs/python/losses-and-gradients.md#coordinate_update) |
| Use the losses as PyTorch layers | `libsdca.torch.CrossEntropyLoss`, `TopKCrossEntropyLoss`, ... | [PyTorch guide](https://github.com/mlapin/libsdca/blob/v0.4.1/docs/TORCH.md) |
| Use the losses in JAX with `jit`, `grad` and `vmap` | `libsdca.jax.cross_entropy`, `topk_hinge_loss`, ... | [JAX guide](https://github.com/mlapin/libsdca/blob/v0.4.1/docs/JAX.md) |

## Quick start

```python
import numpy as np
import libsdca

X = np.array([[1., 0.], [0., 1.], [-1., -1.]])
y = np.array([0, 1, 2])
model = libsdca.fit(X, y)
print(model.status, model.predict(X))
```

Only `model.status == "solved"` guarantees the requested duality gap.

`libsdca.torch.CrossEntropyLoss` takes the arguments of `torch.nn.CrossEntropyLoss`, and the
package adds the other losses
([PyTorch guide](https://github.com/mlapin/libsdca/blob/v0.4.1/docs/TORCH.md)):

```python
import torch
from libsdca.torch import CrossEntropyLoss, TopKHingeLoss

logits = torch.randn(32, 5, requires_grad=True)
target = torch.randint(5, (32,))
(CrossEntropyLoss()(logits, target) + TopKHingeLoss(k=2)(logits, target)).backward()
```

## NumPy and scikit-learn pages

| Page | Covers |
| --- | --- |
| [Loss objects and targets](https://github.com/mlapin/libsdca/blob/v0.4.1/docs/python/losses.md) | The loss objects, their parameters, and the target that each loss takes |
| [scikit-learn classifier](https://github.com/mlapin/libsdca/blob/v0.4.1/docs/python/classifier.md) | `SDCAClassifier`: parameters, fitted attributes and methods |
| [Training with `fit`](https://github.com/mlapin/libsdca/blob/v0.4.1/docs/python/training.md) | `libsdca.fit`: options, kernels, label sets, warm starts and the returned `Model` |
| [Losses and gradients on arrays](https://github.com/mlapin/libsdca/blob/v0.4.1/docs/python/losses-and-gradients.md) | `loss_and_grad` and `coordinate_update`, and the errors of the NumPy interface |
| [Precision](https://github.com/mlapin/libsdca/blob/v0.4.1/docs/python/precision.md) | Input dtypes and the `precision` option |

The index of all pages, including the PyTorch and JAX guides, is in the
[documentation](https://github.com/mlapin/libsdca/blob/v0.4.1/docs/README.md).

## Building from source

```bash
pip install .            # links a system CBLAS if one is found, else the built-in products
```

`SDCA_BLAS_BACKEND` selects the BLAS of the build: `AUTO`, `EXTERNAL` or `BUILTIN`. The
commands are in
[DEPENDENCIES.md](https://github.com/mlapin/libsdca/blob/v0.4.1/DEPENDENCIES.md#python).
A build that finds no BLAS uses the built-in products, and the first `fit` of a process
warns about it with a `RuntimeWarning`
([BLAS](https://github.com/mlapin/libsdca/blob/v0.4.1/docs/notes/blas.md)).
