From: 남궁석/On-Device Lab(SR)/Engineer/삼성전자 Date: Mon, 5 Aug 2019 07:52:23 +0000 (+0900) Subject: [moco-tf] Revise comments of FuseBinaryIntoPreceding (#6204) X-Git-Tag: submit/tizen/20190809.050447~176 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=5a50df6df2cae577d177c88d8f2eedb318d1b9f4;p=platform%2Fcore%2Fml%2Fnnfw.git [moco-tf] Revise comments of FuseBinaryIntoPreceding (#6204) * [moco-tf] Revise comments of FuseBinaryIntoPreceding This commit will revise some comments of `FuseBinaryIntoPreceding.cpp` Signed-off-by: Seok NamKoong * Apply comments --- diff --git a/compiler/moco-tf/src/Transforms/FuseBinaryIntoPreceding.cpp b/compiler/moco-tf/src/Transforms/FuseBinaryIntoPreceding.cpp index 65ecb17..f103505 100644 --- a/compiler/moco-tf/src/Transforms/FuseBinaryIntoPreceding.cpp +++ b/compiler/moco-tf/src/Transforms/FuseBinaryIntoPreceding.cpp @@ -135,35 +135,37 @@ moco::tf::TFConst *create_kernal_from_fuse_mulparam(loco::Graph *graph, moco::tf return ker_fused; } +/** + * @note This creates fused ker:2 from ker:1, 'mulparam' and + * new precedingOp:2 that uses ker:2 as the kernel. + * Then make C to use precedingOp:2 as new input. + * + * + * mulparam-\ + * ker:1 --\ \ + * ifm ----- precedingOp:1 ----------- Mul --- C + * + * + * + * mulparam-\ + * ker:1 --\ \ + * - precedingOp:1 ----------- Mul --- + * / + * ifm ----- precedingOp:2 ------------------- C + * ker:2 ---/ + * + * + * [Where] + * - precedingOp:1 can be one of TFConv2D, TFDepthwiseConv2dNative, FullyConnected + * - 'mulparam' and Mul will be disconnected from the Output. + * - ker:2 is added with fused values of ker:1 and mulparam + * - precedingOp:2 is added using ifm and ker:2 and other parameters + * same as precedingOp:1. + * - ker:1, precedingOp:1, 'mulparam' and Mul should be removed in + * RemoveDeadNodeTransform if not used. + */ bool fuse_to_preceding(loco::Graph *graph, moco::tf::TFMul *node) { - /** - * @note We'll create fused ker' from ker and mulparam and new TFConv2D' - * that uses ker' as the kernel and tell C to use TFConv2D' as new - * input. - * - * Before - * mulparam-\ - * ker -----\ \ - * ifm ----- TFConv2D ------ Mul --- C - * - * After - * mulparam-\ - * ker -----\ \ - * - TFConv2D ----- Mul --- - * / - * ifm ------ TFConv2D' ------------ C - * ker' -----/ - * - * Where - * mulparam and Mul will be disconnected from the Output. - * ker' is added with fused values of ker and mulparam - * TFConv2D' is added using ifm and ker' and other parameters - * same as TFConv2D. - * ker, TGConv2D, mulparam and Mul should be removed in - * RemoveDeadNodeTransform if not used. - */ - LOGGER(l); auto xc = dynamic_cast(node->x()); @@ -199,7 +201,7 @@ bool fuse_to_preceding(loco::Graph *graph, moco::tf::TFMul *node) if (ker == nullptr) { // Wait until ker is becomes TFConst: there are cases when it's Identity. - INFO(l) << "Mul fuse_to_preceding: preceding Conv2D ker is not TFConst"; + INFO(l) << "Mul fuse_to_preceding: precedingOp ker is not TFConst"; return false; } auto ifm = conv2d->ifm(); @@ -208,14 +210,14 @@ bool fuse_to_preceding(loco::Graph *graph, moco::tf::TFMul *node) // we need shape information, if not wait till it's ready if (ker->annot() == nullptr) { - INFO(l) << "Mul fuse_to_preceding: preceding Conv2D ker has no shape"; + INFO(l) << "Mul fuse_to_preceding: precedingOp ker has no shape"; return false; } auto mulparam_shape_inf = mulparam->annot(); if (mulparam_shape_inf == nullptr) { - INFO(l) << "Mul fuse_to_preceding: preceding Conv2D mulparam has no shape"; + INFO(l) << "Mul fuse_to_preceding: mulparam has no shape"; return false; } // if MulParam rank is not 1 we cannot fuse, just skip @@ -235,8 +237,8 @@ bool fuse_to_preceding(loco::Graph *graph, moco::tf::TFMul *node) conv2d_fused->data_layout(conv2d->data_layout()); conv2d_fused->strides(conv2d->strides()); - // Replace TFMul node with new TFConv2D with fused kernel - // This will leave existing TFConv2D as-is but can be removed if not used + // Replace TFMul node with new precedingOp with fused kernel + // This will leave existing precedingOp as-is but can be removed if not used // from other transformations replace(node).with(conv2d_fused); // TODO check if need to disconnect @@ -248,37 +250,44 @@ bool fuse_to_preceding(loco::Graph *graph, moco::tf::TFMul *node) return true; } +/** + * @note TFAdd will be fused to TFBiasAdd + * + * + * If precedingOp is not TFBiasAdd, then insert TFConst:1 + TFBiasAdd that + * TFConst:1 has zero values. + * + * addparam --\ + * \ + * precedingOp ---------------------------- TFAdd ----- C + * + * + * + * If it's TFBiasAdd and one of the input is TFConst type, + * then we can fuse 'addparam' to the input TFConst:2 value of TFBiasAdd, where + * TFConst:2 has added values from 'addparam' + * + * addparam --\ + * TFConst:1 --------\ \ + * precedingOp ------- TFBiasAdd ---------- TFAdd ----- C + * + * + * + * addparam --\ + * TFConst:2 --------\ \ + * precedingOp ------- TFBiasAdd ---------- TFAdd ----- + * \--------------------- C + * + * + * [Where] + * - precedingOp can be TFConv2D, TFDepthwiseConv2dNative, FullyConnected, + * TFBiasAdd. + * - Intermediate is to insert TFBiasAdd + TFConst:1 + * - After is to fuse 'addparam' of TFAdd into TFConst:1 + TFBiasAdd + * that becomes TFConst:2 + TFBiasAdd + */ bool fuse_to_preceding(loco::Graph *graph, moco::tf::TFAdd *node) { - /** - * @note Preceding can be TFConv2D, TFBiasAdd. - * If preceding is TFConv2D then insert TFConst:2 + TFBiasAdd like - * Intermediate with TFConst:2 having zero values. If it's TFBiasAdd, - * like Intermediate, and if one of the input is TFConst type then we - * can fuse 'addparam' to the input TFConst:3 value of TFBiasAdd, where - * TFConst:3 has added values from TFConst:1. - * - * Before - * TFConv2D -------------- TFAdd ----- C - * TFConst:1 --/ - * - * Intermediate - * TFConv2D -------- TFBiasAdd -------------- TFAdd ----- C - * TFConst:2 --/ / - * TFConst:1 --/ - * - * After - * ------------- TFBiasAdd ---------------- C - * TFConst:3 --/ \ - * \-- TFAdd -- - * TFConst:1 --/ - * - * Where - * Intermediate is to insert TFBiasAdd + TFConst:2 - * After is to fuse TFConst:1 of TFAdd into TFConst:2 + TFBiasAdd - * that becomes TFConst:3 + TFBiasAdd - */ - LOGGER(l); auto xc = dynamic_cast(node->x()); @@ -310,7 +319,7 @@ bool fuse_to_preceding(loco::Graph *graph, moco::tf::TFAdd *node) auto addparam_shape_inf = addparam->annot(); if (addparam_shape_inf == nullptr) { - INFO(l) << "Add fuse_to_preceding: preceding Conv2D addparam has no shape"; + INFO(l) << "Add fuse_to_preceding: addparam has no shape"; return false; } // if AddParam rank is not 1 we cannot fuse, just skip @@ -327,7 +336,7 @@ bool fuse_to_preceding(loco::Graph *graph, moco::tf::TFAdd *node) if (conv2d != nullptr && addparam != nullptr) { - // Preceding is TFConv2D. Let's insert TFConst - TFBiasAdd + // precedingOp is TFConv2D. Let's insert TFConst - TFBiasAdd // The plan is to fuse 'addparam' to TFBiasAdd bias auto dtype = addparam->dtype();