You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
150 lines
5.8 KiB
150 lines
5.8 KiB
7 years ago
|
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
|
||
|
|
||
|
Licensed 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. */
|
||
|
#include "paddle/framework/lod_rank_table.h"
|
||
|
#include "paddle/operators/array_operator.h"
|
||
|
#include "paddle/operators/math/math_function.h"
|
||
|
|
||
|
namespace paddle {
|
||
|
namespace operators {
|
||
|
|
||
7 years ago
|
class ShrinkRNNMemoryOp : public ArrayOp {
|
||
7 years ago
|
public:
|
||
7 years ago
|
ShrinkRNNMemoryOp(const std::string &type,
|
||
|
const framework::VariableNameMap &inputs,
|
||
|
const framework::VariableNameMap &outputs,
|
||
|
const framework::AttributeMap &attrs)
|
||
7 years ago
|
: ArrayOp(type, inputs, outputs, attrs) {}
|
||
|
|
||
|
void Run(const framework::Scope &scope,
|
||
|
const platform::DeviceContext &dev_ctx) const override {
|
||
|
auto *x_var = scope.FindVar(Input("X"));
|
||
|
PADDLE_ENFORCE(x_var != nullptr, "Input X must be set");
|
||
|
auto &x_tensor = x_var->Get<framework::LoDTensor>();
|
||
|
size_t offset = this->GetOffset(scope, dev_ctx);
|
||
|
auto *rank_table_var = scope.FindVar(Input("RankTable"));
|
||
|
PADDLE_ENFORCE(rank_table_var != nullptr, "RankTable must be set");
|
||
|
auto &rank_table = rank_table_var->Get<framework::LoDRankTable>();
|
||
|
|
||
7 years ago
|
auto &rank_items = rank_table.items();
|
||
|
int dst_num_rows =
|
||
|
std::lower_bound(rank_items.begin(), rank_items.end(), offset,
|
||
|
[](const framework::LoDRankTable::TableItem &a,
|
||
|
size_t b) { return a.length > b; }) -
|
||
|
rank_items.begin();
|
||
7 years ago
|
|
||
|
auto *out_var = scope.FindVar(Output("Out"));
|
||
|
PADDLE_ENFORCE(out_var != nullptr, "Output Out must be set");
|
||
|
auto &out_tensor = *out_var->GetMutable<framework::LoDTensor>();
|
||
|
if (dst_num_rows != 0) {
|
||
|
out_tensor.ShareDataWith(x_tensor.Slice(0, dst_num_rows));
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
|
||
7 years ago
|
class ShrinkRNNMemoryOpProtoMaker : public framework::OpProtoAndCheckerMaker {
|
||
7 years ago
|
public:
|
||
7 years ago
|
ShrinkRNNMemoryOpProtoMaker(framework::OpProto *proto,
|
||
|
framework::OpAttrChecker *op_checker)
|
||
7 years ago
|
: OpProtoAndCheckerMaker(proto, op_checker) {
|
||
|
AddInput("X", "");
|
||
|
AddInput("RankTable", "");
|
||
|
AddInput("I", "");
|
||
|
AddOutput("Out", "");
|
||
|
AddComment("");
|
||
|
}
|
||
|
};
|
||
|
|
||
7 years ago
|
class ShrinkRNNMemoryInferShape : public framework::InferShapeBase {
|
||
7 years ago
|
public:
|
||
|
void operator()(framework::InferShapeContext *context) const override {
|
||
|
PADDLE_ENFORCE(context->HasInput("X"));
|
||
|
PADDLE_ENFORCE(context->HasInput("I"));
|
||
|
PADDLE_ENFORCE(context->HasInput("RankTable"));
|
||
|
context->SetOutputDim("Out", context->GetInputDim("X"));
|
||
|
}
|
||
|
};
|
||
|
|
||
7 years ago
|
class ShrinkRNNMemoryGradOp : public ArrayOp {
|
||
7 years ago
|
public:
|
||
7 years ago
|
ShrinkRNNMemoryGradOp(const std::string &type,
|
||
|
const framework::VariableNameMap &inputs,
|
||
|
const framework::VariableNameMap &outputs,
|
||
|
const framework::AttributeMap &attrs)
|
||
7 years ago
|
: ArrayOp(type, inputs, outputs, attrs) {}
|
||
|
|
||
|
void Run(const framework::Scope &scope,
|
||
|
const platform::DeviceContext &dev_ctx) const override {
|
||
|
auto *dout_var = scope.FindVar(Input(framework::GradVarName("Out")));
|
||
7 years ago
|
auto *dx_var = scope.FindVar(Output(framework::GradVarName("X")));
|
||
7 years ago
|
PADDLE_ENFORCE(dx_var != nullptr, "Input Gradient should not be nullptr");
|
||
|
auto *x_var = scope.FindVar(Input("X"));
|
||
|
PADDLE_ENFORCE(x_var != nullptr);
|
||
|
|
||
|
auto &x_tensor = x_var->Get<framework::LoDTensor>();
|
||
|
auto &dx_tensor = *dx_var->GetMutable<framework::LoDTensor>();
|
||
|
dx_tensor.Resize(x_tensor.dims());
|
||
|
dx_tensor.mutable_data(x_tensor.place(), x_tensor.type());
|
||
|
|
||
|
if (dout_var == nullptr) { // dx_tensor fill zero
|
||
|
math::set_constant(dev_ctx, &dx_tensor, 0.0f);
|
||
|
} else {
|
||
|
auto &dout_tensor = dout_var->Get<framework::LoDTensor>();
|
||
|
auto height = dout_tensor.dims()[0];
|
||
|
dx_tensor.Slice(0, static_cast<int>(height))
|
||
|
.CopyFrom(dout_tensor, dout_tensor.place(), dev_ctx);
|
||
7 years ago
|
if (dx_tensor.dims()[0] < height) {
|
||
7 years ago
|
auto rest_tensor = dx_tensor.Slice(
|
||
|
static_cast<int>(height), static_cast<int>(dout_tensor.dims()[0]));
|
||
|
math::set_constant(dev_ctx, &rest_tensor, 0.0f);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
|
||
7 years ago
|
class ShrinkRNNMemoryGradInferShape : public framework::InferShapeBase {
|
||
7 years ago
|
public:
|
||
|
void operator()(framework::InferShapeContext *context) const override {
|
||
|
PADDLE_ENFORCE(context->HasInput("X"));
|
||
|
PADDLE_ENFORCE(context->HasOutput(framework::GradVarName("X")));
|
||
|
context->SetOutputDim(framework::GradVarName("X"),
|
||
|
context->GetInputDim("X"));
|
||
|
}
|
||
|
};
|
||
|
|
||
7 years ago
|
class ShrinkRNNGradOpMaker : public framework::SingleGradOpDescMaker {
|
||
7 years ago
|
public:
|
||
|
using framework::SingleGradOpDescMaker::SingleGradOpDescMaker;
|
||
|
|
||
|
protected:
|
||
|
std::unique_ptr<framework::OpDescBind> Apply() const override {
|
||
|
auto *op = new framework::OpDescBind();
|
||
7 years ago
|
op->SetType("shrink_rnn_memory_grad");
|
||
7 years ago
|
op->SetInput("X", Input("X"));
|
||
|
op->SetInput(framework::GradVarName("Out"), OutputGrad("Out"));
|
||
|
op->SetOutput(framework::GradVarName("X"), InputGrad("X"));
|
||
|
op->SetAttrMap(Attrs());
|
||
|
return std::unique_ptr<framework::OpDescBind>(op);
|
||
|
}
|
||
|
};
|
||
|
|
||
|
} // namespace operators
|
||
|
} // namespace paddle
|
||
|
|
||
|
namespace ops = paddle::operators;
|
||
7 years ago
|
REGISTER_OPERATOR(shrink_rnn_memory, ops::ShrinkRNNMemoryOp,
|
||
|
ops::ShrinkRNNMemoryInferShape,
|
||
|
ops::ShrinkRNNMemoryOpProtoMaker, ops::ShrinkRNNGradOpMaker);
|
||
|
REGISTER_OPERATOR(shrink_rnn_memory_grad, ops::ShrinkRNNMemoryGradOp,
|
||
|
ops::ShrinkRNNMemoryGradInferShape);
|