mmcv icon indicating copy to clipboard operation
mmcv copied to clipboard

auto_fp16 goes through torch.no_grad(), and the gradient used outside torch.no_grad() also disappears

Open qfwysw opened this issue 3 years ago • 5 comments

这是我的函数 image 在no grad外面使用梯度也会消失 image

qfwysw avatar Jun 06 '22 07:06 qfwysw

We recommend using English or English & Chinese for issues so that we could have broader discussion.

mm-assistant[bot] avatar Jun 06 '22 07:06 mm-assistant[bot]

It looks strange. Could you provide some codes that are easy to reproduce?

teamwong111 avatar Jun 09 '22 03:06 teamwong111

I rewrote create_data, so my dataset does not apply the official generated pkl. I think you can rewrite the dataset, input 4 frames of pictures and related information at a time, the detector part is as shown in my picture.

qfwysw avatar Jun 09 '22 03:06 qfwysw

I tried a simple case

class MyModule1(nn.Module):
    def __init__(self):
        super().__init__()
        self.model = nn.Sequential(*list(resnet50().children())[:-2])

    @auto_fp16(apply_to=('img'), out_fp32=True)
    def extract_img_feat(self, img, img_metas):
        return self.model(img)

    def forward(self, img, img_metas):
        with torch.no_grad():
            feat1 = self.extract_img_feat(img, img_metas)
        feat2 = self.extract_img_feat(img, img_metas)
        print('feat1.requires_grad: ', feat1.requires_grad)
        print('feat2.requires_grad: ', feat2.requires_grad)
        return feat2

input = torch.rand(1, 3, 224, 224)
img_metas = {}
model = MyModule1()
_ = model(input, img_metas)

And I got:

feat1.requires_grad:  False
feat2.requires_grad:  True

It indicates that auto_fp16 does not affect gradient calculation.

teamwong111 avatar Jun 10 '22 02:06 teamwong111

Sorry, I didn't make it clearer, this is the case with distributed training.

qfwysw avatar Jun 10 '22 02:06 qfwysw