Minimumal-ish example code:
import torch
from torch import nn


N = 5
seq_len = 3
vocab_size = 7
embedding_size = 8

embedding = nn.Embedding(vocab_size, embedding_size)
h = nn.Linear(embedding_size * seq_len, vocab_size)

encoded = torch.rand(seq_len, N, embedding_size, requires_grad=True)
out_probs = torch.zeros(seq_len, N, vocab_size)
out = torch.LongTensor(seq_len, N)
for t in range(seq_len):
    out_emb = torch.zeros(seq_len, N, embedding_size)
    if t > 0:
        # out_t = out[:t].data
        out_t = out[:t].detach()
        out_emb[:t] = embedding(out_t)
    out_emb = encoded + out_emb
    out_emb = h(out_emb.transpose(0, 1).contiguous().view(N, -1))
    out_probs[t] = out_emb
    _, decoded = out_emb.max(dim=-1)
    out[t] = decoded

loss = out_probs.sum()
loss.backward()

gives error
RuntimeError: one of the variables needed for gradient computation has been modified by an inplace operation

But if replace .detach() with .data or .clone().detach(), then no error.
Why?  Bug in .detach()? bug in my own code? bug in .data?