Comments (4)
Could you link the error message here? thx!
from opacus.
Hey, I'm having this issue too.
When I use torch.nn.utils.spectral_norm
, the error occurs as follows :
NotImplementedError: [NotImplementedError("Model contains a trainable layer that Opacus doesn't currently support(blocks.0.0.conv2d0.conv2d:Conv2d(3, 192, kernel_size=(1, 1), stride=(1, 1))). Please implement and register grad sampler for this layer. (See opacus.grad_sample.utils.register_grad_sampler)"), NotImplementedError("Model contains a trainable layer that Opacus doesn't currently support(blocks.0.0.conv2d1.conv2d:Conv2d(3, 192, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))). Please implement and register grad sampler for this layer. (See opacus.grad_sample.utils.register_grad_sampler)"), NotImplementedError("Model contains a trainable layer that Opacus doesn't currently support(blocks.0.0.conv2d2.conv2d:Conv2d(192, 192, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))). Please implement and register grad sampler for this layer. (See opacus.grad_sample.utils.register_grad_sampler)"), NotImplementedError("Model contains a trainable layer that Opacus doesn't currently support(blocks.1.0:SelfAttention(\n (conv1x1_theta): SNConv2d(\n (conv2d): Conv2d(192, 24, kernel_size=(1, 1), stride=(1, 1), bias=False)\n )\n (conv1x1_phi): SNConv2d(\n (conv2d): Conv2d(192, 24, kernel_size=(1, 1), stride=(1, 1), bias=False)\n )\n (conv1x1_g): SNConv2d(\n (conv2d): Conv2d(192, 96, kernel_size=(1, 1), stride=(1, 1), bias=False)\n )\n (conv1x1_attn): SNConv2d(\n (conv2d): Conv2d(96, 192, kernel_size=(1, 1), stride=(1, 1), bias=False)\n )\n (maxpool): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n (softmax): Softmax(dim=-1)\n)). Please implement and register grad sampler for this layer. (See opacus.grad_sample.utils.register_grad_sampler)"), NotImplementedError("Model contains a trainable layer that Opacus doesn't currently support(blocks.1.0.conv1x1_theta.conv2d:Conv2d(192, 24, kernel_size=(1, 1), stride=(1, 1), bias=False)). Please implement and register grad sampler for this layer. (See opacus.grad_sample.utils.register_grad_sampler)"), NotImplementedError("Model contains a trainable layer that Opacus doesn't currently support(blocks.1.0.conv1x1_phi.conv2d:Conv2d(192, 24, kernel_size=(1, 1), stride=(1, 1), bias=False)). Please implement and register grad sampler for this layer. (See opacus.grad_sample.utils.register_grad_sampler)"), NotImplementedError("Model contains a trainable layer that Opacus doesn't currently support(blocks.1.0.conv1x1_g.conv2d:Conv2d(192, 96, kernel_size=(1, 1), stride=(1, 1), bias=False)). Please implement and register grad sampler for this layer. (See opacus.grad_sample.utils.register_grad_sampler)"), NotImplementedError("Model contains a trainable layer that Opacus doesn't currently support(blocks.1.0.conv1x1_attn.conv2d:Conv2d(96, 192, kernel_size=(1, 1), stride=(1, 1), bias=False)). Please implement and register grad sampler for this layer. (See opacus.grad_sample.utils.register_grad_sampler)"), NotImplementedError("Model contains a trainable layer that Opacus doesn't currently support(blocks.2.0.conv2d0.conv2d:Conv2d(192, 192, kernel_size=(1, 1), stride=(1, 1))). Please implement and register grad sampler for this layer. (See opacus.grad_sample.utils.register_grad_sampler)"), NotImplementedError("Model contains a trainable layer that Opacus doesn't currently support(blocks.2.0.conv2d1.conv2d:Conv2d(192, 192, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))). Please implement and register grad sampler for this layer. (See opacus.grad_sample.utils.register_grad_sampler)"), NotImplementedError("Model contains a trainable layer that Opacus doesn't currently support(blocks.2.0.conv2d2.conv2d:Conv2d(192, 192, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))). Please implement and register grad sampler for this layer. (See opacus.grad_sample.utils.register_grad_sampler)"), NotImplementedError("Model contains a trainable layer that Opacus doesn't currently support(blocks.3.0.conv2d1.conv2d:Conv2d(192, 192, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))). Please implement and register grad sampler for this layer. (See opacus.grad_sample.utils.register_grad_sampler)"), NotImplementedError("Model contains a trainable layer that Opacus doesn't currently support(blocks.3.0.conv2d2.conv2d:Conv2d(192, 192, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))). Please implement and register grad sampler for this layer. (See opacus.grad_sample.utils.register_grad_sampler)"), NotImplementedError("Model contains a trainable layer that Opacus doesn't currently support(blocks.4.0.conv2d1.conv2d:Conv2d(192, 192, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))). Please implement and register grad sampler for this layer. (See opacus.grad_sample.utils.register_grad_sampler)"), NotImplementedError("Model contains a trainable layer that Opacus doesn't currently support(blocks.4.0.conv2d2.conv2d:Conv2d(192, 192, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))). Please implement and register grad sampler for this layer. (See opacus.grad_sample.utils.register_grad_sampler)"), NotImplementedError("Model contains a trainable layer that Opacus doesn't currently support(linear1.liner:Linear(in_features=192, out_features=1, bias=True)). Please implement and register grad sampler for this layer. (See opacus.grad_sample.utils.register_grad_sampler)")]
When I use torch.nn.utils.parametrizations.spectral_norm
, the error occurs as follows :
NotImplementedError: [NotImplementedError("Model contains a trainable layer that Opacus doesn't currently support(blocks.0.0.conv2d0.conv2d:ParametrizedConv2d(\n 3, 192, kernel_size=(1, 1), stride=(1, 1)\n (parametrizations): ModuleDict(\n (weight): ParametrizationList(\n (0): _SpectralNorm()\n )\n )\n)). Please implement and register grad sampler for this layer. (See opacus.grad_sample.utils.register_grad_sampler)"), NotImplementedError("Model contains a trainable layer that Opacus doesn't currently support(blocks.0.0.conv2d0.conv2d.parametrizations.weight:ParametrizationList(\n (0): _SpectralNorm()\n)). Please implement and register grad sampler for this layer. (See opacus.grad_sample.utils.register_grad_sampler)"), NotImplementedError("Model contains a trainable layer that Opacus doesn't currently support(blocks.0.0.conv2d1.conv2d:ParametrizedConv2d(\n 3, 192, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)\n (parametrizations): ModuleDict(\n (weight): ParametrizationList(\n (0): _SpectralNorm()\n )\n )\n)). Please implement and register grad sampler for this layer. (See opacus.grad_sample.utils.register_grad_sampler)"), NotImplementedError("Model contains a trainable layer that Opacus doesn't currently support(blocks.0.0.conv2d1.conv2d.parametrizations.weight:ParametrizationList(\n (0): _SpectralNorm()\n)). Please implement and register grad sampler for this layer. (See opacus.grad_sample.utils.register_grad_sampler)"), NotImplementedError("Model contains a trainable layer that Opacus doesn't currently support(blocks.0.0.conv2d2.conv2d:ParametrizedConv2d(\n 192, 192, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)\n (parametrizations): ModuleDict(\n (weight): ParametrizationList(\n (0): _SpectralNorm()\n )\n )\n)). Please implement and register grad sampler for this layer. (See opacus.grad_sample.utils.register_grad_sampler)"), NotImplementedError("Model contains a trainable layer that Opacus doesn't currently support(blocks.0.0.conv2d2.conv2d.parametrizations.weight:ParametrizationList(\n (0): _SpectralNorm()\n)). Please implement and register grad sampler for this layer. (See opacus.grad_sample.utils.register_grad_sampler)"), NotImplementedError("Model contains a trainable layer that Opacus doesn't currently support(blocks.1.0:SelfAttention(\n (conv1x1_theta): SNConv2d(\n (conv2d): ParametrizedConv2d(\n 192, 24, kernel_size=(1, 1), stride=(1, 1), bias=False\n (parametrizations): ModuleDict(\n (weight): ParametrizationList(\n (0): _SpectralNorm()\n )\n )\n )\n )\n (conv1x1_phi): SNConv2d(\n (conv2d): ParametrizedConv2d(\n 192, 24, kernel_size=(1, 1), stride=(1, 1), bias=False\n (parametrizations): ModuleDict(\n (weight): ParametrizationList(\n (0): _SpectralNorm()\n )\n )\n )\n )\n (conv1x1_g): SNConv2d(\n (conv2d): ParametrizedConv2d(\n 192, 96, kernel_size=(1, 1), stride=(1, 1), bias=False\n (parametrizations): ModuleDict(\n (weight): ParametrizationList(\n (0): _SpectralNorm()\n )\n )\n )\n )\n (conv1x1_attn): SNConv2d(\n (conv2d): ParametrizedConv2d(\n 96, 192, kernel_size=(1, 1), stride=(1, 1), bias=False\n (parametrizations): ModuleDict(\n (weight): ParametrizationList(\n (0): _SpectralNorm()\n )\n )\n )\n )\n (maxpool): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n (softmax): Softmax(dim=-1)\n)). Please implement and register grad sampler for this layer. (See opacus.grad_sample.utils.register_grad_sampler)"), NotImplementedError("Model contains a trainable layer that Opacus doesn't currently support(blocks.1.0.conv1x1_theta.conv2d.parametrizations.weight:ParametrizationList(\n (0): _SpectralNorm()\n)). Please implement and register grad sampler for this layer. (See opacus.grad_sample.utils.register_grad_sampler)"), NotImplementedError("Model contains a trainable layer that Opacus doesn't currently support(blocks.1.0.conv1x1_phi.conv2d.parametrizations.weight:ParametrizationList(\n (0): _SpectralNorm()\n)). Please implement and register grad sampler for this layer. (See opacus.grad_sample.utils.register_grad_sampler)"), NotImplementedError("Model contains a trainable layer that Opacus doesn't currently support(blocks.1.0.conv1x1_g.conv2d.parametrizations.weight:ParametrizationList(\n (0): _SpectralNorm()\n)). Please implement and register grad sampler for this layer. (See opacus.grad_sample.utils.register_grad_sampler)"), NotImplementedError("Model contains a trainable layer that Opacus doesn't currently support(blocks.1.0.conv1x1_attn.conv2d.parametrizations.weight:ParametrizationList(\n (0): _SpectralNorm()\n)). Please implement and register grad sampler for this layer. (See opacus.grad_sample.utils.register_grad_sampler)"), NotImplementedError("Model contains a trainable layer that Opacus doesn't currently support(blocks.2.0.conv2d0.conv2d:ParametrizedConv2d(\n 192, 192, kernel_size=(1, 1), stride=(1, 1)\n (parametrizations): ModuleDict(\n (weight): ParametrizationList(\n (0): _SpectralNorm()\n )\n )\n)). Please implement and register grad sampler for this layer. (See opacus.grad_sample.utils.register_grad_sampler)"), NotImplementedError("Model contains a trainable layer that Opacus doesn't currently support(blocks.2.0.conv2d0.conv2d.parametrizations.weight:ParametrizationList(\n (0): _SpectralNorm()\n)). Please implement and register grad sampler for this layer. (See opacus.grad_sample.utils.register_grad_sampler)"), NotImplementedError("Model contains a trainable layer that Opacus doesn't currently support(blocks.2.0.conv2d1.conv2d:ParametrizedConv2d(\n 192, 192, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)\n (parametrizations): ModuleDict(\n (weight): ParametrizationList(\n (0): _SpectralNorm()\n )\n )\n)). Please implement and register grad sampler for this layer. (See opacus.grad_sample.utils.register_grad_sampler)"), NotImplementedError("Model contains a trainable layer that Opacus doesn't currently support(blocks.2.0.conv2d1.conv2d.parametrizations.weight:ParametrizationList(\n (0): _SpectralNorm()\n)). Please implement and register grad sampler for this layer. (See opacus.grad_sample.utils.register_grad_sampler)"), NotImplementedError("Model contains a trainable layer that Opacus doesn't currently support(blocks.2.0.conv2d2.conv2d:ParametrizedConv2d(\n 192, 192, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)\n (parametrizations): ModuleDict(\n (weight): ParametrizationList(\n (0): _SpectralNorm()\n )\n )\n)). Please implement and register grad sampler for this layer. (See opacus.grad_sample.utils.register_grad_sampler)"), NotImplementedError("Model contains a trainable layer that Opacus doesn't currently support(blocks.2.0.conv2d2.conv2d.parametrizations.weight:ParametrizationList(\n (0): _SpectralNorm()\n)). Please implement and register grad sampler for this layer. (See opacus.grad_sample.utils.register_grad_sampler)"), NotImplementedError("Model contains a trainable layer that Opacus doesn't currently support(blocks.3.0.conv2d1.conv2d:ParametrizedConv2d(\n 192, 192, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)\n (parametrizations): ModuleDict(\n (weight): ParametrizationList(\n (0): _SpectralNorm()\n )\n )\n)). Please implement and register grad sampler for this layer. (See opacus.grad_sample.utils.register_grad_sampler)"), NotImplementedError("Model contains a trainable layer that Opacus doesn't currently support(blocks.3.0.conv2d1.conv2d.parametrizations.weight:ParametrizationList(\n (0): _SpectralNorm()\n)). Please implement and register grad sampler for this layer. (See opacus.grad_sample.utils.register_grad_sampler)"), NotImplementedError("Model contains a trainable layer that Opacus doesn't currently support(blocks.3.0.conv2d2.conv2d:ParametrizedConv2d(\n 192, 192, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)\n (parametrizations): ModuleDict(\n (weight): ParametrizationList(\n (0): _SpectralNorm()\n )\n )\n)). Please implement and register grad sampler for this layer. (See opacus.grad_sample.utils.register_grad_sampler)"), NotImplementedError("Model contains a trainable layer that Opacus doesn't currently support(blocks.3.0.conv2d2.conv2d.parametrizations.weight:ParametrizationList(\n (0): _SpectralNorm()\n)). Please implement and register grad sampler for this layer. (See opacus.grad_sample.utils.register_grad_sampler)"), NotImplementedError("Model contains a trainable layer that Opacus doesn't currently support(blocks.4.0.conv2d1.conv2d:ParametrizedConv2d(\n 192, 192, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)\n (parametrizations): ModuleDict(\n (weight): ParametrizationList(\n (0): _SpectralNorm()\n )\n )\n)). Please implement and register grad sampler for this layer. (See opacus.grad_sample.utils.register_grad_sampler)"), NotImplementedError("Model contains a trainable layer that Opacus doesn't currently support(blocks.4.0.conv2d1.conv2d.parametrizations.weight:ParametrizationList(\n (0): _SpectralNorm()\n)). Please implement and register grad sampler for this layer. (See opacus.grad_sample.utils.register_grad_sampler)"), NotImplementedError("Model contains a trainable layer that Opacus doesn't currently support(blocks.4.0.conv2d2.conv2d:ParametrizedConv2d(\n 192, 192, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1)\n (parametrizations): ModuleDict(\n (weight): ParametrizationList(\n (0): _SpectralNorm()\n )\n )\n)). Please implement and register grad sampler for this layer. (See opacus.grad_sample.utils.register_grad_sampler)"), NotImplementedError("Model contains a trainable layer that Opacus doesn't currently support(blocks.4.0.conv2d2.conv2d.parametrizations.weight:ParametrizationList(\n (0): _SpectralNorm()\n)). Please implement and register grad sampler for this layer. (See opacus.grad_sample.utils.register_grad_sampler)"), NotImplementedError("Model contains a trainable layer that Opacus doesn't currently support(linear1.liner:ParametrizedLinear(\n in_features=192, out_features=1, bias=True\n (parametrizations): ModuleDict(\n (weight): ParametrizationList(\n (0): _SpectralNorm()\n )\n )\n)). Please implement and register grad sampler for this layer. (See opacus.grad_sample.utils.register_grad_sampler)"), NotImplementedError("Model contains a trainable layer that Opacus doesn't currently support(linear1.liner.parametrizations.weight:ParametrizationList(\n (0): _SpectralNorm()\n)). Please implement and register grad sampler for this layer. (See opacus.grad_sample.utils.register_grad_sampler)")]
I customized a SpectralNorm
based on the error message and wrapped it with opacus.grad_sample.utils.register_grad_sampler
, which produces an error :
RuntimeError: mutating directly with
.data
inside functorch transform is not allowed.
The custom SpectralNorm comes from this python file.
from opacus.
I have checked the implementation of "nn.utils.spectral_norm" and this. I think the main problem comes from their use of "_forward_pre_hooks", either explicitly or implicitly, which Opacus does not support right now. As a consequence, "weight_original" (or "w_bar" in the latter) is recognized as a trainable parameter but "weight" instead is used in the forward pass. This discrepancy confuses Opacus, leadibg to the empty per_sample_grad for "weight_original".
from opacus.
Unfortunately, I do not have a quick solution in my mind right now (cc @karthikprasad in case you have a different opinion). And we might not have bandwidth to support this function in the very near future. Right now, I would suggest you just turn spectral norm off, or replace it by other normalization technique (like layer_normalization).
from opacus.
Related Issues (20)
- UnsupportedModuleError: [IllegalModuleConfigurationError('Model needs to be in training mode')] HOT 1
- LLM finetuning with Opacus HOT 2
- Add context manager to toggle on/off privacy in training loop
- Some issue with loading model with 'weight' as opposed to 'pretrained=True' HOT 1
- Error: Trying to add hooks twice to the same model HOT 2
- OverflowError: cannot convert float infinity to integer HOT 2
- ModuleValidator.fix() causes layer gradients to be None. HOT 1
- Integrating Opacus for a custom pytorch model raises errors but works fine on its own HOT 2
- `BatchSplittingSampler` return wrong length HOT 5
- Error occurred when executing GroundingDinoSAMSegment (segment anything): Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu! (when checking argument for argument mat2 in method wrapper_CUDA_mm)
- Why GDP and RDP give different result for the same config HOT 1
- Making a custom transformer architecture work with opacus HOT 3
- ValueError: Per sample gradient is not initialized. Not updated in backward pass? HOT 4
- BatchMemoryManager for training private GANs
- Wrapper references can be easily replaced, consider using properties instead HOT 3
- Error in DPOptimizer: Inconsistency between batch_first argument of PrivacyEngine and DPMultiheadAttention HOT 2
- Grad Sample Module: Use full backward hook to save activations and backprop values. HOT 1
- Microbatching Support HOT 4
- Training a simple transformer model with Opacus produces runtime error due to mismatch in dimensions HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from opacus.