Skip to content

Mrt v2 #81

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 158 commits into from
Nov 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
158 commits
Select commit Hold shift + click to select a range
e978a11
Merge remote-tracking branch 'origin/wlt' into ryt_tune
declmal Aug 4, 2020
6a29b77
Merge branch 'ryt_tune' of https://github.com/CortexFoundation/cvm-ru…
declmal Aug 5, 2020
d49514d
Merge remote-tracking branch 'origin/wlt' into ryt_tune
declmal Aug 10, 2020
3a28325
update feature module: optimizor, sampler, feature data types
declmal Aug 12, 2020
e6e6433
Merge remote-tracking branch 'origin/wlt' into archRev
declmal Aug 12, 2020
241c336
update module
declmal Aug 12, 2020
33c56f6
update sym_config_infos deserialize
declmal Aug 12, 2020
41eb1bd
update sym_calibrate_gen
declmal Aug 12, 2020
8a99202
Merge remote-tracking branch 'origin/wlt' into archRev
declmal Aug 12, 2020
3b94d30
enhance framework for quantize
declmal Aug 13, 2020
d8dc1ed
update
declmal Aug 13, 2020
c05af4d
update scale and quantizer
declmal Aug 15, 2020
65d9a55
update doc for quantization
declmal Aug 15, 2020
7aff300
Merge remote-tracking branch 'origin/wlt' into archRev
declmal Aug 15, 2020
4be515e
update
declmal Aug 16, 2020
873636a
update
declmal Aug 17, 2020
f09b5c7
update exp formulation for Convolutiion
declmal Aug 17, 2020
bc2dd6b
update generalized conv quantization formalization
declmal Aug 18, 2020
0f91ba3
update form: nn ops
declmal Aug 19, 2020
e614164
update doc
declmal Aug 19, 2020
c6a265d
update form
declmal Aug 19, 2020
eab1123
update
declmal Aug 19, 2020
ff5f1a1
correct formalization
declmal Aug 20, 2020
1e91b36
update
declmal Aug 20, 2020
3712f02
update types
declmal Aug 20, 2020
1fbabbf
update quantization interfaces
declmal Aug 20, 2020
f6cc3f9
quantize interfaces
declmal Aug 21, 2020
e5fe280
update
declmal Aug 21, 2020
1213bfa
update QuantInfo
declmal Aug 21, 2020
cb7b0ef
update
declmal Aug 21, 2020
81ccb0a
update quantizers
declmal Aug 24, 2020
511e61c
update
declmal Aug 24, 2020
d61354e
update quantizer
declmal Aug 25, 2020
9b803b7
create a new module: mrt.gen
declmal Aug 26, 2020
7552588
add user interface for mrt.gen
declmal Aug 26, 2020
68946a3
update
declmal Aug 26, 2020
c65ef56
update transformer
declmal Aug 26, 2020
2e060bf
rename
declmal Aug 26, 2020
518f209
update
declmal Aug 26, 2020
896cee0
Merge remote-tracking branch 'origin/ryt_tune' into archRev
declmal Aug 26, 2020
6f92b20
update
declmal Aug 26, 2020
7687298
update
declmal Aug 27, 2020
5c793cd
update example ini
declmal Aug 27, 2020
6998902
enable calibration
declmal Aug 27, 2020
4be9177
fix bugs
declmal Aug 27, 2020
273bf5a
update arch for quantize tfm_ops
declmal Aug 27, 2020
b2b3246
udpate
declmal Aug 27, 2020
180de1f
update
declmal Aug 27, 2020
5200697
update doc
declmal Aug 27, 2020
51d73aa
update
declmal Aug 27, 2020
4baed0a
update
declmal Aug 28, 2020
e5cf8d0
enable quantization
declmal Aug 28, 2020
477f14b
update form
declmal Aug 28, 2020
12fcda2
update
declmal Aug 28, 2020
0e5d194
Merge branch 'archRev' of github.com:CortexFoundation/cvm-runtime int…
declmal Aug 28, 2020
e157170
enable evaluation
declmal Aug 29, 2020
1ae7077
fix bugs for alexnet
declmal Aug 29, 2020
31c66cb
update
declmal Aug 29, 2020
471c2ef
enable slice_channel for granularity control in quantization
declmal Aug 29, 2020
117b4e9
update sym_slice
declmal Aug 31, 2020
bd0d8df
enable channel-wise quantization
declmal Aug 31, 2020
a13d6a6
unit for conv channel-wise
declmal Aug 31, 2020
5a5f583
fix bug for channel-wise Convolution
declmal Sep 1, 2020
d18a2fc
arch rev
wlt-cortex Sep 4, 2020
b407ccc
fix bug
declmal Sep 4, 2020
48da233
update
declmal Sep 4, 2020
ba55984
update
declmal Sep 7, 2020
f770e17
enable mobilenet-v1-0.25-224-lite
declmal Sep 7, 2020
250722a
enable multiple step in slicechannel
declmal Sep 7, 2020
83ac7e5
Merge branch 'archRev' of github.com:CortexFoundation/cvm-runtime int…
declmal Sep 7, 2020
4e37aab
enable restore in gen
declmal Sep 7, 2020
e0d6e42
update
declmal Sep 7, 2020
dc774b1
update
declmal Sep 8, 2020
aaaefa7
update
declmal Sep 8, 2020
031ad90
Merge remote-tracking branch 'origin/wlt' into archRev
declmal Sep 8, 2020
cecd049
update cvm_op
declmal Sep 9, 2020
3cb05c2
update channelconv
declmal Sep 9, 2020
161e0f7
fix
declmal Sep 9, 2020
856b226
update types
declmal Sep 9, 2020
8413452
update
declmal Sep 11, 2020
c87ce80
update
declmal Sep 12, 2020
a121275
Merge remote-tracking branch 'origin/wlt' into archRev
declmal Sep 12, 2020
0ec7f46
update
declmal Sep 12, 2020
a0364b8
update
declmal Sep 12, 2020
73e0a64
update channelwise conv test
declmal Sep 12, 2020
91a4c89
updatE
declmal Sep 12, 2020
b5178e9
update
declmal Sep 12, 2020
c16a8d8
fix slice_channel bugs for groupwise conv
declmal Sep 14, 2020
4e14a96
update
declmal Sep 14, 2020
331796e
update zp
declmal Sep 14, 2020
e994ed0
update doc gen
declmal Sep 14, 2020
b1b848c
uypdate
declmal Sep 14, 2020
5468479
enable zero point
declmal Sep 15, 2020
15caab1
update
declmal Sep 15, 2020
64d46a5
enable zp for broadcast_add
declmal Sep 16, 2020
60e944e
Merge branch 'zp' of github.com:CortexFoundation/cvm-runtime into zp
declmal Sep 16, 2020
317bd3d
update
declmal Sep 16, 2020
6d7f871
update
declmal Sep 16, 2020
165d167
weigt only zero point quantization
declmal Sep 16, 2020
115c822
Merge pull request #76 from CortexFoundation/zp
declmal Sep 16, 2020
1b07994
update
declmal Sep 16, 2020
84b485d
enable resnet50_v1
declmal Sep 18, 2020
54f4aa0
enable resnet50_v2
declmal Sep 18, 2020
0fcd4ea
enable resnet18_v1
declmal Sep 18, 2020
d21c6b9
enable quickdraww resnet18_v1b_0.89
declmal Sep 18, 2020
596369a
enable qd10_resnetv1_20
declmal Sep 18, 2020
f88d844
enable densenet161
declmal Sep 18, 2020
3c019d7
enable cifar_resnet20_v1
declmal Sep 18, 2020
e256374
enable mobilenet1_0
declmal Sep 18, 2020
1cad85f
enable mobilenetv2_1.0
declmal Sep 18, 2020
31dafff
enabel shufflenet
declmal Sep 18, 2020
11b853e
enable squeezenet1.0 tf_inception_v3 vgg19
declmal Sep 18, 2020
b795c98
enable trec:
declmal Sep 18, 2020
1991cb2
enable mnist
declmal Sep 21, 2020
a47e0f8
enable yolo3_darknet53_voc
declmal Sep 21, 2020
f5d0d20
enable yolo3_mobilenet1.0_voc
declmal Sep 21, 2020
aab6018
enable ssd_512_resnet50_v1_voc
declmal Sep 21, 2020
f91f731
enable ssd_512_mobilenet1.0_voc
declmal Sep 21, 2020
54d533e
enable tf models
declmal Sep 21, 2020
cc4b407
update
declmal Sep 21, 2020
0d76ffe
add slice channel settings
declmal Sep 21, 2020
f15c6db
update docs for mrt.gen.tfm_types
declmal Sep 21, 2020
3e7e509
update
declmal Sep 21, 2020
cd7b7b4
update docs for tfm_types
declmal Sep 23, 2020
b4f9b6b
update for tfm_pass
declmal Sep 23, 2020
45ae8d9
update doc for gen tfm_base
declmal Sep 23, 2020
e36c6c4
update docs for quantize tutorial
declmal Sep 23, 2020
006ca84
Merge pull request #78 from CortexFoundation/ryt_doc
declmal Sep 23, 2020
4b05904
update accuracy comparison doc
declmal Sep 25, 2020
17236cf
update
declmal Sep 25, 2020
2c3f910
update
declmal Oct 16, 2020
b5c698e
update cvm_right_shift_channel
declmal Oct 19, 2020
7bb9660
update cvm_conv_2d_channel
declmal Oct 19, 2020
1ca7c37
update cvm_conv_2d_channel prop
declmal Oct 19, 2020
feb5240
revise
declmal Oct 19, 2020
a8c35cb
update
declmal Oct 19, 2020
5bf4922
update test for slicechannel
declmal Nov 10, 2020
6b70df1
update slicechannel: fix bug for precision
declmal Nov 10, 2020
ac634a7
update
declmal Nov 10, 2020
d0c0215
update
declmal Nov 10, 2020
1cb4f7c
update
declmal Nov 11, 2020
f9dcb86
update generalized step conv
declmal Nov 11, 2020
4a34541
update
declmal Nov 13, 2020
190013a
add mobilenet1.0
declmal Nov 13, 2020
943a33b
update
declmal Nov 13, 2020
fdb5c94
update
declmal Nov 13, 2020
1557568
rename gen to v2
declmal Nov 13, 2020
6566776
rename to V2
declmal Nov 13, 2020
b51122e
update
declmal Nov 13, 2020
f561909
update
declmal Nov 13, 2020
90b5464
update transformer
declmal Nov 16, 2020
597e589
fix bug for mrt feature get_threshold
declmal Nov 16, 2020
5838897
fix bugs for exactly
declmal Nov 16, 2020
c030527
update realize_ch
declmal Nov 17, 2020
dfa9834
update realize_ch
declmal Nov 17, 2020
c765fc0
enable channelwise quantization
declmal Nov 17, 2020
20178ac
update
declmal Nov 17, 2020
b6aa1c7
fix conflict with dev
declmal Nov 17, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
557 changes: 557 additions & 0 deletions docs/mrt/V2.md

