2019-09-11 20:50:28 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
* Copyright (c) 2015-2018 Skymind, Inc.
|
|
|
|
*
|
|
|
|
* This program and the accompanying materials are made available under the
|
|
|
|
* terms of the Apache License, Version 2.0 which is available at
|
|
|
|
* https://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.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
//
|
|
|
|
// @author saudet
|
|
|
|
// @author raver119@gmail.com
|
2020-02-06 19:12:54 +01:00
|
|
|
// @author Yurii Shyrma (iuriish@yahoo.com)
|
2019-09-11 20:50:28 +02:00
|
|
|
//
|
|
|
|
|
|
|
|
#include <ops/declarable/PlatformHelper.h>
|
|
|
|
#include <ops/declarable/OpRegistrator.h>
|
2020-03-02 10:49:41 +01:00
|
|
|
#include <system/platform_boilerplate.h>
|
2019-09-11 20:50:28 +02:00
|
|
|
|
|
|
|
#include <helpers/MKLDNNStream.h>
|
|
|
|
#include "mkldnnUtils.h"
|
|
|
|
#include <ops/declarable/helpers/convolutions.h>
|
|
|
|
|
2019-11-20 11:23:08 +01:00
|
|
|
using namespace dnnl;
|
2020-03-09 06:21:44 +01:00
|
|
|
using namespace sd;
|
2019-09-11 20:50:28 +02:00
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
namespace sd {
|
2020-01-28 16:23:07 +01:00
|
|
|
namespace ops {
|
|
|
|
namespace platforms {
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
PLATFORM_IMPL(avgpool2d, ENGINE_CPU) {
|
|
|
|
|
2020-02-06 19:12:54 +01:00
|
|
|
auto input = INPUT_VARIABLE(0);
|
|
|
|
auto output = OUTPUT_VARIABLE(0);
|
2020-01-28 16:23:07 +01:00
|
|
|
|
|
|
|
// 0,1 - kernel Height/Width; 2,3 - stride Height/Width; 4,5 - pad Height/Width; 6,7 - dilation Height/Width; 8 - same mode;
|
|
|
|
|
|
|
|
const auto kH = INT_ARG(0);
|
|
|
|
const auto kW = INT_ARG(1);
|
|
|
|
const auto sH = INT_ARG(2);
|
|
|
|
const auto sW = INT_ARG(3);
|
2020-02-06 19:12:54 +01:00
|
|
|
auto pH = INT_ARG(4);
|
|
|
|
auto pW = INT_ARG(5);
|
2020-01-28 16:23:07 +01:00
|
|
|
const auto dH = INT_ARG(6);
|
|
|
|
const auto dW = INT_ARG(7);
|
2020-02-06 19:12:54 +01:00
|
|
|
const auto paddingMode = INT_ARG(8);
|
2020-01-28 16:23:07 +01:00
|
|
|
const auto extraParam0 = INT_ARG(9);
|
2020-02-06 19:12:54 +01:00
|
|
|
const int isNCHW = block.getIArguments()->size() > 10 ? !INT_ARG(10) : 1; // INT_ARG(10): 0-NCHW, 1-NHWC
|
2020-01-28 16:23:07 +01:00
|
|
|
|
2020-02-06 19:12:54 +01:00
|
|
|
REQUIRE_TRUE(input->rankOf() == 4, 0, "AVGPOOL2D MKLDNN op: input should have rank of 4, but got %i instead", input->rankOf());
|
|
|
|
REQUIRE_TRUE(dH != 0 && dW != 0, 0, "AVGPOOL2D MKLDNN op: dilation must not be zero, but got instead {%i, %i}", dH, dW);
|
2020-01-28 16:23:07 +01:00
|
|
|
|
2020-02-06 19:12:54 +01:00
|
|
|
int bS, iC, iH, iW, oC, oH, oW; // batch size, input channels, input height/width, output channels, output height/width;
|
|
|
|
int indIOioC, indIiH, indWoC, indWiC, indWkH, indOoH; // corresponding indexes
|
|
|
|
ConvolutionUtils::getSizesAndIndexesConv2d(isNCHW, *input, *output, bS, iC, iH, iW, oC, oH, oW, indIOioC, indIiH, indWiC, indWoC, indWkH, indOoH);
|
2020-01-28 16:23:07 +01:00
|
|
|
|
2020-02-06 19:12:54 +01:00
|
|
|
if (paddingMode)
|
2020-01-28 16:23:07 +01:00
|
|
|
ConvolutionUtils::calcPadding2D(pH, pW, oH, oW, iH, iW, kH, kW, sH, sW, dH, dW);
|
|
|
|
|
2020-02-06 19:12:54 +01:00
|
|
|
auto mode = (extraParam0 == 0) ? algorithm::pooling_avg_exclude_padding : algorithm::pooling_avg_include_padding;
|
|
|
|
|
|
|
|
mkldnnUtils::poolingMKLDNN(input, output, 0,kH,kW, 0,sH,sW, 0,pH,pW, isNCHW, mode);
|
2020-01-28 16:23:07 +01:00
|
|
|
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
PLATFORM_CHECK(avgpool2d, ENGINE_CPU) {
|
2020-02-06 19:12:54 +01:00
|
|
|
|
2020-01-28 16:23:07 +01:00
|
|
|
auto input = INPUT_VARIABLE(0);
|
|
|
|
auto output = OUTPUT_VARIABLE(0);
|
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
return block.isUseMKLDNN() && sd::MKLDNNStream::isSupported({input, output});
|
2020-01-28 16:23:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
PLATFORM_IMPL(avgpool2d_bp, ENGINE_CPU) {
|
2020-02-06 19:12:54 +01:00
|
|
|
|
|
|
|
auto input = INPUT_VARIABLE(0); // [bS, iH, iW, iC] (NHWC) or [bS, iC, iH, iW] (NCHW)
|
|
|
|
auto gradO = INPUT_VARIABLE(1); // [bS, oH, oW, oC] (NHWC) or [bS, oC, oH, oW] (NCHW), epsilon_next
|
|
|
|
auto gradI = OUTPUT_VARIABLE(0); // [bS, iH, iW, iC] (NHWC) or [bS, iC, iH, iW] (NCHW), epsilon
|
2020-01-28 16:23:07 +01:00
|
|
|
|
|
|
|
int kH = INT_ARG(0); // filter(kernel) height
|
|
|
|
int kW = INT_ARG(1); // filter(kernel) width
|
|
|
|
int sH = INT_ARG(2); // strides height
|
|
|
|
int sW = INT_ARG(3); // strides width
|
|
|
|
int pH = INT_ARG(4); // paddings height
|
|
|
|
int pW = INT_ARG(5); // paddings width
|
|
|
|
int dH = INT_ARG(6); // dilations height
|
|
|
|
int dW = INT_ARG(7); // dilations width
|
2020-02-06 19:12:54 +01:00
|
|
|
int paddingMode = INT_ARG(8); // 0-VALID, 1-SAME
|
2020-01-28 16:23:07 +01:00
|
|
|
int extraParam0 = INT_ARG(9);
|
2020-02-06 19:12:54 +01:00
|
|
|
int isNCHW = block.getIArguments()->size() > 10 ? !INT_ARG(10) : 1; // INT_ARG(10): 0-NCHW, 1-NHWC
|
2020-01-28 16:23:07 +01:00
|
|
|
|
2020-02-06 19:12:54 +01:00
|
|
|
REQUIRE_TRUE(input->rankOf() == 4, 0, "AVGPOOL2D_BP MKLDNN op: input should have rank of 4, but got %i instead", input->rankOf());
|
|
|
|
REQUIRE_TRUE(dH != 0 && dW != 0, 0, "AVGPOOL2D_BP MKLDNN op: dilation must not be zero, but got instead {%i, %i}", dH, dW);
|
2020-01-28 16:23:07 +01:00
|
|
|
|
|
|
|
int bS, iC, iH, iW, oC, oH, oW; // batch size, input channels, input height/width, output channels, output height/width;
|
|
|
|
int indIOioC, indIiH, indWoC, indWiC, indWkH, indOoH; // corresponding indexes
|
2020-02-06 19:12:54 +01:00
|
|
|
ConvolutionUtils::getSizesAndIndexesConv2d(isNCHW, *input, *gradO, bS, iC, iH, iW, oC, oH, oW, indIOioC, indIiH, indWiC, indWoC, indWkH, indOoH);
|
|
|
|
|
|
|
|
std::vector<Nd4jLong> expectedGradOShape = ShapeUtils::composeShapeUsingDimsAndIdx({bS,iC,oH,oW, 0,indIOioC,indIiH,indIiH+1});
|
|
|
|
REQUIRE_TRUE(gradO->isSameShape(expectedGradOShape), 0, "AVGPOOL2D_BP MKLDNN op: wrong shape of output's gradients array (next epsilon), expected is %s, but got %s instead !", ShapeUtils::shapeAsString(expectedGradOShape).c_str(), ShapeUtils::shapeAsString(gradO).c_str());
|
|
|
|
|
|
|
|
if(paddingMode) // SAME
|
2020-01-28 16:23:07 +01:00
|
|
|
ConvolutionUtils::calcPadding2D(pH, pW, oH, oW, iH, iW, kH, kW, sH, sW, dH, dW);
|
|
|
|
|
2020-02-06 19:12:54 +01:00
|
|
|
auto mode = (extraParam0 == 0) ? algorithm::pooling_avg_exclude_padding : algorithm::pooling_avg_include_padding;
|
2020-01-28 16:23:07 +01:00
|
|
|
|
2020-02-06 19:12:54 +01:00
|
|
|
mkldnnUtils::poolingBpMKLDNN(input, gradO, gradI, 0,kH,kW, 0,sH,sW, 0,pH,pW, isNCHW, mode);
|
2020-01-28 16:23:07 +01:00
|
|
|
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
PLATFORM_CHECK(avgpool2d_bp, ENGINE_CPU) {
|
|
|
|
auto input = INPUT_VARIABLE(0);
|
|
|
|
auto output = OUTPUT_VARIABLE(0);
|
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
return block.isUseMKLDNN() && sd::MKLDNNStream::isSupported({input, output});
|
2020-01-28 16:23:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2019-09-11 20:50:28 +02:00
|
|
|
}
|