{
  "id": 390589,
  "title": "[Solved] Train XGBClassifier on GPU and inference on CPU ?",
  "url": "/competitions/predict-student-performance-from-game-play/discussion/390589",
  "author_name": "",
  "post_date": "2023-02-26T10:24:05.485294300Z",
  "votes": 4,
  "comment_count": 6,
  "views": 0,
  "content": "<p>Hi,<br>\nI trained XGBClassifier models on GPU by setting <code>tree_method='gpu_hist'</code> in the parameters.<br>\nWhen I want to do inference with CPU on the models trained on GPU, I got an error indicating that there are missing dependencies.</p>\n<p>I'm saving the models with pickle and I get the error when trying to load them back with CPU.</p>\n<p>Is there a way to convert a XGBClassifier model trained on GPU to infer on CPU?</p>\n<p>Thank you!</p>\n<p>(I use RAPIDS cuDF for loading and feature engineering.)</p>\n<p>This is a trained model<br>\n<img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F11265932%2Fe33e2fccfea688388b5bdb84bd36e64b%2FCapture.PNG?generation=1677406399967687&amp;alt=media\" alt=\"\"></p>",
  "messages": [
    {
      "id": "2160031",
      "postDate": "02/26/2023 10:24:05",
      "content": "<p>Hi,<br>\nI trained XGBClassifier models on GPU by setting <code>tree_method='gpu_hist'</code> in the parameters.<br>\nWhen I want to do inference with CPU on the models trained on GPU, I got an error indicating that there are missing dependencies.</p>\n<p>I'm saving the models with pickle and I get the error when trying to load them back with CPU.</p>\n<p>Is there a way to convert a XGBClassifier model trained on GPU to infer on CPU?</p>\n<p>Thank you!</p>\n<p>(I use RAPIDS cuDF for loading and feature engineering.)</p>\n<p>This is a trained model<br>\n<img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F11265932%2Fe33e2fccfea688388b5bdb84bd36e64b%2FCapture.PNG?generation=1677406399967687&amp;alt=media\" alt=\"\"></p>",
      "rawMarkdown": "Hi,\nI trained XGBClassifier models on GPU by setting `tree_method='gpu_hist'` in the parameters.\nWhen I want to do inference with CPU on the models trained on GPU, I got an error indicating that there are missing dependencies.\n\nI'm saving the models with pickle and I get the error when trying to load them back with CPU.\n\nIs there a way to convert a XGBClassifier model trained on GPU to infer on CPU?\n\nThank you!\n\n(I use RAPIDS cuDF for loading and feature engineering.)\n\nThis is a trained model\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F11265932%2Fe33e2fccfea688388b5bdb84bd36e64b%2FCapture.PNG?generation=1677406399967687&alt=media)",
      "votes": null
    },
    {
      "id": "2160049",
      "postDate": "02/26/2023 10:52:14",
      "content": "<p>Hi, setting the parameter <code>predictor='cpu_predictor'</code> should do the job.</p>",
      "rawMarkdown": "Hi, setting the parameter `predictor='cpu_predictor'` should do the job.",
      "votes": null
    },
    {
      "id": "2160063",
      "postDate": "02/26/2023 11:10:05",
      "content": "<p>I'm not sure what the cause is, but I trained on GPU, saved to JSON, then loaded and infered on CPU just fine without setting any extra parameters other than those specified in some public notebooks (<a href=\"https://www.kaggle.com/code/cdeotte/xgboost-baseline-0-676\" target=\"_blank\">Chris's</a> for example). Maybe try saving it to JSON instead?</p>",
      "rawMarkdown": "I'm not sure what the cause is, but I trained on GPU, saved to JSON, then loaded and infered on CPU just fine without setting any extra parameters other than those specified in some public notebooks ([Chris's](https://www.kaggle.com/code/cdeotte/xgboost-baseline-0-676) for example). Maybe try saving it to JSON instead?",
      "votes": null
    },
    {
      "id": "2160160",
      "postDate": "02/26/2023 12:42:39",
      "content": "<p>Hi. Instead of pickle, use 'model.save_model()' and 'model.load_model()' and it will work.</p>",
      "rawMarkdown": "Hi. Instead of pickle, use 'model.save_model()' and 'model.load_model()' and it will work.",
      "votes": null
    },
    {
      "id": "2160167",
      "postDate": "02/26/2023 12:47:27",
      "content": "<p>It didn't work, I think that there is a problem when serializing my models with pickle. But thanks</p>",
      "rawMarkdown": "It didn't work, I think that there is a problem when serializing my models with pickle. But thanks",
      "votes": null
    },
    {
      "id": "2160170",
      "postDate": "02/26/2023 12:49:43",
      "content": "<p>Thank you, it saved the problem!</p>",
      "rawMarkdown": "Thank you, it saved the problem!",
      "votes": null
    },
    {
      "id": "2160171",
      "postDate": "02/26/2023 12:49:56",
      "content": "<p>Thank you problem solved! :)</p>",
      "rawMarkdown": "Thank you problem solved! :)",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 2160049,
      "author_name": "heiligerl",
      "author_url": "",
      "post_date": "02/26/2023 10:52:14",
      "content": "<p>Hi, setting the parameter <code>predictor='cpu_predictor'</code> should do the job.</p>",
      "votes": null,
      "replies": [
        {
          "id": 2160167,
          "author_name": "paulbacher",
          "author_url": "",
          "post_date": "02/26/2023 12:47:27",
          "content": "<p>It didn't work, I think that there is a problem when serializing my models with pickle. But thanks</p>",
          "votes": null,
          "replies": []
        }
      ]
    },
    {
      "id": 2160063,
      "author_name": "hoangnguyen719",
      "author_url": "",
      "post_date": "02/26/2023 11:10:05",
      "content": "<p>I'm not sure what the cause is, but I trained on GPU, saved to JSON, then loaded and infered on CPU just fine without setting any extra parameters other than those specified in some public notebooks (<a href=\"https://www.kaggle.com/code/cdeotte/xgboost-baseline-0-676\" target=\"_blank\">Chris's</a> for example). Maybe try saving it to JSON instead?</p>",
      "votes": null,
      "replies": [
        {
          "id": 2160170,
          "author_name": "paulbacher",
          "author_url": "",
          "post_date": "02/26/2023 12:49:43",
          "content": "<p>Thank you, it saved the problem!</p>",
          "votes": null,
          "replies": []
        }
      ]
    },
    {
      "id": 2160160,
      "author_name": "cdeotte",
      "author_url": "",
      "post_date": "02/26/2023 12:42:39",
      "content": "<p>Hi. Instead of pickle, use 'model.save_model()' and 'model.load_model()' and it will work.</p>",
      "votes": null,
      "replies": [
        {
          "id": 2160171,
          "author_name": "paulbacher",
          "author_url": "",
          "post_date": "02/26/2023 12:49:56",
          "content": "<p>Thank you problem solved! :)</p>",
          "votes": null,
          "replies": []
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "2160031": "Hi,\nI trained XGBClassifier models on GPU by setting `tree_method='gpu_hist'` in the parameters.\nWhen I want to do inference with CPU on the models trained on GPU, I got an error indicating that there are missing dependencies.\n\nI'm saving the models with pickle and I get the error when trying to load them back with CPU.\n\nIs there a way to convert a XGBClassifier model trained on GPU to infer on CPU?\n\nThank you!\n\n(I use RAPIDS cuDF for loading and feature engineering.)\n\nThis is a trained model\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F11265932%2Fe33e2fccfea688388b5bdb84bd36e64b%2FCapture.PNG?generation=1677406399967687&alt=media)",
    "2160049": "Hi, setting the parameter `predictor='cpu_predictor'` should do the job.",
    "2160063": "I'm not sure what the cause is, but I trained on GPU, saved to JSON, then loaded and infered on CPU just fine without setting any extra parameters other than those specified in some public notebooks ([Chris's](https://www.kaggle.com/code/cdeotte/xgboost-baseline-0-676) for example). Maybe try saving it to JSON instead?",
    "2160160": "Hi. Instead of pickle, use 'model.save_model()' and 'model.load_model()' and it will work.",
    "2160167": "It didn't work, I think that there is a problem when serializing my models with pickle. But thanks",
    "2160170": "Thank you, it saved the problem!",
    "2160171": "Thank you problem solved! :)"
  },
  "source": "meta"
}