lebeg commented on a change in pull request #12308: [MKLDNN] Enable convolution fusion. URL: https://github.com/apache/incubator-mxnet/pull/12308#discussion_r212640810
########## File path: src/operator/subgraph/mkldnn/mkldnn_conv.cc ########## @@ -0,0 +1,325 @@ +/* +* Licensed to the Apache Software Foundation (ASF) under one +* or more contributor license agreements. See the NOTICE file +* distributed with this work for additional information +* regarding copyright ownership. The ASF licenses this file +* to you under the Apache License, Version 2.0 (the +* "License"); you may not use this file except in compliance +* with the License. You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an +* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +* KIND, either express or implied. See the License for the +* specific language governing permissions and limitations +* under the License. +*/ + +#if MXNET_USE_MKLDNN == 1 +#include <nnvm/graph.h> +#include <mshadow/base.h> +#include "./mkldnn_conv.h" +#include "../../nn/mkldnn/mkldnn_ops-inl.h" +#include "../../../imperative/imperative_utils.h" +#include "../../../imperative/cached_op.h" +#include "../../nn/convolution-inl.h" +#include "../../nn/batch_norm-inl.h" +namespace mxnet { +namespace op { + +#define SUBGRAPH_DEBUG 0 + +template <typename DType> +static void UpdateConvWeightBias(const NDArray &weight, const NDArray *bias, + const NDArray &gamma, const NDArray &beta, + const NDArray &mean, + const NDArray &variance, + std::shared_ptr<NDArray> update_weight, + std::shared_ptr<NDArray> update_bias, + const BatchNormParam ¶m) { +#if SUBGRAPH_DEBUG + printf("input weight: %f %f %f %f \n", weight.data().dptr<float>()[0], + weight.data().dptr<float>()[1], + weight.data().dptr<float>()[2], + weight.data().dptr<float>()[3]); + printf("bn param eps: %f \n", param.eps); + printf("bn param fix_gamma: %d \n", param.fix_gamma); + printf("bn param use_global_stats: %d \n", param.use_global_stats); + printf("bn param output_mean_var: %d \n", param.output_mean_var); + printf("bn param axis: %d \n", param.axis); +#endif + DType *weight_ptr = weight.Reorder2Default().data().dptr<DType>(); + DType *bias_ptr = bias ? bias->Reorder2Default().data().dptr<DType>() : nullptr; + DType *gamma_ptr = gamma.Reorder2Default().data().dptr<DType>(); + DType *beta_ptr = beta.Reorder2Default().data().dptr<DType>(); + DType *mean_ptr = mean.Reorder2Default().data().dptr<DType>(); + DType *var_ptr = variance.Reorder2Default().data().dptr<DType>(); + DType *update_weight_ptr = update_weight->data().dptr<DType>(); + DType *update_bias_ptr = update_bias->data().dptr<DType>(); + size_t channel = gamma.shape()[0]; + size_t offset = weight.shape()[1] * weight.shape()[2] * weight.shape()[3]; +#pragma omp parallel for + for (size_t c = 0; c < channel; ++c) { + DType *p1 = reinterpret_cast<DType *>(weight_ptr + c * offset); + DType *p2 = reinterpret_cast<DType *>(update_weight_ptr + c * offset); + DType alpha = (param.fix_gamma ? static_cast<DType>(1.0f) : gamma_ptr[c]) / + sqrt(var_ptr[c] + param.eps); + + if (bias_ptr) + update_bias_ptr[c] = beta_ptr[c] + alpha * (bias_ptr[c] - mean_ptr[c]); + else + update_bias_ptr[c] = beta_ptr[c] - alpha * mean_ptr[c]; + + for (size_t k = 0; k < offset; ++k) { + p2[k] = p1[k] * alpha; + } + } +#if SUBGRAPH_DEBUG + printf("update weight: %f %f %f %f \n", update_weight->data().dptr<float>()[0], + update_weight->data().dptr<float>()[1], + update_weight->data().dptr<float>()[2], + update_weight->data().dptr<float>()[3]); +#endif +} + +static void ConvFusionFallBackCompute() { + LOG(FATAL) << "Don't know how to do ConvFusionFallBackCompute!"; +} + +static void ConvolutionFusionComputeExCPU(const nnvm::NodeAttrs &conv_attrs, + const OpContext &ctx, + const std::vector<NDArray> &inputs, + const std::vector<OpReqType> &req, + const std::vector<NDArray> &outputs) { + const ConvolutionParam ¶ms = + nnvm::get<ConvolutionParam>(conv_attrs.parsed); + if (SupportMKLDNNConv(params, inputs[0])) { + // MKLDNN_OPCHECK_INIT(false, outputs.size(), inputs, outputs); + MKLDNNConvolutionForward(conv_attrs, ctx, inputs, req, outputs); + // MKLDNN_OPCHECK_RUN(ConvolutionCompute<cpu>, attrs, ctx, inputs, req, + // outputs); + return; + } + ConvFusionFallBackCompute(); +} + +class SgMKLDNNConvOperator { Review comment: Why not have the definition in the header file? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
