未验证 提交 e3574f72 编写于 作者: H Hui Zhang 提交者: GitHub

[jit] add jit layer function default constructor (#45169)

* fix jit layer function

* fix comment

* fix comment
上级 307ad60d
...@@ -30,12 +30,20 @@ Function::Function(BaseEngine* engine) : engine_(engine) {} ...@@ -30,12 +30,20 @@ Function::Function(BaseEngine* engine) : engine_(engine) {}
std::vector<Tensor> Function::operator()( std::vector<Tensor> Function::operator()(
const std::vector<Tensor>& inputs) const { const std::vector<Tensor>& inputs) const {
PADDLE_ENFORCE_EQ(IsValid(),
true,
phi::errors::PreconditionNotMet(
"Funtion engine ptr is nullptr, please check it."));
auto dense_tensors = utils::ToDenseTensors(inputs); auto dense_tensors = utils::ToDenseTensors(inputs);
return utils::ToTensors(this->operator()(dense_tensors)); return utils::ToTensors(this->operator()(dense_tensors));
} }
std::vector<DenseTensor> Function::operator()( std::vector<DenseTensor> Function::operator()(
const std::vector<DenseTensor>& inputs) const { const std::vector<DenseTensor>& inputs) const {
PADDLE_ENFORCE_EQ(IsValid(),
true,
phi::errors::PreconditionNotMet(
"Funtion engine ptr is nullptr, please check it."));
return (*engine_)(inputs); return (*engine_)(inputs);
} }
......
...@@ -27,6 +27,7 @@ using Tensor = paddle::experimental::Tensor; ...@@ -27,6 +27,7 @@ using Tensor = paddle::experimental::Tensor;
class Function { class Function {
public: public:
Function() : engine_(nullptr) {}
explicit Function(BaseEngine* engine); explicit Function(BaseEngine* engine);
std::vector<Tensor> operator()(const std::vector<Tensor>& inputs) const; std::vector<Tensor> operator()(const std::vector<Tensor>& inputs) const;
...@@ -34,6 +35,8 @@ class Function { ...@@ -34,6 +35,8 @@ class Function {
std::vector<DenseTensor> operator()( std::vector<DenseTensor> operator()(
const std::vector<DenseTensor>& inputs) const; const std::vector<DenseTensor>& inputs) const;
bool IsValid() const { return engine_ != nullptr; }
~Function() = default; ~Function() = default;
private: private:
......
...@@ -69,6 +69,11 @@ std::vector<Tensor> PrepareInputs(const phi::Place& place) { ...@@ -69,6 +69,11 @@ std::vector<Tensor> PrepareInputs(const phi::Place& place) {
return utils::ToTensors({t}); return utils::ToTensors({t});
} }
TEST(CpuLayerTest, Function) {
auto func_null = Function();
EXPECT_TRUE(!func_null.IsValid());
}
TEST(CpuLayerTest, Construct) { TEST(CpuLayerTest, Construct) {
auto place = phi::CPUPlace(); auto place = phi::CPUPlace();
std::string path = "./multi_program_load/export"; std::string path = "./multi_program_load/export";
...@@ -103,6 +108,7 @@ TEST(CpuLayerTest, Construct) { ...@@ -103,6 +108,7 @@ TEST(CpuLayerTest, Construct) {
EXPECT_NEAR(out_data[0], 0.02194316, 1e-6); EXPECT_NEAR(out_data[0], 0.02194316, 1e-6);
auto func = layer.Function("infer"); auto func = layer.Function("infer");
EXPECT_TRUE(func.IsValid());
outs = func(inputs); outs = func(inputs);
out_data = outs[0].data<float>(); out_data = outs[0].data<float>();
EXPECT_NEAR(out_data[0], 1.41562390, 1e-6); EXPECT_NEAR(out_data[0], 1.41562390, 1e-6);
...@@ -128,6 +134,7 @@ TEST(GpuLayerTest, Construct) { ...@@ -128,6 +134,7 @@ TEST(GpuLayerTest, Construct) {
EXPECT_NEAR(out_data[0], 0.02194316, 1e-6); EXPECT_NEAR(out_data[0], 0.02194316, 1e-6);
auto func = layer.Function("infer"); auto func = layer.Function("infer");
EXPECT_TRUE(func.IsValid());
outs = func(inputs); outs = func(inputs);
gpu_tensor = outs[0]; gpu_tensor = outs[0];
cpu_tensor = paddle::experimental::copy_to(gpu_tensor, phi::CPUPlace(), true); cpu_tensor = paddle::experimental::copy_to(gpu_tensor, phi::CPUPlace(), true);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册