LiteRT 8-बिट क्वांटाइज़ेशन की खास बातें

इस दस्तावेज़ में, LiteRT के 8-बिट क्वांटाइज़ेशन स्कीम के बारे में बताया गया है. इसका मकसद, हार्डवेयर डेवलपर की मदद करना है, ताकि वे क्वान्टाइज़ किए गए LiteRT मॉडल के साथ अनुमान लगाने के लिए हार्डवेयर सपोर्ट दे सकें.

खास जानकारी

हम एक स्पेसिफ़िकेशन दे रहे हैं. अगर स्पेसिफ़िकेशन का पालन किया जाता है, तो हम सिर्फ़ कुछ गारंटी दे सकते हैं. हम यह भी समझते हैं कि अलग-अलग हार्डवेयर में कुछ खास सेटिंग और पाबंदियां हो सकती हैं. इनकी वजह से, स्पेसिफ़िकेशन लागू करते समय थोड़ा अंतर आ सकता है. इससे, बिट-परफ़ेक्ट तरीके से लागू नहीं किया जा सकता. ज़्यादातर मामलों में, यह स्वीकार किया जा सकता है. हम आपको कई ऐसे टेस्ट उपलब्ध कराएंगे जिनमें हर ऑपरेशन के लिए टॉलरेंस शामिल है. यह जानकारी हमने कई मॉडल से इकट्ठा की है. हालांकि, मशीन लर्निंग (और ज़्यादातर मामलों में डीप लर्निंग) की वजह से, हम कोई गारंटी नहीं दे सकते.

8-बिट क्वांटाइज़ेशन, फ़्लोटिंग पॉइंट वैल्यू का अनुमान लगाने के लिए इस फ़ॉर्मूले का इस्तेमाल करता है.

\[real\_value = (int8\_value - zero\_point) \times scale\]

हर ऐक्सिस (इसे कन्वर्ज़न ऑपरेशंस में हर चैनल भी कहा जाता है) या हर टेंसर के वज़न को int8 के बीच मौजूद दो के पूरक मानों से दिखाया जाता है. इसमें शून्य बिंदु 0 के बराबर होता है.[-127, 127] हर-टेंसर ऐक्टिवेशन/इनपुट को int8 के पूरक मानों से दिखाया जाता है. ये मान [-128, 127] की सीमा में होते हैं. साथ ही, शून्य बिंदु [-128, 127] की सीमा में होता है.

कुछ खास कार्रवाइयों के लिए, अन्य अपवाद भी हैं. इनके बारे में यहां बताया गया है.

साइन किए गए पूर्णांक बनाम साइन नहीं किए गए पूर्णांक

LiteRT क्वांटाइज़ेशन, मुख्य रूप से टूलिंग और कर्नल को प्राथमिकता देगा. int8 क्वांटाइज़ेशन 8-बिट के लिए होगा. यह सिमेट्रिक क्वांटाइज़ेशन की सुविधा के लिए है. इसे ज़ीरो-पॉइंट के तौर पर दिखाया जाता है, जो 0 के बराबर होता है. इसके अलावा, कई बैकएंड में int8xint8 को इकट्ठा करने के लिए अतिरिक्त ऑप्टिमाइज़ेशन होते हैं.

हर ऐक्सिस बनाम हर टेंसर

पर-टेंसर क्वांटाइज़ेशन का मतलब है कि पूरे टेंसर के लिए एक स्केल और/या ज़ीरो-पॉइंट होगा. हर ऐक्सिस के हिसाब से क्वांटाइज़ेशन का मतलब है कि quantized_dimension में हर स्लाइस के लिए एक स्केल और/या zero_point होगा. क्वांटाइज़्ड डाइमेंशन, Tensor के शेप के उस डाइमेंशन के बारे में बताता है जिससे स्केल और ज़ीरो-पॉइंट जुड़े होते हैं. उदाहरण के लिए, t टेंसर में dims=[4, 3, 2, 1] है. इसमें क्वानटाइज़ेशन पैरामीटर: scale=[1.0, 2.0, 3.0], zero_point=[1, 2, 3], quantization_dimension=1 हैं. इसे t के दूसरे डाइमेंशन के हिसाब से क्वानटाइज़ किया जाएगा:

t[:, 0, :, :] will have scale[0]=1.0, zero_point[0]=1
t[:, 1, :, :] will have scale[1]=2.0, zero_point[1]=2
t[:, 2, :, :] will have scale[2]=3.0, zero_point[2]=3

