InitValue
Function
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.
InitValue is used together with the SetNeedAtomic API. The API must be set to true.
Prototype
- Clear the GM space corresponding to the output parameter before operator execution.
1OpParamDef &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.
1OpParamDef &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.
1OpParamDef &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 following product models:
Atlas A3 training products /Atlas A3 inference products Atlas A2 training products /Atlas A2 inference products Atlas 200I/500 A2 inference products Atlas inference products Atlas training products When the memset operator is inserted, the initial value can be set to any value only in the graph input scenario. In the single-operator API execution scenario, only clearing is supported.
- 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.
- The custom operator project generated based on the CANN package of an earlier version (which does not support the InitValue feature) is incompatible with the InitValue API. When using a custom operator project generated based on the CANN package of a non-current version, pay special attention to compatibility issues. You can check whether the current project supports this feature by checking whether the output_init_value field exists in the cmake/util/ascendc_impl_build.py file in the custom operator project. If the field does not exist, you need to regenerate the custom operator project to enable the InitValue feature.
Example
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 |
// 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. |