Function: set_exception_info_callback

Applicability

Product

Supported

Atlas 350 Accelerator Card

Atlas A3 training product / Atlas A3 inference product

Atlas A2 training product / Atlas A2 inference product

Atlas training product

Atlas inference product

Atlas 200I/500 A2 inference product

Description

Sets the exception callback function.

Prototype

  • C Prototype
    1
    aclError aclrtSetExceptionInfoCallback(aclrtExceptionInfoCallback callback)
    
  • Python Function
    1
    ret = acl.rt.set_exception_info_callback(fn)
    

Parameters

Parameter

Description

callback

Function, callback function on the Python side. The format is as follows:

def exception_callback(exception_info)
"""
:exception_info: pointer address of aclrtExceptionInfo.
:return:
"""

Return Value

Return Value

Description

ret

Int, error code. 0 on success; else, failure.

Restrictions

  • Callback functions involve shared resources (such as locks). Exercise caution when using callback functions. Do not call APIs for resource application & release, stream synchronization, device synchronization, task delivery, and task termination in callback functions. Otherwise, errors or deadlocks may occur.
  • Before executing an asynchronous task, you need to set an exception callback function. When an exception occurs during task execution on the device, the system transfers a pointer address of aclrtExceptionInfo that contains the task ID, stream ID, thread ID, device ID, and error code to the exception callback function, execute the callback function. You can call the acl.rt.get_task_id_from_exception_info, acl.rt.get_stream_id_from_exception_info, acl.rt.get_thread_id_from_exception_info, acl.rt.get_device_id_from_exception_info, and acl.rt.get_error_code_from_exception_info APIs to obtain the task ID, stream ID, thread ID, device ID, and error code to locate the fault.

    Application scenario Example: Before calling the acl.op.execute_v2 API, call the acl.rt.set_exception_info_callback API to set the exception callback function. When an exception occurs during operator execution on the device, the system passes a pointer address of aclrtExceptionInfo that contains the task ID, stream ID, thread ID, device ID, and error code to the exception callback function, execute the callback function.

  • If the exception callback function is set repeatedly, the most recent setting applies.
  • To clear the callback function, pass None or no parameter to the acl.rt.set_exception_info_callback call.

API Call Sequence

Application scenario: For example, if an AI Core error is reported during network inference (dynamic shape scenarios unsupported), you can call this API to obtain the description of the error operator and then perform further troubleshooting.

The recommended API call sequence is as follows:
  1. Define and implement the exception callback function fn (of the aclrtExceptionInfoCallback type). For details about the callback function prototype, see acl.rt.set_exception_info_callback.

    The key steps for implementing the callback function are as follows:

    1. Call acl.rt.get_device_id_from_exception_info, acl.rt.get_stream_id_from_exception_info, and acl.rt.get_task_id_from_exception_info in the exception callback function fn to obtain the device ID, stream ID, and task ID.
    2. Call acl.mdl.create_and_get_op_desc in the exception callback function fn to obtain the operator description.
    3. Call acl.get_tensor_desc_by_index in the exception callback function fn to obtain the input/output tensor description of the specified operator.
    4. In fn, obtain the tensor description for further analysis.

      For example, call acl.get_tensor_desc_address to obtain the memory address of the tensor data (you can obtain the tensor data from the memory address), call acl.get_tensor_desc_type to obtain the data type in the tensor description, call acl.get_tensor_desc_format to obtain the format in the tensor description, call acl.get_tensor_desc_num_dims to obtain the number of shape dimensions in the tensor description, and call acl.get_tensor_desc_dim_v2 to obtain the size of a specified dimension in the shape.

  2. Call acl.rt.set_exception_info_callback to set the exception callback function.
  3. Run model inference.

    If an AI Core error is reported, fn is triggered to obtain the operator information for further analysis.

Sample Code

Add exception handling branches following the API calls. The sample code of key steps is as follows, which is for reference only. Do not directly copy and run the code.

For details about how to allocate and release runtime resources, see Runtime Resource Allocation and Deallocation. For details about the API call sequence for model loading, see API Call Sequence. For details about the API call sequence for model inference and API call sequence for preparing the input and output data for model inference, see Preparing Input/Output Data Structure for Model Execution.
 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
51
52
import acl
import numpy as np
# ......

# 1. Allocate runtime resources.
# ......

# 2. Load a model. After the model is successfully loaded, model_id that identifies the model is returned.
# ......

# 3. Create data of type aclmdlDataset to describe the inputs and outputs of the model.
# ......

# 4. Implement the exception callback function.
def exception_callback(info):
    stream_id = acl.rt.get_stream_id_from_exception_info(info)
    device_id = acl.rt.get_device_id_from_exception_info(info)
    task_id = acl.rt.get_task_id_from_exception_info(info)
    # You can write the obtained operator information to a file, or start another thread to listen to the exception callback. When an error occurs, the thread handling function is triggered, and the operator information is printed to the screen.
    op_name, input_desc, num_inputs, output_desc, num_outputs, ret = \
        acl.mdl.create_and_get_op_desc(device_id, stream_id, task_id, 256)

    # You can call related tensor APIs provided by acl to obtain the operator information as required.
    for i in range(num_inputs):
        desc = acl.get_tensor_desc_by_index(input_desc, i)
        address = acl.get_tensor_desc_address(desc)
        num_dims = acl.get_tensor_desc_num_dims(desc)
        dim_0, ret = acl.get_tensor_desc_dim_v2(desc, 0)

    for i in range(num_outputs):
        desc = acl.get_tensor_desc_by_index(output_desc, i)
        address = acl.get_tensor_desc_address(desc)
        num_dims = acl.get_tensor_desc_num_dims(desc)
        dim_0, ret = acl.get_tensor_desc_dim_v2(desc, 0)

    acl.destroy_tensor_desc(input_desc)
    acl.destroy_tensor_desc(output_desc)

# 5. Set the exception callback function.
ret = acl.rt.set_exception_info_callback(exception_callback)

# 6. Execute the model.
ret = acl.mdl.execute(model_id, input, output)

# 7. Process the model inference result.
# ......

# 8. Destroy allocations such as the model inputs and outputs, free memory, and unload the model.
# ......

# 9. Deallocate runtime resources.
# ......