未验证 提交 64696b9b 编写于 作者: W Wang Xin 提交者: GitHub

static graph autogen code support for exponential_ op (#54734)

* static graph autogen code support for exponential_ op

* set default value
上级 27edf0b2
/* Copyright (c) 2021 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/fluid/framework/infershape_utils.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/phi/infermeta/unary.h"
namespace paddle {
namespace operators {
class ExponentialOp : public framework::OperatorWithKernel {
public:
using framework::OperatorWithKernel::OperatorWithKernel;
protected:
phi::KernelKey GetExpectedKernelType(
const framework::ExecutionContext &ctx) const override {
return phi::KernelKey(OperatorWithKernel::IndicateVarDataType(ctx, "X"),
ctx.GetPlace());
}
};
class ExponentialOpMaker : public framework::OpProtoAndCheckerMaker {
public:
void Make() override {
AddComment(R"DOC(
This operator fills the input tensor with random values sampled from a
exponential distribution.
)DOC");
AddInput("X", "The input tensor.");
AddOutput("Out", "The output tensor of exponential OP.");
AddAttr<float>(
"lambda", "lambd parameter of exponential distribution. [default 1.0].")
.SetDefault(1.0f);
}
};
template <typename T>
class ExponentialGradOpMaker : public framework::SingleGradOpMaker<T> {
public:
using framework::SingleGradOpMaker<T>::SingleGradOpMaker;
protected:
void Apply(GradOpPtr<T> retv) const override {
retv->SetType("fill_any_like");
retv->SetInput("X", this->OutputGrad("Out"));
retv->SetAttr("value", 0.0f);
retv->SetOutput("Out", this->InputGrad("X"));
}
};
} // namespace operators
} // namespace paddle
namespace ops = paddle::operators;
namespace plat = paddle::platform;
DECLARE_INPLACE_OP_INFERER(ExponentialInferer, {"X", "Out"});
DECLARE_INFER_SHAPE_FUNCTOR(exponential,
ExponentialInfershapeFunctor,
PD_INFER_META(phi::UnchangedInferMeta));
REGISTER_OPERATOR(exponential,
ops::ExponentialOp,
ops::ExponentialOpMaker,
ops::ExponentialGradOpMaker<paddle::framework::OpDesc>,
ops::ExponentialGradOpMaker<paddle::imperative::OpBase>,
ExponentialInferer,
ExponentialInfershapeFunctor);
...@@ -29,6 +29,12 @@ def to_named_dict(items: List[Dict], is_op=False) -> Dict[str, Dict]: ...@@ -29,6 +29,12 @@ def to_named_dict(items: List[Dict], is_op=False) -> Dict[str, Dict]:
item["name"] = ( item["name"] = (
item["name"] if item["name"][-1] != '_' else item["name"][:-1] item["name"] if item["name"][-1] != '_' else item["name"][:-1]
) )
if "forward" in item:
item["forward"]["name"] = (
item["forward"]["name"]
if item["forward"]["name"][-1] != '_'
else item["forward"]["name"][:-1]
)
name = item["name"] name = item["name"]
named_dict[name] = item named_dict[name] = item
else: else:
......
...@@ -932,6 +932,15 @@ ...@@ -932,6 +932,15 @@
extra : extra :
attrs : [bool use_mkldnn = false, bool use_cudnn = false] attrs : [bool use_mkldnn = false, bool use_cudnn = false]
- op : exponential_
backward : exponential__grad
inputs :
x : X
outputs :
out : Out
attrs :
lam : lambda
- op : eye - op : eye
outputs : outputs :
out : Out out : Out
......
...@@ -89,6 +89,14 @@ ...@@ -89,6 +89,14 @@
data_type : out_grad data_type : out_grad
no_need_buffer : weight no_need_buffer : weight
- backward_op : exponential__grad
forward : exponential_ (Tensor x, float lam=1.0f) -> Tensor(out)
args : (Tensor out_grad)
output : Tensor(x_grad)
infer_meta :
func : UnchangedInferMeta
invoke : full_like(out_grad, 0.0f)
- backward_op : frobenius_norm_grad - backward_op : frobenius_norm_grad
forward: frobenius_norm (Tensor x, IntArray axis={0}, bool keepdim=false, bool reduce_all=false, int in_dtype=-1, int out_dtype=-1) -> Tensor(out) forward: frobenius_norm (Tensor x, IntArray axis={0}, bool keepdim=false, bool reduce_all=false, int in_dtype=-1, int out_dtype=-1) -> Tensor(out)
args : (Tensor x, Tensor out, Tensor out_grad, IntArray axis={0}, bool keepdim=false, bool reduce_all=false, int in_dtype=-1, int out_dtype=-1) args : (Tensor x, Tensor out, Tensor out_grad, IntArray axis={0}, bool keepdim=false, bool reduce_all=false, int in_dtype=-1, int out_dtype=-1)
......
...@@ -182,6 +182,17 @@ ...@@ -182,6 +182,17 @@
backend : x backend : x
force_backend : force_cpu force_backend : force_cpu
- op : exponential_
args : (Tensor x, float lam = 1.0f)
output : Tensor(out)
infer_meta :
func : UnchangedInferMeta
param : [x]
kernel :
func : exponential
inplace : (x -> out)
backward : exponential__grad
- op : eye - op : eye
args : (Scalar(int64_t) num_rows, Scalar(int64_t) num_columns = -1, DataType dtype = DataType::FLOAT32) args : (Scalar(int64_t) num_rows, Scalar(int64_t) num_columns = -1, DataType dtype = DataType::FLOAT32)
output : Tensor(out) output : Tensor(out)
......
/* Copyright (c) 2022 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/phi/core/compat/op_utils.h"
namespace phi {
KernelSignature ExponentialOpArgumentMapping(
const ArgumentMappingContext& ctx UNUSED) {
return KernelSignature("exponential", {"X"}, {"lambda"}, {"Out"});
}
} // namespace phi
PD_REGISTER_ARG_MAPPING_FN(exponential, phi::ExponentialOpArgumentMapping);
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册