未验证 提交 523f46fe 编写于 作者: L Leo Chen 提交者: GitHub

change metaclass of Layer from pybind11_builtins.pybind11_type to type (#35538)

* change metaclass of Layer from pybind11_builtins.pybind11_type to type

* fix cast

* add ut
上级 fe4deac9
......@@ -1791,7 +1791,11 @@ void BindImperative(py::module *m_ptr) {
.def_property_readonly("type", &imperative::VarBase::Type)
.def_property_readonly("dtype", &imperative::VarBase::DataType);
py::class_<imperative::Layer, Layer /* <--- trampoline*/> layer(m, "Layer");
// NOTE(zhiqiu): set the metaclass of Layer.
// See details: https://github.com/pybind/pybind11/pull/679
// https://github.com/pybind/pybind11/blob/028812ae7eee307dca5f8f69d467af7b92cc41c8/tests/test_methods_and_attributes.cpp#L284
py::class_<imperative::Layer, Layer /* <--- trampoline*/> layer(
m, "Layer", py::metaclass((PyObject *)&PyType_Type)); // NOLINT
layer.def(py::init<>())
.def("forward",
[](imperative::Layer &self,
......
......@@ -841,6 +841,14 @@ class TestDygraphGuardWithError(unittest.TestCase):
y = fluid.layers.matmul(x, x)
class TestMetaclass(unittest.TestCase):
def test_metaclass(self):
self.assertEqual(type(MyLayer).__name__, 'type')
self.assertNotEqual(type(MyLayer).__name__, 'pybind11_type')
self.assertEqual(
type(paddle.fluid.core.VarBase).__name__, 'pybind11_type')
if __name__ == '__main__':
paddle.enable_static()
unittest.main()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册