Skip to content

GradClipScheduler updates optimizer.max_grad_norm without updating ghost-clipping bound #865

Open

Description

@tudorcebere

Summary

In ghost clipping mode (grad_sample_mode="ghost"), gradient-clip schedulers such as ExponentialGradClip update optimizer.max_grad_norm (which controls the Gaussian noise standard deviation), while GradSampleModuleFastGradientClipping and DPLossFastGradientClipping continue clipping per-sample gradients using their cached initial max_grad_norm. As optimizer.max_grad_norm decays, noise shrinks without correspondingly reducing clipping sensitivity.

Privacy Impact

In grad_sample_mode="ghost", attaching a decreasing gradient-clip scheduler such as ExponentialGradClip reduces optimizer.max_grad_norm—and therefore the Gaussian noise standard deviation—while GradSampleModuleFastGradientClipping keeps clipping every example to the initial max_grad_norm. After three halvings (1.0 -> 0.125), each record is still clipped to 1.0000 while noise is calibrated for 0.1250, reducing the effective noise multiplier by 8x and breaking the accounted (epsilon, delta) bound. Standard hooks mode and runs without a clip scheduler are unaffected.

Upstream Status

Open upstream PR exists (unmerged): meta-pytorch/opacus#847 ("Patch stale clipping bound for FastGradientClipping", opened 2026-10-06). Monitor PR #847 rather than opening a duplicate.

Affected version

  • Audited commit: df040e4a1594ee8ae706679db06c9981eec9bf4c (main)
  • Latest PyPI release: opacus==1.6.0

Reproduction

# Gradient-clip scheduler with ghost clipping: after scheduler.step() the noise is scaled to
# optimizer.max_grad_norm, but per-sample gradients are still clipped to the initial value.
import warnings
import torch, torch.nn as nn
from torch.utils.data import DataLoader, TensorDataset
from opacus import PrivacyEngine
from opacus.schedulers import ExponentialGradClip

warnings.filterwarnings("ignore")
B = 16
torch.manual_seed(1)
# Every record is identical and has a large gradient, so
# ||sum of clipped per-sample grads|| / B is the norm each record is actually clipped to.
x, y = (100 * torch.randn(1, 8)).repeat(B, 1), torch.zeros(B, dtype=torch.long)

print(f"opacus {__import__('opacus').__version__}, torch {torch.__version__}")
for mode in ["hooks", "ghost"]:
    torch.manual_seed(0)
    model = nn.Linear(8, 4)
    opt = torch.optim.SGD(model.parameters(), lr=0.0)  # lr=0: same gradients every epoch
    out = PrivacyEngine().make_private(
        module=model, optimizer=opt, data_loader=DataLoader(TensorDataset(x, y), batch_size=B),
        criterion=nn.CrossEntropyLoss(), noise_multiplier=0.0, max_grad_norm=1.0,
        grad_sample_mode=mode, poisson_sampling=False,
    )
    model, opt, criterion = (out[0], out[1], out[2]) if mode == "ghost" else (out[0], out[1], nn.CrossEntropyLoss())
    scheduler = ExponentialGradClip(opt, gamma=0.5)
    for epoch in range(4):
        opt.zero_grad()
        criterion(model(x), y).backward()
        opt.step()  # noise_multiplier=0: p.grad is now (sum of clipped per-sample grads) / B
        clipped_to = torch.cat([p.grad.flatten() for p in model.parameters()]).norm().item()
        print(f"{mode:5s} epoch {epoch}: optimizer.max_grad_norm (noise std = noise_multiplier * this) = "
              f"{opt.max_grad_norm:.4f}   norm each record is actually clipped to = {clipped_to:.4f}")
        scheduler.step()

Observed output:

opacus 1.6.0, torch 2.6.0+cpu
hooks epoch 0: optimizer.max_grad_norm (noise std = noise_multiplier * this) = 1.0000   norm each record is actually clipped to = 1.0000
hooks epoch 1: optimizer.max_grad_norm (noise std = noise_multiplier * this) = 0.5000   norm each record is actually clipped to = 0.5000
hooks epoch 2: optimizer.max_grad_norm (noise std = noise_multiplier * this) = 0.2500   norm each record is actually clipped to = 0.2500
hooks epoch 3: optimizer.max_grad_norm (noise std = noise_multiplier * this) = 0.1250   norm each record is actually clipped to = 0.1250
ghost epoch 0: optimizer.max_grad_norm (noise std = noise_multiplier * this) = 1.0000   norm each record is actually clipped to = 1.0000
ghost epoch 1: optimizer.max_grad_norm (noise std = noise_multiplier * this) = 0.5000   norm each record is actually clipped to = 1.0000
ghost epoch 2: optimizer.max_grad_norm (noise std = noise_multiplier * this) = 0.2500   norm each record is actually clipped to = 1.0000
ghost epoch 3: optimizer.max_grad_norm (noise std = noise_multiplier * this) = 0.1250   norm each record is actually clipped to = 1.0000

Root cause

GradSampleModuleFastGradientClipping and DPLossFastGradientClipping store max_grad_norm as an independent scalar copy at initialization rather than reading optimizer.max_grad_norm dynamically when GradClipScheduler.step() mutates it.

Impact

Any ghost-clipping run using a decreasing GradClipScheduler adds Gaussian noise calibrated to the smaller scheduled max_grad_norm (e.g., 0.125) while still clipping per-sample gradients to the larger initial bound (1.0).

Automated Privacy Audit Finding (AI-Generated): This finding is the output of a custom harness for privacy auditing. We ran the harness on this library because it is a production-grade library that processes privacy-sensitive data. I have reviewed and verified this finding, and I take responsibility for it. Feedback on the finding is very welcome. If you would prefer this library to be excluded from future automated audits, reply here and we will opt it out.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions