इस दस्तावेज़ में, TensorFlow Lite की 8-बिट क्वांटाइज़ेशन स्कीम की खास बातों के बारे में बताया गया है. इसका मकसद, हार्डवेयर डेवलपर को संख्या वाले TensorFlow Lite मॉडल के लिए, हार्डवेयर के इस्तेमाल से जुड़ी सहायता देने में मदद करना है.
खास जानकारी
हम इसके बारे में जानकारी दे रहे हैं. अगर निर्देशों का पालन किया जाता है, तो ही हम व्यवहार की कुछ गारंटी दे सकते हैं. हम यह भी समझते हैं कि अलग-अलग हार्डवेयर की प्राथमिकताएं और पाबंदियां हो सकती हैं, जिनकी वजह से उस शर्त को लागू करने में थोड़ा अंतर दिख सकता है. हालांकि, ज़्यादातर मामलों में यह स्वीकार किया जा सकता है (और हम जांच का एक सुइट उपलब्ध कराएंगे, जिसमें हमारी जानकारी के हिसाब से, हर ऑपरेशन के दौरान की जाने वाली सहिष्णुता शामिल होगी, जिसे हमने कई मॉडल से इकट्ठा किया है). मशीन लर्निंग (और ज़्यादातर मामलों में डीप लर्निंग) की वजह से इसकी गारंटी देना संभव नहीं होता.
8-बिट क्वांटाइज़ेशन, नीचे दिए गए फ़ॉर्मूला का इस्तेमाल करके फ़्लोटिंग पॉइंट वैल्यू का अनुमान लगाता है.
\[real\_value = (int8\_value - zero\_point) \times scale\]
हर ऐक्सिस (कन्वर्ज़न ऑपरेशन में हर चैनल यानी हर चैनल के हिसाब से) या हर टेंसर वेट को
int8
[-127, 127]
में दो की पूरक वैल्यू से दिखाया जाता है, जिनका शून्य-पॉइंट 0 होता है. हर टेन्सर ऐक्टिवेशन/इनपुट को [-128, 127]
में int8
की दो पूरक वैल्यू से दिखाया जाता है. वैल्यू की रेंज [-128, 127]
में ज़ीरो-पॉइंट होती है.
खास कार्रवाइयों के कुछ दूसरे अपवाद भी हैं, जिनके बारे में नीचे बताया गया है.
साइन किया गया पूर्णांक बनाम साइन नहीं किया गया पूर्णांक
TensorFlow Lite क्वांटाइज़ेशन में मुख्य तौर पर 8-बिट के लिए int8
क्वांटाइज़ेशन के लिए, टूल और कर्नेल को प्राथमिकता दी जाएगी. यह सिमेट्रिक क्वांटाइज़ेशन की सुविधा के लिए है, जिसे 0 के बराबर ज़ीरो-पॉइंट से दिखाया जाता है. इसके अलावा, कई
बैकएंड में int8xint8
एग्रीगेशन के लिए, अन्य ऑप्टिमाइज़ेशन भी होते हैं.
हर ऐक्सिस बनाम हर टेंसर
पर-टेन्सर क्वांटाइज़ेशन का मतलब है कि हर टेंसर के लिए एक स्केल और/या ज़ीरो पॉइंट होगा. हर ऐक्सिस पर संख्या दिखाने का मतलब है कि quantized_dimension
में हर स्लाइस के लिए एक स्केल और/या zero_point
होगा. संख्या वाला डाइमेंशन, Tensor के आकार के ऐसे डाइमेंशन के बारे में बताता है जिसके हिसाब से स्केल और ज़ीरो पॉइंट तय होते हैं. उदाहरण के लिए, संख्या बढ़ाने वाले पैरामीटर scale=[1.0, 2.0, 3.0]
, zero_point=[1, 2, 3]
, और quantization_dimension=1
वाले dims=[4, 3, 2, 1]
वाले टेंसर t
को 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 से हर दिशा में मदद मिलती है, ताकि काम की संख्या बढ़ती जा सके. इस दस्तावेज़ को फ़िलहाल, Convert2d और depthwise Conversion2d पर सहायता टीम मौजूद है.
सिमेट्रिक बनाम एसिमेट्रिक
ऐक्टिवेशन का मतलब एसिमेट्रिक तरीके से नहीं है: इनका ज़ीरो-पॉइंट, साइन की गई int8
रेंज [-128, 127]
के अंदर कहीं भी हो सकता है. कई ऐक्टिवेशन प्रोसेस में अलग-अलग तरह की गतिविधियां होती हैं और ज़ीरो-पॉइंट, ज़्यादा सटीक जानकारी पाने का सस्ता तरीका है. ऐक्टिवेशन को सिर्फ़ कॉन्स्टेंट भार से गुणा किया जाता है, इसलिए स्थिर ज़ीरो-पॉइंट वैल्यू को अच्छी तरह ऑप्टिमाइज़ किया जा सकता है.
वेट सिमेट्रिक होते हैं: 0 के बराबर शून्य को हर हाल में लागू किया जाता है. वज़न की वैल्यू को डाइनैमिक इनपुट और ऐक्टिवेशन वैल्यू से गुणा किया जाता है. इसका मतलब है कि वैल्यू के ज़ीरो-पॉइंट को ऐक्टिवेशन वैल्यू से गुणा करने के लिए, रनटाइम की लागत घट सकती है. हालांकि, इससे बचा नहीं जा सकता. ज़ीरो-पॉइंट 0 को लागू करके, हम इस लागत से बच सकते हैं.
गणित के बारे में जानकारी: यह arXiv:1712.05877 के सेक्शन 2.3 के जैसा है. हालांकि, इसमें अंतर को छोड़कर हम स्केल की वैल्यू को प्रति-ऐक्सिस होने की अनुमति देते हैं. यह अपने हिसाब से, नीचे बताए गए तरीके से सामान्य बना देता है:
$A$, संख्या दिए गए ऐक्टिवेशन का $m \times n$ मैट्रिक्स है.
$B$, कैलकुलेट किए गए वज़न का $n \times p$ मैट्रिक्स है.
$A$, $a_j$ की $j$ th पंक्ति को $B$, $b_k$, दोनों की लंबाई $n$ के $k$th कॉलम से गुणा करें. संख्यात्मक पूर्णांक मान और
शून्य-पॉइंट मान क्रमश: $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 kernel के लिए, संख्या बढ़ाने की ज़रूरी शर्तों के बारे में बताया है:
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