Large diffs are not rendered by default.

91 changes: 91 additions & 0 deletions docs/mrt/api/gen.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@

********************************
MRT Generalized Quantization API
********************************

.. contents::

.. _mrt_gen_api:


Quantizer API
-------------
.. automodule:: mrt.V2.tfm_types


.. autoclass:: mrt.V2.tfm_types.Feature
:members:


.. autoclass:: mrt.V2.tfm_types.AFeature


.. autoclass:: mrt.V2.tfm_types.MMFeature


.. autoclass:: mrt.V2.tfm_types.Buffer
:members:


.. autoclass:: mrt.V2.tfm_types.SBuffer


.. autoclass:: mrt.V2.tfm_types.SZBuffer


.. autoclass:: mrt.V2.tfm_types.Quantizer
:members:


.. autoclass:: mrt.V2.tfm_types.USQuantizer


.. autoclass:: mrt.V2.tfm_types.UAQuantizer


.. autoclass:: mrt.V2.tfm_types.Optimizor
:members:


.. autoclass:: HVOptimizor


.. autoclass:: MAOptimizor


.. autoclass:: KLDOptimizor


Graph API
---------
.. automodule:: mrt.V2.tfm_pass


.. autofunction:: mrt.V2.tfm_pass.sym_config_infos


.. autofunction:: mrt.V2.tfm_pass.deserialize


