我应该如何修复Python代码,使RAM不会快速耗尽?

2024-09-28 13:14:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我的python代码有问题,我的RAM开始快速耗尽。 执行以下功能时会出现问题:

# for loop to calculate TVD
def TVD_loop(test_i, test_dummy_i, nlayer, best_model):

    TVD_tensor = torch.zeros(test_i.size()[1], (nlayer+1), test_i.size()[0]).float()

    # replace every 0's in TVD_tensor to -2
    TVD_tensor = torch.where(TVD_tensor == 0.0, torch.tensor(-2.0), TVD_tensor)

    for m in range(test_i.size()[1]):

        gc.collect()

        input_ids = test_i[:,m]
        input_ids = torch.tensor(input_ids.tolist()).unsqueeze(0) 

        # NOTE: Hidden states are in torch.FloatTensor,
        #       (one for the output of each layer + the output of the embeddings)
        # jth layer
        for j in range(nlayer+1):

            del gc.garbage[:]
            gc.collect()

            for l in range(m * test_i.size()[0], (m+1) * test_i.size()[0]):

                del gc.garbage[:]
                gc.collect()

                tst_hidden_states = best_model(input_ids)[3][j][0, (test_i.size()[0] - 1), :]

                input_ids_dummy = test_dummy_i[:,l]
                input_ids_dummy = torch.tensor(input_ids_dummy.tolist()).unsqueeze(0) 

                tst_hidden_states_dummy = best_model(input_ids_dummy)[3][j][0, (test_i.size()[0] - 1), :]

                del input_ids_dummy
                del gc.garbage[:]
                gc.collect()

                # TVD_tensor[i,j,k] denotes for TVD calculated at 
                # batch i, layer j, and dummy output k
                TVD_tensor[m,j,(l % (test_i.size()[0]))] = TVD(tst_hidden_states, tst_hidden_states_dummy)

                del tst_hidden_states
                del tst_hidden_states_dummy
                del gc.garbage[:]
                gc.collect()

                print('l={}, gc_get_count={}'.format(l,gc.get_count()))

            del gc.garbage[:]
            gc.collect()
            print('j={}, gc_get_count={}'.format(j,gc.get_count()))

        del gc.garbage[:]
        del input_ids
        gc.collect()

        print('m={}, gc_get_count={}'.format(m,gc.get_count()))

    return TVD_tensor      

从上面的代码来看,当m=0, j=0, l=0时,一切正常,但一旦达到m=0, j=1, l=0,内存使用量就会开始快速累积。部分tst_hidden_states = best_model(input_ids)[3][j][0, (test_i.size()[0] - 1), :]tst_hidden_states_dummy = best_model(input_ids_dummy)[3][j][0, (test_i.size()[0] - 1), :]是消耗大部分内存的地方。gc.get_count()输出为(1,0,0)

错误消息如下:

Traceback (most recent call last):
  File "PhD_Code_Pub1_PennTreeBank_v6.py", line 615, in <module>
    TVD_tensor_penn = TVD_loop(test_penn_iter, test_dummy_penn, nlayer, best_model_ch2_penn)
  File "PhD_Code_Pub1_PennTreeBank_v6.py", line 514, in TVD_loop
    tst_hidden_states_dummy = best_model(input_ids_dummy)[3][j][0, (test_i.size()[0] - 1), :]
  File "/home/ec2-user/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 532, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/ec2-user/anaconda3/lib/python3.6/site-packages/transformers/modeling_gpt2.py", line 655, in forward
    inputs_embeds=inputs_embeds)
  File "/home/ec2-user/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 532, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/ec2-user/anaconda3/lib/python3.6/site-packages/transformers/modeling_gpt2.py", line 460, in forward
    head_mask=head_mask[i])
  File "/home/ec2-user/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 532, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/ec2-user/anaconda3/lib/python3.6/site-packages/transformers/modeling_gpt2.py", line 232, in forward
    head_mask=head_mask)
  File "/home/ec2-user/anaconda3/lib/python3.6/site-packages/torch/nn/modules/module.py", line 532, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/ec2-user/anaconda3/lib/python3.6/site-packages/transformers/modeling_gpt2.py", line 193, in forward
    attn_outputs = self._attn(query, key, value, attention_mask, head_mask)
  File "/home/ec2-user/anaconda3/lib/python3.6/site-packages/transformers/modeling_gpt2.py", line 147, in _attn
    w = w / math.sqrt(v.size(-1))
RuntimeError: [enforce fail at CPUAllocator.cpp:64] . DefaultCPUAllocator: can't allocate memory: you tried to allocate 50331648 bytes. Error code 12 (Cannot allocate memory)

我应该如何修复我的代码

谢谢,


Tags: inpytestidsinputsizelinetorch

热门问题