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 8 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
1 change: 1 addition & 0 deletions paddle/framework/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ cc_test(op_proto_test SRCS op_proto_test.cc DEPS op_proto protobuf)

proto_library(op_desc SRCS op_desc.proto DEPS attr_type)
cc_test(op_desc_test SRCS op_desc_test.cc DEPS op_desc protobuf)
cc_library(operator SRCS operator.cc)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Need unittest.

Copy link
Member Author

Choose a reason for hiding this comment

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

done

43 changes: 43 additions & 0 deletions paddle/framework/operator.cc
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) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

It seems that OpBase doesn't take whole OpDesc as the arguments, but only inputs and outputs.

So maybe std::vector<string> input and std::vector<string> output is enough.

Copy link
Member Author

Choose a reason for hiding this comment

The 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() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Also, const is need.

Copy link
Member Author

Choose a reason for hiding this comment

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

done

return desc_.DebugString();
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 Operator Cosine, from (var_input1, var_input2) to (var_output)

}

Variable* OperatorBase::input(Scope *scope, int index) {
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 input method.

Copy link
Member Author

Choose a reason for hiding this comment

The 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
63 changes: 63 additions & 0 deletions paddle/framework/operator.h
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>
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/scope.h"
#include "paddle/framework/ddim.h"
#include "paddle/framework/op_desc.pb.h"

namespace paddle {
namespace framework {

class DeviceContext {};
class CpuContext : public DeviceContext {};
Copy link
Collaborator

Choose a reason for hiding this comment

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

Cpu => CPU. It is an acronym.

class GpuContext : public DeviceContext {};
Copy link
Collaborator

Choose a reason for hiding this comment

The 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;
Copy link
Collaborator

Choose a reason for hiding this comment

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

That interface is not needed, due to @Canpio 's design.

Copy link
Member Author

Choose a reason for hiding this comment

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

done, move to CreateOperator


virtual void InferShape(const Scope* scope) const = 0;
Copy link
Collaborator

Choose a reason for hiding this comment

The 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:
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.

const OpDesc desc_;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Op do not store OpDesc

Copy link
Member Author

Choose a reason for hiding this comment

The 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.

Copy link
Member Author

Choose a reason for hiding this comment

The 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