When running the program, I always encounter the problem of insufficient video memory.
After consulting the information, I added these two lines at the beginning of the code
torch.cuda.set_per_process_memory_fraction(0.5, 0)
torch.cuda.empty_cache()
I was able to run for a while longer than before, but soon said that the video memory was insufficient, so I tried to clear the cache before each training iteration, that is
def train_iter(self):
# ...
for epoch in range(config.n_epochs): # 每次迭代开始前
torch.cuda.empty_cache() # 新增代码
#...
problem solved