I have the following tiny code snippet, which allocates a new chunk of CUDA memory every time I call model.forward():
auto model = torch::jit::load("model.tm");
auto out   = torch::empty({1, 512, 512});
for (int i = 0; i < 20; i++) {
	auto in  = torch::empty({1, 3, 512, 512}, torch::kCUDA);
	auto res = model->forward({in}).toTensor(); // every time this is called, another 2GB CUDA memory
	out.copy_(res[0]); // without this line, no leaks
}

Obviously, this runs out of CUDA memory very quickly.
Curiously, if I don’t consume the result of model.forward(), then no leaks.
Am I doing something wrong?
I’m on Ubuntu 18.04, CUDA 10.0, and PyTorch compiled from source, v1.0.1 branch.