Skip to content

Commit

Permalink
[SOT][Faster Guard] add some basic guard (#69313)
Browse files Browse the repository at this point in the history
  • Loading branch information
zrr1999 authored Nov 13, 2024
1 parent 20332cd commit bf0e51c
Show file tree
Hide file tree
Showing 5 changed files with 378 additions and 0 deletions.
1 change: 1 addition & 0 deletions paddle/fluid/pybind/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ set(PYBIND_SRCS
sot/eval_frame_tools.cc
sot/cpython_internals.c
sot/eval_frame.c
sot/guards.cc
op_callstack_utils.cc
python_callable_registry.cc)

Expand Down
42 changes: 42 additions & 0 deletions paddle/fluid/pybind/jit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ limitations under the License. */
#include "paddle/fluid/jit/serializer.h"
#include "paddle/fluid/pybind/sot/eval_frame.h"
#include "paddle/fluid/pybind/sot/eval_frame_tools.h"
#include "paddle/fluid/pybind/sot/guards.h"
#include "paddle/fluid/pybind/sot/macros.h"
#include "paddle/phi/common/data_type.h"
#include "paddle/phi/common/place.h"
#include "paddle/utils/pybind.h"

Expand Down Expand Up @@ -59,6 +61,45 @@ void BindJit(pybind11::module *m) {
});
}

void BindGuard(pybind11::module *m) {
#if SOT_IS_SUPPORTED
py::class_<GuardBase, std::shared_ptr<GuardBase>>(
*m, "GuardBase", R"DOC(GuardBase Class.)DOC")
.def("check", &GuardBase::check_pybind);
py::class_<LambdaGuard, GuardBase, std::shared_ptr<LambdaGuard>>(
*m, "LambdaGuard", R"DOC(LambdaGuard Class.)DOC")
.def(py::init<const py::function &>(), py::arg("guard_check_fn"));
py::class_<GuardGroup, GuardBase, std::shared_ptr<GuardGroup>>(
*m, "GuardGroup", R"DOC(GuardGroup Class.)DOC")
.def(py::init<std::vector<std::shared_ptr<GuardBase>>>(),
py::arg("guards"));
py::class_<TypeMatchGuard, GuardBase, std::shared_ptr<TypeMatchGuard>>(
*m, "TypeMatchGuard", R"DOC(TypeMatchGuard Class.)DOC")
.def(py::init<const py::type &>(), py::arg("py_type"));
py::class_<LengthMatchGuard, GuardBase, std::shared_ptr<LengthMatchGuard>>(
*m, "LengthMatchGuard", R"DOC(LengthMatchGuard Class.)DOC")
.def(py::init<Py_ssize_t>(), py::arg("length"));
py::class_<ValueMatchGuard, GuardBase, std::shared_ptr<ValueMatchGuard>>(
*m, "ValueMatchGuard", R"DOC(ValueMatchGuard Class.)DOC")
.def(py::init<const py::object &>(), py::arg("py_value"));
py::class_<DtypeMatchGuard, GuardBase, std::shared_ptr<DtypeMatchGuard>>(
*m, "DtypeMatchGuard", R"DOC(DtypeMatchGuard Class.)DOC")
.def(py::init<const paddle::framework::proto::VarType &>(),
py::arg("dtype"))
.def(py::init<const phi::DataType &>(), py::arg("dtype"));
py::class_<LayerMatchGuard, GuardBase, std::shared_ptr<LayerMatchGuard>>(
*m, "LayerMatchGuard", R"DOC(LayerMatchGuard Class.)DOC")
.def(py::init<const py::object &>(), py::arg("layer_obj"));

m->def(
"merge_guard",
[](const std::vector<std::shared_ptr<GuardBase>> &py_guards) {
return GuardGroup(py_guards);
},
py::arg("py_guards"));
#endif
}

void BindSot(pybind11::module *m) {
#if SOT_IS_SUPPORTED
PyInit__eval_frame();
Expand Down Expand Up @@ -107,6 +148,7 @@ void BindSot(pybind11::module *m) {
return obj;
},
py::arg("py_codes"));
BindGuard(m);
#endif
}

