Module: TensorStream::MathOps

Included in:
Evaluator::RubyEvaluator
Defined in:
lib/tensor_stream/evaluator/ruby/math_ops.rb

Class Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/tensor_stream/evaluator/ruby/math_ops.rb', line 3

def self.included(klass)
  klass.class_eval do
    register_op :tanh, no_eval: true do |context, _tensor, inputs|
      call_op(inputs[0], context) { |t, _b| Math.tanh(t) }
    end

    register_op :tan, no_eval: true do |context, tensor, inputs|
      call_op(inputs[0], context) { |t, _b| Math.tan(t) }
    end

    register_op :atan, no_eval: true do |context, _tensor, inputs|
      call_op(inputs[0], context) { |t, _b| Math.atan(t) }
    end

    register_op :sin, no_eval: true do |context, _tensor, inputs|
      call_op(inputs[0], context) { |t, _b| Math.sin(t) }
    end

    register_op :add, no_eval: true do |context, tensor, inputs|
      a, b = inputs
      call_vector_op(tensor, :add, a, b, context) { |t, u| t + u }
    end

    register_op :add_n, no_eval: true do |context, tensor, inputs|
      if inputs.size == 1
        complete_eval(inputs[0], context)
      elsif inputs.size > 1

        a = inputs.pop
        until inputs.empty?
          b = inputs.pop
          a = call_vector_op(tensor, :add, a, b, context) { |t, u| t + u }
        end
        a
      end
    end

    register_op :bias_add do |_context, _tensor, inputs|
      value, bias = inputs
      arr = value.flatten.each_slice(bias.size).map do |slice|
        slice.each_with_index.map { |elem, index| elem + bias[index] }
      end
      TensorShape.reshape(arr, shape_eval(value))
    end

    register_op :bias_add_grad do |_context, _tensor, inputs|
      received_grad = inputs[0]
      bias_size = shape_eval(received_grad).last
      grad_sum = Array.new(bias_size) { 0.0 }
      received_grad.flatten.each_slice(bias_size) do |slice|
        slice.each_with_index.map { |elem, index| grad_sum[index] += elem }
      end
      grad_sum
    end

    register_op :sub, no_eval: true do |context, tensor, inputs|
      a, b = inputs
      call_vector_op(tensor, :sub, a, b, context) { |t, u| t - u }
    end

    register_op %i[floor_mod mod], no_eval: true do |context, tensor, inputs|
      a, b = inputs
      call_vector_op(tensor, :mod, a, b, context) { |t, u| t % u }
    end

    register_op %i[floor_div], no_eval: true do |context, tensor, inputs|
      a, b = inputs
      if fp_type?(tensor.data_type)
        call_vector_op(tensor, :div, a, b, context) { |t, u| (t / u).to_i.to_f }
      else
        call_vector_op(tensor, :div, a, b, context) { |t, u| t / u }
      end
    end

    register_op :mul, no_eval: true do |context, tensor, inputs|
      a, b = inputs
      call_vector_op(tensor, :mul, a, b, context) { |t, u| t * u }
    end

    register_op :pow, no_eval: true do |context, tensor, inputs|
      a, b = inputs
      call_vector_op(tensor, :pow, a, b, context) { |t, u| t**u }
    end

    register_op :squared_difference, no_eval: true do |context, tensor, inputs|
      a, b = inputs
      call_vector_op(tensor, :squared_difference, a, b, context) { |t, u| (t - u) * (t - u) }
    end

    register_op :round, no_eval: true do |context, _tensor, inputs|
      call_op(inputs[0], context) { |t, _b|  t.round }
    end

    register_op :abs, no_eval: true do |context, _tensor, inputs|
      call_op(inputs[0], context) { |t, _b| t.abs }
    end

    register_op :asin, no_eval: true do |context, _tensor, inputs|
      call_op(inputs[0], context) { |t, _b| Math.asin(t) }
    end

    register_op :acos, no_eval: true do |context, _tensor, inputs|
      call_op(inputs[0], context) { |t, _b| Math.acos(t) }
    end

    register_op :cos, no_eval: true do |context, tensor, inputs|
      call_op(inputs[0], context) { |t, _b| Math.cos(t) }
    end

    register_op :log1p, no_eval: true do |context, _tensor, inputs|
      call_op(inputs[0], context) { |t, _b| Math.log(1 + t) }
    end

    register_op :log, no_eval: true do |context, _tensor, inputs|
      call_op(inputs[0], context) { |t, _b| t < 0 ? Float::NAN : Math.log(t) }
    end

    register_op :exp, no_eval: true do |context, _tensor, inputs|
      call_op(inputs[0], context) { |t, _b| Math.exp(t) }
    end

    register_op :sigmoid, no_eval: true do |context, _tensor, inputs|
      call_op(inputs[0], context) { |t, _b| sigmoid(t) }
    end

    register_op :sqrt, no_eval: true do |context, _tensor, inputs|
      call_op(inputs[0], context) { |t, _b| Math.sqrt(t) }
    end

    register_op :rsqrt, no_eval: true do |context, _tensor, inputs|
      call_op(inputs[0], context) { |t, _b|  1 / Math.sqrt(t) }
    end

    register_op :rsqrt_grad, no_eval: true do |context, tensor, inputs|
      y, grad = inputs
      call_vector_op(tensor, :rsqrt_grad, y, grad, context) { |_y, g| 0.5 * g * (_y ** 3) }
    end

    register_op :floor, no_eval: true do |context, _tensor, inputs|
      call_op(inputs[0], context) { |t, _b| t.floor }
    end

    register_op :ceil, no_eval: true do |context, _tensor, inputs|
      call_op(inputs[0], context) { |t, _b| t.ceil }
    end

    register_op :square, no_eval: true do |context, _tensor, inputs|
      call_op(inputs[0], context) { |t, _b| t * t }
    end

    register_op :reciprocal, no_eval: true do |context, _tensor, inputs|
      call_op(inputs[0], context) { |t, _b| 1 / t }
    end

    register_op %i[neg negate], no_eval: true do |context, tensor, inputs|
      call_vector_op(tensor, :negate, inputs[0], nil, context) { |t, _u| -t }
    end

    register_op :tanh_grad, no_eval: true do |context, _tensor, inputs|
      call_op(inputs[0], context) { |t, _b| 1 - Math.tanh(t) * Math.tanh(t) }
    end

    register_op :top_k do |context, tensor, inputs|
      values, k = inputs
      v_shape = shape_eval(values)

      sorted = tensor.options[:sorted]
      work_values = TensorShape.reshape(values, [-1, v_shape.last])
      work_values.map! do |row|
        last_k = row.map.with_index { |r, index| [r, index] }.sort! { |a,b| a[0] <=> b[0] }.last(k)
        last_k.reverse! if sorted
        last_k
      end

      top_k = work_values.map { |row| row.map { |r| r[0] } }
      top_indices = work_values.map { |row| row.map { |r| r[1] } }
      v_shape[-1] = k

      TensorStream::Evaluator::OutputGroup.new([TensorShape.reshape(top_k, v_shape), TensorShape.reshape(top_indices, v_shape)], [tensor.inputs[0].data_type, :int32])
    end

    register_op(%i[argmax arg_max]) do |_context, tensor, inputs|
      axis = inputs[1] || 0
      rank = get_rank(inputs[0])
      raise TensorStream::InvalidArgumentError, "Expected dimension in the range [#{-rank},#{rank}) but got #{axis}" if axis < -rank || axis >= rank

      new_shape = shape_eval(inputs[0])
      ns = new_shape.each_with_index.collect { |shape, index|
        next nil if index == axis

        shape
      }.compact

      Tensor.cast_dtype(TensorShape.reshape(get_op_with_axis(inputs[0], axis, 0, :max), ns), tensor.options[:output_type])
    end

    register_op(%i[argmin arg_min]) do |_context, tensor, inputs|
      axis = inputs[1] || 0
      rank = get_rank(inputs[0])
      raise TensorStream::InvalidArgumentError, "Expected dimension in the range [#{-rank},#{rank}) but got #{axis}" if axis < -rank || axis >= rank

      new_shape = shape_eval(inputs[0])
      ns = new_shape.each_with_index.collect { |shape, index|
        next nil if index == axis

        shape
      }.compact

      Tensor.cast_dtype(TensorShape.reshape(get_op_with_axis(inputs[0], axis, 0, :min), ns), tensor.options[:output_type])
    end

    register_op :cumprod do |context, tensor, inputs|
      c = fp_type?(tensor.data_type) ? 1.0 : 1
      reverse_option = tensor.options[:reverse]
      exclusive = tensor.options[:exclusive]

      reduction(context, tensor) do |arr|
        if arr.nil?
          c
        else
          count = arr.size
          arr = arr.reverse if reverse_option
          arr = [1] + arr if exclusive

          start_prod = arr[0]
          mapped = arr[1...count].map { |v|
            start_prod = vector_op(start_prod, v) { |a, b| a * b }
          }

          arr = [arr[0]] + mapped
          reverse_option ? arr.reverse : arr
        end
      end
    end

    register_op :sum, noop: true do |context, tensor, _inputs|
      reduction(context, tensor) do |arr|
        reduced_val = arr[0]
        arr[1..arr.size].each do |v|
          reduced_val = vector_op(reduced_val, v) { |t, u| t + u }
        end
        reduced_val
      end
    end

    register_op :prod, noop: true do |context, tensor, _inputs|
      c = fp_type?(tensor.data_type) ? 1.0 : 1
      reduction(context, tensor) do |arr|
        if arr.nil?
          c
        else
          reduced_val = arr[0]
          arr[1..arr.size].each do |v|
            reduced_val = vector_op(reduced_val, v) { |a, b| a * b }
          end
          reduced_val
        end
      end
    end

    register_op :sigmoid_grad, no_eval: true do |context, tensor, inputs|
      a, b = inputs
      call_vector_op(tensor, :sigmoid_grad, a, b, context) { |t, u| u * sigmoid(t) * (1 - sigmoid(t)) }
    end

    register_op :mean, noop: true do |context, tensor, _inputs|
      c = fp_type?(tensor.data_type) ? 0.0 : 0

      reduction(context, tensor) do |arr|
        return c if arr.nil?

        reduced_val = arr[0]
        arr[1..arr.size].each do |v|
          reduced_val = vector_op(reduced_val, v) { |a, b| a + b }
        end

        vector_op(reduced_val, nil) { |a, _b| a / arr.size }
      end
    end

    register_op :mat_mul do |_context, tensor, inputs|
      matrix_a, matrix_b = inputs
      rank_a = get_rank(matrix_a)
      rank_b = get_rank(matrix_b)
      raise "#{tensor.inputs[0].name} rank must be greater than 1" if rank_a < 2
      raise "#{tensor.inputs[1].name} rank must be greater than 1" if rank_b < 2

      # check matrix dimensions
      if rank_a >= 3
        matrix_a.zip(matrix_b).map do |m_a, m_b|
          matmul(m_a, m_b, tensor)
        end
      else
        matmul(matrix_a, matrix_b, tensor)
      end
    end

    def matmul(m_a, m_b, tensor)
      m_a = m_a.transpose if tensor.options[:transpose_a]
      m_b = m_b.transpose if tensor.options[:transpose_b]
      raise TensorStream::ValueError, "incompatible shape sizes for matrix multiplication (#{m_a[0].size} != #{m_b.size}) #{shape_eval(m_a)} vs #{shape_eval(m_b)}" if m_a[0].size != m_b.size

      (Matrix[*m_a] * Matrix[*m_b]).to_a
    end

    register_op %i[max maximum], noop: true do |context, tensor, inputs|
      call_vector_op(tensor, :max, inputs[0], inputs[1], context) { |t, u| [t, u].max }
    end

    register_op %i[min minimum], noop: true do |context, tensor, inputs|
      call_vector_op(tensor, :min, inputs[0], inputs[1], context) { |t, u| [t, u].min }
    end

    def reduction(child_context, tensor, &block)
      val = global_eval(tensor, tensor.inputs[0], child_context)
      axis = global_eval(tensor, tensor.inputs[1], child_context)
      keep_dims = global_eval(tensor, tensor.options[:keepdims], child_context)

      reduce(val, axis, keep_dims, &block)
    end
  end
end