Giter VIP home page Giter VIP logo

Comments (14)

HIT-cwh avatar HIT-cwh commented on August 18, 2024 1

Forcing optimize_config to None may not be a good idea, for example, it's not easy to adapt to fp16 training when setting optimizer_config to None.

Why not just comment out all the .backward and .step in https://github.com/open-mmlab/mmrazor/blob/master/mmrazor/models/algorithms/autoslim.py#L226

I tried and it works.

Thank you for your issue.
It does work to comment out all the .backward and .step. And we use these here just to save GPU memory. It is hacky as it's not easy to adapt to fp16 training. But just using OptimizerHook may lead to cuda out of memory when training on bigger dataset, such as Imagenet. We are still working on it.

from mmrazor.

HIT-cwh avatar HIT-cwh commented on August 18, 2024 1

Maybe I didn't make it clear.
code 1:

loss1 = forward_loss()
loss2 = forward_loss()
loss3 = forward_loss()
loss = loss1 + loss2 + loss3
loss.backward()
optimizor.step()

code2:

loss1 = forward_loss()
loss1.backward()
loss2 = forward_loss()
loss2.backward()
loss3 = forward_loss()
loss3.backward()
optimizor.step()

The memory cost of code1 is 3 times as much as that of code2.
If we use OptimizerHook, the effect is similar to code1.

from mmrazor.

jiehuang165 avatar jiehuang165 commented on August 18, 2024

I ran into this issue just now. And I solve it by setting optimizer_config = None as


The setting avoids the second call of backward() via no registering OptimizerHook.

from mmrazor.

twmht avatar twmht commented on August 18, 2024

Forcing optimize_config to None may not be a good idea, for example, it's not easy to adapt to fp16 training when setting optimizer_config to None.

Why not just comment out all the .backward and .step in https://github.com/open-mmlab/mmrazor/blob/master/mmrazor/models/algorithms/autoslim.py#L226

I tried and it works.

from mmrazor.

twmht avatar twmht commented on August 18, 2024

@HIT-cwh

I am wondering why the current implementation would result OOM, I reimplement with SlimConv2D from https://github.com/JiahuiYu/slimmable_networks/blob/master/models/slimmable_ops.py#L25.

And there is no OOM occur.

from mmrazor.

pppppM avatar pppppM commented on August 18, 2024

@HIT-cwh

from mmrazor.

pppppM avatar pppppM commented on August 18, 2024

I am wondering why the current implementation would result OOM, I reimplement with SlimConv2D from https://github.com/JiahuiYu/slimmable_networks/blob/master/models/slimmable_ops.py#L25.

And there is no OOM occur.

It is better to make a new issue to discuss this question. @twmht

from mmrazor.

HIT-cwh avatar HIT-cwh commented on August 18, 2024

@HIT-cwh

I am wondering why the current implementation would result OOM, I reimplement with SlimConv2D from https://github.com/JiahuiYu/slimmable_networks/blob/master/models/slimmable_ops.py#L25.

And there is no OOM occur.

Look here. In the official code, the gradient of the loss is computed immediately for each width_mult. That is, loss.backward() is executed immediately after forward_loss. So there is no OOM occur.
But in our implementation, if we use OptimizerHook, the pseudo code will be roughly as follows:

loss = 0.   
for i, subnet in enumerate(self.channel_cfg):   
    ...   
    model_loss, _ = self._parse_losses(model_losses)  
    loss += model_loss  
loss.backward()  

It will use much more memory.

from mmrazor.

twmht avatar twmht commented on August 18, 2024

@HIT-cwh

But you can also do in the same way with mmrazor, where is the OOM from if you do in the same way ?

from mmrazor.

twmht avatar twmht commented on August 18, 2024

Interesting. To the best of my knowledge, I though both use the same memory. Thank you for pointing out that.

from mmrazor.

HIT-cwh avatar HIT-cwh commented on August 18, 2024

Interesting. To the best of my knowledge, I though both use the same memory. Thank you for pointing out that.

I'm sorry to bother you again. To some degree, the above code2 has the same effect as grad-cumulating. So, I think the implementation like code2 may save much memory. If it turns out that I'm wrong, please let me know. Thanks.

from mmrazor.

twmht avatar twmht commented on August 18, 2024

Yup. I think you are right, since we need to trace the autograd graph when training.

from mmrazor.

twmht avatar twmht commented on August 18, 2024

@HIT-cwh

Well, I found out it also works by commenting out all the backward and step when training object detection, No OOM happens.

It's weird since the consuming memory is almost the same as when doing normal object detection training.

My pytorch version is 1.9.

What is your batch size and number of gpus when training with ImageNet?

from mmrazor.

pppppM avatar pppppM commented on August 18, 2024

Samples_per_gpu=128 and GPUS=8

from mmrazor.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.