Ghost clipping is a memory-efficient technique for per-sample gradient clipping in DP-SGD that avoids materializing individual per-sample gradients. Instead of computing and storing the gradient for each example in a mini-batch separately—which incurs prohibitive memory overhead for large models—ghost clipping computes the per-sample gradient norms directly from the per-sample activation and output-layer gradients using an analytical formula, then scales the batched gradient accordingly.
Glossary
Ghost Clipping

What is Ghost Clipping?
A computational technique that enforces per-sample gradient clipping in differentially private training without explicitly instantiating the gradient tensor for each individual sample in a batch.
The method exploits the chain rule to express the gradient norm of a linear layer's weights as the outer product of the input activation norm and the output gradient norm. By computing these norms per sample and applying the clipping factor to the aggregated batch gradient, ghost clipping achieves the same privacy guarantee as naive per-sample clipping while reducing peak memory usage from O(B * P) to O(B + P), where B is batch size and P is parameter count.
Key Features of Ghost Clipping
Ghost clipping is a computational technique that eliminates the memory bottleneck of traditional per-sample gradient clipping in differentially private training. By avoiding the materialization of individual per-sample gradients, it enables DP-SGD on large models without prohibitive hardware requirements.
Per-Example Gradient Norm Calculation Without Materialization
The core innovation of ghost clipping is computing the gradient norm of each individual sample in a batch without ever instantiating the full per-sample gradient tensor. Traditional DP-SGD requires storing a separate gradient for every sample in the batch, causing memory to scale linearly with batch size. Ghost clipping uses a double backpropagation trick or efficient norm computation via the Jacobian-vector product to derive the per-sample norm directly from the loss, reducing peak memory from O(B*P) to O(P) where B is batch size and P is model parameters.
- Key mechanism: Computes the gradient of the loss norm rather than the norm of the gradient
- Memory impact: Eliminates the need to store B copies of the model gradient simultaneously
- Enabling factor: Makes large-batch DP-SGD feasible on a single GPU for models like GPT-2 and ResNet-152
Double Backpropagation Mechanics
Ghost clipping leverages higher-order automatic differentiation by computing a second derivative through the computational graph. The process works in two passes: first, the standard forward pass computes per-sample losses; second, a gradient-of-gradient operation computes the squared L2 norm of each sample's gradient vector. This is achieved by taking the gradient of a scalar proxy—the dot product of the batch loss gradient with itself—with respect to the model parameters. The resulting computation yields exact per-sample norms without materializing the intermediate per-sample gradient tensors.
- Mathematical foundation: Uses the identity ||∇L_i||² = ∇_θ (∇_θ L_i · v) evaluated at v = ∇_θ L_i
- Framework support: Implemented in libraries like Opacus (via
Functorch) and JAX (viavmap+grad) - Computational cost: Requires roughly 2x the backward passes of standard SGD, but memory savings often outweigh this overhead
Group-Scale Clipping for Transformer Models
For large transformer architectures, ghost clipping can be extended to operate at the sub-layer or parameter-group level rather than computing a single global per-sample norm. This group-wise ghost clipping clips the gradients of each layer or attention head independently before aggregation, which has been shown to improve the privacy-utility tradeoff in language models. By applying different clipping thresholds to different parameter groups, the technique prevents over-clipping of sensitive layers while maintaining tight privacy bounds.
- Granularity options: Per-layer, per-attention-head, or per-parameter-matrix clipping
- Benefit: Preserves important gradient signals in critical layers that would otherwise be destroyed by a global clipping threshold
- Implementation: Requires computing separate per-sample norms for each designated parameter group, which ghost clipping handles without additional memory overhead
Integration with DP-SGD Privacy Accounting
Ghost clipping is a drop-in replacement for the per-sample clipping step in the standard DP-SGD algorithm and integrates seamlessly with existing privacy accounting frameworks. After ghost clipping computes the per-sample norms, the gradients are clipped to a maximum norm C, aggregated, and perturbed with Gaussian noise calibrated to the privacy budget (ε, δ). The privacy accountant—whether using Rényi DP, Moments Accountant, or Gaussian DP—operates identically because the output distribution of the clipped and noised gradients is indistinguishable from traditional DP-SGD.
- Compatibility: Works with all major privacy accountants (RDP, GDP, MA)
- Noise calibration: Standard σ = C · z / ε scaling applies unchanged
- Auditability: The clipping and noising steps remain transparent to privacy auditors
Performance Benchmarks and Scaling Behavior
Empirical evaluations demonstrate that ghost clipping reduces the peak memory footprint of DP-SGD by up to 90% for large batch sizes compared to the naive per-sample gradient approach. On a ResNet-50 trained with batch size 256 on CIFAR-10, ghost clipping uses approximately 2.1 GB of GPU memory versus 18.4 GB for the materialized approach. The technique scales to models with hundreds of millions of parameters on a single accelerator, making differential privacy practical for fine-tuning large language models where memory was previously the binding constraint.
- Throughput tradeoff: Typically 1.5-2.5x slower per step due to the double backward pass
- Scaling efficiency: Memory savings increase proportionally with batch size
- Hardware accessibility: Enables DP training on consumer GPUs (e.g., RTX 3090) that would otherwise require data-center accelerators
Ghost Norm vs. Ghost Clipping Variants
The literature distinguishes between ghost clipping (computing per-sample norms for clipping) and the more general ghost norm technique (computing any per-sample scalar quantity without materialization). Ghost norm extends the principle to other applications such as per-sample gradient normalization, adaptive learning rates, and loss reweighting based on individual sample difficulty. Both variants rely on the same underlying Jacobian-vector product machinery but serve different purposes in the privacy and optimization pipeline.
- Ghost clipping: Specifically computes per-sample gradient norms for the clipping operation in DP-SGD
- Ghost norm: Generalizes to any per-sample scalar derived from gradients (e.g., gradient variance, curvature estimates)
- Shared infrastructure: Both use efficient higher-order autodiff, so implementing one typically enables the other
Ghost Clipping vs. Standard Per-Sample Clipping
A technical comparison of memory footprint, computational graph overhead, and operational mechanics between Ghost Clipping and standard per-sample gradient clipping in DP-SGD.
| Feature | Ghost Clipping | Standard Per-Sample Clipping |
|---|---|---|
Per-sample gradient materialization | ||
Peak memory complexity | O(batch_size) for norms only | O(batch_size × parameters) |
Computational graph strategy | Double backpropagation on per-sample loss norms | Sequential backpropagation on each sample |
Memory overhead vs. non-private SGD | ~1.2-1.5x | ~5-10x |
Maximum practical batch size on single GPU | Thousands of samples | Hundreds of samples |
Gradient norm computation | Implicit via second-order differentiation | Explicit per-sample norm calculation |
Compatibility with mixed-precision training | ||
Numerical precision of clipping threshold | Equivalent to standard within floating-point tolerance | Exact |
Frequently Asked Questions
Clear, technical answers to the most common questions about ghost clipping, its mechanism, and its role in memory-efficient differentially private deep learning.
Ghost clipping is a memory-efficient technique for computing per-sample gradient norms in differentially private stochastic gradient descent (DP-SGD) without explicitly materializing the per-sample gradients in memory. The standard DP-SGD workflow requires computing the gradient for each sample in a batch, clipping each to a maximum L2 norm, then averaging and adding noise. This materialization creates a memory bottleneck because the per-sample gradient tensor has dimensions [batch_size, ...], which is prohibitively large for models with millions of parameters. Ghost clipping circumvents this by exploiting the chain rule and linear algebra to compute the per-sample gradient norms directly from the per-sample loss gradients with respect to the activations (the output layer gradients) and the model's weight matrices. Specifically, for a linear layer, the per-sample gradient norm can be expressed as the product of the input activation norm and the output gradient norm. By recursively applying this decomposition through the network, ghost clipping computes all per-sample norms in a single backward pass without ever storing full per-sample parameter gradients, reducing peak memory from O(batch_size * parameters) to O(batch_size * activations).
Enabling Efficiency, Speed & Accuracy
Intelligent Analysis, Decision & Execution
We build AI systems for teams that need search across company data, workflow automation across tools, or AI features inside products and internal software.
Talk to Us
Search across company data
Give teams answers from docs, tickets, runbooks, and product data with sources and permissions.
Useful when people spend too long searching or get different answers from different systems.

