|
| 1 | +/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. */ |
| 14 | + |
| 15 | +#include "Layer.h" |
| 16 | +#include "paddle/math/Matrix.h" |
| 17 | +#include "paddle/utils/Logging.h" |
| 18 | +#include "paddle/utils/Stat.h" |
| 19 | + |
| 20 | +namespace paddle { |
| 21 | + |
| 22 | +/** |
| 23 | + * @brief A layer for computing the dot product of two vectors. |
| 24 | + * Input1: vector (batchSize * dim) |
| 25 | + * Input2: vector (batchSize * dim) |
| 26 | + * Output: a matrix: (batchSize * 1) |
| 27 | + */ |
| 28 | + |
| 29 | +class DotProdLayer : public Layer { |
| 30 | +public: |
| 31 | + explicit DotProdLayer(const LayerConfig& config) : Layer(config) {} |
| 32 | + |
| 33 | + ~DotProdLayer() {} |
| 34 | + |
| 35 | + bool init(const LayerMap& layerMap, |
| 36 | + const ParameterMap& parameterMap) override; |
| 37 | + |
| 38 | + void forward(PassType passType) override; |
| 39 | + void backward(const UpdateCallback& callback = nullptr) override; |
| 40 | +}; |
| 41 | + |
| 42 | +REGISTER_LAYER(dot_prod, DotProdLayer); |
| 43 | + |
| 44 | +bool DotProdLayer::init(const LayerMap& layerMap, |
| 45 | + const ParameterMap& parameterMap) { |
| 46 | + Layer::init(layerMap, parameterMap); |
| 47 | + |
| 48 | + CHECK_EQ(inputLayers_.size(), 2U); |
| 49 | + CHECK_EQ(1UL, getSize()) |
| 50 | + << "The output dimensionality of this layer should be fixed to 1."; |
| 51 | + |
| 52 | + return true; |
| 53 | +} |
| 54 | + |
| 55 | +void DotProdLayer::forward(PassType passType) { |
| 56 | + Layer::forward(passType); |
| 57 | + |
| 58 | + MatrixPtr inV0 = getInputValue(0); |
| 59 | + MatrixPtr inV1 = getInputValue(1); |
| 60 | + |
| 61 | + size_t batchSize = inV0->getHeight(); |
| 62 | + CHECK_EQ(inV1->getHeight(), batchSize); |
| 63 | + CHECK_EQ(inV0->getWidth(), inV1->getWidth()); |
| 64 | + |
| 65 | + { |
| 66 | + REGISTER_TIMER_INFO("FwResetTimer", getName().c_str()); |
| 67 | + reserveOutput(batchSize, 1); |
| 68 | + } |
| 69 | + |
| 70 | + MatrixPtr outV = getOutputValue(); |
| 71 | + { |
| 72 | + REGISTER_TIMER_INFO("FwDotProdTimer", getName().c_str()); |
| 73 | + outV->sumOfProducts(*inV0, *inV1, 1, 0); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +void DotProdLayer::backward(const UpdateCallback& callback) { |
| 78 | + MatrixPtr inV0 = getInputValue(0); |
| 79 | + MatrixPtr inV1 = getInputValue(1); |
| 80 | + MatrixPtr outG = getOutputGrad(); |
| 81 | + MatrixPtr inG0 = getInputGrad(0); |
| 82 | + MatrixPtr inG1 = getInputGrad(1); |
| 83 | + |
| 84 | + { |
| 85 | + REGISTER_TIMER_INFO("BwDotProdTimer", getName().c_str()); |
| 86 | + |
| 87 | + if (inG0) { |
| 88 | + inG0->addRowScale(0, *inV1, *outG); |
| 89 | + } |
| 90 | + |
| 91 | + if (inG1) { |
| 92 | + inG1->addRowScale(0, *inV0, *outG); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +} // namespace paddle |
0 commit comments