Class: Cohere::Transcribe::Alignment::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/cohere/transcribe/alignment/aligner.rb

Overview

Lazy ONNX Runtime session with an explicitly reported execution provider. FP16 is admitted only when the CUDA provider is genuinely available; the packaged CPU runtime always uses the full FP32 export.

Constant Summary collapse

CPU_PROVIDER =
"CPUExecutionProvider"
CUDA_PROVIDER =
"CUDAExecutionProvider"
SESSION_OPTIONS =
{
  graph_optimization_level: :all,
  log_severity_level: 4
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dtype: "fp32", device: "cpu", model_provider: ModelProvider.new, session: nil, session_factory: nil, available_providers: nil) ⇒ Session

Returns a new instance of Session.



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/cohere/transcribe/alignment/aligner.rb', line 122

def initialize(dtype: "fp32", device: "cpu", model_provider: ModelProvider.new,
               session: nil, session_factory: nil, available_providers: nil)
  @dtype = dtype
  @device = device
  @model_provider = model_provider
  @session = session
  @session_factory = session_factory
  @available_providers = available_providers
  @provider = session ? CPU_PROVIDER : nil
  @load_seconds = 0.0
  validate_configuration!
end

Instance Attribute Details

#deviceObject (readonly)

Returns the value of attribute device.



120
121
122
# File 'lib/cohere/transcribe/alignment/aligner.rb', line 120

def device
  @device
end

#dtypeObject (readonly)

Returns the value of attribute dtype.



120
121
122
# File 'lib/cohere/transcribe/alignment/aligner.rb', line 120

def dtype
  @dtype
end

#load_secondsObject (readonly)

Returns the value of attribute load_seconds.



120
121
122
# File 'lib/cohere/transcribe/alignment/aligner.rb', line 120

def load_seconds
  @load_seconds
end

#providerObject (readonly)

Returns the value of attribute provider.



120
121
122
# File 'lib/cohere/transcribe/alignment/aligner.rb', line 120

def provider
  @provider
end

Instance Method Details

#closeObject



151
152
153
154
155
# File 'lib/cohere/transcribe/alignment/aligner.rb', line 151

def close
  @session.close if @session.respond_to?(:close)
ensure
  @session = nil
end

#load!Object



146
147
148
149
# File 'lib/cohere/transcribe/alignment/aligner.rb', line 146

def load!
  session
  self
end

#run(input_values) ⇒ Object



135
136
137
138
139
140
141
142
143
144
# File 'lib/cohere/transcribe/alignment/aligner.rb', line 135

def run(input_values)
  values = session.run(["logits"], { "input_values" => input_values }, output_type: :numo)
  raise TranscriptionRuntimeError, "MMS ONNX aligner must return one logits tensor" unless values.is_a?(Array) && values.length == 1

  values.first
rescue BackendUnavailable, TranscriptionRuntimeError
  raise
rescue StandardError => e
  raise BackendUnavailable, "MMS ONNX inference failed: #{e.class}: #{e.message}"
end