Repository navigation
Conversation
Map the fitted checkpoint to CPU when unpickling transformer models, so a model trained on GPU can be loaded with `load_model`, `load` or `loads` on a machine without CUDA. Weights are moved to the target device before inference and by the trainer when fitting, so nothing changes on GPU machines. Fixes MTSWebServices#286
maxteneff
force-pushed
the
fix/load-gpu-model-on-cpu
branch
from
October 17, 2026 17:12
39ae5c4 to
2117a2b
Compare
This branch has not been deployed
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes #286.
A fitted
SASRecModelorBERT4RecModelsaved on a GPU machine can't be loaded withload_model,loadorloadson a machine without CUDA:A pickled fitted model holds its Lightning checkpoint, which
TransformerModelBase.__setstate__reads withtorch.load(...)without amap_location. torch then tries to restore the tensors on the device they were saved on. Unlikeload_from_checkpoint,loadgives users no way to pass amap_location.This PR makes
__setstate__load the checkpoint to CPU. Nothing changes on GPU machines:TransformerLightningModule._prepare_for_inferencemoves the model torecommend_torch_device(CUDA when it's available) before every inference, and the trainer places the model on its device when fitting.Type of change
How Has This Been Tested?
test_load_fitted_model_saved_on_gpu_without_gpuforSASRecModelandBERT4RecModel. It saves a fitted model with tensors tagged ascuda:0(patchingtorch.serialization.location_tag), makestorch.cuda.is_available()returnFalse, loads the model, and checks that its recommendations match the original model. Without the fix it fails with the error above; with the fix it passes.tests/models/nn/transformers: 213 passed, 57 skipped (GPU-only).isort,black,flake8,pylint,mypyandcodespellpass on the changed files.Checklist:
Not included here:
load_weights_from_checkpointalso callstorch.loadwithout amap_location, so a GPU checkpoint passed to it fails the same way on a CPU-only machine. Happy to handle it in a follow-up, e.g. by adding amap_locationargument likeload_from_checkpointhas.