Module: TensorStream::ArrayOpsHelper

Included in:
Evaluator::RubyEvaluator, InferShape
Defined in:
lib/tensor_stream/evaluator/operation_helpers/array_ops_helper.rb

Overview

varoius utility functions for array processing

Instance Method Summary collapse

Instance Method Details

#_reduced_shape(input_shape, axes) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/tensor_stream/evaluator/operation_helpers/array_ops_helper.rb', line 41

def _reduced_shape(input_shape, axes)
  return [] if axes.nil? # reduce to scalar
  axes = [axes] unless axes.is_a?(Array)
  return input_shape if axes.empty?

  axes.each do |dimen|
    input_shape[dimen] = 1
  end
  input_shape
end

#arr_pad(arr, paddings, data_type = :float32, rank = 0) ⇒ Object



310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/tensor_stream/evaluator/operation_helpers/array_ops_helper.rb', line 310

def arr_pad(arr, paddings, data_type = :float32, rank = 0)
  raise "padding #{paddings[rank]} needs to have to elements [before, after]" if paddings[rank].size != 2

  before = paddings[rank][0]
  after = paddings[rank][1]
  pad_value = fp_type?(data_type) ? 0.0 : 0
  if arr[0].is_a?(Array)
    next_dim_elem = arr.collect { |a| arr_pad(a, paddings, data_type, rank + 1) }
    padding = deep_dup_array(next_dim_elem[0], pad_value)
    Array.new(before) { padding } + next_dim_elem + Array.new(after) { padding }
  else
    Array.new(before) { pad_value } + arr + Array.new(after) { pad_value }
  end
end

#broadcast(input_a, input_b) ⇒ Object



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
# File 'lib/tensor_stream/evaluator/operation_helpers/array_ops_helper.rb', line 52

def broadcast(input_a, input_b)
  sa = shape_eval(input_a)
  sb = shape_eval(input_b)

  return [input_a, input_b] if sa == sb

  # descalar
  if sa.empty?
    input_a = [input_a]
    sa = [1]
  end

  if sb.empty?
    input_b = [input_b]
    sb = [1]
  end

  target_shape = shape_diff(sa, sb)

  if target_shape
    input_b = broadcast_dimensions(input_b, target_shape)
  else
    target_shape = shape_diff(sb, sa)
    raise "Incompatible shapes for op #{shape_eval(input_a)} vs #{shape_eval(input_b)}" if target_shape.nil?

    input_a = broadcast_dimensions(input_a, target_shape)
  end

  [input_a, input_b]
end

#broadcast_dimensions(input, dims = []) ⇒ Object

explicit broadcasting helper



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/tensor_stream/evaluator/operation_helpers/array_ops_helper.rb', line 84

def broadcast_dimensions(input, dims = [])
  return input if dims.empty?

  d = dims.shift

  if input.is_a?(Array) && (get_rank(input) - 1) == dims.size
    row_to_dup = input.collect { |item|
      broadcast_dimensions(item, dims.dup)
    }

    row_to_dup + Array.new(d) { row_to_dup }.flatten(1)
  elsif input.is_a?(Array)
    Array.new(d) { broadcast_dimensions(input, dims.dup) }
  else
    Array.new(d + 1) { input }
  end
end

#deep_dup_array(arr, value = nil) ⇒ Object



325
326
327
328
329
330
331
332
333
# File 'lib/tensor_stream/evaluator/operation_helpers/array_ops_helper.rb', line 325

def deep_dup_array(arr, value = nil)
  if arr.is_a?(Array)
    arr.dup.collect do |a|
      deep_dup_array(a, value)
    end
  else
    value.nil? ? arr : value
  end
end

#gather(params, indexes) ⇒ Object



224
225
226
227
228
229
230
231
232
# File 'lib/tensor_stream/evaluator/operation_helpers/array_ops_helper.rb', line 224

def gather(params, indexes)
  indexes.collect do |index|
    if index.is_a?(Array)
      gather(params, index)
    else
      params[index]
    end
  end
end

