Skip to content

Commit d19bceb

Browse files
authored
pack the @[email protected] into library. test=develop (#33322)
1 parent a01e513 commit d19bceb

File tree

5 files changed

+66
-25
lines changed

5 files changed

+66
-25
lines changed

paddle/fluid/framework/CMakeLists.txt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,20 @@ add_subdirectory(io)
2929
proto_library(framework_proto SRCS framework.proto)
3030

3131
proto_library(op_def_proto SRCS op_def.proto)
32-
set(OP_DEF_FOLDER "${PADDLE_SOURCE_DIR}/paddle/fluid/operators/compat/")
33-
configure_file("op_def_api.h.in" "op_def_api.h")
3432
cc_library(op_def_api SRCS op_def_api.cc DEPS op_def_proto)
3533

34+
FILE(GLOB OP_DEF_FILES ${PADDLE_SOURCE_DIR}/paddle/fluid/operators/compat/*.pbtxt)
35+
FILE(WRITE ${CMAKE_CURRENT_BINARY_DIR}/op_def.pbtxt
36+
"namespace { \n"
37+
"const std::unordered_map<std::string, std::string> op_def_map = { \n")
38+
foreach(OP_DEF_FILE ${OP_DEF_FILES})
39+
FILE(READ ${OP_DEF_FILE} OP_DEF_CONTENT)
40+
get_filename_component(OP_NAME ${OP_DEF_FILE} NAME_WE)
41+
FILE(APPEND ${CMAKE_CURRENT_BINARY_DIR}/op_def.pbtxt
42+
"{\"${OP_NAME}\",R\"(${OP_DEF_CONTENT})\"},\n")
43+
endforeach(OP_DEF_FILE)
44+
FILE(APPEND ${CMAKE_CURRENT_BINARY_DIR}/op_def.pbtxt "{\"\",\"\"}};\n}")
45+
3646
proto_library(heter_service_proto SRCS heter_service.proto)
3747
proto_library(data_feed_proto SRCS data_feed.proto)
3848
proto_library(trainer_desc_proto SRCS trainer_desc.proto DEPS framework_proto

paddle/fluid/framework/ir/op_compat_sensible_pass_tester.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,18 @@ TEST(OpCompatSensiblePass, compatOpAttribute) {
9191
delete info.checker_;
9292
}
9393

94+
TEST(OpCompatSensiblePass, opDefNotFound) {
95+
OpCompat compat("fc_1");
96+
97+
OpDesc fc_op;
98+
99+
compat.Judge(fc_op);
100+
101+
OpCompat compat_1("");
102+
103+
compat_1.Judge(fc_op);
104+
}
105+
94106
TEST(OpCompatSensiblePass, compatOpAttributeOptional) {
95107
OpCompat compat("fc");
96108
compat.AddAttr("activation_type")

paddle/fluid/framework/op_def_api.cc

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@
3232
#include "io/fs.h"
3333
#include "paddle/fluid/framework/op_def.pb.h"
3434

35+
/*
36+
// op_def.pbtxt
37+
namespace {
38+
const std::unordered_map<std::string, std::std::string> op_def_map = {...};
39+
}
40+
*/
41+
#include "paddle/fluid/framework/op_def.pbtxt" //NOLINT
42+
3543
namespace paddle {
3644
namespace framework {
3745

@@ -42,20 +50,20 @@ const proto::OpDef& GetOpDef(const std::string& op_name) {
4250
std::lock_guard<std::mutex> lk(mtx);
4351
if (ops_definition.find(op_name) == ops_definition.end()) {
4452
proto::OpDef op_def;
45-
std::string op_path = OP_DEF_FOLDER + op_name + ".pbtxt";
46-
int fd = open(op_path.c_str(), O_RDONLY);
47-
if (fd == -1) {
48-
LOG(WARNING) << op_path << " open failed!";
53+
if (op_def_map.find(op_name) == op_def_map.end()) {
54+
LOG(WARNING) << op_name << ".pbtxt not exist!";
4955
} else {
50-
::google::protobuf::io::FileInputStream* input =
51-
new ::google::protobuf::io::FileInputStream(fd);
52-
if (!::google::protobuf::TextFormat::Parse(input, &op_def)) {
53-
LOG(WARNING) << "Failed to parse " << op_path;
56+
if (!::google::protobuf::TextFormat::ParseFromString(
57+
op_def_map.at(op_name), &op_def)) {
58+
LOG(WARNING) << "Failed to parse " << op_name;
5459
}
55-
delete input;
56-
close(fd);
5760
}
58-
ops_definition.emplace(std::make_pair(op_name, std::move(op_def)));
61+
if (op_def.type() != op_name) {
62+
LOG(WARNING) << op_name << ".pbtxt has error type :" << op_def.type();
63+
ops_definition.emplace(std::make_pair(op_name, proto::OpDef()));
64+
} else {
65+
ops_definition.emplace(std::make_pair(op_name, std::move(op_def)));
66+
}
5967
}
6068
}
6169
return ops_definition.at(op_name);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#pragma once
16+
17+
#include "paddle/fluid/framework/op_def.pb.h"
18+
19+
namespace paddle {
20+
namespace framework {
21+
const proto::OpDef& GetOpDef(const std::string& op_name);
22+
}
23+
}

paddle/fluid/framework/op_def_api.h.in

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)