Class: TensorStream::OpMaker

Inherits:
Object
  • Object
show all
Defined in:
lib/tensor_stream/op_maker.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(op) ⇒ OpMaker

Returns a new instance of OpMaker.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/tensor_stream/op_maker.rb', line 8

def initialize(op)
  @operation = op
  @parameters = []
  @options = {}
  @gradient = nil
  @supports_broadcast = false
  @data_type_coercion = false
  @exclude = false
  @description = []
  @aliases = []
  @custom = []
  @infer_type_proc = lambda { |tensor|
    next nil if tensor.inputs[0].nil?
    next tensor.inputs[0].shape.shape if tensor.inputs.size == 1

    TensorStream::TensorShape.infer_shape(tensor.inputs[0].shape.shape, tensor.inputs[1].shape.shape) if tensor.inputs.size == 2 && tensor.inputs[0] && tensor.inputs[1]
  }
end

Instance Attribute Details

#aliasesObject (readonly)

Returns the value of attribute aliases.



2
3
4
# File 'lib/tensor_stream/op_maker.rb', line 2

def aliases
  @aliases
end

#check_typesObject (readonly)

Returns the value of attribute check_types.



2
3
4
# File 'lib/tensor_stream/op_maker.rb', line 2

def check_types
  @check_types
end

#customObject (readonly)

Returns the value of attribute custom.



2
3
4
# File 'lib/tensor_stream/op_maker.rb', line 2

def custom
  @custom
end

#data_type_blockObject (readonly)

Returns the value of attribute data_type_block.



2
3
4
# File 'lib/tensor_stream/op_maker.rb', line 2

def data_type_block
  @data_type_block
end

#data_type_coercionObject (readonly)

Returns the value of attribute data_type_coercion.



2
3
4
# File 'lib/tensor_stream/op_maker.rb', line 2

def data_type_coercion
  @data_type_coercion
end

#descriptionObject (readonly)

Returns the value of attribute description.



2
3
4
# File 'lib/tensor_stream/op_maker.rb', line 2

def description
  @description
end

#excludeObject (readonly)

Returns the value of attribute exclude.



2
3
4
# File 'lib/tensor_stream/op_maker.rb', line 2

def exclude
  @exclude
end

#gradientObject (readonly)

Returns the value of attribute gradient.



2
3
4
# File 'lib/tensor_stream/op_maker.rb', line 2

def gradient
  @gradient
end

#infer_type_procObject (readonly)

Returns the value of attribute infer_type_proc.



2
3
4
# File 'lib/tensor_stream/op_maker.rb', line 2

def infer_type_proc
  @infer_type_proc
end

#operationObject (readonly)

Returns the value of attribute operation.



2
3
4
# File 'lib/tensor_stream/op_maker.rb', line 2

def operation
  @operation
end

#optionsObject (readonly)

Returns the value of attribute options.



2
3
4
# File 'lib/tensor_stream/op_maker.rb', line 2

def options
  @options
end

#parametersObject (readonly)

Returns the value of attribute parameters.



2
3
4
# File 'lib/tensor_stream/op_maker.rb', line 2

def parameters
  @parameters
end

#supports_broadcastObject (readonly)

Returns the value of attribute supports_broadcast.



2
3
4
# File 'lib/tensor_stream/op_maker.rb', line 2

def supports_broadcast
  @supports_broadcast
end

Class Method Details

.define_operation(op_code, &block) ⇒ Object



42
43
44
45
46
47
# File 'lib/tensor_stream/op_maker.rb', line 42

def self.define_operation(op_code, &block)
  @ops ||= {}
  op_maker = TensorStream::OpMaker.new(op_code.to_sym)
  block.call(op_maker)
  @ops[op_code.to_sym] = op_maker
end

.each_op(&block) ⇒ Object



78
79
80
81
82
# File 'lib/tensor_stream/op_maker.rb', line 78

def self.each_op(&block)
  @ops.values.sort_by { |op| op.operation }.reject(&:exclude).each do |op|
    block.call(op)
  end
end

.gradient_op(context_caller, node, grad) ⇒ Object

call an operations’ gradient definition



50
51
52
53
54
# File 'lib/tensor_stream/op_maker.rb', line 50

def self.gradient_op(context_caller, node, grad)
  raise "No derivative op defined for #{node.operation}" if @ops[node.operation].nil? || @ops[node.operation].gradient.nil?

  context_caller.instance_exec(grad, node, node.inputs, &@ops[node.operation].gradient)
end

.infer_data_type(context_caller, tensor, passed_data_type) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/tensor_stream/op_maker.rb', line 62

def self.infer_data_type(context_caller, tensor, passed_data_type)
  return passed_data_type if passed_data_type

  if @ops[tensor.operation] && @ops[tensor.operation].data_type_block
    context_caller.instance_exec(tensor, &@ops[tensor.operation].data_type_block)
  else
    if tensor.inputs[0]
      tensor.inputs[0].data_type
    elsif tensor.inputs[1]
      tensor.inputs[1].data_type
    else
      :unknown
    end
  end
end

.infer_shape(context_caller, tensor) ⇒ Object



56
57
58
59
60
# File 'lib/tensor_stream/op_maker.rb', line 56

def self.infer_shape(context_caller, tensor)
  return nil unless @ops[tensor.operation]

  context_caller.instance_exec(tensor, &@ops[tensor.operation].infer_type_proc)
end

.scanObject



35
36
37
38
39
40
# File 'lib/tensor_stream/op_maker.rb', line 35

