Load_learner on CPU throws "RuntimeError('Attempting to deserialize object on a CUDA)"

The 1.0.43 solved this load_learner issue for me.

But I am now attempting to use a GPU trained language model on my local CPU to train a text classifier, and I am getting the same RuntimeError when I try to load the encoder:

learn = text_classifier_learner(data_clas, AWD_LSTM, drop_mult=0.5)
learn.load_encoder(name='xxx')

Is there a work around for this?

Good point, I just added the option in master to pass a device (like load has).
If you don’t have a dev install, a workaround is to load your encoder file on the CPU then save it again:

encoder = torch.load(path/'models'/'xxx', map_location='cpu')
torch.save(encoder, path/'models'/'xxx')
1 Like

I am new to fastai and deep learning, I have 2 models that were trained and saved on GPU based system. I am now attempting to load the pkl files that were saved on the GPU system and make predictions from a CPU based computer and I keep getting this error “Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False. If you are running on a CPU-only machine, please use torch.load with map_location=‘cpu’ to map your storages to the CPU.”
FastAI version = 1.0.49
Torch version = 1.0.1

See code below.

import fastai
from fastai import *
from fastai.text import * 

def predict(text, category):
    if(category=='xyz' or category=='xyz2'):
        learn = load_learner(path=".", fname='model1.pkl')
    else:
        learn = load_learner(path=".", fname='model2.pkl')
    return str(learn.predict(text)[0])

text = 'my text goes here'
res = predict(text,'xyz')
print(res)

hi.i’m trying to solve it , but error replied.

test.py

    if args.cuda:
        refine_net.load_state_dict(copyStateDict(torch.load(args.refiner_model)))
        refine_net = refine_net.cuda()
        refine_net = torch.nn.DataParallel(refine_net)
    else:
        refine_net.load_state_dict(copyStateDict(torch.load(args.refiner_model, map_location=torch.device('cpu:0'))))

error

      File "test.py", line 128, in <module>
net.load_state_dict(copyStateDict(torch.load(args.trained_model)))
      File "C:\Users\User\Anaconda3\lib\site-packages\torch\serialization.py", line 386, in load
return _load(f, map_location, pickle_module, **pickle_load_args)
      File "C:\Users\User\Anaconda3\lib\site-packages\torch\serialization.py", line 573, in _load
result = unpickler.load()
      File "C:\Users\User\Anaconda3\lib\site-packages\torch\serialization.py", line 536, in persistent_load
deserialized_objects[root_key] = restore_location(obj, location)
      File "C:\Users\User\Anaconda3\lib\site-packages\torch\serialization.py", line 119, in default_restore_location
result = fn(storage, location)
     File "C:\Users\User\Anaconda3\lib\site-packages\torch\serialization.py", line 95, in _cuda_deserialize
device = validate_cuda_device(location)
    File "C:\Users\User\Anaconda3\lib\site-packages\torch\serialization.py", line 79, in validate_cuda_device
raise RuntimeError('Attempting to deserialize object on a CUDA '
RuntimeError: Attempting to deserialize object on a CUDA device but torch.cuda.is_available() is False. If you are running on a CPU-only machine, please use torch.load with map_location=torch.device('cpu') to map your storages to the CPU.

use map_location = torch.device("cpu") instead in your test.py script.