From ce005a6aae3336ef206274123286e36da4de469c Mon Sep 17 00:00:00 2001 From: kai-ma Date: Fri, 11 Jul 2025 11:51:28 +0800 Subject: [PATCH] add csrc/python --- msprobe/msprobe/csrc/python/PyACLActuator.cpp | 591 ++++++++++++++++++ msprobe/msprobe/csrc/python/PyACLActuator.h | 26 + msprobe/msprobe/csrc/python/PyInterface.cpp | 64 ++ msprobe/msprobe/csrc/python/PyLog.cpp | 107 ++++ msprobe/msprobe/csrc/python/PyLog.h | 32 + 5 files changed, 820 insertions(+) create mode 100644 msprobe/msprobe/csrc/python/PyACLActuator.cpp create mode 100644 msprobe/msprobe/csrc/python/PyACLActuator.h create mode 100644 msprobe/msprobe/csrc/python/PyInterface.cpp create mode 100644 msprobe/msprobe/csrc/python/PyLog.cpp create mode 100644 msprobe/msprobe/csrc/python/PyLog.h diff --git a/msprobe/msprobe/csrc/python/PyACLActuator.cpp b/msprobe/msprobe/csrc/python/PyACLActuator.cpp new file mode 100644 index 00000000000..de87fa3f247 --- /dev/null +++ b/msprobe/msprobe/csrc/python/PyACLActuator.cpp @@ -0,0 +1,591 @@ +/* + * Copyright (C) 2025-2025. Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "python/PyACLActuator.h" + +#include "acl/include/AclApi.h" +#include "utils/Constant.h" + +namespace MSPROBE_C { + PyDoc_STRVAR(ACLInferfaceModuleDoc, "The part of the module acl actuator that is implemented in CXX."); + static PyObject *pyCallBack = nullptr; + + static PyObject *AclApi_AclInit(PyObject *module) { + int ret = CALL_ACL_API(AclInit); + return PyLong_FromLong(ret); + } + + static PyObject *AclApi_AclRtSetDevice(PyObject *module, PyObject *arg) { + int ret = -1; + if (!PyLong_Check(arg)) { + PyErr_SetString(PyExc_TypeError, " should be a integer."); + return PyLong_FromLong(ret); + } + int32_t num = static_cast(PyLong_AsLong(arg)); + if (PyErr_Occurred()) { + return PyLong_FromLong(ret); + } + ret = CALL_ACL_API(AclRtSetDevice, num); + return PyLong_FromLong(ret); + } + + static PyObject *AclApi_AclRtCreateContext(PyObject *module, PyObject *arg) { + int ret = -1; + aclrtContext context; + PyObject *pyContext = nullptr; + if (!PyLong_Check(arg)) { + PyErr_SetString(PyExc_TypeError, " should be a integer."); + return Py_BuildValue("(Oi)", Py_None, ret); + } + int32_t num = static_cast(PyLong_AsLong(arg)); + if (PyErr_Occurred()) { + return Py_BuildValue("(Oi)", Py_None, ret); + } + ret = CALL_ACL_API(AclRtCreateContext, &context, num); + pyContext = PyCapsule_New(context, "aclrtContext", nullptr); + if (pyContext == nullptr) { + return Py_BuildValue("(Oi)", Py_None, ret); + } + return Py_BuildValue("(Oi)", pyContext, ret); + } + + static PyObject *AclApi_AclMdlLoadFromFile(PyObject *module, PyObject *arg) { + int ret = -1; + if (!PyUnicode_Check(arg)) { + PyErr_SetString(PyExc_TypeError, " should be a string."); + return Py_BuildValue("(Oi)", Py_None, ret); + } + const char *path = PyUnicode_AsUTF8(arg); + if (path == nullptr) { + return Py_BuildValue("(Oi)", Py_None, ret); + } + Ascendcl::LoadFileResult cTuple = CALL_ACL_API(AclMdlLoadFromFile, path); + return Py_BuildValue("(Ii)", cTuple.modelId, cTuple.ret); + } + + static PyObject *AclApi_AclMdlCreateDesc(PyObject *module) { + aclmdlDesc *modelDesc = CALL_ACL_API(AclMdlCreateDesc); + PyObject *pyModelDesc = PyCapsule_New(modelDesc, "aclmdlDesc", nullptr); + if (pyModelDesc == nullptr) { + Py_RETURN_NONE; + } + return pyModelDesc; + } + + static PyObject *AclApi_AclMdlGetDesc(PyObject *module, PyObject *args) { + int ret = -1; + // Two parameters: modelDesc and modelId. + if (args == nullptr || PyTuple_GET_SIZE(args) != Cst::ARGS_LEN_2) { + PyErr_SetString(PyExc_TypeError, " expects 2 arguments."); + return PyLong_FromLong(ret); + } + PyObject *pyModelDesc = nullptr; + uint32_t modelId; + if (!PyArg_ParseTuple(args, "OI", &pyModelDesc, &modelId)) { + return PyLong_FromLong(ret); + } + aclmdlDesc *modelDesc = reinterpret_cast(PyCapsule_GetPointer(pyModelDesc, "aclmdlDesc")); + ret = CALL_ACL_API(AclMdlGetDesc, modelDesc, modelId); + return PyLong_FromLong(ret); + } + + static PyObject *AclApi_AclMdlGetNumInputs(PyObject *module, PyObject *args) { + size_t inputSize; + PyObject *pyModelDesc = nullptr; + if (!PyArg_ParseTuple(args, "O", &pyModelDesc)) { + Py_RETURN_NONE; + } + aclmdlDesc *modelDesc = reinterpret_cast(PyCapsule_GetPointer(pyModelDesc, "aclmdlDesc")); + inputSize = CALL_ACL_API(AclMdlGetNumInputs, modelDesc); + return PyLong_FromSize_t(inputSize); + } + + static PyObject *AclApi_AclMdlGetNumOutputs(PyObject *module, PyObject *args) { + size_t outputSize; + PyObject *pyModelDesc = nullptr; + if (!PyArg_ParseTuple(args, "O", &pyModelDesc)) { + Py_RETURN_NONE; + } + aclmdlDesc *modelDesc = reinterpret_cast(PyCapsule_GetPointer(pyModelDesc, "aclmdlDesc")); + outputSize = CALL_ACL_API(AclMdlGetNumOutputs, modelDesc); + return PyLong_FromSize_t(outputSize); + } + + static PyObject *AclApi_AclMdlGetInputNameByIndex(PyObject *module, PyObject *args) { + const char *name; + // Two parameters: modelDesc and index. + if (args == nullptr || PyTuple_GET_SIZE(args) != Cst::ARGS_LEN_2) { + PyErr_SetString(PyExc_TypeError, " expects 2 arguments."); + Py_RETURN_NONE; + } + PyObject *pyModelDesc = nullptr; + size_t index; + if (!PyArg_ParseTuple(args, "OK", &pyModelDesc, &index)) { + Py_RETURN_NONE; + } + aclmdlDesc *modelDesc = reinterpret_cast(PyCapsule_GetPointer(pyModelDesc, "aclmdlDesc")); + name = CALL_ACL_API(AclMdlGetInputNameByIndex, modelDesc, index); + return PyUnicode_FromString(name); + } + + static PyObject *AclApi_AclMdlGetInputSizeByIndex(PyObject *module, PyObject *args) { + size_t inputSize; + // Two parameters: modelDesc and index. + if (args == nullptr || PyTuple_GET_SIZE(args) != Cst::ARGS_LEN_2) { + PyErr_SetString(PyExc_TypeError, " expects 2 arguments."); + Py_RETURN_NONE; + } + PyObject *pyModelDesc = nullptr; + size_t index; + if (!PyArg_ParseTuple(args, "OK", &pyModelDesc, &index)) { + Py_RETURN_NONE; + } + aclmdlDesc *modelDesc = reinterpret_cast(PyCapsule_GetPointer(pyModelDesc, "aclmdlDesc")); + inputSize = CALL_ACL_API(AclMdlGetInputSizeByIndex, modelDesc, index); + return PyLong_FromSize_t(inputSize); + } + + static PyObject *AclApi_AclMdlGetOutputSizeByIndex(PyObject *module, PyObject *args) { + size_t outputSize; + // Two parameters: modelDesc and index. + if (args == nullptr || PyTuple_GET_SIZE(args) != Cst::ARGS_LEN_2) { + PyErr_SetString(PyExc_TypeError, " expects 2 arguments."); + Py_RETURN_NONE; + } + PyObject *pyModelDesc = nullptr; + size_t index; + if (!PyArg_ParseTuple(args, "OK", &pyModelDesc, &index)) { + Py_RETURN_NONE; + } + aclmdlDesc *modelDesc = reinterpret_cast(PyCapsule_GetPointer(pyModelDesc, "aclmdlDesc")); + outputSize = CALL_ACL_API(AclMdlGetOutputSizeByIndex, modelDesc, index); + return PyLong_FromSize_t(outputSize); + } + + static PyObject *AclApi_AclMdlGetInputDataType(PyObject *module, PyObject *args) { + // Two parameters: modelDesc and index. + if (args == nullptr || PyTuple_GET_SIZE(args) != Cst::ARGS_LEN_2) { + PyErr_SetString(PyExc_TypeError, " expects 2 arguments."); + Py_RETURN_NONE; + } + PyObject *pyModelDesc = nullptr; + size_t index; + if (!PyArg_ParseTuple(args, "OK", &pyModelDesc, &index)) { + Py_RETURN_NONE; + } + aclmdlDesc *modelDesc = reinterpret_cast(PyCapsule_GetPointer(pyModelDesc, "aclmdlDesc")); + int type = CALL_ACL_API(AclMdlGetInputDataType, modelDesc, index); + return PyLong_FromLong(type); + } + + static PyObject *AclApi_AclMdlGetInputDims(PyObject *module, PyObject *args) { + int ret = -1; + // Two parameters: modelDesc and index. + if (args == nullptr || PyTuple_GET_SIZE(args) != Cst::ARGS_LEN_2) { + PyErr_SetString(PyExc_TypeError, " expects 2 arguments."); + return Py_BuildValue("(Oi)", Py_None, ret); + } + PyObject *pyModelDesc = nullptr; + size_t index; + if (!PyArg_ParseTuple(args, "OK", &pyModelDesc, &index)) { + return Py_BuildValue("(Oi)", Py_None, ret); + } + aclmdlDesc *modelDesc = reinterpret_cast(PyCapsule_GetPointer(pyModelDesc, "aclmdlDesc")); + aclmdlIODims ioDims; + ret = CALL_ACL_API(AclMdlGetInputDims, modelDesc, index, &ioDims); + PyObject *dimsDict = PyDict_New(); + PyObject *pyDims = PyList_New(ioDims.dimCount); + for (size_t i = 0; i < ioDims.dimCount; ++i) { + PyList_SET_ITEM(pyDims, i, PyLong_FromSize_t(ioDims.dims[i])); + } + PyDict_SetItemString(dimsDict, "name", PyUnicode_FromString(ioDims.name)); + PyDict_SetItemString(dimsDict, "dimCount", PyLong_FromSize_t(ioDims.dimCount)); + PyDict_SetItemString(dimsDict, "dims", pyDims); + return Py_BuildValue("(Oi)", dimsDict, ret); + } + + static PyObject *AclApi_AclRtMalloc(PyObject *module, PyObject *args) { + int ret = -1; + void *ptr = nullptr; + size_t bufferSize; + PyObject *pyMallocPtr = nullptr; + if (!PyArg_ParseTuple(args, "K", &bufferSize)) { + return Py_BuildValue("(Oi)", Py_None, ret); + } + ret = CALL_ACL_API(AclRtMalloc, &ptr, bufferSize); + pyMallocPtr = PyCapsule_New(ptr, "aclrtMallocPtr", nullptr); + if (pyMallocPtr == nullptr) { + return Py_BuildValue("(Oi)", Py_None, ret); + } + return Py_BuildValue("(Oi)", pyMallocPtr, ret); + } + + static PyObject *AclApi_AclMdlCreateDataset(PyObject *module) { + aclmdlDataset *dataset = CALL_ACL_API(AclMdlCreateDataset); + PyObject *pyDataset = PyCapsule_New(dataset, "aclmdlDataset", nullptr); + if (pyDataset == nullptr) { + Py_RETURN_NONE; + } + return pyDataset; + } + + static PyObject *AclApi_AclCreateDataBuffer(PyObject *module, PyObject *args) { + // Two parameters: ptr and buffersize. + if (args == nullptr || PyTuple_GET_SIZE(args) != Cst::ARGS_LEN_2) { + PyErr_SetString(PyExc_TypeError, " expects 2 arguments."); + Py_RETURN_NONE; + } + PyObject *pyMallocPtr = nullptr; + size_t bufferSize; + if (!PyArg_ParseTuple(args, "OK", &pyMallocPtr, &bufferSize)) { + Py_RETURN_NONE; + } + void *ptr = reinterpret_cast(PyCapsule_GetPointer(pyMallocPtr, "aclrtMallocPtr")); + aclDataBuffer *dataBuffer = CALL_ACL_API(AclCreateDataBuffer, ptr, bufferSize); + PyObject *pyDataBuffer = PyCapsule_New(dataBuffer, "aclDataBuffer", nullptr); + if (pyDataBuffer == nullptr) { + Py_RETURN_NONE; + } + return pyDataBuffer; + } + + static PyObject *AclApi_AclMdlAddDatasetBuffer(PyObject *module, PyObject *args) { + int ret = -1; + // Two parameters: dataset and buffer. + if (args == nullptr || PyTuple_GET_SIZE(args) != Cst::ARGS_LEN_2) { + PyErr_SetString(PyExc_TypeError, " expects 2 arguments."); + return PyLong_FromLong(ret); + } + PyObject *pyDataset = nullptr; + PyObject *pyDataBuffer = nullptr; + if (!PyArg_ParseTuple(args, "OO", &pyDataset, &pyDataBuffer)) { + return PyLong_FromLong(ret); + } + aclDataBuffer *buffer = reinterpret_cast(PyCapsule_GetPointer(pyDataBuffer, "aclDataBuffer")); + aclmdlDataset *dataset = reinterpret_cast(PyCapsule_GetPointer(pyDataset, "aclmdlDataset")); + ret = CALL_ACL_API(AclMdlAddDatasetBuffer, dataset, buffer); + return PyLong_FromLong(ret); + } + + static PyObject *AclApi_AclMdlExecute(PyObject *module, PyObject *args) { + int ret = -1; + // Three parameters: modelId, inputDataset and outputDataset. + if (args == nullptr || PyTuple_GET_SIZE(args) != Cst::ARGS_LEN_3) { + PyErr_SetString(PyExc_TypeError, " expects 3 arguments."); + return PyLong_FromLong(ret); + } + PyObject *pyInputDataset = nullptr; + PyObject *pyOutputDataset = nullptr; + uint32_t modelId; + if (!PyArg_ParseTuple(args, "IOO", &modelId, &pyInputDataset, &pyOutputDataset)) { + return PyLong_FromLong(ret); + } + aclmdlDataset *inputDataset = + reinterpret_cast(PyCapsule_GetPointer(pyInputDataset, "aclmdlDataset")); + aclmdlDataset *outputDataset = + reinterpret_cast(PyCapsule_GetPointer(pyOutputDataset, "aclmdlDataset")); + ret = CALL_ACL_API(AclMdlExecute, modelId, inputDataset, outputDataset); + return PyLong_FromLong(ret); + } + + static PyObject *AclApi_AclRtMemcpy(PyObject *module, PyObject *args) { + int ret = -1; + // Five parameters: dest, destMax, src, count and kind. + if (args == nullptr || PyTuple_GET_SIZE(args) != Cst::ARGS_LEN_5) { + PyErr_SetString(PyExc_TypeError, " expects 5 arguments."); + return PyLong_FromLong(ret); + } + PyObject *pyDst = nullptr; + PyObject *pySrc = nullptr; + size_t destCount; + size_t srcCount; + int kind; + if (!PyArg_ParseTuple(args, "OKOKi", &pyDst, &destCount, &pySrc, &srcCount, &kind)) { + return PyLong_FromLong(ret); + } + const void *src = nullptr; + if (PyCapsule_CheckExact(pySrc)) { + src = reinterpret_cast(PyCapsule_GetPointer(pySrc, "aclrtMallocPtr")); + } else if (PyBytes_Check(pySrc)) { + src = reinterpret_cast(PyBytes_AsString(pySrc)); + } else { + PyErr_SetString(PyExc_TypeError, " invalid type."); + } + void *dst = reinterpret_cast(PyCapsule_GetPointer(pyDst, "aclrtMallocPtr")); + aclrtMemcpyKind cKind = static_cast(kind); + ret = CALL_ACL_API(AclRtMemcpy, dst, destCount, src, srcCount, cKind); + return PyLong_FromLong(ret); + } + + static PyObject *AclApi_AclMdlGetDatasetNumBuffers(PyObject *module, PyObject *args) { + size_t bufferNum; + PyObject *pyDataset = nullptr; + if (!PyArg_ParseTuple(args, "O", &pyDataset)) { + Py_RETURN_NONE; + } + const aclmdlDataset *dataset = + reinterpret_cast(PyCapsule_GetPointer(pyDataset, "aclmdlDataset")); + bufferNum = CALL_ACL_API(AclMdlGetDatasetNumBuffers, dataset); + return PyLong_FromSize_t(bufferNum); + } + + static PyObject *AclApi_AclMdlGetDatasetBuffer(PyObject *module, PyObject *args) { + // Two parameters: dataset and index. + if (args == nullptr || PyTuple_GET_SIZE(args) != Cst::ARGS_LEN_2) { + PyErr_SetString(PyExc_TypeError, " expects 2 arguments."); + Py_RETURN_NONE; + } + PyObject *pyDataset = nullptr; + size_t index; + if (!PyArg_ParseTuple(args, "OK", &pyDataset, &index)) { + Py_RETURN_NONE; + } + const aclmdlDataset *dataset = + reinterpret_cast(PyCapsule_GetPointer(pyDataset, "aclmdlDataset")); + aclDataBuffer *dataBuffer = CALL_ACL_API(AclMdlGetDatasetBuffer, dataset, index); + PyObject *pyDataBuffer = PyCapsule_New(dataBuffer, "aclDataBuffer", nullptr); + if (pyDataBuffer == nullptr) { + Py_RETURN_NONE; + } + return pyDataBuffer; + } + + static PyObject *AclApi_AclDestroyDataBuffer(PyObject *module, PyObject *args) { + int ret = -1; + PyObject *pyDataBuffer = nullptr; + if (!PyArg_ParseTuple(args, "O", &pyDataBuffer)) { + return PyLong_FromLong(ret); + } + const aclDataBuffer *buffer = + reinterpret_cast(PyCapsule_GetPointer(pyDataBuffer, "aclDataBuffer")); + ret = CALL_ACL_API(AclDestroyDataBuffer, buffer); + return PyLong_FromLong(ret); + } + + static PyObject *AclApi_AclMdlDestroyDataset(PyObject *module, PyObject *args) { + int ret = -1; + PyObject *pyDataset = nullptr; + if (!PyArg_ParseTuple(args, "O", &pyDataset)) { + return PyLong_FromLong(ret); + } + const aclmdlDataset *dataset = + reinterpret_cast(PyCapsule_GetPointer(pyDataset, "aclmdlDataset")); + ret = CALL_ACL_API(AclMdlDestroyDataset, dataset); + return PyLong_FromLong(ret); + } + + static PyObject *AclApi_AclRtFree(PyObject *module, PyObject *args) { + int ret = -1; + PyObject *pyMallocPtr = nullptr; + if (!PyArg_ParseTuple(args, "O", &pyMallocPtr)) { + return PyLong_FromLong(ret); + } + void *ptr = reinterpret_cast(PyCapsule_GetPointer(pyMallocPtr, "aclrtMallocPtr")); + ret = CALL_ACL_API(AclRtFree, ptr); + return PyLong_FromLong(ret); + } + + static PyObject *AclApi_AclFinalize(PyObject *module) { + int ret = CALL_ACL_API(AclFinalize); + return PyLong_FromLong(ret); + } + + static PyObject *AclApi_AclMdlUnload(PyObject *module, PyObject *arg) { + int ret = -1; + if (!PyLong_Check(arg)) { + PyErr_SetString(PyExc_TypeError, " should be a integer."); + return PyLong_FromLong(ret); + } + int32_t num = static_cast(PyLong_AsLong(arg)); + if (PyErr_Occurred()) { + return PyLong_FromLong(ret); + } + ret = CALL_ACL_API(AclMdlUnload, num); + return PyLong_FromLong(ret); + } + + static PyObject *AclApi_AclMdlDestroyDesc(PyObject *module, PyObject *args) { + int ret = -1; + PyObject *pyModelDesc = nullptr; + if (!PyArg_ParseTuple(args, "O", &pyModelDesc)) { + return PyLong_FromLong(ret); + } + aclmdlDesc *modelDesc = reinterpret_cast(PyCapsule_GetPointer(pyModelDesc, "aclmdlDesc")); + ret = CALL_ACL_API(AclMdlDestroyDesc, modelDesc); + return PyLong_FromLong(ret); + } + + static PyObject *AclApi_AclRtDestroyContext(PyObject *module, PyObject *args) { + int ret = -1; + PyObject *pyContext = nullptr; + if (!PyArg_ParseTuple(args, "O", &pyContext)) { + return PyLong_FromLong(ret); + } + aclrtContext modelDesc = reinterpret_cast(PyCapsule_GetPointer(pyContext, "aclrtContext")); + ret = CALL_ACL_API(AclRtDestroyContext, modelDesc); + return PyLong_FromLong(ret); + } + + static PyObject *AclApi_AclRtResetDevice(PyObject *module, PyObject *arg) { + int ret = -1; + if (!PyLong_Check(arg)) { + PyErr_SetString(PyExc_TypeError, " should be a integer."); + return PyLong_FromLong(ret); + } + int32_t num = static_cast(PyLong_AsLong(arg)); + if (PyErr_Occurred()) { + return PyLong_FromLong(ret); + } + ret = CALL_ACL_API(AclRtResetDevice, num); + return PyLong_FromLong(ret); + } + + static PyObject *AclApi_AclInitDump(PyObject *module) { + int ret = CALL_ACL_API(AclInitDump); + return PyLong_FromLong(ret); + } + + static PyObject *AclApi_AclSetDump(PyObject *module, PyObject *arg) { + int ret = -1; + if (!PyUnicode_Check(arg)) { + PyErr_SetString(PyExc_TypeError, "\"dump config path\" should be a string."); + return PyLong_FromLong(ret); + } + const char *path = PyUnicode_AsUTF8(arg); + if (path == nullptr) { + return PyLong_FromLong(ret); + } + ret = CALL_ACL_API(AclSetDump, path); + return PyLong_FromLong(ret); + } + + static PyObject *AclApi_AclFinalizeDump(PyObject *module) { + int ret = CALL_ACL_API(AclFinalizeDump); + return PyLong_FromLong(ret); + } + + static int32_t CppToPyCallBack(const acldumpChunk *chunk, int32_t len) { + if (!pyCallBack || !chunk) { + return 0; + } + if (!PyCallable_Check(pyCallBack)) { + PyErr_SetString(PyExc_TypeError, "callback func must can be call."); + return 0; + } + PyObject *pyChunk = PyDict_New(); + PyDict_SetItemString(pyChunk, "file_name", PyUnicode_FromString(chunk->fileName)); + PyDict_SetItemString(pyChunk, "buf_len", PyLong_FromUnsignedLong(chunk->bufLen)); + PyDict_SetItemString(pyChunk, "is_last_chunk", PyLong_FromUnsignedLong(chunk->isLastChunk)); + PyDict_SetItemString(pyChunk, "offset", PyLong_FromLongLong(chunk->offset)); + PyDict_SetItemString(pyChunk, "flag", PyLong_FromLong(chunk->flag)); + PyDict_SetItemString(pyChunk, + "data_buf", + PyBytes_FromStringAndSize(reinterpret_cast(chunk->dataBuf), chunk->bufLen)); + PyObject *args = Py_BuildValue("(Oi)", pyChunk, PyLong_FromLong(len)); + PyObject *result = PyObject_CallObject(pyCallBack, args); + Py_XDECREF(args); + Py_XDECREF(result); + Py_XDECREF(pyChunk); + return 0; + } + + static PyObject *AclApi_AclDumpRegCallBack(PyObject *module, PyObject *args) { + PyObject *callback; + int flag; + int ret = -1; + if (!PyArg_ParseTuple(args, "Oi", &callback, &flag)) { + Py_RETURN_NONE; + } + if (!PyCallable_Check(callback)) { + PyErr_SetString(PyExc_TypeError, "callback func must can be call."); + Py_RETURN_NONE; + } + Py_XDECREF(pyCallBack); + pyCallBack = callback; + Py_INCREF(pyCallBack); + ret = CALL_ACL_API(AclDumpRegCallBack, CppToPyCallBack, flag); + return PyLong_FromLong(ret); + } + + static PyObject *AclApi_AclDumpUnregCallBack(PyObject *module) { + CALL_ACL_API(AclDumpUnregCallBack); + Py_INCREF(pyCallBack); + pyCallBack = nullptr; + Py_RETURN_NONE; + } + + static PyMethodDef ACLActuatorMethods[] = { + {"init", reinterpret_cast(AclApi_AclInit), METH_NOARGS, nullptr}, + {"rt_set_device", reinterpret_cast(AclApi_AclRtSetDevice), METH_O, nullptr}, + {"rt_create_context", reinterpret_cast(AclApi_AclRtCreateContext), METH_O, nullptr}, + {"load_from_file", reinterpret_cast(AclApi_AclMdlLoadFromFile), METH_O, nullptr}, + {"create_desc", reinterpret_cast(AclApi_AclMdlCreateDesc), METH_NOARGS, nullptr}, + {"get_desc", reinterpret_cast(AclApi_AclMdlGetDesc), METH_VARARGS, nullptr}, + {"get_num_inputs", reinterpret_cast(AclApi_AclMdlGetNumInputs), METH_VARARGS, nullptr}, + {"get_num_outputs", reinterpret_cast(AclApi_AclMdlGetNumOutputs), METH_VARARGS, nullptr}, + {"get_input_name_by_index", + reinterpret_cast(AclApi_AclMdlGetInputNameByIndex), + METH_VARARGS, + nullptr}, + {"get_input_size_by_index", + reinterpret_cast(AclApi_AclMdlGetInputSizeByIndex), + METH_VARARGS, + nullptr}, + {"get_output_size_by_index", + reinterpret_cast(AclApi_AclMdlGetOutputSizeByIndex), + METH_VARARGS, + nullptr}, + {"get_input_data_type", reinterpret_cast(AclApi_AclMdlGetInputDataType), METH_VARARGS, nullptr}, + {"get_input_dims", reinterpret_cast(AclApi_AclMdlGetInputDims), METH_VARARGS, nullptr}, + {"rt_malloc", reinterpret_cast(AclApi_AclRtMalloc), METH_VARARGS, nullptr}, + {"create_dataset", reinterpret_cast(AclApi_AclMdlCreateDataset), METH_NOARGS, nullptr}, + {"create_databuffer", reinterpret_cast(AclApi_AclCreateDataBuffer), METH_VARARGS, nullptr}, + {"add_dataset_buffer", reinterpret_cast(AclApi_AclMdlAddDatasetBuffer), METH_VARARGS, nullptr}, + {"execute", reinterpret_cast(AclApi_AclMdlExecute), METH_VARARGS, nullptr}, + {"rt_memcpy", reinterpret_cast(AclApi_AclRtMemcpy), METH_VARARGS, nullptr}, + {"get_dataset_num_buffers", + reinterpret_cast(AclApi_AclMdlGetDatasetNumBuffers), + METH_VARARGS, + nullptr}, + {"get_dataset_buffer", reinterpret_cast(AclApi_AclMdlGetDatasetBuffer), METH_VARARGS, nullptr}, + {"destroy_databuffer", reinterpret_cast(AclApi_AclDestroyDataBuffer), METH_VARARGS, nullptr}, + {"destroy_dataset", reinterpret_cast(AclApi_AclMdlDestroyDataset), METH_VARARGS, nullptr}, + {"rt_free", reinterpret_cast(AclApi_AclRtFree), METH_VARARGS, nullptr}, + {"finalize", reinterpret_cast(AclApi_AclFinalize), METH_NOARGS, nullptr}, + {"unload", reinterpret_cast(AclApi_AclMdlUnload), METH_O, nullptr}, + {"destroy_desc", reinterpret_cast(AclApi_AclMdlDestroyDesc), METH_VARARGS, nullptr}, + {"rt_destroy_context", reinterpret_cast(AclApi_AclRtDestroyContext), METH_VARARGS, nullptr}, + {"rt_reset_device", reinterpret_cast(AclApi_AclRtResetDevice), METH_O, nullptr}, + {"init_dump", reinterpret_cast(AclApi_AclInitDump), METH_NOARGS, nullptr}, + {"set_dump", reinterpret_cast(AclApi_AclSetDump), METH_O, nullptr}, + {"finalize_dump", reinterpret_cast(AclApi_AclFinalizeDump), METH_NOARGS, nullptr}, + {"dump_reg_callback", reinterpret_cast(AclApi_AclDumpRegCallBack), METH_VARARGS, nullptr}, + {"dump_unreg_callback", reinterpret_cast(AclApi_AclDumpUnregCallBack), METH_NOARGS, nullptr}, + {nullptr, nullptr, 0, nullptr}, + }; + + static struct PyModuleDef g_ACLActuatorModule = { + PyModuleDef_HEAD_INIT, + "msprobe_c.acl", // m_name + ACLInferfaceModuleDoc, // m_doc + -1, // m_size + ACLActuatorMethods, // m_methods + }; + + PyObject *GetACLActuatorModule() { + return PyModule_Create(&g_ACLActuatorModule); + } +} // namespace MSPROBE_C diff --git a/msprobe/msprobe/csrc/python/PyACLActuator.h b/msprobe/msprobe/csrc/python/PyACLActuator.h new file mode 100644 index 00000000000..a47d402123d --- /dev/null +++ b/msprobe/msprobe/csrc/python/PyACLActuator.h @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2025-2025. Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef PYACL_ACTUATOR_H +#define PYACL_ACTUATOR_H + +#include + +namespace MSPROBE_C { + PyObject *GetACLActuatorModule(); +} + +#endif diff --git a/msprobe/msprobe/csrc/python/PyInterface.cpp b/msprobe/msprobe/csrc/python/PyInterface.cpp new file mode 100644 index 00000000000..c6aa54ad7de --- /dev/null +++ b/msprobe/msprobe/csrc/python/PyInterface.cpp @@ -0,0 +1,64 @@ +/* + * Copyright (C) 2025-2025. Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "python/PyACLActuator.h" +#include "python/PyLog.h" + +#include + +namespace MSPROBE_C { + PyDoc_STRVAR(InferfaceModuleDoc, "The part of the msprobe module that is implemented in CXX."); + + static struct PyModuleDef g_InterfaceModule = { + PyModuleDef_HEAD_INIT, + "msprobe_c", // m_name + InferfaceModuleDoc, // m_doc + -1, // m_size + nullptr, // m_methods + }; +} // namespace MSPROBE_C + +PyMODINIT_FUNC PyInit_msprobe_c(void) { + PyObject *m = PyModule_Create(&MSPROBE_C::g_InterfaceModule); + if (m == nullptr) { + return nullptr; + } + + PyObject *cpyACLActuator = MSPROBE_C::GetACLActuatorModule(); + if (cpyACLActuator == nullptr) { + PyErr_SetString(PyExc_ImportError, "Failed to create submodule ACLActuatorModule."); + Py_DECREF(m); + return nullptr; + } + if (PyModule_AddObject(m, "acl", cpyACLActuator) < 0) { + PyErr_SetString(PyExc_ImportError, "Failed to bind submodule ACLActuatorModule."); + Py_DECREF(m); + return nullptr; + } + + PyObject *cpyLog = MSPROBE_C::GetLogModule(); + if (cpyLog == nullptr) { + PyErr_SetString(PyExc_ImportError, "Failed to create submodule LogModule."); + Py_DECREF(m); + return nullptr; + } + if (PyModule_AddObject(m, "log", cpyLog) < 0) { + PyErr_SetString(PyExc_ImportError, "Failed to bind submodule LogModule."); + Py_DECREF(m); + return nullptr; + } + return m; +} diff --git a/msprobe/msprobe/csrc/python/PyLog.cpp b/msprobe/msprobe/csrc/python/PyLog.cpp new file mode 100644 index 00000000000..9b7fe4b54db --- /dev/null +++ b/msprobe/msprobe/csrc/python/PyLog.cpp @@ -0,0 +1,107 @@ +/* + * Copyright (C) 2025-2025. Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "python/PyLog.h" + +#include "utils/Constant.h" +#include "utils/Log.h" + +namespace MSPROBE_C { + PyDoc_STRVAR(LogModuleDoc, "The part of the module log that is implemented in CXX."); + + void LogDebugPy(const std::string &msg) { + Utility::Log::GetInstance().DebugStream() << msg; + } + + void LogInfoPy(const std::string &msg) { + Utility::Log::GetInstance().InfoStream() << msg; + } + + void LogWarningPy(const std::string &msg) { + Utility::Log::GetInstance().WarningStream() << msg; + } + + void LogErrorPy(const std::string &msg) { + Utility::Log::GetInstance().ErrorStream() << msg; + } + + static PyObject *Print_Log(PyObject *module, PyObject *args) { + int logLevel; + const char *msg; + if (args == nullptr || PyTuple_GET_SIZE(args) != Cst::ARGS_LEN_2) { + PyErr_SetString(PyExc_TypeError, " expects 2 arguments."); + Py_RETURN_NONE; + } + if (!PyArg_ParseTuple(args, "is", &logLevel, &msg)) { + PyErr_SetString(PyExc_TypeError, " should input a integer and a string."); + Py_RETURN_NONE; + } + switch (logLevel) { + case static_cast(Utility::Log::LogLevel::DEBUG): + LogDebugPy(msg); + break; + case static_cast(Utility::Log::LogLevel::INFO): + LogInfoPy(msg); + break; + case static_cast(Utility::Log::LogLevel::WARNING): + LogWarningPy(msg); + break; + case static_cast(Utility::Log::LogLevel::ERROR): + LogErrorPy(msg); + break; + default: + break; + } + Py_RETURN_NONE; + } + + static PyObject *Set_Log_Level(PyObject *module, PyObject *arg) { + if (!PyLong_Check(arg)) { + PyErr_SetString(PyExc_TypeError, " should be a integer."); + Py_RETURN_NONE; + } + int logLevel = static_cast(PyLong_AsLong(arg)); + if (PyErr_Occurred()) { + Py_RETURN_NONE; + } + Utility::Log::GetInstance().SetLogLevel(logLevel); + Py_RETURN_NONE; + } + + static PyObject *Get_Log_Level(PyObject *module) { + int logLevel = Utility::Log::GetInstance().GetLogLevel(); + return PyLong_FromLong(logLevel); + } + + static PyMethodDef LogMethods[] = { + {"print_log", reinterpret_cast(Print_Log), METH_VARARGS, nullptr}, + {"set_log_level", reinterpret_cast(Set_Log_Level), METH_O, nullptr}, + {"get_log_level", reinterpret_cast(Get_Log_Level), METH_NOARGS, nullptr}, + {nullptr, nullptr, 0, nullptr}, + }; + + static struct PyModuleDef g_LogModule = { + PyModuleDef_HEAD_INIT, + "msprobe_c.log", // m_name + LogModuleDoc, // m_doc + -1, // m_size + LogMethods, // m_methods + }; + + PyObject *GetLogModule() { + return PyModule_Create(&g_LogModule); + } +} // namespace MSPROBE_C diff --git a/msprobe/msprobe/csrc/python/PyLog.h b/msprobe/msprobe/csrc/python/PyLog.h new file mode 100644 index 00000000000..d768e4a99b6 --- /dev/null +++ b/msprobe/msprobe/csrc/python/PyLog.h @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2025-2025. Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef PY_LOG_H +#define PY_LOG_H + +#include + +#include "utils/Log.h" + +namespace MSPROBE_C { + void LogDebugPy(const std::string &msg); + void LogInfoPy(const std::string &msg); + void LogWarningPy(const std::string &msg); + void LogErrorPy(const std::string &msg); + PyObject *GetLogModule(); +} // namespace MSPROBE_C + +#endif -- Gitee