This section describes the Profiling APIs. Three Profiling methods are provided:
Profiling AscendCL API (collecting and flushing profile data): Writes the collected profile data to a file, which is then parsed and displayed by the Profiling tool. For details, see section "Parsing Profile Data" in Profiling Instructions.
These APIs can be called repeatedly in a process, allowing for varied Profiling configurations with each call.
aclInit can be called only once per process. To modify the Profiling configuration, modify the JSON configuration file. For details, see the description of the aclInit API.
Profiling AscendCL API for Extension (extension APIs): When you need to locate the performance bottleneck of your app or the high-level framework program, call the Profiling AscendCL extension APIs during the Profiling process (between the aclprofStart and aclprofStop calls). The extension APIs together achieve the msproftx function, which is used to record the time span of specific events during app running and write data to a profile data file. You can use the Profiling tool to parse the file and export the profile data.
msproftx: msprof tool extension, which is an extension of the MindStudio system tuning tool
For details about how to parse and export profile data, see sections "Parsing Profile Data" and "Exporting Profile Data" in Profiling Instructions.
In a process, these APIs can be called for multiple times as required.
API calling: Call aclprofCreateStamp, aclprofPush, aclprofPop, aclprofRangeStart, aclprofRangeStop, aclprofDestroyStamp between the aclprofStart and aclprofStop calls. These API calls obtain the events that occur at a specific time during app running and record the event time span.
In a process, these APIs can be called for multiple times as required.
Profiling AscendCL API for Subscription (subscription to operator information): Parses the collected profile data and writes the data to pipelines. You need to read the data to the memory, and then call the related AscendCL API to obtain the profile data.
API calling: aclprofModelSubscribe, aclprofGet*, and aclprofModelUnSubscribe. The profile data of operators in the model can be obtained, including the operator name, operator type name, and operator execution time.
Add exception handling branches following the API calls. The following is a code snippet of key steps only, which is not ready to be built or run.
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 31 32 33 34 35 36 37 38 39 40 41 42 |
//1. Initialize AscendCL. //2. Allocate runtime resources, including setting the compute device and creating a context and a stream. //3. Initialize Profiling. //Set the data flush path. const char *aclProfPath = "..."; aclprofInit(aclProfPath, strlen(aclProfPath)); //4. Set Profiling configuration. uint32_t deviceIdList[1] = {0}; //Create a configuration struct. aclprofConfig *config = aclprofCreateConfig(deviceIdList, 1, ACL_AICORE_ARITHMETIC_UTILIZATION, nullptr,ACL_PROF_ACL_API | ACL_PROF_TASK_TIME); aclprofStart(config); aclprofStepInfo *stepInfo = aclprofCreateStepInfo(); int ret = aclprofGetStepTimestamp(stepInfo, ACL_STEP_START, stream_); //5. Load your model. After the model is successfully loaded, modelId that identifies the model is returned. //6. Create data of type aclmdlDataset to describe the inputs and outputs of your model. //7. Execute your model. ret = aclmdlExecute(modelId, input, output); //8. Process the model inference result. //9. Destroy the model input and output descriptions, free memory, and unload the model. int ret = aclprofGetStepTimestamp(stepInfo, ACL_STEP_END, stream_); aclprofDestroyStepInfo(stepInfo); //10. Stop Profiling and destroy the configuration and related resources. aclprofStop(config); aclprofDestroyConfig(config); aclprofFinalize(); //11. Destroy runtime allocations. //12. Deinitialize AscendCL. // ...... |
Add exception handling branches following the API calls. The following is a code snippet of key steps only, which is not ready to be built or run.
The following code in bold is used as examples of Profiling msproftx APIs:
Example 1 (aclprofMark):
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
//1. Initialize AscendCL. //2. Allocate runtime resources, including setting the compute device and creating a context and a stream. stamp = aclprofCreateStamp(); aclprofSetStampTraceMessage(stamp, "aclrtCreateStream_mark", strlen("AscendCL_Init_Mark")); aclprofMark(stamp); //Mark the stream creation event. aclprofDestroyStamp(stamp); ret = aclrtCreateStream(&stream_); //3. Initialize Profiling. //Set the data flush path. const char *aclProfPath = "..."; aclprofInit(aclProfPath, strlen(aclProfPath)); //4. Configure Profiling. uint32_t deviceIdList[1] = {0}; //Create a configuration struct. aclprofConfig *config = aclprofCreateConfig(deviceIdList, 1, ACL_AICORE_ARITHMETIC_UTILIZATION, nullptr,ACL_PROF_ACL_API | ACL_PROF_TASK_TIME); aclprofStart(config); aclprofStepInfo *stepInfo = aclprofCreateStepInfo(); int ret = aclprofGetStepTimestamp(stepInfo, ACL_STEP_START, stream_); //5. Load your model. After the model is successfully loaded, modelId that identifies the model is returned. stamp = aclprofCreateStamp(); aclprofSetStampTraceMessage(stamp, "model_load_mark", strlen("model_load_mark")); aclprofMark(stamp); //Mark the model loading event. aclprofDestroyStamp(stamp); //6. Create data of type aclmdlDataset to describe the inputs and outputs of your model. //7. Execute your model. ret = aclmdlExecute(modelId, input, output); //8. Process the model inference result. //9. Destroy the model input and output descriptions, free memory, and unload the model. int ret = aclprofGetStepTimestamp(stepInfo, ACL_STEP_END, stream_); aclprofDestroyStepInfo(stepInfo); //10. Stop Profiling and destroy the configuration and related resources. aclprofStop(config); aclprofDestroyConfig(config); aclprofFinalize(); //11. Destroy runtime allocations. //12. Deinitialize AscendCL. //...... |
Example 2 (aclprofPush/aclprofPop, applicable to single-thread scenarios):
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
//1. Initialize AscendCL. //2. Allocate runtime resources, including setting the compute device and creating a context and a stream. ret = aclrtCreateStream(&stream_); //3. Initialize Profiling. //Set the data flush path. const char *aclProfPath = "..."; aclprofInit(aclProfPath, strlen(aclProfPath)); //4. Configure Profiling. uint32_t deviceIdList[1] = {0}; //Create a configuration struct. aclprofConfig *config = aclprofCreateConfig(deviceIdList, 1, ACL_AICORE_ARITHMETIC_UTILIZATION, nullptr,ACL_PROF_ACL_API | ACL_PROF_TASK_TIME); aclprofStart(config); aclprofStepInfo *stepInfo = aclprofCreateStepInfo(); int ret = aclprofGetStepTimestamp(stepInfo, ACL_STEP_START, stream_); //5. Load your model. After the model is successfully loaded, modelId that identifies the model is returned. //6. Create data of type aclmdlDataset to describe the inputs and outputs of your model. //7. Execute the model (only in a single thread). stamp = aclprofCreateStamp(); aclprofSetStampTraceMessage(stamp, "aclmdlExecute_duration", strlen("aclmdlExecute_duration")); aclprofPush(stamp); ret = aclmdlExecute(modelId, input, output); aclprofPop(stamp); aclprofDestroyStamp(stamp); //8. Process the model inference result. //9. Destroy the model input and output descriptions, free memory, and unload the model. int ret = aclprofGetStepTimestamp(stepInfo, ACL_STEP_END, stream_); aclprofDestroyStepInfo(stepInfo); //10. Stop Profiling and destroy the configuration and related resources. aclprofStop(config); aclprofDestroyConfig(config); aclprofFinalize(); //11. Destroy runtime allocations. //12. Deinitialize AscendCL. //...... |
Example 3 (aclprofRangeStart/aclprofRangeStop, applicable to single-thread or cross-thread scenarios):
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
//1. Initialize AscendCL. //2. Allocate runtime resources, including setting the compute device and creating a context and a stream. ret = aclrtCreateStream(&stream_); //3. Initialize Profiling. //Set the data flush path. const char *aclProfPath = "..."; aclprofInit(aclProfPath, strlen(aclProfPath)); //4. Configure Profiling. uint32_t deviceIdList[1] = {0}; //Create a configuration struct. aclprofConfig *config = aclprofCreateConfig(deviceIdList, 1, ACL_AICORE_ARITHMETIC_UTILIZATION, nullptr,ACL_PROF_ACL_API | ACL_PROF_TASK_TIME); aclprofStart(config); aclprofStepInfo *stepInfo = aclprofCreateStepInfo(); int ret = aclprofGetStepTimestamp(stepInfo, ACL_STEP_START, stream_); //5. Load your model. After the model is successfully loaded, modelId that identifies the model is returned. //6. Create data of type aclmdlDataset to describe the inputs and outputs of your model. //7. Execute the model (across threads). stamp = aclprofCreateStamp(); aclprofSetStampTraceMessage(stamp, "aclmdlExecute_duration", strlen("aclmdlExecute_duration")); aclprofRangeStart(stamp, &rangeId); ret = aclmdlExecute(modelId, input, output); aclprofRangeStop(rangeId); aclprofDestroyStamp(stamp); //8. Process the model inference result. //9. Destroy the model input and output descriptions, free memory, and unload the model. int ret = aclprofGetStepTimestamp(stepInfo, ACL_STEP_END, stream_); aclprofDestroyStepInfo(stepInfo); //10. Stop Profiling and destroy the configuration and related resources. aclprofStop(config); aclprofDestroyConfig(config); aclprofFinalize(); //11. Destroy runtime allocations. //12. Deinitialize AscendCL. //...... |
Following the API calls, add exception handling branches and specify log printing of error and information levels. The following is a code snippet of key steps only, which is not ready to be built or run.
This section describes the code logic for Profiling data collection. For details about how to initialize and deinitialize AscendCL, see AscendCL Initialization and Deinitialization. For details about how to allocate and deallocate runtime resources, see Runtime Resource Allocation and Deallocation. For details about how to load a model, prepare the input/output data of model inference, and execute and unload the model, see Model Inference.
//1. Initialize AscendCL.
//2. Allocate runtime resources, including setting the compute device and creating a context and a stream.
//3. Load your model. After the model is successfully loaded, modelId that identifies the model is returned.
//4. Create data of type aclmdlDataset to describe the inputs and outputs of your model.
//5. Create a pipeline (on UNIX, you need the unistd.h header file of the C++ standard library) to read and write the model subscription data.
int subFd[2];
//The read pipeline pointer points to subFd[0], and the write pipeline pointer points to subFd[1].
pipe(subFd);
//6. Create a model subscription configuration and subscribe to the model.
aclprofSubscribeConfig *config = aclprofCreateSubscribeConfig(1, ACL_AICORE_NONE, &subFd[1]);
//Pass modelId of the model to subscribe.
aclprofModelSubscribe(modelId, config);
//7. Enable the pipeline to read subscription data.
//7.1 Customize a function to read subscription data from the user memory.
void getModelInfo(void *data, uint32_t len) {
uint32_t opNumber = 0;
uint32_t dataLen = 0;
//Read the number of operators by using the AscendCL API call.
aclprofGetOpNum(data, len, &opNumber);
//Iterate over the operator information in the user memory.
for (int32_t i = 0; i < opNumber; i++){
//Obtain the modelId of the operator.
uint32_t modelId = aclprofGetModelId(data,len, i);
//Obtain the length of the operator type name.
size_t opTypeLen = 0;
aclprofGetOpTypeLen(data, len, i, &opTypeLen);
//Obtain the operator type name.
char opType[opTypeLen];
aclprofGetOpType(data, len, i, opType, opTypeLen);
//Obtain the length of the operator name.
size_t opNameLen = 0;
aclprofGetOpNameLen(data, len, i, &opNameLen);
//Obtain the operator name.
char opName[opNameLen];
aclprofGetOpName(data, len, i, opName, opNameLen);
//Obtain the execution start time of the operator.
uint64_t opStart = aclprofGetOpStart(data, len, i);
//Obtain the execution end time of the operator.
uint64_t opEnd = aclprofGetOpEnd(data, len, i);
uint64_t opDuration = aclprofGetOpDuration(data, len, i);
}
}
//7.2 Customize a function to read data from the pipeline to the user memory.
void *profDataRead(void *fd) {
//Set the number of operators read from the pipeline each time.
uint64_t N = 10;
//Obtain the operator information buffer size (in bytes) per operator.
uint64_t bufferSize = 0;
aclprofGetOpDescSize(&bufferSize);
//Calculate the total operator information buffer size and allocate buffer accordingly.
uint64_t readbufLen = bufferSize * N;
char *readbuf = new char[readbufLen];
//Read data from the pipeline to the allocated memory. The actual size of the read data (dataLen) may be less than bufferSize * N. If there is no data in the pipeline, the process is blocked until data is read.
auto dataLen = read(*(int*)fd, readbuf, readbufLen);
//The data is successfully read to the readbuf.
while (dataLen > 0) {
//Call the function implemented in 5.1 to parse data in the memory.
getModelInfo(readbuf, dataLen);
memset(readbuf, 0, bufferSize);
dataLen = read(*(int*)fd, readbuf, readbufLen);
}
delete []readbuf;
}
//8. Start the thread to read and parse the pipeline data.
pthread_t subTid = 0;
pthread_create(&subTid, NULL, profDataRead, &subFd[0]);
//9. Execute your model.
ret = aclmdlExecute(modelId, input, output);
//10. Process the model inference result.
//11. Destroy the model input and output descriptions, free memory, and unload the model.
//12. Unsubscribe from the model and destroy the subscription-related resources.
aclprofModelUnSubscribe(modelId);
pthread_join(subTid, NULL);
//Close the read pipeline pointer.
close(subFd[0]);
//Destroy the config pointer.
aclprofDestroySubscribeConfig(config);
//13. Destroy runtime allocations.
//14. Deinitialize AscendCL.
// ......