Merge pull request #2914 from reyoung/feature/add_some_skeletons_of_ops
Add skeletons of `mul`, `rowwise_add`, `sigmoid`, `softmax` opscblas_new
commit
861b66d4e3
@ -0,0 +1,60 @@
|
|||||||
|
/* 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/op_registry.h>
|
||||||
|
#include <paddle/framework/tensor.h>
|
||||||
|
#include <paddle/operators/mul_op.h>
|
||||||
|
|
||||||
|
namespace paddle {
|
||||||
|
namespace operators {
|
||||||
|
|
||||||
|
class MulOp : public framework::OperatorWithKernel {
|
||||||
|
protected:
|
||||||
|
void InferShape(
|
||||||
|
const std::vector<const framework::Tensor *> &inputs,
|
||||||
|
const std::vector<framework::Tensor *> &outputs) const override {
|
||||||
|
PADDLE_ENFORCE(inputs.size() == 2, "The mul op must take two inputs");
|
||||||
|
auto dim0 = inputs[0]->dims();
|
||||||
|
auto dim1 = inputs[1]->dims();
|
||||||
|
PADDLE_ENFORCE(dim0.size() == 2 && dim1.size() == 2,
|
||||||
|
"The input of mul op must be matrix");
|
||||||
|
PADDLE_ENFORCE(
|
||||||
|
dim0[1] == dim1[0],
|
||||||
|
"First matrix's width must be equal with second matrix's height.");
|
||||||
|
PADDLE_ENFORCE(outputs.size() == 1, "The mul op must take one output");
|
||||||
|
outputs[0]->set_dims({dim0[0], dim1[1]});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class MulOpMaker : public framework::OpProtoAndCheckerMaker {
|
||||||
|
public:
|
||||||
|
MulOpMaker(framework::OpProto *proto, framework::OpAttrChecker *op_checker)
|
||||||
|
: framework::OpProtoAndCheckerMaker(proto, op_checker) {
|
||||||
|
AddInput("X", "The first input of mul op");
|
||||||
|
AddInput("Y", "The second input of mul op");
|
||||||
|
AddOutput("Out", "The output of mul op");
|
||||||
|
AddComment(R"DOC(
|
||||||
|
Two Element Mul Operator.
|
||||||
|
|
||||||
|
The equation is: Out = X * Y
|
||||||
|
)DOC");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace operators
|
||||||
|
} // namespace paddle
|
||||||
|
|
||||||
|
REGISTER_OP(mul, paddle::operators::MulOp, paddle::operators::MulOpMaker);
|
||||||
|
REGISTER_OP_CPU_KERNEL(
|
||||||
|
mul, paddle::operators::MulKernel<paddle::platform::CPUPlace>);
|
@ -0,0 +1,20 @@
|
|||||||
|
/* 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/operators/mul_op.h>
|
||||||
|
#include <paddle/framework/op_registry.h>
|
||||||
|
|
||||||
|
REGISTER_OP_GPU_KERNEL(mul,
|
||||||
|
paddle::operators::MulKernel<paddle::platform
|
||||||
|
::GPUPlace>);
|
@ -0,0 +1,31 @@
|
|||||||
|
/* 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 <glog/logging.h>
|
||||||
|
#include <paddle/framework/operator.h>
|
||||||
|
|
||||||
|
namespace paddle {
|
||||||
|
namespace operators {
|
||||||
|
|
||||||
|
template <typename Place>
|
||||||
|
class MulKernel : public framework::OpKernel {
|
||||||
|
public:
|
||||||
|
void Compute(const framework::KernelContext &context) const override {
|
||||||
|
LOG(INFO) << "Mul kernel in " << typeid(Place).name();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} // namespace operators
|
||||||
|
} // namespace paddle
|
@ -0,0 +1,61 @@
|
|||||||
|
/* 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/op_registry.h>
|
||||||
|
#include <paddle/operators/rowwise_add_op.h>
|
||||||
|
namespace paddle {
|
||||||
|
namespace operators {
|
||||||
|
|
||||||
|
class RowWiseAddOp : public framework::OperatorWithKernel {
|
||||||
|
protected:
|
||||||
|
void InferShape(
|
||||||
|
const std::vector<const framework::Tensor *> &inputs,
|
||||||
|
const std::vector<framework::Tensor *> &outputs) const override {
|
||||||
|
PADDLE_ENFORCE(inputs.size() == 2UL, "Two inputs is needed by rowwise add");
|
||||||
|
auto dim0 = inputs[0]->dims();
|
||||||
|
auto dim1 = inputs[1]->dims();
|
||||||
|
|
||||||
|
PADDLE_ENFORCE(dim0.size() == 2, "Input 0 must be matrix");
|
||||||
|
PADDLE_ENFORCE(dim1.size() == 1, "The second input must be vector");
|
||||||
|
PADDLE_ENFORCE(dim0[1] == dim1[0], "The width of two input must be same");
|
||||||
|
PADDLE_ENFORCE(outputs.size() == 1, "The output size must be 1");
|
||||||
|
outputs[0]->set_dims(inputs[0]->dims());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class RowWiseAddOpMaker : public framework::OpProtoAndCheckerMaker {
|
||||||
|
public:
|
||||||
|
RowWiseAddOpMaker(framework::OpProto *proto,
|
||||||
|
framework::OpAttrChecker *op_checker)
|
||||||
|
: framework::OpProtoAndCheckerMaker(proto, op_checker) {
|
||||||
|
AddInput("X", "The left input of row-wise add op, must be matrix");
|
||||||
|
AddInput("b", "The right input of row-wise add op, must be vector");
|
||||||
|
AddOutput("Out", "The output of row-wise add op");
|
||||||
|
AddComment(R"DOC(Row-wise Add operator
|
||||||
|
|
||||||
|
for i in xrange(X.shape[0]):
|
||||||
|
Out = X[i] + b
|
||||||
|
)DOC");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace operators
|
||||||
|
} // namespace paddle
|
||||||
|
|
||||||
|
REGISTER_OP(rowwise_add,
|
||||||
|
paddle::operators::RowWiseAddOp,
|
||||||
|
paddle::operators::RowWiseAddOpMaker);
|
||||||
|
REGISTER_OP_CPU_KERNEL(
|
||||||
|
rowwise_add,
|
||||||
|
paddle::operators::RowWiseAddKernel<paddle::platform::CPUPlace>);
|
@ -0,0 +1,6 @@
|
|||||||
|
#include <paddle/framework/op_registry.h>
|
||||||
|
#include <paddle/operators/rowwise_add_op.h>
|
||||||
|
|
||||||
|
REGISTER_OP_GPU_KERNEL(
|
||||||
|
rowwise_add,
|
||||||
|
paddle::operators::RowWiseAddKernel<paddle::platform ::GPUPlace>);
|
@ -0,0 +1,31 @@
|
|||||||
|
/* 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 <glog/logging.h>
|
||||||
|
#include <paddle/framework/operator.h>
|
||||||
|
|
||||||
|
namespace paddle {
|
||||||
|
namespace operators {
|
||||||
|
|
||||||
|
template <typename Place>
|
||||||
|
class RowWiseAddKernel : public framework::OpKernel {
|
||||||
|
public:
|
||||||
|
void Compute(const framework::KernelContext &context) const override {
|
||||||
|
LOG(INFO) << "RowWiseAdd kernel in " << typeid(Place).name();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace operators
|
||||||
|
} // namespace paddle
|
@ -0,0 +1,49 @@
|
|||||||
|
/* 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/op_registry.h>
|
||||||
|
#include <paddle/operators/sigmoid_op.h>
|
||||||
|
namespace paddle {
|
||||||
|
namespace operators {
|
||||||
|
|
||||||
|
class SigmoidOp : public framework::OperatorWithKernel {
|
||||||
|
protected:
|
||||||
|
void InferShape(
|
||||||
|
const std::vector<const framework::Tensor *> &inputs,
|
||||||
|
const std::vector<framework::Tensor *> &outputs) const override {
|
||||||
|
PADDLE_ENFORCE(inputs.size() == 1, "Sigmoid Op only have one input");
|
||||||
|
PADDLE_ENFORCE(outputs.size() == 1, "Sigmoid Op only have one output");
|
||||||
|
outputs[0]->set_dims(inputs[0]->dims());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class SigmoidOpMaker : public framework::OpProtoAndCheckerMaker {
|
||||||
|
public:
|
||||||
|
SigmoidOpMaker(framework::OpProto *proto,
|
||||||
|
framework::OpAttrChecker *op_checker)
|
||||||
|
: framework::OpProtoAndCheckerMaker(proto, op_checker) {
|
||||||
|
AddInput("X", "sigmoid input");
|
||||||
|
AddInput("Y", "sigmoid output");
|
||||||
|
AddComment("Sigmoid function");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace operators
|
||||||
|
} // namespace paddle
|
||||||
|
|
||||||
|
REGISTER_OP(sigmoid,
|
||||||
|
paddle::operators::SigmoidOp,
|
||||||
|
paddle::operators::SigmoidOpMaker);
|
||||||
|
REGISTER_OP_CPU_KERNEL(
|
||||||
|
sigmoid, paddle::operators::SigmoidKernel<paddle::platform::CPUPlace>);
|
@ -0,0 +1,5 @@
|
|||||||
|
#include <paddle/operators/sigmoid_op.h>
|
||||||
|
#include <paddle/framework/op_registry.h>
|
||||||
|
|
||||||
|
REGISTER_OP_GPU_KERNEL(
|
||||||
|
sigmoid, paddle::operators::SigmoidKernel<paddle::platform::GPUPlace>);
|
@ -0,0 +1,31 @@
|
|||||||
|
/* 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 <glog/logging.h>
|
||||||
|
#include <paddle/framework/operator.h>
|
||||||
|
|
||||||
|
namespace paddle {
|
||||||
|
namespace operators {
|
||||||
|
|
||||||
|
template <typename Place>
|
||||||
|
class SigmoidKernel : public framework::OpKernel {
|
||||||
|
public:
|
||||||
|
void Compute(const framework::KernelContext &context) const override {
|
||||||
|
LOG(INFO) << "Sigmoid kernel in " << typeid(Place).name();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} // namespace operators
|
||||||
|
} // namespace paddle
|
@ -0,0 +1,49 @@
|
|||||||
|
/* 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/op_registry.h>
|
||||||
|
#include <paddle/operators/softmax_op.h>
|
||||||
|
|
||||||
|
namespace paddle {
|
||||||
|
namespace operators {
|
||||||
|
|
||||||
|
class SoftmaxOp : public framework::OperatorWithKernel {
|
||||||
|
protected:
|
||||||
|
void InferShape(
|
||||||
|
const std::vector<const framework::Tensor *> &inputs,
|
||||||
|
const std::vector<framework::Tensor *> &outputs) const override {
|
||||||
|
PADDLE_ENFORCE(inputs.size() == 1, "Only one input is need for softmax");
|
||||||
|
PADDLE_ENFORCE(outputs.size() == 1, "Only one output is need for softmax");
|
||||||
|
|
||||||
|
outputs[0]->set_dims(inputs[0]->dims());
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class SoftmaxOpMaker : public framework::OpProtoAndCheckerMaker {
|
||||||
|
public:
|
||||||
|
SoftmaxOpMaker(framework::OpProto *proto,
|
||||||
|
framework::OpAttrChecker *op_checker)
|
||||||
|
: OpProtoAndCheckerMaker(proto, op_checker) {
|
||||||
|
AddInput("X", "input of softmax");
|
||||||
|
AddOutput("Y", "output of softmax");
|
||||||
|
AddComment("Softmax Op");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace operators
|
||||||
|
} // namespace paddle
|
||||||
|
|
||||||
|
namespace ops = paddle::operators;
|
||||||
|
|
||||||
|
REGISTER_OP(softmax, ops::SoftmaxOp, ops::SoftmaxOpMaker);
|
||||||
|
REGISTER_OP_CPU_KERNEL(softmax, ops::SoftmaxKernel<paddle::platform::CPUPlace>);
|
@ -0,0 +1,5 @@
|
|||||||
|
#include <paddle/framework/op_registry.h>
|
||||||
|
#include <paddle/operators/softmax_op.h>
|
||||||
|
|
||||||
|
REGISTER_OP_GPU_KERNEL(
|
||||||
|
softmax, paddle::operators::SoftmaxKernel<paddle::platform::GPUPlace>);
|
@ -0,0 +1,31 @@
|
|||||||
|
/* 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 <glog/logging.h>
|
||||||
|
#include <paddle/framework/operator.h>
|
||||||
|
|
||||||
|
namespace paddle {
|
||||||
|
namespace operators {
|
||||||
|
|
||||||
|
template <typename Place>
|
||||||
|
class SoftmaxKernel : public framework::OpKernel {
|
||||||
|
public:
|
||||||
|
void Compute(const framework::KernelContext &context) const override {
|
||||||
|
LOG(INFO) << "Softmax kernel in " << typeid(Place).name();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
} // namespace operators
|
||||||
|
} // namespace paddle
|
@ -1 +1,2 @@
|
|||||||
cc_library(paddle_pybind SHARED SRCS pybind.cc DEPS pybind python add_op)
|
cc_library(paddle_pybind SHARED SRCS pybind.cc DEPS pybind python
|
||||||
|
add_op mul_op rowwise_add_op sigmoid_op softmax_op)
|
||||||
|
Loading…
Reference in new issue