| Files | Last commit | Last update |
|---|---|---|
3_cond_model
Description
This sample demonstrates aclGraph conditional operation (IF/WHILE/SWITCH) graph capture and execution, covering 3 typical scenarios:
- IF conditional double branch validation (true branch alpha=2.0, false branch alpha=0.5)
- WHILE single iteration loop (loop body executes once then exits)
- SWITCH multi-branch selection (3 cases each with different alpha)
Core conclusion: aclGraph conditional operations use aclmdlRICondHandle + aclmdlRIAddCondTask to implement IF/WHILE/SWITCH branch capture, use aclmdlRICondHandleGetCondPtr to get device-side condition pointer, and execution dynamically determines branch direction by condition value.
Note: This sample requires CANN version support for aclGraph conditional operation APIs (aclmdlRICondHandleCreate, aclmdlRIAddCondTask, aclmdlRICaptureToModelRIBegin, and so on). Please confirm CANN version includes these interfaces before running.
Product Support
This sample has the following support status on the following products:
| Product | Supported |
|---|---|
| Ascend 950PR/Ascend 950DT | No |
| 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
- Download sample code to environment with CANN software installed, switch to sample directory.
cd ${git_clone_path}/example/2_advanced_features/model_ri/3_cond_model
- Set environment variables.
# Replace ${install_root} with CANN installation root directory, default installation at /usr/local/Ascend
source ${install_root}/cann/set_env.sh
export ASCEND_INSTALL_PATH=${install_root}/cann
# Set SOC_VERSION and ASCENDC_CMAKE_DIR
source ${git_clone_path}/example/set_sample_env.sh
- 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 Management
- Call
aclrtSetDeviceinterface to specify Device for computation. - Call
aclrtResetDeviceForceinterface to reset current computation Device and reclaim Device resources.
- Call
- Context Management
- Call
aclrtCreateContextinterface to create Context. - Call
aclrtDestroyContextinterface to destroy Context.
- Call
- Stream Management
- Call
aclrtCreateStreaminterface to create Stream. - Call
aclrtDestroyStreaminterface to destroy Stream. - Call
aclrtSynchronizeStreaminterface to block waiting for Stream task completion.
- Call
- Memory Management
- Call
aclrtMallocinterface to allocate Device memory. - Call
aclrtFreeinterface to release Device memory.
- Call
- Data Transfer
- Call
aclrtMemcpyinterface to implement data transfer by memory copy. - Call
aclrtMemcpyAsyncinterface to perform asynchronous memory copy.
- Call
- aclGraph Conditional Operations
- Call
aclmdlRICaptureBegininterface to start graph capture. - Call
aclmdlRICaptureEndinterface to end graph capture and get modelRI handle. - Call
aclmdlRICaptureGetInfointerface to get graph capture status and modelRI. - Call
aclmdlRICondHandleCreateinterface to create condition handle. - Call
aclmdlRICondHandleGetCondPtrinterface to get condition pointer. - Call
aclmdlRIAddCondTaskinterface to register condition task (IF/WHILE/SWITCH). - Call
aclmdlRICaptureToModelRIBegininterface to start sub-model capture. - Call
aclmdlRIExecuteAsyncinterface to asynchronously execute model runtime instance. - Call
aclmdlRIDestroyinterface to destroy model runtime instance.
- Call
- aclnn Operators
- Call
aclnnAddGetWorkspaceSizeandaclnnAddinterfaces to execute addition operator calculation.
- Call
Sample Output
[INFO] ========== IF condition ==========
[INFO] IF true branch result:
[INFO] The vector data is: 1.2500 2.5000 3.7500 5.0000 6.2500 7.5000 8.7500 10.0000
[INFO] IF false branch result:
[INFO] The vector data is: 1.2500 2.5000 3.7500 5.0000 6.2500 7.5000 8.7500 10.0000
[INFO] IF condition PASSED
[INFO] ========== WHILE condition ==========
[INFO] WHILE single iteration result:
[INFO] The vector data is: 1.2500 2.5000 3.7500 5.0000 6.2500 7.5000 8.7500 10.0000
[INFO] WHILE condition PASSED
[INFO] ========== SWITCH condition ==========
[INFO] SWITCH case 0 result:
[INFO] The vector data is: 1.5000 3.0000 4.5000 6.0000 7.5000 9.0000 10.5000 12.0000
...
[INFO] SWITCH condition PASSED
[INFO] ========== All tests PASSED ==========
Known Issues
None