Class: Cohere::Transcribe::Safetensors::NativeDTypeConverter

Inherits:
DTypeConverter
  • Object
show all
Defined in:
lib/cohere/transcribe/safetensors.rb

Overview

Uses GGML's vector conversion routines when its base library is available. The Ruby fallback above remains authoritative and testable.

Constant Summary collapse

LIBRARY_NAMES =
%w[libggml-base.so libggml-base.dylib ggml-base.dll].freeze
LIBRARY_PATTERNS =
case RbConfig::CONFIG["host_os"]
when /darwin/
  ["libggml-base.dylib", "libggml-base*.dylib"]
when /mswin|mingw|cygwin/
  ["ggml-base.dll", "libggml-base.dll"]
else
  ["libggml-base.so", "libggml-base.so.*"]
end.freeze
FUNCTIONS =
{
  bf16_to_f32: "ggml_bf16_to_fp32_row",
  f16_to_f32: "ggml_fp16_to_fp32_row",
  f32_to_f16: "ggml_fp32_to_fp16_row",
  f32_to_bf16: "ggml_fp32_to_bf16_row"
}.freeze
PRIVATE_FUNCTIONS =
{
  bf16_to_f32: "crispasr_bf16_to_fp32_row",
  f16_to_f32: "crispasr_fp16_to_fp32_row",
  f32_to_f16: "crispasr_fp32_to_fp16_row",
  f32_to_bf16: "crispasr_fp32_to_bf16_row"
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(library = nil) ⇒ NativeDTypeConverter

Returns a new instance of NativeDTypeConverter.



519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
# File 'lib/cohere/transcribe/safetensors.rb', line 519

def initialize(library = nil)
  super()
  @handle = library ? Fiddle::Handle.new(library) : Fiddle::Handle::DEFAULT
  @functions = FUNCTIONS.to_h do |name, symbol|
    address = begin
      @handle[symbol]
    rescue Fiddle::DLError
      @handle[PRIVATE_FUNCTIONS.fetch(name)]
    end
    function = Fiddle::Function.new(
      address,
      [Fiddle::TYPE_VOIDP, Fiddle::TYPE_VOIDP, Fiddle::TYPE_LONG_LONG],
      Fiddle::TYPE_VOID
    )
    [name, function]
  end.freeze
end

Class Method Details

.auto(library = ENV.fetch("COHERE_TRANSCRIBE_GGML_LIBRARY", nil)) ⇒ Object



459
460
461
462
463
464
465
466
# File 'lib/cohere/transcribe/safetensors.rb', line 459

def auto(library = ENV.fetch("COHERE_TRANSCRIBE_GGML_LIBRARY", nil))
  candidate_libraries(library).each do |candidate|
    return new(candidate)
  rescue Fiddle::DLError, LoadError
    next
  end
  nil
end

.candidate_libraries(library = ENV.fetch("COHERE_TRANSCRIBE_GGML_LIBRARY", nil)) ⇒ Object

Ordered candidates used by the lazy default converter. This keeps dense conversion fast when GGML is packaged beside libcrispasr, while retaining system-library and portable Ruby fallbacks.



471
472
473
474
475
# File 'lib/cohere/transcribe/safetensors.rb', line 471

def candidate_libraries(library = ENV.fetch("COHERE_TRANSCRIBE_GGML_LIBRARY", nil))
  explicit = library.to_s.empty? ? [] : [library]
  native = native_library_candidates.select { |candidate| path_like?(candidate) && File.file?(candidate) }
  [*explicit, nil, *native, *discovered_library_paths, *LIBRARY_NAMES].uniq.freeze
end

Instance Method Details

#convert(bytes, from:, to:) ⇒ Object



537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
# File 'lib/cohere/transcribe/safetensors.rb', line 537

def convert(bytes, from:, to:)
  from = from.to_s.upcase
  to = to.to_s.upcase
  validate_native_input!(bytes, from)
  return bytes.dup if from == to

  case [from, to]
  when %w[BF16 F32] then native_call(:bf16_to_f32, bytes, 4)
  when %w[F16 F32] then native_call(:f16_to_f32, bytes, 4)
  when %w[F32 F16] then native_call(:f32_to_f16, bytes, 2)
  when %w[F32 BF16] then native_call(:f32_to_bf16, bytes, 2)
  when %w[BF16 F16]
    intermediate = native_call(:bf16_to_f32, bytes, 4)
    native_call(:f32_to_f16, intermediate, 2)
  when %w[F16 BF16]
    intermediate = native_call(:f16_to_f32, bytes, 4)
    native_call(:f32_to_bf16, intermediate, 2)
  else
    super
  end
end