Class: TensorStream::Session
- Inherits:
-
Object
- Object
- TensorStream::Session
show all
- Includes:
- StringHelper
- Defined in:
- lib/tensor_stream/session.rb
Overview
TensorStream class that defines a session
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#camelize, #constantize, #symbolize_keys, #underscore
Constructor Details
#initialize(evaluator = nil, thread_pool_class: Concurrent::ImmediateExecutor, log_device_placement: false, profile_enabled: false, evaluator_options: {}) ⇒ Session
Returns a new instance of Session.
9
10
11
12
13
14
15
16
17
18
|
# File 'lib/tensor_stream/session.rb', line 9
def initialize(evaluator = nil, thread_pool_class: Concurrent::ImmediateExecutor, log_device_placement: false, profile_enabled: false, evaluator_options: {})
@thread_pool = thread_pool_class.new
@closed = false
@session_cache = {}
@randomizer = {}
@log_device_placement = log_device_placement
@evaluator_options = evaluator_options.merge(profile_enabled: profile_enabled)
get_evaluator_classes(evaluator)
@evaluators = {}
end
|
Instance Attribute Details
#closed ⇒ Object
Returns the value of attribute closed.
6
7
8
|
# File 'lib/tensor_stream/session.rb', line 6
def closed
@closed
end
|
#last_session_context ⇒ Object
Returns the value of attribute last_session_context.
6
7
8
|
# File 'lib/tensor_stream/session.rb', line 6
def last_session_context
@last_session_context
end
|
#randomizer ⇒ Object
Returns the value of attribute randomizer.
7
8
9
|
# File 'lib/tensor_stream/session.rb', line 7
def randomizer
@randomizer
end
|
#session_cache ⇒ Object
Returns the value of attribute session_cache.
6
7
8
|
# File 'lib/tensor_stream/session.rb', line 6
def session_cache
@session_cache
end
|
#target ⇒ Object
Returns the value of attribute target.
6
7
8
|
# File 'lib/tensor_stream/session.rb', line 6
def target
@target
end
|
Class Method Details
.default_session ⇒ Object
28
29
30
|
# File 'lib/tensor_stream/session.rb', line 28
def self.default_session
@session ||= Session.new
end
|
Instance Method Details
#assign_evaluator(tensor) ⇒ Object
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
# File 'lib/tensor_stream/session.rb', line 137
def assign_evaluator(tensor)
device = @evaluator_classes.map { |klass|
next nil if tensor.is_a?(Operation) && !klass.ops.include?(tensor.operation.to_sym)
next klass.default_device if tensor.device.nil?
klass.query_device(tensor.device)
}.compact.first
raise "no evaluator available to execute #{tensor.operation}" if device.nil?
key = "#{device.evaluator}/#{device.name}"
if @evaluators.key?(key)
@evaluators[key]
else
@evaluators[key] = [device, device.evaluator.new(self, device)]
end
end
|
#clear_session_cache ⇒ Object
24
25
26
|
# File 'lib/tensor_stream/session.rb', line 24
def clear_session_cache
@session_cache = {}
end
|
#close ⇒ Object
91
92
93
94
95
96
|
# File 'lib/tensor_stream/session.rb', line 91
def close
@last_session_context = nil
@session_cache = {}
@closed = true
end
|
#closed? ⇒ Boolean
98
99
100
|
# File 'lib/tensor_stream/session.rb', line 98
def closed?
@closed
end
|
#delegate_to_evaluator(tensor_arr, session_context, context) ⇒ Object
123
124
125
126
127
128
129
130
131
132
133
134
135
|
# File 'lib/tensor_stream/session.rb', line 123
def delegate_to_evaluator(tensor_arr, session_context, context)
if tensor_arr.is_a?(Array)
tensor_arr.collect do |tensor|
if tensor.is_a?(Array)
delegate_to_evaluator(tensor, session_context, context)
else
run_with_session_context(tensor, session_context, context)
end
end
else
run_with_session_context(tensor_arr.op, session_context, context)
end
end
|
#dump_internal_ops(tensor) ⇒ Object
102
103
104
|
# File 'lib/tensor_stream/session.rb', line 102
def dump_internal_ops(tensor)
dump_ops(tensor, ->(_k, n) { n.is_a?(Tensor) && n.internal? })
end
|
#dump_ops(tensor, selector) ⇒ Object
110
111
112
113
114
115
116
117
|
# File 'lib/tensor_stream/session.rb', line 110
def dump_ops(tensor, selector)
graph = tensor.graph
graph.nodes.select { |k, v| selector.call(k, v) }.collect { |k, node|
next unless @last_session_context[node.name]
"#{k} #{node.to_math(true, 1)} = #{@last_session_context[node.name]}"
}.compact
end
|
#dump_user_ops(tensor) ⇒ Object
106
107
108
|
# File 'lib/tensor_stream/session.rb', line 106
def dump_user_ops(tensor)
dump_ops(tensor, ->(_k, n) { n.is_a?(Tensor) && !n.internal? })
end
|
#get_evaluator_classes(evaluators) ⇒ Object
#graph_ml(tensor, filename) ⇒ Object
#list_devices ⇒ Object
83
84
85
86
87
88
89
|
# File 'lib/tensor_stream/session.rb', line 83
def list_devices
TensorStream::Evaluator.evaluators.collect { |_k, v|
v[:class].query_supported_devices.collect do |device|
device
end
}.flatten
end
|
#run(*args) ⇒ Object
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
|
# File 'lib/tensor_stream/session.rb', line 32
def run(*args)
options = if args.last.is_a?(Hash)
args.pop
else
{}
end
@evaluator_options[:thread_pool] = @thread_pool
@evaluator_options[:log_intermediates] = options[:log_intermediates]
context = {
_cache: @session_cache,
_options: options.merge(@evaluator_options),
profile: {step: 0, operations: {}},
}
options[:feed_dict]&.each_key do |k|
if k.is_a?(Placeholder)
ph = options[:feed_dict][k]
context[k.name.to_sym] = ph.is_a?(Tensor) ? ph.op : ph
elsif k.is_a?(String)
target_graph = args[0].graph
node = target_graph.get_node(k)
raise "Cannot find placeholder with the name of #{k}" if node.operation != :placeholder
context[k.to_sym] = options[:feed_dict][k]
elsif k.is_a?(Operation) && k.operation == :placeholder
context[k.name.to_sym] = options[:feed_dict][k]
else
raise "Invalid placeholder type passed key must be a string or a placeholder type"
end
end
args.each { |t| prepare_evaluators(t, context) }
@last_session_context = context
if @log_device_placement
context[:_cache][:placement].each do |k, v|
puts "#{k} : #{v[0].name}"
end
end
result = args.collect { |e|
next e.value if e.is_a?(Tensor) && e.is_const && e.value
value = delegate_to_evaluator(e, context, {})
recursive_eval(value)
}
args.size == 1 ? result.first : result
end
|