Tensorflow 每通道量化

Tensorflow per channel quantization

使用当前的 Tensorflow quantization ops, how would I go about simulating per-channel quantization during inference? This paperper-layer 量化定义为

We can specify a single quantizer (defined by the scale and zero-point) for an entire tensor referred to as per-layer quantization

每通道量化为

Per-channel quantization has a different scale and offset for each convolutional kernel.

假设我们有这个 子图

import tensorflow as tf

x = np.random.uniform(size=500*80*64*1)
      .astype('float32')
      .reshape(500, 80, 64, 1)
W1 = tf.get_variable('W1', 9, 5, 1, 96],
                    initializer=tf.truncated_normal_initializer(stddev=0.1))
h1 = tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='VALID')

在当前的 API 下,我可能会做这样的事情来模拟 per-layer 在推理时的量化

import tensorflow as tf

x = np.random.uniform(size=500*80*64*1)
      .astype('float32')
      .reshape(500, 80, 64, 1)
min_x = tf.reduce_min(x)
max_x = tf.reduce_max(x)

W1 = tf.get_variable('W1', 9, 5, 1, 96],
                    initializer=tf.truncated_normal_initializer(stddev=0.1))
min_W1 = tf.reduce_min(W1)
max_W1 = tf.reduce_max(W1)

qX = tf.quantize(A, min_X, max_X, tf.quint8, mode='MIN_FIRST')
qW = tf.quantize(W, min_W, max_W, tf.quint8, mode='MIN_FIRST')

# This is how one would simulate per layer quantization for convolution.
qAW = tf.nn.quantized_conv2d(qX[0], qW[0], qX[1], qX[2], qW[1], qW[2], 
strides = [1, 1, 1, 1], padding='VALID')

我的问题是如何模拟每个通道的量化?据我了解,tf.quantization.quantize 实际上是在进行 每层 量化,而不是 每通道 量化。此外,tf.nn.quantized_conv2d 实际上是在对量化层内核卷积进行量化层输入。

根据我对每通道量化的理解,会有koutput_minoutput_max。在我的示例中 k96 (内核的数量,类似于此 API )。

tensorflow 中是否存在可以处理每通道 量化的任何现有 Ops,或者是否有使其与现有 ops 一起工作的方法?

目前无法在 tflite 上模拟每通道量化推理。 正如我所见,如今的 tensorflow 开发人员正在实施 experimental symmetric per channel quantization。但是没有办法测试