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
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.
Parent topic: OpParamDef