Files
TorchNPU/test/cpp_extensions/pluggable_allocator_extensions.cpp
T
LiNuohang c1d2049682 [feat]mempool基本结构对齐pytorch
Co-authored-by: LiNuohang<linuohang@huawei.com>



# message auto-generated for no-merge-commit merge:
!39284 merge mempool-core into master

[feat]mempool基本结构对齐pytorch

Created-by: LiNuohang
Commit-by: LiNuohang
Merged-by: ascend-robot
Description: <!--
PR描述模板更新日期:20260203
-->

# 【合入来源】
> <font color="red">**如有社区issue,请关联issue链接**</font>\
> <font color="red">**请勿携带内部流程信息(需求链接、问题单、内部issue等)**</font>

- [ ] 需求
- [ ] 问题单
- [x] issue/工单
- [ ] 重构优化
- [ ] 资料更新

# 【修改方案】
核心改动:将 MemPool 对自定义分配器的所有权从 thread-local 全局状态(MemPoolContext)改为由 PrivatePool 内部持有 shared_ptr<NPUAllocator>。
具体变更:
PrivatePool 新增 allocator_ 成员,分配/释放路径不再依赖全局状态,改为 pool->owner_PrivatePool->allocator()->raw_alloc/raw_delete
删除 MemPoolContext 类及其 thread-local 全局变量
MemPool API变更:allocator() 改为 use_count(),构造函数接收 shared_ptr 而非裸指针
API 重命名:_npu_endAllocateCurrentStreamToPool → _npu_endAllocateToPool
测试重构:将 mempool 测试集中到 test_npu_allocator.py,新增 alloc/free 配对验证和 allocator 先于 tensor 删除的回归测试

# 【资料变更】
删除 MemPoolContext 类及其 thread-local 全局变量
MemPool API变更:allocator() 改为 use_count(),构造函数接收 shared_ptr 而非裸指针
重命名:_npu_endAllocateCurrentStreamToPool → _npu_endAllocateToPool

# 【接口变更】
删除 MemPoolContext 类及其 thread-local 全局变量
MemPool API变更:allocator() 改为 use_count(),构造函数接收 shared_ptr 而非裸指针
重命名:_npu_endAllocateCurrentStreamToPool → _npu_endAllocateToPool

# 【功能验证】
> 说明测试场景,测试方法。如果本次测试方式与常规单元测试不同,请详细说明您的测试步骤\
> 新增/变更内容是否已新增/适配UT测试用例看护,并补充测试自验证截图

# 【CheckList】
> PR提交人对以下CheckList自检项进行全量自检,自检通过或不涉及,均修改 [ ] 为 [x]

- [x] 代码注释完备,正确记录错误日志
- [x] 代码实现进行了返回值、空指针等校验
- [x] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等
- [x] PR持续集成流水线(CI)执行通过,代码检查无异常


See merge request: Ascend/pytorch!39284
2026-07-02 19:35:37 +08:00

74 lines
1.7 KiB
C++

#include <sys/types.h>
#include <atomic>
#include <iostream>
#include <torch/extension.h>
#include "third_party/acl/inc/acl/acl_base.h"
#include "third_party/acl/inc/acl/acl_rt.h"
#include "torch_npu/csrc/core/npu/NPUCachingAllocator.h"
extern "C" {
using c10_npu::NPUCachingAllocator::DeviceStats;
static bool useflag = false;
static std::atomic<int> alloc_count{0};
static std::atomic<int> free_count{0};
void* my_malloc(ssize_t size, int device, aclrtStream stream)
{
void *ptr;
aclrtMallocAlign32(&ptr, size, aclrtMemMallocPolicy::ACL_MEM_MALLOC_HUGE_FIRST);
std::cout<<"alloc ptr = "<<ptr<<", size = "<<size<<std::endl;
useflag = true;
alloc_count++;
return ptr;
}
void my_free(void* ptr, ssize_t size, int device, aclrtStream stream)
{
std::cout<<"free ptr = "<<ptr<<std::endl;
aclrtFree(ptr);
free_count++;
}
bool check_custom_allocator_used()
{
return useflag;
}
int get_alloc_count()
{
return alloc_count.load();
}
int get_free_count()
{
return free_count.load();
}
void reset_alloc_free_count()
{
alloc_count.store(0);
free_count.store(0);
}
DeviceStats my_get_device_stats(c10::DeviceIndex device)
{
DeviceStats stats;
return stats;
}
void my_reset_peak_status(c10::DeviceIndex device)
{
std::cout<<"resetPeakStatus success!"<<std::endl;
}
}
PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) {
m.def("my_malloc", &my_malloc, "");
m.def("my_free", &my_free, "");
m.def("check_custom_allocator_used", &check_custom_allocator_used, "");
m.def("get_alloc_count", &get_alloc_count, "");
m.def("get_free_count", &get_free_count, "");
m.def("reset_alloc_free_count", &reset_alloc_free_count, "");
}