How does BatchNorm keeps track of running_mean?

WFS: <p>Hi all,</p>
<p>I try to implement a custom version of batch norm from scratch (to use a complex-valued network, I already have the other components, just need the batch norm).<br>
For now, I want to write it in pure python using Module + Function:<br>
<a href="https://pytorch.org/docs/stable/notes/extending.html" class="onebox" target="_blank" rel="nofollow noopener">https://pytorch.org/docs/stable/notes/extending.html</a></p>
<p>To help me, I read the way batch norm is coded in:<br>
<a href="https://pytorch.org/docs/stable/_modules/torch/nn/modules/batchnorm.html" class="onebox" target="_blank" rel="nofollow noopener">https://pytorch.org/docs/stable/_modules/torch/nn/modules/batchnorm.html</a><br>
and<br>
<a href="https://pytorch.org/docs/stable/_modules/torch/nn/functional.html" class="onebox" target="_blank" rel="nofollow noopener">https://pytorch.org/docs/stable/_modules/torch/nn/functional.html</a></p>
<p>The module is defined in torch.nn.modules.batchnorm, where running_mean and running_var are created as buffers and then passed to the forward function that called nn.functional.batch_norm that takes running_mean as argument, itself calling the cpp batchnorm function.</p>
<p>But at no point running_mean is updated in the python code I read, how does the Module keeps track of it if it is only evaluated in the cpp code? What would be the best way to do it in pure python?</p>
<p>Thanks for your help,</p>
<p>SMP</p>

ptrblck: <p>The stats are updated in the <a href="https://github.com/pytorch/pytorch/blob/96fe2b4ecbbd02143d95f467655a2d697282ac32/aten/src/ATen/native/Normalization.cpp#L137" rel="nofollow noopener">ATen implementation</a>.</p>
<p>If you would like to create a custom Python implementation, you could initialize the layer similar to the current BatchNorm implementation (i.e. register the affine parameters as <code>nn.Parameter</code> and the running estimates as buffers) and update the estimates manually.</p>
<p>Let me know, if you get stuck somewhere.</p>

WFS: <p>Hi ptrblck,</p>
<p>Thank you very much for your reply. I have never used the cpp API and I am not sure how the python Module gets updated with the new value of current_mean but for writing a custom Python implementation, I guess the only thing I need to know (for now) is that I have to do it manually. Thanks for the tip.</p>
<p>Now for the practical part; if I want to keep it as close to the original structure as possible, and also as done in the example of the “extending Pytorch tutorial”, I have to create an autograd Function what will do the calculation and an nn Module that will call this function in forward(). However, since the current_mean would be calculated inside the Function (as done in the original batchnorm structure), how to allow the Module to keep track of it? Should I simply calculate the current_mean inside the Module?</p>
<p>I am sorry if I am not very clear, it is my first customizing experience in pyTorch, I am a physicist attempting a conversion…</p>
<p>Thanks again for your help !</p>
<p>SMP</p>

ptrblck: <p>You could calculate the current mean and var inside the <code>forward</code> method of your custom batch norm layer.<br>
Note that the backward pass can automatically be calculated if your <code>forward</code> method just uses PyTorch functions, so that you don’t necessarily need to write a custom <code>autograd.Function</code>.</p>
<p>I’ve created a Python implementation of the <code>nn.BatchNorm2d</code> layer <a href="https://github.com/ptrblck/pytorch_misc/blob/master/batch_norm_manual.py" rel="nofollow noopener">here</a>, which might be a good starter for your experiments.<br>
I haven’t timed this approach yet, but I assume it to be a lot slower than the PyTorch implementation.</p>

WFS: <p>You are amazing, your example is exactly what I needed!</p>
<p>So using PyTorch functions is enough for the Method to calculate the backward, then I am not sure to understand the Linear/LinearFunction <a href="https://pytorch.org/docs/stable/notes/extending.html" rel="nofollow noopener">example</a> of the docs, as it seems to use only torch functions too, right?</p>
<p>I agree that it may be significantly slower, but I try to go step by step. It seems that complexed-value tensors are not so relevant for most learning applications, that is at least my conclusion seeing that their implementation is not a priority at all. However, for physics applications, it is of the utmost importance as transformations are usually complex, even is the phase is often a hidden variable of the problem. So I try to get my hands dirty.</p>
<p>Thanks for your help,</p>
<p>SMP</p>

ptrblck: <p>You would have to write the <code>backward</code> method, if you are using another library inside the method (e.g. numpy) or if you think the automatically generated backward method might be slower than your custom one.<br>
The example just shows the general approach of doing so.</p>

WFS: <p>Ok got it, it makes sense.</p>
<p>Thanks</p>

