Inside the forward pass you are accumulating tensors and with them the computation graph, which will increase the memory usage. While this might not be an issue, if you are calling the forward method manually, note that the LBFGS optimizer will call the closure repeatedly, which might yield the OOM issue. Based on the docs 1 this optimizer is generally memory intensive and you might need to reduce the history_size to avoid the OOM.