Add some ops for training (#17442)
parent
72fb4adba1
commit
e9f333207d
@ -0,0 +1 @@
|
||||
include_directories(lite)
|
@ -0,0 +1,46 @@
|
||||
import numpy
|
||||
import sys, os
|
||||
import numpy as np
|
||||
import paddle.fluid as fluid
|
||||
from paddle.fluid.backward import append_backward
|
||||
|
||||
a = fluid.layers.data(name="a", shape=[100], dtype='float32')
|
||||
label = fluid.layers.data(name="label", shape=[100], dtype='float32')
|
||||
|
||||
a1 = fluid.layers.fc(input=a, size=500, act=None, bias_attr=False)
|
||||
|
||||
cost = fluid.layers.square_error_cost(a1, label)
|
||||
avg_cost = fluid.layers.mean(cost)
|
||||
|
||||
optimizer = fluid.optimizer.SGD(learning_rate=0.001)
|
||||
optimizer.minimize(cost)
|
||||
|
||||
cpu = fluid.core.CPUPlace()
|
||||
loss = exe = fluid.Executor(cpu)
|
||||
|
||||
exe.run(fluid.default_startup_program())
|
||||
with open('startup_program.pb', 'wb') as f:
|
||||
f.write(fluid.default_startup_program().desc.serialize_to_string())
|
||||
|
||||
data_1 = np.array(numpy.random.random([100, 100]), dtype='float32')
|
||||
|
||||
#fluid.default_main_program().desc.
|
||||
|
||||
|
||||
|
||||
#prog = fluid.compiler.CompiledProgram(fluid.default_main_program())
|
||||
prog = fluid.default_main_program()
|
||||
|
||||
#append_backward(loss)
|
||||
|
||||
with open('main_program.pb', 'wb') as f:
|
||||
f.write(prog.desc.serialize_to_string())
|
||||
|
||||
|
||||
#outs = exe.run(program=prog, feed={'a':data_1, }, fetch_list=[cost])
|
||||
|
||||
sys.exit(0)
|
||||
fluid.io.save_inference_model("./model2", [a.name], [a1], exe)
|
||||
|
||||
print(numpy.array(outs))
|
||||
|
@ -0,0 +1,6 @@
|
||||
if(NOT LITE_WITH_X86)
|
||||
return()
|
||||
endif()
|
||||
|
||||
cc_library(activation_compute SRCS activation_compute.cc DEPS ${lite_kernel_deps} activation_op)
|
||||
cc_library(elementwise_compute SRCS elementwise_compute.cc DEPS ${lite_kernel_deps} elementwise_op)
|
@ -0,0 +1,108 @@
|
||||
#include "paddle/fluid/framework/eigen.h"
|
||||
#include "paddle/fluid/framework/operator.h"
|
||||
#include "paddle/fluid/lite/core/kernel.h"
|
||||
#include "paddle/fluid/lite/core/op_registry.h"
|
||||
#include "paddle/fluid/operators/activation_op.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace lite {
|
||||
namespace kernels {
|
||||
namespace x86 {
|
||||
|
||||
template <typename Functor>
|
||||
void Activate(const platform::CPUDeviceContext& context,
|
||||
const framework::LoDTensor* X, framework::LoDTensor* Out) {
|
||||
using T = typename Functor::ELEMENT_TYPE;
|
||||
auto* place = context.eigen_device();
|
||||
auto x =
|
||||
framework::EigenVector<T>::Flatten(paddle::operators::detail::Ref(X));
|
||||
auto out =
|
||||
framework::EigenVector<T>::Flatten(paddle::operators::detail::Ref(Out));
|
||||
Functor()(*place, x, out);
|
||||
}
|
||||
|
||||
template <typename Functor>
|
||||
void ActivateGrad(const platform::CPUDeviceContext& context,
|
||||
const framework::LoDTensor* X,
|
||||
const framework::LoDTensor* Out,
|
||||
const framework::LoDTensor* Out_grad,
|
||||
framework::LoDTensor* X_grad) {
|
||||
using T = typename Functor::ELEMENT_TYPE;
|
||||
auto* place = context.eigen_device();
|
||||
auto x =
|
||||
framework::EigenVector<T>::Flatten(paddle::operators::detail::Ref(X));
|
||||
auto out =
|
||||
framework::EigenVector<T>::Flatten(paddle::operators::detail::Ref(Out));
|
||||
auto x_grad = framework::EigenVector<T>::Flatten(
|
||||
paddle::operators::detail::Ref(X_grad));
|
||||
auto out_grad = framework::EigenVector<T>::Flatten(
|
||||
paddle::operators::detail::Ref(Out_grad));
|
||||
Functor()(*place, x, out, out_grad, x_grad);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
class SquareCompute : public KernelLite<TARGET(kHost), PRECISION(kFloat)> {
|
||||
public:
|
||||
using param_t = operators::ActivationParam;
|
||||
|
||||
void Run() override {
|
||||
auto& context = context_->As<X86Context>();
|
||||
auto& param = *param_.get_mutable<operators::ActivationParam>();
|
||||
CHECK(context.x86_device_context);
|
||||
|
||||
param.Out->template mutable_data<T>();
|
||||
Activate<paddle::operators::SquareFunctor<T>>(*context.x86_device_context,
|
||||
¶m.X->raw_tensor(),
|
||||
¶m.Out->raw_tensor());
|
||||
}
|
||||
|
||||
// TargetType target() const override;
|
||||
// PrecisionType precision() const override;
|
||||
|
||||
virtual ~SquareCompute() = default;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class SquareGradCompute : public KernelLite<TARGET(kHost), PRECISION(kFloat)> {
|
||||
public:
|
||||
using param_t = operators::ActivationGradParam;
|
||||
|
||||
void Run() override {
|
||||
auto& context = context_->As<X86Context>();
|
||||
auto& param = *param_.get_mutable<operators::ActivationGradParam>();
|
||||
CHECK(context.x86_device_context);
|
||||
param.X_grad->template mutable_data<T>();
|
||||
|
||||
ActivateGrad<paddle::operators::SquareGradFunctor<T>>(
|
||||
*context.x86_device_context, ¶m.X->raw_tensor(),
|
||||
¶m.Out->raw_tensor(), ¶m.Out_grad->raw_tensor(),
|
||||
¶m.X_grad->raw_tensor());
|
||||
}
|
||||
|
||||
// TargetType target() const override;
|
||||
// PrecisionType precision() const override;
|
||||
|
||||
virtual ~SquareGradCompute() = default;
|
||||
};
|
||||
|
||||
} // namespace x86
|
||||
} // namespace kernels
|
||||
} // namespace lite
|
||||
} // namespace paddle
|
||||
|
||||
// float
|
||||
REGISTER_LITE_KERNEL(square, kX86, kFloat, kNCHW,
|
||||
paddle::lite::kernels::x86::SquareCompute<float>, def)
|
||||
.BindInput("Input", {LiteType::GetTensorTy(TARGET(kHost))})
|
||||
.BindInput("Bias", {LiteType::GetTensorTy(TARGET(kHost))})
|
||||
.BindInput("W", {LiteType::GetTensorTy(TARGET(kHost))})
|
||||
.BindOutput("Out", {LiteType::GetTensorTy(TARGET(kHost))})
|
||||
.Finalize();
|
||||
|
||||
REGISTER_LITE_KERNEL(square_grad, kX86, kFloat, kNCHW,
|
||||
paddle::lite::kernels::x86::SquareGradCompute<float>, def)
|
||||
.BindInput("Input", {LiteType::GetTensorTy(TARGET(kHost))})
|
||||
.BindInput("Bias", {LiteType::GetTensorTy(TARGET(kHost))})
|
||||
.BindInput("W", {LiteType::GetTensorTy(TARGET(kHost))})
|
||||
.BindOutput("Out", {LiteType::GetTensorTy(TARGET(kHost))})
|
||||
.Finalize();
|
@ -0,0 +1,56 @@
|
||||
#include "paddle/fluid/framework/eigen.h"
|
||||
#include "paddle/fluid/framework/operator.h"
|
||||
#include "paddle/fluid/lite/core/kernel.h"
|
||||
#include "paddle/fluid/lite/core/op_registry.h"
|
||||
#include "paddle/fluid/operators/activation_op.h"
|
||||
#include "paddle/fluid/operators/elementwise/elementwise_op.h"
|
||||
#include "paddle/fluid/operators/elementwise/elementwise_op_function.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace lite {
|
||||
namespace kernels {
|
||||
namespace x86 {
|
||||
|
||||
template <typename T>
|
||||
struct SubFunctor {
|
||||
inline HOSTDEVICE T operator()(T a, T b) const { return a - b; }
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class ElementwiseSubCompute
|
||||
: public KernelLite<TARGET(kHost), PRECISION(kFloat)> {
|
||||
public:
|
||||
using param_t = operators::ElementwiseParam;
|
||||
|
||||
void Run() override {
|
||||
auto& param = *param_.get_mutable<param_t>();
|
||||
auto& context = context_->As<X86Context>();
|
||||
CHECK(context.x86_device_context);
|
||||
|
||||
param.Out->template mutable_data<T>();
|
||||
paddle::operators::ElementwiseComputeEx<SubFunctor<T>,
|
||||
platform::CPUDeviceContext, T>(
|
||||
*context.x86_execution_context, ¶m.X->raw_tensor(),
|
||||
¶m.Y->raw_tensor(), param.axis, SubFunctor<T>(),
|
||||
¶m.Out->raw_tensor());
|
||||
}
|
||||
|
||||
// TargetType target() const override;
|
||||
// PrecisionType precision() const override;
|
||||
|
||||
virtual ~ElementwiseSubCompute() = default;
|
||||
};
|
||||
|
||||
} // namespace x86
|
||||
} // namespace kernels
|
||||
} // namespace lite
|
||||
} // namespace paddle
|
||||
|
||||
// float
|
||||
REGISTER_LITE_KERNEL(square, kHost, kFloat, kNCHW,
|
||||
paddle::lite::kernels::x86::ElementwiseSubCompute<float>,
|
||||
def)
|
||||
.BindInput("X", {LiteType::GetTensorTy(TARGET(kX86))})
|
||||
.BindInput("Y", {LiteType::GetTensorTy(TARGET(kX86))})
|
||||
.BindOutput("Out", {LiteType::GetTensorTy(TARGET(kX86))})
|
||||
.Finalize();
|
@ -0,0 +1,37 @@
|
||||
#include "paddle/fluid/lite/core/op_lite.h"
|
||||
#include "paddle/fluid/lite/core/op_registry.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace lite {
|
||||
namespace operators {
|
||||
|
||||
class ActivationOp : public OpLite {
|
||||
public:
|
||||
explicit ActivationOp(const std::string& type) : OpLite(type) {}
|
||||
|
||||
bool CheckShape() const override { return true; }
|
||||
|
||||
bool InferShape() const override {
|
||||
param_.Out->Resize(param_.X->dims());
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AttachImpl(const OpDesc& opdesc, lite::Scope* scope) override {
|
||||
auto X_name = opdesc.Input("X").front();
|
||||
auto Out_name = opdesc.Output("Out").front();
|
||||
|
||||
param_.X = GetVar<lite::Tensor>(scope, X_name);
|
||||
param_.Out = GetMutableVar<Tensor>(scope, Out_name);
|
||||
}
|
||||
|
||||
void AttachKernel(KernelBase* kernel) override { kernel->SetParam(param_); }
|
||||
|
||||
private:
|
||||
mutable ActivationParam param_;
|
||||
};
|
||||
|
||||
} // namespace operators
|
||||
} // namespace lite
|
||||
} // namespace paddle
|
||||
|
||||
REGISTER_LITE_OP(square, paddle::lite::operators::ActivationOp);
|
@ -0,0 +1,47 @@
|
||||
#include "paddle/fluid/lite/core/op_lite.h"
|
||||
#include "paddle/fluid/lite/core/op_registry.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace lite {
|
||||
namespace operators {
|
||||
|
||||
class ElementwiseOp : public OpLite {
|
||||
public:
|
||||
explicit ElementwiseOp(const std::string& type) : OpLite(type) {}
|
||||
|
||||
bool CheckShape() const override {
|
||||
CHECK_OR_FALSE(param_.X);
|
||||
CHECK_OR_FALSE(param_.Y);
|
||||
CHECK_OR_FALSE(param_.Out);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool InferShape() const override {
|
||||
CHECK_OR_FALSE(param_.X->dims() == param_.Y->dims());
|
||||
param_.Out->Resize(param_.X->dims());
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AttachImpl(const OpDesc& opdesc, lite::Scope* scope) override {
|
||||
CHECK_EQ(opdesc.Inputs().size(), 2UL);
|
||||
auto X_name = opdesc.Input("X").front();
|
||||
auto Y_name = opdesc.Input("Y").front();
|
||||
auto Out_name = opdesc.Output("Out").front();
|
||||
|
||||
param_.X = GetVar<lite::Tensor>(scope, X_name);
|
||||
param_.Y = GetVar<lite::Tensor>(scope, Y_name);
|
||||
param_.Out = GetMutableVar<Tensor>(scope, Out_name);
|
||||
param_.axis = boost::get<int>(opdesc.GetAttr("axis"));
|
||||
}
|
||||
|
||||
void AttachKernel(KernelBase* kernel) override { kernel->SetParam(param_); }
|
||||
|
||||
private:
|
||||
mutable operators::ElementwiseParam param_;
|
||||
};
|
||||
|
||||
} // namespace operators
|
||||
} // namespace lite
|
||||
} // namespace paddle
|
||||
|
||||
REGISTER_LITE_OP(elementwise_sub, paddle::lite::operators::ElementwiseOp);
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue