-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SOT][Faster Guard] add some basic guard (#69313)
- Loading branch information
Showing
5 changed files
with
378 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.