From a8bc997c0bb441c9c8355935201a1753747244cd Mon Sep 17 00:00:00 2001 From: jiangcheng Date: Thu, 16 Sep 2021 16:06:36 +0800 Subject: [PATCH] add REGISTER_PIANO_OP_WITHOUT_MAKER (#48) --- .../fluid/compiler/paddle2piano/piano_op_registry.cc | 5 +++++ .../fluid/compiler/paddle2piano/piano_op_registry.h | 11 ++++++----- .../compiler/paddle2piano/piano_op_registry_test.cc | 7 ++++++- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/paddle/fluid/compiler/paddle2piano/piano_op_registry.cc b/paddle/fluid/compiler/paddle2piano/piano_op_registry.cc index 131001736b405..c3fcb47abd0a7 100644 --- a/paddle/fluid/compiler/paddle2piano/piano_op_registry.cc +++ b/paddle/fluid/compiler/paddle2piano/piano_op_registry.cc @@ -22,6 +22,11 @@ limitations under the License. */ namespace paddle { namespace piano { +PianoOpRegistry& PianoOpRegistry::Instance() { + static PianoOpRegistry r; + return r; +} + void PianoOpRegistry::RegisterBackend( const std::string& backend_name, const std::unordered_set& supported_types, diff --git a/paddle/fluid/compiler/paddle2piano/piano_op_registry.h b/paddle/fluid/compiler/paddle2piano/piano_op_registry.h index 858a2d30d29d0..09d4c29e7753d 100644 --- a/paddle/fluid/compiler/paddle2piano/piano_op_registry.h +++ b/paddle/fluid/compiler/paddle2piano/piano_op_registry.h @@ -99,10 +99,7 @@ class PianoOpRegistry final { template friend class PianoOpRegistrar; - static PianoOpRegistry& Instance() { - static PianoOpRegistry r; - return r; - } + static PianoOpRegistry& Instance(); PianoOpRegistry() = default; ~PianoOpRegistry() = default; @@ -158,7 +155,7 @@ class PianoOpMakerBase { class PianoOpMaker : public PianoOpMakerBase { public: - virtual void Make() = 0; + virtual void Make() {} virtual ~PianoOpMaker() = default; @@ -231,6 +228,10 @@ class PianoOpRegistrar final : public framework::Registrar { #define REGISTER_PIANO_OP(op_type, op_maker, op_kernel) \ REGISTER_PIANO_OP_EX(op_type, PLAIN, op_maker, op_kernel) +#define REGISTER_PIANO_OP_WITHOUT_MAKER(op_type, op_kernel) \ + class op_type##PianoOpMaker : public ::paddle::piano::PianoOpMaker {}; \ + REGISTER_PIANO_OP(op_type, op_type##PianoOpMaker, op_kernel) + #define REGISTER_PIANO_OP_EX(TYPE, LIB, MAKER, KERNEL) \ STATIC_ASSERT_GLOBAL_NAMESPACE( \ __reg_piano_op__##TYPE##_##LIB, \ diff --git a/paddle/fluid/compiler/paddle2piano/piano_op_registry_test.cc b/paddle/fluid/compiler/paddle2piano/piano_op_registry_test.cc index 53e5c65f3d2ba..0044c7a15efda 100644 --- a/paddle/fluid/compiler/paddle2piano/piano_op_registry_test.cc +++ b/paddle/fluid/compiler/paddle2piano/piano_op_registry_test.cc @@ -80,6 +80,8 @@ REGISTER_OP_WITHOUT_GRADIENT(op_not_piano, paddle::piano::TestOp, paddle::piano::TestOpMaker); REGISTER_OP_WITHOUT_GRADIENT(test_limit_backend, paddle::piano::TestOp, paddle::piano::TestOpMaker); +REGISTER_OP_WITHOUT_GRADIENT(test_without_maker, paddle::piano::TestOp, + paddle::piano::TestOpMaker); // register backend which support some datetype but all op REGISTER_PIANO_BACKEND(PIANO_JIT_TEST_TYPE, paddle::piano::TestDatatypes()) @@ -120,6 +122,8 @@ REGISTER_PIANO_OP(test, paddle::piano::TestPianoOpMaker, REGISTER_PIANO_OP(test_limit_backend, paddle::piano::TestPianoOpWithAllowBackendMaker, paddle::piano::TestPianoOpKernel) +REGISTER_PIANO_OP_WITHOUT_MAKER(test_without_maker, + paddle::piano::TestPianoOpKernel) namespace paddle { namespace piano { @@ -147,7 +151,8 @@ TEST(TestPianoOpRegistry, CheckPianoOpRegistered) { // check register store OK auto ops = PianoOpRegistry::AllPianoOps(); std::stable_sort(ops.begin(), ops.end()); - ASSERT_EQ(ops, std::vector({"test", "test_limit_backend"})); + ASSERT_EQ(ops, std::vector( + {"test", "test_limit_backend", "test_without_maker"})); // check piano op's attribute OK const auto& attrs = PianoOpRegistry::Attrs("test");