Lab 2 for Advanced Deep Learning Systems (ADLS)#
ELEC70109/EE9-AML3-10/EE9-AO25
Written by
Aaron Zhao ,
Cheng Zhang ,
Pedro Gimenes
General introduction#
In this lab, you will learn how to use analysis and transform pass system in the software stack of MASE.
There are in total 7 tasks you would need to finish, and also 1 optional task.
Turning you network to a graph#
One specific feature of MASE is its capability to transform DL models to a computation graph using the torch.fx framework.
In this section, we will look at how to transform a model to a
MASEGraph
, starting from the
Notebook.
Once you finished the notebook, consider the following problems:
Explain the functionality of
report_graph_analysis_pass
and its printed jargons such asplaceholder
,get_attr
… You might find the doc of torch.fx useful.What are the functionalities of
profile_statistics_analysis_pass
andreport_node_meta_param_analysis_pass
respectively?
MASE OPs and MASE Types#
MASE is designed to be a very high-level intermediate representation (IR), this is very different from the classic LLVM IR that you might be familiar with.
The following MASE Types are available:
module_related_func
: MASE module_realted_func includes functions undertorch.nn.functional
and thetorch.nn.Module
that wrapps them. For example,torch.nn.functional.relu
andtorch.nn.ReLU
are both MASE module_related_func.module
: a MASE module is a subclass oftorch.nn.Module
that does not have correspondingtorch.nn.functional
counterpart. For example,torch.nn.BatchNorm2D
is a MASE module becausetorch.nn.functional.batch_norm_2d
does not exist.builtin_func
: MASE builtin_func includes functions undertorch
that are nottorch.nn.functional
andtorch.nn.Module
. For example,torch.cat
andtorch.bmm
are both MASE builtin_func.placeholder
: a MASE placeholder is the input node of a MASEGraph, i.e., a proxy of input tensor to the network. This type inherits from torch.fx.get_attr
: a MASE get_attr is a node that represents the attribute of a MASE module. This type inherits from torch.fx. An example isself.scale * x
in aforward
function whereself.scale
is user-definedtorch.nn.Parameter
andx
is an intermediate tensor.output
: a MASE output is the output node of a MASEGraph, i.e., a proxy of output tensor to the network. This type also inherits from torch.fx.
You may find more clue in this file.
A deeper dive into the quantisation transform#
Explain why only 1 OP is changed after the
quantize_transform_pass
.Write some code to traverse both
mg
andori_mg
, check and comment on the nodes in these two graphs. You might find the source code for the implementation ofsummarize_quantization_analysis_pass
useful.Perform the same quantisation flow to the bigger JSC network that you have trained in lab1. You must be aware that now the
pass_args
for your custom network might be different if you have used more than theLinear
layer in your network.Write code to show and verify that the weights of these layers are indeed quantised. You might need to go through the source code of the implementation of the quantisation pass and also the implementation of the Quantized Layers .
The command line interface#
The same flow can also be executed on the command line throw the
transform
action.
# make sure you have the same printout
pwd
# it should show
# your_dir/mase-tools/machop
# enter the following command
./ch transform --config configs/examples/jsc_toy_by_type.toml --task cls --cpu=0
Load your own pre-trained JSC network, and perform perform the quantisation using the command line interface.
Optional Task: Write your own pass#
Many examples of existing passes are in the source code, the test files for these passes also contain useful information on helping you to understand how these passes are used.
Implement a pass to count the number of FLOPs (floating-point operations) and BitOPs (bit-wise operations).