From e3574f729f8bb3d4bd37dc25fb7a1918d36ed44b Mon Sep 17 00:00:00 2001 From: Hui Zhang Date: Mon, 22 Aug 2022 10:50:25 +0800 Subject: [PATCH] [jit] add jit layer function default constructor (#45169) * fix jit layer function * fix comment * fix comment --- paddle/fluid/jit/function.cc | 8 ++++++++ paddle/fluid/jit/function.h | 3 +++ paddle/fluid/jit/layer_test.cc | 7 +++++++ 3 files changed, 18 insertions(+) diff --git a/paddle/fluid/jit/function.cc b/paddle/fluid/jit/function.cc index 0d297da500..5c4c3af960 100644 --- a/paddle/fluid/jit/function.cc +++ b/paddle/fluid/jit/function.cc @@ -30,12 +30,20 @@ Function::Function(BaseEngine* engine) : engine_(engine) {} std::vector Function::operator()( const std::vector& inputs) const { + PADDLE_ENFORCE_EQ(IsValid(), + true, + phi::errors::PreconditionNotMet( + "Funtion engine ptr is nullptr, please check it.")); auto dense_tensors = utils::ToDenseTensors(inputs); return utils::ToTensors(this->operator()(dense_tensors)); } std::vector Function::operator()( const std::vector& inputs) const { + PADDLE_ENFORCE_EQ(IsValid(), + true, + phi::errors::PreconditionNotMet( + "Funtion engine ptr is nullptr, please check it.")); return (*engine_)(inputs); } diff --git a/paddle/fluid/jit/function.h b/paddle/fluid/jit/function.h index daaecd55bf..a80d46d639 100644 --- a/paddle/fluid/jit/function.h +++ b/paddle/fluid/jit/function.h @@ -27,6 +27,7 @@ using Tensor = paddle::experimental::Tensor; class Function { public: + Function() : engine_(nullptr) {} explicit Function(BaseEngine* engine); std::vector operator()(const std::vector& inputs) const; @@ -34,6 +35,8 @@ class Function { std::vector operator()( const std::vector& inputs) const; + bool IsValid() const { return engine_ != nullptr; } + ~Function() = default; private: diff --git a/paddle/fluid/jit/layer_test.cc b/paddle/fluid/jit/layer_test.cc index b54ea3c4aa..360eebe8b4 100644 --- a/paddle/fluid/jit/layer_test.cc +++ b/paddle/fluid/jit/layer_test.cc @@ -69,6 +69,11 @@ std::vector PrepareInputs(const phi::Place& place) { return utils::ToTensors({t}); } +TEST(CpuLayerTest, Function) { + auto func_null = Function(); + EXPECT_TRUE(!func_null.IsValid()); +} + TEST(CpuLayerTest, Construct) { auto place = phi::CPUPlace(); std::string path = "./multi_program_load/export"; @@ -103,6 +108,7 @@ TEST(CpuLayerTest, Construct) { EXPECT_NEAR(out_data[0], 0.02194316, 1e-6); auto func = layer.Function("infer"); + EXPECT_TRUE(func.IsValid()); outs = func(inputs); out_data = outs[0].data(); EXPECT_NEAR(out_data[0], 1.41562390, 1e-6); @@ -128,6 +134,7 @@ TEST(GpuLayerTest, Construct) { EXPECT_NEAR(out_data[0], 0.02194316, 1e-6); auto func = layer.Function("infer"); + EXPECT_TRUE(func.IsValid()); outs = func(inputs); gpu_tensor = outs[0]; cpu_tensor = paddle::experimental::copy_to(gpu_tensor, phi::CPUPlace(), true); -- GitLab