.. autofunction:: mrt.V2.tfm_pass.sym_calibrate


.. autofunction:: mrt.V2.tfm_pass.sym_separate_pad


.. autofunction:: mrt.V2.tfm_pass.sym_separate_bias


.. autofunction:: mrt.V2.tfm_pass.sym_slice_channel


.. autofunction:: mrt.V2.tfm_pass.quantize


Transformer API
---------------
.. automodule:: mrt.V2.tfm_base


.. autoclass:: mrt.V2.tfm_base.Transformer
:members: quantize, slice_channel
11 changes: 8 additions & 3 deletions docs/mrt/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Mnist Tutorial
:maxdepth: 3

mnist_tutorial.md
gen.md


API Documentation
Expand All @@ -62,6 +63,10 @@ Deprecated Docs
.. toctree::
:maxdepth: 2

README.md
mrt.md

MRT Main2 API <api/main2>
MRT Transformer API <api/transformer>
MRT Operator API <api/operator>
MRT Graph API <api/graph>
MRT Dataset API <api/dataset>
MRT Utils API <api/utils>
MRT Generalized Quantization API <api/gen>
3 changes: 3 additions & 0 deletions python/mrt/V2/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from . import transformer
from . import tfm_ops
from . import tfm_types
196 changes: 196 additions & 0 deletions python/mrt/V2/cvm_op.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
import mxnet as mx
from mxnet import ndarray as nd