WFS: <p>Hi again,</p>
<p>I have a few remarks about your code:</p>
<p>Line 63, I guess it should be <code>n = input.numel() / input.size(1)</code></p>
<p>Then for the calculation of the variance, <code>var = input.var([0, 2, 3], unbiased=False)</code> gives me:</p>
<p><code>TypeError: var(): argument 'dim' (position 1) must be int, not list</code></p>
<p>I have pytorch 1.1.0a0+ac206a9, maybe it is version related, however, using<br>
<code>var = input.transpose(1,0).contiguous().view(input.size()[1],-1).var(-1, unbiased=False)</code><br>
did the trick for me.</p>
<p>By running your test, the max difference between the parameters of the stock batchnorm and the custom one is about 1e-6, I’d say good enough for me. I did not quantify the time difference yet, but I ran a network with it, and it is significantly slower, but I do not mind right now.</p>
<p>I implemented the complex version following your example and this <a href="https://openreview.net/forum?id=H1T2hmZAb" rel="nofollow noopener">paper</a>, it should work, but I am a bit worry about the speed now at it is heavier than the real version. The ball is in my court now.</p>
<p>Thanks again,</p>
<p>SMP</p>

ptrblck: <p>I’m using <code>1.0.0.dev20190312</code> and am not sure why <code>.var()</code> isn’t taking multiple dimensions in your case, but good you found a workaround.</p>
<p>The difference of <code>1e-6</code> is most likely created by floating point precision, so you don’t have to worry about it.</p>
<p>Once your custom layer works fine, we could try to write a more performant extension, so that this layer isn’t a bottleneck in your model.</p>

WFS: <p>Hi again,</p>
<p>So I came up with some code to replace critical parts of NNs with complex functions so that it works on two N,C,H,W tensors for 2D or two N,C tensors for 1D in the form</p>
<p><code>xr,xi = complexLayer(xr,xi)</code></p>
<p>The code is <a href="https://github.com/wavefrontshaping/pytorch-Complex-Layers" rel="nofollow noopener">here</a></p>
<p>It works nicely, and without time cost except for the complex batch norm. It uses the 2x2 covariance matrix between the real and imaginary parts, so there is calculation performed in python.</p>
<p>I have three issues with that complex batch norm code:</p>
<ol>
<li>
<p>It is slow. I really do not want to go into cpp code, especially regarding point 3 below, so is there any easy and/or automatic way to optimize the code, like numba style? I would perform some quantitative test when I have some time.</p>
</li>
<li>
<p>There seems to be some memory leak, as the memory usage of my GPU increases over time till it crashes. When I use the “naive” complex batch norm approach, where is do standard batch norm independently on the real and imaginary part, I do not have memory issue, the ram usage is constant. Is there a need to del/free something inside the forward to prevent memory issues?</p>
</li>
<li>
<p>More a remark than an issue, I did not see any significant improvement compared to the naive approach. It is true also in the <a href="https://openreview.net/forum?id=H1T2hmZAb" rel="nofollow noopener">paper</a> I took it from but I work on a physical system where quantities are complex, so I hope it will improve things. Still, I do not give up so soon, I will continue testing.</p>
</li>
</ol>

WFS: <p>Hi,</p>
<p>I am still struggling with those issues, does anybody have some advice about that matter?</p>
<p>Best,</p>
<p>SMP</p>

ptrblck: <ol>
<li>
<p>Not sure how much this will help, but you could try to optimize your code using <a href="https://pytorch.org/docs/stable/jit.html#torch.jit.trace" rel="nofollow noopener">torch.jit.trace</a> and compare the performance.</p>
</li>
<li>
<p>Does <a href="https://github.com/wavefrontshaping/pytorch-Complex-Layers/blob/0ebf62f42bf9b6127f98a2849bf0aa6ff19f678f/ComplexLayers.py#L138" rel="nofollow noopener">ComplexBatchNorm2d</a> have this memory leak or another module? I could have a look at it and try to narrow down possible issues.</p>
</li>
<li>
<p>Do you mean the complex modules do not improve the performance of your overall model compared to just the “real” input?</p>
</li>
</ol>

