stdplus-ext 0.0.1__tar.gz → 0.1.1__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.
Files changed (31) hide show
  1. stdplus_ext-0.1.1/.gitignore +58 -0
  2. stdplus_ext-0.1.1/PKG-INFO +139 -0
  3. stdplus_ext-0.1.1/README.md +116 -0
  4. stdplus_ext-0.1.1/pyproject.toml +52 -0
  5. {stdplus_ext-0.0.1 → stdplus_ext-0.1.1}/src/stdplus_ext/__init__.py +2 -0
  6. stdplus_ext-0.1.1/src/stdplus_ext/_numerics.py +361 -0
  7. stdplus_ext-0.1.1/src/stdplus_ext/_triton.py +367 -0
  8. stdplus_ext-0.1.1/src/stdplus_ext/log_stdplus_triton.py +8 -0
  9. stdplus_ext-0.1.1/src/stdplus_ext/py.typed +0 -0
  10. stdplus_ext-0.1.1/src/stdplus_ext/stdplus.py +208 -0
  11. stdplus_ext-0.1.1/src/stdplus_ext/stdplus_triton.py +8 -0
  12. stdplus_ext-0.1.1/tests/conftest.py +23 -0
  13. stdplus_ext-0.1.1/tests/reference.py +43 -0
  14. stdplus_ext-0.1.1/tests/test_api.py +112 -0
  15. stdplus_ext-0.1.1/tests/test_autograd.py +101 -0
  16. stdplus_ext-0.1.1/tests/test_compiled_accuracy.py +67 -0
  17. stdplus_ext-0.1.1/tests/test_negative_accuracy.py +52 -0
  18. stdplus_ext-0.1.1/tests/test_numerics.py +140 -0
  19. stdplus_ext-0.1.1/tests/test_regressions.py +117 -0
  20. stdplus_ext-0.0.1/PKG-INFO +0 -42
  21. stdplus_ext-0.0.1/README.md +0 -31
  22. stdplus_ext-0.0.1/pyproject.toml +0 -20
  23. stdplus_ext-0.0.1/setup.cfg +0 -4
  24. stdplus_ext-0.0.1/src/stdplus_ext/log_stdplus_triton.py +0 -173
  25. stdplus_ext-0.0.1/src/stdplus_ext/stdplus.py +0 -246
  26. stdplus_ext-0.0.1/src/stdplus_ext/stdplus_triton.py +0 -205
  27. stdplus_ext-0.0.1/src/stdplus_ext.egg-info/PKG-INFO +0 -42
  28. stdplus_ext-0.0.1/src/stdplus_ext.egg-info/SOURCES.txt +0 -11
  29. stdplus_ext-0.0.1/src/stdplus_ext.egg-info/dependency_links.txt +0 -1
  30. stdplus_ext-0.0.1/src/stdplus_ext.egg-info/top_level.txt +0 -1
  31. {stdplus_ext-0.0.1 → stdplus_ext-0.1.1}/LICENSE +0 -0
