pub struct DifferentiallyPrivateOptimizer<O, A, D>{ /* private fields */ }Expand description
Differentially private optimizer wrapper
Implementations§
Source§impl<O, A, D> DifferentiallyPrivateOptimizer<O, A, D>
impl<O, A, D> DifferentiallyPrivateOptimizer<O, A, D>
Sourcepub fn new(baseoptimizer: O, config: DifferentialPrivacyConfig) -> Result<Self>
pub fn new(baseoptimizer: O, config: DifferentialPrivacyConfig) -> Result<Self>
Create a new differentially private optimizer.
The configuration is validated up front, and the accountant named by
config.accounting_method is constructed (an unimplemented method is
an error, never a silent substitution).
Sourcepub fn dp_step_per_example(
&mut self,
params: &Array<A, D>,
per_example_gradients: &[Array<A, D>],
) -> Result<Array<A, D>>
pub fn dp_step_per_example( &mut self, params: &Array<A, D>, per_example_gradients: &[Array<A, D>], ) -> Result<Array<A, D>>
Perform a differentially private step from per-example gradients.
This is the entry point that delivers the standard DP-SGD guarantee:
- every per-example gradient is clipped to the current L2 threshold
C, bounding one example’s influence on the sum byC; - the clipped gradients are summed;
N(0, sigma^2 C^2)noise is added to the sum (adding it after the division would silently reduce the effective sigma by the batch size);- the noisy sum is divided by the batch size.
Adjacency: add/remove-one-example, for which the L2 sensitivity of the
clipped sum is exactly C.
The privacy budget is enforced before the noisy gradient is
released: if composing this step would push epsilon past
target_epsilon, the call returns
OptimError::PrivacyBudgetExhausted and nothing is released.
Sourcepub fn dp_step_presummed(
&mut self,
params: &Array<A, D>,
summed_clipped_gradients: &Array<A, D>,
batch_size: usize,
) -> Result<Array<A, D>>
pub fn dp_step_presummed( &mut self, params: &Array<A, D>, summed_clipped_gradients: &Array<A, D>, batch_size: usize, ) -> Result<Array<A, D>>
Perform a differentially private step from a pre-clipped sum of per-example gradients.
The caller asserts that summed_clipped_gradients is the sum of
batch_size per-example gradients, each already clipped to
DifferentialPrivacyConfig::l2_norm_clip. Noise N(0, sigma^2 C^2)
is added to the sum, which is then divided by batch_size.
Use this when per-example gradients are produced by an external framework and materialising them all is impractical.
Sourcepub fn dp_step(
&mut self,
params: &Array<A, D>,
gradients: &mut Array<A, D>,
) -> Result<Array<A, D>>
pub fn dp_step( &mut self, params: &Array<A, D>, gradients: &mut Array<A, D>, ) -> Result<Array<A, D>>
Perform a step that clips the already aggregated gradient.
§This does not provide per-example differential privacy
Clipping a batch-mean (or batch-sum) gradient bounds the influence of the whole batch, not of any single example, so the standard DP-SGD analysis – and the epsilon this optimizer reports – does not apply under example-level adjacency. One outlier example can still move the aggregate arbitrarily far inside the clipping ball.
The entry point is retained for batch-level adjacency (neighbouring
datasets differing in an entire batch) and for reproducing legacy
behaviour. Because subsampling amplification does not apply under that
adjacency, the step is accounted with sampling probability q = 1,
which is strictly more conservative than the per-example path.
It returns an error unless
DifferentialPrivacyConfig::acknowledge_aggregate_clipping is set,
so nobody gets this behaviour by accident. Prefer
Self::dp_step_per_example.
Sourcepub fn consumed_epsilon(&self) -> Result<f64>
pub fn consumed_epsilon(&self) -> Result<f64>
Epsilon consumed so far.
Errors from the accountant are propagated rather than swallowed: an accounting failure must never be reported as “zero spent”, which would let training continue with no budget enforcement at all.
Sourcepub fn has_privacy_budget(&self) -> Result<bool>
pub fn has_privacy_budget(&self) -> Result<bool>
Whether at least one more step fits inside the epsilon budget.
Sourcepub fn get_privacy_budget(&self) -> Result<PrivacyBudget>
pub fn get_privacy_budget(&self) -> Result<PrivacyBudget>
Current privacy budget status.
Returns an error if the accountant cannot produce a number – failing closed instead of reporting a fabricated zero spend.
Sourcepub fn accounting_segments(&self) -> &[AccountingSegment]
pub fn accounting_segments(&self) -> &[AccountingSegment]
Immutable view of the accountant’s segment ledger.
Sourcepub fn config(&self) -> &DifferentialPrivacyConfig
pub fn config(&self) -> &DifferentialPrivacyConfig
The configuration in force.
Sourcepub fn get_clipping_threshold(&self) -> f64
pub fn get_clipping_threshold(&self) -> f64
Current clipping threshold (adaptive if enabled).
Sourcepub fn get_clipping_stats(&self) -> ClippingStats
pub fn get_clipping_stats(&self) -> ClippingStats
Gradient clipping statistics.
Sourcepub fn get_audit_trail(&self) -> &[PrivacyEvent]
pub fn get_audit_trail(&self) -> &[PrivacyEvent]
Privacy audit trail.
Sourcepub fn validate_privacy(&self) -> Result<PrivacyValidation>
pub fn validate_privacy(&self) -> Result<PrivacyValidation>
Validate privacy guarantees against the configured budget.
Auto Trait Implementations§
impl<O, A, D> !RefUnwindSafe for DifferentiallyPrivateOptimizer<O, A, D>
impl<O, A, D> !Send for DifferentiallyPrivateOptimizer<O, A, D>
impl<O, A, D> !Sync for DifferentiallyPrivateOptimizer<O, A, D>
impl<O, A, D> !UnwindSafe for DifferentiallyPrivateOptimizer<O, A, D>
impl<O, A, D> Freeze for DifferentiallyPrivateOptimizer<O, A, D>where
O: Freeze,
impl<O, A, D> Unpin for DifferentiallyPrivateOptimizer<O, A, D>
impl<O, A, D> UnsafeUnpin for DifferentiallyPrivateOptimizer<O, A, D>where
O: UnsafeUnpin,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.