Repackage_hidden function in word_language_model example

Can you pls explain what this function do?

def repackage_hidden(h):
    """Wraps hidden states in new Variables, to detach them from their history."""
    if type(h) == Variable:
        return Variable(h.data)
    else:
        return tuple(repackage_hidden(v) for v in h)

There has been a thread where I explained it.

I see thank you. Is this a general practice for BPTT in pytorch or just memory efficiency?

A general practice. You want to cut out the history of the outputs, so the memory ever gets freed. Otherwise there’s always going to be a reference to it. In the future it will be enough to call .detach() but not yet.