Last active
April 9, 2018 09:23
-
-
Save dylandjian/05d872c6d3d74e80c04bb70187090c3e to your computer and use it in GitHub Desktop.
PyTorch cuda device pointer error ?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import multiprocessing | |
import multiprocessing.pool | |
import torch | |
class NoDaemonProcess(multiprocessing.Process): | |
# make 'daemon' attribute always return False | |
def _get_daemon(self): | |
return False | |
def _set_daemon(self, value): | |
pass | |
daemon = property(_get_daemon, _set_daemon) | |
class MyPool(multiprocessing.pool.Pool): | |
Process = NoDaemonProcess | |
class Net1(torch.nn.Module): | |
def __init__(self): | |
super(Net1, self).__init__() | |
self.fc = torch.nn.Linear(1, 1) | |
def final_process(model): | |
print(model) | |
def new_process2(model): | |
pool = multiprocessing.Pool(1) | |
new_process = pool.apply_async(final_process, args=(model,)) | |
new_process.get() | |
pool.close() | |
pool.join() | |
def new_process1(): | |
model = Net1().cuda() | |
pool = MyPool(1) | |
new_process = pool.apply_async(new_process2, args=(model,)) | |
new_process.get() | |
pool.close() | |
pool.join() | |
def main(): | |
multiprocessing.set_start_method("spawn") | |
pool = MyPool() | |
new_process = pool.apply_async(new_process1) | |
new_process.get() | |
pool.close() | |
pool.join() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment