-
Notifications
You must be signed in to change notification settings - Fork 0
/
blob.lua
192 lines (171 loc) · 4.82 KB
/
blob.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
local Blob = {}
Blob.__index = Blob
local ffi = require("ffi")
ffi.cdef[[
void *malloc( size_t size );
void *memcpy(void *dest, const void *src, size_t n);
]]
function Blob.SetFloat(index, val)
error("NYI")
end
function Blob.GetFloat(index)
error("NYI", 2)
end
function Blob:CopyTo(thisOffset, that, thatOffset, size)
if self.type == "F32" and that.type == "F32" then
ffi.C.memcpy(that.blob + thatOffset, self.blob + thisOffset, size * self.byte_stride)
else
for i = thatOffset, thatOffset + size - 1 do
that.SetFloat(i, self.GetFloat(i - thatOffset + thisOffset))
end
end
end
function Blob:Fill(thisOffset, size, identity)
if self.type == "F32" then
if identity == 0 then
ffi.fill(self.blob + thisOffset, size * self.byte_stride, 0)
else
for i = thisOffset, thisOffset + size - 1 do
self.SetFloat(i, identity)
end
end
else
error("NYI", 2)
end
return self
end
function Blob:F32(size, blob)
local stride = 4
blob = ffi.cast("float*", blob or ffi.cast("float*", ffi.C.malloc(size * stride)))
return setmetatable(
{
type = "F32",
blob = blob,
size = tonumber(size),
byte_size = tonumber(size * stride),
byte_stride = stride,
SetFloat = function(index, val)
blob[index] = val
end,
GetFloat = function(index)
return blob[index]
end,
},
Blob
)-- :Fill(0, size, 0)
end
function Blob:F64(size, blob)
local stride = ffi.sizeof("double")
blob = ffi.cast("double*", blob or ffi.cast("double*", ffi.C.malloc(size * stride)))
return setmetatable(
{
type = "F64",
blob = blob,
size = tonumber(size),
byte_size = tonumber(size * stride),
byte_stride = stride,
SetFloat = function(index, val)
blob[index] = val
end,
GetFloat = function(index)
return blob[index]
end,
},
Blob
)-- :Fill(0, size, 0)
end
do
local ggf = require("gguf")
local block_size = ggf.GGMLTypeMap.Q4_0.block_size
local half_block_size = block_size / 2
local type_size = ggf.GGMLTypeMap.Q4_0.type_size
local half_type_size = type_size / 2
local rshift = bit.rshift
local band = bit.band
local ldexp = math.ldexp
local function f16_to_f32(buf)
return ldexp(buf.mantissa + 1024, buf.exponent - 25) * (1 - buf.sign * 2)
end
local function f16_to_f32(bits)
local sign = 1 - band(rshift(bits, 15), 0x1) * 2
local exponent = band(rshift(bits, 10), 0x1F)
local mantissa = band(bits, 0x3FF)
return sign * ldexp(mantissa + 1024, exponent - 25)
end
local cached_f16_to_f32 = ffi.new("float[65536]")
for i = 0, 65536 - 1 do
cached_f16_to_f32[i] = f16_to_f32(i)
end
function Blob:Q4_0(size, blob)
local byte_size = size * type_size
blob = ffi.cast("uint8_t*", blob or ffi.cast("uint8_t*", ffi.C.malloc(byte_size)))
local blob_f16 = ffi.cast("uint16_t*", blob)
assert(byte_size % block_size == 0, "Total size must be a multiple of the block size")
byte_size = byte_size / block_size
local floats = ffi.typeof("float[32]")
local f = floats()
return setmetatable(
{
type = "Q4_0",
blob = blob,
size = tonumber(size),
byte_size = tonumber(byte_size),
byte_stride = 1,
blob_f16 = blob_f16,
cached_f16_to_f32 = cached_f16_to_f32,
half_type_size = half_type_size,
type_size = type_size,
half_block_size = half_block_size,
GetFloat = function(index)
local block_index = rshift(index, 5)
local block_offset = block_index * type_size
local scale = cached_f16_to_f32[blob_f16[block_index * half_type_size]]
local modIndex = band(index, block_size - 1)
local base_offset = block_offset + band(modIndex, half_block_size - 1)
local shift_amount = rshift(modIndex, 4) * 4
local quant = band(rshift(blob[2 + base_offset], shift_amount), 0x0F)
return (quant - 8) * scale
end,
Get32FloatsFromBlockIndex = function(block_index)
local scale = cached_f16_to_f32[blob_f16[block_index * half_type_size]]
local block_offset = block_index * type_size
for modIndex = 0, 16 - 1 do
local byte = blob[block_offset + band(modIndex, half_block_size - 1) + 2]
f[modIndex] = (band(byte, 0x0F) - 8) * scale
f[modIndex + 16] = (band(rshift(byte, 4), 0x0F) - 8) * scale
end
return f
end,
},
Blob
)-- :Fill(0, size, 0)
end
end
do
local ctype = ffi.typeof([[
struct {
int size;
int type;
void *blob;
}
]])
local ctype_ptr = ffi.typeof("$*", ctype)
local ctype_box = ffi.typeof("$[1]", ctype)
local type_map = {
F32 = 0,
Q4_0 = 1,
}
-- double lookup
for i, str in pairs(type_map) do
type_map[str] = i
end
function Blob:ThreadSerialize()
return ctype(self.size, type_map[self.type], ffi.cast("void *", self.blob))
end
function Blob:ThreadDeserialize(ptr)
local data = ffi.cast(ctype_ptr, ptr)
if not type_map[data.type] then error("unknown type " .. data.type) end
return Blob[type_map[data.type]](Blob, data.size, data.blob)
end
end
return Blob