libsdca 0.3.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- libsdca-0.3.0/AGENTS.md +56 -0
- libsdca-0.3.0/CHANGELOG.md +77 -0
- libsdca-0.3.0/DEPENDENCIES.md +91 -0
- libsdca-0.3.0/LICENSE +22 -0
- libsdca-0.3.0/MANIFEST.in +9 -0
- libsdca-0.3.0/PKG-INFO +205 -0
- libsdca-0.3.0/README.md +224 -0
- libsdca-0.3.0/include/sdca/c_api.h +361 -0
- libsdca-0.3.0/include/sdca/loss.h +637 -0
- libsdca-0.3.0/include/sdca/math/blas.h +298 -0
- libsdca-0.3.0/include/sdca/math/blas_builtin.h +255 -0
- libsdca-0.3.0/include/sdca/math/cblas.h +58 -0
- libsdca-0.3.0/include/sdca/math/compensated.h +126 -0
- libsdca-0.3.0/include/sdca/math/distillation.h +491 -0
- libsdca-0.3.0/include/sdca/math/entropy.h +1855 -0
- libsdca-0.3.0/include/sdca/math/exp_rows.h +382 -0
- libsdca-0.3.0/include/sdca/math/functor.h +58 -0
- libsdca-0.3.0/include/sdca/math/lambert.h +229 -0
- libsdca-0.3.0/include/sdca/math/log_exp.h +281 -0
- libsdca-0.3.0/include/sdca/math/prox_rows.h +396 -0
- libsdca-0.3.0/include/sdca/prox/entropy.h +290 -0
- libsdca-0.3.0/include/sdca/prox/entropy_norm.h +58 -0
- libsdca-0.3.0/include/sdca/prox/knapsack_eq.h +235 -0
- libsdca-0.3.0/include/sdca/prox/knapsack_le.h +108 -0
- libsdca-0.3.0/include/sdca/prox/knapsack_le_biased.h +223 -0
- libsdca-0.3.0/include/sdca/prox/proxdef.h +459 -0
- libsdca-0.3.0/include/sdca/prox/topk_cone.h +261 -0
- libsdca-0.3.0/include/sdca/prox/topk_cone_biased.h +71 -0
- libsdca-0.3.0/include/sdca/prox/topk_entropy.h +178 -0
- libsdca-0.3.0/include/sdca/prox/topk_entropy_biased.h +109 -0
- libsdca-0.3.0/include/sdca/prox/topk_simplex.h +95 -0
- libsdca-0.3.0/include/sdca/prox/topk_simplex_biased.h +100 -0
- libsdca-0.3.0/include/sdca/prox/two_entropy.h +111 -0
- libsdca-0.3.0/include/sdca/prox/two_simplex.h +394 -0
- libsdca-0.3.0/include/sdca/prox/two_simplex_sort.h +83 -0
- libsdca-0.3.0/include/sdca/prox.h +22 -0
- libsdca-0.3.0/include/sdca/solver/context.h +248 -0
- libsdca-0.3.0/include/sdca/solver/data/dataset.h +43 -0
- libsdca-0.3.0/include/sdca/solver/data/input.h +69 -0
- libsdca-0.3.0/include/sdca/solver/data/output.h +215 -0
- libsdca-0.3.0/include/sdca/solver/data/scratch.h +135 -0
- libsdca-0.3.0/include/sdca/solver/data.h +106 -0
- libsdca-0.3.0/include/sdca/solver/eval/accumulate.h +73 -0
- libsdca-0.3.0/include/sdca/solver/eval/core.h +123 -0
- libsdca-0.3.0/include/sdca/solver/eval/dual.h +46 -0
- libsdca-0.3.0/include/sdca/solver/eval/evaluate.h +586 -0
- libsdca-0.3.0/include/sdca/solver/eval/primal.h +114 -0
- libsdca-0.3.0/include/sdca/solver/eval/scores.h +86 -0
- libsdca-0.3.0/include/sdca/solver/eval/types.h +203 -0
- libsdca-0.3.0/include/sdca/solver/eval.h +81 -0
- libsdca-0.3.0/include/sdca/solver/objective/feasible_dual.h +116 -0
- libsdca-0.3.0/include/sdca/solver/objective/l2_distillation.h +218 -0
- libsdca-0.3.0/include/sdca/solver/objective/l2_entropy.h +71 -0
- libsdca-0.3.0/include/sdca/solver/objective/l2_entropy_topk.h +188 -0
- libsdca-0.3.0/include/sdca/solver/objective/l2_hinge_topk.h +194 -0
- libsdca-0.3.0/include/sdca/solver/objective/l2_multilabel_entropy.h +69 -0
- libsdca-0.3.0/include/sdca/solver/objective/l2_multilabel_hinge.h +213 -0
- libsdca-0.3.0/include/sdca/solver/objective/l2_topk_hinge.h +203 -0
- libsdca-0.3.0/include/sdca/solver/objective/objective_base.h +341 -0
- libsdca-0.3.0/include/sdca/solver/objective.h +103 -0
- libsdca-0.3.0/include/sdca/solver/reporting.h +89 -0
- libsdca-0.3.0/include/sdca/solver/screening.h +163 -0
- libsdca-0.3.0/include/sdca/solver/solverdef.h +127 -0
- libsdca-0.3.0/include/sdca/solver/update.h +245 -0
- libsdca-0.3.0/include/sdca/solver.h +352 -0
- libsdca-0.3.0/include/sdca/utility/fpenv.h +140 -0
- libsdca-0.3.0/include/sdca/utility/logging.cpp +15 -0
- libsdca-0.3.0/include/sdca/utility/logging.h +86 -0
- libsdca-0.3.0/include/sdca/utility/parallel.h +484 -0
- libsdca-0.3.0/include/sdca/utility/scratch.h +76 -0
- libsdca-0.3.0/include/sdca/utility/stopwatch.h +128 -0
- libsdca-0.3.0/include/sdca/utility/types.h +34 -0
- libsdca-0.3.0/pyproject.toml +163 -0
- libsdca-0.3.0/python/JAX.md +80 -0
- libsdca-0.3.0/python/README.md +142 -0
- libsdca-0.3.0/python/TORCH.md +195 -0
- libsdca-0.3.0/python/libsdca/__init__.py +142 -0
- libsdca-0.3.0/python/libsdca/_core.py +1884 -0
- libsdca-0.3.0/python/libsdca/_cpu.py +1110 -0
- libsdca-0.3.0/python/libsdca/_cpu_entropy.py +146 -0
- libsdca-0.3.0/python/libsdca/_cross_entropy.py +650 -0
- libsdca-0.3.0/python/libsdca/_cuda_launch.py +235 -0
- libsdca-0.3.0/python/libsdca/_entropy.py +268 -0
- libsdca-0.3.0/python/libsdca/_extensions.py +315 -0
- libsdca-0.3.0/python/libsdca/_first_order.py +50 -0
- libsdca-0.3.0/python/libsdca/_hinge.py +98 -0
- libsdca-0.3.0/python/libsdca/_jax_ffi.cpp +253 -0
- libsdca-0.3.0/python/libsdca/_jax_ffi.py +221 -0
- libsdca-0.3.0/python/libsdca/_jax_losses.py +1235 -0
- libsdca-0.3.0/python/libsdca/_linear_cross_entropy.py +1014 -0
- libsdca-0.3.0/python/libsdca/_linear_full_range.py +81 -0
- libsdca-0.3.0/python/libsdca/_multilabel.py +188 -0
- libsdca-0.3.0/python/libsdca/_ops.py +565 -0
- libsdca-0.3.0/python/libsdca/_tensor_losses.py +520 -0
- libsdca-0.3.0/python/libsdca/_tensor_prox.py +446 -0
- libsdca-0.3.0/python/libsdca/_topk.py +104 -0
- libsdca-0.3.0/python/libsdca/_torch_ce.h +832 -0
- libsdca-0.3.0/python/libsdca/_torch_cuda.h +771 -0
- libsdca-0.3.0/python/libsdca/_torch_distill.h +1627 -0
- libsdca-0.3.0/python/libsdca/_torch_distributed.py +76 -0
- libsdca-0.3.0/python/libsdca/_torch_entropy.cpp +427 -0
- libsdca-0.3.0/python/libsdca/_torch_lanes.h +1123 -0
- libsdca-0.3.0/python/libsdca/_torch_prox.h +1157 -0
- libsdca-0.3.0/python/libsdca/_torch_topk_hinge.h +1822 -0
- libsdca-0.3.0/python/libsdca/_triton_distillation.py +1715 -0
- libsdca-0.3.0/python/libsdca/_triton_entropy.py +601 -0
- libsdca-0.3.0/python/libsdca/_triton_hinge.py +363 -0
- libsdca-0.3.0/python/libsdca/_triton_linear.py +30 -0
- libsdca-0.3.0/python/libsdca/_triton_multilabel.py +1101 -0
- libsdca-0.3.0/python/libsdca/_triton_prox.py +866 -0
- libsdca-0.3.0/python/libsdca/_triton_reduce.py +187 -0
- libsdca-0.3.0/python/libsdca/_triton_topk.py +1070 -0
- libsdca-0.3.0/python/libsdca/_triton_weighted_entropy.py +388 -0
- libsdca-0.3.0/python/libsdca/jax.py +1335 -0
- libsdca-0.3.0/python/libsdca/py.typed +0 -0
- libsdca-0.3.0/python/libsdca/sklearn.py +205 -0
- libsdca-0.3.0/python/libsdca/torch.py +1978 -0
- libsdca-0.3.0/python/libsdca.egg-info/PKG-INFO +205 -0
- libsdca-0.3.0/python/libsdca.egg-info/SOURCES.txt +222 -0
- libsdca-0.3.0/python/libsdca.egg-info/dependency_links.txt +1 -0
- libsdca-0.3.0/python/libsdca.egg-info/requires.txt +36 -0
- libsdca-0.3.0/python/libsdca.egg-info/top_level.txt +1 -0
- libsdca-0.3.0/sdca_build.py +63 -0
- libsdca-0.3.0/setup.cfg +4 -0
- libsdca-0.3.0/setup.py +73 -0
- libsdca-0.3.0/src/c_api.cpp +1241 -0
- libsdca-0.3.0/src/tensor_losses.h +1339 -0
- libsdca-0.3.0/tests/python/child_process.py +34 -0
- libsdca-0.3.0/tests/python/common_inputs.py +760 -0
- libsdca-0.3.0/tests/python/conftest.py +179 -0
- libsdca-0.3.0/tests/python/interpreter.py +89 -0
- libsdca-0.3.0/tests/python/loss_references.py +200 -0
- libsdca-0.3.0/tests/python/mpmath_types.py +28 -0
- libsdca-0.3.0/tests/python/test_abi_compatibility.py +106 -0
- libsdca-0.3.0/tests/python/test_accuracy_claims.py +43 -0
- libsdca-0.3.0/tests/python/test_api_contracts.py +468 -0
- libsdca-0.3.0/tests/python/test_backend_routing.py +189 -0
- libsdca-0.3.0/tests/python/test_build_config.py +261 -0
- libsdca-0.3.0/tests/python/test_centered_certificate.py +208 -0
- libsdca-0.3.0/tests/python/test_certificate.py +361 -0
- libsdca-0.3.0/tests/python/test_convergence.py +90 -0
- libsdca-0.3.0/tests/python/test_cpu_extension.py +488 -0
- libsdca-0.3.0/tests/python/test_cpu_memory.py +86 -0
- libsdca-0.3.0/tests/python/test_cpu_process_state.py +48 -0
- libsdca-0.3.0/tests/python/test_cross_entropy.py +460 -0
- libsdca-0.3.0/tests/python/test_cuda.py +284 -0
- libsdca-0.3.0/tests/python/test_cuda_float64_entropy.py +510 -0
- libsdca-0.3.0/tests/python/test_cuda_launch.py +775 -0
- libsdca-0.3.0/tests/python/test_distillation.py +480 -0
- libsdca-0.3.0/tests/python/test_distillation_bounded_expm1.py +120 -0
- libsdca-0.3.0/tests/python/test_distillation_offsets.py +37 -0
- libsdca-0.3.0/tests/python/test_distillation_references.py +237 -0
- libsdca-0.3.0/tests/python/test_distillation_stream_accuracy.py +58 -0
- libsdca-0.3.0/tests/python/test_distillation_two_pass.py +70 -0
- libsdca-0.3.0/tests/python/test_distillation_value.py +49 -0
- libsdca-0.3.0/tests/python/test_distributed.py +312 -0
- libsdca-0.3.0/tests/python/test_distributed_higher.py +103 -0
- libsdca-0.3.0/tests/python/test_entropy_higher_precision.py +79 -0
- libsdca-0.3.0/tests/python/test_entropy_references.py +203 -0
- libsdca-0.3.0/tests/python/test_float32_zero_gaps.py +112 -0
- libsdca-0.3.0/tests/python/test_float64_zero_gaps.py +79 -0
- libsdca-0.3.0/tests/python/test_fpenv.py +44 -0
- libsdca-0.3.0/tests/python/test_fused_entropy.py +347 -0
- libsdca-0.3.0/tests/python/test_half_gradient_rounding.py +319 -0
- libsdca-0.3.0/tests/python/test_half_precision.py +333 -0
- libsdca-0.3.0/tests/python/test_half_zero_gaps.py +96 -0
- libsdca-0.3.0/tests/python/test_indexed_losses.py +223 -0
- libsdca-0.3.0/tests/python/test_inplace_gradient.py +310 -0
- libsdca-0.3.0/tests/python/test_integration.py +65 -0
- libsdca-0.3.0/tests/python/test_interface.py +282 -0
- libsdca-0.3.0/tests/python/test_jax.py +1005 -0
- libsdca-0.3.0/tests/python/test_jax_backend.py +1032 -0
- libsdca-0.3.0/tests/python/test_jax_distillation_references.py +134 -0
- libsdca-0.3.0/tests/python/test_jax_ffi.py +269 -0
- libsdca-0.3.0/tests/python/test_jax_half_precision.py +222 -0
- libsdca-0.3.0/tests/python/test_jax_k_domain.py +73 -0
- libsdca-0.3.0/tests/python/test_jax_loss_dtypes.py +89 -0
- libsdca-0.3.0/tests/python/test_jax_mask_gap_rounding.py +49 -0
- libsdca-0.3.0/tests/python/test_jax_topk_mass.py +33 -0
- libsdca-0.3.0/tests/python/test_k_domain.py +78 -0
- libsdca-0.3.0/tests/python/test_kernel_limits.py +156 -0
- libsdca-0.3.0/tests/python/test_linear_cross_entropy.py +526 -0
- libsdca-0.3.0/tests/python/test_linear_cuda_accumulation.py +210 -0
- libsdca-0.3.0/tests/python/test_linear_full_range.py +127 -0
- libsdca-0.3.0/tests/python/test_loss_dtypes.py +108 -0
- libsdca-0.3.0/tests/python/test_losses.py +185 -0
- libsdca-0.3.0/tests/python/test_module_options.py +31 -0
- libsdca-0.3.0/tests/python/test_multilabel_gap_rounding.py +75 -0
- libsdca-0.3.0/tests/python/test_multilabel_kernels.py +581 -0
- libsdca-0.3.0/tests/python/test_multilabel_negative_max.py +49 -0
- libsdca-0.3.0/tests/python/test_multilabel_online.py +85 -0
- libsdca-0.3.0/tests/python/test_multilabel_split.py +119 -0
- libsdca-0.3.0/tests/python/test_multilabel_well_fit.py +78 -0
- libsdca-0.3.0/tests/python/test_native_tensor_losses.py +776 -0
- libsdca-0.3.0/tests/python/test_objectives.py +546 -0
- libsdca-0.3.0/tests/python/test_ordinary_inputs.py +135 -0
- libsdca-0.3.0/tests/python/test_precision_ranges.py +185 -0
- libsdca-0.3.0/tests/python/test_projection_termination.py +267 -0
- libsdca-0.3.0/tests/python/test_proximal_extremes.py +187 -0
- libsdca-0.3.0/tests/python/test_proximal_subnormal.py +133 -0
- libsdca-0.3.0/tests/python/test_shrinking.py +191 -0
- libsdca-0.3.0/tests/python/test_sklearn.py +326 -0
- libsdca-0.3.0/tests/python/test_softmax_limits.py +349 -0
- libsdca-0.3.0/tests/python/test_stress.py +289 -0
- libsdca-0.3.0/tests/python/test_stress_torch.py +106 -0
- libsdca-0.3.0/tests/python/test_tensor_losses.py +345 -0
- libsdca-0.3.0/tests/python/test_tensor_prox.py +438 -0
- libsdca-0.3.0/tests/python/test_threads.py +239 -0
- libsdca-0.3.0/tests/python/test_topk_kernels.py +808 -0
- libsdca-0.3.0/tests/python/test_topk_large_k.py +84 -0
- libsdca-0.3.0/tests/python/test_topk_references.py +595 -0
- libsdca-0.3.0/tests/python/test_topk_subnormal_mass.py +119 -0
- libsdca-0.3.0/tests/python/test_torch.py +646 -0
- libsdca-0.3.0/tests/python/test_torch_common_inputs.py +297 -0
- libsdca-0.3.0/tests/python/test_torch_compile.py +233 -0
- libsdca-0.3.0/tests/python/test_torch_cpu_paths.py +396 -0
- libsdca-0.3.0/tests/python/test_torch_func.py +109 -0
- libsdca-0.3.0/tests/python/test_torch_k_domain.py +122 -0
- libsdca-0.3.0/tests/python/test_torch_references.py +160 -0
- libsdca-0.3.0/tests/python/test_triton_devices.py +164 -0
- libsdca-0.3.0/tests/python/test_weighted_cuda.py +586 -0
- libsdca-0.3.0/tests/python/test_wide_float32_prox.py +163 -0
- libsdca-0.3.0/tests/python/test_wide_float64_sums.py +109 -0
- libsdca-0.3.0/tests/python/topk_rows.py +32 -0
libsdca-0.3.0/AGENTS.md
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# Using `libsdca` (Guide for AI Coding Assistants)
|
|
2
|
+
|
|
3
|
+
A short reference for AI coding assistants integrating `libsdca` into user code; usage
|
|
4
|
+
examples are in the [README](README.md#quick-start) and the linked guides.
|
|
5
|
+
|
|
6
|
+
## Installation
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
pip install libsdca # NumPy solver and losses (numpy>=1.23)
|
|
10
|
+
pip install 'libsdca[torch]' # PyTorch losses (torch>=2.1, installs ninja)
|
|
11
|
+
pip install 'libsdca[jax]' # JAX losses (jax>=0.4.35)
|
|
12
|
+
pip install 'libsdca[sklearn]' # SDCAClassifier (scikit-learn>=1.6)
|
|
13
|
+
cargo add libsdca # Rust crate (Rust>=1.74 and a C++11 compiler)
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
- Prebuilt wheels: Linux `x86_64` (`manylinux_2_28`) and macOS `arm64` (11+).
|
|
17
|
+
- PyTorch and JAX compile small C++ extensions on first use when a compiler and Python
|
|
18
|
+
headers are present; otherwise they fall back with a one-time warning
|
|
19
|
+
([TORCH.md](python/TORCH.md#build-tools)).
|
|
20
|
+
|
|
21
|
+
## Which API to use
|
|
22
|
+
|
|
23
|
+
| User goal | API | Guide |
|
|
24
|
+
| --- | --- | --- |
|
|
25
|
+
| Replace `torch.nn.CrossEntropyLoss` (integer class labels only, no label smoothing) | `libsdca.CrossEntropyLoss`, `cross_entropy` | [TORCH.md](python/TORCH.md#replace-pytorch-cross-entropy) |
|
|
26
|
+
| Large-vocabulary head + cross-entropy without `(N, V)` logits | `LinearCrossEntropyLoss`, `linear_cross_entropy` | [TORCH.md](python/TORCH.md#linear-layer-and-cross-entropy-without-the-logits) |
|
|
27
|
+
| Top-*k*, hinge and multilabel PyTorch losses | `TopKCrossEntropyLoss`, `TopKHingeLoss`, `MulticlassHingeLoss`, `MultilabelHingeLoss`, `MultilabelCrossEntropyLoss` | [TORCH.md](python/TORCH.md) |
|
|
28
|
+
| Logit distillation in PyTorch | `DistillationLoss`, `distillation_loss` | [TORCH.md](python/TORCH.md#teacher-logits-and-distillation) |
|
|
29
|
+
| Losses in JAX (`jit`, `grad`, `vmap`) | `libsdca.jax` | [JAX.md](python/JAX.md) |
|
|
30
|
+
| Linear or kernel SVM / softmax classifier (scikit-learn) | `libsdca.SDCAClassifier` | [python/README.md](python/README.md#classifier) |
|
|
31
|
+
| Same, with duality-gap history and warm starts | `libsdca.fit` | [python/README.md](python/README.md#training) |
|
|
32
|
+
| Rust, C++, C, MATLAB | crate `libsdca`, headers, `libsdca_c`, `matsdca_*` | [Rust](rust/README.md), [C++](docs/CPP.md), [C](docs/C.md), [MATLAB](docs/MATLAB.md) |
|
|
33
|
+
|
|
34
|
+
## Pitfalls
|
|
35
|
+
|
|
36
|
+
- **Class axis:** `CrossEntropyLoss` takes classes on `dim=1` like PyTorch; every other
|
|
37
|
+
loss defaults to `dim=-1`. Pass `dim=1` for `(N, C, H, W)` inputs to the others
|
|
38
|
+
([TORCH.md](python/TORCH.md#argument-parity)).
|
|
39
|
+
- **Label smoothing and probability targets:** use `distillation_loss` with
|
|
40
|
+
log-probabilities as the teacher
|
|
41
|
+
([TORCH.md](python/TORCH.md#label-smoothing-and-probability-targets)).
|
|
42
|
+
- **Distillation** takes raw logits for both inputs and applies no $T^2$ factor
|
|
43
|
+
([TORCH.md](python/TORCH.md#teacher-logits-and-distillation)).
|
|
44
|
+
- **Memory:** CUDA losses can reuse the logits buffer for the gradient when nothing else
|
|
45
|
+
references it ([TORCH.md](python/TORCH.md#memory)).
|
|
46
|
+
- **Solver objective:** $\frac12\Vert W \Vert_2^2 + C\sum_i \ell_i$ with no intercept; standardize
|
|
47
|
+
features; for $d \gg n$ pass a Gram matrix ([python/README.md](python/README.md#training),
|
|
48
|
+
[API.md](docs/API.md#options)).
|
|
49
|
+
- **Convergence:** only `status == "solved"` certifies the duality gap
|
|
50
|
+
([API.md](docs/API.md#results)).
|
|
51
|
+
|
|
52
|
+
## Further documentation
|
|
53
|
+
|
|
54
|
+
- Loss formulas, options and environment variables: [docs/API.md](docs/API.md)
|
|
55
|
+
- Numerics and known limits: [docs/numerics.md](docs/numerics.md)
|
|
56
|
+
- Performance: [docs/performance.md](docs/performance.md)
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.3.0 (2026-11-09)
|
|
4
|
+
|
|
5
|
+
### Highlights
|
|
6
|
+
|
|
7
|
+
- **Unified interfaces:** Python package (`fit`, `loss_and_grad`, `coordinate_update`),
|
|
8
|
+
scikit-learn estimator (`SDCAClassifier`), PyTorch modules and functions, JAX loss
|
|
9
|
+
functions, Rust crate, C ABI (`include/sdca/c_api.h`), C++11 headers, and MATLAB/Octave
|
|
10
|
+
gateways sharing one set of objective, option and status names ([API.md](docs/API.md)).
|
|
11
|
+
- **Numerical stability:** Losses and gradients preserve tiny probability tails down to the
|
|
12
|
+
subnormal limit (avoiding softmax collapse), and distillation gradients are exact zero at
|
|
13
|
+
equal logits ([numerics](docs/numerics.md),
|
|
14
|
+
[known limits](docs/numerics.md#aggregate-tail-limitations)).
|
|
15
|
+
- **Fused CUDA and CPU kernels:** On an A100 (`8192 × 32000` and `8192 × 128256`),
|
|
16
|
+
cross-entropy takes `0.93–0.98×` `torch.compile` time (`0.33–0.41×` eager), top-5
|
|
17
|
+
cross-entropy `0.23–0.26×`, and distillation `0.62–0.81×` ([performance](docs/performance.md)).
|
|
18
|
+
- **Certified convergence:** `status == "solved"` requires the duality gap of the returned
|
|
19
|
+
weights and the feasible projection of the returned duals, evaluated in `double` with a
|
|
20
|
+
first-order rounding estimate, to meet
|
|
21
|
+
`tol` ([API.md](docs/API.md#results)).
|
|
22
|
+
|
|
23
|
+
### Interfaces and losses
|
|
24
|
+
|
|
25
|
+
- **PyTorch (`libsdca`, `libsdca.torch`):** `CrossEntropyLoss` (drop-in for
|
|
26
|
+
`torch.nn.CrossEntropyLoss`, except integer class labels only and no label smoothing), `TopKCrossEntropyLoss`, `MulticlassHingeLoss`,
|
|
27
|
+
`TopKHingeLoss` ($\alpha$ and $\beta$), `MultilabelCrossEntropyLoss`,
|
|
28
|
+
`MultilabelHingeLoss`, `ProximalCrossEntropyLoss`, `DistillationLoss`,
|
|
29
|
+
`ProximalDistillationLoss`, `SDCALoss`, and `LinearCrossEntropyLoss` (chunked linear head
|
|
30
|
+
+ cross-entropy without materializing `(N, V)` logits) across `float16`, `bfloat16`,
|
|
31
|
+
`float32` and `float64` on CPU and CUDA, with `torch.compile(fullgraph=True)` and
|
|
32
|
+
`DTensor` support ([TORCH.md](python/TORCH.md)).
|
|
33
|
+
- **JAX (`libsdca.jax`):** Classification and distillation losses for `jit`, `grad` and
|
|
34
|
+
`vmap`, with zero-copy XLA FFI custom calls on CPU ([JAX.md](python/JAX.md)).
|
|
35
|
+
- **Python & scikit-learn:** `fit`, `loss_and_grad`, `distillation_loss_and_grad`,
|
|
36
|
+
`proximal_distillation_loss_and_grad`, `coordinate_update`, and `SDCAClassifier`
|
|
37
|
+
([python/README.md](python/README.md)).
|
|
38
|
+
- **Rust, C, C++ and MATLAB:** Rust crate `libsdca` ([rust/README.md](rust/README.md)),
|
|
39
|
+
versioned C ABI `libsdca::sdca_c` ([docs/C.md](docs/C.md)), CMake target
|
|
40
|
+
`libsdca::libsdca` ([docs/CPP.md](docs/CPP.md)), and MEX gateways `matsdca_fit`,
|
|
41
|
+
`matsdca_loss` and `matsdca_prox` ([docs/MATLAB.md](docs/MATLAB.md)).
|
|
42
|
+
|
|
43
|
+
### Performance and solver improvements
|
|
44
|
+
|
|
45
|
+
- **CUDA kernels:** Fused Triton kernels for all losses with C++ `cuLaunchKernel` autograd
|
|
46
|
+
dispatch, multi-row CTA tiling for short rows, streaming tiles up to $2^{25}$ classes, and
|
|
47
|
+
in-place backward gradient writes into unreferenced logits tensors.
|
|
48
|
+
- **CPU kernels:** JIT-compiled ATen/OpenMP C++ extension with C++ autograd nodes and SIMD
|
|
49
|
+
`float32` exponentials + `float64` row statistics for narrow cross-entropy and top-*k*
|
|
50
|
+
cross-entropy ($k \le 64$).
|
|
51
|
+
- **Shrinking and screening (`shrinking="auto"`):** Combines LIBLINEAR-style active-set
|
|
52
|
+
shrinking with gap-safe screening for hinge fits, adapting the evaluation schedule to
|
|
53
|
+
shrunk epoch work (`0.14–0.59×` `LinearSVC` runtime; [API.md](docs/API.md#shrinking)).
|
|
54
|
+
- **$O(n \log n)$ projections and Taylor root warm-starts:** Top-*k* cone, simplex, knapsack
|
|
55
|
+
and top-*k* entropy projections run in $O(n \log n)$, and proximal / entropy root solves
|
|
56
|
+
converge in `1.0–1.3` row evaluations on average.
|
|
57
|
+
|
|
58
|
+
### Numerics and precision
|
|
59
|
+
|
|
60
|
+
- **Single rounding:** 16-bit gradients are scaled by the upstream cotangent and reduction
|
|
61
|
+
factor in working precision before a single rounding to the output dtype, and hinge / top-*k*
|
|
62
|
+
target gradients are formed from the unrounded competitor sum.
|
|
63
|
+
- **Double iterate for `float32` fits (`precision="float64"` default):** Keeps internal
|
|
64
|
+
solver weights and duals in `double` (`8(d + n)m` bytes) and rounds once at each
|
|
65
|
+
evaluation so `float32` fits reach `float64` gap tolerances down to `float32` output
|
|
66
|
+
rounding (`3.5e-7` on `digits`; pass `precision="float32"` for pure `float32` speed and
|
|
67
|
+
memory; [numerics.md](docs/numerics.md#what-each-precision-guarantees)).
|
|
68
|
+
- **Compensated reductions & hardened projections:** Compensated sums in multilabel
|
|
69
|
+
cross-entropy and distillation, warm-start residual preservation at large score offsets,
|
|
70
|
+
and checked `float64` / extended-precision `multilabel_hinge` projections
|
|
71
|
+
([API.md](docs/API.md#projection-precision)).
|
|
72
|
+
|
|
73
|
+
### Packaging and build
|
|
74
|
+
|
|
75
|
+
- Portable `py3-none` wheels for Linux `x86_64` (`manylinux_2_28`) and macOS 11+ (`arm64`)
|
|
76
|
+
with built-in BLAS; configurable BLAS (`SDCA_BLAS_BACKEND=AUTO|EXTERNAL|BUILTIN`);
|
|
77
|
+
CMake 3.12+ package config; and GitHub Actions CI on Linux and macOS.
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# Dependencies and optional builds
|
|
2
|
+
|
|
3
|
+
`libsdca` requires a C++11 compiler. An external BLAS library is optional: a built-in
|
|
4
|
+
backend lets the solver and loss functions run with no third-party runtime dependencies.
|
|
5
|
+
CMake 3.12+ is required only for the CMake packages (`libsdca::libsdca` and
|
|
6
|
+
`libsdca::sdca_c`).
|
|
7
|
+
|
|
8
|
+
## Tested configurations
|
|
9
|
+
|
|
10
|
+
| Component | Validated environments |
|
|
11
|
+
| --- | --- |
|
|
12
|
+
| C++ & C ABI | Ubuntu 22.04 / 24.04 (`x86_64`) with GCC 11.4 / 13 and Clang 18 (`BUILTIN` and `EXTERNAL` BLAS, C and C++ consumers, GCC ASan/UBSan, Clang TSan); macOS (`arm64`) with Apple Clang 21 (`BUILTIN` and Accelerate) |
|
|
13
|
+
| Python (CPU) | Python 3.10–3.14, NumPy 1.23–2.5, SciPy 1.10–1.18, scikit-learn 1.6–1.9, PyTorch 2.1–2.15 (CPU extension across AVX-512, AVX2 and generic/arm64), JAX 0.4.35–0.12 (with and without XLA FFI) |
|
|
14
|
+
| CUDA | NVIDIA A100-SXM4-80GB and H100 (PyTorch 2.14–2.15, Triton 3.8–3.9, both C++ launcher and Triton launch paths; JAX 0.12 with CUDA plugin) |
|
|
15
|
+
| Packaging | Portable `py3-none` wheels (`SDCA_BLAS_BACKEND=BUILTIN`) via `cibuildwheel` on `manylinux_2_28` `x86_64` and macOS 11+ `arm64`, sdist builds, and standalone `.crate` archives |
|
|
16
|
+
| Rust | Rust 1.74 (MSRV) and stable on Linux `x86_64` (`BUILTIN`, reference BLAS, OpenBLAS and BLIS, shared and static) |
|
|
17
|
+
| Octave | GNU Octave 10.3 (`mkoctfile` MEX gateways) |
|
|
18
|
+
|
|
19
|
+
- **PyTorch & JAX version floors:** Requires `python >= 3.10`, `numpy >= 1.23`,
|
|
20
|
+
`torch >= 2.1` (use `numpy < 2` with PyTorch 2.1 wheels), `jax >= 0.4.35`, and
|
|
21
|
+
`scikit-learn >= 1.6`.
|
|
22
|
+
- **CUDA fused kernels:** Require `torch >= 2.7` and `triton >= 3.3` (older versions fall
|
|
23
|
+
back to composed PyTorch tensor ops). The C++ `cuLaunchKernel` launcher supports Triton
|
|
24
|
+
3.3–3.9 (`SDCA_CUDA_LAUNCHER` overrides selection; `libsdca.torch.explain_backend`
|
|
25
|
+
reports the active backend).
|
|
26
|
+
|
|
27
|
+
## C++ configuration
|
|
28
|
+
|
|
29
|
+
| CMake option | Default | Description |
|
|
30
|
+
| --- | --- | --- |
|
|
31
|
+
| `SDCA_BLAS_BACKEND` | `AUTO` | `AUTO` (prefer external CBLAS, else built-in), `BUILTIN` (no external BLAS/OpenMP), or `EXTERNAL` (require external CBLAS) |
|
|
32
|
+
| `SDCA_BUILD_C_API` | `OFF` | Build and install the C ABI library `libsdca::sdca_c` (links `Threads`) |
|
|
33
|
+
| `SDCA_WARNINGS_AS_ERRORS` | `OFF` | Treat compiler warnings as errors (`-Werror`) |
|
|
34
|
+
| `BUILD_TESTING` | `OFF` | Build bundled GoogleTest and C++ test suites |
|
|
35
|
+
| `SDCA_BUILD_MATLAB` | `OFF` | Build MATLAB MEX gateways |
|
|
36
|
+
| `USE_CHRONO` | `ON` | Use `<chrono>` wall clock (`OFF` selects POSIX `gettimeofday`) |
|
|
37
|
+
| `USE_SEQUENTIAL`, `USE_ILP64`, `USE_THREADING_SUFFIX` | `ON`, `ON`, `OFF` | Intel MKL threading layer, 64-bit integer ABI, and MEX binary suffix |
|
|
38
|
+
|
|
39
|
+
Minimal installation and CMake usage:
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
cmake -S . -B build/minimal -DSDCA_BLAS_BACKEND=BUILTIN -DCMAKE_INSTALL_PREFIX=/your/prefix
|
|
43
|
+
cmake --build build/minimal --target install
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
```cmake
|
|
47
|
+
find_package(libsdca CONFIG REQUIRED)
|
|
48
|
+
target_link_libraries(my_program PRIVATE libsdca::libsdca)
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Include `sdca/utility/logging.cpp` in exactly one translation unit. Direct header users
|
|
52
|
+
without CMake default to `BLAS_BUILTIN` and `<chrono>` (or define `BLAS_DEFAULT`,
|
|
53
|
+
`BLAS_DEFAULT_LOCAL_HEADER`, `BLAS_MKL`, `BLAS_ACCELERATE`, or `BLAS_MATLAB` and link the
|
|
54
|
+
corresponding CBLAS library).
|
|
55
|
+
|
|
56
|
+
## Python configuration
|
|
57
|
+
|
|
58
|
+
The PyTorch CPU extension and CUDA launcher compile on first use via
|
|
59
|
+
`torch.utils.cpp_extension` (requiring `ninja`, a C++17/C++20 compiler with OpenMP, Python
|
|
60
|
+
headers, and CUDA headers for the launcher); native JAX CPU losses likewise compile a small
|
|
61
|
+
XLA FFI handler against `jax.ffi.include_dir()` ([environment variables](docs/API.md#environment-variables)).
|
|
62
|
+
Without build tools, both fall back automatically with a one-time warning.
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
# Default source install (uses system CBLAS if found, else built-in):
|
|
66
|
+
python -m pip install .
|
|
67
|
+
|
|
68
|
+
# Force built-in BLAS (no external BLAS dependency):
|
|
69
|
+
SDCA_BLAS_BACKEND=BUILTIN python -m pip install .
|
|
70
|
+
|
|
71
|
+
# Require external BLAS:
|
|
72
|
+
SDCA_BLAS_BACKEND=EXTERNAL python -m pip install .
|
|
73
|
+
|
|
74
|
+
# Pure-Python wheel for PyTorch/JAX tensor losses only (no C++ compiler needed):
|
|
75
|
+
SDCA_BUILD_NATIVE=OFF python -m pip install '.[torch]'
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
| Optional extra | Packages | Purpose |
|
|
79
|
+
| --- | --- | --- |
|
|
80
|
+
| *(default)* | `numpy>=1.23` | NumPy solver and loss interface |
|
|
81
|
+
| `torch` | `torch>=2.1`, `ninja` | PyTorch loss layers and JIT extensions |
|
|
82
|
+
| `jax` | `jax>=0.4.35` | JAX loss functions (`libsdca.jax`) |
|
|
83
|
+
| `sklearn` | `scikit-learn>=1.6` | `SDCAClassifier` estimator |
|
|
84
|
+
| `test`, `test-torch`, `test-jax` | `pytest>=9`, `pytest-xdist>=3.6`, `scipy`, `mpmath`, ... | Test suites (`--group core\|torch\|jax\|all`) |
|
|
85
|
+
| `coverage` | `coverage[toml]>=7` | Python line-coverage instrumentation |
|
|
86
|
+
|
|
87
|
+
## Rust configuration
|
|
88
|
+
|
|
89
|
+
The `libsdca` crate has zero Rust dependencies and requires Rust 1.74+. Its `build.rs`
|
|
90
|
+
compiles `src/c_api.cpp` with `CXX` (default `c++`) and the built-in BLAS, or links an
|
|
91
|
+
external CBLAS via `SDCA_BLAS` ([rust/README.md](rust/README.md#building)).
|
libsdca-0.3.0/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2015-2026 Maksim Lapin
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
recursive-include include *.h
|
|
2
|
+
include src/c_api.cpp src/tensor_losses.h
|
|
3
|
+
include sdca_build.py
|
|
4
|
+
include python/README.md
|
|
5
|
+
include python/TORCH.md python/JAX.md
|
|
6
|
+
include python/libsdca/_torch_entropy.cpp python/libsdca/_torch_*.h python/libsdca/_jax_ffi.cpp
|
|
7
|
+
include include/sdca/utility/logging.cpp
|
|
8
|
+
include CHANGELOG.md DEPENDENCIES.md AGENTS.md
|
|
9
|
+
include tests/python/*.py
|
libsdca-0.3.0/PKG-INFO
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: libsdca
|
|
3
|
+
Version: 0.3.0
|
|
4
|
+
Summary: Numerically stable multiclass, top-k, multilabel and distillation losses for PyTorch, JAX and NumPy, and certified SDCA solvers for linear and kernel SVM and softmax classifiers, with scikit-learn, C++, C, Rust and MATLAB interfaces
|
|
5
|
+
Author: Max Lapin
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://mlapin.github.io/libsdca/
|
|
8
|
+
Project-URL: Documentation, https://github.com/mlapin/libsdca#documentation
|
|
9
|
+
Project-URL: Source, https://github.com/mlapin/libsdca
|
|
10
|
+
Project-URL: Changelog, https://github.com/mlapin/libsdca/blob/v0.3.0/CHANGELOG.md
|
|
11
|
+
Project-URL: Issues, https://github.com/mlapin/libsdca/issues
|
|
12
|
+
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
|
|
13
|
+
Classifier: Development Status :: 4 - Beta
|
|
14
|
+
Classifier: Environment :: GPU :: NVIDIA CUDA
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Classifier: Intended Audience :: Science/Research
|
|
17
|
+
Classifier: Operating System :: MacOS
|
|
18
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
19
|
+
Classifier: Programming Language :: C++
|
|
20
|
+
Classifier: Programming Language :: Python :: 3
|
|
21
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
24
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
25
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
26
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
27
|
+
Classifier: Topic :: Scientific/Engineering :: Mathematics
|
|
28
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
29
|
+
Classifier: Typing :: Typed
|
|
30
|
+
Requires-Python: >=3.10
|
|
31
|
+
Description-Content-Type: text/markdown
|
|
32
|
+
License-File: LICENSE
|
|
33
|
+
Requires-Dist: numpy>=1.23
|
|
34
|
+
Provides-Extra: torch
|
|
35
|
+
Requires-Dist: torch>=2.1; extra == "torch"
|
|
36
|
+
Requires-Dist: ninja; extra == "torch"
|
|
37
|
+
Provides-Extra: jax
|
|
38
|
+
Requires-Dist: jax>=0.4.35; extra == "jax"
|
|
39
|
+
Provides-Extra: sklearn
|
|
40
|
+
Requires-Dist: scikit-learn>=1.6; extra == "sklearn"
|
|
41
|
+
Provides-Extra: test
|
|
42
|
+
Requires-Dist: pytest>=9; extra == "test"
|
|
43
|
+
Requires-Dist: pytest-xdist>=3.6; extra == "test"
|
|
44
|
+
Requires-Dist: scipy; extra == "test"
|
|
45
|
+
Requires-Dist: mpmath; extra == "test"
|
|
46
|
+
Provides-Extra: test-torch
|
|
47
|
+
Requires-Dist: pytest>=9; extra == "test-torch"
|
|
48
|
+
Requires-Dist: pytest-xdist>=3.6; extra == "test-torch"
|
|
49
|
+
Requires-Dist: scipy; extra == "test-torch"
|
|
50
|
+
Requires-Dist: mpmath; extra == "test-torch"
|
|
51
|
+
Requires-Dist: torch>=2.1; extra == "test-torch"
|
|
52
|
+
Requires-Dist: ninja; extra == "test-torch"
|
|
53
|
+
Requires-Dist: scikit-learn>=1.6; extra == "test-torch"
|
|
54
|
+
Provides-Extra: test-jax
|
|
55
|
+
Requires-Dist: pytest>=9; extra == "test-jax"
|
|
56
|
+
Requires-Dist: pytest-xdist>=3.6; extra == "test-jax"
|
|
57
|
+
Requires-Dist: scipy; extra == "test-jax"
|
|
58
|
+
Requires-Dist: mpmath; extra == "test-jax"
|
|
59
|
+
Requires-Dist: jax>=0.4.35; extra == "test-jax"
|
|
60
|
+
Provides-Extra: coverage
|
|
61
|
+
Requires-Dist: coverage[toml]>=7; extra == "coverage"
|
|
62
|
+
Dynamic: license-file
|
|
63
|
+
|
|
64
|
+
# libsdca for Python
|
|
65
|
+
|
|
66
|
+
SDCA solvers for linear and kernel SVMs and softmax classifiers (`cross_entropy`,
|
|
67
|
+
`topk_cross_entropy`, `multiclass_hinge`, `topk_hinge_alpha`, `topk_hinge_beta`,
|
|
68
|
+
`multilabel_hinge`, `multilabel_cross_entropy`, `distillation`), plus differentiable
|
|
69
|
+
loss layers for PyTorch, JAX and NumPy.
|
|
70
|
+
|
|
71
|
+
The native C++ library loads via `ctypes`. Prebuilt wheels for Linux `x86_64` and macOS
|
|
72
|
+
`arm64` require no compiler and depend only on NumPy; `import libsdca` does not import
|
|
73
|
+
PyTorch, scikit-learn or JAX until used. All losses preserve tiny confident-prediction
|
|
74
|
+
losses and gradients down to the output dtype's subnormal limit and produce exact zero
|
|
75
|
+
distillation gradients at equal logits ([numerics](https://github.com/mlapin/libsdca/blob/v0.3.0/docs/numerics.md),
|
|
76
|
+
[known limits](https://github.com/mlapin/libsdca/blob/v0.3.0/docs/numerics.md#known-limits)).
|
|
77
|
+
|
|
78
|
+
```bash
|
|
79
|
+
pip install libsdca # NumPy interface
|
|
80
|
+
pip install 'libsdca[sklearn]' # scikit-learn estimator SDCAClassifier
|
|
81
|
+
pip install 'libsdca[torch]' # PyTorch loss layers
|
|
82
|
+
pip install 'libsdca[jax]' # JAX loss functions
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
| Task | Entry point |
|
|
86
|
+
| --- | --- |
|
|
87
|
+
| Train a scikit-learn classifier | `libsdca.SDCAClassifier` |
|
|
88
|
+
| Train with full control (warm starts, label sets, history) | `libsdca.fit` $\to$ `Model` |
|
|
89
|
+
| Evaluate losses and gradients on NumPy arrays | `libsdca.loss_and_grad`, `distillation_loss_and_grad` |
|
|
90
|
+
| PyTorch neural-network loss layers | `libsdca.CrossEntropyLoss`, `TopKCrossEntropyLoss`, ... ([TORCH.md](https://github.com/mlapin/libsdca/blob/v0.3.0/python/TORCH.md)) |
|
|
91
|
+
| JAX loss functions (`jit`, `grad`, `vmap`) | `libsdca.jax.sdca_loss`, ... ([JAX.md](https://github.com/mlapin/libsdca/blob/v0.3.0/python/JAX.md)) |
|
|
92
|
+
| Single SDCA dual block update | `libsdca.coordinate_update` |
|
|
93
|
+
|
|
94
|
+
## Classifier
|
|
95
|
+
|
|
96
|
+
```python
|
|
97
|
+
import numpy as np
|
|
98
|
+
from libsdca import SDCAClassifier
|
|
99
|
+
|
|
100
|
+
rng = np.random.default_rng(0)
|
|
101
|
+
X = rng.normal(size=(300, 20))
|
|
102
|
+
y = np.array(["cat", "dog", "emu"])[(X @ rng.normal(size=(20, 3))).argmax(axis=1)]
|
|
103
|
+
|
|
104
|
+
clf = SDCAClassifier("topk_cross_entropy", k=2, C=1.0).fit(X, y)
|
|
105
|
+
print(clf.predict(X[:5]), clf.score(X, y))
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
`SDCAClassifier` (`libsdca.sklearn`) passes scikit-learn's `check_estimator` suite (except
|
|
109
|
+
checks that pass invalid Gram matrices to `kernel="precomputed"`) and supports `Pipeline`,
|
|
110
|
+
`clone`, `cross_val_score`, `GridSearchCV`, arbitrary sortable labels, `predict_proba` /
|
|
111
|
+
`predict_log_proba` (for `cross_entropy`), and precomputed Gram matrices. Fitted attributes:
|
|
112
|
+
`classes_`, `coef_ (n_classes, n_features)`, `dual_coef_ (n_classes, n_samples)`,
|
|
113
|
+
`n_features_in_`, `n_iter_`, `status_`, `history_`, and `model_`.
|
|
114
|
+
|
|
115
|
+
## Training
|
|
116
|
+
|
|
117
|
+
```python
|
|
118
|
+
import libsdca
|
|
119
|
+
|
|
120
|
+
model = libsdca.fit(X, y_int, "topk_hinge_alpha", C=1.0, k=2, gamma=0.5)
|
|
121
|
+
print(model.status, model.epochs, model.history["relative_gap"][-1])
|
|
122
|
+
scores = model.decision_function(X) # X @ model.weights
|
|
123
|
+
predicted = model.predict(X)
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
`libsdca.fit(X, y, objective="cross_entropy", *, ...)` minimizes
|
|
127
|
+
$0.5\Vert W \Vert_2^2 + C \sum_i \ell(X_i W, y_i)$ over $W \in \mathbb{R}^{d \times m}$ (no implicit
|
|
128
|
+
intercept; set $C = 1 / (\lambda n)$ for $\frac{\lambda}{2}\Vert W \Vert_2^2 + \frac{1}{n}\sum_i \ell_i$).
|
|
129
|
+
See [API.md](https://github.com/mlapin/libsdca/blob/v0.3.0/docs/API.md#loss-definitions)
|
|
130
|
+
for all objectives and options:
|
|
131
|
+
|
|
132
|
+
| Option | Default | Meaning |
|
|
133
|
+
| --- | --- | --- |
|
|
134
|
+
| `C` | `1.0` | Loss weight ($> 0$) |
|
|
135
|
+
| `k`, `gamma` | `1`, `0.0` | Top-*k* parameter (`[1, m - 1]`) and hinge smoothing (`0` = nonsmooth) |
|
|
136
|
+
| `tol`, `max_epoch`, `eval_epoch` | `1e-3`, `1000`, `10` | Relative duality-gap target, epoch limit, and evaluation period |
|
|
137
|
+
| `shrinking` | `"auto"` | Active-set shrinking and gap-safe screening for hinges ([API.md](https://github.com/mlapin/libsdca/blob/v0.3.0/docs/API.md#shrinking)) |
|
|
138
|
+
| `projection_precision` | `"float64"` | `"float64"` (checked double) or `"extended"` ([API.md](https://github.com/mlapin/libsdca/blob/v0.3.0/docs/API.md#projection-precision)) |
|
|
139
|
+
| `kernel`, `center`, `initial_dual` | `False`, `None`, `None` | Gram matrix `(n, n)` input, regularizer center $W_0$, and warm-start duals |
|
|
140
|
+
| `precision` | `"float64"` | Working arithmetic and solver iterate (`"float64"` or `"float32"`) |
|
|
141
|
+
|
|
142
|
+
`Model` exposes `weights (d, m)` (`None` for kernels), `dual (n, m)`, `status` (only
|
|
143
|
+
`"solved"` certifies the duality gap), `epochs`, `history`, and `params`.
|
|
144
|
+
|
|
145
|
+
## Losses and gradients
|
|
146
|
+
|
|
147
|
+
```python
|
|
148
|
+
values, gradients = libsdca.loss_and_grad(scores, y_int, "topk_hinge_beta", k=2, gamma=0.5)
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
- `loss_and_grad(logits, target, loss="cross_entropy", *, k=1, gamma=0.0, alpha=1.0, num_threads=None, projection_precision="float64", precision=None)`
|
|
152
|
+
computes per-row losses `(n,)` and gradients `(n, m)` for any objective or
|
|
153
|
+
`proximal_cross_entropy`.
|
|
154
|
+
- `distillation_loss_and_grad` and `proximal_distillation_loss_and_grad` evaluate logit
|
|
155
|
+
distillation and its Moreau envelope.
|
|
156
|
+
- `coordinate_update(logits, y, objective="cross_entropy", *, previous=None, C=1.0, norm2=1.0, ...)`
|
|
157
|
+
performs one exact SDCA dual block step.
|
|
158
|
+
|
|
159
|
+
Row loops parallelize across `num_threads` threads (`None` uses `SDCA_NUM_THREADS` or
|
|
160
|
+
automatic CPU detection; `1` runs serially) with bitwise-identical results
|
|
161
|
+
([threads](https://github.com/mlapin/libsdca/blob/v0.3.0/docs/API.md#threads),
|
|
162
|
+
[environment variables](https://github.com/mlapin/libsdca/blob/v0.3.0/docs/API.md#environment-variables)).
|
|
163
|
+
|
|
164
|
+
## Precision
|
|
165
|
+
|
|
166
|
+
Outputs (`weights`, `dual`, `gradients`) keep the input array's `float32` or `float64`
|
|
167
|
+
dtype; loss values are `float64`. For `float32` inputs, the default `precision="float64"`
|
|
168
|
+
keeps the solver iterate in `double` (`8(d + n)m` bytes) so convergence does not stall
|
|
169
|
+
above `1e-5`; pass `precision="float32"` for `1.6–1.9×` faster `cross_entropy` fits
|
|
170
|
+
([support matrix](https://github.com/mlapin/libsdca/blob/v0.3.0/docs/API.md#precision),
|
|
171
|
+
[PyTorch precision](https://github.com/mlapin/libsdca/blob/v0.3.0/python/TORCH.md#precision)).
|
|
172
|
+
|
|
173
|
+
```python
|
|
174
|
+
X32 = X.astype(np.float32)
|
|
175
|
+
model = libsdca.fit(X32, y_int) # float32 arrays, double solve (default)
|
|
176
|
+
fast = libsdca.fit(X32, y_int, precision="float32") # float32 arrays, float32 solve
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
## PyTorch drop-in cross-entropy
|
|
180
|
+
|
|
181
|
+
`CrossEntropyLoss` is a drop-in for `torch.nn.CrossEntropyLoss`, except it takes integer class
|
|
182
|
+
labels only and no label smoothing.
|
|
183
|
+
|
|
184
|
+
```python
|
|
185
|
+
import torch
|
|
186
|
+
from libsdca import CrossEntropyLoss
|
|
187
|
+
|
|
188
|
+
logits = torch.randn(32, 5, requires_grad=True)
|
|
189
|
+
target = torch.randint(5, (32,))
|
|
190
|
+
CrossEntropyLoss()(logits, target).backward()
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
See [TORCH.md](https://github.com/mlapin/libsdca/blob/v0.3.0/python/TORCH.md#replace-pytorch-cross-entropy)
|
|
194
|
+
and [performance.md](https://github.com/mlapin/libsdca/blob/v0.3.0/docs/performance.md).
|
|
195
|
+
|
|
196
|
+
## Building from source and testing
|
|
197
|
+
|
|
198
|
+
```bash
|
|
199
|
+
pip install -e . # editable install (uses system CBLAS if found)
|
|
200
|
+
python -m pytest -n auto --dist worksteal tests/python --group all # core, torch, jax, or all
|
|
201
|
+
python -m pytest tests/python --group all -m serial
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
See [DEPENDENCIES.md](https://github.com/mlapin/libsdca/blob/v0.3.0/DEPENDENCIES.md) for
|
|
205
|
+
`SDCA_BLAS_BACKEND` options (`AUTO`, `EXTERNAL`, `BUILTIN`).
|