InitValue

Function Usage

Sets the initial value of the operator output. After setting, it will clear the GM space of the operator output before implementation, or insert a memset operator to set the initial value.

The InitValue and SetNeedAtomic APIs are used together. Set the value to true for the SetNeedAtomic API.

Prototype

  • Clear the GM space corresponding to the output parameter before operator execution.
    OpParamDef &InitValue(uint64_t value);
  • Specify the type and value of the initial value. If this API is called, the memset operator of the corresponding type and value is inserted into the GM space before operator execution.
    OpParamDef &InitValue(const ScalarVar &value);
  • Specify the list of the types and values of the initial value, which corresponds to the data type and format combination of the output parameter. The memset operator of the corresponding type and value is inserted into the GM space before operator execution.
    OpParamDef &InitValue(const std::vector<ScalarVar> &value);

Parameters

Parameter

Input/Output

Description

value

Input

  • Parameter of the uint64_t type

    Only 0 is supported. If this API is called for an output parameter, the GM space corresponding to the output parameter will be cleared before operator execution.

  • Parameter of the ScalarVar type

    ScalarVar is used to specify the type ScalarType and value ScalarNum of the initial value. The definition is as follows:

    enum class ScalarType : uint32_t {
      UINT64 = 0,
      INT64 = 1,
      UINT32 = 2,
      INT32 = 3,
      UINT16 = 4,
      INT16 = 5,
      UINT8 = 6,
      INT8 = 7,
      FLOAT32 = 8,
      FLOAT16 = 9,
      INVALID_DTYPE = static_cast<uint32_t>(-1),
    };
    union ScalarNum {
      uint64_t value_u64;
      int64_t value_i64;
      float value_f32;
      ScalarNum() : value_u64(0) {}
      explicit ScalarNum(uint64_t value) : value_u64(value) {}
      explicit ScalarNum(int64_t value) : value_i64(value) {}
      explicit ScalarNum(float value) : value_f32(value) {}
    };
    struct ScalarVar {
      ScalarType scalar_type;
      ScalarNum scalar_num;
      ScalarVar();
      ScalarVar(ScalarType type, uint64_t num);
      ScalarVar(ScalarType type, int64_t num);
      ScalarVar(ScalarType type, int num);
      ScalarVar(ScalarType type, unsigned int num);
      ScalarVar(ScalarType type, float num);
      ScalarVar(ScalarType type, double num);
      bool operator==(const ScalarVar& other) const;
    };

    Currently, ScalarType supports only UINT64/INT64/UINT32/INT32/UINT16/INT16/UINT8/INT8/FLOAT32/FLOAT16.

    ScalarNum supports uint64_t/int64_t/float.

    For convenience, ScalarVar also supports immediate initialization. The following is an example:

    InitValue({ScalarType::INT16, 1});
  • const std::vector<ScalarVar> &value type

    Specifies the list of the types and values of the initial value, which corresponds to the data type and format combination of the output parameter.

Returns

OpParamDef operator definition. For details, see OpParamDef.

Constraints

  • InitValue and SetNeedAtomic must be used together. Otherwise, the initialization does not take effect.
  • For the OpParamDef &InitValue(uint64_t value) API, the supported data types of the operator output parameter are UINT64/INT64/UINT32/INT32/UINT16/INT16/UINT8/INT8/FLOAT32/FLOAT16. Other data types may result in undefined behavior.
  • For the OpParamDef &InitValue(const std::vector<ScalarVar> &value) API, the size of value must be the same as that of the DataType or DataTypeList API parameter configured in the output parameter. In addition, the data types and values for the same data type must be the same. Otherwise, an error is reported.
  • For the same output parameter, only one API can be called to set the initial value. Calling multiple InitValue APIs may result in undefined behavior. If the same API is called for multiple times, the initial value set in the last call is used.

Example

// OpParamDef &InitValue(uint64_t value) example
this->Output("z")
     .ParamType(REQUIRED)
     .DataType({ge::DT_FLOAT16, ge::DT_FLOAT, ge::DT_INT32})
     .FormatList({ ge::FORMAT_ND})
     .InitValue(0);

// OpParamDef &InitValue(const ScalarVar &value) example
this->Output("z")
     .ParamType(REQUIRED)
     .DataType({ge::DT_FLOAT16, ge::DT_FLOAT, ge::DT_INT32})
     .FormatList({ ge::FORMAT_ND})
     .InitValue({ScalarType::INT16, 1});

// OpParamDef &InitValue(const std::vector<ScalarVar> &value) example
this->Output("z")
     .ParamType(REQUIRED)
     .DataType({ge::DT_FLOAT16, ge::DT_FLOAT, ge::DT_INT32})
     .FormatList({ ge::FORMAT_ND})
     .InitValue({{ScalarType::INT16, 1}, {ScalarType::FLOAT32, 3.2}, {ScalarType::INT64, 7}});

this->Output("z")
     .ParamType(REQUIRED)
     .DataType({ge::DT_INT32, ge::DT_FLOAT, ge::DT_INT32})  // The first and third data types are the same.
     .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_NHWC})
     .InitValue({{ScalarType::INT16, 1}, {ScalarType::FLOAT32, 3.2}, {ScalarType::INT16, 1}}); // The data type and value corresponding to InitValue must be the same.