def self.scan
  op_files = Dir[File.join(File.dirname(__FILE__), "ops", "*.rb")]
  op_files.each { |file|
    load File.join("tensor_stream", "ops", File.basename(file))
  }
end

Instance Method Details

#add_custom(custom_code) ⇒ Object



31
32
33
# File 'lib/tensor_stream/op_maker.rb', line 31

def add_custom(custom_code)
  @custom << custom_code
end

#apply_data_type_coercion!Object



156
157
158
# File 'lib/tensor_stream/op_maker.rb', line 156

def apply_data_type_coercion!
  @data_type_coercion = true
end

#check_types?Boolean

Returns:

  • (Boolean)


176
177
178
# File 'lib/tensor_stream/op_maker.rb', line 176

def check_types?
  @check_types
end

#data_type_coercion?Boolean

Returns:

  • (Boolean)


172
173
174
# File 'lib/tensor_stream/op_maker.rb', line 172

def data_type_coercion?
  @data_type_coercion
end

#default_with_nil(v) ⇒ Object



196
197
198
# File 'lib/tensor_stream/op_maker.rb', line 196

def default_with_nil(v)
  v == :nil ? 'nil' : v
end

#define_data_type(&block) ⇒ Object



142
143
144
# File 'lib/tensor_stream/op_maker.rb', line 142

def define_data_type(&block)
  @data_type_block = block
end

#define_gradient(&block) ⇒ Object



134
135
136
# File 'lib/tensor_stream/op_maker.rb', line 134

def define_gradient(&block)
  @gradient = block
end

#define_shape(&block) ⇒ Object



138
139
140
# File 'lib/tensor_stream/op_maker.rb', line 138

def define_shape(&block)
  @infer_type_proc = block
end

#description_linesObject



96
97
98
# File 'lib/tensor_stream/op_maker.rb', line 96

def description_lines
  description.map { |line| line.split("\n") }.flatten
end

#exclude!Object



92
93
94
# File 'lib/tensor_stream/op_maker.rb', line 92

def exclude!
  @exclude = true
end

#expand_options(print_defaults) ⇒ Object



180
181
182
183
184
# File 'lib/tensor_stream/op_maker.rb', line 180

def expand_options(print_defaults)
  @options.map { |k, v|
    print_defaults && v[:default_value] ? "#{k}: #{default_with_nil(v[:default_value])}" : "#{k}:"
  }
end

#expand_params(print_defaults) ⇒ Object



146
147
148
149
150
# File 'lib/tensor_stream/op_maker.rb', line 146

def expand_params(print_defaults)
  @parameters.map { |param|
    print_defaults && param[:default_value] ? "#{param[:name]} = #{default_with_nil(param[:default_value])}" : "#{param[:name]}"
  }
end

#generate_bodyObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/tensor_stream/op_maker.rb', line 100

def generate_body
  body = []
  parameters.select { |p| p[:validate] }.each do |p|
    body << "check_allowed_types(#{p[:name]}, TensorStream::Ops::#{p[:validate]})"
  end
  if data_type_coercion?
    body << "#{expand_params(false).join(', ')} = apply_data_type_coercion(#{expand_params(false).join(', ')})"
  end
  if check_types?
    body << "check_data_types(#{expand_params(false).join(', ')})"
  end
  custom.each do |c|
    body << c
  end
  body << "_op(:#{operation}, #{(expand_params(false) + options_call).join(', ')})"
  body.map { |line| "      #{line}"}.join("\n")
end

#option(name, description, default_value = nil, options = {}) ⇒ Object



130
131
132
# File 'lib/tensor_stream/op_maker.rb', line 130

def option(name, description, default_value = nil, options = {})
  @options[name] = { description: description, default_value: default_value, options: options }
end

#options_callObject



186
187
188
189
190
191
192
193
194
# File 'lib/tensor_stream/op_maker.rb', line 186

def options_call
  @options.map { |k, v|
    if v.dig(:options, :alias)
      "#{v.dig(:options, :alias)}: #{k}"
    else
      "#{k}: #{k}"
    end
  }
end

#other_names(aliases) ⇒ Object



27
28
29
# File 'lib/tensor_stream/op_maker.rb', line 27

def other_names(aliases)
  @aliases += aliases
end

#parameter(name, description, default_value = nil, validate: nil) ⇒ Object

adds a parameter to the op



121
122
123
124
125
126
127
128
# File 'lib/tensor_stream/op_maker.rb', line 121

def parameter(name, description, default_value = nil, validate: nil)
  @parameters << {
    name: name.to_s,
    description: description,
    default_value: default_value,
    validate: validate
  }
end

#parameters_must_have_same_data_type!Object



152
153
154
# File 'lib/tensor_stream/op_maker.rb', line 152

def parameters_must_have_same_data_type!
  @check_types = true
end

#supports_broadcasting!Object



160
161
162
163
164
165
166
# File 'lib/tensor_stream/op_maker.rb', line 160

def supports_broadcasting!
  if (@parameters.size> 1)
    @supports_broadcast = true
  else
    raise "Ops with parameters < 2 cannot support broadcasting"
  end
end

#supports_broadcasting?Boolean

Returns:

  • (Boolean)


168
169
170
# File 'lib/tensor_stream/op_maker.rb', line 168

def supports_broadcasting?
  @supports_broadcast
end

#what_it_does(description) ⇒ Object



84
85
86
# File 'lib/tensor_stream/op_maker.rb', line 84

def what_it_does(description)
  @description << description
end

#what_it_does_code(description) ⇒ Object



88
89
90
# File 'lib/tensor_stream/op_maker.rb', line 88

def what_it_does_code(description)
  @description << "<tt>#{description}</tt>"
end