Mini-batch loss in Bi-LSTM + CRF

I have a question about how to compute the mini-batch loss in likelihood.
my function is:

    def neg_log_likelihood(self, sentences, tags, length):
        self.batch_size = sentences.size(0)

        logits = self.__get_lstm_features(sentences, length)
        real_path_score = torch.zeros(1)
        total_score = torch.zeros(1)
        if USE_GPU:
            real_path_score = real_path_score.cuda()
            total_score = total_score.cuda()

        for logit, tag, leng in zip(logits, tags, length):
            logit = logit[:leng]
            tag = tag[:leng]
            real_path_score += self.real_path_score(logit, tag)
            total_score += self.total_score(logit, tag)
        return total_score - real_path_score

loss = model.neg_log_likelihood(sentences, tags, length)
loss.backward()
optimizer.step()

I just wonder if the average gradient automatically calculated by auto_grad ?
Or, should I change my code to:

for sentence, tag , leng in zip(sentences, tags, length):
    loss = model.neg_log_likelihood(sentence, tag, leng)
    loss.backward()
    optimizer.step()

Or, use reduce_mean just like in tensorflow