import numpy as np


class RightShiftChannel(mx.operator.CustomOp):
def __init__(self, precs, sbs, **kwargs):
super(RightShiftChannel, self).__init__(**kwargs)
precs = [eval(prec) for prec in precs.split(',')]
sbs = [eval(sb) for sb in sbs.split(',')]
assert len(precs) == len(sbs), \
"invalid length, length of precs: {}, length of sbs: {}".format( \
(precs, sbs))
clips = [2**(int(prec)-1)-1 for prec in precs]
self.mins = [int(-clip) for clip in clips]
self.maxs = [int(clip) for clip in clips]
self.sbs = [int(sb) for sb in sbs]
assert all([sb > 0 for sb in self.sbs])

def forward(self, is_train, req, in_data, out_data, aux):
assert is_train == False
X = in_data[0]
amins, amaxs = self.mins, self.maxs
out = X.round()
num_channel = X.shape[1]
assert num_channel % len(amins) == 0, \
"num_channel: {} is not divisible by len(amins): {}".format( \
(num_channel, len(amins)))
inc = num_channel // len(amins)
for i in range(0, num_channel, inc):
j = i + inc
k = i // inc
if self.sbs[k] > 1:
out[:,i:j] = out[:,i:j] / (2 ** (self.sbs[k]-1))
out[:,i:j] = out[:,i:j].floor()
out[:,i:j] = out[:,i:j] + 1
out[:,i:j] = out[:,i:j] / 2
out[:,i:j] = out[:,i:j].floor()
out[:,i:j] = out[:,i:j].clip(a_min=amins[k], a_max=amaxs[k])
self.assign(out_data[0], req[0], out)

def backward(self, req, out_grad, in_data, out_data, in_grad, aux):
assert False


class Conv2DChannel(mx.operator.CustomOp):
def __init__(
self, dilate, kernel, layout, no_bias, num_filter,
num_group, pad, stride, inc, **kwargs):
super(RightShiftChannel, self).__init__(**kwargs)
self.dilate = eval(dilate)
self.kernel = eval(kernel)
self.layout = layout
self.no_boas = eval(no_bias)
self.num_filter = eval(num_filter)
self.num_group = eval(num_group)
self.pad = eval(pad)
self.stride = eval(stride)
self.inc = eval(inc)

def forward(self, is_train, req, in_data, out_data, aux):
assert is_train == False
n_batch, in_channels, x_h, x_W = in_data[0].shape
oshp = out_data[0].shape
print(oshp)
# TODO(archRev): realize

def backward(self, req, out_grad, in_data, out_data, in_grad, aux):
assert False


@mx.operator.register("cvm_right_shift_channel")
class RightShiftChannelProp(mx.operator.CustomOpProp):
def __init__(self, precs, sbs):
self.precs = precs
self.sbs = sbs
super(RightShiftChannelProp, self).__init__(need_top_grad=True)
def list_arguments(self):
return ['data']
def list_outputs(self):
return ['output']
def infer_shape(self, in_shape):
X_shape = in_shape[0]
out_shape = in_shape[0]
return [X_shape], [out_shape], []
def infer_type(self, in_type):
X_type = in_type[0]
return [X_type], [X_type], []
def create_operator(self, ctx, shapes, dtypes):
return RightShiftChannel(self.precs, self.sbs)


