Class: TensorStream::Evaluator::OpenclEvaluator

Inherits:
BaseEvaluator show all
Includes:
ArrayOpsHelper, MathHelper, OpHelper
Defined in:
lib/tensor_stream/evaluator/opencl/opencl_evaluator.rb

Overview

PURE ruby evaluator used for testing and development

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MathHelper

#sigmoid

Methods included from ArrayOpsHelper

#broadcast, #broadcast_dimensions, #get_rank, #process_function_op, #reduced_shape, #shape_diff, #slice_tensor, #softmax, #softmax_grad, #tile_arr, #truncate, #vector_op

Methods included from OpHelper

#_op, #cons, #dtype_eval, #format_source, #fp_type?, #i_cons, #i_op, #shape_eval, #val_to_dtype

Methods inherited from BaseEvaluator

#invoke, ops, query_device, register_op

Constructor Details

#initialize(session, device, thread_pool: nil, log_intermediates: false) ⇒ OpenclEvaluator

Returns a new instance of OpenclEvaluator.



38
39
40
41
42
43
# File 'lib/tensor_stream/evaluator/opencl/opencl_evaluator.rb', line 38

def initialize(session, device, thread_pool: nil, log_intermediates: false)
  super
  _create_opencl_context(device.native_device)
  @opencl_device = device.native_device
  create_command_queue
end

Instance Attribute Details

#retainObject

Returns the value of attribute retain.



32
33
34
# File 'lib/tensor_stream/evaluator/opencl/opencl_evaluator.rb', line 32

def retain
  @retain
end

Class Method Details

.default_deviceObject

Select the best device available in the system for this evaluator



73
74
75
76
77
# File 'lib/tensor_stream/evaluator/opencl/opencl_evaluator.rb', line 73

def self.default_device
  devices = OpenclEvaluator.query_devices_with_score
  device = devices.sort { |a| a[1] }.reverse.first
  opencl_to_device(device)
end

.fetch_device(query = []) ⇒ Object



52
53
54
55
56
# File 'lib/tensor_stream/evaluator/opencl/opencl_evaluator.rb', line 52

def self.fetch_device(query = [])
  devices = query_devices_with_score
  platform_devices = devices.select { |d| d[0].platform.to_s.gsub(' ','_').downcase =~ /#{query[0].downcase}/ }
  opencl_to_device(platform_devices[[query[1].to_i, platform_devices.size - 1].min])
end

.opencl_to_device(d) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/tensor_stream/evaluator/opencl/opencl_evaluator.rb', line 58

def self.opencl_to_device(d)
  device = d[0]
  index = d[3]
  platform_name = device.platform.name.gsub(' ', '_').downcase
  uri = [platform_name, index].join(':')

  device_type = device.type.to_s == 'GPU' ? :gpu : :cpu

  OpenclDevice.new(uri, device_type, self).tap do |d|
    d.native_device = device
  end
end

.query_supported_devicesObject



45
46
47
48
49
50
# File 'lib/tensor_stream/evaluator/opencl/opencl_evaluator.rb', line 45

def self.query_supported_devices
  devices = query_devices_with_score
  devices.sort { |a| a[1] }.reverse.map do |d|
    opencl_to_device(d)
  end
end

Instance Method Details

#complete_eval(tensor, context) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/tensor_stream/evaluator/opencl/opencl_evaluator.rb', line 103

def complete_eval(tensor, context)
  buffer = _run(tensor, context)
  if buffer.is_a?(Array)
    buffer = buffer.collect do |b|
      next b if b.buffer.size.zero?
      _opencl_queue.enqueue_read_buffer(b.cl_buffer, b.buffer, event_wait_list: [b.op].compact)
      b
    end
  else
    return buffer if buffer.nil? || buffer.buffer.size.zero?
    _opencl_queue.enqueue_read_buffer(buffer.cl_buffer, buffer.buffer, event_wait_list: [buffer.op].compact)
  end
  _opencl_queue.finish
  buffer
end

#convert_from_buffer(tensor, result) ⇒ Object



99
100
101
# File 'lib/tensor_stream/evaluator/opencl/opencl_evaluator.rb', line 99

def convert_from_buffer(tensor, result)
  convert_to_opencl([result.buffer].flatten, shape_eval(result.buffer), data_type: result.data_type, name: tensor.name)
end

#opencl_deviceObject



119
120
121
# File 'lib/tensor_stream/evaluator/opencl/opencl_evaluator.rb', line 119

def opencl_device
  @opencl_device
end

#run(tensor, execution_context) ⇒ Object

opencl evaluator main entrypoint



80
81
82
# File 'lib/tensor_stream/evaluator/opencl/opencl_evaluator.rb', line 80

def run(tensor, execution_context)
  read_final_result(complete_eval(tensor, execution_context))
end

#run_with_buffer(tensor, context, execution_context) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/tensor_stream/evaluator/opencl/opencl_evaluator.rb', line 84

def run_with_buffer(tensor, context, execution_context)
  @context = context
  @context[:_cache][:_cl_buffers] ||= {} if context[:_cache]

  if tensor.is_a?(Array)
    tensor.collect do |t|
      value = run(t, execution_context)
      Buffer.new(data_type: t.data_type, buffer: value)
    end
  else
    value = run(tensor, execution_context)
    Buffer.new(data_type: tensor.data_type, buffer: value)
  end
end