当前位置: 首页 > article >正文

What‘s the term “unused memory“ in PyTorch?

In the context of the PyTorch documentation, the term “unused memory” refers to GPU memory that is no longer actively used by any PyTorch tensors but is still managed by PyTorch’s caching memory allocator. Here’s how it works:

  1. Caching Allocator in PyTorch:
    PyTorch uses a caching memory allocator to speed up memory management. When PyTorch allocates GPU memory for tensors, it doesn’t immediately return that memory to the GPU when the tensors are no longer needed (e.g., after being deleted or going out of scope). Instead, PyTorch keeps the memory in its internal cache for future use.

  2. “Unused” Memory:

    • This “unused” memory is memory that is no longer being used by any active tensors but is kept in the cache by PyTorch.
    • From PyTorch’s perspective, this memory is available for reuse, so future tensor allocations can use it without requiring a new memory allocation from the GPU.
    • However, from the perspective of the driver (as shown by tools like nvidia-smi), this memory still appears to be in use. This is because PyTorch hasn’t actually released it back to the system; it is still reserved by PyTorch’s caching allocator.
  3. Why Does This Happen?

    • The caching allocator avoids the overhead of frequent memory allocation and deallocation. Allocating and freeing memory directly on the GPU can be slow and may involve device synchronization. By caching memory, PyTorch can make future allocations faster.
    • The tradeoff is that nvidia-smi gives the impression that the memory is still actively used, even though it’s not being used by any tensor at that moment.
  4. How to Free This “Unused” Memory:

    • You can call torch.cuda.empty_cache() to release unused cached memory back to the system. This will free up memory for other GPU processes and reduce the amount of memory that appears as “used” in nvidia-smi.
    • However, note that torch.cuda.empty_cache() does not release memory occupied by active tensors. Only cached (unused) memory is released.

Example:

import torch

# Allocate some GPU memory
a = torch.randn(1000, 1000, device='cuda')

# This memory is actively used by the tensor 'a'
print(torch.cuda.memory_allocated())  # Shows memory occupied by tensors
print(torch.cuda.memory_reserved())  # Shows total memory managed by PyTorch

# Free the tensor
del a

# At this point, the memory used by 'a' is now "unused" (cached by PyTorch)
print(torch.cuda.memory_allocated())  # Now 0 (no active tensor memory)
print(torch.cuda.memory_reserved())  # Still non-zero (cached memory)

# Clear the cache
torch.cuda.empty_cache()

# Now memory_reserved() should be reduced
print(torch.cuda.memory_reserved())  # Close to 0 (if no other allocations exist)

Key Takeaways:

  • Unused memory: Memory no longer used by active tensors but still reserved in PyTorch’s cache for future use.
  • Why it looks “used” in nvidia-smi: Because PyTorch hasn’t returned it to the GPU driver.
  • How to release it: Use torch.cuda.empty_cache(), but this is generally not recommended unless you want to make memory available for other GPU applications.

http://www.kler.cn/a/451751.html

相关文章:

  • python fastapi docs UI 失效解决方案
  • cannot import name ‘_C‘ from ‘pytorch3d‘
  • 【QT开发自制小工具】PDF/图片转excel---调用百度OCR API接口
  • springboot/ssm社区助老志愿者服务平台Java代码编写web志愿捐赠活动项目
  • 运动控制卡网络通讯的心跳检测之C#上位机编程
  • 39.在 Vue3 中使用 OpenLayers 导出 GeoJSON 文件及详解 GEOJSON 格式
  • 16爬虫:使用requests和scrapy分别从链家获取二手房信息
  • 什么是微端游戏?微端应该选择什么配置的服务器?
  • 2024 Gartner 数据库魔力象限概要解读
  • js和html中,将Excel文件渲染在页面上
  • vue3封装而成的APP ,在版本更新后,页面显示空白
  • 2024年种子轮融资趋势:科技引领,消费降温
  • 【理解机器学习中的过拟合与欠拟合】
  • 前端Pako.js 压缩解压库 与 Java 的 zlib 压缩与解压 的互通实现
  • 关于科研中使用linux服务器的集锦
  • 指数函数与累积效应——数学模型
  • MLCC电容加直流偏压时,为什么容值会变低?
  • 虾皮开的很高,还有签字费。
  • VUE3——003、VUE 项目中的文件结构(index.html、main.ts、App.vue)
  • 细说STM32F407单片机轮询方式读写SPI FLASH W25Q16BV
  • ArrayList与LinkedList、Vector的区别
  • 力扣658. 找到 K 个最接近的元素
  • 【RAG实战】语言模型基础
  • WPS工具栏灰色怎么办
  • 工业“元宇宙化”:科技引领生产新潮流
  • 微软的AI转型故事