Apache SINGA
A distributed deep learning platform .
 All Classes Namespaces Files Functions Variables Typedefs Macros
tensor_expr.h
Go to the documentation of this file.
1 #ifndef MSHADOW_TENSOR_EXPR_H
2 #define MSHADOW_TENSOR_EXPR_H
3 
8 #include "tensor_base.h"
9 
10 namespace mshadow{
17  namespace expr{
18 
20  namespace type{
22  const int kContainer = 0;
24  const int kMapper = 1;
26  const int kComplex = 3;
27  };
28 
33  template<typename Saver,typename Container>
34  struct ExpEngine{
35  template<typename EType>
36  inline static void Eval( Container& dst, const EType &exp );
37  };
38 
39  template<typename Container>
40  class ContainerExp;
41  class ScalarExp;
42 
48  template<typename SubType, int exp_type>
49  struct Exp{
50  public:
52  inline const SubType& self( void ) const{
53  return *static_cast<const SubType*>(this);
54  }
56  inline SubType& refself( void ){
57  return *static_cast<SubType*>(this);
58  }
59  };
60 
62  struct ScalarExp: public Exp<ScalarExp, type::kMapper>{
66  ScalarExp( real_t scalar ):scalar_(scalar){}
67  };
68 
70  template<typename EType>
71  struct TransposeExp: public Exp< TransposeExp<EType>, type::kComplex >{
72  public:
74  const EType &exp;
76  TransposeExp( const EType &e ):exp(e){}
78  inline const EType & T( void ) const{
79  return exp;
80  }
81  };
82 
87  template<typename Container>
88  class ContainerExp: public Exp< Container, type::kContainer >{
89  public:
94  inline const TransposeExp<Container> T( void ) const{
95  return TransposeExp<Container>( this->self() );
96  }
97  public:
99  inline Container &operator+=( real_t s ){
101  return this->refself();
102  }
104  inline Container &operator-=( real_t s ){
106  return this->refself();
107  }
109  inline Container &operator*=( real_t s ){
111  return this->refself();
112  }
114  inline Container &operator/=( real_t s ){
116  return this->refself();
117  }
119  inline Container &__assign( real_t s ){
121  return this->refself();
122  }
123  public:
125  template<typename E>
126  inline Container &__assign( const Exp<E,type::kMapper> &exp ){
128  return this->refself();
129  }
131  template<typename E>
132  inline Container &__assign( const Exp<E,type::kComplex> &exp ){
134  return this->refself();
135  }
137  template<typename E,int etype>
138  inline Container &operator+=( const Exp<E,etype> &exp ){
140  return this->refself();
141  }
143  template<typename E,int etype>
144  inline Container &operator-=( const Exp<E,etype> &exp ){
146  return this->refself();
147  }
149  template<typename E,int etype>
150  inline Container &operator*=( const Exp<E,etype> &exp ){
152  return this->refself();
153  }
155  template<typename E,int etype>
156  inline Container &operator/=( const Exp<E,etype> &exp ){
158  return this->refself();
159  }
160  };
161  }; // namespace expr
162 
163  namespace expr{
171  template<typename TA,typename TB,bool ltrans,bool rtrans>
172  struct DotExp: public Exp< DotExp<TA,TB,ltrans,rtrans>, type::kComplex >{
174  const TA& lhs_;
176  const TB& rhs_;
180  DotExp( const TA &lhs, const TB &rhs, real_t scale )
181  :lhs_(lhs),rhs_(rhs),scale_(scale){}
182  };
183 
185  template<typename TA, typename TB>
187  return DotExp<TA,TB,false,false>( lhs.self(), rhs.self(), 1.0f );
188  }
190  template<typename TA, typename TB>
192  return DotExp<TA,TB,true,false>( lhs.exp, rhs.self(), 1.0f );
193  }
195  template<typename TA, typename TB>
197  return DotExp<TA,TB,false,true>( lhs.self(), rhs.exp, 1.0f );
198  }
200  template<typename TA, typename TB>
202  return DotExp<TA,TB,true,true>( lhs.exp, rhs.exp, 1.0f );
203  }
205  template<typename TA, typename TB, bool ltrans, bool rtrans >
207  return DotExp<TA,TB,ltrans,rtrans>( lhs.lhs_, lhs.rhs_, lhs.scale_ * rhs );
208  }
210  template<typename TA, typename TB, bool ltrans, bool rtrans >
212  return DotExp<TA,TB,ltrans,rtrans>( rhs.lhs_, rhs.rhs_, rhs.scale_ * lhs );
213  }
214  }; // namespace expr
215 
216  namespace expr{
224  template<typename OP, typename TA, typename TB, int etype >
225  struct BinaryMapExp: public Exp< BinaryMapExp<OP,TA,TB,etype>, etype >{
227  const TA& lhs_;
229  const TB& rhs_;
231  BinaryMapExp( const TA &lhs, const TB &rhs )
232  :lhs_(lhs), rhs_(rhs){}
233  };
234 
236  template<typename OP,typename TA, typename TB, int ta, int tb>
239  }
240 
252  template<typename OP,typename TA, typename TB, int ta, int tb>
254  return MakeExp<OP>( lhs, rhs );
255  }
257  template<typename OP,typename TA, int ta>
259  return MakeExp<OP>( lhs, rhs );
260  }
262  template<typename OP,typename TB, int tb>
264  return MakeExp<OP>( lhs, rhs );
265  }
266 
267  // operator rules
269  template<typename TA, typename TB, int ta, int tb>
271  return MakeExp<op::plus>( lhs, rhs );
272  }
274  template<typename TA, typename TB, int ta, int tb>
276  return MakeExp<op::minus>( lhs, rhs );
277  }
279  template<typename TA, typename TB, int ta, int tb>
281  return MakeExp<op::mul>( lhs, rhs );
282  }
284  template<typename TA, typename TB, int ta, int tb>
286  return MakeExp<op::div>( lhs, rhs );
287  }
288  // constant operators
290  template<typename TA, int ta>
292  return MakeExp<op::plus>( lhs, rhs );
293  }
295  template<typename TA, int ta>
297  return MakeExp<op::minus>( lhs, rhs );
298  }
300  template<typename TA, int ta>
302  return MakeExp<op::mul>( lhs, rhs );
303  }
305  template<typename TA, int ta>
307  return MakeExp<op::div>( lhs, rhs );
308  }
309  // constant operators 2
311  template<typename TB, int tb>
313  return MakeExp<op::plus>( lhs, rhs );
314  }
316  template<typename TB, int tb>
318  return MakeExp<op::minus>( lhs, rhs );
319  }
321  template<typename TB, int tb>
323  return MakeExp<op::mul>( lhs, rhs );
324  }
326  template<typename TB, int tb>
328  return MakeExp<op::div>( lhs, rhs );
329  }
330  };
331 
332  namespace expr{
339  template<typename OP, typename TA, int etype >
340  struct UnaryMapExp: public Exp< UnaryMapExp<OP,TA,etype>, etype >{
342  const TA& src_;
344  UnaryMapExp( const TA &src ):src_(src){}
345  };
346 
348  template<typename OP,typename TA, int ta>
351  }
352 
361  template<typename OP,typename TA, int ta>
363  return MakeExp<OP>(src);
364  }
365  };
366 };
367 #endif
DotExp< TA, TB, false, false > dot(const ContainerExp< TA > &lhs, const ContainerExp< TB > &rhs)
dot operator def
Definition: tensor_expr.h:186
DotExp< TA, TB, ltrans, rtrans > operator*(const DotExp< TA, TB, ltrans, rtrans > &lhs, real_t rhs)
dot operator def
Definition: tensor_expr.h:206
const int kMapper
this only contains element-wise vector operations
Definition: tensor_expr.h:24
SubType & refself(void)
Definition: tensor_expr.h:56
Container & operator/=(real_t s)
operator overload
Definition: tensor_expr.h:114
const int kComplex
othercase: e.g dot product
Definition: tensor_expr.h:26
binary map expression lhs [op] rhs
Definition: tensor_expr.h:225
Container & operator-=(const Exp< E, etype > &exp)
implementation of operator-=
Definition: tensor_expr.h:144
Container & operator-=(real_t s)
operator overload
Definition: tensor_expr.h:104
BinaryMapExp< OP, TA, TB,(ta|tb|type::kMapper) > F(const Exp< TA, ta > &lhs, const Exp< TB, tb > &rhs)
short hand for MakeExp, usage F<op>(lhs, rhs). create a binary operation expression ...
Definition: tensor_expr.h:253
float real_t
type that will be used for content
Definition: tensor_base.h:118
TransposeExp(const EType &e)
constructor
Definition: tensor_expr.h:76
const SubType & self(void) const
Definition: tensor_expr.h:52
BinaryMapExp< OP, TA, TB,(ta|tb|type::kMapper) > MakeExp(const Exp< TA, ta > &lhs, const Exp< TB, tb > &rhs)
make expression
Definition: tensor_expr.h:237
const int kContainer
this expression directly correspnds to a data class
Definition: tensor_expr.h:22
Container & operator+=(const Exp< E, etype > &exp)
implementation of operator+=
Definition: tensor_expr.h:138
const TA & src_
source expression
Definition: tensor_expr.h:342
BinaryMapExp< op::minus, TA, TB,(ta|tb|type::kMapper) > operator-(const Exp< TA, ta > &lhs, const Exp< TB, tb > &rhs)
operator overload
Definition: tensor_expr.h:275
const EType & T(void) const
transpose expression
Definition: tensor_expr.h:78
DotExp(const TA &lhs, const TB &rhs, real_t scale)
constructor
Definition: tensor_expr.h:180
BinaryMapExp< op::div, TA, TB,(ta|tb|type::kMapper) > operator/(const Exp< TA, ta > &lhs, const Exp< TB, tb > &rhs)
operator overload
Definition: tensor_expr.h:285
Container & operator+=(real_t s)
operator overload
Definition: tensor_expr.h:99
const TransposeExp< Container > T(void) const
transpose of a matrix
Definition: tensor_expr.h:94
Container & __assign(real_t s)
operator overload
Definition: tensor_expr.h:119
ScalarExp(real_t scalar)
constructor
Definition: tensor_expr.h:66
represent a transpose expression of a container
Definition: tensor_expr.h:71
Container & __assign(const Exp< E, type::kComplex > &exp)
implementation of operator=, note that we can not define container = container
Definition: tensor_expr.h:132
definitions of base types, macros functions
Container & operator*=(const Exp< E, etype > &exp)
implementation of operator*=
Definition: tensor_expr.h:150
unary map expression op(src)
Definition: tensor_expr.h:340
matrix multiplication expression dot( lhs[.T], rhs[.T] )
Definition: tensor_expr.h:172
Container & __assign(const Exp< E, type::kMapper > &exp)
implementation of operator=, note that we can not define container = container
Definition: tensor_expr.h:126
scalar expression
Definition: tensor_expr.h:62
Container & operator*=(real_t s)
operator overload
Definition: tensor_expr.h:109
base class for expression
Definition: tensor_expr.h:49
const TB & rhs_
right operand
Definition: tensor_expr.h:176
const TA & lhs_
left operand
Definition: tensor_expr.h:227
UnaryMapExp(const TA &src)
constructor
Definition: tensor_expr.h:344
base class of all variables, that can be assigned to values
Definition: tensor_expr.h:40
expression engine that actually interprets these expressions this is a function template that needed ...
Definition: tensor_expr.h:34
BinaryMapExp(const TA &lhs, const TB &rhs)
constructor
Definition: tensor_expr.h:231
real_t scale_
scale over result
Definition: tensor_expr.h:178
real_t scalar_
scalar value
Definition: tensor_expr.h:64
const TA & lhs_
left operand
Definition: tensor_expr.h:174
const TB & rhs_
right operand
Definition: tensor_expr.h:229
BinaryMapExp< op::plus, TA, TB,(ta|tb|type::kMapper) > operator+(const Exp< TA, ta > &lhs, const Exp< TB, tb > &rhs)
operator overload
Definition: tensor_expr.h:270
const EType & exp
expression to be transposed
Definition: tensor_expr.h:74
Container & operator/=(const Exp< E, etype > &exp)
implementation of operator/=
Definition: tensor_expr.h:156