Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add operator base #2725

Merged
merged 34 commits into from
Jul 11, 2017
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
ff92a58
add operator base
jacquesqiao Jul 4, 2017
3f75b66
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
jacquesqiao Jul 4, 2017
28e2313
import attr_type.proto
jacquesqiao Jul 4, 2017
22c2bec
add test
jacquesqiao Jul 4, 2017
c6407c3
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
jacquesqiao Jul 5, 2017
a591e78
do not return error when run
jacquesqiao Jul 5, 2017
8d8a448
remove Error of InitializeAttrs
jacquesqiao Jul 5, 2017
cb7c234
interface of operator
jacquesqiao Jul 6, 2017
57f1f6a
refactor of operator
jacquesqiao Jul 6, 2017
c248ba0
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
jacquesqiao Jul 6, 2017
57d1fbd
add comment, optimize code style
jacquesqiao Jul 6, 2017
f7d825b
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
jacquesqiao Jul 7, 2017
4f23ec3
net interface of op
jacquesqiao Jul 7, 2017
88f6621
refine operator interface
jacquesqiao Jul 8, 2017
42afab2
optimize operator test
jacquesqiao Jul 9, 2017
0a662ff
optimize code
jacquesqiao Jul 9, 2017
474debd
change test op name to test_operator
jacquesqiao Jul 10, 2017
7b77d76
add optional op name
jacquesqiao Jul 10, 2017
231e056
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
jacquesqiao Jul 10, 2017
2d1d021
rm name, use DeviceContext
jacquesqiao Jul 10, 2017
1e35e83
prepare for opkernel
jacquesqiao Jul 10, 2017
4c68deb
change op in OpContext from ref to const pointer
jacquesqiao Jul 10, 2017
d0fae91
change op member to public
jacquesqiao Jul 10, 2017
40022f7
remove New OpContext
jacquesqiao Jul 10, 2017
e34c579
fix style problem
jacquesqiao Jul 10, 2017
b710d5b
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
jacquesqiao Jul 11, 2017
cd2338f
use fake DeviceContext
jacquesqiao Jul 11, 2017
dfbf13d
use const func call
jacquesqiao Jul 11, 2017
c9aa77c
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
jacquesqiao Jul 11, 2017
6a893c1
add operator with kernel
jacquesqiao Jul 11, 2017
0fc8207
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
jacquesqiao Jul 11, 2017
9324915
merge new attr
jacquesqiao Jul 11, 2017
f0332fb
optimize code
jacquesqiao Jul 11, 2017
c1d8cbb
fix cpp style
jacquesqiao Jul 11, 2017
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
38 changes: 38 additions & 0 deletions paddle/framework/operator.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* 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/operator.h"

namespace paddle {
namespace framework {

Operator::Operator(const OpDesc &desc): op_desc_(desc) {
inputs_.reserve(desc.inputs_size());
for(const std::string& input: op_desc_.inputs()) {
inputs_.push_back(input);
}
outputs_.reserve(op_desc_.outputs_size());
for(const std::string& output: op_desc_.outputs()) {
outputs_.push_back(output);
}
std::vector<AttrDesc> attrs;
attrs.reserve(desc.attrs_size());
for(const AttrDesc& attr: op_desc_.attrs()) {
attrs.push_back(attr);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The declaration of attrs_:

AttributeMap attrs_;

The type is same with attrs, so why not use attrs_ = attrs?

InitializeAttrs(attrs);
}

} // namespace framework
} // namespace paddle
65 changes: 65 additions & 0 deletions paddle/framework/operator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/* 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. */

#pragma once

#include <string>
#include <unordered_map>
#include <vector>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The above four lines can be removed. They have been included in paddle/framework/attr_checker.h.

#include "paddle/utils/Error.h"

#include <paddle/framework/op_desc.pb.h>

namespace paddle {
namespace framework {

class OpContext {};

/**
* @brief Operator is used to do some computation.
*
* We use a OpDesc proto Message to describe and create a operator.
* Operator will get the Variables and computing resource from OpContext when Run.
*/
class Operator {
public:
explicit Operator(const OpDesc& desc);
virtual ~Operator() {}

void InitializeAttrs();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we can remove this interface and do initialize in the ctor


/**
* InferShape is used to infer the shape of tensors related to this Operator.
*/
virtual void InferShape() = 0;

/**
* Run take a OpContext as parameter.
*
* 1. it will get input/output variable from OpContext.scope
* 2. It will get computing resource such as cpu/gpu from OpContext.
*/
virtual void Run(OpContext *context) const = 0;
const std::string DebugString() const {
return op_desc_.ShortDebugString();
}

protected:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inputs_ and outputs_ are all public.

OpDesc op_desc_;
std::vector<std::string> inputs_;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about inputs_name_? People may be confused about inputs_, inputs() and Input().

std::vector<std::string> outputs_;
};

} // namespace framework
} // namespace paddle
19 changes: 19 additions & 0 deletions paddle/framework/operator_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* 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/operator.h"
#include "gtest/gtest.h"

TEST(Operator, Create) {
}