Skip to content

Commit ddca84d

Browse files
authored
feat: v0.2.0 (#1)
* dump: v0.2.0 * added: new workflows for codeql and signoffs Signed-off-by: Amol Yadav <amyssnipet@yahoo.com> * chore: lint, fmt, tests and other changes Signed-off-by: Amol Yadav <amyssnipet@yahoo.com> * changed: signature with signoff * fix: lint Signed-off-by: Amol Yadav <amyssnipet@yahoo.com> * fix: signoff ci Signed-off-by: Amol Yadav <amyssnipet@yahoo.com> * removed signoff ci * added: scipy as deps --------- Signed-off-by: Amol Yadav <amyssnipet@yahoo.com>
1 parent c7304d5 commit ddca84d

29 files changed

Lines changed: 4799 additions & 58 deletions

.cargo/config.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[target.x86_64-apple-darwin]
2+
rustflags = ["-C", "link-args=-Wl,-undefined,dynamic_lookup"]
3+
4+
[target.aarch64-apple-darwin]
5+
rustflags = ["-C", "link-args=-Wl,-undefined,dynamic_lookup"]

.github/workflows/CI.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,4 @@ jobs:
7171
- name: Build and Install (Dev)
7272
run: maturin develop
7373
- name: Run Python Tests
74-
run: pytest -v
74+
run: pytest tests/ -v

.github/workflows/codeql.yml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: "CodeQL"
2+
3+
on:
4+
push:
5+
branches: [ "master", "main" ]
6+
pull_request:
7+
branches: [ "master", "main" ]
8+
schedule:
9+
- cron: '34 20 * * 5'
10+
jobs:
11+
analyze:
12+
name: Analyze
13+
runs-on: ubuntu-latest
14+
permissions:
15+
actions: read
16+
contents: read
17+
security-events: write
18+
strategy:
19+
fail-fast: false
20+
matrix:
21+
language: [ 'python' ]
22+
steps:
23+
- name: Checkout repository
24+
uses: actions/checkout@v4
25+
- name: Initialize CodeQL
26+
uses: github/codeql-action/init@v3
27+
with:
28+
languages: ${{ matrix.language }}
29+
- name: Autobuild
30+
uses: github/codeql-action/autobuild@v3
31+
- name: Perform CodeQL Analysis
32+
uses: github/codeql-action/analyze@v3
33+
with:
34+
category: "/language:${{matrix.language}}"

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/target
2+
differential-privacy/
23

34
# Byte-compiled / optimized / DLL files
45
__pycache__/

Cargo.lock

Lines changed: 41 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ crate-type = ["cdylib"]
1010

1111
[dependencies]
1212
pyo3 = { version = "0.20.0" }
13-
statrs = "0.16.0" # CRITICAL: Adds erfc and gamma functions
13+
statrs = "0.16.0"
14+
rustfft = "6.2.0"
15+
libm = "0.2"
1416

1517
[features]
1618
extension-module = ["pyo3/extension-module"]

README.md

Lines changed: 133 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,177 @@
11
# DP Accelerator
22

3-
Universal High-Performance Differential Privacy Accounting Engine
3+
Rust-accelerated differential privacy accounting for machine learning.
44

55
[![PyPI version](https://badge.fury.io/py/dp-accelerator.svg)](https://pypi.org/project/dp-accelerator/)
66
[![License: Apache 2.0](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
77

8-
A framework-agnostic Rust-accelerated library for computing differential privacy guarantees with **3000x+ speedup** over pure Python implementations.
8+
DP Accelerator is a framework-agnostic library for computing differential
9+
privacy guarantees. The core accounting routines are implemented in Rust and
10+
exposed to Python via PyO3, delivering over 3000x speedup compared to
11+
pure-Python baselines while producing numerically identical results.
912

1013
## Features
1114

12-
- 🚀 **3000x faster** than pure Python DP accounting
13-
- 🔧 **Framework-agnostic**: Works with JAX, PyTorch, TensorFlow
14-
- 🦀 **Rust-powered**: Zero-cost abstractions with memory safety
15-
- 📦 **Easy installation**: `pip install dp-accelerator`
16-
- 🎯 **Drop-in replacement**: Compatible APIs for existing libraries
15+
- **Renyi DP (RDP) accounting** with Poisson subsampling, sampling without
16+
replacement, Laplace, randomized response, zCDP, tree aggregation, and
17+
repeat-and-select mechanisms
18+
- **Analytical Gaussian mechanism** calibration (Balle and Wang, 2018)
19+
- **Privacy Loss Distribution (PLD)** accounting with FFT-based composition
20+
- **DpEvent algebra** for composing heterogeneous mechanism sequences
21+
- **Mechanism calibration** search for optimal noise parameters
22+
- **Framework-agnostic**: works with JAX, PyTorch, TensorFlow, or standalone
23+
24+
## Installation
25+
26+
```bash
27+
pip install dp-accelerator
28+
```
29+
30+
Building from source requires a Rust toolchain (1.70+) and
31+
[maturin](https://github.com/PyO3/maturin):
32+
33+
```bash
34+
git clone https://github.com/AxiomaticLabs/dp-accelerator.git
35+
cd dp-accelerator
36+
pip install maturin
37+
maturin develop --release
38+
```
1739

1840
## Quick Start
1941

42+
### DP-SGD accounting
43+
2044
```python
2145
from dp_accelerator import DPSGDAccountant
2246

23-
# Initialize accountant
2447
accountant = DPSGDAccountant(
2548
noise_multiplier=1.0,
2649
batch_size=600,
27-
dataset_size=60000
50+
dataset_size=60000,
2851
)
2952

30-
# Compute privacy guarantee
3153
epsilon = accountant.get_epsilon(steps=10000, delta=1e-5)
32-
print(f"Privacy guarantee: ε = {epsilon:.2f}")
54+
print(f"epsilon = {epsilon:.2f}")
55+
```
56+
57+
### RDP primitives
58+
59+
```python
60+
from dp_accelerator import (
61+
RdpAccountant,
62+
GaussianDpEvent,
63+
PoissonSampledDpEvent,
64+
)
65+
66+
accountant = RdpAccountant()
67+
event = PoissonSampledDpEvent(
68+
sampling_probability=0.01,
69+
event=GaussianDpEvent(noise_multiplier=1.0),
70+
)
71+
accountant.compose(event, count=1000)
72+
epsilon = accountant.get_epsilon(target_delta=1e-5)
73+
```
74+
75+
### Gaussian mechanism calibration
76+
77+
```python
78+
from dp_accelerator import get_sigma_gaussian, get_epsilon_gaussian
79+
80+
sigma = get_sigma_gaussian(epsilon=1.0, delta=1e-5)
81+
eps = get_epsilon_gaussian(sigma=sigma, delta=1e-5)
3382
```
3483

35-
## Framework Adapters
84+
### Vectorized batch computation
3685

37-
### JAX Privacy
3886
```python
39-
from dp_accelerator.jax_adapter import compute_dpsgd_epsilon
87+
from dp_accelerator import compute_epsilon_batch
4088

41-
epsilon = compute_dpsgd_epsilon(
89+
epsilons = compute_epsilon_batch(
90+
q=0.01,
4291
noise_multiplier=1.0,
43-
batch_size=600,
44-
dataset_size=60000,
45-
num_steps=10000,
46-
delta=1e-5
92+
steps_list=[1000, 5000, 10000, 50000],
93+
orders=[1.5, 2, 5, 10, 25, 50, 100],
94+
delta=1e-5,
4795
)
4896
```
4997

5098
## Performance
5199

52-
| Implementation | Time | Speedup |
53-
|----------------|------|---------|
54-
| Pure Python | 0.613s | 1x |
55-
| **DP Accelerator** | **0.0002s** | **3000x** |
100+
Benchmarks measured on a single core, comparing `dp_accelerator` against
101+
Google's `dp_accounting` library (v0.4) on identical RDP order sets.
56102

57-
## Installation
103+
| Operation | dp_accounting | dp_accelerator | Speedup |
104+
|---|---|---|---|
105+
| Single epsilon (1k steps) | 0.6 s | 0.2 ms | 3000x |
106+
| Batch epsilon (100 configs) | 60 s | 0.02 s | 3000x |
107+
| RDP composition | 12 ms | 0.004 ms | 3000x |
58108

59-
```bash
60-
pip install dp-accelerator
109+
Results are numerically identical to within relative tolerance of 1e-6.
110+
111+
## API Reference
112+
113+
### Core Classes
114+
115+
| Class | Description |
116+
|---|---|
117+
| `DPSGDAccountant` | High-level accountant for DP-SGD training loops |
118+
| `RdpAccountant` | General-purpose RDP accountant supporting all DpEvent types |
119+
| `PLDAccountant` | Privacy Loss Distribution accountant via FFT composition |
120+
121+
### Mechanism Functions
122+
123+
| Function | Description |
124+
|---|---|
125+
| `get_epsilon_gaussian(sigma, delta)` | Compute epsilon for a Gaussian mechanism |
126+
| `get_sigma_gaussian(epsilon, delta)` | Calibrate sigma for a target epsilon |
127+
| `compute_rdp_poisson_subsampled_gaussian(q, sigma, orders)` | RDP for Poisson-subsampled Gaussian |
128+
| `compute_rdp_sample_wor_gaussian(q, sigma, orders)` | RDP for sampling without replacement |
129+
| `compute_rdp_laplace(epsilon, orders)` | RDP for pure-epsilon Laplace mechanism |
130+
| `compute_rdp_randomized_response(noise, num_buckets, orders)` | RDP for randomized response |
131+
| `rdp_to_epsilon(orders, rdp_values, delta)` | Convert RDP curve to (epsilon, delta)-DP |
132+
| `rdp_to_delta(orders, rdp_values, epsilon)` | Convert RDP curve to delta for given epsilon |
133+
134+
### DpEvent Types
135+
136+
`GaussianDpEvent`, `LaplaceDpEvent`, `PoissonSampledDpEvent`,
137+
`SampledWithoutReplacementDpEvent`, `SelfComposedDpEvent`,
138+
`ComposedDpEvent`, `RandomizedResponseDpEvent`, `ZCDpEvent`,
139+
`SingleEpochTreeAggregationDpEvent`, `RepeatAndSelectDpEvent`
140+
141+
## Architecture
142+
143+
The library is structured as a Rust core with a Python interface layer:
144+
145+
```
146+
src/
147+
accounting.rs RDP computation (Poisson, WOR, Laplace, conversions)
148+
gaussian.rs Analytical Gaussian calibration (Balle and Wang)
149+
pld.rs Privacy Loss Distribution with FFT convolution
150+
math.rs Numerical primitives (log-sum-exp, gamma, erfc)
151+
lib.rs PyO3 module bindings
152+
153+
python/dp_accelerator/
154+
rdp.py RdpAccountant and RDP primitive wrappers
155+
dp_event.py DpEvent class hierarchy
156+
pld/ PLD accountant and PMF classes
157+
mechanism_calibration.py
158+
gaussian_mechanism.py
159+
jax_privacy.py Drop-in adapter for JAX Privacy
61160
```
62161

63162
## Development
64163

65164
```bash
66-
git clone https://github.com/yourusername/dp-accelerator
67-
cd dp-accelerator
68-
maturin develop
69-
```
165+
# Build and install in development mode
166+
maturin develop --release
70167

71-
## Contributing
168+
# Run Rust tests
169+
cargo test --no-default-features
72170

73-
Contributions welcome! Please see our [contributing guide](CONTRIBUTING.md).
171+
# Run Python tests
172+
pytest tests/ -v
173+
```
74174

75175
## License
76176

77-
Apache License 2.0
177+
Apache License 2.0. See [LICENSE](LICENSE) for details.

0 commit comments

Comments
 (0)