Single-Operator with Dynamic Shape (Operator Selector Not Registered)

Principles

For operators that support dynamic shape:

Sample Code

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 use.

 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
import acl
# ......

# Determine the allocation of memory for storing the input tensor data of the operator. If the app runs on the host, allocate host memory, as is the case here.
# If the app runs on the device, allocate device memory.
ret = acl.op.infer_shape(op_type, self.in_desc_list, self.in_list, num_outputs, self.out_desc_list, self.attr)

tensor_dims = []
# Perform a for loop for each output to infer or estimate the shape value.
for i in range(len(infer_desc_list)):
    dim_nums = acl.get_tensor_desc_num_dims(infer_desc_list[i])
    dim_size = []
    for j in range(dim_nums):
        dim, ret = acl.get_tensor_desc_dim_v2(infer_desc_list[i], j)
        # The dimension size is dynamic in the dynamic-shape scenario.
        if dim == -1:
            # Obtain the shape range and use the maximum shape value to construct the output tensor_desc, as an argument of the acl.op.execute_v2 call.
            dim_range, ret = acl.get_tensor_desc_dim_range(infer_desc_list[i], j, 2)
            dim = dim_range[1]
        dim_size.append(dim)
    tensor_dims.append(dim_size)

# The preceding provides the operator output shape and the output tensor_desc (that is, the value of self.out_desc_list) is constructed based on dims in tensor_dims, as the input parameters of the acl.op.execute_v2 call.
ret = acl.op.execute_v2(op_type, self.in_desc_list, self.in_list, self.out_desc_list, self.out_list, self.attr, self.stream)

out_tensor_dims = []
# In the preceding scenario where the estimated shape value and the maximum shape in the shape range are used, after the operator is executed, the following call needs to be added to obtain the accurate shape:
for i in range(len(self.out_desc_list)):
    dim_nums = acl.get_tensor_desc_num_dims(self.out_desc_list[i])
    dim_size = []
    for j in range(dim_nums):
        dim, ret = acl.get_tensor_desc_dim_v2(self.out_desc_list[i], j)
dim_size.append(dim)
    out_tensor_dims.append(dim_size))

# ......