Skip to content

Commit

Permalink
Added documentation and error checking for fake quantization
Browse files Browse the repository at this point in the history
  • Loading branch information
ranbir-sharma committed Sep 8, 2024
1 parent c8f2a8d commit 1efd649
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
20 changes: 16 additions & 4 deletions bin/instrument.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
# Everytime the contents of compileOption is changed in input.yaml
# this script should be run to create new fi.exe and prof.exe

import sys, os, shutil
import sys, os, shutil, math
import yaml
import subprocess

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

if 'fakeQuant' in cOpt and (cOpt['fakeQuant']['targetLayer'] == 'conv' or cOpt['fakeQuant']['targetLayer'] == 'matmul'):
if 'fakeQuant' in cOpt:
targetLayer = cOpt['fakeQuant']['targetLayer']
minPercentileOutlierThreshold = cOpt['fakeQuant'].get('minPercentileOutlierThreshold', 0)
maxPercentileOutlierThreshold = cOpt['fakeQuant'].get('maxPercentileOutlierThreshold', 100)
bitWidth = cOpt['fakeQuant'].get('bitWidth')
bitWidth = cOpt['fakeQuant'].get('bitWidth', 8)

assert isinstance(targetLayer, str), "TargetLayer must be of type string"
assert targetLayer == "conv" or targetLayer == "matmul" , "TargetLayer can only be 'conv' and 'matmul'"
assert isinstance(minPercentileOutlierThreshold, int), "Minimum Percentile Value should be of type int"
assert isinstance(maxPercentileOutlierThreshold, int), "Maximum Percentile Value should be of type int"
assert isinstance(bitWidth, int), "BitWidth should be of type int"
assert 0 < bitWidth, "BitWidth must be a integer greater than 0"
assert math.log2(bitWidth).is_integer(), "BitWidth must be a exponent of power 2"
assert 0 <= minPercentileOutlierThreshold and minPercentileOutlierThreshold <= 100, "Minimum Percentile Value for Percentile should be greater than or equal to 0"
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"
assert minPercentileOutlierThreshold <= maxPercentileOutlierThreshold, "Minimum Percentile Value should be lesser than Maximum Percentile Value"

global fakeQuant
fakeQuant = [cOpt['fakeQuant']['targetLayer'], minPercentileOutlierThreshold, maxPercentileOutlierThreshold, bitWidth]
fakeQuant = [targetLayer, minPercentileOutlierThreshold, maxPercentileOutlierThreshold, bitWidth]

################################################################################
def _suffixOfIR():
Expand Down
22 changes: 22 additions & 0 deletions docs/input_masterlist_ml.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,25 @@ runOption:

kernelOption:
- forceRun

# To use Fake Quantization within LLTFI, you need to specify the target layer that would be Quantized
# There is a parameter minPercentileOutlierThreshold and maxPercentileOutlierThreshold which eliminates outliers within the tensor input
# However, this parameter needs to be mentioned by the user and is not automatically set
# When set, the values permitted within the input tensor must satisfy this condition
# (floating number at minPercentileOutlierThreshold) <= input tensor <= (floating number at maxPercentileOutlierThreshold)
# Values that dont satisfy this condition are shifted to the (floating number at minPercentileOutlierThreshold) or (floating number at maxPercentileOutlierThreshold),
# whichever is the nearest
# One sample code is -

compileOption:
fakeQuant:
targetLayer: conv
minPercentileOutlierThreshold : 10
maxPercentileOutlierThreshold : 90
bitWidth: 8


# Fake Qunatization parameters comes within compileOption. In here, you need to mention the targer layer - conv or matmul (currently only supporting these)
# minPercentileOutlierThreshold and maxPercentileOutlierThreshold are optional parameters. On not mentioning these parameters, they are autoamtically set to
# minPercentileOutlierThreshold = 0 and maxPercentileOutlierThreshold = 100
# bitWidth needs to be mentioned and is the bit width of the Quantized Int numbers which needs to be a exponent of power 2
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ compileOption:
regSelMethod: regloc
regloc: dstreg

# Please uncomment this if you need to use the Fake Quantization Feature within LLTFI
# fakeQuant:
# targetLayer: conv
# minPercentileOutlierThreshold : 10
# maxPercentileOutlierThreshold : 90
# bitWidth: 16

includeInjectionTrace:
- forward

Expand Down

0 comments on commit 1efd649

Please sign in to comment.