Automate internal workflows
Use AI to route work, draft outputs, trigger actions, and keep approvals and logs in place.
Useful when repetitive work moves across multiple tools and teams.

Add AI to products and internal tools
Build assistants, guided actions, or decision support into the software your team or customers already use.
Useful when AI needs to be part of the product, not a separate tool.
Related Terms
Ghost clipping is a memory-efficient technique for per-sample gradient clipping in DP-SGD. The following concepts form the core technical landscape surrounding this privacy-preserving optimization.
DP-SGD
The foundational training algorithm that integrates differential privacy into stochastic gradient descent. DP-SGD operates by clipping per-sample gradients to bound sensitivity, then adding calibrated Gaussian noise to the aggregated batch gradient. Ghost clipping is a direct optimization of this algorithm's memory footprint, eliminating the need to materialize individual per-sample gradients during the clipping step.
Per-Sample Gradient Clipping
The core operation that bounds the influence of any single training example on the model update. Each sample's gradient is computed independently and its L2 norm is clipped to a maximum threshold C. Ghost clipping reformulates this operation using second-order backpropagation tricks, computing per-sample gradient norms without instantiating the full per-sample gradient tensors in memory.
Privacy Accountant
An algorithmic component that tracks cumulative privacy loss across training iterations. It composes the privacy expenditure of each DP-SGD step using frameworks like Rényi Differential Privacy (RDP) or Gaussian Differential Privacy (GDP). Ghost clipping does not alter the privacy accounting mathematics—it produces identical gradient statistics to naive per-sample clipping, ensuring full compatibility with existing privacy budget tracking.
Differential Privacy (DP)
A mathematical framework providing provable privacy guarantees by ensuring the output distribution of an algorithm is nearly indistinguishable regardless of any single record's inclusion or exclusion. The privacy loss is quantified by the parameter epsilon (ε). Ghost clipping serves as an efficiency-enabling technique that makes DP training practical at scale without sacrificing the formal guarantees of the DP framework.
Gradient Inversion Attack
A class of privacy attacks that reconstruct private training data from shared model gradients by inverting the mathematical relationship between parameters and loss gradients. Techniques like Deep Leakage from Gradients (DLG) iteratively optimize dummy inputs to match observed gradients. Per-sample clipping in DP-SGD—and by extension ghost clipping—is a primary defense, as it bounds the signal-to-noise ratio available to an adversary.
Privacy Amplification by Subsampling
The phenomenon where randomly sampling a subset of data before applying a differentially private mechanism yields a stronger privacy guarantee than processing the full dataset. In DP-SGD, Poisson sampling—where each example is included with a fixed probability—amplifies privacy. Ghost clipping is fully compatible with subsampling and benefits from the same amplification properties, as it computes identical aggregate gradients.

About the author
Prasad Kumkar
CEO & MD, Inference Systems
Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.
His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.
Partnered with leading AI, data, and software stack.
How We Work
Custom AI workflows for your Business
One-fit-all AI don't work for modern businesses. At Inferensys, we aim to understand your business & custom requirements; which we use to define most efficient agentic workflows, the data, and the tools for your business.
01
Review the use case
We understand the task, the users, and where AI can actually help.
Read more02
Pick the right approach
We define what needs search, automation, or product integration.
Read more03
Build the first useful version
We implement the part that proves the value first.
Read more04
Improve from there
We add the checks and visibility needed to keep it useful.
Read moreThe first call is a practical review of your use case and the right next step.
Talk to Us