Welcome to mirror list, hosted at ThFree Co, Russian Federation.

expression_operators.h « src - github.com/marian-nmt/marian.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3c3dc0310f20f2ac8784dbbc0e09dd8af233e637 (plain)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#pragma once

#include "graph.h"
#include "graph_operators.h"
#include "expressions.h"

namespace marian {

template <typename ...Args>
inline Expr input(Args ...args) {
  return Expr(new InputNode(args...));
}

template <typename ...Args>
inline Expr param(Args ...args) {
  return Expr(new ParamNode(args...));
}
template <typename ...Args>
inline Expr constant(Args ...args) {
  return Expr(new ConstantNode(args...));
}

template <typename ...Args>
inline Expr ones(Args ...args) {
  return Expr(new ConstantNode(keywords::value=1, args...));
}

template <typename ...Args>
inline Expr zeroes(Args ...args) {
  return Expr(new ConstantNode(keywords::value=0, args...));
}

/*********************************************************/

inline Expr sigmoid(Expr a) {
  return Expr(new SigmoidNodeOp(a));
}

inline Expr tanh(Expr a) {
  return Expr(new TanhNodeOp(a));
}

inline Expr log(Expr a) {
  return Expr(new LogNodeOp(a));
};

inline Expr exp(Expr a) {
  return Expr(new ExpNodeOp(a));
};

inline Expr operator-(Expr a) {
  return Expr(new NegNodeOp(a));
};

/*********************************************************/

inline Expr operator+(Expr a, Expr b) {
  return Expr(new PlusNodeOp(a, b));
}

inline Expr operator-(Expr a, Expr b) {
  return Expr(new MinusNodeOp(a, b));
}

inline Expr operator*(Expr a, Expr b) {
  return Expr(new MultNodeOp(a, b));
}

inline Expr operator/(Expr a, Expr b) {
  return Expr(new DivNodeOp(a, b));
}

inline Expr dot(Expr a, Expr b) {
  return Expr(new DotNodeOp(a, b));
}

/******************************************************/

Expr broadcast(Shape bShape, Expr a) {
  const Shape& aShape = a.node()->shape();
  if(aShape == bShape) {
    return a;
  }
  else {
    size_t dimsA = aShape.size();
    size_t dimsB = bShape.size();
    UTIL_THROW_IF2(dimsA != dimsB,
                   "Tensor and shape have different number of dimensions");
    for(size_t i = 0; i < dimsA; ++i) {
      int dimA = aShape[i];
      int dimB = bShape[i];
      bool broadcastable = (dimA == dimB || dimA == 1);
      UTIL_THROW_IF2(!broadcastable,
                     "Cannot broadcast tensor dimension "
                     << dimA << " to " << dimB);
      if(dimA == 1 && dimB != 1) {
        std::cerr << "Broadcasting dim " << i << " from " << dimA << " to " << dimB << std::endl;
        if(i == 0) {
          Expr one = ones(keywords::shape={bShape[0], 1});
          a = dot(one, a);
        }
        else if(i == 1) {
          Expr one = ones(keywords::shape={1, bShape[1]});
          a = dot(a, one);
        }
        else {
          UTIL_THROW2("Not implemented");        
        }
      }
    }
    return a;
  }
}

/*********************************************************/

// inefficient
template <typename ...Args>
inline Expr sum(Expr a, Args ...args) {
  using namespace keywords;
  Keywords params(args...);
  int ax = params.Get<int>(axis, whatevs);
  
  ChainPtr n = a.node();
  if(ax == 0) {
    auto lshape = [n]() -> Shape {
      int rows = n->val().shape()[0];
      return {1, rows};
    };
    Expr one = ones(shape={1, n->shape()[0]},
                    lazy_shape=lshape);
    return dot(one, a);        
  }
  else if(ax == 1) {
    auto lshape = [n]() -> Shape {
      int cols = n->val().shape()[1]; 
      return {cols, 1};
    };
    Expr one = ones(shape={n->shape()[1], 1},
                    lazy_shape=lshape);
    return dot(a, one);          
  }
  else if(ax == 2) {
    UTIL_THROW2("Not implemented");
  }
  else if(ax == 3) {
    UTIL_THROW2("Not implemented");
  }
  return sum(sum(a, axis=0), axis=1);
}

// inefficient
template <typename ...Args>
inline Expr softmax(Expr a, Args ...args) {
  Expr e = exp(a);
  return e / sum(e, args...);
}

// inefficient
template <typename ...Args>
inline Expr mean(Expr a, Args ...args) {
  using namespace keywords;
  Keywords params(args...);
  size_t ax = params.Get<int>(axis, whatevs);

  ChainPtr n = a.node();
  switch (ax) {
    case 0:
      return sum(a, axis=0) / constant(shape={1, 1},
                                       lazy_value=[n]() -> Float {
                                         return n->val().shape()[0];
                                       });
    case 1:
      return sum(a, axis=1) / constant(shape={1, 1},
                                       lazy_value=[n]() -> Float {
                                         return n->val().shape()[1];
                                       });
    case 2:
      UTIL_THROW2("Not implemented");
    case 3:
      UTIL_THROW2("Not implemented");
    default:
      return sum(a) / constant(shape={1, 1},
                               lazy_value=[n]() -> Float {
                                 return n->val().size();
                               });
  }
}

}