commit
9e3a9eb233
@ -1,20 +1,59 @@
|
||||
/*
|
||||
Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
|
||||
|
||||
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/framework/net.h"
|
||||
|
||||
namespace paddle {
|
||||
namespace framework {
|
||||
|
||||
PlainNet::PlainNet(const NetDesc& def) {}
|
||||
|
||||
void PlainNet::InferShape(Scope* scope) {
|
||||
void PlainNet::CompleteAddOp() {
|
||||
std::unordered_set<std::string> input_set;
|
||||
std::unordered_set<std::string> output_set;
|
||||
std::unordered_set<std::string> temp_output;
|
||||
for (auto& op : ops_) {
|
||||
op.InferShape();
|
||||
for (auto& ipt : op->inputs_) {
|
||||
if (!Contains(output_set, ipt)) { // Not other op's output
|
||||
input_set.insert(ipt);
|
||||
} else {
|
||||
temp_output.insert(ipt);
|
||||
}
|
||||
}
|
||||
|
||||
for (auto& opt : op->outputs_) {
|
||||
output_set.insert(opt);
|
||||
}
|
||||
}
|
||||
}
|
||||
inputs_.reserve(input_set.size());
|
||||
std::copy(input_set.begin(), input_set.end(), std::back_inserter(inputs_));
|
||||
|
||||
void PlainNet::Run(std::shared_ptr<Scope> scope, DeviceContext* ctx) {
|
||||
for (auto& op : ops_) {
|
||||
op.Run(ctx);
|
||||
outputs_.reserve(output_set.size());
|
||||
std::vector<int> tmp_index;
|
||||
tmp_index.reserve(temp_output.size());
|
||||
int idx = 0;
|
||||
for (auto& opt : output_set) {
|
||||
if (Contains(temp_output, opt)) {
|
||||
tmp_index.push_back(idx);
|
||||
}
|
||||
outputs_.push_back(opt);
|
||||
++idx;
|
||||
}
|
||||
|
||||
attrs_["temporary_index"] = tmp_index;
|
||||
add_op_done_ = true;
|
||||
}
|
||||
|
||||
} // namespace framework
|
||||
} // namespace paddle
|
||||
|
@ -0,0 +1,67 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <paddle/framework/net.h>
|
||||
#include <paddle/framework/op_registry.h>
|
||||
#include <paddle/framework/operator.h>
|
||||
|
||||
namespace pd = paddle::framework;
|
||||
|
||||
static int infer_shape_cnt = 0;
|
||||
static int run_cnt = 0;
|
||||
|
||||
class TestOp : public pd::OperatorBase {
|
||||
public:
|
||||
void InferShape(const paddle::framework::ScopePtr& scope) const override {
|
||||
++infer_shape_cnt;
|
||||
}
|
||||
void Run(const paddle::framework::ScopePtr& scope,
|
||||
const paddle::platform::DeviceContext& dev_ctx) const override {
|
||||
++run_cnt;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
void AssertSameVectorWithoutOrder(const std::vector<T>& expected,
|
||||
const std::vector<T>& actual) {
|
||||
ASSERT_EQ(expected.size(), actual.size());
|
||||
std::unordered_set<T> expected_set;
|
||||
for (auto& tmp : expected) {
|
||||
expected_set.insert(tmp);
|
||||
}
|
||||
for (auto& act : actual) {
|
||||
ASSERT_NE(expected_set.end(), expected_set.find(act));
|
||||
}
|
||||
}
|
||||
|
||||
TEST(OpKernel, all) {
|
||||
auto net = std::make_shared<paddle::framework::PlainNet>();
|
||||
ASSERT_NE(net, nullptr);
|
||||
|
||||
auto op1 = std::make_shared<TestOp>();
|
||||
op1->inputs_ = {"x", "w1", "b1"};
|
||||
op1->outputs_ = {"y"};
|
||||
net->AddOp(op1);
|
||||
|
||||
auto op2 = std::make_shared<TestOp>();
|
||||
op2->inputs_ = {"y", "w2", "b2"};
|
||||
op2->outputs_ = {"z"};
|
||||
net->AddOp(op2);
|
||||
|
||||
net->CompleteAddOp();
|
||||
AssertSameVectorWithoutOrder({"x", "w1", "b1", "w2", "b2"}, net->inputs_);
|
||||
AssertSameVectorWithoutOrder({"y", "z"}, net->outputs_);
|
||||
auto tmp_idx_iter = net->attrs_.find("temporary_index");
|
||||
ASSERT_NE(net->attrs_.end(), tmp_idx_iter);
|
||||
auto& tmp_idx = boost::get<std::vector<int>>(tmp_idx_iter->second);
|
||||
ASSERT_EQ(1UL, tmp_idx.size());
|
||||
ASSERT_EQ("y", net->outputs_[tmp_idx[0]]);
|
||||
|
||||
auto scope = std::make_shared<pd::Scope>();
|
||||
paddle::platform::CPUDeviceContext dev_ctx;
|
||||
|
||||
net->InferShape(scope);
|
||||
net->Run(scope, dev_ctx);
|
||||
ASSERT_EQ(2, infer_shape_cnt);
|
||||
ASSERT_EQ(2, run_cnt);
|
||||
|
||||
ASSERT_THROW(net->AddOp(op2), paddle::framework::EnforceNotMet);
|
||||
}
|
File diff suppressed because it is too large
Load Diff
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue