Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cpu to gpu #7

Open
onepointfuck opened this issue Feb 5, 2022 · 11 comments
Open

cpu to gpu #7

onepointfuck opened this issue Feb 5, 2022 · 11 comments

Comments

@onepointfuck
Copy link

It has been implemented with cpu, I want to implement this project with gpu, how should I do it, please advise

@AchalS-iglu
Copy link

+1

@arch-user-france1
Copy link

You might like to take a look at this...
https://towardsdatascience.com/pytorch-switching-to-the-gpu-a7c0b21e8a99

@ethicalhacker7192
Copy link

All you have to do is in the agent.py place the following code right after the model init:

model.to('cuda:0')

@dookiethedog
Copy link

dookiethedog commented Jan 1, 2024

Alright I figured out the solution.

Changes to agent.py

Add the below right after line "self.model..."
self.model.to('cuda:0')

Modify def get_action, in the else (add .to('cuda:0')
state0 = torch.tensor(state, dtype=torch.float).to('cuda:0')

Changes to model.py...

"import numpy as np"

add the below just after line "self.model = model"
self.model = model.to('cuda:0')

def train_setup needs to have these changes.

def train_step(self, states, actions, rewards, next_states, dones):
    states = torch.tensor(np.array(states), dtype=torch.float).to('cuda:0')
    actions = torch.tensor(np.array(actions), dtype=torch.long).to('cuda:0')
    rewards = torch.tensor(np.array(rewards), dtype=torch.float).to('cuda:0')
    next_states = torch.tensor(np.array(next_states), dtype=torch.float).to('cuda:0')

    if len(states.shape) == 1:
        states = torch.unsqueeze(states, 0)
        actions = torch.unsqueeze(actions, 0)
        rewards = torch.unsqueeze(rewards, 0)
        next_states = torch.unsqueeze(next_states, 0)
        dones = (dones,)

@ethicalhacker7192
Copy link

Yay!

@ethicalhacker7192
Copy link

Or you can do for agent.py:

Action = self.model(state0.to('cuda:0'))

@PayteR
Copy link

PayteR commented Jul 10, 2024

Hi guys, thx for the help i did it and it works, BUT on my PC with AMD 7950X3D and Nvidia 4090 cpu device performance is actually faster than cuda, why is that? In both cases are devices utilized 100%, thx

@ethicalhacker7192
Copy link

Sometimes GPUs are meant for graphics strictly, which may hinder them in terms of multiprocessing and calculating each action the neurons will take, it is a fairly uncommon issue.

@PayteR
Copy link

PayteR commented Jul 11, 2024

Yea, but that's THE best PC GPU right now although CPU is top too. Any other cases to process AI is like 100-1000x faster with GPU, for example LLM inferences. Im still think that there is some issue in the code.

@ethicalhacker7192
Copy link

If you could show the code, I could probably help optimize it.

@ethicalhacker7192
Copy link

ethicalhacker7192 commented Aug 12, 2024

Additionally, it would probably be better to train the AI in a headless environment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants