| 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 |
import io |
| 41 |
import os |
| 42 |
import sys |
| 43 |
import struct |
| 44 |
import torch |
| 45 |
import numpy as np |
| 46 |
from pathlib import Path |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
def inspect_model(fname_inp): |
| 53 |
"""Load the PyTorch model via pyannote and print its full tensor inventory.""" |
| 54 |
|
| 55 |
from pyannote.audio import Model |
| 56 |
|
| 57 |
print(f"Loading: {fname_inp}") |
| 58 |
print(f"File size: {os.path.getsize(fname_inp)} bytes ({os.path.getsize(fname_inp) / 1024 / 1024:.2f} MB)") |
| 59 |
print() |
| 60 |
|
| 61 |
model = Model.from_pretrained(fname_inp) |
| 62 |
state_dict = model.state_dict() |
| 63 |
|
| 64 |
print(f"Model class: {type(model).__name__}") |
| 65 |
print(f"Number of tensors: {len(state_dict)}") |
| 66 |
print() |
| 67 |
print(f" {'Name':55s} {'Shape':25s} {'Dtype':10s} {'Elements':>12s} {'Size (KB)':>10s}") |
| 68 |
print(" " + "-" * 118) |
| 69 |
|
| 70 |
total_params = 0 |
| 71 |
total_bytes = 0 |
| 72 |
for name in sorted(state_dict.keys()): |
| 73 |
tensor = state_dict[name] |
| 74 |
shape_str = str(list(tensor.shape)) |
| 75 |
n_elements = tensor.numel() |
| 76 |
n_bytes = n_elements * tensor.element_size() |
| 77 |
total_params += n_elements |
| 78 |
total_bytes += n_bytes |
| 79 |
print(f" {name:55s} {shape_str:25s} {str(tensor.dtype):10s} {n_elements:12d} {n_bytes/1024:10.1f}") |
| 80 |
|
| 81 |
print(" " + "-" * 118) |
| 82 |
print(f" Total: {total_params:,} parameters, {total_bytes / 1024 / 1024:.2f} MB") |
| 83 |
print() |
| 84 |
|
| 85 |
|
| 86 |
print("Sample values (first 5 elements of each tensor):") |
| 87 |
for name in sorted(state_dict.keys()): |
| 88 |
tensor = state_dict[name] |
| 89 |
flat = tensor.detach().cpu().flatten() |
| 90 |
vals = flat[:min(5, len(flat))].numpy() |
| 91 |
vals_str = ", ".join(f"{v:.6f}" for v in vals) |
| 92 |
print(f" {name:55s} [{vals_str}]") |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
def convert_model(fname_inp, dir_out): |
| 100 |
"""Convert the segmentation model to GGML format.""" |
| 101 |
|
| 102 |
from pyannote.audio import Model |
| 103 |
|
| 104 |
print(f"Loading: {fname_inp}") |
| 105 |
|
| 106 |
model = Model.from_pretrained(fname_inp) |
| 107 |
list_vars = model.state_dict() |
| 108 |
|
| 109 |
print(f"Model class: {type(model).__name__}") |
| 110 |
print(f"Tensors: {len(list_vars)}") |
| 111 |
|
| 112 |
|
| 113 |
dir_out = Path(dir_out) |
| 114 |
dir_out.mkdir(parents=True, exist_ok=True) |
| 115 |
fname_out = dir_out / "ggml-segmentation.bin" |
| 116 |
|
| 117 |
fout = fname_out.open("wb") |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
fout.write(struct.pack("i", 0x67676d6c)) |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
ms |
| 139 |
sincnet_n_params = list_vars["sincnet.conv1d.0.filterbank.low_hz_"].shape[0] |
| 140 |
sincnet_filters_0 = sincnet_n_params * 2 |
| 141 |
sincnet_kernel_0 = list_vars["sincnet.conv1d.0.filterbank.window_"].shape[0] * 2 + 1 |
| 142 |
sincnet_filters_1 = list_vars["sincnet.conv1d.1.weight"].shape[0] |
| 143 |
sincnet_kernel_1 = list_vars["sincnet.conv1d.1.weight"].shape[2] |
| 144 |
sincnet_filters_2 = list_vars["sincnet.conv1d.2.weight"].shape[0] |
| 145 |
sincnet_kernel_2 = list_vars["sincnet.conv1d.2.weight"].shape[2] |
| 146 |
|
| 147 |
|
| 148 |
lstm_layers = 0 |
| 149 |
while f"lstm.weight_ih_l{lstm_layers}" in list_vars: |
| 150 |
lstm_layers += 1 |
| 151 |
lstm_input_size = list_vars["lstm.weight_ih_l0"].shape[1] |
| 152 |
lstm_hidden_size = list_vars["lstm.weight_hh_l0"].shape[1] |
| 153 |
lstm_bidir = 1 if "lstm.weight_ih_l0_reverse" in list_vars else 0 |
| 154 |
|
| 155 |
|
| 156 |
linear_layers = 0 |
| 157 |
while f"linear.{linear_layers}.weight" in list_vars: |
| 158 |
linear_layers += 1 |
| 159 |
linear_hidden = list_vars["linear.0.weight"].shape[0] |
| 160 |
|
| 161 |
|
| 162 |
n_classes = list_vars["classifier.weight"].shape[0] |
| 163 |
|
| 164 |
re |
| 165 |
|
| 166 |
|
| 167 |
sincnet_stride_0 = 10 |
| 168 |
|
| 169 |
print("hparams (derived from model):") |
| 170 |
print(f" sincnet_filters_0 = {sincnet_filters_0}") |
| 171 |
print(f" sincnet_kernel_0 = {sincnet_kernel_0}") |
| 172 |
print(f" sincnet_stride_0 = {sincnet_stride_0}") |
| 173 |
print(f" sincnet_filters_1 = {sincnet_filters_1}") |
| 174 |
print(f" sincnet_kernel_1 = {sincnet_kernel_1}") |
| 175 |
print(f" sincnet_filters_2 = {sincnet_filters_2}") |
| 176 |
print(f" sincnet_kernel_2 = {sincnet_kernel_2}") |
| 177 |
print(f" lstm_input_size = {lstm_input_size}") |
| 178 |
print(f" lstm_hidden_size = {lstm_hidden_size}") |
| 179 |
print(f" lstm_layers = {lstm_layers}") |
| 180 |
print(f" lstm_bidirectional = {lstm_bidir}") |
| 181 |
print(f" linear_hidden = {linear_hidden}") |
| 182 |
print(f" linear_layers = {linear_layers}") |
| 183 |
print(f" n_classes = {n_classes}") |
| 184 |
|
| 185 |
fout.write(struct.pack("i", sincnet_filters_0)) |
| 186 |
fout.write(struct.pack("i", sincnet_kernel_0)) |
| 187 |
fout.write(struct.pack("i", sincnet_stride_0)) |
| 188 |
fout.write(struct.pack("i", sincnet_filters_1)) |
| 189 |
fout.write(struct.pack("i", sincnet_kernel_1)) |
| 190 |
fout.write(struct.pack("i", sincnet_filters_2)) |
| 191 |
fout.write(struct.pack("i", sincnet_kernel_2)) |
| 192 |
fout.write(struct.pack("i", lstm_input_size)) |
| 193 |
fout.write(struct.pack("i", lstm_hidden_size)) |
| 194 |
fout.write(struct.pack("i", lstm_layers)) |
| 195 |
fout.write(struct.pack("i", lstm_bidir)) |
| 196 |
fout.write(struct.pack("i", linear_hidden)) |
| 197 |
fout.write(struct.pack("i", linear_layers)) |
| 198 |
fout.write(struct.pack("i", n_classes)) |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
for name in list_vars.keys(): |
| 221 |
data = list_vars[name].squeeze().numpy() |
| 222 |
print("Processing variable: " , name , " with shape: ", data.shape) |
| 223 |
|
| 224 |
n_dims = len(data.shape) |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
data = data.astype(np.float32) |
| 230 |
ftype = 0 |
| 231 |
|
| 232 |
|
| 233 |
str_ = name.encode('utf-8') |
| 234 |
fout.write(struct.pack("iii", n_dims, len(str_), ftype)) |
| 235 |
for i in range(n_dims): |
| 236 |
fout.write(struct.pack("i", data.shape[n_dims - 1 - i])) |
| 237 |
fout.write(str_) |
| 238 |
|
| 239 |
|
| 240 |
data.tofile(fout) |
| 241 |
|
| 242 |
fout.close() |
| 243 |
|
| 244 |
print(f"\nDone. Output file: {fname_out}") |
| 245 |
print(f"Output size: {os.path.getsize(fname_out)} bytes ({os.path.getsize(fname_out) / 1024 / 1024:.2f} MB)") |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
if len(sys.argv) < 2: |
| 253 |
print("Usage:") |
| 254 |
print(f" {sys.argv[0]} --inspect model.bin # inspect model tensors") |
| 255 |
print(f" {sys.argv[0]} model.bin dir-output [use-f32] # convert to GGML") |
| 256 |
print() |
| 257 |
print("Prerequisites: pip install pyannote.audio") |
| 258 |
sys.exit(1) |
| 259 |
|
| 260 |
if sys.argv[1] == "--inspect": |
| 261 |
if len(sys.argv) < 3: |
| 262 |
print("Usage: --inspect model.bin") |
| 263 |
sys.exit(1) |
| 264 |
inspect_model(sys.argv[2]) |
| 265 |
else: |
| 266 |
if len(sys.argv) < 3: |
| 267 |
print(f"Usage: {sys.argv[0]} model.bin dir-output") |
| 268 |
sys.exit(1) |
| 269 |
fname_inp = sys.argv[1] |
| 270 |
dir_out = sys.argv[2] |
| 271 |
convert_model(fname_inp, dir_out) |