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) ⇒ 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.
119 120 121 122 123 124 125 126 127 128 |
# File 'lib/cohere/transcribe/runtime/resources.rb', line 119 def initialize @asr_key = nil @asr_ownership = SessionOwnership.new @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.
117 118 119 |
# File 'lib/cohere/transcribe/runtime/resources.rb', line 117 def asr_key @asr_key end |
#batch_controller ⇒ Object (readonly)
Returns the value of attribute batch_controller.
117 118 119 |
# File 'lib/cohere/transcribe/runtime/resources.rb', line 117 def batch_controller @batch_controller end |
Class Method Details
.current_asr_owner ⇒ Object
65 66 67 |
# File 'lib/cohere/transcribe/runtime/resources.rb', line 65 def current_asr_owner OWNER_GUARD.synchronize { current_owner_locked } end |
.evict_current_asr_owner ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/cohere/transcribe/runtime/resources.rb', line 57 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) ⇒ Object
Returns [session, loaded]. A key change evicts this instance's prior session, while acquisition also evicts a different process owner.
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/cohere/transcribe/runtime/resources.rb', line 132 def acquire_asr(key) raise ArgumentError, "an ASR loader block is required" unless block_given? 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) self.class.send(:claim_locked, self) loaded = false unless @asr_ownership.session installed = false session = nil begin session = yield raise TranscriptionRuntimeError, "ASR loader returned no native session" if session.nil? Thread.handle_interrupt(Exception => :never) do @asr_ownership.install(session) @asr_key = owned_key @batch_controller = nil loaded = true installed = true end rescue Exception # rubocop:disable Lint/RescueException -- roll back asynchronous loader interruption begin if @asr_ownership.session.equal?(session) evict_asr_locked else session&.close end rescue Exception # rubocop:disable Lint/RescueException -- preserve the loader failure nil end raise ensure self.class.send(:release_locked, self) unless installed end end [@asr_ownership.session, loaded].freeze end end |
#asr? ⇒ Boolean Also known as: has_asr?
191 192 193 |
# File 'lib/cohere/transcribe/runtime/resources.rb', line 191 def asr? self.class::OWNER_GUARD.synchronize { !@asr_ownership.session.nil? } end |
#asr_circuit_broken? ⇒ Boolean
196 197 198 199 200 |
# File 'lib/cohere/transcribe/runtime/resources.rb', line 196 def asr_circuit_broken? self.class::OWNER_GUARD.synchronize do @batch_controller&.circuit_open? end || false end |
#asr_session ⇒ Object
187 188 189 |
# File 'lib/cohere/transcribe/runtime/resources.rb', line 187 def asr_session self.class::OWNER_GUARD.synchronize { @asr_ownership.session } end |
#close ⇒ Object
207 208 209 210 211 212 213 214 215 216 |
# File 'lib/cohere/transcribe/runtime/resources.rb', line 207 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
218 219 220 |
# File 'lib/cohere/transcribe/runtime/resources.rb', line 218 def closed? self.class::OWNER_GUARD.synchronize { @closed } end |
#evict_asr ⇒ Object
202 203 204 205 |
# File 'lib/cohere/transcribe/runtime/resources.rb', line 202 def evict_asr self.class::OWNER_GUARD.synchronize { evict_asr_locked } nil end |
#install_batch_controller(controller) ⇒ Object
178 179 180 181 182 183 184 185 |
# File 'lib/cohere/transcribe/runtime/resources.rb', line 178 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 |