Here is the script. net_A is encoder-decoder networks, its forward() computes a binary mask (code in question) and net_B is autoencoder trained for reconstruction.
net_A.train()
net_B.eval()
optimizer_A.zero_grad()
for batch_idx, inputs in enumerate(train_loader):
    
    if use_cuda:
        inputs = inputs.cuda()
    
    inputs_a = Variable(inputs)
    
    mask = net_A(inputs_a)                       ### takes image as input and outputs mask of same resolution
    mask.retain_grad()
    inputs_b = torch.mul(inputs_a, mask)
    outputs_b = net_B(inputs_b)                ### takes corrupted image as input and outputs reconstruction of same resolution
    
    targets_b = inputs_a.clone()
    targets_b[:,0,:,:] = torch.mul(targets_b[:,0,:,:], 1.0/(3.0*std_bgr[0]))
    targets_b[:,1,:,:] = torch.mul(targets_b[:,1,:,:], 1.0/(3.0*std_bgr[1]))
    targets_b[:,2,:,:] = torch.mul(targets_b[:,2,:,:], 1.0/(3.0*std_bgr[2]))

    loss = 1 - torch.sum((outputs_b*(1-mask) - targets_b*(1-mask))**2)/outputs_b.nelement()
    
    for f in net_A.parameters():
        f.retain_grad()

    loss.backward()
    
    print mask.grad                          ### gives gradient values
    for f in net_A.parameters():
        print f.grad                         ### gives zeros for all params

    optimizer_A.step()
    optimizer_A.zero_grad()




class net_A(nn.Module):
   def __init__(self, block, layers):
        super(net_A, self).__init__()
        #### Resnet 
        self.inplanes = 64
        self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3,  bias=False)
        self.bn1 = nn.BatchNorm2d(64)
        self.relu = nn.ReLU(inplace=True)
        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
        self.layer1 = self._make_layer(block, 64, layers[0])
        self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
        self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
        self.layer4 = self._make_layer(block, 512, layers[3], stride=2)
        #### Resnet 

        #### Deconvs
        self.deconv1 = nn.ConvTranspose2d(512*block.expansion, 512, kernel_size=4, stride=2, padding=1, bias=False)
        self.bn_d1 = nn.BatchNorm2d(512)
        self.deconv2 = nn.ConvTranspose2d(512, 256, kernel_size=4, stride=2, padding=1, bias=False)
        self.bn_d2 = nn.BatchNorm2d(256)
        self.deconv3 = nn.ConvTranspose2d(256, 128, kernel_size=4, stride=2, padding=1, bias=False)
        self.bn_d3 = nn.BatchNorm2d(128)
        self.deconv4 = nn.ConvTranspose2d(128, 64, kernel_size=4, stride=2, padding=1, bias=False)
        self.bn_d4 = nn.BatchNorm2d(64)
        self.deconv5 = nn.ConvTranspose2d(64, 32, kernel_size=4, stride=2, padding=1, bias=False)
        self.bn_d5 = nn.BatchNorm2d(32)
        #### Deconvs

        self.regressor = nn.Conv2d(32, 1, kernel_size=1, stride=1, padding=0, bias=True)



        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
                m.weight.data.normal_(0, math.sqrt(2. / n))
            elif isinstance(m, nn.BatchNorm2d):
                m.weight.data.fill_(1)
                m.bias.data.zero_()

   def _make_layer(self, block, planes, blocks, stride=1):
        downsample = None
        if stride != 1 or self.inplanes != planes * block.expansion:
            downsample = nn.Sequential(
                nn.Conv2d(self.inplanes, planes * block.expansion,
                          kernel_size=1, stride=stride, bias=False),
                nn.BatchNorm2d(planes * block.expansion),        )

       layers = []
       layers.append(block(self.inplanes, planes, stride, downsample))
       self.inplanes = planes * block.expansion
       for i in range(1, blocks):
           layers.append(block(self.inplanes, planes))

       return nn.Sequential(*layers)

   def get_feature(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.maxpool(x)

        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)

        x = self.deconv1(x)
        x = self.bn_d1(x)
        x = self.relu(x)
    
        x = self.deconv2(x)
        x = self.bn_d2(x)
        x = self.relu(x)
    
        x = self.deconv3(x)
        x = self.bn_d3(x)
        x = self.relu(x)
    
        x = self.deconv4(x)
        x = self.bn_d4(x)
        x = self.relu(x)
    
        x = self.deconv5(x)
        x = self.bn_d5(x)
        x = self.relu(x)

        return x

    
   def forward(self, x):
        features = self.get_feature(x)          ### x is (B, 3, 128, 128)

        score = self.regressor(features)        ### score here is (B, 1, 128, 128)
        size_ = score.size()
        score = score.view(-1)

        topk, indices = torch.topk(score, score.size()[0]/2)

        mask = Variable(torch.zeros(score.size()).cuda(), requires_grad=True)

        mask = mask.scatter(0, indices, 1)          ### thresholding

        mask = mask.view(size_)            ### mask here is (B, 1, 128, 128)

        return mask