tutorials icon indicating copy to clipboard operation
tutorials copied to clipboard

RuntimeError: CUDA error: out of memory while trying mario_rl_tutorial.py

Open marcmolla opened this issue 5 years ago • 2 comments

When I tried the mario_rl_tutorial.py using CUDA, I received an out of memory error:

RuntimeError: CUDA error: out of memory

Monitoring the GPU memory I saw a constant incremental of the use of its memory, specially when the Agent starts the training. As a workaround I changed the cache method of the Agent, as it seems that all the experiencies are sent to the GPU:

        if self.use_cuda:
            state = torch.tensor(state).cuda()
            next_state = torch.tensor(next_state).cuda()
            action = torch.tensor([action]).cuda()
            reward = torch.tensor([reward]).cuda()
            done = torch.tensor([done]).cuda()
 (...)

        self.memory.append((state, next_state, action, reward, done,))

Instead of that, I modified this piece for using CPU and changed the recall method, to send at this moment the batch of experiencies to GPU:


    def recall(self):
        """
        Retrieve a batch of experiences from memory
        """
        batch = random.sample(self.memory, self.batch_size)
        state, next_state, action, reward, done = map(torch.stack, zip(*batch))
        return state.cuda(), next_state.cuda(), action.squeeze().cuda(), reward.squeeze().cuda(), done.squeeze().cuda()

Not sure if this is the right approach, but it works. However, it might impact on the learning performance.

Could anyone help here?

cc @vmoens @nairbv

marcmolla avatar Jul 28 '21 09:07 marcmolla

When I tried the mario_rl_tutorial.py using CUDA, I received an out of memory error:

RuntimeError: CUDA error: out of memory

Monitoring the GPU memory I saw a constant incremental of the use of its memory, specially when the Agent starts the training. As a workaround I changed the cache method of the Agent, as it seems that all the experiencies are sent to the GPU:

        if self.use_cuda:
            state = torch.tensor(state).cuda()
            next_state = torch.tensor(next_state).cuda()
            action = torch.tensor([action]).cuda()
            reward = torch.tensor([reward]).cuda()
            done = torch.tensor([done]).cuda()
 (...)

        self.memory.append((state, next_state, action, reward, done,))

Instead of that, I modified this piece for using CPU and changed the recall method, to send at this moment the batch of experiencies to GPU:

    def recall(self):
        """
        Retrieve a batch of experiences from memory
        """
        batch = random.sample(self.memory, self.batch_size)
        state, next_state, action, reward, done = map(torch.stack, zip(*batch))
        return state.cuda(), next_state.cuda(), action.squeeze().cuda(), reward.squeeze().cuda(), done.squeeze().cuda()

Not sure if this is the right approach, but it works. However, it might impact on the learning performance.

Could anyone help here?

cc @vmoens @nairbv

I had the same issue. I found the reason is that self.memory = deque(maxlen=100000) is an unrealistic number for personal GPU. So I take 60% of that 100000, which is 60000. The peak GPU memory consumption is 15.2 GB out of my 16 GB GPU. So it fits in my memory finally.

pswpswpsw avatar Feb 10 '23 04:02 pswpswpsw

/assigntome

neuralninja27 avatar Jun 01 '23 06:06 neuralninja27