#get_rank(value, rank = 0) ⇒ Object



177
178
179
180
181
182
# File 'lib/tensor_stream/evaluator/operation_helpers/array_ops_helper.rb', line 177

def get_rank(value, rank = 0)
  return rank unless value.is_a?(Array)
  return rank + 1 if value.empty?

  get_rank(value[0], rank + 1)
end

#last_axis(arr) ⇒ Object



184
185
186
187
188
189
190
# File 'lib/tensor_stream/evaluator/operation_helpers/array_ops_helper.rb', line 184

def last_axis(arr)
  return arr if get_rank(arr) <= 2

  arr.inject([]).map do |sub, rows|
    rows + last_axis(sub)
  end
end

#process_function_op(a, &block) ⇒ Object



168
169
170
171
172
173
174
175
# File 'lib/tensor_stream/evaluator/operation_helpers/array_ops_helper.rb', line 168

def process_function_op(a, &block)
  # ruby scalar
  if (a.is_a?(Tensor) && a.shape.rank > 0) || a.is_a?(Array)
    vector_op(a, 0, &block)
  else
    yield a, 0
  end
end

#reduce(val, axis, keep_dims, &block) ⇒ Object



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/tensor_stream/evaluator/operation_helpers/array_ops_helper.rb', line 293

def reduce(val, axis, keep_dims, &block)
  rank = get_rank(val)
  return val if axis&.is_a?(Array) && axis&.empty?

  axis = if axis.nil?
    nil
  elsif axis.is_a?(Array)
    return val if axis.empty?

    axis.map { |a| a < 0 ? rank - a.abs : a }
  else
    axis < 0 ? rank - axis.abs : axis
  end

  reduce_axis(0, axis, val, keep_dims, &block)
end

#reduce_axis(current_axis, axis, val, keep_dims, &block) ⇒ Object



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
# File 'lib/tensor_stream/evaluator/operation_helpers/array_ops_helper.rb', line 267

def reduce_axis(current_axis, axis, val, keep_dims, &block)
  return val unless val.is_a?(Array)

  r = val.collect { |v|
    reduce_axis(current_axis + 1, axis, v, keep_dims, &block)
  }

  should_reduce_axis = axis.nil? || (axis.is_a?(Array) && axis.include?(current_axis)) || (current_axis == axis)

  if should_reduce_axis
    reduced_val = r[0]
    if r.size > 1
      reduced_val = if block_given?
        yield(r[0..val.size])
      else
        r[0..val.size].reduce(:+)
      end
    elsif r.empty?
      reduced_val = yield(nil)
    end
    keep_dims ? [reduced_val] : reduced_val
  else
    r
  end
end

#shape_diff(shape_a, shape_b) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/tensor_stream/evaluator/operation_helpers/array_ops_helper.rb', line 139

def shape_diff(shape_a, shape_b)
  return nil if shape_b.size > shape_a.size

  reversed_a = shape_a.reverse
  reversed_b = shape_b.reverse

  reversed_a.each_with_index.collect { |s, index|
    next s if index >= reversed_b.size
    return nil if reversed_b[index] > s
    s - reversed_b[index]
  }.reverse
end

#slice_tensor(input, start, size) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/tensor_stream/evaluator/operation_helpers/array_ops_helper.rb', line 14

def slice_tensor(input, start, size)
  return input if size.empty?
  start_index = start.shift
  current_size = size.shift
  dimen_size = if current_size == -1
    input.size - 1
  else
    start_index + current_size - 1
  end

  input[start_index..dimen_size].collect do |item|
    if item.is_a?(Array)
      slice_tensor(item, start.dup, size.dup)
    else
      item
    end
  end
end

#softmax(arr) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/tensor_stream/evaluator/operation_helpers/array_ops_helper.rb', line 192

def softmax(arr)
  return arr if arr.empty?

  if !arr[0].is_a?(Array)
    c = arr.max
    arr = arr.map { |a| Math.exp(a - c) }
    sum = arr.reduce(:+)
    arr.collect do |input|
      input / sum
    end
  else
    arr.collect { |input| softmax(input) }
  end
