[RELAY][PASS] add a relay pass to count #macs of a model#2609
[RELAY][PASS] add a relay pass to count #macs of a model#2609yzhliu merged 14 commits intoapache:masterfrom
Conversation
|
Currently the number of real MACs can be dependent on the algorithm used (e.g., when winograd is suitable). Do we plan to consider these cases? |
Yes, we can gradually improve the coverage. |
src/relay/pass/mac_count.cc
Outdated
| Array<Expr> args = call_node->args; | ||
| CHECK(args.size() == 2) | ||
| << "The number of input arguments of a CONV 2D node should be 2."; | ||
| const auto* expr0 = args[0]->checked_type().as<TensorTypeNode>(); |
There was a problem hiding this comment.
expr0 -> type? because it's actually a type node instead of an expr node.
| return 0; | ||
| } | ||
| Array<Expr> args = call_node->args; | ||
| CHECK(args.size() == 2) |
There was a problem hiding this comment.
I am not sure if we still need these checks. Are they supposed to be valid already since we have already inferred types.
There was a problem hiding this comment.
I think it wouldn't hurt. This code can still run with warning if the infer type pass is not called.
src/relay/pass/mac_count.cc
Outdated
| Array<Expr> args = call_node->args; | ||
| CHECK(args.size() == 2) | ||
| << "The number of input arguments of a Dense node should be 2."; | ||
| const auto* expr0 = args[0]->checked_type().as<TensorTypeNode>(); |
There was a problem hiding this comment.
same as above, probably more descriptive name, like data_type and weight_type
src/relay/pass/mac_count.cc
Outdated
|
|
||
| int64_t GetCartesianProd(Array<IndexExpr> arr) { | ||
| int64_t ret = 1; | ||
| for (int i = 0; i < arr.size(); i++) { |
There was a problem hiding this comment.
int -> size_t since arr.size() is size_t or probably just for (const auto& it : arr)
| dshape2 = (m, k) | ||
| data1 = relay.var("data1", shape=dshape1) | ||
| data2 = relay.var("data2", shape=dshape2) | ||
| gemm = relay.nn.dense( |
| import tvm | ||
| from tvm import relay | ||
| import sys | ||
| sys.path.append('../frontend/mxnet') |
There was a problem hiding this comment.
Sorry, why do we need to add it to the system path?
There was a problem hiding this comment.
This is to import the model_zoo from this path correctly. I am happy to change to a more elegant solution if any suggestion.
There was a problem hiding this comment.
I actually suggest to remove the resnet test, as no one can verify the correctness of the test by eyeball. And later if the algorithm changes or enhanced, it will also be a pain to deal with such test.
src/relay/pass/mac_count.cc
Outdated
| class MacCounter : private ExprVisitor { | ||
| public: | ||
| MacCounter() { | ||
| count_ = 0; |
src/relay/pass/mac_count.cc
Outdated
| const auto* data_type = args[0]->checked_type().as<TensorTypeNode>(); | ||
| Array<IndexExpr> data_shape = data_type->shape; | ||
| CHECK(data_shape.size() == 4) | ||
| << "The dimension of an input tensor to Dense node should be 4."; |
There was a problem hiding this comment.
| << "The dimension of an input tensor to Dense node should be 4."; | |
| << "The dimension of an input tensor to Conv2D node should be 4."; |
some non-4D conv implement also use Conv2dAttrs though... I think the dimension of input & output does not really matter.
There was a problem hiding this comment.
If a non-4D conv uses Conv2dAttrs, perhaps we should pick it out, otherwise the output wouldn't make sense?
| count_ += ComputeConv2DMacs(call_node); | ||
| } else if (IsDenseNode(call_node)) { | ||
| count_ += ComputeDenseMacs(call_node); | ||
| } |
There was a problem hiding this comment.
should we log somewhere to remind users the pass only calculates MAC for Conv2D and Dense?
There was a problem hiding this comment.
Added a log info in the beginning of the pass.
| int32_t C_ind = Layout(data_layout).Indexof('C'); | ||
| int32_t c_ind = Layout(data_layout).Indexof('c'); | ||
| CHECK(C_ind != -1 || c_ind != -1) | ||
| << "There is no input channel dimension."; |
|
LGTM |
|
Thanks, @yidawang @zhiics @eqy @yzhliu . Some followup comments would be nice to address as a followup:
|
* add a relay pass to count #macs of a model * remove unnecessary includes * fix end-of-file issues * address review comments * remove test resnet * address more review comments * use data layout string to locate the input channel * fix bug in conv 2d output tensor checking * C must exist
|
Thanks for the feedback, @tqchen As the MAC counting is an estimation, we only take those computationally-intensive ops into account. Therefore, I don't envision that we will extend it to many ops. We can definitely switch to your suggested attribute registration style if necessary. We will add new ops as needed. I will send out a quick PR to fix the dynamic cast issue soon. |
ATT. Currently it only counts the macs of direct CONV 2D and Dense.
@icemelon9 @zhiics @yzhliu