PyABSA icon indicating copy to clipboard operation
PyABSA copied to clipboard

Error when running example

Open songofroland opened this issue 4 years ago • 3 comments

Hi,

First of all thanks for open-sourcing such a fantastic library for ABSA.


When running examples-v2/aspec_polarity_classification/train_apc.py I came across the following error:

RuntimeError                              Traceback (most recent call last)
Cell In [8], line 38
     35 config.lsa = True
     36 config.seed = [random.randint(0, 10000) for _ in range(3)]
---> 38 APC.APCTrainer(config=config,
     39                dataset=dataset,
     40                from_checkpoint='english',
     41                checkpoint_save_mode=ModelSaveOption.SAVE_MODEL_STATE_DICT,
     42                auto_device=DeviceTypeOption.CPU,
     43                ).destroy()

File ~/workplace/uni/thesis/venv/lib/python3.10/site-packages/pyabsa-2.0.5-py3.10.egg/pyabsa/tasks/AspectPolarityClassification/trainer/apc_trainer.py:54, in APCTrainer.__init__(self, config, dataset, from_checkpoint, checkpoint_save_mode, auto_device, path_to_save, load_aug)
     51 self.config.task_code = TaskCodeOption.Aspect_Polarity_Classification
     52 self.config.task_name = TaskNameOption().get(TaskCodeOption.Aspect_Polarity_Classification)
---> 54 self._run()

File ~/workplace/uni/thesis/venv/lib/python3.10/site-packages/pyabsa-2.0.5-py3.10.egg/pyabsa/framework/trainer_class/trainer_template.py:180, in Trainer._run(self)
    178 self.config.seed = s
    179 if self.config.checkpoint_save_mode:
--> 180     model_path.append(self.training_instructor(self.config).run())
    181 else:
    182     # always return the last trained model if you don't save trained model
    183     model = self.inference_model_class(checkpoint=self.training_instructor(self.config).run())
...
    170                        'to map your storages to the CPU.')
    171 device_count = torch.cuda.device_count()
    172 if device >= device_count:

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.

I've updated the framework/instructor_class/instructor_template.py as suggested:

...
if torch.cuda.device_count() > 1 and self.config.device == DeviceTypeOption.ALL_CUDA:
    self.model.module.load_state_dict(torch.load(state_dict_path[0]))
else:
    self.model.load_state_dict(torch.load(state_dict_path[0], map_location=torch.device('cpu')))
self.model.config = self.config
...

Now the training seems to work. It'd be nice to apply this fix. I'm also happy to write a PR if you're willing to.

songofroland avatar Nov 19 '22 11:11 songofroland

Yes, thanks for your help.

yangheng95 avatar Nov 19 '22 11:11 yangheng95

I just commit an update to set the map_location=self.config.device. can you help me to test if it works?

yangheng95 avatar Nov 19 '22 12:11 yangheng95

Setting map_location=self.config.device seems to resolve the issue. Thanks for the quick fix!

songofroland avatar Nov 19 '22 12:11 songofroland