My program stops at loss.backward() without any prompt in cmd

FOXaaFOX: <p>My aim is to make a five-category text classification</p>
<p>I am running transformers fine tuning bert  with <code>cnnbase</code> model but my program stops at <code>loss.backward()</code> without any prompt in <code>cmd</code>.</p>
<p>I debug find that the program stop at the loss.backward line without any error prompt</p>
<p>My program executed successfully in <code>rnn base</code> such as <code>lstm</code> and <code>rcnn</code>.</p>
<p>But when I am running some <code>cnnbase</code> model the strange bug  appears.</p>
<p>My cnn model code:</p>
<pre><code class="lang-auto">import torch
import torch.nn as nn
import torch.nn.functional as F
from transformers.modeling_bert import BertPreTrainedModel, BertModel
n_filters = 200
filter_sizes = [2,3,4]
class BertCNN(BertPreTrainedModel):
    def __init__(self, config):
        super(BertPreTrainedModel, self).__init__(config)
        self.num_filters = n_filters
        self.filter_sizes = filter_sizes
        self.bert = BertModel(config)
        for param in self.bert.parameters():
            param.requires_grad = True
        self.convs = nn.ModuleList(
            [nn.Conv2d(1, self.num_filters, (k, config.hidden_size))
                for k in self.filter_sizes])
        self.dropout = nn.Dropout(config.hidden_dropout_prob)
        self.fc_cnn = nn.Linear(self.num_filters *
                                len(self.filter_sizes), config.num_labels)

    def conv_and_pool(self, x, conv):
        x = F.relu(conv(x)).squeeze(3)
        x = F.max_pool1d(x, x.size(2)).squeeze(2)
        return x

    def forward(self, input_ids,
                attention_mask=None, token_type_ids=None, head_mask=None):
        outputs = self.bert(input_ids,
                            attention_mask=attention_mask,
                            token_type_ids=token_type_ids,
                            head_mask=head_mask)
        encoder_out, text_cls = outputs
        out = encoder_out.unsqueeze(1)
        out = torch.cat([self.conv_and_pool(out, conv)
                         for conv in self.convs], 1)
        out = self.dropout(out)
        out = self.fc_cnn(out)
        return out
</code></pre>
<p>My train code:</p>
<pre><code class="lang-auto">        for step, batch in enumerate(data):
            self.model.train()
            batch = tuple(t.to(self.device) for t in batch)
            input_ids, input_mask, segment_ids, label_ids = batch
            print("input_ids, input_mask, segment_ids, label_ids SIZE: \n")   
            print(input_ids.size(), input_mask.size(),segment_ids.size(), label_ids.size()) 
            # torch.Size([2, 80]) torch.Size([2, 80]) torch.Size([2, 80]) torch.Size([2])
            logits = self.model(input_ids, segment_ids, input_mask)
            print("logits and label ids size: ",logits.size(), label_ids.size())
            # torch.Size([2, 5]) torch.Size([2])
            loss = self.criterion(output=logits, target=label_ids) #loss function:CrossEntropyLoss()
            if len(self.n_gpu) &gt;= 2:
                loss = loss.mean()
            if self.gradient_accumulation_steps &gt; 1:
                loss = loss / self.gradient_accumulation_steps
            if self.fp16:
                with amp.scale_loss(loss, self.optimizer) as scaled_loss:
                    scaled_loss.backward()
                clip_grad_norm_(amp.master_params(self.optimizer), self.grad_clip)
            else:
                loss.backward() # I debug find that the program stop at this line without any error prompt
</code></pre>
<p>HELP~！~   、<br>
I  posted my questions on various community platforms，stackoverflow、other github repositories.<br>
No one replied to me.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/d/5/d57093d8a6a26ed4b284901a26fb726efd9dde35.png" data-download-href="https://discuss.pytorch.org/uploads/default/d57093d8a6a26ed4b284901a26fb726efd9dde35" title="bug"><img src="https://discuss.pytorch.org/uploads/default/original/3X/d/5/d57093d8a6a26ed4b284901a26fb726efd9dde35.png" alt="bug" data-base62-sha1="usaTDL0cZaJrW8niJopnYOiiLf7" width="690" height="404" data-dominant-color="161616"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">bug</span><span class="informations">937×549 7.73 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
I changed the batch size to 1 the bug still occured. sad…</p>

FOXaaFOX: <p>the step1 logits ：<br>
logits tensor([[ 0.8831, -0.0368, -0.2206, -2.3484, -1.3595]], device=‘cuda:1’,<br>
grad_fn=)<br>
the step1 loss：<br>
tensor(1.5489, device=‘cuda:1’, grad_fn=NllLossBackward&gt;)<br>
but why can’t loss.backward()?</p>

ptrblck: <p>Could you try to create a (small) executable code snippet, so that we could try to reproduce this issue?</p>

FOXaaFOX: <p>I I tried to run my program on linux platform, and it ran successfully.</p>
<p>Therefore, it is very likely that it is caused by different os</p>
<p>Previous os：win 10</p>