end

#softmax_grad(arr) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/tensor_stream/evaluator/operation_helpers/array_ops_helper.rb', line 207

def softmax_grad(arr)
  return arr if arr.empty?
  arr.each_with_index.collect do |input, index|
    if input.is_a?(Array)
      softmax_grad(input)
    else
      arr.each_with_index.collect do |input2, index2|
        if index != index2
          -input * input2
        else
          input * (1.0 - input)
        end
      end
    end
  end
end

#split_tensor(input, begin_index, end_index, axis = 0) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/tensor_stream/evaluator/operation_helpers/array_ops_helper.rb', line 4

def split_tensor(input, begin_index, end_index, axis = 0)
  if axis.zero?
    input[begin_index...end_index]
  else
    input.collect do |item|
      split_tensor(item, begin_index, end_index, axis - 1)
    end
  end
end

#tile_arr(input, dimen, multiples) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/tensor_stream/evaluator/operation_helpers/array_ops_helper.rb', line 152

def tile_arr(input, dimen, multiples)
  t = multiples[dimen]
  if dimen == multiples.size - 1
    return nil if t.zero?
    input * t # ruby array dup
  else
    new_arr = input.collect { |sub|
      tile_arr(sub, dimen + 1, multiples)
    }.compact

    return nil if new_arr.empty?

    new_arr * t
  end
end

#transpose_with_perm(arr, new_arr, shape, new_shape, perm) ⇒ Object

general case transposition with flat arrays



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
# File 'lib/tensor_stream/evaluator/operation_helpers/array_ops_helper.rb', line 235

def transpose_with_perm(arr, new_arr, shape, new_shape, perm)
  arr_size = shape.reduce(:*)
  divisors = shape.dup.drop(1).reverse.inject([1]) { |a, s|
    a << s * a.last
  }.reverse

  multipliers = new_shape.dup.drop(1).reverse.inject([1]) { |a, s|
    a << s * a.last
  }.reverse

  arr_size.times do |p|
    ptr = p
    index = []
    divisors.each_with_object(index) do |div, a|
      a << (ptr / div.to_f).floor
      ptr = ptr % div
    end

    # remap based on perm
    remaped = perm.map { |x| index[x] }

    ptr2 = 0
    multipliers.each_with_index do |m, idx|
      ptr2 += remaped[idx] * m
    end

    new_arr[ptr2] = arr[p]
  end

  [new_arr, new_shape]
end

#truncate(input, target_shape) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/tensor_stream/evaluator/operation_helpers/array_ops_helper.rb', line 33

def truncate(input, target_shape)
  rank = get_rank(input)
  return input if rank.zero?

  start = Array.new(rank) { 0 }
  slice_tensor(input, start, target_shape)
end

#vector_op(vector, vector2, switch = false, safe = true, &block) ⇒ Object

handle 2 tensor math operations



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
# File 'lib/tensor_stream/evaluator/operation_helpers/array_ops_helper.rb', line 103

def vector_op(vector, vector2, switch = false, safe = true, &block)
  if get_rank(vector) < get_rank(vector2) # upgrade rank of A
    duplicated = Array.new(vector2.size) {
      vector
    }
    return vector_op(duplicated, vector2, switch, &block)
  end

  return yield(vector, vector2) unless vector.is_a?(Array)

  vector.each_with_index.collect { |input, index|
    next vector_op(input, vector2, switch, &block) if input.is_a?(Array) && get_rank(vector) > get_rank(vector2)

    if safe && vector2.is_a?(Array)
      next nil if vector2.size != 1 && index >= vector2.size
    end

    z = if vector2.is_a?(Array)
      if index < vector2.size
        vector2[index]
      else
        raise "incompatible tensor shapes used during op" if vector2.size != 1
        vector2[0]
      end
    else
      vector2
    end

    if input.is_a?(Array)
      vector_op(input, z, switch, &block)
    else
      switch ? yield(z, input) : yield(input, z)
    end
  }.compact
end