Adds a layer that collects quantization ranges as EMAs of input ranges. MovingAvgQuantize 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: (default False) a b
(inputs,
per_channel=False,
init_min=-6.0,
init_max=6.0,
ema_decay=0.999,
vars_collection=ops.GraphKeys.MOVING_AVERAGE_VARIABLES,
name_prefix='MovingAvgQuantize',
reuse=None,
is_training=True,
num_bits=8,
narrow_range=False,
symmetric=False,
use_qdq=False)
| 189 | |
| 190 | |
| 191 | def MovingAvgQuantize(inputs, |
| 192 | per_channel=False, |
| 193 | init_min=-6.0, |
| 194 | init_max=6.0, |
| 195 | ema_decay=0.999, |
| 196 | vars_collection=ops.GraphKeys.MOVING_AVERAGE_VARIABLES, |
| 197 | name_prefix='MovingAvgQuantize', |
| 198 | reuse=None, |
| 199 | is_training=True, |
| 200 | num_bits=8, |
| 201 | narrow_range=False, |
| 202 | symmetric=False, |
| 203 | use_qdq=False): |
| 204 | """Adds a layer that collects quantization ranges as EMAs of input ranges. |
| 205 | |
| 206 | MovingAvgQuantize creates variables called 'min' and 'max', representing the |
| 207 | interval used for quantization and clamping. |
| 208 | |
| 209 | Args: |
| 210 | inputs: a tensor containing values to be quantized. |
| 211 | per_channel: (default False) a boolean specifying whether to use different |
| 212 | quantization ranges per output channel. |
| 213 | init_min: a float scalar, the initial value for variable min. |
| 214 | init_max: a float scalar, the initial value for variable max. |
| 215 | ema_decay: EMA decay parameter. |
| 216 | vars_collection: (Optional) collection where to store variables for |
| 217 | quantization interval ends. |
| 218 | name_prefix: name_prefix for created nodes. |
| 219 | reuse: whether or not the layer and its variables should be reused. To be |
| 220 | able to reuse the layer scope must be given. |
| 221 | is_training: Whether the op is applied to a training or eval graph. |
| 222 | num_bits: Number of bits to use for quantization, must be between 2 and 8. |
| 223 | narrow_range: Whether to use the narrow quantization range |
| 224 | [1; 2^num_bits - 1] or wide range [0; 2^num_bits - 1]. |
| 225 | symmetric: If true, use symmetric quantization limits instead of training |
| 226 | the minimum and maximum of each quantization range separately. |
| 227 | use_qdq: Use tf.quantize_and_dequantize_v3 op instead of fake_quant_with_min_max_vars |
| 228 | for quantization. The qdq op is used for scaling with no zero point. |
| 229 | Returns: |
| 230 | a tensor containing quantized values. |
| 231 | """ |
| 232 | with variable_scope.variable_scope( |
| 233 | None, default_name=name_prefix, values=[inputs], reuse=reuse) as scope: |
| 234 | scope.set_partitioner(None) |
| 235 | input_shape = inputs.get_shape() |
| 236 | if per_channel: |
| 237 | input_dim = len(input_shape) |
| 238 | # Only support quantizing 1-, 2- and 4-dimensional tensors. |
| 239 | assert input_dim in [1, 2, 4], ('Expected 1D, 2D or 4D input, was: %s in ' |
| 240 | ' scope: %s' % (input_shape, name_prefix)) |
| 241 | min_max_shape = [input_shape[-1]] |
| 242 | else: |
| 243 | min_max_shape = [] |
| 244 | |
| 245 | vars_collections = [vars_collection] if vars_collection else [] |
| 246 | min_var = _ModelVariable( |
| 247 | 'min', |
| 248 | shape=min_max_shape, |
nothing calls this directly
no test coverage detected