{
  "id": 205814,
  "title": "Is it possible to free CPU RAM after model.to(torch.device('cuda'))?",
  "url": "/competitions/riiid-test-answer-prediction/discussion/205814",
  "author_name": "mamas",
  "post_date": "2020-12-22T01:24:17.349000",
  "votes": 8,
  "comment_count": 12,
  "views": 0,
  "content": "<p>In this competition, we can use only 13GB CPU RAM and I would like to reduce memory usage. But I'm a beginner of pytorch and don't know how to free CPU RAM after transferring model to GPU RAM. Is there way to achieve this?<br>\nP.S. It is probably inappropriate to ask this question here, because this question is not related to this competition and doesn't need the knowledge of this competiton to answer.</p>",
  "messages": [
    {
      "id": 1121862,
      "postDate": "2020-12-22T01:24:17.350Z",
      "content": "<p>In this competition, we can use only 13GB CPU RAM and I would like to reduce memory usage. But I'm a beginner of pytorch and don't know how to free CPU RAM after transferring model to GPU RAM. Is there way to achieve this?<br>\nP.S. It is probably inappropriate to ask this question here, because this question is not related to this competition and doesn't need the knowledge of this competiton to answer.</p>",
      "rawMarkdown": "In this competition, we can use only 13GB CPU RAM and I would like to reduce memory usage. But I'm a beginner of pytorch and don't know how to free CPU RAM after transferring model to GPU RAM. Is there way to achieve this?\nP.S. It is probably inappropriate to ask this question here, because this question is not related to this competition and doesn't need the knowledge of this competiton to answer.",
      "votes": 8
    },
    {
      "id": 1122281,
      "postDate": "2020-12-22T10:28:23.867Z",
      "content": "<p>First world problems 😄</p>",
      "rawMarkdown": "First world problems 😄",
      "votes": 7
    },
    {
      "id": 1122714,
      "postDate": "2020-12-22T16:42:34.757Z",
      "content": "<p>seems it is caused by pin memory buffers<br>\n<a href=\"https://pytorch.org/docs/stable/notes/cuda.html#use-pinned-memory-buffers\" target=\"_blank\">https://pytorch.org/docs/stable/notes/cuda.html#use-pinned-memory-buffers</a></p>",
      "rawMarkdown": "seems it is caused by pin memory buffers\nhttps://pytorch.org/docs/stable/notes/cuda.html#use-pinned-memory-buffers",
      "votes": 1,
      "replies": [
        {
          "id": 1123205,
          "postDate": "2020-12-23T03:16:17.243Z",
          "rawMarkdown": "",
          "isDeleted": true
        },
        {
          "id": 1136547,
          "postDate": "2021-01-03T07:36:16.183Z",
          "content": "<p>Hi <a href=\"https://www.kaggle.com/mamasinkgs\" target=\"_blank\">@mamasinkgs</a> thank you for the link. Did it help you reduce cpu ram usage? It is not working for me. </p>",
          "rawMarkdown": "Hi @mamasinkgs thank you for the link. Did it help you reduce cpu ram usage? It is not working for me. "
        }
      ]
    },
    {
      "id": 1122404,
      "postDate": "2020-12-22T12:12:43.393Z",
      "content": "<p>Frankly speaking, i am pretty sure that CPU RAM will be cleared for sure; Need to write some code to test that, that's all i have to do to verify.</p>",
      "rawMarkdown": "Frankly speaking, i am pretty sure that CPU RAM will be cleared for sure; Need to write some code to test that, that's all i have to do to verify."
    },
    {
      "id": 1121932,
      "postDate": "2020-12-22T03:08:04.820Z",
      "content": "<p>I found a discussion on PyTorch community, but it seems that this issue is not solved (at least in this case). <br>\n<a href=\"https://discuss.pytorch.org/t/how-to-free-cpu-ram-after-module-to-cuda-device/20381/5\" target=\"_blank\">https://discuss.pytorch.org/t/how-to-free-cpu-ram-after-module-to-cuda-device/20381/5</a></p>",
      "rawMarkdown": "I found a discussion on PyTorch community, but it seems that this issue is not solved (at least in this case). \nhttps://discuss.pytorch.org/t/how-to-free-cpu-ram-after-module-to-cuda-device/20381/5",
      "replies": [
        {
          "id": 1121937,
          "postDate": "2020-12-22T03:24:32.727Z",
          "content": "<p>Thank you!</p>",
          "rawMarkdown": "Thank you!"
        }
      ]
    },
    {
      "id": 1121892,
      "postDate": "2020-12-22T02:17:01.573Z",
      "content": "<p>Are you sure that it's the PyTorch that's causing you OOM's if any? <a href=\"https://forum.pyro.ai/t/a-clever-trick-to-debug-tensor-memory/556\" target=\"_blank\">This</a> is a cool link to debug the tensors to track GPU mem.</p>",
      "rawMarkdown": "Are you sure that it's the PyTorch that's causing you OOM's if any? [This](https://forum.pyro.ai/t/a-clever-trick-to-debug-tensor-memory/556) is a cool link to debug the tensors to track GPU mem.",
      "replies": [
        {
          "id": 1121911,
          "postDate": "2020-12-22T02:41:24.013Z",
          "content": "<p>Thank you for a great link! I checked my model uses more than 1GB CPU RAM even after model.to(args.device), by using the great memory profiler <a href=\"https://www.kaggle.com/c/riiid-test-answer-prediction/discussion/203020\" target=\"_blank\">https://www.kaggle.com/c/riiid-test-answer-prediction/discussion/203020</a>. For me, GPU RAM is sufficient, but CPU RAM is not sufficient.</p>",
          "rawMarkdown": "Thank you for a great link! I checked my model uses more than 1GB CPU RAM even after model.to(args.device), by using the great memory profiler https://www.kaggle.com/c/riiid-test-answer-prediction/discussion/203020. For me, GPU RAM is sufficient, but CPU RAM is not sufficient."
        },
        {
          "id": 1121913,
          "postDate": "2020-12-22T02:46:24.687Z",
          "content": "<p>Are you using <code>num_workers &gt; 0</code>? If so, it's spinning up additional processes, which will (almost) <code>*n</code> your memory usage in the various data loaders.</p>",
          "rawMarkdown": "Are you using `num_workers > 0`? If so, it's spinning up additional processes, which will (almost) `*n` your memory usage in the various data loaders.",
          "votes": 2
        },
        {
          "id": 1121920,
          "postDate": "2020-12-22T02:51:03.220Z",
          "content": "<p>I think <a href=\"https://www.kaggle.com/authman\" target=\"_blank\">@authman</a> Mamas is more worried about the model being copied to CPU first and then moved to GPU. But the copy is still left there in RAM even though the model is moved to GPU. <br>\nWe don't need num_workers here for sure! Rather we don't quite need the data-loader either as we can prepare the batch ourselves almost.. (wrt inference)<br>\nI am not quite sure what PyTorch does when it comes to this.. </p>",
          "rawMarkdown": " I think @authman Mamas is more worried about the model being copied to CPU first and then moved to GPU. But the copy is still left there in RAM even though the model is moved to GPU. \n\n\nWe don't need num_workers here for sure! Rather we don't quite need the data-loader either as we can prepare the batch ourselves almost.. (wrt inference)\n\n\nI am not quite sure what PyTorch does when it comes to this.. ",
          "votes": 1
        },
        {
          "id": 1121926,
          "postDate": "2020-12-22T03:00:47.177Z",
          "content": "<p>yes, i want pytorch expert that knows how to free memory. maybe  I should ask this question in the issue of pytorch, because the content of the question is not related to this competition and does not need the knowledge of this competition.</p>",
          "rawMarkdown": "yes, i want pytorch expert that knows how to free memory. maybe  I should ask this question in the issue of pytorch, because the content of the question is not related to this competition and does not need the knowledge of this competition."
        }
      ]
    }
  ],
  "comments": [
    {
      "id": 1122281,
      "author_name": "Nikola Bacic",
      "author_url": "",
      "post_date": "2020-12-22T10:28:23.867000",
      "content": "<p>First world problems 😄</p>",
      "votes": 7,
      "replies": []
    },
    {
      "id": 1122714,
      "author_name": "mamas",
      "author_url": "",
      "post_date": "2020-12-22T16:42:34.757000",
      "content": "<p>seems it is caused by pin memory buffers<br>\n<a href=\"https://pytorch.org/docs/stable/notes/cuda.html#use-pinned-memory-buffers\" target=\"_blank\">https://pytorch.org/docs/stable/notes/cuda.html#use-pinned-memory-buffers</a></p>",
      "votes": 1,
      "replies": [
        {
          "id": 1123205,
          "author_name": "",
          "author_url": "",
          "post_date": "2020-12-23T03:16:17.243000",
          "content": "",
          "votes": 0,
          "replies": []
        },
        {
          "id": 1136547,
          "author_name": "Manikanth Reddy",
          "author_url": "",
          "post_date": "2021-01-03T07:36:16.183000",
          "content": "<p>Hi <a href=\"https://www.kaggle.com/mamasinkgs\" target=\"_blank\">@mamasinkgs</a> thank you for the link. Did it help you reduce cpu ram usage? It is not working for me. </p>",
          "votes": 0,
          "replies": []
        }
      ]
    },
    {
      "id": 1122404,
      "author_name": "Aditya Soni",
      "author_url": "",
      "post_date": "2020-12-22T12:12:43.393000",
      "content": "<p>Frankly speaking, i am pretty sure that CPU RAM will be cleared for sure; Need to write some code to test that, that's all i have to do to verify.</p>",
      "votes": 0,
      "replies": []
    },
    {
      "id": 1121932,
      "author_name": "u++",
      "author_url": "",
      "post_date": "2020-12-22T03:08:04.820000",
      "content": "<p>I found a discussion on PyTorch community, but it seems that this issue is not solved (at least in this case). <br>\n<a href=\"https://discuss.pytorch.org/t/how-to-free-cpu-ram-after-module-to-cuda-device/20381/5\" target=\"_blank\">https://discuss.pytorch.org/t/how-to-free-cpu-ram-after-module-to-cuda-device/20381/5</a></p>",
      "votes": 0,
      "replies": [
        {
          "id": 1121937,
          "author_name": "mamas",
          "author_url": "",
          "post_date": "2020-12-22T03:24:32.727000",
          "content": "<p>Thank you!</p>",
          "votes": 0,
          "replies": []
        }
      ]
    },
    {
      "id": 1121892,
      "author_name": "Aditya Soni",
      "author_url": "",
      "post_date": "2020-12-22T02:17:01.573000",
      "content": "<p>Are you sure that it's the PyTorch that's causing you OOM's if any? <a href=\"https://forum.pyro.ai/t/a-clever-trick-to-debug-tensor-memory/556\" target=\"_blank\">This</a> is a cool link to debug the tensors to track GPU mem.</p>",
      "votes": 0,
      "replies": [
        {
          "id": 1121911,
          "author_name": "mamas",
          "author_url": "",
          "post_date": "2020-12-22T02:41:24.013000",
          "content": "<p>Thank you for a great link! I checked my model uses more than 1GB CPU RAM even after model.to(args.device), by using the great memory profiler <a href=\"https://www.kaggle.com/c/riiid-test-answer-prediction/discussion/203020\" target=\"_blank\">https://www.kaggle.com/c/riiid-test-answer-prediction/discussion/203020</a>. For me, GPU RAM is sufficient, but CPU RAM is not sufficient.</p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 1121913,
          "author_name": "عثمان",
          "author_url": "",
          "post_date": "2020-12-22T02:46:24.687000",
          "content": "<p>Are you using <code>num_workers &gt; 0</code>? If so, it's spinning up additional processes, which will (almost) <code>*n</code> your memory usage in the various data loaders.</p>",
          "votes": 2,
          "replies": []
        },
        {
          "id": 1121920,
          "author_name": "Aditya Soni",
          "author_url": "",
          "post_date": "2020-12-22T02:51:03.220000",
          "content": "<p>I think <a href=\"https://www.kaggle.com/authman\" target=\"_blank\">@authman</a> Mamas is more worried about the model being copied to CPU first and then moved to GPU. But the copy is still left there in RAM even though the model is moved to GPU. <br>\nWe don't need num_workers here for sure! Rather we don't quite need the data-loader either as we can prepare the batch ourselves almost.. (wrt inference)<br>\nI am not quite sure what PyTorch does when it comes to this.. </p>",
          "votes": 1,
          "replies": []
        },
        {
          "id": 1121926,
          "author_name": "mamas",
          "author_url": "",
          "post_date": "2020-12-22T03:00:47.177000",
          "content": "<p>yes, i want pytorch expert that knows how to free memory. maybe  I should ask this question in the issue of pytorch, because the content of the question is not related to this competition and does not need the knowledge of this competition.</p>",
          "votes": 0,
          "replies": []
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "1121862": "In this competition, we can use only 13GB CPU RAM and I would like to reduce memory usage. But I'm a beginner of pytorch and don't know how to free CPU RAM after transferring model to GPU RAM. Is there way to achieve this?\nP.S. It is probably inappropriate to ask this question here, because this question is not related to this competition and doesn't need the knowledge of this competiton to answer.",
    "1122281": "First world problems 😄",
    "1122714": "seems it is caused by pin memory buffers\nhttps://pytorch.org/docs/stable/notes/cuda.html#use-pinned-memory-buffers",
    "1122404": "Frankly speaking, i am pretty sure that CPU RAM will be cleared for sure; Need to write some code to test that, that's all i have to do to verify.",
    "1121932": "I found a discussion on PyTorch community, but it seems that this issue is not solved (at least in this case). \nhttps://discuss.pytorch.org/t/how-to-free-cpu-ram-after-module-to-cuda-device/20381/5",
    "1121892": "Are you sure that it's the PyTorch that's causing you OOM's if any? [This](https://forum.pyro.ai/t/a-clever-trick-to-debug-tensor-memory/556) is a cool link to debug the tensors to track GPU mem."
  }
}