Skip to content

Conversation

@From00
Copy link
Contributor

@From00 From00 commented May 23, 2022

PR types

New features

PR changes

Others

Describe

增加CPU内存统计。

主机端和设备端通过不同接口进行区分:
主机端内存统计接口:

int64_t HostMemoryStatCurrentValue(const std::string& stat_type, int dev_id);
int64_t HostMemoryStatPeakValue(const std::string& stat_type, int dev_id);
void HostMemoryStatUpdate(const std::string& stat_type, int dev_id, int64_t increment);

对应运行时性能更高的宏函数:

#define HOST_MEMORY_STAT_CURRENT_VALUE(item, id)
#define HOST_MEMORY_STAT_PEAK_VALUE(item, id)
#define HOST_MEMORY_STAT_UPDATE(item, id, increment)

设备端显存统计接口:

int64_t DeviceMemoryStatCurrentValue(const std::string& stat_type, int dev_id);
int64_t DeviceMemoryStatPeakValue(const std::string& stat_type, int dev_id);
void DeviceMemoryStatUpdate(const std::string& stat_type, int dev_id, int64_t increment);

对应运行时性能更高的宏函数:
#define DEVICE_MEMORY_STAT_CURRENT_VALUE(item, id)
#define DEVICE_MEMORY_STAT_PEAK_VALUE(item, id)
#define DEVICE_MEMORY_STAT_UPDATE(item, id, increment)

目前主机端和设备端支持的统计类型(stat_type)均包括Allocated和Reserced,前者为Allocator中分配给Tensor持有的内存/显存,后者为Allocator向硬件实际申请的内存/显存(包括分配给Tensor的和框架底层缓存和管理的两部分),设备端支持多种异构设备,不限于GPU。

为何要通过不同接口名称进行区分,而不直接通过stat_type进行区分,如主机端信息stat_type="HostAllocated", 设备端信息stat_type="DeviceAllocated"?
因为需要对主机端和设备端这两种统计类型做不同的实现。
目前主要的驱动点来自编译性能方面的考虑,设备端对每张卡单独统计显存信息,最大支持卡数为16,因而会在编译期为每种设备端统计类型定义16个统计对象。这将在stats.h中带来大量的宏和模板展开,而stats.h又被大量文件引用,导致在编译时引入大量的符号,既影响编译速度,也会使得编译build目录体积大幅增长(增加GPU显存统计功能的PR #38657 使Coverage CI上build目录体积从136G增长到了140G)。
基于上述原因,此PR若复用之前的GPU统计接口,直接增加标识主机端的stat_type,将同样会导致编译build目录体积过大的增长。因此,选择通过接口名称而不是stat_type对主机端和设备端做区分,在主机端接口里,每种统计类型只需要支持1个设备,引入的符号数量可降低为1/16。

@paddle-bot-old
Copy link

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

Copy link
Contributor

@zhiqiu zhiqiu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@From00 From00 merged commit 21f11d3 into PaddlePaddle:develop May 27, 2022
@From00 From00 deleted the support-memory-stats-for-cpu branch April 5, 2023 09:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants