Repository navigation
Validate batch dimension consistency across per-sample gradients (#841) - #853
Open
laasyapriya2007 wants to merge 1 commit into
Open
laasyapriya2007 wants to merge 1 commit into
laasyapriya2007 wants to merge 1 commit into
Conversation
…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
|
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Types of changes
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_sampleto assign a per-sample gradient of shape(1, ...)while other parameters have shape(batch_size, ...).Previously, this failed deep inside
clip_and_accumulatewith 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:
check_grad_sample_batch_size_consistencytoopacus.optimizers.utilsto detect mismatched per-sample gradient batch dimensions across parameters.DPOptimizer,AdaClipDPOptimizer, andDPPerLayerOptimizerwith a descriptive, actionableValueErrorthat identifies the exact parameter and explains how to expand inputs (e.g.position_ids.expand(batch_size, -1)) or freeze the layer._param_nameinGradSampleHooksfor clear error reporting.compute_embedding_grad_sampledocstrings with guidelines on transformer position embeddings.opacus/tests/batch_broadcast_embedding_test.py.How Has This Been Tested (if it applies)
opacus/tests/batch_broadcast_embedding_test.pycovering unit checks,DPOptimizer,AdaClipDPOptimizer,DPPerLayerOptimizer, and realGPT2LMHeadModelfine-tuning.opacus/tests/grad_sample_hooks_test.pyandopacus/tests/grad_sample_module_test.py.blackandflake8 --config=.github/workflows/flake8_config.ini.Checklist