WFS: <p>Hi ptrblck,</p>
<p>Thank you for your kind reply.</p>
<ol>
<li>
<p>Thanks, I will look at it.</p>
</li>
<li>
<p>Yes, it is <a href="https://github.com/wavefrontshaping/pytorch-Complex-Layers/blob/0ebf62f42bf9b6127f98a2849bf0aa6ff19f678f/ComplexLayers.py#L138" rel="nofollow noopener">ComplexBatchNorm2d</a> (and ComplexBatchNorm1D) that has this memory leak. It looks like the memory is not freed correctly, but since Python usually takes care of this for me, I do not know how to deal with such issue.</p>
</li>
<li>
<p>Sorry I was not very clear. No, <strong>complex NNs give me better results</strong>! Especially since I use those networks to learn from physical systems where there are internal complex variables. What does not show (obvious) significant improvements is the ComplexBatchNorm() (the batch norm that takes into account the covariance matrix between the real and imaginary part) compared to NaiveComplexBatchNorm() (which basically perform batch norm independently on the real and imaginary part). But it is likely problem dependent, I need to perform further tests once my memory issue corrected.</p>
</li>
</ol>

ptrblck: <p>I cannot reproduce the memory leak using this simple code:</p>
<pre><code class="lang-python">device = 'cuda'
bn = ComplexBatchNorm2D(3)
bn.to(device)
x_r = torch.randn(10, 3, 24, 24, device=device)
x_i = torch.randn(10, 3, 24, 24, device=device)

print(torch.cuda.max_memory_allocated() / 1024**3)

for epoch in range(100):
    bn.zero_grad()
    output = bn(x_r, x_i)
    print('Epoch {}, max mem {}'.format(
        epoch, torch.cuda.max_memory_allocated() / 1024**3))
    output[0].mean().backward()
    output[1].mean().backward()
</code></pre>
<p>Could you take a look and give me a hint how your usage differs from mine?</p>

WFS: <p>Hi ptrblck,</p>
<p>Thanks for your reply.</p>
<p>I tested your code, and there is no memory issue when I run it. However, I still have those issues when I use ComplexBatchNorm2D in an actual neural network.</p>
<p>I tried to write an as-minimal-as-I-can example to replicate my issues. When I run the following, the allocated memory does increase. If I comment the lines corresponding to the batch norm, it does not.</p>
<p>Any idea?</p>
<pre><code class="lang-auto">import torch
import torch.nn as nn
import torch.nn.functional as F
from torchvision import datasets, transforms
from complexLayers import ComplexBatchNorm2D

batch_size = 64
trans = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (1.0,))])
train_set = datasets.MNIST('../data', train=True, transform=trans, download=True)
test_set = datasets.MNIST('../data', train=False, transform=trans, download=True)

train_loader = torch.utils.data.DataLoader(train_set, batch_size= batch_size, shuffle=True)
test_loader = torch.utils.data.DataLoader(test_set, batch_size= batch_size, shuffle=True)

class ComplexNet(nn.Module):
    
    def __init__(self):
        super(ComplexNet, self).__init__()
        self.conv1 = nn.Conv2d(1, 20, 5, 1)
        self.bn  = ComplexBatchNorm2D(20)
        self.conv2 = nn.Conv2d(20, 50, 5, 1)
        self.fc1 = nn.Linear(4*4*50, 500)
        self.fc2 = nn.Linear(500, 10)
             
    def forward(self,x):
        x = F.relu(self.conv1(x))
        x = F.max_pool2d(x, 2, 2)
        x,_ = self.bn(x,x)
        x = F.relu(self.conv2(x))
        x = F.max_pool2d(x, 2, 2)
        x = x.view(-1, 4*4*50)
        x = F.relu(self.fc1(x))
        x = self.fc2(x)
        return F.log_softmax(x, dim=1)
    
device = torch.device("cuda:3" )
model = ComplexNet().to(device)
optimizer = torch.optim.SGD(model.parameters(), lr=0.01, momentum=0.9)

def train(model, device, train_loader, optimizer, epoch):
    model.train()
    for batch_idx, (data, target) in enumerate(train_loader):
        data, target = data.to(device), target.to(device)
        optimizer.zero_grad()
        output = model(data)
        loss = F.nll_loss(output, target)
        loss.backward()
        optimizer.step()

for epoch in range(100):
    train(model, device, train_loader, optimizer, epoch)
    print('Epoch {} '.format(epoch))
    print('Max allocated memory: {:.0f}MiB'.format(torch.cuda.max_memory_allocated(device = device) / 1024**2))
</code></pre>

ptrblck: <p>Thank you very much for this great code to reproduce this issue!</p>
<p>Indeed the memory is growing in each epoch.<br>
After looking into the code, I think the reason is that you might track the computation graph in <code>self.running_mean</code> and <code>self.running_covar</code> unintentionally.<br>
This might be the case if you assign a value with a <code>grad_fn</code> to these tensors:</p>
<pre><code class="lang-python">self.running_mean = exponential_average_factor * mean\
    + (1 - exponential_average_factor) * self.running_mean  # mean is holding onto the computation graph
