-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
add operator base #2725
Changes from 7 commits
ff92a58
3f75b66
28e2313
22c2bec
c6407c3
a591e78
8d8a448
cb7c234
57f1f6a
c248ba0
57d1fbd
f7d825b
4f23ec3
88f6621
42afab2
0a662ff
474debd
7b77d76
231e056
2d1d021
1e35e83
4c68deb
d0fae91
40022f7
e34c579
b710d5b
cd2338f
dfbf13d
c9aa77c
6a893c1
0fc8207
9324915
f0332fb
c1d8cbb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
} | ||
InitializeAttrs(attrs); | ||
} | ||
|
||
} // namespace framework | ||
} // namespace paddle |
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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The above four lines can be removed. They have been included in |
||
#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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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_; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about |
||
std::vector<std::string> outputs_; | ||
}; | ||
|
||
} // namespace framework | ||
} // namespace paddle |
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) { | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The declaration of attrs_:
The type is same with attrs, so why not use attrs_ = attrs?