Problems with Legacy autograd function

Hello, I am new to PyTorch and I have been running into several problems when trying to run the file from XNOR-Net-PyTorch/ at master · jiecaoyu/XNOR-Net-PyTorch · GitHub. I have the newest stable version of PyTorch with Cuda 11.7.

I get the following error

Traceback (most recent call last):

File “C:\Users\saft_\miniconda3\envs\XNOR\lib\site-packages\spyder_kernels\”, line 356, in compat_exec
exec(code, globals, locals)

File “c:\users\saft_\machinelearning\xnor-net-pytorch\mnist\”, line 189, in

File “c:\users\saft_\machinelearning\xnor-net-pytorch\mnist\”, line 39, in train
output = model(data)

File “C:\Users\saft_\miniconda3\envs\XNOR\lib\site-packages\torch\nn\modules\”, line 1190, in _call_impl
return forward_call(*input, **kwargs)

File “C:\Users\saft_\MachineLearning\XNOR-Net-PyTorch\MNIST\models\”, line 93, in forward
x = self.bin_conv2(x)

File “C:\Users\saft_\miniconda3\envs\XNOR\lib\site-packages\torch\nn\modules\”, line 1190, in _call_impl
return forward_call(*input, **kwargs)

File “C:\Users\saft_\MachineLearning\XNOR-Net-PyTorch\MNIST\models\”, line 53, in forward
x = BinActive()(x)

File “C:\Users\saft_\miniconda3\envs\XNOR\lib\site-packages\torch\autograd\”, line 330, in call
raise RuntimeError(

RuntimeError: Legacy autograd function with non-static forward method is deprecated. Please use new-style autograd function with static forward method. (Example: Automatic differentiation package - torch.autograd — PyTorch 1.13 documentation)

The entire code reads

from __future__ import print_function
import argparse
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import os
import sys
import models
import util
from torchvision import datasets, transforms
from torch.autograd import Variable

import util

def save_state(model, acc):
    print('==> Saving model ...')
    state = {
            'acc': acc,
            'state_dict': model.state_dict(),
    for key in state['state_dict'].keys():
        if 'module' in key:
            state['state_dict'][key.replace('module.', '')] = \
                    state['state_dict'].pop(key), 'models/'+args.arch+'.best.pth.tar')

def train(epoch):
    for batch_idx, (data, target) in enumerate(train_loader):
        if args.cuda:
            data, target = data.cuda(), target.cuda()
        data, target = Variable(data), Variable(target)

        # process the weights including binarization

        output = model(data)
        loss = criterion(output, target)

        # restore weights

        if batch_idx % args.log_interval == 0:
            print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
                epoch, batch_idx * len(data), len(train_loader.dataset),
                100. * batch_idx / len(train_loader),

def test(evaluate=False):
    global best_acc
    test_loss = 0
    correct = 0

    for data, target in test_loader:
        if args.cuda:
            data, target = data.cuda(), target.cuda()
        data, target = Variable(data, volatile=True), Variable(target)
        output = model(data)
        test_loss += criterion(output, target).data.item()
        pred =, keepdim=True)[1]
        correct += pred.eq(

    acc = 100. * float(correct) / len(test_loader.dataset)
    if (acc > best_acc):
        best_acc = acc
        if not evaluate:
            save_state(model, best_acc)

    test_loss /= len(test_loader.dataset)
    print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.2f}%)'.format(
        test_loss * args.batch_size, correct, len(test_loader.dataset),
        100. * float(correct) / len(test_loader.dataset)))
    print('Best Accuracy: {:.2f}%\n'.format(best_acc))

def adjust_learning_rate(optimizer, epoch):
    """Sets the learning rate to the initial LR decayed by 10 every 15 epochs"""
    lr = * (0.1 ** (epoch // args.lr_epochs))
    print('Learning rate:', lr)
    for param_group in optimizer.param_groups:
        param_group['lr'] = lr
    return lr

if __name__=='__main__':
    # Training settings
    parser = argparse.ArgumentParser(description='PyTorch MNIST Example')
    parser.add_argument('--batch-size', type=int, default=128, metavar='N',
            help='input batch size for training (default: 128)')
    parser.add_argument('--test-batch-size', type=int, default=128, metavar='N',
            help='input batch size for testing (default: 128)')
    parser.add_argument('--epochs', type=int, default=60, metavar='N',
            help='number of epochs to train (default: 60)')
    parser.add_argument('--lr-epochs', type=int, default=15, metavar='N',
            help='number of epochs to decay the lr (default: 15)')
    parser.add_argument('--lr', type=float, default=0.01, metavar='LR',
            help='learning rate (default: 0.01)')
    parser.add_argument('--momentum', type=float, default=0.9, metavar='M',
            help='SGD momentum (default: 0.9)')
    parser.add_argument('--weight-decay', '--wd', default=1e-5, type=float,
            metavar='W', help='weight decay (default: 1e-5)')
    parser.add_argument('--no-cuda', action='store_true', default=False,
            help='disables CUDA training')
    parser.add_argument('--seed', type=int, default=1, metavar='S',
            help='random seed (default: 1)')
    parser.add_argument('--log-interval', type=int, default=100, metavar='N',
            help='how many batches to wait before logging training status')
    parser.add_argument('--arch', action='store', default='LeNet_5',
            help='the MNIST network structure: LeNet_5')
    parser.add_argument('--pretrained', action='store', default=None,
            help='pretrained model')
    parser.add_argument('--evaluate', action='store_true', default=False,
            help='whether to run evaluation')
    args = parser.parse_args()
    args.cuda = not args.no_cuda and torch.cuda.is_available()

    if args.cuda:
    # load data
    kwargs = {'num_workers': 1, 'pin_memory': True} if args.cuda else {}
    train_loader =
            datasets.MNIST('data', train=True, download=True,
                    transforms.Normalize((0.1307,), (0.3081,))
                batch_size=args.batch_size, shuffle=True, **kwargs)
    test_loader =
            datasets.MNIST('data', train=False, transform=transforms.Compose([
                transforms.Normalize((0.1307,), (0.3081,))
            batch_size=args.test_batch_size, shuffle=True, **kwargs)
    # generate the model
    if args.arch == 'LeNet_5':
        model = models.LeNet_5()
        print('ERROR: specified arch is not suppported')

    if not args.pretrained:
        best_acc = 0.0
        pretrained_model = torch.load(args.pretrained)
        best_acc = pretrained_model['acc']

    if args.cuda:
    param_dict = dict(model.named_parameters())
    params = []
    base_lr = 0.1
    for key, value in param_dict.items():
        params += [{'params':[value], 'lr':,
            'weight_decay': args.weight_decay,
    optimizer = optim.Adam(params,,

    criterion = nn.CrossEntropyLoss()

    # define the binarization operator
    bin_op = util.BinOp(model)

    if args.evaluate:

    for epoch in range(1, args.epochs + 1):
        adjust_learning_rate(optimizer, epoch)

I am aware that Variable is deprecated, but I really don’t know how to solve this issue.

The legacy autograd Function usage needs to be updated. Here’s the new version:

class BinActive(torch.autograd.Function):
    Binarize the input activations and calculate the mean across channel dimension.
    def forward(ctx, input):
        size = input.size()
        input = input.sign()
        return input

    def backward(ctx, grad_output):
        input, = ctx.saved_tensors
        grad_input = grad_output.clone()
        grad_input[] = 0
        grad_input[input.le(-1)] = 0
        return grad_input

This line needs to be updated:

It should be changed to:

x = BinActive.apply(x)