FullyShardedDataParallel question

username: gordon
cooked: <p>Using FSDP with torch 1.11.0 on a model containing a ParameterList – FSDP(model) seems to remove the parameterlist content.  I’ve even tried “manual wrapping”, wrapping only other layers (using the toy model below) but the content of the parameterlist still changes.  I’m also getting a warning about ‘Setting attributes on ParameterList is not supported’, but I’m trying to avoid wrapping the parameterlist.  Here’s the model being manually wrapped:</p>
<p>class model(nn.Module):<br>
def <strong>init</strong>(self):<br>
super().<strong>init</strong>()<br>
self.layer1 = wrap(nn.Linear(8, 4))<br>
self.layer2 = nn.Linear(4, 16)<br>
self.params = nn.ParameterList([nn.Parameter(torch.randn(10, 10)) for i in range(10)])<br>
# self.param = nn.Parameter(torch.randn(10,10))<br>
self.layer3 = wrap(nn.Linear(16, 4))</p>
<p>Here’s printing the model before and after wrapping:<br>
print(“orig model”, model())<br>
print(“params”, model().params)<br>
wrapper_kwargs = dict(cpu_offload=CPUOffload(offload_params=True))<br>
with enable_wrap(wrapper_cls=FullyShardedDataParallel, **wrapper_kwargs):<br>
fsdp_model = wrap(model())<br>
print(fsdp_model)<br>
print(“params”, fsdp_model.params)</p>
<p>and the output:<br>
orig model model(<br>
(layer1): Linear(in_features=8, out_features=4, bias=True)<br>
(layer2): Linear(in_features=4, out_features=16, bias=True)<br>
(params): ParameterList(<br>
(0): Parameter containing: [torch.FloatTensor of size 10x10]<br>
(1): Parameter containing: [torch.FloatTensor of size 10x10]<br>
(2): Parameter containing: [torch.FloatTensor of size 10x10]<br>
(3): Parameter containing: [torch.FloatTensor of size 10x10]<br>
(4): Parameter containing: [torch.FloatTensor of size 10x10]<br>
(5): Parameter containing: [torch.FloatTensor of size 10x10]<br>
(6): Parameter containing: [torch.FloatTensor of size 10x10]<br>
(7): Parameter containing: [torch.FloatTensor of size 10x10]<br>
(8): Parameter containing: [torch.FloatTensor of size 10x10]<br>
(9): Parameter containing: [torch.FloatTensor of size 10x10]<br>
)<br>
(layer3): Linear(in_features=16, out_features=4, bias=True)<br>
)<br>
params ParameterList(<br>
(0): Parameter containing: [torch.FloatTensor of size 10x10]<br>
(1): Parameter containing: [torch.FloatTensor of size 10x10]<br>
(2): Parameter containing: [torch.FloatTensor of size 10x10]<br>
(3): Parameter containing: [torch.FloatTensor of size 10x10]<br>
(4): Parameter containing: [torch.FloatTensor of size 10x10]<br>
(5): Parameter containing: [torch.FloatTensor of size 10x10]<br>
(6): Parameter containing: [torch.FloatTensor of size 10x10]<br>
(7): Parameter containing: [torch.FloatTensor of size 10x10]<br>
(8): Parameter containing: [torch.FloatTensor of size 10x10]<br>
(9): Parameter containing: [torch.FloatTensor of size 10x10]<br>
)<br>
/opt/conda/lib/python3.8/site-packages/torch/nn/modules/container.py:487: UserWarning: Setting attributes on ParameterList is not supported.<br>
warnings.warn(“Setting attributes on ParameterList is not supported.”)<br>
FullyShardedDataParallel(<br>
(_fsdp_wrapped_module): FlattenParamsWrapper(<br>
(_fpw_module): model(<br>
(layer1): FullyShardedDataParallel(<br>
(_fsdp_wrapped_module): FlattenParamsWrapper(<br>
(_fpw_module): Linear(in_features=8, out_features=4, bias=True)<br>
)<br>
)<br>
(layer2): Linear(in_features=4, out_features=16, bias=True)<br>
(params): ParameterList()<br>
(layer3): FullyShardedDataParallel(<br>
(_fsdp_wrapped_module): FlattenParamsWrapper(<br>
(_fpw_module): Linear(in_features=16, out_features=4, bias=True)<br>
)<br>
)<br>
)<br>
)<br>
)<br>
params [Parameter containing:<br>
tensor([-0.2085,  0.1279, -0.1589,  …,  1.9485, -0.1839, -0.6993],<br>
requires_grad=True)]</p>
<p>Can I FSDP the model in some way that leaves the parameterlist entries alone?</p>

username: Yanli_Zhao
cooked: <p>Hey thanks for trying out FSDP API!</p>
<p>What you mentioned is expected, because the parameterlist is wrapped by outermost FSDP wrapper, and they are flattened and sharded, so losing the access to the original parameterlist.</p>
<p>Do you want to make FSDP wrapper ignore the params? we added a new argument in nightly called “ignored_module”, ignored_module will not be managed by FSDP, so these modules will not be flattened, sharded and their gradients will not be synced as well.  Is this what you want?</p>

username: gordon
cooked: <p>Hi <a class="mention" href="/u/yanli_zhao">@Yanli_Zhao</a> – this does indeed look like what I want.  Thank you very much!</p>