Skip to content

Commit 6b30207

Browse files
authored
Add Docstring for TF 3x API and Torch 3x Mixed Precision (#1944)
Signed-off-by: zehao-intel <[email protected]>
1 parent d254d50 commit 6b30207

File tree

30 files changed

+162
-24
lines changed

30 files changed

+162
-24
lines changed

.azure-pipelines/scripts/codeScan/pydocstyle/scan_path.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,6 @@
2222
/neural_compressor/torch/export
2323
/neural_compressor/common
2424
/neural_compressor/torch/algorithms/weight_only
25-
/neural_compressor/torch/algorithms/layer_wise
25+
/neural_compressor/torch/algorithms/layer_wise
26+
/neural_compressor/torch/algorithms/mixed_precision
27+
/neural_compressor/tensorflow

docs/source/3x/TensorFlow.md

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@ TensorFlow
22
===============
33

44

5-
1. [Introduction](#introduction)
6-
2. [API for TensorFlow](#api-for-tensorflow)
7-
3. [Support Matrix](#support-matrix)
8-
3.1 [Quantization Scheme](#quantization-scheme)
9-
3.2 [Quantization Approaches](#quantization-approaches)
10-
3.3 [Backend and Device](#backend-and-device)
5+
- [TensorFlow](#tensorflow)
6+
- [Introduction](#introduction)
7+
- [API for TensorFlow](#api-for-tensorflow)
8+
- [Support Matrix](#support-matrix)
9+
- [Quantization Scheme](#quantization-scheme)
10+
- [Quantization Approaches](#quantization-approaches)
11+
- [Post Training Static Quantization](#post-training-static-quantization)
12+
- [Smooth Quantization](#smooth-quantization)
13+
- [Mixed Precision](#mixed-precison)
14+
- [Backend and Device](#backend-and-device)
1115

1216
## Introduction
1317

@@ -152,9 +156,16 @@ The supported Quantization methods for TensorFlow and Keras are listed below:
152156
<td align="center"><a href="https://github.com/tensorflow/tensorflow">TensorFlow</a>/<a href="https://github.com/Intel-tensorflow/tensorflow">Intel TensorFlow</a></td>
153157
</tr>
154158
<tr>
155-
<td rowspan="2" align="center">Smooth Quantization(SQ)</td>
156-
<td rowspan="2" align="center">weights</td>
157-
<td rowspan="2" align="center">calibration</td>
159+
<td rowspan="1" align="center">Smooth Quantization(SQ)</td>
160+
<td rowspan="1" align="center">weights</td>
161+
<td rowspan="1" align="center">calibration</td>
162+
<td align="center">Tensorflow</td>
163+
<td align="center"><a href="https://github.com/tensorflow/tensorflow">TensorFlow</a>/<a href="https://github.com/Intel-tensorflow/tensorflow">Intel TensorFlow</a></td>
164+
</tr>
165+
<tr>
166+
<td rowspan="1" align="center">Mixed Precision(MP)</td>
167+
<td rowspan="1" align="center">weights and activations</td>
168+
<td rowspan="1" align="center">NA</td>
158169
<td align="center">Tensorflow</td>
159170
<td align="center"><a href="https://github.com/tensorflow/tensorflow">TensorFlow</a>/<a href="https://github.com/Intel-tensorflow/tensorflow">Intel TensorFlow</a></td>
160171
</tr>
@@ -175,6 +186,10 @@ Smooth Quantization (SQ) is an advanced quantization technique designed to optim
175186

176187
Refer to the [SQ Guide](./TF_SQ.md) for detailed information.
177188

189+
##### Mixed Precision
190+
The Mixed Precision (MP) is enabled with Post Training Static Quantization. Once `BF16` is supported on machine, the matched operators will be automatically converted.
191+
192+
178193
#### Backend and Device
179194
Intel(R) Neural Compressor supports TF GPU with [ITEX-XPU](https://github.com/intel/intel-extension-for-tensorflow). We will automatically run model on GPU by checking if it has been installed.
180195

neural_compressor/tensorflow/algorithms/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14-
14+
"""Initialize all algorithms."""
1515

1616
from neural_compressor.tensorflow.algorithms.smoother import SmoothQuant
1717
from neural_compressor.tensorflow.algorithms.static_quant import KerasAdaptor, TensorFlowAdaptor, Tensorflow_ITEXAdaptor

neural_compressor/tensorflow/algorithms/smoother/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1515
# See the License for the specific language governing permissions and
1616
# limitations under the License.
17+
"""Initialize classes of smooth quant algorithm."""
1718

1819
from neural_compressor.tensorflow.algorithms.smoother.core import SmoothQuant
1920
from neural_compressor.tensorflow.algorithms.smoother.scaler import (

neural_compressor/tensorflow/algorithms/smoother/core.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1515
# See the License for the specific language governing permissions and
1616
# limitations under the License.
17+
"""The core components of sq."""
1718

1819
from typing import Callable, Dict
1920

neural_compressor/tensorflow/algorithms/static_quant/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
"""Initialize classes of static quant algorithm."""
1415

1516
from neural_compressor.tensorflow.algorithms.static_quant.keras import KerasAdaptor
1617
from neural_compressor.tensorflow.algorithms.static_quant.tensorflow import TensorFlowAdaptor, Tensorflow_ITEXAdaptor

neural_compressor/tensorflow/algorithms/static_quant/keras.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1515
# See the License for the specific language governing permissions and
1616
# limitations under the License.
17+
"""The quantization classes for Keras."""
1718

1819
import copy
1920
import json
@@ -373,9 +374,9 @@ def _calibrate(self, model, dataloader=None, calib_interation=None):
373374
"""Apply calibration.
374375
375376
Args:
376-
model (tf.keras.Model): The model inserted with FakeQuant layers for calibration.
377+
model(tf.keras.Model): The model inserted with FakeQuant layers for calibration.
377378
dataloader(object): The calibration dataloader used to load quantization dataset.
378-
iteration(int): The iteration of calibration.
379+
calib_interation(int): The iteration of calibration.
379380
"""
380381
# run eagerly to fetch the numpy min/max
381382
results = {}
@@ -580,9 +581,9 @@ def _one_shot_query(self):
580581

581582
def _get_specified_version_cfg(self, data):
582583
"""Get the configuration for the current runtime.
584+
583585
If there's no matched configuration in the input yaml, we'll
584586
use the `default` field of yaml.
585-
586587
Args:
587588
data (Yaml content): input yaml file.
588589

neural_compressor/tensorflow/keras/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
"""Initialize classes for Keras quantization."""
1415

1516
from neural_compressor.tensorflow.keras.quantization import (
1617
StaticQuantConfig,

neural_compressor/tensorflow/keras/layers/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1515
# See the License for the specific language governing permissions and
1616
# limitations under the License.
17+
"""Initialize custom layers for Keras quantization."""
1718

1819
from neural_compressor.tensorflow.keras.layers.conv2d import QConv2D
1920
from neural_compressor.tensorflow.keras.layers.dense import QDense

neural_compressor/tensorflow/keras/layers/conv2d.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1515
# See the License for the specific language governing permissions and
1616
# limitations under the License.
17+
"""Initialize custom conv2d layers for Keras quantization."""
1718

1819
import json
1920

@@ -33,6 +34,8 @@
3334
if version1_gte_version2(tf.__version__, "2.16.1"): # pragma: no cover
3435

3536
class QConv2D(BaseConv):
37+
"""The custom quantized Conv2D layer."""
38+
3639
def __init__(
3740
self,
3841
name,
@@ -65,6 +68,7 @@ def __init__(
6568
quant_axis=None,
6669
**kwargs
6770
):
71+
"""Initialize custom quantized Conv2D layer."""
6872
super(QConv2D, self).__init__(
6973
name=name,
7074
rank=2,
@@ -100,6 +104,7 @@ def __init__(
100104
self.quant_axis = quant_axis
101105

102106
def call(self, inputs):
107+
"""The __call__ function of custom quantized Conv2D layer."""
103108
if self.quant_status == "calib" and not isinstance(inputs, tf.keras.KerasTensor):
104109
if self.granularity == "per_tensor":
105110
self.act_min_value = tf.math.reduce_min(inputs)
@@ -168,9 +173,11 @@ def call(self, inputs):
168173

169174
@classmethod
170175
def from_config(cls, config):
176+
"""Deserialize this class from a config dict."""
171177
return cls(**config)
172178

173179
def get_config(self):
180+
"""Serialize this class to a config dict."""
174181
config = super(QConv2D, self).get_config()
175182
config.update(
176183
{
@@ -193,6 +200,8 @@ def get_config(self):
193200
else:
194201

195202
class QConv2D(Conv):
203+
"""The custom quantized Conv2D layer."""
204+
196205
def __init__(
197206
self,
198207
name,
@@ -225,6 +234,7 @@ def __init__(
225234
quant_axis=None,
226235
**kwargs
227236
):
237+
"""Initialize custom quantized Conv2D layer."""
228238
super(QConv2D, self).__init__(
229239
name=name,
230240
rank=2,
@@ -260,6 +270,7 @@ def __init__(
260270
self.quant_axis = quant_axis
261271

262272
def call(self, inputs):
273+
"""The __call__ function of custom quantized Conv2D layer."""
263274
if self.quant_status == "calib":
264275
if self.granularity == "per_tensor":
265276
self.act_min_value = tf.math.reduce_min(inputs)
@@ -328,9 +339,11 @@ def call(self, inputs):
328339

329340
@classmethod
330341
def from_config(cls, config):
342+
"""Deserialize this class from a config dict."""
331343
return cls(**config)
332344

333345
def get_config(self):
346+
"""Serialize this class to a config dict."""
334347
config = super(QConv2D, self).get_config()
335348
config.update(
336349
{
@@ -352,6 +365,7 @@ def get_config(self):
352365

353366

354367
def initialize_int8_conv2d(fp32_layer, q_config):
368+
"""Initialize int8 conv2d."""
355369
kwargs = fp32_layer.get_config()
356370

357371
param_list = [

neural_compressor/tensorflow/keras/layers/dense.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1515
# See the License for the specific language governing permissions and
1616
# limitations under the License.
17+
"""Initialize custom dense layers for Keras quantization."""
1718

1819
import json
1920

@@ -25,6 +26,8 @@
2526

2627

2728
class QDense(Dense):
29+
"""The custom quantized Dense layer."""
30+
2831
def __init__(
2932
self,
3033
name,
@@ -51,6 +54,7 @@ def __init__(
5154
quant_axis=None,
5255
**kwargs
5356
):
57+
"""Initialize custom quantized Dense layer."""
5458
super(QDense, self).__init__(
5559
name=name,
5660
units=units,
@@ -79,6 +83,7 @@ def __init__(
7983
self.quant_axis = quant_axis
8084

8185
def call(self, inputs):
86+
"""The __call__ function of custom quantized Dense layer."""
8287
if self.quant_status == "calib" and not (
8388
version1_gte_version2(tf.__version__, "2.16.1") and isinstance(inputs, tf.keras.KerasTensor)
8489
):
@@ -144,9 +149,11 @@ def call(self, inputs):
144149

145150
@classmethod
146151
def from_config(cls, config):
152+
"""Deserialize this class from a config dict."""
147153
return cls(**config)
148154

149155
def get_config(self):
156+
"""Serialize this class to a config dict."""
150157
config = super(QDense, self).get_config()
151158
config.update(
152159
{
@@ -168,6 +175,7 @@ def get_config(self):
168175

169176

170177
def initialize_int8_dense(fp32_layer, q_config):
178+
"""Initialize int8 dense."""
171179
kwargs = fp32_layer.get_config()
172180

173181
param_list = [

neural_compressor/tensorflow/keras/layers/depthwise_conv2d.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1515
# See the License for the specific language governing permissions and
1616
# limitations under the License.
17+
"""Initialize custom depthwise conv2d layers for Keras quantization."""
1718

1819
import json
1920

@@ -35,6 +36,8 @@
3536
if version1_gte_version2(tf.__version__, "2.16.1"):
3637

3738
class QDepthwiseConv2D(BaseDepthwiseConv): # pragma: no cover
39+
"""The custom quantized DepthwiseConv2D layer."""
40+
3841
def __init__(
3942
self,
4043
kernel_size,
@@ -65,6 +68,7 @@ def __init__(
6568
quant_axis=None,
6669
**kwargs
6770
):
71+
"""Initialize custom quantized DepthwiseConv2D layer."""
6872
super().__init__(
6973
2,
7074
kernel_size=kernel_size,
@@ -98,6 +102,7 @@ def __init__(
98102
self.quant_axis = quant_axis
99103

100104
def call(self, inputs):
105+
"""The __call__ function of custom quantized DepthwiseConv2D layer."""
101106
if self.quant_status == "calib" and not isinstance(inputs, tf.keras.KerasTensor):
102107
if self.granularity == "per_tensor":
103108
self.act_min_value = tf.math.reduce_min(inputs)
@@ -165,9 +170,11 @@ def call(self, inputs):
165170

166171
@classmethod
167172
def from_config(cls, config):
173+
"""Deserialize this class from a config dict."""
168174
return cls(**config)
169175

170176
def get_config(self):
177+
"""Serialize this class to a config dict."""
171178
config = super(QDepthwiseConv2D, self).get_config()
172179
config.update(
173180
{
@@ -190,6 +197,8 @@ def get_config(self):
190197
else:
191198

192199
class QDepthwiseConv2D(DepthwiseConv):
200+
"""The custom quantized DepthwiseConv2D layer."""
201+
193202
def __init__(
194203
self,
195204
kernel_size,
@@ -220,6 +229,7 @@ def __init__(
220229
quant_axis=None,
221230
**kwargs
222231
):
232+
"""Initialize custom quantized DepthwiseConv2D layer."""
223233
super().__init__(
224234
2,
225235
kernel_size=kernel_size,
@@ -253,6 +263,7 @@ def __init__(
253263
self.quant_axis = quant_axis
254264

255265
def call(self, inputs):
266+
"""The __call__ function of custom quantized DepthwiseConv2D layer."""
256267
if self.quant_status == "calib":
257268
if self.granularity == "per_tensor":
258269
self.act_min_value = tf.math.reduce_min(inputs)
@@ -315,9 +326,11 @@ def call(self, inputs):
315326

316327
@classmethod
317328
def from_config(cls, config):
329+
"""Deserialize this class from a config dict."""
318330
return cls(**config)
319331

320332
def get_config(self):
333+
"""Serialize this class to a config dict."""
321334
config = super(QDepthwiseConv2D, self).get_config()
322335
config.update(
323336
{
@@ -339,6 +352,7 @@ def get_config(self):
339352

340353
@tf_utils.shape_type_conversion
341354
def compute_output_shape(self, input_shape):
355+
"""Compute_output_shape."""
342356
if self.data_format == "channels_first":
343357
rows = input_shape[2]
344358
cols = input_shape[3]
@@ -369,6 +383,7 @@ def compute_output_shape(self, input_shape):
369383

370384

371385
def initialize_int8_depthwise_conv2d(fp32_layer, q_config):
386+
"""Initialize int8 depthwise conv2d."""
372387
kwargs = fp32_layer.get_config()
373388
q_name = fp32_layer.name
374389

neural_compressor/tensorflow/keras/layers/layer_initializer.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1515
# See the License for the specific language governing permissions and
1616
# limitations under the License.
17+
"""Initialize layer initializer functions."""
1718

1819
from neural_compressor.tensorflow.keras.layers.conv2d import initialize_int8_conv2d
1920
from neural_compressor.tensorflow.keras.layers.dense import initialize_int8_dense

0 commit comments

Comments
 (0)