Skip to content

Commit 2e57737

Browse files
committed
add cos
1 parent 25317bd commit 2e57737

File tree

5 files changed

+84
-0
lines changed

5 files changed

+84
-0
lines changed

paddle/fluid/operators/activation_op.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,21 @@ Floor Activation Operator.
260260
}
261261
};
262262

263+
class CosOpMaker : public framework::OpProtoAndCheckerMaker {
264+
public:
265+
CosOpMaker(OpProto *proto, OpAttrChecker *op_checker)
266+
: framework::OpProtoAndCheckerMaker(proto, op_checker) {
267+
AddInput("X", "Input of Floor operator");
268+
AddOutput("Out", "Output of Floor operator");
269+
AddComment(R"DOC(
270+
Floor Activation Operator.
271+
272+
$out = cos(x)$
273+
274+
)DOC");
275+
}
276+
};
277+
263278
class RoundOpMaker : public framework::OpProtoAndCheckerMaker {
264279
public:
265280
RoundOpMaker(OpProto *proto, OpAttrChecker *op_checker)
@@ -561,6 +576,9 @@ REGISTER_OP(ceil, ops::ActivationOp, ops::CeilOpMaker, ceil_grad,
561576
REGISTER_OP(floor, ops::ActivationOp, ops::FloorOpMaker, floor_grad,
562577
ops::ActivationOpGrad);
563578

579+
REGISTER_OP(cos, ops::ActivationOp, ops::CosOpMaker, cos_grad,
580+
ops::ActivationOpGrad);
581+
564582
REGISTER_OP(round, ops::ActivationOp, ops::RoundOpMaker, round_grad,
565583
ops::ActivationOpGrad);
566584

paddle/fluid/operators/activation_op.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,54 @@ struct FloorFunctor : public BaseActivationFunctor<T> {
331331
}
332332
};
333333

334+
template <typename T>
335+
struct Sine {
336+
HOSTDEVICE T operator()(const T& val) const { return sin(val); }
337+
};
338+
339+
template <typename T>
340+
struct Cosine {
341+
HOSTDEVICE T operator()(const T& val) const { return cos(val); }
342+
};
343+
344+
// cosine'(x) = -sin(x)
345+
template <typename T>
346+
struct CosGradFunctor : public BaseActivationFunctor<T> {
347+
template <typename Device, typename X, typename Out, typename dOut,
348+
typename dX>
349+
void operator()(Device d, X x, Out out, dOut dout, dX dx) const {
350+
dx.device(d) = -dout * x.unaryExpr(Sine<T>());
351+
}
352+
};
353+
354+
// cosine(x) = cos(x)
355+
template <typename T>
356+
struct CosFunctor : public BaseActivationFunctor<T> {
357+
template <typename Device, typename X, typename Out>
358+
void operator()(Device d, X x, Out out) const {
359+
out.device(d) = x.unaryExpr(Cosine<T>());
360+
}
361+
};
362+
363+
// sine'(x) = cos(x)
364+
template <typename T>
365+
struct SinGradFunctor : public BaseActivationFunctor<T> {
366+
template <typename Device, typename X, typename Out, typename dOut,
367+
typename dX>
368+
void operator()(Device d, X x, Out out, dOut dout, dX dx) const {
369+
dx.device(d) = dout * x.unaryExpr(Cosine<T>());
370+
}
371+
};
372+
373+
// sine(x) = sin(x)
374+
template <typename T>
375+
struct SinFunctor : public BaseActivationFunctor<T> {
376+
template <typename Device, typename X, typename Out>
377+
void operator()(Device d, X x, Out out) const {
378+
out.device(d) = x.unaryExpr(Sine<T>());
379+
}
380+
};
381+
334382
// round(x) = [x]
335383
template <typename T>
336384
struct RoundFunctor : public BaseActivationFunctor<T> {
@@ -782,6 +830,7 @@ struct SwishGradFunctor : public BaseActivationFunctor<T> {
782830
__macro(abs, AbsFunctor, AbsGradFunctor); \
783831
__macro(ceil, CeilFunctor, ZeroGradFunctor); \
784832
__macro(floor, FloorFunctor, ZeroGradFunctor); \
833+
__macro(cos, CosFunctor, CosGradFunctor); \
785834
__macro(round, RoundFunctor, ZeroGradFunctor); \
786835
__macro(reciprocal, ReciprocalFunctor, ReciprocalGradFunctor); \
787836
__macro(log, LogFunctor, LogGradFunctor); \

paddle/function/EigenGemm.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ struct EigenBlasGemm {
6363
const EigenMatrix a(const_cast<T*>(A), sizeA);
6464
const EigenMatrix b(const_cast<T*>(B), sizeB);
6565
EigenMatrix c(C, sizeC);
66+
Eigen::Tensor<T, 2, Eigen::RowMajor, int> ss;
6667

6768
typedef typename Eigen::Tensor<T, 2>::DimensionPair DimPair;
6869
Eigen::array<DimPair, 1> dims;

python/paddle/fluid/layers/ops.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
'abs',
2626
'ceil',
2727
'floor',
28+
'cos',
2829
'round',
2930
'reciprocal',
3031
'log',

python/paddle/fluid/tests/unittests/test_activation_op.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import unittest
1616
import numpy as np
17+
import math
1718
import paddle.fluid.core as core
1819
from op_test import OpTest
1920
from scipy.special import expit
@@ -196,6 +197,20 @@ def test_check_grad(self):
196197
self.check_grad(['X'], 'Out', max_relative_error=0.007)
197198

198199

200+
class TestCos(OpTest):
201+
def setUp(self):
202+
self.op_type = "cos"
203+
x = np.random.uniform(-1, 1, [4, 4]).astype("float32")
204+
self.inputs = {'X': x}
205+
self.outputs = {'Out': math.cos(self.inputs['X'])}
206+
207+
def test_check_output(self):
208+
self.check_output()
209+
210+
def test_check_grad(self):
211+
self.check_grad(['X'], 'Out', max_relative_error=0.007)
212+
213+
199214
class TestRound(OpTest):
200215
def setUp(self):
201216
self.op_type = "round"

0 commit comments

Comments
 (0)