</code></pre>
<p>If you wrap the update codes into a <code>with torch.no_grad()</code> guard, the memory footprint stays constant:</p>
<pre><code class="lang-python">class ComplexBatchNorm2D(_ComplexBatchNorm):     
    
    def forward(self, input_r, input_i):
        assert(input_r.size() == input_i.size())
        assert(len(input_r.shape) == 4)
        #self._check_input_dim(input)

        exponential_average_factor = 0.0
        

        if self.training and self.track_running_stats:
            if self.num_batches_tracked is not None:
                self.num_batches_tracked += 1
                if self.momentum is None:  # use cumulative moving average
                    exponential_average_factor = 1.0 / float(self.num_batches_tracked)
                else:  # use exponential moving average
                    exponential_average_factor = self.momentum
        
        if self.training:
             
            # calculate mean of real and imaginary part
            mean_r = input_r.mean([0, 2, 3])
            mean_i = input_i.mean([0, 2, 3])
            
            mean = torch.stack((mean_r,mean_i),dim=1)
            
            with torch.no_grad():
                # update running mean
                self.running_mean = exponential_average_factor * mean\
                    + (1 - exponential_average_factor) * self.running_mean
            
            # works for 2d 
            input_r = input_r-mean_r[None, :, None, None]
            input_i = input_i-mean_i[None, :, None, None]          
        
            # Elements of the covariance matrix (biased for train)
            n = input_r.numel() / input_r.size(1)
            Crr = 1./n*input_r.pow(2).sum(dim=[0,2,3])+self.eps
            Cii = 1./n*input_i.pow(2).sum(dim=[0,2,3])+self.eps
            Cri = (input_r.mul(input_i)).mean(dim=[0,2,3])

            with torch.no_grad():
    
                self.running_covar[:,0] = exponential_average_factor * Crr * n / (n - 1)\
                    + (1 - exponential_average_factor) * self.running_covar[:,0]
                
                self.running_covar[:,1] = exponential_average_factor * Cii * n / (n - 1)\
                    + (1 - exponential_average_factor) * self.running_covar[:,1]
                    
                self.running_covar[:,2] = exponential_average_factor * Cri * n / (n - 1)\
                    + (1 - exponential_average_factor) * self.running_covar[:,2]
                
        else:
            mean = self.running_mean
            Crr = self.running_covar[:,0]+self.eps
            Cii = self.running_covar[:,1]+self.eps
            Cri = self.running_covar[:,2]#+self.eps

            input_r = input_r-mean[None,:,0,None,None]
            input_i = input_i-mean[None,:,1,None,None]



        

            
        # caclualte the inverse square root the covariance matrix
        det = Crr*Cii-Cri.pow(2)
        s = torch.sqrt(det)
        t = torch.sqrt(Cii+Crr + 2 * s)
        inverse_st = 1.0 / (s * t)
        Rrr = (Cii + s) * inverse_st
        Rii = (Crr + s) * inverse_st
        Rri = -Cri * inverse_st
                  
        input_r, input_i = Rrr[None,:,None,None]*input_r+Rri[None,:,None,None]*input_i, \
                           Rii[None,:,None,None]*input_i+Rri[None,:,None,None]*input_r
                           
        if self.affine:
            input_r, input_i = self.weight[None,:,0,None,None]*input_r+self.weight[None,:,2,None,None]*input_i+\
                               self.bias[None,:,0,None,None], \
                               self.weight[None,:,2,None,None]*input_r+self.weight[None,:,1,None,None]*input_i+\
                               self.bias[None,:,1,None,None]

        return input_r, input_i
</code></pre>

WFS: <p>Great! That solves my memory issue, no more leaks. Thanks a lot!</p>
<p>I will be able to perform more tests and try to optimize the code.</p>
<p>Just a question, is it an anticipated behaviour of PyTorch? It would seem logical to me that using <code>register_buffer</code> should forbid such tracking. Is there a better way to initialize <code>running_mean</code>?</p>
<p>Thanks again!</p>
<p>SMP</p>

ptrblck: <p>Although you register the tensor as a buffer initially, the assignment re-creates <code>self.running_xxx</code> thus storing the computation graph.</p>
<p>I’m not sure if some kind of “recreation guard” would make sense and I rather tend to manually disable the gradient in case there is some kind of special case, where this behavior is needed.</p>

himat: <p>Would using <code>detach</code> also give the same result?</p>
<pre><code class="lang-auto">self.running_mean = exponential_average_factor * mean.detach()\
                    + (1 - exponential_average_factor) * self.running_mean
</code></pre>

WFS: <p>Yep, it seems to do the job too, thanks!</p>
<p>I do not know if one is best, but I like the <code>with torch.no_grad()</code> as it makes obvious for the reader what the tricky part is about.</p>