Class: Toy::Core::GGUFMeta

Inherits:
Object
  • Object
show all
Defined in:
lib/toy/core/gguf_meta.rb

Overview

Parsed view of a GGUF file’s metadata (no tensor data read).

meta = GGUFMeta.read("model.gguf")
meta.kv["llama.embedding_length"]   # => 576
meta.tensor?("output.weight")       # => false (tied)

Defined Under Namespace

Classes: ParseError

Constant Summary collapse

T_UINT8 =

GGUF metadata value type tags (gguf.h: enum gguf_metadata_value_type).

0
T_INT8 =
1
T_UINT16 =
2
T_INT16 =
3
T_UINT32 =
4
T_INT32 =
5
T_FLOAT32 =
6
T_BOOL =
7
T_STRING =
8
T_ARRAY =
9
T_UINT64 =
10
T_INT64 =
11
T_FLOAT64 =
12

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, path) ⇒ GGUFMeta

Returns a new instance of GGUFMeta.



50
51
52
53
54
55
# File 'lib/toy/core/gguf_meta.rb', line 50

def initialize(io, path)
  @io = io
  @path = path
  @kv = {}
  @tensor_names = []
end

Instance Attribute Details

#kvObject (readonly)

Returns the value of attribute kv.



44
45
46
# File 'lib/toy/core/gguf_meta.rb', line 44

def kv
  @kv
end

#n_tensorsObject (readonly)

Returns the value of attribute n_tensors.



44
45
46
# File 'lib/toy/core/gguf_meta.rb', line 44

def n_tensors
  @n_tensors
end

#tensor_namesObject (readonly)

Returns the value of attribute tensor_names.



44
45
46
# File 'lib/toy/core/gguf_meta.rb', line 44

def tensor_names
  @tensor_names
end

#versionObject (readonly)

Returns the value of attribute version.



44
45
46
# File 'lib/toy/core/gguf_meta.rb', line 44

def version
  @version
end

Class Method Details

.read(path) ⇒ Object



46
47
48
# File 'lib/toy/core/gguf_meta.rb', line 46

def self.read(path)
  File.open(path, "rb") { |io| new(io, path).parse }
end

Instance Method Details

#parseObject



57
58
59
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
# File 'lib/toy/core/gguf_meta.rb', line 57

def parse
  magic = read_bytes(4)
  unless magic == "GGUF"
    raise ParseError, "#{@path}: not a GGUF file (magic=#{magic.inspect})"
  end
  @version = read_u32
  unless @version == 3
    raise ParseError, "#{@path}: unsupported GGUF version #{@version} (only 3)"
  end
  @n_tensors = read_u64
  n_kv = read_u64

  n_kv.times do
    key = read_gguf_string
    @kv[key] = read_value
  end

  @n_tensors.times do
    name = read_gguf_string
    @tensor_names << name
    n_dims = read_u32
    n_dims.times { read_u64 }  # dims
    read_u32                   # ggml type
    read_u64                   # offset
  end

  self
end

#tensor?(name) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/toy/core/gguf_meta.rb', line 86

def tensor?(name)
  @tensor_names.include?(name)
end