diff --git a/paddle/fluid/lite/core/kernel.h b/paddle/fluid/lite/core/kernel.h index 2eee83bd4a51f2d90ddbf81055146403c78314fa..629da86bbdd6e89f74cd5144fc5529c8c5d0df85 100644 --- a/paddle/fluid/lite/core/kernel.h +++ b/paddle/fluid/lite/core/kernel.h @@ -41,8 +41,24 @@ class KernelBase { const std::map& input_types, const std::string& out_arg)>; + protected: + /// Run some initialization before `Run`, it will invoke after `SetParam` and + /// `SetContext`, that is both the param_ and context_ are valid. + virtual void PrepareForRun() {} + + /// Run the kernel. Before Run, both the param_ and context_ should be valid. virtual void Run() = 0; + public: + void Launch() { + if (is_first_epoch_) { + PrepareForRun(); + is_first_epoch_ = false; + } + + Run(); + } + void SetContext(std::unique_ptr&& ctx) { ctx_ = std::move(ctx); } @@ -141,6 +157,7 @@ class KernelBase { // The extra identity to help defficiate a specific kernel, op_type_ + alias_ // is the unique ID for the kernel. std::string alias_{}; + bool is_first_epoch_{true}; }; // Light-weight kernel implementation. diff --git a/paddle/fluid/lite/core/op_lite.cc b/paddle/fluid/lite/core/op_lite.cc index bd98b23bf25e8682be87714203e3d388cc9df55c..dc22e4fb4b46a3d04b6dce8b9a703ac6a73ccc70 100644 --- a/paddle/fluid/lite/core/op_lite.cc +++ b/paddle/fluid/lite/core/op_lite.cc @@ -62,7 +62,7 @@ bool OpLite::Run() { CHECK(kernel_); SyncInputEvents(); - kernel_->Run(); + kernel_->Launch(); RecordOutputEvents(); return true; diff --git a/paddle/fluid/lite/core/program.h b/paddle/fluid/lite/core/program.h index 1ebd6b437c7a78d2de5e5186950b0551be41fd75..234626a91e9e2778284f97666cfb0eab706b5a55 100644 --- a/paddle/fluid/lite/core/program.h +++ b/paddle/fluid/lite/core/program.h @@ -129,7 +129,7 @@ struct Instruct { CHECK(op_->CheckShape()); } op_->InferShape(); - kernel_->Run(); + kernel_->Launch(); } friend std::ostream& operator<<(std::ostream& os, const Instruct& other) {