Skip to main content

DifferentiallyPrivateOptimizer

Struct DifferentiallyPrivateOptimizer 

Source
pub struct DifferentiallyPrivateOptimizer<O, A, D>
where A: Float + ScalarOperand + Debug + Send + Sync, D: Dimension, O: Optimizer<A, D>,
{ /* private fields */ }
Expand description

Differentially private optimizer wrapper

Implementations§

Source§

impl<O, A, D> DifferentiallyPrivateOptimizer<O, A, D>

Source

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).

Source

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:

  1. every per-example gradient is clipped to the current L2 threshold C, bounding one example’s influence on the sum by C;
  2. the clipped gradients are summed;
  3. 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);
  4. 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.

Source

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.

Source

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.

Source

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.

Source

pub fn has_privacy_budget(&self) -> Result<bool>

Whether at least one more step fits inside the epsilon budget.

Source

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.

Source

pub fn accounting_segments(&self) -> &[AccountingSegment]

Immutable view of the accountant’s segment ledger.

Source

pub fn config(&self) -> &DifferentialPrivacyConfig

The configuration in force.

Source

pub fn get_clipping_threshold(&self) -> f64

Current clipping threshold (adaptive if enabled).

Source

pub fn get_clipping_stats(&self) -> ClippingStats

Gradient clipping statistics.

Source

pub fn get_audit_trail(&self) -> &[PrivacyEvent]

Privacy audit trail.

Source

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>
where O: Unpin, A: Unpin, D: Unpin,

§

impl<O, A, D> UnsafeUnpin for DifferentiallyPrivateOptimizer<O, A, D>
where O: UnsafeUnpin,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V