Adds a layer that collects quantization ranges as last input ranges. LastValueQuantize creates variables called 'min' and 'max', representing the interval used for quantization and clamping. Args: inputs: a tensor containing values to be quantized. per_channel: (Optional) a boolean s
(inputs,
per_channel=False,
init_min=-6.0,
init_max=6.0,
vars_collection=None,
name_prefix='LastValueQuant',
reuse=None,
is_training=True,
num_bits=8,
narrow_range=False,
symmetric=False,
use_qdq=False)
| 60 | |
| 61 | |
| 62 | def LastValueQuantize(inputs, |
| 63 | per_channel=False, |
| 64 | init_min=-6.0, |
| 65 | init_max=6.0, |
| 66 | vars_collection=None, |
| 67 | name_prefix='LastValueQuant', |
| 68 | reuse=None, |
| 69 | is_training=True, |
| 70 | num_bits=8, |
| 71 | narrow_range=False, |
| 72 | symmetric=False, |
| 73 | use_qdq=False): |
| 74 | """Adds a layer that collects quantization ranges as last input ranges. |
| 75 | |
| 76 | LastValueQuantize creates variables called 'min' and 'max', representing the |
| 77 | interval used for quantization and clamping. |
| 78 | |
| 79 | Args: |
| 80 | inputs: a tensor containing values to be quantized. |
| 81 | per_channel: (Optional) a boolean specifying whether to use different |
| 82 | quantization ranges per output channel. |
| 83 | init_min: a float scalar, the initial value for variable min. |
| 84 | init_max: a float scalar, the initial value for variable max. |
| 85 | vars_collection: (Optional) collection where to store variables for |
| 86 | quantization interval ends. |
| 87 | name_prefix: name_prefix for created nodes. |
| 88 | reuse: whether or not the layer and its variables should be reused. To be |
| 89 | able to reuse the layer scope must be given. |
| 90 | is_training: Whether the op is applied to a training or eval graph. |
| 91 | num_bits: Number of bits to use for quantization, must be between 2 and 8. |
| 92 | narrow_range: Whether to use the narrow quantization range |
| 93 | [1; 2^num_bits - 1] or wide range [0; 2^num_bits - 1]. |
| 94 | symmetric: If true, use symmetric quantization limits instead of training |
| 95 | the minimum and maximum of each quantization range separately. |
| 96 | use_qdq: Use tf.quantize_and_dequantize_v3 op instead of fake_quant_with_min_max_vars |
| 97 | for quantization. The qdq op is used for scaling with no zero point. |
| 98 | Returns: |
| 99 | a tensor containing quantized values. |
| 100 | """ |
| 101 | with variable_scope.variable_scope( |
| 102 | None, default_name=name_prefix, values=[inputs], reuse=reuse) as scope: |
| 103 | scope.set_partitioner(None) |
| 104 | input_shape = inputs.get_shape() |
| 105 | input_dim = len(input_shape) |
| 106 | if per_channel: |
| 107 | # Only support quantizing 1-, 2- and 4-dimensional tensors. |
| 108 | assert input_dim in [1, 2, 4], ('Expected 1D, 2D or 4D input, was: %s in ' |
| 109 | ' scope: %s' % (input_shape, name_prefix)) |
| 110 | min_max_shape = [input_shape[-1]] |
| 111 | else: |
| 112 | min_max_shape = [] |
| 113 | |
| 114 | vars_collections = [vars_collection] if vars_collection else [] |
| 115 | min_var = _ModelVariable( |
| 116 | 'min', |
| 117 | shape=min_max_shape, |
| 118 | initializer=init_ops.constant_initializer(init_min), |
| 119 | collections=vars_collections, |
nothing calls this directly
no test coverage detected