@@ -12,17 +12,16 @@ We currently provide three independent approaches for computing per sample gradi
1212Each implementation comes with its own set of limitations and benefits.
1313
1414** TL;DR:**
15- - Use ` GradSampleModule ` (` grad_sample_mode="hooks" ` ) for stable implementation with standard models
16- - Use ` GradSampleController ` via ` PrivacyEngineGradSampleController ` for transformer models and when you need direct model access without wrapping
15+ - Use ` GradSampleModule ` (` grad_sample_mode="hooks" ` ) for stable implementation with standard models (default)
16+ - Use controller mode ( ` return_controller=True ` ) for transformer models and when you need direct model access without wrapping
1717- Use ` GradSampleModuleExpandedWeights ` (` grad_sample_mode="ew" ` ) if you want to experiment with better performance
1818- Use ` grad_sample_mode="functorch" ` if your model has unsupported layers
1919
2020Please report any strange errors or unexpected behaviour to us!
2121
22- ## GradSampleController approach (No Model Wrapping)
22+ ## Controller-Based Approach (No Model Wrapping)
23+ - Usage: Set ` return_controller=True ` in ` PrivacyEngine.make_private() `
2324- Controller class: `` opacus.grad_sample.GradSampleController ``
24- - Privacy Engine: `` opacus.privacy_engine_gsc.PrivacyEngineGradSampleController ``
25- - Usage: Use ` PrivacyEngineGradSampleController ` instead of ` PrivacyEngine `
2625
2726** Recommended for transformer models and when model wrapping causes issues.**
2827
@@ -35,7 +34,21 @@ Computes per-sample gradients by attaching hooks directly to model parameters wi
3534- ✅ Better compatibility with HuggingFace transformers and models with custom ` __getattr__ `
3635- ✅ Same grad sampler methods as ` GradSampleModule `
3736
38- See [ CONTROLLER_BASED_PRIVACY_ENGINE.md] ( ../../docs/CONTROLLER_BASED_PRIVACY_ENGINE.md ) for detailed documentation.
37+ ** Example:**
38+ ``` python
39+ from opacus import PrivacyEngine
40+
41+ privacy_engine = PrivacyEngine()
42+ model, optimizer, dataloader = privacy_engine.make_private(
43+ module = model,
44+ optimizer = optimizer,
45+ data_loader = dataloader,
46+ noise_multiplier = 1.0 ,
47+ max_grad_norm = 1.0 ,
48+ return_controller = True , # ← Enable controller mode
49+ )
50+ # model is now unwrapped with hooks attached directly
51+ ```
3952
4053## Hooks-based approach (Model Wrapping)
4154- Model wrapping class: `` opacus.grad_sample.grad_sample_module.GradSampleModule ``
@@ -81,7 +94,7 @@ Please note that these are known limitations and we plan to improve Expanded Wei
8194| xxx | GradSampleModule (Hooks) | GradSampleController | Expanded Weights | Functorch |
8295| :----------------------------:| :------------------------:| :-------------------:| :----------------:| :------------:|
8396| Required PyTorch version | 1.8+ | 1.8+ | 1.13+ | 1.12 (to be updated) |
84- | Development status | Deprecated mechanism | ✅ Stable | Beta | Beta |
97+ | Development status | Deprecated mechanism | ✅ Beta | Beta | Beta |
8598| Model wrapping | ✅ Wraps model | ✅ No wrapping | ✅ Wraps model | ✅ Wraps model |
8699| Runtime Performance† | baseline | baseline | ✅ ~ 25% faster | 🟨 0-50% slower |
87100| Transformer compatibility | 🟨 May have issues | ✅ Excellent | 🟨 May have issues | 🟨 May have issues |
0 commit comments