forked from apache/mxnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathndarray_function.h
More file actions
112 lines (94 loc) · 2.98 KB
/
ndarray_function.h
File metadata and controls
112 lines (94 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*!
* Copyright (c) 2015 by Contributors
* \file ndarray_op.h
* \brief the real execution functions of ndarray operations
*/
#ifndef MXNET_NDARRAY_NDARRAY_FUNCTION_H_
#define MXNET_NDARRAY_NDARRAY_FUNCTION_H_
#include <dmlc/logging.h>
#include <mshadow/tensor.h>
#include <mxnet/base.h>
#include <mxnet/resource.h>
#include <vector>
namespace mxnet {
/*! \brief namespace to support all possible Ndarray operator */
namespace ndarray {
struct BinaryBase {
inline static TShape GetShape(const TShape &lshape, const TShape &rshape) {
CHECK(lshape == rshape) << "operands shape mismatch";
CHECK(lshape.ndim() != 0) << "source operand have zero dimension shape";
return lshape;
}
};
// operators
struct Plus : public BinaryBase {
typedef mshadow::op::plus mshadow_op;
};
struct Minus : public BinaryBase {
typedef mshadow::op::minus mshadow_op;
};
struct Mul : public BinaryBase {
typedef mshadow::op::mul mshadow_op;
};
struct Div : public BinaryBase {
typedef mshadow::op::div mshadow_op;
};
struct ClipMin : public BinaryBase {
struct mshadow_op {
MSHADOW_XINLINE static real_t Map(real_t a, real_t b) {
if (a < b) {
return b;
} else {
return a;
}
}
};
};
struct ClipMax : public BinaryBase {
struct mshadow_op {
MSHADOW_XINLINE static real_t Map(real_t a, real_t b) {
if (a > b) {
return b;
} else {
return a;
}
}
};
};
struct Dot {
inline static TShape GetShape(const TShape &lshape, const TShape &rshape) {
CHECK(lshape.ndim() == 2 && rshape.ndim() == 2) << "dot only support 2D Array";
CHECK_EQ(lshape[1], rshape[0]) << "dot shape error: " << lshape << " X " << rshape;
size_t target_shape[] = {lshape[0], rshape[1]};
return TShape(target_shape, target_shape + 2);
}
};
// type holder for random number generators
struct UniformDistribution {};
struct GaussianDistribution {};
template<typename Device>
void EvalClip(const TBlob &src, const real_t &a_min, const real_t &a_max,
TBlob *ret, RunContext ctx);
template<typename Device, typename OP>
void Eval(const TBlob &lhs, const TBlob &rhs, TBlob *ret, RunContext ctx);
template<typename Device, typename OP, bool reverse>
void Eval(const TBlob &lhs, const real_t &rhs, TBlob *ret, RunContext ctx);
template<typename Device>
void Eval(const real_t &rhs, TBlob *ret, RunContext ctx);
template<typename Device, typename Distribution>
void EvalRandom(const real_t &a,
const real_t &b,
const Resource &resource,
TBlob *ret, RunContext ctx);
// copy function when only cpu is involved
template<typename DeviceFrom, typename DeviceTo>
void Copy(const TBlob &from, TBlob *to,
Context from_ctx, Context to_ctx,
RunContext ctx);
template<typename Device>
void ElementwiseSum(const std::vector<TBlob> source,
TBlob *out,
RunContext ctx);
} // namespace ndarray
} // namespace mxnet
#endif // MXNET_NDARRAY_NDARRAY_FUNCTION_H_