Class: Cohere::Transcribe::Runtime::ModelResources
- Inherits:
-
Object
- Object
- Cohere::Transcribe::Runtime::ModelResources
- Defined in:
- lib/cohere/transcribe/runtime/resources.rb
Overview
Owns one reusable native ASR session. Across all ModelResources instances, only one may retain ASR at a time; this bounds process memory when applications create multiple reusable Transcribers.
Constant Summary collapse
- OWNER_GUARD =
Monitor.new
Instance Attribute Summary collapse
-
#asr_key ⇒ Object
readonly
Returns the value of attribute asr_key.
-
#batch_controller ⇒ Object
readonly
Returns the value of attribute batch_controller.
Class Method Summary collapse
Instance Method Summary collapse
-
#acquire_asr(key, &loader) ⇒ Object
Returns [session, loaded].
- #asr? ⇒ Boolean (also: #has_asr?)
- #asr_circuit_broken? ⇒ Boolean
- #asr_session ⇒ Object
- #close ⇒ Object
- #closed? ⇒ Boolean
- #evict_asr ⇒ Object
-
#initialize ⇒ ModelResources
constructor
A new instance of ModelResources.
- #install_batch_controller(controller) ⇒ Object
Constructor Details
#initialize ⇒ ModelResources
Returns a new instance of ModelResources.
80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/cohere/transcribe/runtime/resources.rb', line 80 def initialize @asr_key = nil @asr_ownership = Internal::SessionOwnership.new( installed_error: "ASR session ownership is already installed" ) @batch_controller = nil @closed = false ObjectSpace.define_finalizer( self, self.class.send(:finalizer_for, @asr_ownership) ) end |
Instance Attribute Details
#asr_key ⇒ Object (readonly)
Returns the value of attribute asr_key.
78 79 80 |
# File 'lib/cohere/transcribe/runtime/resources.rb', line 78 def asr_key @asr_key end |
#batch_controller ⇒ Object (readonly)
Returns the value of attribute batch_controller.
78 79 80 |
# File 'lib/cohere/transcribe/runtime/resources.rb', line 78 def batch_controller @batch_controller end |
Class Method Details
.current_asr_owner ⇒ Object
26 27 28 |
# File 'lib/cohere/transcribe/runtime/resources.rb', line 26 def current_asr_owner OWNER_GUARD.synchronize { current_owner_locked } end |
.evict_current_asr_owner ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/cohere/transcribe/runtime/resources.rb', line 18 def evict_current_asr_owner OWNER_GUARD.synchronize do owner = current_owner_locked owner&.send(:evict_asr_locked) end nil end |
Instance Method Details
#acquire_asr(key, &loader) ⇒ Object
Returns [session, loaded]. A key change evicts this instance's prior session, while acquisition also evicts a different process owner.
95 96 97 98 99 100 101 102 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 138 139 140 141 142 |
# File 'lib/cohere/transcribe/runtime/resources.rb', line 95 def acquire_asr(key, &loader) raise ArgumentError, "an ASR loader block is required" unless loader self.class::OWNER_GUARD.synchronize do ensure_open! owned_key = immutable_key(key) evict_asr_locked if @asr_ownership.session && @asr_key != owned_key owner = self.class.send(:current_owner_locked) owner.send(:evict_asr_locked) if owner && !owner.equal?(self) loaded = false if @asr_ownership.session self.class.send(:claim_locked, self) else completed = false session = nil begin Thread.handle_interrupt(Object => :never) do self.class.send(:claim_locked, self) session = Thread.handle_interrupt(Object => :on_blocking, &loader) raise TranscriptionRuntimeError, "ASR loader returned no native session" if session.nil? @asr_ownership.install(session) @asr_key = owned_key @batch_controller = nil loaded = true end completed = true ensure unless completed Thread.handle_interrupt(Object => :never) do if @asr_ownership.session.equal?(session) evict_asr_locked else session&.close end rescue Exception # rubocop:disable Lint/RescueException -- preserve loader termination nil ensure self.class.send(:release_locked, self) end end end end [@asr_ownership.session, loaded].freeze end end |
#asr? ⇒ Boolean Also known as: has_asr?
157 158 159 |
# File 'lib/cohere/transcribe/runtime/resources.rb', line 157 def asr? self.class::OWNER_GUARD.synchronize { !@asr_ownership.session.nil? } end |
#asr_circuit_broken? ⇒ Boolean
162 163 164 165 166 |
# File 'lib/cohere/transcribe/runtime/resources.rb', line 162 def asr_circuit_broken? self.class::OWNER_GUARD.synchronize do @batch_controller&.circuit_open? end || false end |
#asr_session ⇒ Object
153 154 155 |
# File 'lib/cohere/transcribe/runtime/resources.rb', line 153 def asr_session self.class::OWNER_GUARD.synchronize { @asr_ownership.session } end |
#close ⇒ Object
173 174 175 176 177 178 179 180 181 182 |
# File 'lib/cohere/transcribe/runtime/resources.rb', line 173 def close self.class::OWNER_GUARD.synchronize do return if @closed evict_asr_locked @closed = true ObjectSpace.undefine_finalizer(self) end nil end |
#closed? ⇒ Boolean
184 185 186 |
# File 'lib/cohere/transcribe/runtime/resources.rb', line 184 def closed? self.class::OWNER_GUARD.synchronize { @closed } end |
#evict_asr ⇒ Object
168 169 170 171 |
# File 'lib/cohere/transcribe/runtime/resources.rb', line 168 def evict_asr self.class::OWNER_GUARD.synchronize { evict_asr_locked } nil end |
#install_batch_controller(controller) ⇒ Object
144 145 146 147 148 149 150 151 |
# File 'lib/cohere/transcribe/runtime/resources.rb', line 144 def install_batch_controller(controller) self.class::OWNER_GUARD.synchronize do ensure_open! raise TranscriptionRuntimeError, "Cannot install an ASR controller before acquiring ASR" unless @asr_ownership.session @batch_controller ||= controller end end |