Shape Range of Dynamic Input

The section describes how to set the shape range of a dynamic input at model build time.

Overview

The section instructs you how to develop a model supporting dynamic inputs by specifying the shape range of the model input at build time.

Applicability

Atlas Training Series Product

Procedure

  1. In Data operator definition, set the dynamic dimension in the shape to -1.
    1
    2
    3
    4
    5
        auto shape_data = vector<int64_t>({ -1,3,5,-1 });
        TensorDesc desc_data(ge::Shape(shape_data), FORMAT_ND, DT_FLOAT);
        auto data = op::Data("data");
        data.update_input_desc_data(desc_data);
        data.update_output_desc_out(desc_data);
    
  2. At model build time, pass INPUT_SHAPE to options of the aclgrphBuildModel API to specify the shape range of the model input.
    1
    2
    3
    4
    5
    6
    void PrepareOptions(std::map<AscendString, AscendString>& options) {
        options.insert({
            {ge::ir_option::INPUT_FORMAT, "NCHW"},
            {ge::ir_option::INPUT_SHAPE, "8~20,3,5,-1"} 
        });
    }
    

    The format requirements for setting the shape range of the INPUT_SHAPE parameter:

    • To set the shape range based on node names, the format is "input_name1:n1,c1,h1,w1;input_name2:n2,c2,h2,w2", for example, "input_name1:8~20,3,5,-1;input_name2:5,3~9,10,-1". Enclose the specified nodes in double quotation marks (""), and separate them by semicolons (;). input_name must be the node name in the network model before model conversion. As a best practice, you should set the parameter based on node names.
    • To set the shape range based on node indexes, the format is "n1,c1,h1,w1;n2,c2,h2,w2", for example, "8~20,3,5,-1;5,3~9,10,-1". If the node name is not specified, the nodes are sorted by the index and separated by semicolons (;). When the shape range is specified based on the index, the index attribute must be set sequentially from 0 for data nodes.
    • The size of a static dimension is specified by a determinant value. The size range of a dynamic dimension is specified by using a tilde (~). A dynamic dimension without size range specified is denoted by –1.

Precautions

If this option is used to set the dynamic input shape range during model build, you need to call the aclmdlSetDatasetTensorDesc API to set the actual input tensor description (input shape range) before the model executes the aclmdlExecute API during model inference using an application project. After model execution, call the aclmdlGetDatasetTensorDesc API to obtain the tensor description information dynamically output by the model. Then call the APIs under aclTensorDesc to obtain the memory size occupied by the output tensor data, tensor format, and tensor dimensions.