Skip to content

Commit

Permalink
Update gguf.js (#1209)
Browse files Browse the repository at this point in the history
  • Loading branch information
lutzroeder committed Jan 12, 2024
1 parent af6a4d9 commit a89846a
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions source/gguf.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,27 +229,30 @@ gguf.Reader = class {
const entry = reader.entry();
this.metadata.set(entry.name, entry.value);
}
for (let i = 0; i < context.header.n_tensors; i++) {
const tensor = reader.tensor();
this.tensors.set(tensor.name, tensor);
}
context.alignment = this.metadata.get('general.alignment') || 32;
const offset_pad = reader.position % context.alignment;
if (offset_pad != 0) {
reader.skip(context.alignment - offset_pad);
}
context.offset = reader.position;
if (context.offset < this._stream.length) {
for (const tensor of this.tensors.values()) {
reader.seek(context.offset + tensor.offset);
if (!gguf.Reader.GGML_QUANT_SIZES.has(tensor.type)) {
throw new gguf.Error(`Unsupported tensor quantization type '${tensor.type}'.`);
const tensors = context.header.n_tensors;
if (tensors > 0) {
for (let i = 0; i < tensors; i++) {
const tensor = reader.tensor();
this.tensors.set(tensor.name, tensor);
}
context.alignment = this.metadata.get('general.alignment') || 32;
const offset_pad = reader.position % context.alignment;
if (offset_pad != 0) {
reader.skip(context.alignment - offset_pad);
}
context.offset = reader.position;
if (context.offset < this._stream.length) {
for (const tensor of this.tensors.values()) {
reader.seek(context.offset + tensor.offset);
if (!gguf.Reader.GGML_QUANT_SIZES.has(tensor.type)) {
throw new gguf.Error(`Unsupported tensor quantization type '${tensor.type}'.`);
}
const [block_size, type_size, dtype] = gguf.Reader.GGML_QUANT_SIZES.get(tensor.type);
const n_elems = tensor.ne.reduce((a, b) => a * b, 1);
const n_bytes = Math.floor(n_elems * type_size / block_size);
tensor.dtype = dtype || '?';
tensor.data = reader.stream(n_bytes);
}
const [block_size, type_size, dtype] = gguf.Reader.GGML_QUANT_SIZES.get(tensor.type);
const n_elems = tensor.ne.reduce((a, b) => a * b, 1);
const n_bytes = Math.floor(n_elems * type_size / block_size);
tensor.dtype = dtype || '?';
tensor.data = reader.stream(n_bytes);
}
}
}
Expand Down

0 comments on commit a89846a

Please sign in to comment.