From e328867822259f8d9e78e9714cf78e1f8013101e Mon Sep 17 00:00:00 2001 From: Pierre-Antoine Bannier Date: Fri, 11 Sep 2026 13:46:35 +0200 Subject: [PATCH] refactor: share tensor writer across ggml converters convert_sam3/sam2/edgetam_to_ggml.py each carried a copy of write_tensor and the FTYPE constants. Move them to ggml_writer.py, parameterised by the keep-f32 name substrings (sam3 adds "freqs_cis", edgetam adds "latents"). Output bytes are unchanged. Co-Authored-By: Claude Opus 5 (1M context) Claude-Session: https://claude.ai/code/session_01M2qLmQ9ag49hT61V6i9qAq --- convert_edgetam_to_ggml.py | 48 ++----------------------------------- convert_sam2_to_ggml.py | 45 +--------------------------------- convert_sam3_to_ggml.py | 49 ++------------------------------------ ggml_writer.py | 21 ++++++++++++++++ 4 files changed, 26 insertions(+), 137 deletions(-) create mode 100644 ggml_writer.py diff --git a/convert_edgetam_to_ggml.py b/convert_edgetam_to_ggml.py index 468293b..047b754 100644 --- a/convert_edgetam_to_ggml.py +++ b/convert_edgetam_to_ggml.py @@ -19,14 +19,12 @@ import struct import sys import os -import numpy as np +from ggml_writer import KEEP_F32, write_tensor # ── Constants ────────────────────────────────────────────────────────────────── MAGIC = 0x73616D32 # "sam2" — reuse SAM2 magic, dispatch on backbone_type VERSION = 1 -FTYPE_F32 = 0 -FTYPE_F16 = 1 # EdgeTAM hyperparameters (from edgetam.yaml) EDGETAM_HPARAMS = { @@ -472,48 +470,6 @@ def write_header(fout, ftype, n_tensors, hp): fout.write(struct.pack("= 2 - and "embed" not in name - and "pos_embed" not in name - and "tpos" not in name - and "pe_gaussian" not in name - and "token" not in name - and "no_obj" not in name - and "no_mem" not in name - and "gamma" not in name - and "latents" not in name) - - dtype_id = FTYPE_F16 if use_f16 else FTYPE_F32 - - if use_f16: - data = data.astype(np.float16) - else: - data = data.astype(np.float32) - - fout.write(struct.pack("= 2 - and "embed" not in name - and "pos_embed" not in name - and "tpos" not in name - and "pe_gaussian" not in name - and "token" not in name - and "no_obj" not in name - and "no_mem" not in name - and "gamma" not in name) - - dtype_id = FTYPE_F16 if use_f16 else FTYPE_F32 - - if use_f16: - data = data.astype(np.float16) - else: - data = data.astype(np.float32) - - fout.write(struct.pack(" str: diff --git a/convert_sam3_to_ggml.py b/convert_sam3_to_ggml.py index d35534f..56ae837 100644 --- a/convert_sam3_to_ggml.py +++ b/convert_sam3_to_ggml.py @@ -14,13 +14,12 @@ import os import re import numpy as np +from ggml_writer import KEEP_F32, write_tensor # ── Constants ────────────────────────────────────────────────────────────────── MAGIC = 0x73616D33 # "sam3" VERSION = 3 -FTYPE_F32 = 0 -FTYPE_F16 = 1 # ── Hyperparameter defaults ─────────────────────────────────────────────────── @@ -211,50 +210,6 @@ def write_header(fout, ftype: int, n_tensors: int, visual_only: bool = False): fout.write(struct.pack("= 2 - and "embed" not in name - and "pos_embed" not in name - and "tpos" not in name - and "pe_gaussian" not in name - and "freqs_cis" not in name - and "token" not in name - and "no_obj" not in name - and "no_mem" not in name - and "gamma" not in name) - - dtype_id = FTYPE_F16 if use_f16 else FTYPE_F32 - - if use_f16: - data = data.astype(np.float16) - else: - data = data.astype(np.float32) - - # Write: n_dims, name_len, dtype, shape (reversed), name, padding, data - fout.write(struct.pack("= 2 and not any(s in name for s in keep_f32) + name_bytes = name.encode("utf-8") + fout.write(struct.pack("<3i", data.ndim, len(name_bytes), FTYPE_F16 if use_f16 else FTYPE_F32)) + fout.write(struct.pack(f"<{data.ndim}i", *reversed(data.shape))) # ggml dims are reversed + fout.write(name_bytes) + fout.write(b"\x00" * (-fout.tell() % 32)) + fout.write(data.astype(np.float16 if use_f16 else np.float32).tobytes())