Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions paddle/fluid/framework/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,20 @@ add_subdirectory(io)
proto_library(framework_proto SRCS framework.proto)

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

FILE(GLOB OP_DEF_FILES ${PADDLE_SOURCE_DIR}/paddle/fluid/operators/compat/*.pbtxt)
FILE(WRITE ${CMAKE_CURRENT_BINARY_DIR}/op_def.pbtxt
"namespace { \n"
"const std::unordered_map<std::string, std::string> op_def_map = { \n")
foreach(OP_DEF_FILE ${OP_DEF_FILES})
FILE(READ ${OP_DEF_FILE} OP_DEF_CONTENT)
get_filename_component(OP_NAME ${OP_DEF_FILE} NAME_WE)
FILE(APPEND ${CMAKE_CURRENT_BINARY_DIR}/op_def.pbtxt
"{\"${OP_NAME}\",R\"(${OP_DEF_CONTENT})\"},\n")
endforeach(OP_DEF_FILE)
FILE(APPEND ${CMAKE_CURRENT_BINARY_DIR}/op_def.pbtxt "{\"\",\"\"}};\n}")

proto_library(heter_service_proto SRCS heter_service.proto)
proto_library(data_feed_proto SRCS data_feed.proto)
proto_library(trainer_desc_proto SRCS trainer_desc.proto DEPS framework_proto
Expand Down
12 changes: 12 additions & 0 deletions paddle/fluid/framework/ir/op_compat_sensible_pass_tester.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ TEST(OpCompatSensiblePass, compatOpAttribute) {
delete info.checker_;
}

TEST(OpCompatSensiblePass, opDefNotFound) {
OpCompat compat("fc_1");

OpDesc fc_op;

compat.Judge(fc_op);

OpCompat compat_1("");

compat_1.Judge(fc_op);
}

TEST(OpCompatSensiblePass, compatOpAttributeOptional) {
OpCompat compat("fc");
compat.AddAttr("activation_type")
Expand Down
30 changes: 19 additions & 11 deletions paddle/fluid/framework/op_def_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@
#include "io/fs.h"
#include "paddle/fluid/framework/op_def.pb.h"

/*
// op_def.pbtxt
namespace {
const std::unordered_map<std::string, std::std::string> op_def_map = {...};
}
*/
#include "paddle/fluid/framework/op_def.pbtxt" //NOLINT

namespace paddle {
namespace framework {

Expand All @@ -42,20 +50,20 @@ const proto::OpDef& GetOpDef(const std::string& op_name) {
std::lock_guard<std::mutex> lk(mtx);
if (ops_definition.find(op_name) == ops_definition.end()) {
proto::OpDef op_def;
std::string op_path = OP_DEF_FOLDER + op_name + ".pbtxt";
int fd = open(op_path.c_str(), O_RDONLY);
if (fd == -1) {
LOG(WARNING) << op_path << " open failed!";
if (op_def_map.find(op_name) == op_def_map.end()) {
LOG(WARNING) << op_name << ".pbtxt not exist!";
} else {
::google::protobuf::io::FileInputStream* input =
new ::google::protobuf::io::FileInputStream(fd);
if (!::google::protobuf::TextFormat::Parse(input, &op_def)) {
LOG(WARNING) << "Failed to parse " << op_path;
if (!::google::protobuf::TextFormat::ParseFromString(
op_def_map.at(op_name), &op_def)) {
LOG(WARNING) << "Failed to parse " << op_name;
}
delete input;
close(fd);
}
ops_definition.emplace(std::make_pair(op_name, std::move(op_def)));
if (op_def.type() != op_name) {
LOG(WARNING) << op_name << ".pbtxt has error type :" << op_def.type();
ops_definition.emplace(std::make_pair(op_name, proto::OpDef()));
} else {
ops_definition.emplace(std::make_pair(op_name, std::move(op_def)));
}
}
}
return ops_definition.at(op_name);
Expand Down
23 changes: 23 additions & 0 deletions paddle/fluid/framework/op_def_api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) 2021 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 "paddle/fluid/framework/op_def.pb.h"

namespace paddle {
namespace framework {
const proto::OpDef& GetOpDef(const std::string& op_name);
}
}
12 changes: 0 additions & 12 deletions paddle/fluid/framework/op_def_api.h.in

This file was deleted.