--precision_mode
Applicable Products
Product |
Supported (√/x) |
|---|---|
√ |
|
√ |
|
√ |
|
√ |
|
√ |
Description
Sets the precision mode of a model.
See Also
- When this option is set to allow_mix_precision, if you wish to adjust the precision based on the built-in tiling policy, specify which operators allow precision reduction and which operators do not. For details, see --modify_mixlist.
- In the inference scenario, --precision_mode can be used to set the global precision mode of a network model, but it may result in performance or accuracy problems on particular operators. Therefore, you can use --keep_dtype to keep the computation precision of these operators unchanged during the compilation of the original network model. However, --keep_dtype does not take effect when --precision_mode is set to must_keep_origin_dtype.
Figure 1 shows the associated options.
To set the operator precision mode:
- Obtain the --op_precision_mode option and check whether the .ini configuration file exists. If the file exists, parse the file and read the operator precision mode. Otherwise, an error is reported.
- If the --op_precision_mode option does not exist, read --op_select_implmode.
- Check whether this option is set to high_xxx_for_all. If yes, parse the high_xxx_for_all.ini file and read the operator precision mode.
- If the high_xxx option is configured, check whether --op_select_implmode is configured. If yes, read the operator precision mode specified by this option. If no, parse the high_xxx.ini file and read the operator precision mode.
Arguments
Arguments:
- force_fp32/cube_fp16in_fp32out:force_fp32 and cube_fp16in_fp32out have the same effect. This option indicates that the system selects different processing modes based on the operator type when the operator in the AI Core supports both the float32 and float16 data types. cube_fp16in_fp32out is newly added to the new version. For cube operators, this option has clearer semantics.
- For cube operators, the system processes the computation based on the operator implementation.
- The preferred input data type is float16 and the output data type is float32.
- If the float16 input data and float32 output data types are not supported, set both the input and output data types to float32.
- If the float32 input and output data types are not supported, set both the input and output data types to float16.
- If the float16 input and output data types are not supported, an error is reported.
- For vector compute operators, the operator precision in the original graph is float16 or bfloat16, this option forces the operator to use float32.
This option takes no effect if the original graph contains operators whose implementation on the AI Core does not support float32, for example, an operator that supports only float16. In this case, the supported float16 is used. If the operator implementation on the AI Core does not support float32 and the blocklist mode is enabled (by setting precision_reduce to false), the float32 AI CPU operator is used. If the AI CPU operator does not support float32 either, an error is reported.
- For cube operators, the system processes the computation based on the operator implementation.
- force_fp16 (default):
Forces operators in the original model to use float16, regardless of whether their original computation precision is float16, bfloat16, or float32.
- allow_fp32_to_fp16:
- For matrix operators:
- If the operator computation precision in the original graph is float32, the precision is preferentially reduced to float16. If the operator does not have a float16 implementation on the AI Core, float32 is used. If the AI Core also does not support float32 for this operator, the AI CPU operator is used for computation. If the AI CPU operator is also unsupported, an error is reported.
- If the operator computation precision in the original graph is bfloat16, the original precision is used preferentially. If the operator does not have a bfloat16 implementation on the AI Core, float32 is used. If the AI Core also does not support float32 for this operator, the precision is reduced to float16. If the AI Core also does not support float16 for this operator, the AI CPU operator is used for computation. If the AI CPU operator is also unsupported, an error is reported.
- For vector operators, the original precision is used preferentially.
- If the operator computation precision in the original graph is float32, the original precision is used preferentially. If the operator does not have a float32 implementation on the AI Core, the precision is reduced to float16. If the AI Core also does not support float16 for this operator, the AI CPU operator is used for computation. If the AI CPU operator is also unsupported, an error is reported.
- If the operator computation precision in the original graph is bfloat16, the original precision is used preferentially. If the operator does not have a bfloat16 implementation on the AI Core, float32 is used. If the AI Core also does not support float32 for this operator, the precision is reduced to float16. If the AI Core also does not support float16 for this operator, the AI CPU operator is used for computation. If the AI CPU operator is also unsupported, an error is reported.
- For matrix operators:
- must_keep_origin_dtype:
Retain the original precision.
- If the precision of an operator in the original graph is float16, and the implementation of the operator in the AI Core does not support float16 but supports only float32 and bfloat16, the system automatically uses high-precision float32.
- If the precision of an operator in the original graph is float16, and the implementation of the operator in the AI Core does not support float16 but supports only bfloat16, the AI CPU operator of float16 is used. If the AI CPU operator is not supported, an error is reported.
- If the precision of an operator in the original graph is float32, and the implementation of the operator in the AI Core does not support float32 but supports only float16, the AI CPU operator of float32 is used. If the AI CPU operator is not supported, an error is reported.
- allow_mix_precision/allow_mix_precision_fp16:
allow_mix_precision has the same effect as that of allow_mix_precision_fp16, indicating that mixed precision of float16, bfloat16, and float32 is used for neural network processing. allow_mix_precision_fp16 is newly added to the new version, which has clearer semantics for easy understanding.
For float32 and bfloat16 operators in the original model, float16 is automatically used for certain float32 and bfloat16 operators based on the built-in tuning policy, which improves system performance and reduces memory usage with minimal accuracy loss.
If this mode is configured, you can view the value of precision_reduce in the built-in tuning policy file ${INSTALL_DIR}/opp/built-in/op_impl/ai_core/tbe/config/xxx/aic-xxx-ops-info-*.json.
- If it is set to true, the operator is on the trustlist and its precision will be reduced from float32 or bfloat16 to float16.
- If it is set to false, the operator is on the blocklist and its precision will not be reduced from float32 or bfloat16 to float16. Such operators will continue to use their original precision (float32 or bfloat16).
- If an operator in the network model does not have precision_reduce configured (that is, it is in the graylist), the mixed-precision handling mechanism for the current operator follows that of the previous operator. That is, if the previous operator supports precision reduction, the current operator also supports it; if the previous operator does not allow precision reduction, the current operator does not either.
- allow_mix_precision_bf16:
Mixed precision of bfloat16 and float32 is used for neural network processing. In this mode, bfloat16 is automatically used for certain float32 operators on the original model based on the built-in tuning policy, which improves system performance and reduces memory usage with minimal accuracy loss. If the operator has neither bfloat16 nor float32 implementation on the AI Core, the AI CPU operator is used for computation. If the AI CPU operator is also unsupported, an error is reported.
If this mode is configured, you can view the value of precision_reduce in the built-in tuning policy file ${INSTALL_DIR}/opp/built-in/op_impl/ai_core/tbe/config/xxx/aic-xxx-ops-info-*.json.
- If it is set to true, the operator is on trustlist and its precision will be reduced from float32 to bfloat16.
- If it is set to false, the operator is on the blocklist and its precision will not be reduced from float32 to bfloat16.
- If an operator in the network model does not have precision_reduce configured (that is, it is in the graylist), the mixed-precision handling mechanism for the current operator follows that of the previous operator. That is, if the previous operator supports precision reduction, the current operator also supports it; if the previous operator does not allow precision reduction, the current operator does not either.
- allow_fp32_to_bf16:
- If the operator computation precision in the original graph is float32, the original precision is used preferentially. If the operator does not have a float32 implementation on the AI Core, the precision is reduced to bfloat16. If the AI Core also does not support bfloat16 for this operator, the AI CPU operator is used for computation. If the AI CPU operator is also unsupported, an error is reported.
- If the operator computation precision in the original graph is bfloat16, the original precision is used preferentially. If the operator does not have a bfloat16 implementation on the AI Core, float32 is used. If the AI Core also does not support float32 for this operator, the AI CPU operator is used for computation. If the AI CPU operator is also unsupported, an error is reported.
The default values are as follows:
Atlas 200I/500 A2 inference products inference scenario: force_fp16Atlas inference products inference scenario: force_fp16Atlas training products inference scenario: force_fp16Atlas A3 training products /Atlas A3 inference products inference scenario: force_fp16Atlas A2 training products /Atlas A2 inference products training scenario: must_keep_origin_dtypeAtlas A3 training products /Atlas A3 inference products training scenario: must_keep_origin_dtypeAtlas training products training scenario: allow_fp32_to_fp16
Suggestions and Benefits
The accuracy and performance of the network model vary according to the configured precision mode.
Accuracy ranked from high to low: force_fp32 > must_keep_origin_dtype > allow_fp32_to_fp16 > allow_mix_precision > force_fp16
Performance ranked from high to low: force_fp16 >= allow_mix_precision > allow_fp32_to_fp16 > must_keep_origin_dtype > force_fp32
Examples
--precision_mode=force_fp16
Dependencies and Restrictions
None
