0

In my code, I want to replace values in the tensor given values of some indices are zero, for example

target_mac_out[avail_actions[:, 1:] == 0] = -9999999 

But, it returns OOM

RuntimeError: CUDA out of memory. Tried to allocate 166.00 MiB (GPU 0; 10.76 GiB total capacity; 9.45 GiB already allocated; 4.75 MiB free; 9.71 GiB reserved in total by PyTorch)

I think there is no memory allocation because it just visits the tensor of target_mac_out and check the value and replace a new value for some indices.

Am I understanding right?

4
  • Are your tensors requiring grad? Commented Jan 17, 2021 at 10:20
  • @Ivan target_mac_out requires grad while avail_actions not. Commented Jan 17, 2021 at 11:46
  • can you display a code sample that shows how these tensors were created and initialized ? Commented Jan 17, 2021 at 20:35
  • @trialNerror the code is complex, avail_actions is a tensor in the GPU and target_mac_out is a tensor returned from the network. Commented Jan 18, 2021 at 4:56

2 Answers 2

1

It's hard to guess since we do not even know the sizes if the involved tensors, but your indexing avail_actions[:, 1:] == 0 creates a temporary tensor that does require memory allocation.

Sign up to request clarification or add additional context in comments.

Comments

1

The avail_actions[:, 1:] == 0 create a new tensor, and possibly the whole line itself create another tensor before delete the old one after finish the operation.

If speed is not a problem then you can just use for loop. Like

for i in range(target_mac_out.size(0)):
    for j in range(target_mac_out.size(1)-1):
        if target_mac_out[i, j+1] == 0:
            target_mac_out[i, j+1] = -9999999

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.