RuntimeError: No grad accumulator for a saved leaf!

Issue description

When I run the code, an error was reported as follows:

RuntimeError                              Traceback (most recent call last)
<ipython-input-89-0bfb9354ae29> in <module>()
      1 train_model(model=model, optimizer=optimizer, scheduler=step_scheduler, 
      2             num_epochs=num_epochs, train_loader=train_loader, dev_loader=dev_loader,
----> 3             test_loader=test_loader, device=device)

<ipython-input-86-615427f40473> in train_model(model, optimizer, scheduler, num_epochs, train_loader, dev_loader, test_loader, device)
    161             loss2 = F.cross_entropy(pred2, a[:, 0])
    162             loss = loss1 + loss2
--> 163             loss.backward()
    164             optimizer.step()

~/anaconda3/lib/python3.6/site-packages/torch/ in backward(self, gradient, retain_graph, create_graph)
     91                 products. Defaults to ``False``.
     92         """
---> 93         torch.autograd.backward(self, gradient, retain_graph, create_graph)
     95     def register_hook(self, hook):

~/anaconda3/lib/python3.6/site-packages/torch/autograd/ in backward(tensors, grad_tensors, retain_graph, create_graph, grad_variables)
     87     Variable._execution_engine.run_backward(
     88         tensors, grad_tensors, retain_graph, create_graph,
---> 89         allow_unreachable=True)  # allow_unreachable flag

RuntimeError: No grad accumulator for a saved leaf!

Code example

I’m using torch 0.4.0 and Cw, Cc, Qw, Qc are elements in a batch with requires_grad=True. a is the label, a tensor with requires_grad=False.

my code looks like as follows:

for ids, Cw, Cc, Qw, Qc, a in train_loader:
    pred1, pred2 = model(Cw, Cc, Qw, Qc)
    loss1 = F.cross_entropy(pred1, a[:, 0])
    loss2 = F.cross_entropy(pred2, a[:, 0])
    loss = loss1 + loss2

System Info

PyTorch version: 0.4.0
Is debug build: No
CUDA used to build PyTorch: None

OS: Mac OSX 10.12.6
GCC version: Could not collect
CMake version: version 3.9.6

Python version: 3.6
Is CUDA available: No
CUDA runtime version: No CUDA
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA

Versions of relevant libraries:
[pip3] msgpack-numpy (0.4.1)
[pip3] numpy (1.14.2)
[pip3] numpydoc (0.7.0)
[pip3] pytorchviz (0.0.1)
[pip3] tensorboard-pytorch (0.7.1)
[pip3] torch (0.4.0)
[pip3] torchfile (0.1.0)
[pip3] torchtext (0.2.3)
[pip3] torchvision (0.2.1)
[conda] pytorch 0.4.0 py36_cuda0.0_cudnn0.0_1 pytorch
[conda] pytorchviz 0.0.1
[conda] tensorboard-pytorch 0.7.1
[conda] torchfile 0.1.0
[conda] torchtext 0.2.3
[conda] torchvision 0.2.1 py36_1 pytorch

Problem solved.
Cw, Cc, Qw, Qc should has requires_grad=False