I don’t know what val is doing exactly, but it seems you are appending its output to some object.
If you are not running this code (inside val) in a with torch.no_grad() context, this could store the entire computation graph and thus increase the memory and yield to slowdowns.