add file check_op_desc.py and add interface to get default value. (#21530)

* add file check_op_desc.py and add interface to get default value. test=develop

* add test for c++ coverage rate. test=develop

* Correct typo. test=develop
paddle_tiny_install
liym27 6 years ago committed by GitHub
parent 2057df7ac0
commit 9da7e6b4d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -296,7 +296,15 @@ class TypedAttrChecker {
return *this;
}
void operator()(AttributeMap* attr_map) const {
void operator()(AttributeMap* attr_map,
bool get_default_value_only = false) const {
if (get_default_value_only) {
if (!default_value_setter_.empty()) {
attr_map->emplace(attr_name_, default_value_setter_[0]());
}
return;
}
auto it = attr_map->find(attr_name_);
if (it == attr_map->end()) {
// user do not set this attr
@ -321,7 +329,7 @@ class TypedAttrChecker {
// check whether op's all attributes fit their own limits
class OpAttrChecker {
typedef std::function<void(AttributeMap*)> AttrChecker;
typedef std::function<void(AttributeMap*, bool)> AttrChecker;
public:
template <typename T>
@ -333,8 +341,16 @@ class OpAttrChecker {
void Check(AttributeMap* attr_map) const {
for (const auto& checker : attr_checkers_) {
checker(attr_map);
checker(attr_map, false);
}
}
AttributeMap GetAttrsDefaultValuesMap() const {
AttributeMap default_values_map;
for (const auto& checker : attr_checkers_) {
checker(&default_values_map, true);
}
return default_values_map;
}
private:

@ -22,7 +22,6 @@ limitations under the License. */
#include <unordered_set>
#include <utility>
#include <vector>
#include "paddle/fluid/framework/executor.h"
#include "paddle/fluid/framework/feed_fetch_method.h"
#include "paddle/fluid/framework/framework.pb.h"
@ -43,6 +42,7 @@ limitations under the License. */
#include "paddle/fluid/framework/scope_pool.h"
#include "paddle/fluid/framework/selected_rows.h"
#include "paddle/fluid/framework/trainer.h"
#include "paddle/fluid/framework/type_defs.h"
#include "paddle/fluid/framework/version.h"
#include "paddle/fluid/imperative/layer.h"
#include "paddle/fluid/memory/allocation/allocator_strategy.h"
@ -65,6 +65,7 @@ limitations under the License. */
#include "paddle/fluid/pybind/imperative.h"
#include "paddle/fluid/pybind/inference_api.h"
#include "paddle/fluid/pybind/ir.h"
#include "paddle/fluid/pybind/pybind_boost_headers.h"
#ifndef _WIN32
#include "paddle/fluid/pybind/nccl_wrapper_py.h"
@ -1067,6 +1068,19 @@ All parameter, weight, gradient are variables in Paddle.
}
return ret_values;
});
m.def("get_op_attrs_default_value",
[](py::bytes byte_name) -> paddle::framework::AttributeMap {
std::string op_type = byte_name;
paddle::framework::AttributeMap res;
auto info = OpInfoMap::Instance().GetNullable(op_type);
if (info != nullptr) {
if (info->HasOpProtoAndChecker()) {
auto op_checker = info->Checker();
res = op_checker->GetAttrsDefaultValuesMap();
}
}
return res;
});
m.def(
"get_grad_op_desc", [](const OpDesc &op_desc,
const std::unordered_set<std::string> &no_grad_set,

@ -0,0 +1,29 @@
# Copyright (c) 2019 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.
from __future__ import print_function
import unittest
from paddle.fluid import core
from paddle import compat as cpt
class TestPybindInference(unittest.TestCase):
# call get_op_attrs_default_value for c++ coverage rate
def test_get_op_attrs_default_value(self):
core.get_op_attrs_default_value(cpt.to_bytes("fc"))
if __name__ == '__main__':
unittest.main()

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save