Skip to content

Commit 1efd649

Browse files
committed
Added documentation and error checking for fake quantization
1 parent c8f2a8d commit 1efd649

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

bin/instrument.py

+16-4
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
# Everytime the contents of compileOption is changed in input.yaml
2626
# this script should be run to create new fi.exe and prof.exe
2727

28-
import sys, os, shutil
28+
import sys, os, shutil, math
2929
import yaml
3030
import subprocess
3131

@@ -319,13 +319,25 @@ def readCompileOption():
319319
if (str(cOpt["tracingPropagationOption"]["generateCDFG"]).lower() == "true"):
320320
options["genDotGraph"] = True
321321

322-
if 'fakeQuant' in cOpt and (cOpt['fakeQuant']['targetLayer'] == 'conv' or cOpt['fakeQuant']['targetLayer'] == 'matmul'):
322+
if 'fakeQuant' in cOpt:
323+
targetLayer = cOpt['fakeQuant']['targetLayer']
323324
minPercentileOutlierThreshold = cOpt['fakeQuant'].get('minPercentileOutlierThreshold', 0)
324325
maxPercentileOutlierThreshold = cOpt['fakeQuant'].get('maxPercentileOutlierThreshold', 100)
325-
bitWidth = cOpt['fakeQuant'].get('bitWidth')
326+
bitWidth = cOpt['fakeQuant'].get('bitWidth', 8)
327+
328+
assert isinstance(targetLayer, str), "TargetLayer must be of type string"
329+
assert targetLayer == "conv" or targetLayer == "matmul" , "TargetLayer can only be 'conv' and 'matmul'"
330+
assert isinstance(minPercentileOutlierThreshold, int), "Minimum Percentile Value should be of type int"
331+
assert isinstance(maxPercentileOutlierThreshold, int), "Maximum Percentile Value should be of type int"
332+
assert isinstance(bitWidth, int), "BitWidth should be of type int"
333+
assert 0 < bitWidth, "BitWidth must be a integer greater than 0"
334+
assert math.log2(bitWidth).is_integer(), "BitWidth must be a exponent of power 2"
335+
assert 0 <= minPercentileOutlierThreshold and minPercentileOutlierThreshold <= 100, "Minimum Percentile Value for Percentile should be greater than or equal to 0"
336+
assert 0 <= maxPercentileOutlierThreshold and maxPercentileOutlierThreshold <= 100, "Maximum Percentile Value for Percentile should be greater than or equal to 0 and lesser than or equal to 100"
337+
assert minPercentileOutlierThreshold <= maxPercentileOutlierThreshold, "Minimum Percentile Value should be lesser than Maximum Percentile Value"
326338

327339
global fakeQuant
328-
fakeQuant = [cOpt['fakeQuant']['targetLayer'], minPercentileOutlierThreshold, maxPercentileOutlierThreshold, bitWidth]
340+
fakeQuant = [targetLayer, minPercentileOutlierThreshold, maxPercentileOutlierThreshold, bitWidth]
329341

330342
################################################################################
331343
def _suffixOfIR():

docs/input_masterlist_ml.yaml

+22
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,25 @@ runOption:
139139

140140
kernelOption:
141141
- forceRun
142+
143+
# To use Fake Quantization within LLTFI, you need to specify the target layer that would be Quantized
144+
# There is a parameter minPercentileOutlierThreshold and maxPercentileOutlierThreshold which eliminates outliers within the tensor input
145+
# However, this parameter needs to be mentioned by the user and is not automatically set
146+
# When set, the values permitted within the input tensor must satisfy this condition
147+
# (floating number at minPercentileOutlierThreshold) <= input tensor <= (floating number at maxPercentileOutlierThreshold)
148+
# Values that dont satisfy this condition are shifted to the (floating number at minPercentileOutlierThreshold) or (floating number at maxPercentileOutlierThreshold),
149+
# whichever is the nearest
150+
# One sample code is -
151+
152+
compileOption:
153+
fakeQuant:
154+
targetLayer: conv
155+
minPercentileOutlierThreshold : 10
156+
maxPercentileOutlierThreshold : 90
157+
bitWidth: 8
158+
159+
160+
# Fake Qunatization parameters comes within compileOption. In here, you need to mention the targer layer - conv or matmul (currently only supporting these)
161+
# minPercentileOutlierThreshold and maxPercentileOutlierThreshold are optional parameters. On not mentioning these parameters, they are autoamtically set to
162+
# minPercentileOutlierThreshold = 0 and maxPercentileOutlierThreshold = 100
163+
# bitWidth needs to be mentioned and is the bit width of the Quantized Int numbers which needs to be a exponent of power 2

sample_programs/ml_sample_programs/vision_models/cnn-fmnist/input.yaml

+7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ compileOption:
1010
regSelMethod: regloc
1111
regloc: dstreg
1212

13+
# Please uncomment this if you need to use the Fake Quantization Feature within LLTFI
14+
# fakeQuant:
15+
# targetLayer: conv
16+
# minPercentileOutlierThreshold : 10
17+
# maxPercentileOutlierThreshold : 90
18+
# bitWidth: 16
19+
1320
includeInjectionTrace:
1421
- forward
1522

0 commit comments

Comments
 (0)