@@ -0,0 +1,58 @@
1
+ # Python and compiler caches
2
+ __pycache__/
3
+ *.py[cod]
4
+ .pytest_cache/
5
+ .mypy_cache/
6
+ .ruff_cache/
7
+ .triton/
8
+ .cache/
9
+ torch_compile_debug/
10
+ torchinductor_*/
11
+
12
+ # Build and release artifacts
13
+ /build/
14
+ /dist/
15
+ /wheelhouse/
16
+ *.whl
17
+ stdplus_ext-*.tar.gz
18
+ stdplus-ext-*.tar.gz
19
+ stdplus_ext-*.zip
20
+ stdplus-ext-*.zip
21
+ *.egg-info/
22
+ *.egg
23
+ .eggs/
24
+
25
+ # Virtual environments and local credentials
26
+ .venv*/
27
+ venv/
28
+ env/
29
+ .env
30
+ .env.*
31
+ !.env.example
32
+ !.env.*.example
33
+ .envrc
34
+ .pypirc
35
+
36
+ # Test and coverage output
37
+ .coverage
38
+ .coverage.*
39
+ coverage.xml
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+
44
+ # Local notebooks and editor state
45
+ .ipynb_checkpoints/
46
+ .idea/
47
+ .vscode/
48
+ *.swp
49
+ *.swo
50
+ *~
51
+ *.log
52
+
53
+ # Operating system metadata
54
+ .DS_Store
55
+ Thumbs.db
56
+
57
+ # Keep docs, benchmarks, and tests versioned. Distribution contents are
58
+ # controlled separately by the allowlists in pyproject.toml.
@@ -0,0 +1,139 @@
1
+ Metadata-Version: 2.4
2
+ Name: stdplus_ext
3
+ Version: 0.1.1
4
+ Summary: Numerically stable PyTorch stdplus activation for Slashed Normal parameterization
5
+ Project-URL: Repository, https://github.com/Yujia-Yan/stdplus_ext
6
+ Project-URL: Issues, https://github.com/Yujia-Yan/stdplus_ext/issues
7
+ Author: Yujia Yan
8
+ License-Expression: MIT
9
+ License-File: LICENSE
10
+ Keywords: pytorch,slashed-normal,special-functions,stdplus
11
+ Classifier: Intended Audience :: Science/Research
12
+ Classifier: Programming Language :: Python :: 3
13
+ Classifier: Programming Language :: Python :: 3 :: Only
14
+ Classifier: Topic :: Scientific/Engineering :: Mathematics
15
+ Requires-Python: >=3.9
16
+ Requires-Dist: torch>=2.6
17
+ Provides-Extra: dev
18
+ Requires-Dist: build>=1; extra == 'dev'
19
+ Requires-Dist: mpmath>=1.3; extra == 'dev'
20
+ Requires-Dist: pytest>=7; extra == 'dev'
21
+ Requires-Dist: twine>=6.1; extra == 'dev'
22
+ Description-Content-Type: text/markdown
23
+
24
+ # stdplus_ext
25
+
26
+ Differentiable PyTorch implementations of the **stdplus activation function**,
27
+ its logarithm, and their inverses. Slashed Normal uses stdplus to parameterize
28
+ the standard deviation of a normal posterior distribution through its
29
+ **KL amplitude**.
30
+
31
+ For $x \in \mathbb{R}$, $y = \mathrm{stdplus}(x)$ is the positive solution of
32
+
33
+ $$
34
+ \log(y^2) - y^2 = -x^2 - 1,
35
+ $$
36
+
37
+ choosing $y < 1$ for $x < 0$ and $y > 1$ for $x > 0$.
38
+ It is smooth and increasing, with $\mathrm{stdplus}(0) = 1$.
39
+
40
+ ## Installation
41
+
42
+ ```sh
43
+ pip install stdplus_ext
44
+ ```
45
+
46
+ Requires **Python 3.9+** and **PyTorch 2.6+**. Install your preferred CPU or CUDA
47
+ [PyTorch build](https://pytorch.org/get-started/locally/) first if needed.
48
+ Supported NVIDIA CUDA devices use Triton acceleration when available. Triton is
49
+ optional; CPU and installations without Triton use the PyTorch implementation.
50
+ CPU and NVIDIA CUDA are the tested platforms; other accelerator backends have
51
+ not been validated.
52
+
53
+ ## Quick start
54
+
55
+ ```python
56
+ import torch
57
+ from stdplus_ext import stdplus, log_stdplus, inv_stdplus, inv_log_stdplus
58
+
59
+ x = torch.tensor([-2.0, 0.0, 2.0], requires_grad=True)
60
+ sigma = stdplus(x)
61
+ log_sigma = log_stdplus(x)
62
+
63
+ recovered = inv_log_stdplus(log_sigma)
64
+ sigma.sum().backward()
65
+ ```
66
+
67
+ ## Functions
68
+
69
+ - `stdplus(x)` evaluates the stdplus activation function. Its mathematical range
70
+ is positive, although sufficiently negative inputs can underflow to zero.
71
+ - `log_stdplus(x)` evaluates $\log\mathrm{stdplus}(x)$ directly. Prefer it
72
+ for log densities and negative tails to avoid taking the logarithm of an
73
+ underflowed `stdplus` result.
74
+ - `inv_stdplus(y)` inverts `stdplus` for $y > 0$. It returns zero at one,
75
+ negative infinity at zero, and NaN for negative inputs.
76
+ - `inv_log_stdplus(z)` inverts `log_stdplus` on the real line and returns zero
77
+ at zero.
78
+
79
+ All functions accept dense, strided tensors with `float16`, `bfloat16`,
80
+ `float32`, or `float64` dtype and preserve shape, dtype, and device. Scalars,
81
+ empty tensors, slices, transposes, and expanded views are supported.
82
+ Half and bfloat16 inputs use float32 internal arithmetic.
83
+
84
+ NaNs propagate, and infinities follow the mathematical limits. Results outside
85
+ the output dtype's range can overflow or underflow. Inverse round trips lose
86
+ information when the forward value rounds to zero or one; use log space when
87
+ possible. Python numbers, integer tensors, complex tensors, and sparse tensors
88
+ are not supported.
89
+
90
+ ## Differentiation and compilation
91
+
92
+ Eager execution supports reverse-mode and forward-mode differentiation,
93
+ higher derivatives, and `torch.func` transforms including `vmap`, `jvp`,
94
+ `jacrev`, and `hessian`.
95
+
96
+ Compiled forward and first-order reverse-mode execution are supported:
97
+
98
+ ```python
99
+ compiled_stdplus = torch.compile(stdplus, fullgraph=True)
100
+ ```
101
+
102
+ Compilation can improve throughput, especially for large CPU tensors; the
103
+ first call includes compilation overhead. Combining compilation with higher
104
+ derivatives or `torch.func` transforms is not part of the supported contract.
105
+
106
+ ## Slashed Normal and KL amplitude
107
+
108
+ For a standard normal prior $\mathcal{N}(0, 1)$, the Slashed Normal
109
+ parameterization uses the complex KL amplitude $\psi = a + bi$ to specify the
110
+ normal posterior $\mathcal{N}(\mu, \sigma^2)$ through
111
+
112
+ $$
113
+ \mu = \sqrt{2}\,a, \qquad \sigma = \mathrm{stdplus}(\sqrt{2}\,b)
114
+ $$
115
+
116
+ The KL divergence from the posterior to the prior equals the squared modulus
117
+ of the KL amplitude:
118
+
119
+ $$
120
+ D_{\mathrm{KL}} \left(\mathcal{N}(\mu, \sigma^2)\,\middle\|\,\mathcal{N}(0, 1)\right)
121
+ = |\psi|^2 = a^2 + b^2.
122
+ $$
123
+
124
+ ## References
125
+
126
+ Yujia Yan, Xingjian Du, and Zhiyao Duan, *Slashed Normal: Parameterize Normal
127
+ Posterior Distributions with KL Amplitude*, 2024.
128
+ [Read the paper](https://openreview.net/pdf?id=6ifeGfWxtX).
129
+
130
+ For the updated description, see Yujia Yan, *Structured Analysis and Generation
131
+ in Music, Audio, and Beyond*, University of Rochester, 2025, Chapter 6:
132
+ Section 6.3.2 defines the KL amplitude and stdplus, and Section 6.6 describes
133
+ the activation function and its numerical evaluation.
134
+ [Read the dissertation](https://urresearch.rochester.edu/fileDownloadForInstitutionalItem.action?itemFileId=195735&itemId=39584).
135
+
136
+ ## License and source
137
+
138
+ MIT licensed. [Source code](https://github.com/Yujia-Yan/stdplus_ext) and
139
+ [issue tracker](https://github.com/Yujia-Yan/stdplus_ext/issues) are on GitHub.
@@ -0,0 +1,116 @@
1
+ # stdplus_ext
2
+
3
+ Differentiable PyTorch implementations of the **stdplus activation function**,
4
+ its logarithm, and their inverses. Slashed Normal uses stdplus to parameterize
5
+ the standard deviation of a normal posterior distribution through its
6
+ **KL amplitude**.
7
+
8
+ For $x \in \mathbb{R}$, $y = \mathrm{stdplus}(x)$ is the positive solution of
9
+
10
+ $$
11
+ \log(y^2) - y^2 = -x^2 - 1,
12
+ $$
13
+
14
+ choosing $y < 1$ for $x < 0$ and $y > 1$ for $x > 0$.
15
+ It is smooth and increasing, with $\mathrm{stdplus}(0) = 1$.
16
+
17
+ ## Installation
18
+
19
+ ```sh
20
+ pip install stdplus_ext
21
+ ```
22
+
23
+ Requires **Python 3.9+** and **PyTorch 2.6+**. Install your preferred CPU or CUDA
24
+ [PyTorch build](https://pytorch.org/get-started/locally/) first if needed.
25
+ Supported NVIDIA CUDA devices use Triton acceleration when available. Triton is
26
+ optional; CPU and installations without Triton use the PyTorch implementation.
27
+ CPU and NVIDIA CUDA are the tested platforms; other accelerator backends have
28
+ not been validated.
29
+
30
+ ## Quick start
31
+
32
+ ```python
33
+ import torch
34
+ from stdplus_ext import stdplus, log_stdplus, inv_stdplus, inv_log_stdplus
35
+
36
+ x = torch.tensor([-2.0, 0.0, 2.0], requires_grad=True)
37
+ sigma = stdplus(x)
38
+ log_sigma = log_stdplus(x)
39
+
40
+ recovered = inv_log_stdplus(log_sigma)
41
+ sigma.sum().backward()
42
+ ```
43
+
44
+ ## Functions
45
+
46
+ - `stdplus(x)` evaluates the stdplus activation function. Its mathematical range
47
+ is positive, although sufficiently negative inputs can underflow to zero.
48
+ - `log_stdplus(x)` evaluates $\log\mathrm{stdplus}(x)$ directly. Prefer it
49
+ for log densities and negative tails to avoid taking the logarithm of an
50
+ underflowed `stdplus` result.
51
+ - `inv_stdplus(y)` inverts `stdplus` for $y > 0$. It returns zero at one,
52
+ negative infinity at zero, and NaN for negative inputs.
53
+ - `inv_log_stdplus(z)` inverts `log_stdplus` on the real line and returns zero
54
+ at zero.
55
+
56
+ All functions accept dense, strided tensors with `float16`, `bfloat16`,
57
+ `float32`, or `float64` dtype and preserve shape, dtype, and device. Scalars,
58
+ empty tensors, slices, transposes, and expanded views are supported.
59
+ Half and bfloat16 inputs use float32 internal arithmetic.
60
+
61
+ NaNs propagate, and infinities follow the mathematical limits. Results outside
62
+ the output dtype's range can overflow or underflow. Inverse round trips lose
63
+ information when the forward value rounds to zero or one; use log space when
64
+ possible. Python numbers, integer tensors, complex tensors, and sparse tensors
65
+ are not supported.
66
+
67
+ ## Differentiation and compilation
68
+
69
+ Eager execution supports reverse-mode and forward-mode differentiation,
70
+ higher derivatives, and `torch.func` transforms including `vmap`, `jvp`,
71
+ `jacrev`, and `hessian`.
72
+
73
+ Compiled forward and first-order reverse-mode execution are supported:
74
+
75
+ ```python
76
+ compiled_stdplus = torch.compile(stdplus, fullgraph=True)
77
+ ```
78
+
79
+ Compilation can improve throughput, especially for large CPU tensors; the
80
+ first call includes compilation overhead. Combining compilation with higher
81
+ derivatives or `torch.func` transforms is not part of the supported contract.
82
+
83
+ ## Slashed Normal and KL amplitude
84
+
85
+ For a standard normal prior $\mathcal{N}(0, 1)$, the Slashed Normal
86
+ parameterization uses the complex KL amplitude $\psi = a + bi$ to specify the
87
+ normal posterior $\mathcal{N}(\mu, \sigma^2)$ through
88
+
89
+ $$
90
+ \mu = \sqrt{2}\,a, \qquad \sigma = \mathrm{stdplus}(\sqrt{2}\,b)
91
+ $$
92
+
93
+ The KL divergence from the posterior to the prior equals the squared modulus
94
+ of the KL amplitude:
95
+
96
+ $$
97
+ D_{\mathrm{KL}} \left(\mathcal{N}(\mu, \sigma^2)\,\middle\|\,\mathcal{N}(0, 1)\right)
98
+ = |\psi|^2 = a^2 + b^2.
99
+ $$
100
+
101
+ ## References
102
+
103
+ Yujia Yan, Xingjian Du, and Zhiyao Duan, *Slashed Normal: Parameterize Normal
104
+ Posterior Distributions with KL Amplitude*, 2024.
105
+ [Read the paper](https://openreview.net/pdf?id=6ifeGfWxtX).
106
+
107
+ For the updated description, see Yujia Yan, *Structured Analysis and Generation
108
+ in Music, Audio, and Beyond*, University of Rochester, 2025, Chapter 6:
109
+ Section 6.3.2 defines the KL amplitude and stdplus, and Section 6.6 describes
110
+ the activation function and its numerical evaluation.
111
+ [Read the dissertation](https://urresearch.rochester.edu/fileDownloadForInstitutionalItem.action?itemFileId=195735&itemId=39584).
112
+
113
+ ## License and source
114
+
115
+ MIT licensed. [Source code](https://github.com/Yujia-Yan/stdplus_ext) and
116
+ [issue tracker](https://github.com/Yujia-Yan/stdplus_ext/issues) are on GitHub.
@@ -0,0 +1,52 @@
1
+ [build-system]
2
+ requires = ["hatchling>=1.26"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "stdplus_ext"
7
+ version = "0.1.1"
8
+ description = "Numerically stable PyTorch stdplus activation for Slashed Normal parameterization"
9
+ readme = {file = "README.md", content-type = "text/markdown"}
10
+ requires-python = ">=3.9"
11
+ license = "MIT"
12
+ license-files = ["LICENSE"]
13
+ authors = [{name = "Yujia Yan"}]
14
+ dependencies = ["torch>=2.6"]
15
+ keywords = ["pytorch", "stdplus", "special-functions", "slashed-normal"]
16
+ classifiers = [
17
+ "Intended Audience :: Science/Research",
18
+ "Programming Language :: Python :: 3",
19
+ "Programming Language :: Python :: 3 :: Only",
20
+ "Topic :: Scientific/Engineering :: Mathematics",
21
+ ]
22
+
23
+ [project.urls]
24
+ Repository = "https://github.com/Yujia-Yan/stdplus_ext"
25
+ Issues = "https://github.com/Yujia-Yan/stdplus_ext/issues"
26
+
27
+ [project.optional-dependencies]
28
+ dev = ["pytest>=7", "mpmath>=1.3", "build>=1", "twine>=6.1"]
29
+
30
+ # The public README is embedded in metadata; repository docs stay in Git.
31
+ [tool.hatch.build.targets.wheel]
32
+ # SPDX licensing needs 2.4; retain compatibility with Twine 6.1/6.2.
33
+ core-metadata-version = "2.4"
34
+ include = ["/src/stdplus_ext/**/*.py", "/src/stdplus_ext/py.typed"]
35
+ sources = ["src"]
36
+
37
+ # Keep tests for downstream validation, but exclude internal docs/benchmarks.
38
+ [tool.hatch.build.targets.sdist]
39
+ core-metadata-version = "2.4"
40
+ include = [
41
+ "/src/stdplus_ext/**/*.py",
42
+ "/src/stdplus_ext/py.typed",
43
+ "/tests/**/*.py",
44
+ "/pyproject.toml",
45
+ "/README.md",
46
+ "/LICENSE",
47
+ ]
48
+
49
+ [tool.pytest.ini_options]
50
+ testpaths = ["tests"]
51
+ pythonpath = ["src", "tests"]
52
+ markers = ["compile: compiler integration tests (enabled by --run-compile)"]
@@ -1,3 +1,5 @@
1
+ """The stdplus activation function, its logarithm, and their inverses for PyTorch."""
2
+
1
3
  from .stdplus import stdplus, log_stdplus, inv_log_stdplus, inv_stdplus
2
4
 
3
5