Files
TorchNPU/test/cpp_extensions/pluggable_allocator_extensions.cpp
T
LiNuohang 033d3c1b6d [feat] support torch.accelerator
Co-authored-by: LiNuohang<linuohang@huawei.com>



# message auto-generated for no-merge-commit merge:
!35518 merge acc-m into master

[feat] support torch.accelerator

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/工单
- [ ] 重构优化
- [ ] 资料更新

# 【修改方案】
对齐社区支持torch.accelerator接口

# 【资料变更】
涉及

# 【接口变更】
新增API/环境变量列表:
| --- | --- | --- |
| accelerator接口 | PyTorch开始支持版本 | PTA开始支持版本 |
| device_count | 2.6.0 | 已支持 |
| is_available | 2.6.0 | 已支持 |
| current_accelerator | 2.6.0 | 已支持 |
| set_device_index | 2.6.0 | 已支持 |
| set_device_idx | 2.6.0 | 已支持 |
| current_device_index | 2.6.0 | 已支持 |
| current_device_idx | 2.6.0 | 已支持 |
| get_device_capability | 2.12.0 | 本次支持2.13.0 |
| set_stream | 2.6.0 | 已支持 |
| current_stream | 2.6.0 | 已支持 |
| synchronize | 2.6.0 | 已支持 |
| device_index | 2.8.0 | 本次支持2.13.0 |
| Graph | | 不关注 |
| Memory
management | | |
| empty_cache | 2.9.1 | 本次支持2.13.0 |
| empty_host_cache | 2.12.0 | 本次支持2.13.0 |
| get_memory_info | 2.10.0 | 本次支持2.13.0 |
| max_memory_allocated | 2.9.1 | 本次支持2.13.0 |
| max_memory_reserved | 2.9.1 | 本次支持2.13.0 |
| memory_allocated | 2.9.1 | 本次支持2.13.0 |
| memory_reserved | 2.9.1 | 本次支持2.13.0 |
| memory_stats | 2.9.1 | 本次支持2.13.0 |
| reset_accumulated_memory_stats | 2.9.1 | 本次支持2.13.0 |
| reset_peak_memory_stats | 2.9.1 | 本次支持2.13.0 |

# 【功能验证】
新增UT,测试新增接口是否可用

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

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


See merge request: Ascend/pytorch!35518
2026-06-24 15:08:25 +08:00

50 lines
1.2 KiB
C++

#include <sys/types.h>
#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;
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;
return ptr;
}
void my_free(void* ptr, ssize_t size, int device, aclrtStream stream)
{
std::cout<<"free ptr = "<<ptr<<std::endl;
aclrtFree(ptr);
}
bool check_custom_allocator_used()
{
return useflag;
}
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, "");
}