未验证 提交 ab60fd8b 编写于 作者: Y YuanRisheng 提交者: GitHub

[PHI]Add RNN yaml (#46812)

* add yaml entry for rnn and rrnn_grad, move infershape function for rnn_grad to phi infer meta

* WIP: move rnn kernrl to phi

* Change the code generation to avoid converting from intializer list to tuple of heterogeneous types.
This is only triggered when an api has intermediate outputs, and the result of the outputs are of heterogeneous types.

* fix the bug that when none in a vector of tensors requires gradient, the conversion to InferShapeContext to InferMetaContext (a.k.a. BuildInferMetaContext) produces errorous results.

* fix ci bugs

* fix ci bugs

* fix ci bugs

* modify code according comment
Co-authored-by: Nchenfeiyu <chenfeiyu@baidu.com>
上级 715b9d66
...@@ -31,6 +31,8 @@ limitations under the License. */ ...@@ -31,6 +31,8 @@ limitations under the License. */
#include "paddle/phi/core/kernel_factory.h" #include "paddle/phi/core/kernel_factory.h"
#include "paddle/phi/core/tensor_utils.h" #include "paddle/phi/core/tensor_utils.h"
#include "glog/logging.h"
namespace paddle { namespace paddle {
namespace framework { namespace framework {
...@@ -270,6 +272,7 @@ void CompatMetaTensor::set_dims(const DDim& dims) { ...@@ -270,6 +272,7 @@ void CompatMetaTensor::set_dims(const DDim& dims) {
ValidCheck(*this); ValidCheck(*this);
if (is_runtime_) { if (is_runtime_) {
auto* var = PADDLE_GET(Variable*, var_); auto* var = PADDLE_GET(Variable*, var_);
if (var == nullptr) return;
if (var->IsType<phi::DenseTensor>()) { if (var->IsType<phi::DenseTensor>()) {
auto* tensor = var->GetMutable<phi::DenseTensor>(); auto* tensor = var->GetMutable<phi::DenseTensor>();
phi::DenseTensorUtils::GetMutableMeta(tensor)->dims = dims; phi::DenseTensorUtils::GetMutableMeta(tensor)->dims = dims;
...@@ -295,14 +298,17 @@ void CompatMetaTensor::set_dims(const DDim& dims) { ...@@ -295,14 +298,17 @@ void CompatMetaTensor::set_dims(const DDim& dims) {
} }
} else { } else {
auto* var = PADDLE_GET(VarDesc*, var_); auto* var = PADDLE_GET(VarDesc*, var_);
if (var) {
var->SetShape(vectorize(dims)); var->SetShape(vectorize(dims));
} }
}
} }
void CompatMetaTensor::set_dtype(phi::DataType dtype) { void CompatMetaTensor::set_dtype(phi::DataType dtype) {
ValidCheck(*this); ValidCheck(*this);
if (is_runtime_) { if (is_runtime_) {
auto* var = PADDLE_GET(Variable*, var_); auto* var = PADDLE_GET(Variable*, var_);
if (var == nullptr) return;
if (var->IsType<phi::DenseTensor>()) { if (var->IsType<phi::DenseTensor>()) {
auto* tensor = var->GetMutable<phi::DenseTensor>(); auto* tensor = var->GetMutable<phi::DenseTensor>();
phi::DenseTensorUtils::GetMutableMeta(tensor)->dtype = dtype; phi::DenseTensorUtils::GetMutableMeta(tensor)->dtype = dtype;
...@@ -321,14 +327,17 @@ void CompatMetaTensor::set_dtype(phi::DataType dtype) { ...@@ -321,14 +327,17 @@ void CompatMetaTensor::set_dtype(phi::DataType dtype) {
} }
} else { } else {
auto* var = PADDLE_GET(VarDesc*, var_); auto* var = PADDLE_GET(VarDesc*, var_);
if (var) {
var->SetDataType(paddle::framework::TransToProtoVarType(dtype)); var->SetDataType(paddle::framework::TransToProtoVarType(dtype));
} }
}
} }
void CompatMetaTensor::set_layout(DataLayout layout) { void CompatMetaTensor::set_layout(DataLayout layout) {
ValidCheck(*this); ValidCheck(*this);
if (is_runtime_) { if (is_runtime_) {
auto* var = PADDLE_GET(Variable*, var_); auto* var = PADDLE_GET(Variable*, var_);
if (var == nullptr) return;
if (var->IsType<phi::DenseTensor>()) { if (var->IsType<phi::DenseTensor>()) {
auto* tensor = var->GetMutable<phi::DenseTensor>(); auto* tensor = var->GetMutable<phi::DenseTensor>();
phi::DenseTensorUtils::GetMutableMeta(tensor)->layout = layout; phi::DenseTensorUtils::GetMutableMeta(tensor)->layout = layout;
...@@ -357,6 +366,7 @@ void CompatMetaTensor::share_lod(const MetaTensor& meta_tensor) { ...@@ -357,6 +366,7 @@ void CompatMetaTensor::share_lod(const MetaTensor& meta_tensor) {
ValidCheck(meta_tensor); ValidCheck(meta_tensor);
if (is_runtime_) { if (is_runtime_) {
auto* var = PADDLE_GET(Variable*, var_); auto* var = PADDLE_GET(Variable*, var_);
if (var == nullptr) return;
if (var->IsType<phi::DenseTensor>() && meta_tensor.is_dense()) { if (var->IsType<phi::DenseTensor>() && meta_tensor.is_dense()) {
auto* tensor = var->GetMutable<phi::DenseTensor>(); auto* tensor = var->GetMutable<phi::DenseTensor>();
phi::DenseTensorUtils::GetMutableMeta(tensor)->lod = phi::DenseTensorUtils::GetMutableMeta(tensor)->lod =
...@@ -371,8 +381,10 @@ void CompatMetaTensor::share_lod(const MetaTensor& meta_tensor) { ...@@ -371,8 +381,10 @@ void CompatMetaTensor::share_lod(const MetaTensor& meta_tensor) {
VLOG(3) << "input metatensor is not LoDTensor or LoDTensorArray."; VLOG(3) << "input metatensor is not LoDTensor or LoDTensorArray.";
return; return;
} }
var->SetLoDLevel( if (var) {
static_cast<const CompatMetaTensor&>(meta_tensor).GetCompileTimeLoD()); var->SetLoDLevel(static_cast<const CompatMetaTensor&>(meta_tensor)
.GetCompileTimeLoD());
}
} }
} }
...@@ -382,6 +394,7 @@ void CompatMetaTensor::share_dims(const MetaTensor& meta_tensor) { ...@@ -382,6 +394,7 @@ void CompatMetaTensor::share_dims(const MetaTensor& meta_tensor) {
set_dims(meta_tensor.dims()); set_dims(meta_tensor.dims());
if (is_runtime_) { if (is_runtime_) {
auto* var = PADDLE_GET(Variable*, var_); auto* var = PADDLE_GET(Variable*, var_);
if (var == nullptr) return;
if (var->IsType<phi::SelectedRows>()) { if (var->IsType<phi::SelectedRows>()) {
auto* selected_rows = var->GetMutable<phi::SelectedRows>(); auto* selected_rows = var->GetMutable<phi::SelectedRows>();
auto& input_selected_rows = auto& input_selected_rows =
......
...@@ -94,17 +94,12 @@ bool InterpretercoreInferShapeContext::HasOutputs(const std::string& name, ...@@ -94,17 +94,12 @@ bool InterpretercoreInferShapeContext::HasOutputs(const std::string& name,
if (it == outs.end() || it->second.empty()) { if (it == outs.end() || it->second.empty()) {
return false; return false;
} }
if (allow_null) { if (!allow_null) {
for (auto& output : it->second) {
if (output != nullptr) return true;
}
return false;
} else {
for (auto& output : it->second) { for (auto& output : it->second) {
if (output == nullptr) return false; if (output == nullptr) return false;
} }
return true;
} }
return true;
} }
AttrReader InterpretercoreInferShapeContext::Attrs() const { AttrReader InterpretercoreInferShapeContext::Attrs() const {
......
...@@ -1227,17 +1227,12 @@ bool CompileTimeInferShapeContext::HasOutputs(const std::string &name, ...@@ -1227,17 +1227,12 @@ bool CompileTimeInferShapeContext::HasOutputs(const std::string &name,
if (output_names.empty()) { if (output_names.empty()) {
return false; return false;
} }
if (allow_null) { if (!allow_null) {
for (auto &output : output_names) {
if (block_.HasVarRecursive(output)) return true;
}
return false;
} else {
for (auto &output : output_names) { for (auto &output : output_names) {
if (!block_.HasVarRecursive(output)) return false; if (!block_.HasVarRecursive(output)) return false;
} }
return true;
} }
return true;
} }
AttrReader CompileTimeInferShapeContext::Attrs() const { AttrReader CompileTimeInferShapeContext::Attrs() const {
......
...@@ -755,17 +755,12 @@ class RuntimeInferShapeContext : public InferShapeContext { ...@@ -755,17 +755,12 @@ class RuntimeInferShapeContext : public InferShapeContext {
if (it == outs.end() || it->second.empty()) { if (it == outs.end() || it->second.empty()) {
return false; return false;
} }
if (allow_null) { if (!allow_null) {
for (auto& output : it->second) {
if (output != nullptr) return true;
}
return false;
} else {
for (auto& output : it->second) { for (auto& output : it->second) {
if (output == nullptr) return false; if (output == nullptr) return false;
} }
return true;
} }
return true;
} }
AttrReader Attrs() const override { AttrReader Attrs() const override {
......
...@@ -109,21 +109,14 @@ class DygraphInferShapeContext : public framework::InferShapeContext { ...@@ -109,21 +109,14 @@ class DygraphInferShapeContext : public framework::InferShapeContext {
if (it == var_map_out_->end() || it->second.empty()) { if (it == var_map_out_->end() || it->second.empty()) {
return false; return false;
} }
if (allow_null) { if (!allow_null) {
for (auto& output : it->second) {
if (output != nullptr) {
return true;
}
}
return false;
} else {
for (auto& output : it->second) { for (auto& output : it->second) {
if (output == nullptr) { if (output == nullptr) {
return false; return false;
} }
} }
return true;
} }
return true;
} }
framework::AttrReader Attrs() const override { framework::AttrReader Attrs() const override {
...@@ -288,7 +281,11 @@ class DygraphInferShapeContext : public framework::InferShapeContext { ...@@ -288,7 +281,11 @@ class DygraphInferShapeContext : public framework::InferShapeContext {
var_map_out_->end(), var_map_out_->end(),
platform::errors::NotFound("Can not find [%s] in outputs.", name)); platform::errors::NotFound("Can not find [%s] in outputs.", name));
for (auto& var : it->second) { for (auto& var : it->second) {
if (var) {
res.emplace_back(var->MutableVar()); res.emplace_back(var->MutableVar());
} else {
res.emplace_back(framework::InferShapeVarPtr());
}
} }
return res; return res;
} }
......
...@@ -19,6 +19,7 @@ limitations under the License. */ ...@@ -19,6 +19,7 @@ limitations under the License. */
#include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/op_version_registry.h" #include "paddle/fluid/framework/op_version_registry.h"
#include "paddle/phi/core/infermeta_utils.h" #include "paddle/phi/core/infermeta_utils.h"
#include "paddle/phi/infermeta/backward.h"
#include "paddle/phi/infermeta/multiary.h" #include "paddle/phi/infermeta/multiary.h"
namespace paddle { namespace paddle {
...@@ -115,29 +116,6 @@ class RNNGradOp : public framework::OperatorWithKernel { ...@@ -115,29 +116,6 @@ class RNNGradOp : public framework::OperatorWithKernel {
public: public:
using framework::OperatorWithKernel::OperatorWithKernel; using framework::OperatorWithKernel::OperatorWithKernel;
void InferShape(framework::InferShapeContext* ctx) const override {
OP_INOUT_CHECK(ctx->HasInput("Input"), "Input", "Input", "RNN");
OP_INOUT_CHECK(ctx->HasInputs("PreState"), "Input", "PreState", "RNN");
OP_INOUT_CHECK(ctx->HasInput("Out"), "Input", "Out", "RNN");
// OP_INOUT_CHECK(ctx->HasInputs("State"), "Input", "State", "RNN");
auto SetOutGradDim = [&ctx](const std::string& name) {
auto g_name = framework::GradVarName(name);
if (ctx->HasOutput(g_name)) {
ctx->SetOutputDim(g_name, ctx->GetInputDim(name));
}
};
SetOutGradDim("Input");
if (ctx->HasOutputs(framework::GradVarName("WeightList"))) {
ctx->SetOutputsDim(framework::GradVarName("WeightList"),
ctx->GetInputsDim("WeightList"));
}
if (ctx->HasOutputs(framework::GradVarName("PreState"))) {
ctx->SetOutputsDim(framework::GradVarName("PreState"),
ctx->GetInputsDim("PreState"));
}
}
framework::OpKernelType GetExpectedKernelType( framework::OpKernelType GetExpectedKernelType(
const framework::ExecutionContext& ctx) const override { const framework::ExecutionContext& ctx) const override {
return framework::OpKernelType(OperatorWithKernel::IndicateVarDataType( return framework::OpKernelType(OperatorWithKernel::IndicateVarDataType(
...@@ -192,6 +170,9 @@ namespace ops = paddle::operators; ...@@ -192,6 +170,9 @@ namespace ops = paddle::operators;
DECLARE_INFER_SHAPE_FUNCTOR(rnn, DECLARE_INFER_SHAPE_FUNCTOR(rnn,
RnnInferShapeFunctor, RnnInferShapeFunctor,
PD_INFER_META(phi::RnnInferMeta)); PD_INFER_META(phi::RnnInferMeta));
DECLARE_INFER_SHAPE_FUNCTOR(rnn_grad,
RnnGradInferShapeFunctor,
PD_INFER_META(phi::RnnGradInferMeta));
REGISTER_OPERATOR(rnn, REGISTER_OPERATOR(rnn,
ops::RNNOp, ops::RNNOp,
...@@ -199,4 +180,4 @@ REGISTER_OPERATOR(rnn, ...@@ -199,4 +180,4 @@ REGISTER_OPERATOR(rnn,
ops::RNNGradOpMaker<paddle::framework::OpDesc>, ops::RNNGradOpMaker<paddle::framework::OpDesc>,
ops::RNNGradOpMaker<paddle::imperative::OpBase>, ops::RNNGradOpMaker<paddle::imperative::OpBase>,
RnnInferShapeFunctor); RnnInferShapeFunctor);
REGISTER_OPERATOR(rnn_grad, ops::RNNGradOp); REGISTER_OPERATOR(rnn_grad, ops::RNNGradOp, RnnGradInferShapeFunctor);
...@@ -49,6 +49,23 @@ class BaseAPI(object): ...@@ -49,6 +49,23 @@ class BaseAPI(object):
self.data_transform = self.parse_data_transform(api_item_yaml) self.data_transform = self.parse_data_transform(api_item_yaml)
self.inplace_map, self.view_map = {}, {} self.inplace_map, self.view_map = {}, {}
self.gene_input_func = {
"const Tensor&": {
"dense": self.gene_dense_input,
"selected_rows": self.gene_selected_rows_input
},
"const paddle::optional<Tensor>&": {
"dense": self.gene_dense_input,
"selected_rows": self.gene_selected_rows_input
},
"const std::vector<Tensor>&": {
"dense": self.gene_vec_dense_input
},
"const paddle::optional<std::vector<Tensor>>&": {
"dense": self.gene_optional_vec_dense_input
}
}
def get_api_name(self, api_item_yaml): def get_api_name(self, api_item_yaml):
return api_item_yaml['op'] return api_item_yaml['op']
...@@ -550,56 +567,62 @@ PADDLE_API {self.get_return_type(inplace_flag=True)} {api_func_name}({self.get_d ...@@ -550,56 +567,62 @@ PADDLE_API {self.get_return_type(inplace_flag=True)} {api_func_name}({self.get_d
{code_indent} phi::{infer_meta['func']}({param_code}); {code_indent} phi::{infer_meta['func']}({param_code});
""" """
def get_kernel_args(self, kernel_tensor_type=None, code_indent=''): def gene_trans_flag(self, input_name):
dense_input_trans_map = { trans_flag = "{}"
'const Tensor&': if input_name in self.data_transform['skip_transform']:
'const phi::DenseTensor&', trans_flag = "{true}"
'const std::vector<Tensor>&': elif input_name in self.data_transform['support_trans_dtype']:
'const std::vector<const phi::DenseTensor*>&', trans_flag = "{false, true}"
'const paddle::optional<Tensor&>': return trans_flag
'paddle::optional<const phi::DenseTensor&>',
'const paddle::optional<Tensor>&': def gene_dense_input(self,
'const paddle::optional<phi::DenseTensor>&', input_name,
'const paddle::optional<std::vector<Tensor>>&': input_name_tensor_map,
'const paddle::optional<std::vector<const phi::DenseTensor*>>&' code_indent=''):
} input_tensor_code = ""
dense_out_trans_map = { trans_flag = self.gene_trans_flag(input_name)
'Tensor': 'phi::DenseTensor*',
'std::vector<Tensor>': 'std::vector<phi::DenseTensor*>&'
}
sr_input_trans_map = {
'const Tensor&':
'const phi::SelectedRows&',
'const paddle::optional<Tensor>&':
'const paddle::optional<phi::SelectedRows>&'
}
sr_out_trans_map = {'Tensor': 'phi::SelectedRows*'}
input_names = self.inputs['names'] input_names = self.inputs['names']
input_infos = self.inputs['input_info'] attr_names = self.attrs['names']
kernel_args_type_list = ['const platform::DeviceContext&'] kernel_param = self.kernel['param']
if kernel_param is None:
kernel_param = input_names + attr_names
input_name_tensor_map[input_name].append(
(f"{PREFIX_TENSOR_NAME}{input_name}", False))
input_tensor_code = input_tensor_code + f"""
{code_indent} auto {PREFIX_TENSOR_NAME}{input_name} = PrepareData({input_name}, kernel.InputAt({kernel_param.index(input_name)}), {trans_flag});"""
return input_tensor_code
def gene_selected_rows_input(self,
input_name,
input_name_tensor_map,
code_indent=''):
input_tensor_code = ""
trans_flag = self.gene_trans_flag(input_name)
input_names = self.inputs['names']
attr_names = self.attrs['names'] attr_names = self.attrs['names']
kernel_param = self.kernel['param'] kernel_param = self.kernel['param']
if kernel_param is None: if kernel_param is None:
kernel_param = input_names + attr_names kernel_param = input_names + attr_names
input_name_tensor_map[input_name].append(
(f"{PREFIX_TENSOR_NAME}{input_name}", False))
input_tensor_code = input_tensor_code + f"""
{code_indent} auto {PREFIX_TENSOR_NAME}{input_name} = TensorToSelectedRows({input_name});
"""
return input_tensor_code
def gene_optional_vec_dense_input(self,
input_name,
input_name_tensor_map,
code_indent=''):
input_tensor_code = "" input_tensor_code = ""
input_name_tensor_map = collections.defaultdict(list) trans_flag = self.gene_trans_flag(input_name)
for i, input_name in enumerate(input_names): input_names = self.inputs['names']
# set input code attr_names = self.attrs['names']
if input_name in kernel_param: kernel_param = self.kernel['param']
# input is dense tensor if kernel_param is None:
if kernel_tensor_type is None or kernel_tensor_type[0][ kernel_param = input_names + attr_names
kernel_param.index(input_name)] == 'dense':
trans_flag = "{}"
if input_name in self.data_transform['skip_transform']:
trans_flag = "{true}"
elif input_name in self.data_transform[
'support_trans_dtype']:
trans_flag = "{false, true}"
if input_name in self.optional_vars:
if self.inputs['input_info'][
input_name] == "const paddle::optional<std::vector<Tensor>>&":
if input_name in self.inplace_map.values(): if input_name in self.inplace_map.values():
input_name_tensor_map[input_name].append( input_name_tensor_map[input_name].append(
(f"{PREFIX_TENSOR_NAME}{input_name}", True)) (f"{PREFIX_TENSOR_NAME}{input_name}", True))
...@@ -607,8 +630,7 @@ PADDLE_API {self.get_return_type(inplace_flag=True)} {api_func_name}({self.get_d ...@@ -607,8 +630,7 @@ PADDLE_API {self.get_return_type(inplace_flag=True)} {api_func_name}({self.get_d
{code_indent} paddle::optional<std::vector<const phi::DenseTensor*>> {PREFIX_TENSOR_NAME}{input_name} = TensorToConstDenseTensorPtr({input_name});""" {code_indent} paddle::optional<std::vector<const phi::DenseTensor*>> {PREFIX_TENSOR_NAME}{input_name} = TensorToConstDenseTensorPtr({input_name});"""
else: else:
input_name_tensor_map[input_name].append( input_name_tensor_map[input_name].append(
(f"{PREFIX_TENSOR_NAME}{input_name}_vec", (f"{PREFIX_TENSOR_NAME}{input_name}_vec", True))
True))
input_tensor_code = input_tensor_code + f""" input_tensor_code = input_tensor_code + f"""
{code_indent} auto {PREFIX_TENSOR_NAME}{input_name}_vec = PrepareData({input_name}, kernel.InputAt({kernel_param.index(input_name)}), {trans_flag}); {code_indent} auto {PREFIX_TENSOR_NAME}{input_name}_vec = PrepareData({input_name}, kernel.InputAt({kernel_param.index(input_name)}), {trans_flag});
{code_indent} paddle::optional<std::vector<const phi::DenseTensor*>> {PREFIX_TENSOR_NAME}{input_name}; {code_indent} paddle::optional<std::vector<const phi::DenseTensor*>> {PREFIX_TENSOR_NAME}{input_name};
...@@ -618,22 +640,20 @@ PADDLE_API {self.get_return_type(inplace_flag=True)} {api_func_name}({self.get_d ...@@ -618,22 +640,20 @@ PADDLE_API {self.get_return_type(inplace_flag=True)} {api_func_name}({self.get_d
{code_indent} {PREFIX_TENSOR_NAME}{input_name}->at(i) = &{PREFIX_TENSOR_NAME}{input_name}_vec->at(i); {code_indent} {PREFIX_TENSOR_NAME}{input_name}->at(i) = &{PREFIX_TENSOR_NAME}{input_name}_vec->at(i);
{code_indent} }} {code_indent} }}
{code_indent} }}""" {code_indent} }}"""
else: return input_tensor_code
input_name_tensor_map[input_name].append(
(f"{PREFIX_TENSOR_NAME}{input_name}", False))
input_tensor_code = input_tensor_code + f"""
{code_indent} auto {PREFIX_TENSOR_NAME}{input_name} = PrepareData({input_name}, kernel.InputAt({kernel_param.index(input_name)}), {trans_flag});"""
else: def gene_vec_dense_input(self,
if self.inputs['input_info'][ input_name,
input_name] == "const Tensor&": input_name_tensor_map,
input_name_tensor_map[input_name].append( code_indent=''):
(f"{PREFIX_TENSOR_NAME}{input_name}", False)) input_tensor_code = ""
input_tensor_code = input_tensor_code + f""" trans_flag = self.gene_trans_flag(input_name)
{code_indent} auto {PREFIX_TENSOR_NAME}{input_name} = PrepareData({input_name}, kernel.InputAt({kernel_param.index(input_name)}), {trans_flag});""" input_names = self.inputs['names']
attr_names = self.attrs['names']
kernel_param = self.kernel['param']
if kernel_param is None:
kernel_param = input_names + attr_names
elif self.inputs['input_info'][
input_name] == "const std::vector<Tensor>&":
if input_name in self.inplace_map.values(): if input_name in self.inplace_map.values():
input_name_tensor_map[input_name].append( input_name_tensor_map[input_name].append(
(f"{PREFIX_TENSOR_NAME}{input_name}", True)) (f"{PREFIX_TENSOR_NAME}{input_name}", True))
...@@ -641,24 +661,37 @@ PADDLE_API {self.get_return_type(inplace_flag=True)} {api_func_name}({self.get_d ...@@ -641,24 +661,37 @@ PADDLE_API {self.get_return_type(inplace_flag=True)} {api_func_name}({self.get_d
{code_indent} std::vector<const phi::DenseTensor*> {PREFIX_TENSOR_NAME}{input_name} = TensorToConstDenseTensorPtr({input_name});""" {code_indent} std::vector<const phi::DenseTensor*> {PREFIX_TENSOR_NAME}{input_name} = TensorToConstDenseTensorPtr({input_name});"""
else: else:
input_name_tensor_map[input_name].append( input_name_tensor_map[input_name].append(
(f"{PREFIX_TENSOR_NAME}{input_name}_vec", (f"{PREFIX_TENSOR_NAME}{input_name}_vec", True))
True))
input_tensor_code = input_tensor_code + f""" input_tensor_code = input_tensor_code + f"""
{code_indent} auto {PREFIX_TENSOR_NAME}{input_name}_vec = PrepareData({input_name}, kernel.InputAt({kernel_param.index(input_name)}), {trans_flag}); {code_indent} auto {PREFIX_TENSOR_NAME}{input_name}_vec = PrepareData({input_name}, kernel.InputAt({kernel_param.index(input_name)}), {trans_flag});
{code_indent} std::vector<const phi::DenseTensor*> {PREFIX_TENSOR_NAME}{input_name}({PREFIX_TENSOR_NAME}{input_name}_vec->size()); {code_indent} std::vector<const phi::DenseTensor*> {PREFIX_TENSOR_NAME}{input_name}({PREFIX_TENSOR_NAME}{input_name}_vec->size());
{code_indent} for (size_t i = 0; i < {PREFIX_TENSOR_NAME}{input_name}.size(); ++i) {{ {code_indent} for (size_t i = 0; i < {PREFIX_TENSOR_NAME}{input_name}.size(); ++i) {{
{code_indent} {PREFIX_TENSOR_NAME}{input_name}[i] = &{PREFIX_TENSOR_NAME}{input_name}_vec->at(i); {code_indent} {PREFIX_TENSOR_NAME}{input_name}[i] = &{PREFIX_TENSOR_NAME}{input_name}_vec->at(i);
{code_indent} }}""" {code_indent} }}"""
return input_tensor_code
def gene_input(self, kernel_tensor_type=None, code_indent=''):
input_names = self.inputs['names']
attr_names = self.attrs['names']
kernel_param = self.kernel['param']
if kernel_param is None:
kernel_param = input_names + attr_names
input_name_tensor_map = collections.defaultdict(list)
input_tensor_code = ""
for i, input_name in enumerate(input_names):
# set input code
if input_name in kernel_param:
# input is dense tensor
api_tensor_type = self.inputs['input_info'][input_name]
phi_tensor_type = 'dense' if kernel_tensor_type is None else kernel_tensor_type[
0][kernel_param.index(input_name)]
if api_tensor_type in self.gene_input_func.keys():
input_tensor_code += self.gene_input_func[api_tensor_type][
phi_tensor_type](input_name, input_name_tensor_map,
code_indent)
else: else:
# do nothing # do nothing
pass pass
else: # input is selected_rows
input_name_tensor_map[input_name].append(
(f"{PREFIX_TENSOR_NAME}{input_name}", False))
input_tensor_code = input_tensor_code + f"""
{code_indent} auto {PREFIX_TENSOR_NAME}{input_name} = TensorToSelectedRows({input_name});
"""
else: else:
if input_name in self.infer_meta['param']: if input_name in self.infer_meta['param']:
if input_name in self.optional_vars: if input_name in self.optional_vars:
...@@ -674,6 +707,45 @@ PADDLE_API {self.get_return_type(inplace_flag=True)} {api_func_name}({self.get_d ...@@ -674,6 +707,45 @@ PADDLE_API {self.get_return_type(inplace_flag=True)} {api_func_name}({self.get_d
else: else:
input_tensor_code = input_tensor_code + f""" input_tensor_code = input_tensor_code + f"""
{code_indent} auto {PREFIX_TENSOR_NAME}{input_name} = {input_name}.impl();""" {code_indent} auto {PREFIX_TENSOR_NAME}{input_name} = {input_name}.impl();"""
return input_name_tensor_map, input_tensor_code
def get_kernel_args(self, kernel_tensor_type=None, code_indent=''):
dense_input_trans_map = {
'const Tensor&':
'const phi::DenseTensor&',
'const std::vector<Tensor>&':
'const std::vector<const phi::DenseTensor*>&',
'const paddle::optional<Tensor&>':
'paddle::optional<const phi::DenseTensor&>',
'const paddle::optional<Tensor>&':
'const paddle::optional<phi::DenseTensor>&',
'const paddle::optional<std::vector<Tensor>>&':
'const paddle::optional<std::vector<const phi::DenseTensor*>>&'
}
dense_out_trans_map = {
'Tensor': 'phi::DenseTensor*',
'std::vector<Tensor>': 'std::vector<phi::DenseTensor*>&'
}
sr_input_trans_map = {
'const Tensor&':
'const phi::SelectedRows&',
'const paddle::optional<Tensor>&':
'const paddle::optional<phi::SelectedRows>&'
}
sr_out_trans_map = {'Tensor': 'phi::SelectedRows*'}
input_names = self.inputs['names']
input_infos = self.inputs['input_info']
kernel_args_type_list = ['const platform::DeviceContext&']
attr_names = self.attrs['names']
kernel_param = self.kernel['param']
if kernel_param is None:
kernel_param = input_names + attr_names
input_name_tensor_map, input_tensor_code = self.gene_input(
kernel_tensor_type, code_indent)
input_tensor_code = input_tensor_code + f""" input_tensor_code = input_tensor_code + f"""
{code_indent} if(platform::RecordOpInfoSupplement::IsEnabled()){{""" {code_indent} if(platform::RecordOpInfoSupplement::IsEnabled()){{"""
single_tensor_names = [] single_tensor_names = []
......
...@@ -45,6 +45,26 @@ class ForwardAPI(BaseAPI): ...@@ -45,6 +45,26 @@ class ForwardAPI(BaseAPI):
else: else:
return self.api return self.api
def gene_input(self, kernel_tensor_type=None, code_indent=''):
kernel_param = self.kernel['param']
input_name_tensor_map, input_tensor_code = super().gene_input(
kernel_tensor_type, code_indent)
# generate the input that is in view list
for i, input_name in enumerate(self.inputs['names']):
if input_name in self.view_map.values(
) and input_name not in input_name_tensor_map.keys():
if kernel_tensor_type is None or kernel_tensor_type[0][
kernel_param.index(input_name)] == 'dense':
trans_flag = self.gene_trans_flag(input_name)
input_tensor_code = input_tensor_code + f"""
{code_indent} auto {PREFIX_TENSOR_NAME}{input_name} = PrepareData({input_name}, kernel.InputAt(0), {trans_flag});"""
else:
# do nothing
pass
return input_name_tensor_map, input_tensor_code
def parse_intermediate(self, api_item_yaml): def parse_intermediate(self, api_item_yaml):
if 'intermediate' in api_item_yaml: if 'intermediate' in api_item_yaml:
intermediate_outs = [ intermediate_outs = [
...@@ -215,11 +235,15 @@ class ForwardAPI(BaseAPI): ...@@ -215,11 +235,15 @@ class ForwardAPI(BaseAPI):
if not inplace_flag and self.view_map is not None and self.outputs[ if not inplace_flag and self.view_map is not None and self.outputs[
'names'][i] in self.view_map: 'names'][i] in self.view_map:
if out_dtype_list[i] == 'Tensor':
output_create = output_create + f""" output_create = output_create + f"""
{code_indent} kernel_out_{i}->ShareBufferWith(*{PREFIX_TENSOR_NAME}{self.view_map[self.outputs['names'][i]]}); {code_indent} kernel_out_{i}->ShareBufferWith(*{PREFIX_TENSOR_NAME}{self.view_map[self.outputs['names'][i]]});
{code_indent} kernel_out_{i}->ShareInplaceVersionCounterWith(*{PREFIX_TENSOR_NAME}{self.view_map[self.outputs['names'][i]]}); {code_indent} kernel_out_{i}->ShareInplaceVersionCounterWith(*{PREFIX_TENSOR_NAME}{self.view_map[self.outputs['names'][i]]});
{code_indent} VLOG(3) << "Perform View between Output and Input Tensor, share allocation and inplace version.";""" {code_indent} VLOG(3) << "Perform View between Output and Input Tensor, share allocation and inplace version.";"""
else:
raise ValueError(
"{} : Output error: only support Tensor type when use view in yaml. But get {}"
.format(self.api, out_dtype_list[i]))
else: else:
raise ValueError( raise ValueError(
"{} : Output error: the output should not be empty.".format( "{} : Output error: the output should not be empty.".format(
......
...@@ -1928,6 +1928,18 @@ ...@@ -1928,6 +1928,18 @@
output : Tensor(x_grad) output : Tensor(x_grad)
invoke : reverse(out_grad, axis) invoke : reverse(out_grad, axis)
- backward_op : rnn_grad
forward : rnn (Tensor x, Tensor[] pre_state, Tensor[] weight_list, Tensor sequence_length, Tensor dropout_state_in, float dropout_prob, bool is_bidirec, int input_size, int hidden_size, int num_layers, str mode, int seed, bool is_test) -> Tensor(out), Tensor(dropout_state_out), Tensor[](state), Tensor(reserve)
args : (Tensor x, Tensor[] pre_state, Tensor[] weight_list, Tensor sequence_length, Tensor out, Tensor dropout_state_out, Tensor reserve, Tensor out_grad, Tensor[] state_grad, float dropout_prob, bool is_bidirec, int input_size, int hidden_size, int num_layers, str mode, int seed, bool is_test)
output : Tensor(x_grad), Tensor[](pre_state_grad){pre_state.size()}, Tensor[](weight_list_grad){weight_list.size()}
infer_meta :
func : RnnGradInferMeta
param : [x, pre_state, weight_list]
kernel :
func : rnn_grad
data_type: out_grad
optional : sequence_length
- backward_op : roi_align_grad - backward_op : roi_align_grad
forward : roi_align (Tensor x, Tensor boxes, Tensor boxes_num, int pooled_height, int pooled_width, float spatial_scale, int sampling_ratio, bool aligned) -> Tensor(out) forward : roi_align (Tensor x, Tensor boxes, Tensor boxes_num, int pooled_height, int pooled_width, float spatial_scale, int sampling_ratio, bool aligned) -> Tensor(out)
args : (Tensor x, Tensor boxes, Tensor boxes_num, Tensor out_grad, int pooled_height, int pooled_width, float spatial_scale, int sampling_ratio, bool aligned) args : (Tensor x, Tensor boxes, Tensor boxes_num, Tensor out_grad, int pooled_height, int pooled_width, float spatial_scale, int sampling_ratio, bool aligned)
......
...@@ -2991,6 +2991,21 @@ ...@@ -2991,6 +2991,21 @@
func: overlap_add func: overlap_add
backward: overlap_add_grad backward: overlap_add_grad
- op: rnn
args: (Tensor x, Tensor[] pre_state, Tensor[] weight_list, Tensor sequence_length, Tensor dropout_state_in, float dropout_prob=0.0, bool is_bidirec=false, int input_size=10, int hidden_size=100, int num_layers=1, str mode="RNN_TANH", int seed=0, bool is_test=false)
output: Tensor(out), Tensor(dropout_state_out), Tensor[](state){pre_state.size()}, Tensor(reserve)
infer_meta:
func: RnnInferMeta
param : [x, pre_state, weight_list, sequence_length, dropout_prob, is_bidirec, input_size, hidden_size, num_layers, mode, seed, is_test]
kernel:
func: rnn
param : [x, pre_state, weight_list, sequence_length, dropout_prob, is_bidirec, input_size, hidden_size, num_layers, mode, seed, is_test]
data_type: x
backward: rnn_grad
optional : sequence_length
intermediate : reserve
view : (dropout_state_in -> dropout_state_out)
- op: uniform_random_inplace - op: uniform_random_inplace
args: (Tensor x, float min, float max, int seed, int diag_num, int diag_step, float diag_val) args: (Tensor x, float min, float max, int seed, int diag_num, int diag_step, float diag_val)
output: Tensor(out) output: Tensor(out)
......
...@@ -807,6 +807,33 @@ void ReshapeDoubleGradInferMeta(const MetaTensor& out_grad, ...@@ -807,6 +807,33 @@ void ReshapeDoubleGradInferMeta(const MetaTensor& out_grad,
} }
} }
void RnnGradInferMeta(const MetaTensor& x,
const std::vector<const MetaTensor*>& pre_state,
const std::vector<const MetaTensor*>& weight_list,
MetaTensor* x_grad,
std::vector<MetaTensor*> pre_state_grad,
std::vector<MetaTensor*> weight_grad_list) {
PADDLE_ENFORCE_GT(
pre_state.size(),
0UL,
phi::errors::InvalidArgument(
"The input pre_state in RnnGradInferMeta can't be empty."));
PADDLE_ENFORCE_GT(
weight_grad_list.size(),
0UL,
phi::errors::InvalidArgument(
"The input weight_grad_list in RnnGradInferMeta can't be empty."));
if (x_grad) {
UnchangedInferMeta(x, x_grad);
}
if (pre_state_grad.size()) {
UnchangedMultiInferMeta(pre_state, pre_state_grad);
}
if (weight_grad_list.size()) {
UnchangedMultiInferMeta(weight_list, weight_grad_list);
}
}
void ScatterGradInferMeta(const MetaTensor& index, void ScatterGradInferMeta(const MetaTensor& index,
const MetaTensor& updates, const MetaTensor& updates,
const MetaTensor& out_grad, const MetaTensor& out_grad,
......
...@@ -339,6 +339,13 @@ void ReshapeDoubleGradInferMeta(const MetaTensor& out_grad, ...@@ -339,6 +339,13 @@ void ReshapeDoubleGradInferMeta(const MetaTensor& out_grad,
const MetaTensor& x_grad_grad, const MetaTensor& x_grad_grad,
MetaTensor* out_grad_grad); MetaTensor* out_grad_grad);
void RnnGradInferMeta(const MetaTensor& x,
const std::vector<const MetaTensor*>& pre_state,
const std::vector<const MetaTensor*>& weight_list,
MetaTensor* x_grad,
std::vector<MetaTensor*> pre_state_grad,
std::vector<MetaTensor*> weight_grad_list);
void ScatterGradInferMeta(const MetaTensor& index, void ScatterGradInferMeta(const MetaTensor& index,
const MetaTensor& updates, const MetaTensor& updates,
const MetaTensor& out_grad, const MetaTensor& out_grad,
......
...@@ -2496,6 +2496,14 @@ void StackInferMeta(const std::vector<const MetaTensor*>& x, ...@@ -2496,6 +2496,14 @@ void StackInferMeta(const std::vector<const MetaTensor*>& x,
void UnchangedMultiInferMeta(const std::vector<const MetaTensor*>& x, void UnchangedMultiInferMeta(const std::vector<const MetaTensor*>& x,
std::vector<MetaTensor*> out) { std::vector<MetaTensor*> out) {
PADDLE_ENFORCE_EQ(
x.size(),
out.size(),
phi::errors::InvalidArgument(
"Input's size should be equal to the output's size"
"but received input size: (%d) does not equals output_size: (%d)",
x.size(),
out.size()));
for (size_t i = 0; i < x.size(); ++i) { for (size_t i = 0; i < x.size(); ++i) {
if (out[i]) { if (out[i]) {
out[i]->share_meta(*x[i]); out[i]->share_meta(*x[i]);
......
...@@ -142,6 +142,30 @@ class TestRNNOp(OpTest): ...@@ -142,6 +142,30 @@ class TestRNNOp(OpTest):
self.check_grad(set(grad_check_list), self.check_grad(set(grad_check_list),
['Out', 'last_hidden', 'last_cell']) ['Out', 'last_hidden', 'last_cell'])
def test_grad_only_input(self):
if not self.is_test:
var_name_list = self.get_weight_names()
grad_check_list = ['Input']
grad_check_list.extend(var_name_list)
self.check_grad(set(grad_check_list),
['Out', 'last_hidden', 'last_cell'])
def test_grad_only_h(self):
if not self.is_test:
var_name_list = self.get_weight_names()
grad_check_list = ['init_h']
grad_check_list.extend(var_name_list)
self.check_grad(set(grad_check_list),
['Out', 'last_hidden', 'last_cell'])
def test_grad_only_c(self):
if not self.is_test:
var_name_list = self.get_weight_names()
grad_check_list = ['init_c']
grad_check_list.extend(var_name_list)
self.check_grad(set(grad_check_list),
['Out', 'last_hidden', 'last_cell'])
class TestRNNOp1(TestRNNOp): class TestRNNOp1(TestRNNOp):
......
...@@ -1012,7 +1012,13 @@ class RNNBase(LayerList): ...@@ -1012,7 +1012,13 @@ class RNNBase(LayerList):
if not self.time_major: if not self.time_major:
inputs = paddle.tensor.transpose(inputs, [1, 0, 2]) inputs = paddle.tensor.transpose(inputs, [1, 0, 2])
if in_dynamic_mode(): if in_dygraph_mode():
out, _, state = _C_ops.rnn(
inputs, initial_states, self._all_weights, sequence_length,
self._dropout_state, self.dropout, self.num_directions == 2,
self.input_size, self.hidden_size, self.num_layers, self.mode,
0, not self.training)
elif in_dynamic_mode():
_, _, out, state = _legacy_C_ops.rnn( _, _, out, state = _legacy_C_ops.rnn(
inputs, initial_states, self._all_weights, sequence_length, inputs, initial_states, self._all_weights, sequence_length,
self._dropout_state, self.state_components, 'dropout_prob', self._dropout_state, self.state_components, 'dropout_prob',
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册