C/C++函数原型:mstxRangeId mstxRangeStartA(const char *message, aclrtStream stream)
Python函数:mstx.range_start(message, stream)
mstx range能力的起始位置标记。
参数 |
输入/输出 |
说明 |
---|---|---|
message |
输入 |
message为标记的文字,携带打点信息。 C/C++中数据类型:const char *。 Python中,message为字符串。 默认None。 |
stream |
输入 |
stream表示使用mark的线程。 C/C++中数据类型:aclrtStream。 Python中stream是aclrtStream对象。 默认None。 |
如果返回0,则表示失败。
... bool RunOp() { // create op desc ... char message = "h1"; mstxRangeId id = mstxRangeStartA(message, NULL); ... // Run op if (!opRunner.RunOp()) { ERROR_LOG("Run op failed"); return false; } mstxRangeEnd(id); ...
通过Python API接口,以C/C++语言实现相关接口内容并编译生成so,相关so在PYTHONPATH中可以被python直接引用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import mstx mstx.range_start("aaa", 1) print(1) mstx.range_end(1) import torch import torch_npu a = torch.Tensor([1,2,3,4]).npu() b = torch.Tensor([1,2,3,4]).npu() hi_str = "hi" hello_str = "hello" hi_id = mstx.range_start(hi_str, None) c = a + b hello_id = mstx.range_start(hello_str, stream=None) d = a - b mstx.range_end(hi_id) e = a * b mstx.range_end(hello_id) |
直接使用Python开发,通过ctypes.CDLL("libms_tools_ext.so")直接引用原mstx的so文件,并使用其中提供的API。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | import mstx import torch import torch_npu import acl import sys import ctypes lib = ctypes.CDLL("libms_tools_ext.so") # 定义函数的参数类型和返回类型 lib.mstxRangeStartA.argtypes = [ctypes.c_char_p, ctypes.c_void_p] lib.mstxRangeStartA.restype = ctypes.c_uint64 lib.mstxRangeEnd.argtypes = [ctypes.c_uint64] lib.mstxRangeEnd.restype = None a = torch.Tensor([1,2,3,4]).npu() b = torch.Tensor([1,2,3,4]).npu() # 创建一个ctypes.c_char_p指针 hi_str = b"hi" hi_ptr = ctypes.c_char_p(hi_str) hi_id = ctypes.c_uint64() # 创建一个ctypes.c_char_p指针 hello_str = b"hello" hello_ptr = ctypes.c_char_p(hello_str) hello_id = ctypes.c_uint64() # 调用函数 hi_id.value = lib.mstxRangeStartA(hi_ptr, None) c = a + b hello_id.value = lib.mstxRangeStartA(hello_ptr, None) d = a - b lib.mstxRangeEnd(hi_id) e = a * b lib.mstxRangeEnd(hello_id) |