Comments (8)
Thanks @eracah! The requires_grad=False
is indeed the easiest fix sor far for my specific problem.
I tested it and it works as expected.
Thanks for the help and all your work, Composer is a very cool library!
from composer.
Hm... yes I think it might be not properly saving the subset of parameters if the group is not all params.
Code to save:
composer/composer/distributed/dist_strategy.py
Lines 265 to 295 in 55f0b7d
@sashaDoubov what do you think? iirc you added this bit
@DavidBert do you have a repro we can use here?
from composer.
Thanks for the quick answer!
This code should demonstrate the undesired behavior:
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchvision import datasets, transforms
from torch.utils.data import DataLoader
# optimizer
from torch.optim import Adam
from composer import Trainer
from composer.models import ComposerClassifier
from composer.utils import dist
import copy
class Model(nn.Module):
"""Toy convolutional neural network architecture in pytorch for MNIST."""
def __init__(self, num_classes: int = 10):
super().__init__()
self.num_classes = num_classes
self.conv1 = nn.Conv2d(1, 16, (3, 3), padding=0)
self.conv2 = nn.Conv2d(16, 32, (3, 3), padding=0)
self.bn = nn.BatchNorm2d(32)
self.fc1 = nn.Linear(32 * 16, 32)
self.fc2 = nn.Linear(32, num_classes)
def forward(self, x):
out = self.conv1(x)
out = F.relu(out)
out = self.conv2(out)
out = self.bn(out)
out = F.relu(out)
out = F.adaptive_avg_pool2d(out, (4, 4))
out = torch.flatten(out, 1, -1)
out = self.fc1(out)
out = F.relu(out)
return self.fc2(out)
transform = transforms.Compose([transforms.ToTensor()])
dataset = datasets.MNIST("data", train=True, download=True, transform=transform)
sampler = dist.get_sampler(dataset, shuffle=True)
dataloader = DataLoader(dataset, batch_size=32, sampler=sampler)
optimizer = Adam(Model().conv1.parameters(), lr=1e-3)
unwrapped_optimizer = copy.deepcopy(optimizer)
trainer = Trainer(
model=ComposerClassifier(module=Model(), num_classes=10),
train_dataloader=dataloader,
max_duration="2ep",
optimizers=optimizer,
fsdp_config={"sharding_strategy":"_HYBRID_SHARD_ZERO2",
"mixed_precision":"PURE",
"backward_prefetch":"BACKWARD_PRE"},
)
with trainer.state.model.module.summon_full_params(trainer.state.model.module):
nb_parameters_before_fsdp = len(unwrapped_optimizer.param_groups[0]["params"])
nb_parameters_after_fsdp = len(trainer.state.optimizers[0].param_groups[0]["params"])
assert nb_parameters_before_fsdp == nb_parameters_after_fsdp, f"expected {nb_parameters_before_fsdp} but got {nb_parameters_after_fsdp}"
from composer.
CC: @eracah can you take a look?
from composer.
Good find, @DavidBert ! It looks like here we re-init the optimizer with all parameters even if you created it with only a subset of params (for optimizers with 1 param_group). We'll file a bug to create using just the parameters from the original optimizer. For now to unblock yourself you could make a param_group with the Model().conv1.parameters() and another param_group with the rest of the params using the Optimizer.add_param_group
function. This will create >1 param group, which will then guarantee the optimizer is recreated with the correct setup. If all you want to do is freeze the other parameters you can use the optimizer as normal, but set requires_grad=False
for all parameters. That might be simpler than creating an optimizer with a subset of the parameters
Lmk if that helps unblock you!
cc: @sashaDoubov @mvpatel2000
from composer.
No problem! Glad we could unblock you, @DavidBert !
from composer.
Should now be fixed in general
from composer.
Thank you guys!
from composer.
Related Issues (20)
- Error when using `Trainer.compile_config={}` in DDP mode HOT 4
- Enable control over MLFlowLogger run_name str to match a pre-existing tag run_name in MLFlow and resume model training HOT 1
- TORCH_NCCL_ASYNC_ERROR_HANDLING vs NCCL_ASYNC_ERROR_HANDLING at launcher.py HOT 3
- NUMA affinity control HOT 2
- Optional `CheckpointSaver` instantiation inside the `Trainer` HOT 9
- TypeError: Subscripted generics cannot be used with class and instance checks HOT 2
- Autoresume and duration mismatch on reload HOT 12
- CUDA OOM error not caught with auto microbatching HOT 3
- Computing train metrics at a given frequency HOT 1
- Support DDP with rank-dependent dataloader lengths HOT 2
- Training stops after first pass of Evaluation when pretraining MosaicBert HOT 8
- EVAL_STANDALONE_END documentation missing HOT 1
- Epoch length incorrectly calculated when using DDP HOT 5
- Resuming Training from Load Path HOT 1
- ONNX export with `dynamic_axes` does not work when applying `BlurPool` HOT 1
- Save interval not working HOT 9
- How to use composer under python3.8 HOT 1
- Multi-TPU Training Support for Composer HOT 4
- Eval Loss 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 composer.