Imported Upstream version 1.19.0
[platform/core/ml/nnfw.git] / compiler / one-cmds / one-quantize
index cd623a6..22d4ddb 100644 (file)
@@ -39,13 +39,13 @@ def _get_parser():
 
     # input and output path.
     parser.add_argument(
-        '-i', '--input_path', type=str, help='full filepath of the input file')
+        '-i', '--input_path', type=str, help='full filepath of the input circle model')
     parser.add_argument(
         '-d',
         '--input_data',
         type=str,
         help=
-        'full filepath of the input data file. if not specified, run with random input data.'
+        'full filepath of the input data used for post-training quantization. if not specified, run with random input data.'
     )
     parser.add_argument(
         '-f',
@@ -55,7 +55,10 @@ def _get_parser():
         'file format of input data. h5/hdf5 (default), list/filelist (a text file where a file path of input data is written in each line), or dir/directory (a directory where input data are saved)'
     )
     parser.add_argument(
-        '-o', '--output_path', type=str, help='full filepath of the output file')
+        '-o',
+        '--output_path',
+        type=str,
+        help='full filepath of the output quantized model')
 
     # argument for profiling
     parser.add_argument(
@@ -70,41 +73,77 @@ def _get_parser():
     quantization_group.add_argument(
         '--input_dtype',
         type=str,
-        help='input data type (supported: float32, default=float32)')
+        help=
+        'input model data type (supported: float32, default=float32). Deprecated (Use input_model_dtype)'
+    )
+    quantization_group.add_argument(
+        '--input_model_dtype',
+        type=str,
+        help='input model data type (supported: float32, default=float32)')
     quantization_group.add_argument(
         '--quantized_dtype',
         type=str,
-        help='output quantized data type (supported: uint8, int16, default=uint8)')
+        help='data type of output quantized model (supported: uint8, int16, default=uint8)'
+    )
     quantization_group.add_argument(
         '--granularity',
         type=str,
-        help='quantize granularity (supported: layer, channel, default=layer)')
+        help='quantization granularity (supported: layer, channel, default=layer)')
+    quantization_group.add_argument(
+        '--input_type',
+        type=str,
+        help=
+        'data type of inputs of quantized model (supported: uint8, int16, default=quantized_dtype). QUANTIZE Op will be inserted at the beginning of the quantized model if input_type is different from quantized_dtype.'
+    )
+    quantization_group.add_argument(
+        '--output_type',
+        type=str,
+        help=
+        'data type of outputs of quantized model (supported: uint8, int16, default=quantized_dtype). QUANTIZE Op will be inserted at the end of the quantized model if output_type is different from quantized_dtype.'
+    )
     quantization_group.add_argument(
-        '--min_percentile', type=str, help='minimum percentile (0.0~100.0, default=1.0)')
+        '--min_percentile',
+        type=str,
+        help=
+        'minimum percentile (0.0~100.0, default=1.0). Algorithm parameter for calibration. This is valid when calibration algorithm is percentile.'
+    )
     quantization_group.add_argument(
-        '--max_percentile', type=str, help='maximum percentile (0.0~100.0, default=99.0)')
+        '--max_percentile',
+        type=str,
+        help=
+        'maximum percentile (0.0~100.0, default=99.0). Algorithm parameter for calibration. This is valid when calibration algorithm is percentile.'
+    )
     quantization_group.add_argument(
         '--mode',
         type=str,
-        help='record mode (supported: percentile/moving_average, default=percentile)')
+        help=
+        "calibration algorithm for post-training quantization (supported: percentile/moving_average, default=percentile). 'percentile' mode uses the n-th percentiles as min/max values. 'moving_average' mode records the moving average of min/max."
+    )
 
-    # arguments for force_quantparam
-    parser.add_argument(
+    # arguments for force_quantparam option
+    force_quantparam_group = parser.add_argument_group(
+        'arguments for force_quantparam option')
+
+    force_quantparam_group.add_argument(
         '--force_quantparam',
         action='store_true',
-        help='write quantparam to the specified tensor')
-    parser.add_argument(
+        help=
+        'overwrite quantparam (scale, zero_point) to the specified tensor in the quantized model.'
+    )
+    force_quantparam_group.add_argument(
         '--tensor_name', type=str, action='append', help='tensor name (string)')
-    parser.add_argument('--scale', type=float, action='append', help='scale (float)')
-    parser.add_argument(
+    force_quantparam_group.add_argument(
+        '--scale', type=float, action='append', help='scale (float)')
+    force_quantparam_group.add_argument(
         '--zero_point', type=int, action='append', help='zero point (int)')
 
     return parser
 
 
 def _set_default_values(args):
-    if not _utils._is_valid_attr(args, 'input_dtype'):
-        setattr(args, 'input_dtype', 'float32')
+    if not _utils._is_valid_attr(args, 'input_model_dtype') and not _utils._is_valid_attr(
+            args, 'input_dtype'):
+        setattr(args, 'input_model_dtype', 'float32')
     if not _utils._is_valid_attr(args, 'quantized_dtype'):
         setattr(args, 'quantized_dtype', 'uint8')
     if not _utils._is_valid_attr(args, 'granularity'):
@@ -174,7 +213,10 @@ def _quantize(args):
             circle_quantizer_cmd.append('--verbose')
         # quantize_dequantize_weights
         circle_quantizer_cmd.append('--quantize_dequantize_weights')
-        if _utils._is_valid_attr(args, 'input_dtype'):
+        # Use input_model_dtype if it exists. Use input_dtype otherwise.
+        if _utils._is_valid_attr(args, 'input_model_dtype'):
+            circle_quantizer_cmd.append(getattr(args, 'input_model_dtype'))
+        elif _utils._is_valid_attr(args, 'input_dtype'):
             circle_quantizer_cmd.append(getattr(args, 'input_dtype'))
         if _utils._is_valid_attr(args, 'quantized_dtype'):
             circle_quantizer_cmd.append(getattr(args, 'quantized_dtype'))
@@ -243,12 +285,21 @@ def _quantize(args):
             circle_quantizer_cmd.append('--verbose')
         # quantize_dequantize_weights
         circle_quantizer_cmd.append('--quantize_with_minmax')
-        if _utils._is_valid_attr(args, 'input_dtype'):
+        # Use input_model_dtype if it exists. Use input_dtype otherwise.
+        if _utils._is_valid_attr(args, 'input_model_dtype'):
+            circle_quantizer_cmd.append(getattr(args, 'input_model_dtype'))
+        elif _utils._is_valid_attr(args, 'input_dtype'):
             circle_quantizer_cmd.append(getattr(args, 'input_dtype'))
         if _utils._is_valid_attr(args, 'quantized_dtype'):
             circle_quantizer_cmd.append(getattr(args, 'quantized_dtype'))
         if _utils._is_valid_attr(args, 'granularity'):
             circle_quantizer_cmd.append(getattr(args, 'granularity'))
+        if _utils._is_valid_attr(args, 'input_type'):
+            circle_quantizer_cmd.append('--input_type')
+            circle_quantizer_cmd.append(getattr(args, 'input_type'))
+        if _utils._is_valid_attr(args, 'output_type'):
+            circle_quantizer_cmd.append('--output_type')
+            circle_quantizer_cmd.append(getattr(args, 'output_type'))
         # input and output path
         circle_quantizer_cmd.append(tmp_output_path_2)
         if _utils._is_valid_attr(args, 'output_path'):