Closed notreadyyet closed 4 years ago
Hello, the easiest solution is:
q_net = QNet(observe_dim, action_num).to("cuda:0")
q_net_t = QNet(observe_dim, action_num).to("cuda:0")
# let framework determine input/output device based on parameter location
# a warning will be thrown.
q_net = QNet(observe_dim, action_num)
q_net_t = QNet(observe_dim, action_num)
# to mark the input/output device Manually
# will not work if you move your model to other devices
# after wrapping
# q_net = static_module_wrapper(q_net, "cpu", "cpu")
# q_net_t = static_module_wrapper(q_net_t, "cpu", "cpu")
# to mark the input/output device Automatically
# will not work if you model locates on multiple devices
# q_net = dynamic_module_wrapper(q_net)
# q_net_t = dynamic_module_wrapper(q_net_t)
If your modules are not wrapped, the framework will try to determine your model's input&output location by its parameter location, but your model must satisfy input device = output device = parameter device(s)
you can see this process at here
dynamic_module_wrapper
is a class inheriting nn.Module
from Pytorch, after wrapping your module inside this wrapper, you will be able to move your module around, however, it is using the same mechanism to determine your input/output location as not wrapping your model at all, therefore you must also make sure: input device = output device = parameter device(s)
:
q_net = dynamic_module_wrapper(q_net).to("cuda:0")
It is meant to be a serve as a reminder to tell you that a wrapper is needed, for your raw model, since you need to specify input/output whenever possible, otherwise, it would be hard to do things like auto-partition your model, etc.
static_module_wrapper
is aimed at more complex models, with input from one device and output on another device, therefore, it is a one-time-for-all wrapper, as said in the document, you should only use this wrapper if you are not going to move your model around using .to(<device>)
, .cuda()
, .cpu()
, etc:
def static_module_wrapper(wrapped_module: nn.Module,
input_device: Union[str, t.device],
output_device: Union[str, t.device]):
"""
Wrapped module could locate on multiple devices, but must not be moved.
Input device and output device are statically specified by user.
"""
wrapped_module.input_device = input_device
wrapped_module.output_device = output_device
return wrapped_module
Feel free to reopen this if you have any questions.
I've tried to train your_first_program on a GPU by uncommenting static_module_wrapper lines like so:
and got an error:
a combination
fails too. Following setting runs, but doesn't use GPU:
What shall I do to train your_first_program on a GPU? Here is requirements.txt: