[DataLoader] Move loop content into a function to ensure we don't preserve anything (#83595)
Can lead to CPU memory saving as we don't hold onto the pin memory buffer as long as we used to.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/83595
Approved by: https://github.com/ejguan, https://github.com/NivekT