Device-side assert triggered ( FocalLoss )

username: bfeeny
cooked: <p>I am trying to use the FocalLoss function I found below.  But I keep getting a “device-side assert triggered”.  It seems to be on the line where it moves self.alpha to cuda if it’s not there.  I can’t understand why doing that would throw an error.  If anyone can see what is wrong it would be much appreciated.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/7/a/7ad0bf81e218930bafa910a9417f5b5c60d53024.png" data-download-href="https://discuss.pytorch.org/uploads/default/7ad0bf81e218930bafa910a9417f5b5c60d53024" title="Screen Shot 2020-07-10 at 3.47.55 AM"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/7/a/7ad0bf81e218930bafa910a9417f5b5c60d53024_2_690x248.png" alt="Screen Shot 2020-07-10 at 3.47.55 AM" data-base62-sha1="hwtuvzqp7sXVNAp7x30xs7FQ5z6" width="690" height="248" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/7/a/7ad0bf81e218930bafa910a9417f5b5c60d53024_2_690x248.png, https://discuss.pytorch.org/uploads/default/optimized/3X/7/a/7ad0bf81e218930bafa910a9417f5b5c60d53024_2_1035x372.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/7/a/7ad0bf81e218930bafa910a9417f5b5c60d53024_2_1380x496.png 2x" data-dominant-color="4E2924"><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">Screen Shot 2020-07-10 at 3.47.55 AM</span><span class="informations">2338×842 149 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></p>
<pre><code class="lang-auto">class FocalLoss(nn.Module):
    def __init__(self, class_num, alpha=None, gamma=2, size_average=True):
        super(FocalLoss, self).__init__()
        if alpha is None:
            self.alpha = torch.ones(class_num, 1)
        else:
            if isinstance(alpha, torch.Tensor):
                self.alpha = alpha
            else:
                self.alpha = torch.tensor(alpha)
        self.gamma = gamma
        self.class_num = class_num
        self.size_average = size_average
    
    def forward(self, inputs, targets):
        N = inputs.size(0)
        C = inputs.size(1)
        P = F.softmax(inputs)
        class_mask = inputs.data.new(N, C).fill_(0)
        class_mask = class_mask
        ids = targets.view(-1, 1)
        class_mask.scatter_(1, ids.data, 1.)
        #print(class_mask)
        if inputs.is_cuda and not self.alpha.is_cuda:
            self.alpha = self.alpha.to(device)
        alpha = self.alpha[ids.data.view(-1)]
        probs = (P*class_mask).sum(1).view(-1,1)
        log_p = probs.log()
        #print('probs size= {}'.format(probs.size()))
        #print(probs)
        batch_loss = -alpha*(torch.pow((1-probs), self.gamma))*log_p 
        #print('-----bacth_loss------')
        #print(batch_loss)
        if self.size_average:
            loss = batch_loss.mean()
        else:
            loss = batch_loss.sum()
        return loss
</code></pre>

username: ptrblck
cooked: <p>Is your code working fine on the CPU?<br>
This would give you a better error message than the current assert statement.</p>
<p>If it’s working on the CPU, rerun the script with:</p>
<pre><code class="lang-python">CUDA_LAUNCH_BLOCKING=1 python scripy.py args
</code></pre>
<p>and post the stack trace here, please.</p>