0_simple_label
Description
This sample demonstrates basic CANN Runtime Label management usage flow, including creating Label, assembling LabelList, setting Label on persistent Stream bound to model runtime instance, and executing Label switch by index in Device memory. After running, you can see logs for model runtime instance build, execution, and resource release completion.
Product Support
This sample has the following support status on the following products:
| Product | Supported |
|---|---|
| Ascend 950PR/Ascend 950DT | Yes |
| Atlas A3 training series products/Atlas A3 inference series products | Yes |
| Atlas A2 training series products/Atlas A2 inference series products | Yes |
Build and Run
For environment installation, environment variable configuration, and general running instructions, see README in the example directory. You can execute source <cann_path>/set_env.sh beforehand to set CANN environment variables; if not set beforehand, run.sh automatically attempts to detect ASCEND_INSTALL_PATH, ASCEND_HOME_PATH, $HOME/Ascend/cann, /usr/local/Ascend/cann, and /opt/Ascend/cann.
After entering current sample directory, run the following command to execute the sample:
bash run.sh
CANN RUNTIME API
Key features and interfaces in this sample:
- Initialization
- Call
aclInitinterface to initialize configuration. - Call
aclFinalizeinterface to deinitialize.
- Call
- Device and Context Management
- Call
aclrtSetDeviceinterface to specify Device for computation. - Call
aclrtCreateContextinterface to create Context. - Call
aclrtDestroyContextinterface to destroy Context. - Call
aclrtResetDeviceForceinterface to forcibly reset current computation Device and reclaim Device resources.
- Call
- Stream Management
- Call
aclrtCreateStreamWithConfiginterface to create persistent Stream. - Call
aclrtCreateStreaminterface to create Stream for executing model runtime instance. - Call
aclrtSynchronizeStreaminterface to block waiting for Stream task execution completion. - Call
aclrtDestroyStreaminterface to destroy Stream.
- Call
- Memory Management
- Call
aclrtMallocinterface to allocate Device memory. - Call
aclrtFreeinterface to release Device memory.
- Call
- Data Transfer
- Call
aclrtMemcpyinterface to write Host-side branch index to Device memory.
- Call
- Model Runtime Instance Management
- Call
aclmdlRIBuildBeginandaclmdlRIBuildEndinterfaces to start and end model runtime instance build. - Call
aclmdlRIBindStreamandaclmdlRIUnbindStreaminterfaces to bind and unbind persistent Stream. - Call
aclmdlRIEndTaskinterface to mark task dispatch end on bound Stream. - Call
aclmdlRIExecuteAsyncinterface to asynchronously execute model runtime instance. - Call
aclmdlRIDestroyinterface to destroy model runtime instance.
- Call
- Label Management
- Call
aclrtCreateLabelandaclrtDestroyLabelinterfaces to create and release Label. - Call
aclrtCreateLabelListandaclrtDestroyLabelListinterfaces to assemble and release LabelList. - Call
aclrtSetLabelinterface to set Label on Stream bound to model runtime instance. - Call
aclrtSwitchLabelByIndexinterface to execute Label switch based on branch index in Device memory.
- Call
Sample Output
[INFO] ACL initialized.
[INFO] Device 0 selected.
[INFO] Context created on device 0.
[INFO] Persistent label stream created.
[INFO] Execute stream created.
[INFO] Model runtime instance build started.
[INFO] Persistent label stream bound to the model runtime instance.
[INFO] Allocated device memory for branch index.
[INFO] Copied branch index 1 from host to device.
[INFO] Created label 0.
[INFO] Created label 1.
[INFO] Created label list with 2 labels.
[INFO] Submitted switch-label task with branch index 1.
[INFO] Set label 0 on the persistent stream.
[INFO] Set label 1 on the persistent stream.
[INFO] Model runtime instance build finished.
[INFO] Switch label executed successfully with branch index 1.
[INFO] Run the simple_label sample successfully.