-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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 8 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,43 @@ | ||
/* 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 { | ||
|
||
OperatorBase::OperatorBase(const OpDesc &desc): desc_(desc) { | ||
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. It seems that OpBase doesn't take whole So maybe 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. done, remove construct function |
||
inputs_.reserve(desc.inputs_size()); | ||
for(const std::string& input: desc_.inputs()) { | ||
inputs_.push_back(input); | ||
} | ||
outputs_.reserve(desc_.outputs_size()); | ||
for(const std::string& output: desc_.outputs()) { | ||
outputs_.push_back(output); | ||
} | ||
} | ||
|
||
std::string OperatorBase::DebugString() { | ||
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. Also, 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. done |
||
return desc_.DebugString(); | ||
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. DebugString() is not in OpDesc. It should be generated in run time. Like |
||
} | ||
|
||
Variable* OperatorBase::input(Scope *scope, int index) { | ||
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. It should not be a part of the interface. Because the invoker of Operator is not need 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. done, will put input() into OpRunContext |
||
return scope->CreateVariable(inputs_[index]); | ||
} | ||
|
||
Variable* OperatorBase::output(Scope *scope, int index) { | ||
return scope->CreateVariable(outputs_[index]); | ||
} | ||
} // namespace framework | ||
} // namespace paddle |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* 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/scope.h" | ||
#include "paddle/framework/ddim.h" | ||
#include "paddle/framework/op_desc.pb.h" | ||
|
||
namespace paddle { | ||
namespace framework { | ||
|
||
class DeviceContext {}; | ||
class CpuContext : public DeviceContext {}; | ||
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. Cpu => CPU. It is an acronym. |
||
class GpuContext : public DeviceContext {}; | ||
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. Gpu => GPU |
||
|
||
/** | ||
* @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 from scope and computing resource from DeviceContext. | ||
*/ | ||
class OperatorBase { | ||
public: | ||
explicit OperatorBase(const OpDesc& desc); | ||
virtual ~OperatorBase() {} | ||
|
||
/// initialize Attributes of this OP from proto message desc.attrs() | ||
/// you should derive this function to init the attr you need in OP. | ||
virtual void InitializeAttributes() = 0; | ||
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. That interface is not needed, due to @Canpio 's design. 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. done, move to CreateOperator |
||
|
||
virtual void InferShape(const Scope* scope) const = 0; | ||
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. InferShape should have default implementation? virtual void InferShape(const Scope* scope) {} Also, it might change properties of inputs and outputs, so shouldn't be suffixed with const? |
||
|
||
/// when implement an Op, your should implement this function. | ||
virtual void Run(Scope* scope, DeviceContext* device_context) const = 0; | ||
|
||
std::string DebugString(); | ||
Variable* input(Scope* scope, int index); | ||
Variable* output(Scope* scope, int index); | ||
|
||
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. |
||
const OpDesc desc_; | ||
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. Op do not store 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. why not store OpDesc, it will not change and can use directly when to serialize. 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. done.... though i still want to keep it |
||
std::vector<std::string> inputs_; | ||
std::vector<std::string> outputs_; | ||
}; | ||
|
||
} // namespace framework | ||
} // namespace paddle |
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.
Need unittest.
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.
done