Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use multiple gpus for pruner #1915

Closed
xuezu29 opened this issue Jan 2, 2020 · 8 comments
Closed

How to use multiple gpus for pruner #1915

xuezu29 opened this issue Jan 2, 2020 · 8 comments

Comments

@xuezu29
Copy link

xuezu29 commented Jan 2, 2020

Lottery Ticket pruner running on one GPU is OK, but when i used multi gpus was defeated.

import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.utils.data
import torchvision.datasets as datasets
import torchvision.transforms as transforms
from nni.compression.torch import LotteryTicketPruner

class fc1(nn.Module):

    def __init__(self, num_classes=10):
        super(fc1, self).__init__()
        self.conv = nn.Sequential(
            nn.Conv2d(1, 32, kernel_size=3, stride=1, padding=1),
            nn.BatchNorm2d(32),
            nn.ReLU(inplace=True)
        )
        self.classifier = nn.Sequential(
            nn.Linear(32*28*28, 300),
            nn.ReLU(inplace=True),
            nn.Linear(300, 100),
            nn.ReLU(inplace=True),
            nn.Linear(100, num_classes),
        )

    def forward(self, x):
        x = self.conv(x)
        x = torch.flatten(x,1)
        #x = torch.flatten(x, 1)
        x = self.classifier(x)
        return x

def train(model, train_loader, optimizer, criterion):
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    model.train()
    for imgs, targets in train_loader:
        optimizer.zero_grad()
        imgs, targets = imgs.to(device), targets.to(device)
        output = model(imgs)
        train_loss = criterion(output, targets)
        train_loss.backward()
        optimizer.step()
    return train_loss.item()

def test(model, test_loader, criterion):
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    model.eval()
    test_loss = 0
    correct = 0
    with torch.no_grad():
        for data, target in test_loader:
            data, target = data.to(device), target.to(device)
            output = model(data)
            test_loss += F.nll_loss(output, target, reduction='sum').item()  # sum up batch loss
            pred = output.data.max(1, keepdim=True)[1]  # get the index of the max log-probability
            correct += pred.eq(target.data.view_as(pred)).sum().item()
        test_loss /= len(test_loader.dataset)
        accuracy = 100. * correct / len(test_loader.dataset)
    return accuracy


if __name__ == '__main__':
    transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,))])
    traindataset = datasets.MNIST('./data', train=True, download=True, transform=transform)
    testdataset = datasets.MNIST('./data', train=False, transform=transform)
    train_loader = torch.utils.data.DataLoader(traindataset, batch_size=60, shuffle=True, num_workers=10, drop_last=False)
    test_loader = torch.utils.data.DataLoader(testdataset, batch_size=60, shuffle=False, num_workers=10, drop_last=True)

    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    model = fc1()
    if torch.cuda.device_count()>1:
        model = nn.DataParallel(model)

    model.to(device)
    optimizer = torch.optim.Adam(model.parameters(), lr=1.2e-3)
    criterion = nn.CrossEntropyLoss()

    configure_list = [{
        'prune_iterations': 5,
        'sparsity': 0.86,
        'op_types': ['default']
    }]
    pruner = LotteryTicketPruner(model, configure_list, optimizer)
    pruner.compress()

    for i in pruner.get_prune_iterations():
        pruner.prune_iteration_start()
        loss = 0
        accuracy = 0
        for epoch in range(10):
            loss = train(model, train_loader, optimizer, criterion)
            accuracy = test(model, test_loader, criterion)
            print('current epoch: {0}, loss: {1}, accuracy: {2}'.format(epoch, loss, accuracy))
        print('prune iteration: {0}, loss: {1}, accuracy: {2}'.format(i, loss, accuracy))
    pruner.export_model('model.pth', 'mask.pth')

RuntimeError: weight__storage_saved.value().is_alias_of(weight_.storage()) ASSERT FAILED at /opt/conda/conda-bld/pytorch_1556653114079/work/torch/csrc/autograd/generated/VariableType_2.cpp:8492, please report a bug to PyTorch.

@xuezu29 xuezu29 changed the title How to use multiple gpus for Lottery Ticket pruner How to use multiple gpus for pruner Jan 2, 2020
@scarlett2018 scarlett2018 added this to Needs triage in Community via automation Jan 2, 2020
@QuanluZhang QuanluZhang assigned QuanluZhang and unassigned Cjkkkk Jan 3, 2020
@xuezu29
Copy link
Author

xuezu29 commented Jan 3, 2020

I have tested fpgm_torch_mnist.py on multi gpus also defeated.

@QuanluZhang
Copy link
Contributor

thanks @xuezu29 for reporting this issue. Currently model compression does not support DataParallel. The support will be included in release v1.4. Now, we are also checking DistributedDataParallel, will update when we get result.

@xuezu29
Copy link
Author

xuezu29 commented Jan 6, 2020

@QuanluZhang Thanks nni team !

@Cjkkkk
Copy link
Contributor

Cjkkkk commented Jan 9, 2020

@xuezu29 you can track the status of pr #1923 . Thanks for bringing up this issue!

@QuanluZhang
Copy link
Contributor

@xuezu29 data parallel has been supported in v1.4, please refer to this example

Community automation moved this from Needs triage to Closed Feb 19, 2020
@prismformore
Copy link

@QuanluZhang Sorry but the example website is 404. So can we use DistributedDataParallel or DataParallel on the NAS model now? And how should we set the gpuNum? Thank you!

@suiguoxin
Copy link
Member

suiguoxin commented Nov 16, 2020

@QuanluZhang Sorry but the example website is 404. So can we use DistributedDataParallel or DataParallel on the NAS model now? And how should we set the gpuNum? Thank you!

The file has been renamed, you can find the updated example here: https://github.com/microsoft/nni/blob/master/examples/model_compress/model_prune_torch.py

@songkq
Copy link

songkq commented Apr 1, 2022

@xuezu29 data parallel has been supported in v1.4, please refer to this example
Hi, could you please provide the latest link to the tutorial of using nni.pruner in ddp training mode?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
No open projects
Community
  
Closed
Development

No branches or pull requests

8 participants