Expand Down
90 changes: 90 additions & 0 deletions paddle/fluid/pybind/sot/guards.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/* Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#include "paddle/fluid/pybind/sot/guards.h"

#if SOT_IS_SUPPORTED

#include <Python.h>
#include <frameobject.h>

#if !defined(PyObject_CallOneArg) && PY_VERSION_HEX < PY_3_9_0_HEX
static inline PyObject* PyObject_CallOneArg(PyObject* func, PyObject* arg) {
return PyObject_CallFunctionObjArgs(func, arg, NULL);
}
#endif

bool LambdaGuard::check(PyObject* value) {
PyObject* x = PyObject_CallOneArg(_guard_check_fn, value);
if (x == nullptr) {
PyErr_Clear();
return false;
}
bool ret = PyObject_IsTrue(x);
Py_DECREF(x);
return ret;
}

bool GuardGroup::check(PyObject* value) {
for (auto& guard : _guards) {
if (!guard->check(value)) {
return false;
}
}
return true;
}

bool TypeMatchGuard::check(PyObject* value) {
return Py_TYPE(value) == _expected;
}

bool ValueMatchGuard::check(PyObject* value) {
if (value == _expected_value) {
return true;
}
if (Py_TYPE(value) != _expected_type) {
return false;
}
int result = PyObject_RichCompareBool(value, _expected_value, Py_EQ);
// Check for exception
if (result == -1) {
PyErr_Clear();
return false;
}
return result;
}

bool LengthMatchGuard::check(PyObject* value) {
return PySequence_Size(value) == _expected;
}

bool DtypeMatchGuard::check(PyObject* value) {
if (!paddle::pybind::PyCheckTensor(value)) {
// TODO(zrr1999): PyCheckTensor only check if the object is a p_tensor_type.
return false;
}
auto dtype =
reinterpret_cast<paddle::pybind::TensorObject*>(value)->tensor.type();
return phi::TransToProtoVarType(dtype) == _expected;
}

bool LayerMatchGuard::check(PyObject* value) {
if (value != _layer_ptr) {
return false;
}
PyObject* training = PyObject_GetAttrString(value, "training");
return (training == Py_True) == _training;
}

#endif
148 changes: 148 additions & 0 deletions paddle/fluid/pybind/sot/guards.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/* Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#pragma once

#include <Python.h>
#include "paddle/fluid/framework/data_type.h"
#include "paddle/fluid/pybind/sot/macros.h"
#include "paddle/phi/core/framework/heter_service.pb.h"
#include "paddle/phi/core/utils/data_type.h"
#include "paddle/utils/pybind.h"
#include "pybind11/pybind11.h"

namespace py = pybind11;
#define PYBIND11_DETAILED_ERROR_MESSAGES
#if SOT_IS_SUPPORTED

class GuardBase {
public:
GuardBase() = default;

bool check_pybind(py::handle value) { return check(value.ptr()); }

virtual bool check(PyObject* value) = 0;
virtual ~GuardBase() = default;
};

class LambdaGuard : public GuardBase {
public:
explicit LambdaGuard(PyObject* guard_check_fn)
: _guard_check_fn(guard_check_fn) {}

explicit LambdaGuard(const py::function& guard_check_fn)
: _guard_check_fn(guard_check_fn.ptr()) {
Py_INCREF(_guard_check_fn);
}

~LambdaGuard() { Py_DECREF(_guard_check_fn); }

bool check(PyObject* value);

private:
PyObject* _guard_check_fn;
};

class GuardGroup : public GuardBase {
public:
explicit GuardGroup(std::vector<std::shared_ptr<GuardBase>> guards) {
for (auto& guard : guards) {
if (auto group = dynamic_cast<GuardGroup*>(guard.get())) {
_guards.insert(
_guards.end(), group->_guards.begin(), group->_guards.end());
} else {
_guards.push_back(std::move(guard));
}
}
}
bool check(PyObject* value);

private:
std::vector<std::shared_ptr<GuardBase>> _guards;
};

class TypeMatchGuard : public GuardBase {
public:
explicit TypeMatchGuard(PyObject* type_ptr)
: _expected(reinterpret_cast<PyTypeObject*>(type_ptr)) {}

explicit TypeMatchGuard(const py::type& py_type)
: _expected(reinterpret_cast<PyTypeObject*>(py_type.ptr())) {}

bool check(PyObject* value);

private:
PyTypeObject* _expected;
};

class ValueMatchGuard : public GuardBase {
public:
explicit ValueMatchGuard(PyObject* value_ptr)
: _expected_value(value_ptr), _expected_type(value_ptr->ob_type) {}

explicit ValueMatchGuard(const py::object& py_value)
: _expected_value(py_value.ptr()),
_expected_type(Py_TYPE(py_value.ptr())) {
Py_INCREF(_expected_value);
}

~ValueMatchGuard() { Py_DECREF(_expected_value); }

bool check(PyObject* value);

private:
PyObject* _expected_value;
PyTypeObject* _expected_type;
};

class LengthMatchGuard : public GuardBase {
public:
explicit LengthMatchGuard(Py_ssize_t length) : _expected(length) {}

bool check(PyObject* value);

private:
Py_ssize_t _expected;
};

class DtypeMatchGuard : public GuardBase {
public:
explicit DtypeMatchGuard(const paddle::framework::proto::VarType& dtype_ptr)
: _expected(dtype_ptr.type()) {}

explicit DtypeMatchGuard(const phi::DataType& dtype_ptr)
: _expected(phi::TransToProtoVarType(dtype_ptr)) {}

bool check(PyObject* value);

private:
int _expected;
};

class LayerMatchGuard : public GuardBase {
public:
explicit LayerMatchGuard(PyObject* layer_ptr) : _layer_ptr(layer_ptr) {
_training = PyObject_GetAttrString(layer_ptr, "training") == Py_True;
}

explicit LayerMatchGuard(const py::object& layer_obj)
: _layer_ptr(layer_obj.ptr()), _training(layer_obj.attr("training")) {}

bool check(PyObject* value);

private:
PyObject* _layer_ptr;
bool _training;
};

#endif
Loading

0 comments on commit bf0e51c

Please sign in to comment.