अक्सर, quantized_dimension, कनवोल्यूशन के वज़न का output_channel होता है. हालांकि, सिद्धांत के तौर पर यह वह डाइमेंशन हो सकता है जो कर्नल के लागू होने में हर डॉट-प्रॉडक्ट से मेल खाता है. इससे परफ़ॉर्मेंस पर असर डाले बिना, ज़्यादा क्वांटाइज़ेशन ग्रेन्यूलैरिटी मिलती है. इससे जवाब ज़्यादा सटीक हो जाते हैं.

TFLite में, हर ऐक्सिस के लिए ज़्यादा से ज़्यादा ऑपरेशनों को सपोर्ट किया जाता है. इस दस्तावेज़ के मुताबिक, Conv2d और DepthwiseConv2d के लिए सहायता उपलब्ध है.

सिमेट्रिक और एसिमेट्रिक

एक्टिवेशन एसिमेट्रिक होते हैं: इनके शून्य बिंदु, साइंड int8 रेंज [-128, 127] में कहीं भी हो सकते हैं. कई ऐक्टिवेशन, असिमेट्रिक होते हैं. साथ ही, ज़ीरो-पॉइंट, सटीक जानकारी का एक अतिरिक्त बाइनरी बिट पाने का एक सस्ता तरीका है. ऐक्टिवेशन को सिर्फ़ कॉन्स्टेंट वेट से गुणा किया जाता है. इसलिए, कॉन्स्टेंट ज़ीरो-पॉइंट वैल्यू को काफ़ी हद तक ऑप्टिमाइज़ किया जा सकता है.

वज़न सिमेट्रिक होते हैं: इन्हें 0.0 के बराबर होना चाहिए. वज़न की वैल्यू को डाइनैमिक इनपुट और ऐक्टिवेशन वैल्यू से गुणा किया जाता है. इसका मतलब है कि वज़न के ज़ीरो-पॉइंट को ऐक्टिवेशन वैल्यू से गुणा करने पर, रनटाइम की लागत को टाला नहीं जा सकता. शून्य बिंदु को 0 पर सेट करके, इस लागत से बचा जा सकता है.

गणित के बारे में जानकारी: यह arXiv:1712.05877 के सेक्शन 2.3 से मिलती-जुलती है. हालांकि, इसमें यह अंतर है कि हम स्केल वैल्यू को हर ऐक्सिस के हिसाब से तय करने की अनुमति देते हैं. इसे आसानी से इस तरह सामान्य बनाया जा सकता है:

$A$, क्वांटाइज़ किए गए ऐक्टिवेशन की $m \times n$ मैट्रिक्स है.
$B$, क्वांटाइज़ किए गए वेट की $n \times p$ मैट्रिक्स है.
$A$की $j$वीं लाइन $a_j$ को $B$के $k$वें कॉलम $b_k$ से गुणा करें. दोनों की लंबाई $n$ है. पूर्णांक की क्वांटाइज़ की गई वैल्यू और शून्य-बिंदु की वैल्यू, क्रमशः $q_a$, $z_a$ और $q_b$, $z_b$ हैं.

\[a_j \cdot b_k = \sum_{i=0}^{n} a_{j}^{(i)} b_{k}^{(i)} = \sum_{i=0}^{n} (q_{a}^{(i)} - z_a) (q_{b}^{(i)} - z_b) = \sum_{i=0}^{n} q_{a}^{(i)} q_{b}^{(i)} - \sum_{i=0}^{n} q_{a}^{(i)} z_b - \sum_{i=0}^{n} q_{b}^{(i)} z_a + \sum_{i=0}^{n} z_a z_b\]

\(\sum_{i=0}^{n} q_{a}^{(i)} q_{b}^{(i)}\) टर्म को हटाया नहीं जा सकता, क्योंकि यह इनपुट वैल्यू और वेट वैल्यू का डॉट प्रॉडक्ट है.

\(\sum_{i=0}^{n} q_{b}^{(i)} z_a\) और \(\sum_{i=0}^{n} z_a z_b\) टर्म, ऐसे कॉन्स्टेंट से बनी होती हैं जो हर इन्फ़्रेंस इनवोकेशन के लिए एक जैसे रहते हैं. इसलिए, इनकी पहले से गणना की जा सकती है.

\(\sum_{i=0}^{n} q_{a}^{(i)} z_b\) टर्म को हर अनुमान के लिए कंप्यूट करना होता है, क्योंकि हर अनुमान के लिए ऐक्टिवेशन बदलता है. वज़न को सिमेट्रिक बनाने पर, हम इस शब्द की लागत को हटा सकते हैं.

int8 क्वांटाइज़्ड ऑपरेटर की खास बातें

