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.
51 52 53 54 55 56 |
# File 'lib/cohere/transcribe/runtime/resources.rb', line 51 def initialize @asr_key = nil @asr_session = nil @batch_controller = nil @closed = false end |
Instance Attribute Details
#asr_key ⇒ Object (readonly)
Returns the value of attribute asr_key.
49 50 51 |
# File 'lib/cohere/transcribe/runtime/resources.rb', line 49 def asr_key @asr_key end |
#batch_controller ⇒ Object (readonly)
Returns the value of attribute batch_controller.
49 50 51 |
# File 'lib/cohere/transcribe/runtime/resources.rb', line 49 def batch_controller @batch_controller end |
Class Method Details
.current_asr_owner ⇒ Object
24 25 26 |
# File 'lib/cohere/transcribe/runtime/resources.rb', line 24 def current_asr_owner OWNER_GUARD.synchronize { current_owner_locked } end |
.evict_current_asr_owner ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/cohere/transcribe/runtime/resources.rb', line 16 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.
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 85 86 87 88 89 |
# File 'lib/cohere/transcribe/runtime/resources.rb', line 60 def acquire_asr(key) raise ArgumentError, "an ASR loader block is required" unless block_given? self.class::OWNER_GUARD.synchronize do ensure_open! evict_asr_locked if @asr_session && @asr_key != 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_session installed = false begin session = yield raise TranscriptionRuntimeError, "ASR loader returned no native session" if session.nil? @asr_session = session @asr_key = immutable_key(key) @batch_controller = nil loaded = true installed = true ensure self.class.send(:release_locked, self) unless installed end end [@asr_session, loaded].freeze end end |
#asr? ⇒ Boolean Also known as: has_asr?
104 105 106 |
# File 'lib/cohere/transcribe/runtime/resources.rb', line 104 def asr? self.class::OWNER_GUARD.synchronize { !@asr_session.nil? } end |
#asr_circuit_broken? ⇒ Boolean
109 110 111 112 113 |
# File 'lib/cohere/transcribe/runtime/resources.rb', line 109 def asr_circuit_broken? self.class::OWNER_GUARD.synchronize do @batch_controller&.circuit_open? end || false end |
#asr_session ⇒ Object
100 101 102 |
# File 'lib/cohere/transcribe/runtime/resources.rb', line 100 def asr_session self.class::OWNER_GUARD.synchronize { @asr_session } end |
#close ⇒ Object
120 121 122 123 124 125 126 127 128 |
# File 'lib/cohere/transcribe/runtime/resources.rb', line 120 def close self.class::OWNER_GUARD.synchronize do return if @closed evict_asr_locked @closed = true end nil end |
#closed? ⇒ Boolean
130 131 132 |
# File 'lib/cohere/transcribe/runtime/resources.rb', line 130 def closed? self.class::OWNER_GUARD.synchronize { @closed } end |
#evict_asr ⇒ Object
115 116 117 118 |
# File 'lib/cohere/transcribe/runtime/resources.rb', line 115 def evict_asr self.class::OWNER_GUARD.synchronize { evict_asr_locked } nil end |
#install_batch_controller(controller) ⇒ Object
91 92 93 94 95 96 97 98 |
# File 'lib/cohere/transcribe/runtime/resources.rb', line 91 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_session @batch_controller ||= controller end end |