@mx.operator.register("cvm_conv2d_channel")
class Conv2DChannelProp(mx.operator.CustomOpProp):
def __init__(
self, dilate, kernel, layout, no_bias,
num_filter, num_group, pad, stride, inc):
self.dilate = dilate
self.kernel = kernel
self.layout = layout
self.no_bias = no_bias
self.num_filter = num_filter
self.num_group = num_group
self.pad = pad
self.stride = stride
self.inc = inc
super(Conv2DChannelProp, self).__init__(need_top_grad=True)
def list_arguments(self):
no_bias = eval(self.no_bias)
return ['data', 'weight'] if no_bias else ['data', 'weight', 'bias']
def list_outputs(self, in_shape):
return ['output']
def infer_shape(self, in_shape):
# verify layout
assert self.layout == "NCHW", \
"cvm_conv2d_channel only supports layout: NCHW vs {}".format(
self.layout)
# verify shapes
no_bias = eval(no_bias)
if no_bias == True:
assert len(in_shape) == 2, \
"invalid in_shape: {}".format(in_shape)
else:
assert len(in_shape) == 3, \
"invalid in_shape: {}".format(in_shape)
X_shape, W_shape = in_shape
assert len(X_shape) == 4, \
"input data should be 4d: {}".format(X_shape)
for attr, name in [
(self.kernel, "kernel"), (self.pad, "pad"),
(self.strides, "strides"), (self.dilate, "dilate"),
]:
attr = eval(attr)
assert len(attr) == 2, \
"invalid attr, name: {}, value: {}".format((name, attr))
verify_attr_range(attr[0], name)
verify_attr_range(attr[1], name)
assert len(W_shape) == 4, \
"input weight should be 4d: {}".format(W_shape)
# verify num_group
num_group = eval(self.num_group)
assert num_group > 0 and \
X_shape[1] % num_group == 0 and W_shape[0] % num_group == 0, \
"invalid num_group: {}, X_shape: {}, W_shape: {}".format( \
(num_group, X_shape, W_shape))
# verify kernel shape
KH, KW = eval(self.kernel)
assert KH == W_shape[2] and KW == W_shape[3], \
"invalid kernel attr, kernel: {}, W_shape: {}".format( \
(self.kernel, W_shape))
DH, DW = eval(self.dilate)
DH_size = 1 + (KH-1)*DH
DW_size = 1 + (KW-1)*DW
PH, PW = eval(self.pad)
assert DH_size < H+2*PH and DW_size < W+2*PW, \
"invalid kernel attr, kernel: {}, pad: {}, dilate: {}".format( \
(self.kernel, self.pad, self.dilate))
# num_group
# TODO(archRev): case when num_group > 1
num_group = eval(self.num_group)
assert num_group == 1, "invalid attr num_group: {}".format(num_group)
num_channel = X_shape[1]
assert W_shape[1] == X_shape[1], "input channel size: {} and " + \
"weight channel size: {} not consistant".format( \
(X_shape[1], W_shape[1]))
inc = eval(self.inc)
assert num_channel % inc == 0, \
"the number of channels: {} must be divisible by inc: {}".format( \
(num_channel, inc))
# output shape
out_shape = [X_shape[0], W_shape[0], num_channel//inc, 0, 0]
SH, SW = eval(self.stride)
H, W = in_shape[2:]
if H != 0:
out_shape[3] = (H+PH*2-DH_size) // SH + 1
if W != 0:
out_shape[4] = (W+PW*2-DW_size) // SW + 1
out_shape = tuple(out_shape)
return [X_shape, W_shape], [out_shape], []
def infer_type(self, in_type):
X_type = in_type[0]
W_type = in_type[1]
return [X_type, B_type], [X_type], []
def create_operator(self, ctx, shapes, dtypes):
return Conv2DChannel(
self.dilate, self.kernel ,self.layout, self.no_bias,
self.num_filter, self.num_group, self.pad, self.stride, self.inc)

ATTR_MIN_VALUE = 0
ATTR_MAX_VALUE = 4096

def verify_attr_range(val, name, minv=ATTR_MIN_VALUE, maxv=ATTR_MAX_VALUE):
assert minv <= val <= maxv, \
"val: {} not in valid range, name: {}, minv: {}, maxv: {}".format(
(val, name, minv, maxv))
Loading