Class: TensorStream::Evaluator::BaseEvaluator
- Inherits:
-
Object
- Object
- TensorStream::Evaluator::BaseEvaluator
- Defined in:
- lib/tensor_stream/evaluator/base_evaluator.rb
Overview
Evaluator base class
Direct Known Subclasses
Class Method Summary collapse
-
.default_device ⇒ Object
Select the best device available in the system for this evaluator.
-
.fetch_device(_query = []) ⇒ Object
Selects the best device with the specified query, query can be evaluator specific.
-
.ops ⇒ Object
gets all supported ops for this Evaluator class.
-
.query_device(query) ⇒ Object
Select device using uri.
-
.query_supported_devices ⇒ Object
Query all supported devices.
-
.register_op(opcode, options = {}, &block) ⇒ Object
registers an op for the current evaluator class.
Instance Method Summary collapse
-
#initialize(session, _device, thread_pool: nil, log_intermediates: false) ⇒ BaseEvaluator
constructor
A new instance of BaseEvaluator.
- #invoke(tensor, execution_context) ⇒ Object
Constructor Details
#initialize(session, _device, thread_pool: nil, log_intermediates: false) ⇒ BaseEvaluator
Returns a new instance of BaseEvaluator.
23 24 25 26 27 28 |
# File 'lib/tensor_stream/evaluator/base_evaluator.rb', line 23 def initialize(session, _device, thread_pool: nil, log_intermediates: false) @session = session @log_intermediates = log_intermediates @thread_pool = thread_pool || Concurrent::ImmediateExecutor.new @context[:compute_history] = [] if log_intermediates end |
Class Method Details
.default_device ⇒ Object
Select the best device available in the system for this evaluator
38 39 40 |
# File 'lib/tensor_stream/evaluator/base_evaluator.rb', line 38 def self.default_device Device.new('cpu', :cpu, self) end |
.fetch_device(_query = []) ⇒ Object
Selects the best device with the specified query, query can be evaluator specific
45 46 47 |
# File 'lib/tensor_stream/evaluator/base_evaluator.rb', line 45 def self.fetch_device(_query = []) Device.new('cpu', :cpu, self) end |
.ops ⇒ Object
gets all supported ops for this Evaluator class
101 102 103 |
# File 'lib/tensor_stream/evaluator/base_evaluator.rb', line 101 def self.ops @ops ||= {} end |
.query_device(query) ⇒ Object
Select device using uri
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 |
# File 'lib/tensor_stream/evaluator/base_evaluator.rb', line 51 def self.query_device(query) return default_device if query.nil? || query == :default all_devices = query_supported_devices substrs = query.split('/') substrs.each do |q| components = q.split(':') next if components.size.zero? if components[0] == 'device' # use tensorflow convention device_type = components[1] select_index = components[2].to_i devices = all_devices.select { |d| d.type == device_type.downcase.to_sym } return nil if devices.empty? select_index = [devices.size - 1, select_index].min return devices[select_index] elsif %w[cpu gpu].include?(components[0]) device_type = components[0].to_sym select_index = components[1].to_i devices = all_devices.select { |d| d.type == device_type.downcase.to_sym } return nil if devices.empty? select_index = [devices.size - 1, select_index].min return devices[select_index] elsif components[0] == 'ts' # tensorstream specific evaluator_class = TensorStream::Evaluator.evaluators[components[1]][:class] return nil unless self == evaluator_class return evaluator_class.fetch_device(components[2..components.size]) if evaluator_class.respond_to?(:fetch_device) return nil end end end |
.query_supported_devices ⇒ Object
Query all supported devices
32 33 34 |
# File 'lib/tensor_stream/evaluator/base_evaluator.rb', line 32 def self.query_supported_devices [Device.new('cpu', :cpu, self)] end |
.register_op(opcode, options = {}, &block) ⇒ Object
registers an op for the current evaluator class
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/tensor_stream/evaluator/base_evaluator.rb', line 88 def self.register_op(opcode, = {}, &block) @ops ||= {} if opcode.is_a?(Array) opcode.each do |op| @ops[op.to_sym] = { options: , block: block } end else @ops[opcode.to_sym] = { options: , block: block } end end |
Instance Method Details
#invoke(tensor, execution_context) ⇒ Object
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 |
# File 'lib/tensor_stream/evaluator/base_evaluator.rb', line 105 def invoke(tensor, execution_context) return eval_tensor(tensor, execution_context) unless tensor.is_a?(Operation) raise UnsupportedOp.new(tensor), "op #{tensor.operation} is not yet supported" unless self.class.ops.key?(tensor.operation.to_sym) op = self.class.ops[tensor.operation.to_sym] = op[:options] resolved_inputs = tensor.inputs.map do |i| next if i.nil? if i.is_a?(Array) next i.collect { |sub_item| sub_item.is_a?(Tensor) ? invoke(sub_item, execution_context) : sub_item } end if ![:noop] && @context[:_cache][:placement][tensor.name] != @context[:_cache][:placement][i.name] # tensor is on another device or evaluator cache_key = "#{tensor.graph.object_id}_#{i.name}:#{object_id}" next @context[:_cache][cache_key] if @context[:_cache].key?(cache_key) result = @session.delegate_to_evaluator(i, @context, execution_context) convert_from_buffer(i, result).tap do |buffer| @context[:_cache][cache_key] = buffer if i.is_const end else prepare_input(i, execution_context, ) end end instance_exec(execution_context, tensor, resolved_inputs, &op[:block]) end |