Skip to content

Feature/mod op #5454

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

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
55 changes: 55 additions & 0 deletions paddle/operators/elementwise_mod_op.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* 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/elementwise_mod_op.h"
#include "paddle/operators/elementwise_op.h"

namespace paddle {
namespace operators {

// the fundamental operator in program language is + - * / %
// https://softwareengineering.stackexchange.com/questions/206293/why-is-mod-a-fundamental-mathematical-operator-in-many-programming-languages

class ElementwiseModOpMaker : public framework::OpProtoAndCheckerMaker {
public:
ElementwiseModOpMaker(framework::OpProto* proto,
framework::OpAttrChecker* op_checker)
: OpProtoAndCheckerMaker(proto, op_checker) {
AddInput("X", "(Tensor) The first input tensor of elementwise op");
AddInput("Y", "(Tensor) The second input tensor of elementwise op");
AddOutput("Out", "The output of elementwise op");
AddComment(R"DOC(
ElementwiseMod Operator.

X is a Tensor, Y is a Scalar. Note that Y only can be int(int32).
And Gradient of Y at int position is undefined.
Out is a Tensor which is mod elementwise of Y.

$Out = X % Y$

)DOC");
}
};
} // namespace operators
} // namespace paddle

namespace ops = paddle::operators;
REGISTER_OP(elementwise_mod, ops::ElementwiseOp, ops::ElementwiseModOpMaker,
elementwise_mod_grad, ops::ElementwiseOpGrad);
REGISTER_OP_CPU_KERNEL(
elementwise_mod,
ops::ElementwiseModKernel<paddle::platform::CPUPlace, int64_t>);
REGISTER_OP_CPU_KERNEL(
elementwise_mod_grad,
ops::ElementwiseModGradKernel<paddle::platform::CPUPlace, int64_t>);
25 changes: 25 additions & 0 deletions paddle/operators/elementwise_mod_op.cu
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* 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. */

#define EIGEN_USE_GPU
#include "paddle/operators/elementwise_mod_op.h"

namespace ops = paddle::operators;

REGISTER_OP_GPU_KERNEL(
elementwise_mod,
ops::ElementwiseModKernel<paddle::platform::GPUPlace, int64_t>);
REGISTER_OP_GPU_KERNEL(
elementwise_mod_grad,
ops::ElementwiseModGradKernel<paddle::platform::GPUPlace, int64_t>);
94 changes: 94 additions & 0 deletions paddle/operators/elementwise_mod_op.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/* 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 "paddle/framework/eigen.h"
#include "paddle/framework/op_registry.h"
#include "paddle/framework/tensor_util.h"

namespace paddle {
namespace operators {

template <typename T, int MajorType = Eigen::RowMajor,
typename IndexType = Eigen::DenseIndex>
using EigenVector = framework::EigenVector<T, MajorType, IndexType>;

template <typename T, int MajorType = Eigen::RowMajor,
typename IndexType = Eigen::DenseIndex>
using EigenScalar = framework::EigenScalar<T, MajorType, IndexType>;

using framework::Tensor;

template <typename Place, typename T>
class ElementwiseModKernel : public framework::OpKernel<T> {
public:
void Compute(const framework::ExecutionContext& ctx) const override {
// only support Tensor % Scalar
auto* x = ctx.Input<Tensor>("X");
auto* y = ctx.Input<Tensor>("Y");
auto* z = ctx.Output<Tensor>("Out");
z->mutable_data<T>(ctx.GetPlace());

auto x_dims = x->dims();
auto y_dims = y->dims();

PADDLE_ENFORCE_GE(x_dims.size(), y_dims.size(),
"Rank of first input must >= rank of second input.");
PADDLE_ENFORCE(
y_dims[0] == 1,
" ElementwiseModOp Input(Y) must be Scalar, shape equal 1.");

auto x_e = framework::EigenVector<T>::Flatten(*x);
auto y_e = framework::EigenScalar<int64_t>::From(*y);
auto z_e = framework::EigenVector<T>::Flatten(*z);
z_e.device(ctx.GetEigenDevice<Place>()) = x_e % y_e(0);
}
};

template <typename Place, typename T>
class ElementwiseModGradKernel : public framework::OpKernel<T> {
public:
void Compute(const framework::ExecutionContext& ctx) const override {
// at integer point, the gradient is undefined.
// https://math.stackexchange.com/questions/1849280/derivative-of-remainder-function-wrt-denominator
auto* x = ctx.Input<Tensor>("X");
auto* y = ctx.Input<Tensor>("Y");
auto* dz = ctx.Input<Tensor>(framework::GradVarName("Out"));

auto* dx = ctx.Output<Tensor>(framework::GradVarName("X"));
auto* dy = ctx.Output<Tensor>(framework::GradVarName("Y"));
auto x_e = framework::EigenVector<T>::Flatten(*x);
auto y_e = framework::EigenScalar<int64_t>::From(*y);
auto dz_e = framework::EigenVector<T>::Flatten(*dz);

auto place = ctx.GetEigenDevice<Place>();

if (dx) {
dx->mutable_data<T>(ctx.GetPlace());
auto dx_e = framework::EigenVector<T>::Flatten(*dx);
dx_e.device(place) = dz_e;
}

if (dy) {
dy->mutable_data<T>(ctx.GetPlace());
auto dy_e = framework::EigenScalar<int64_t>::From(*dy);
auto floor_div = x_e / y_e;
dy_e.device(place) = -1.0 * dz_e * floor_div.floor();
}
}
};

} // namespace operators
} // namespace paddle
Empty file modified paddle/operators/sequence_slice_op.h
100644 → 100755
Empty file.
39 changes: 39 additions & 0 deletions python/paddle/v2/fluid/tests/test_elementwise_mod_op.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import unittest
import numpy as np
from op_test import OpTest


class ElementwiseModOp(OpTest):
def setUp(self):
self.op_type = "elementwise_mod"
""" Warning
CPU gradient check error!
'X': np.random.randint((32,84)).astype("int32"),
'Y': np.random.randint((32,84)).astype("int32")
"""
self.inputs = {
'X': np.random.randint(1, 10, [13, 17]).astype("int64"),
'Y': np.random.randint(1, 10, [1,]).astype("int64")
}
self.outputs = {'Out': np.mod(self.inputs['X'], self.inputs['Y'])}

def test_check_output(self):
self.check_output()

# TODO(dzhwinter) : need int gradient support
# currently our op_test framework only support float/double gradient
# elementwise_mod has gradient, set 0 when hits the interger(undefined in math)
# def test_check_grad_normal(self):
# self.check_grad(['X', 'Y'], 'Out', max_relative_error=0.05)

# def test_check_grad_ingore_x(self):
# self.check_grad(
# ['Y'], 'Out', max_relative_error=0.05, no_grad_set=set("X"))

# def test_check_grad_ingore_y(self):
# self.check_grad(
# ['X'], 'Out', max_relative_error=0.05, no_grad_set=set('Y'))


if __name__ == '__main__':
unittest.main()