Skip to content

Validate batch dimension consistency across per-sample gradients (#841) - #853

Open
laasyapriya2007 wants to merge 1 commit into
meta-pytorch:mainfrom
laasyapriya2007:fix-embedding-broadcast-841
Open

laasyapriya2007 wants to merge 1 commit into
meta-pytorch:mainfrom
laasyapriya2007:fix-embedding-broadcast-841

Conversation

@laasyapriya2007

@laasyapriya2007 laasyapriya2007 commented Oct 20, 2026 •

Copy link
Copy Markdown

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Docs change / refactoring / dependency upgrade

Motivation and Context / Related issue

Fixes #841.

When performing full fine-tuning on transformer architectures such as GPT-2 with differential privacy, position embeddings (transformer.wpe.weight) are often constructed with shape [1, seq_len] and broadcasted across the batch dimension.
During backpropagation, PyTorch sums the gradient across the broadcasted batch dimension, causing compute_embedding_grad_sample to assign a per-sample gradient of shape (1, ...) while other parameters have shape (batch_size, ...).
Previously, this failed deep inside clip_and_accumulate with an unhelpful PyTorch internal error:
RuntimeError: stack expects each tensor to be equal size, but got [4] at entry 0 and [1] at entry 1.

Because differential privacy requires clipping individual sample gradients (which are lost if inputs were broadcasted prior to backward), this PR:

  1. Adds check_grad_sample_batch_size_consistency to opacus.optimizers.utils to detect mismatched per-sample gradient batch dimensions across parameters.
  2. Integrates this check into DPOptimizer, AdaClipDPOptimizer, and DPPerLayerOptimizer with a descriptive, actionable ValueError that identifies the exact parameter and explains how to expand inputs (e.g. position_ids.expand(batch_size, -1)) or freeze the layer.
  3. Preserves _param_name in GradSampleHooks for clear error reporting.
  4. Updates compute_embedding_grad_sample docstrings with guidelines on transformer position embeddings.
  5. Adds unit tests in opacus/tests/batch_broadcast_embedding_test.py.

How Has This Been Tested (if it applies)

  • Ran newly added unit tests in opacus/tests/batch_broadcast_embedding_test.py covering unit checks, DPOptimizer, AdaClipDPOptimizer, DPPerLayerOptimizer, and real GPT2LMHeadModel fine-tuning.
  • Verified existing test suites opacus/tests/grad_sample_hooks_test.py and opacus/tests/grad_sample_module_test.py.
  • Formatted and linted with black and flake8 --config=.github/workflows/flake8_config.ini.

Checklist

  • The documentation is up-to-date with the changes I made.
  • I have read the CONTRIBUTING document and completed the CLA.
  • All tests passed, and additional code has been covered with new tests.

…a-pytorch#841)

- Add check_grad_sample_batch_size_consistency in opacus.optimizers.utils to detect mismatched per-sample gradient batch dimensions
- Integrate check into DPOptimizer, AdaClipDPOptimizer, and DPPerLayerOptimizer before clipping
- Annotate _param_name on trainable parameters in GradSampleHooks for descriptive error reporting
- Document batch-dimension expansion requirement for embedding layers in compute_embedding_grad_sample
- Add comprehensive unit tests in opacus/tests/batch_broadcast_embedding_test.py covering DPOptimizer, AdaClipDPOptimizer, DPPerLayerOptimizer, and GPT-2 models
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 20, 2026
@meta-codesync

meta-codesync Bot commented Oct 20, 2026

Copy link
Copy Markdown

This pull request has been imported. If you are a Meta employee, you can view this in D126395728. (Because this pull request was imported automatically, there will not be any future comments.)

This branch has not been deployed

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

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Embedding grad sampler mis-sizes per-sample gradients for batch-broadcast inputs, breaking DP full fine-tuning of GPT-2-class models

1 participant