यहां हम अपने int8 tflite कर्नेल के लिए, क्वानटाइज़ेशन से जुड़ी ज़रूरी शर्तों के बारे में बताते हैं:

ADD
  Input 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  Input 1:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  Output 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor

AVERAGE_POOL_2D
  Input 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  Output 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  restriction: Input and outputs must all have same scale/zero_point

CONCATENATION
  Input ...:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  Output 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  restriction: Input and outputs must all have same scale/zero_point

CONV_2D
  Input 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  Input 1 (Weight):
    data_type  : int8
    range      : [-127, 127]
    granularity: per-axis (dim = 0)
    restriction: zero_point = 0
  Input 2 (Bias):
    data_type  : int32
    range      : [int32_min, int32_max]
    granularity: per-axis
    restriction: (scale, zero_point) = (input0_scale * input1_scale[...], 0)
  Output 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor

DEPTHWISE_CONV_2D
  Input 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  Input 1 (Weight):
    data_type  : int8
    range      : [-127, 127]
    granularity: per-axis (dim = 3)
    restriction: zero_point = 0
  Input 2 (Bias):
    data_type  : int32
    range      : [int32_min, int32_max]
    granularity: per-axis
    restriction: (scale, zero_point) = (input0_scale * input1_scale[...], 0)
  Output 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor

FULLY_CONNECTED
  Input 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  Input 1 (Weight):
    data_type  : int8
    range      : [-127, 127]
    granularity: per-axis (dim = 0)
    restriction: zero_point = 0
  Input 2 (Bias):
    data_type  : int32
    range      : [int32_min, int32_max]
    granularity: per-tensor
    restriction: (scale, zero_point) = (input0_scale * input1_scale[...], 0)
  Output 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor

L2_NORMALIZATION
  Input 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  Output 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
    restriction: (scale, zero_point) = (1.0 / 128.0, 0)

LOGISTIC
  Input 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  Output 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
    restriction: (scale, zero_point) = (1.0 / 256.0, -128)

MAX_POOL_2D
  Input 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  Output 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  restriction: Input and outputs must all have same scale/zero_point

MUL
  Input 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  Input 1:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  Output 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor

RESHAPE
  Input 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  Output 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  restriction: Input and outputs must all have same scale/zero_point

RESIZE_BILINEAR
  Input 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  Output 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  restriction: Input and outputs must all have same scale/zero_point

SOFTMAX
  Input 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  Output 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
    restriction: (scale, zero_point) = (1.0 / 256.0, -128)

SPACE_TO_DEPTH
  Input 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  Output 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  restriction: Input and outputs must all have same scale/zero_point

TANH
  Input 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  Output 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
    restriction: (scale, zero_point) = (1.0 / 128.0, 0)

PAD
  Input 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  Output 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  restriction: Input and outputs must all have same scale/zero_point

GATHER
  Input 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  Output 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  restriction: Input and outputs must all have same scale/zero_point

BATCH_TO_SPACE_ND
  Input 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  Output 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  restriction: Input and outputs must all have same scale/zero_point

SPACE_TO_BATCH_ND
  Input 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  Output 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  restriction: Input and outputs must all have same scale/zero_point

TRANSPOSE
  Input 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  Output 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  restriction: Input and outputs must all have same scale/zero_point

MEAN
  Input 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  Output 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor

SUB
  Input 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  Input 1:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  Output 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor

SUM
  Input 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  Output 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor

SQUEEZE
  Input 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  Output 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  restriction: Input and outputs must all have same scale/zero_point

LOG_SOFTMAX
  Input 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  Output 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
    restriction: (scale, zero_point) = (16.0 / 256.0, 127)

MAXIMUM
  Input 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  Output 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  restriction: Input and outputs must all have same scale/zero_point

ARG_MAX
  Input 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor

MINIMUM
  Input 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  Output 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  restriction: Input and outputs must all have same scale/zero_point

LESS
  Input 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  Input 1:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor

PADV2
  Input 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  Output 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  restriction: Input and outputs must all have same scale/zero_point

GREATER
  Input 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  Input 1:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor

GREATER_EQUAL
  Input 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  Input 1:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor

LESS_EQUAL
  Input 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  Input 1:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor

SLICE
  Input 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  Output 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  restriction: Input and outputs must all have same scale/zero_point

EQUAL
  Input 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  Input 1:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor

NOT_EQUAL
  Input 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  Input 1:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor

SHAPE
  Input 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor

QUANTIZE (Requantization)
  Input 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor
  Output 0:
    data_type  : int8
    range      : [-128, 127]
    granularity: per-tensor

रेफ़रंस

arXiv:1712.05877