| 1 |
import os |
| 2 |
import struct |
| 3 |
import argparse |
| 4 |
import torch |
| 5 |
import numpy as np |
| 6 |
from silero_vad import load_silero_vad, __version__ as silero_version |
| 7 |
|
| 8 |
def convert_silero_vad(output_path, print_tensors=True): |
| 9 |
model = load_silero_vad() |
| 10 |
state_dict = model.state_dict() |
| 11 |
|
| 12 |
# Clean up state dict keys - filter out 8k model |
| 13 |
cleaned_dict = {} |
| 14 |
for key, value in state_dict.items(): |
| 15 |
# Skip 8k model |
| 16 |
if "_8k" not in key: |
| 17 |
clean_key = key |
| 18 |
if not key.startswith("_model."): |
| 19 |
clean_key = "_model." + key |
| 20 |
cleaned_dict[clean_key] = value |
| 21 |
|
| 22 |
base, ext = os.path.splitext(output_path) |
| 23 |
output_file = f"{base}-v{silero_version}{ext}" |
| 24 |
print(f"Saving GGML Silero-VAD model to {output_file}") |
| 25 |
|
| 26 |
print("\nTensor info for debugging:") |
| 27 |
for key, tensor in cleaned_dict.items(): |
| 28 |
print(f" - {key}: {tensor.shape} ({tensor.dtype})") |
| 29 |
print() |
| 30 |
|
| 31 |
with open(output_file, "wb") as fout: |
| 32 |
# Write magic and version |
| 33 |
fout.write(struct.pack("i", 0x67676d6c)) |
| 34 |
|
| 35 |
model_type = "silero-16k" |
| 36 |
str_len = len(model_type) |
| 37 |
fout.write(struct.pack("i", str_len)) |
| 38 |
fout.write(model_type.encode('utf-8')) |
| 39 |
|
| 40 |
version_parts = silero_version.split('.') |
| 41 |
major, minor, patch = map(int, version_parts) |
| 42 |
print(f"Version: {major}.{minor}.{patch}") |
| 43 |
fout.write(struct.pack("i", major)) |
| 44 |
fout.write(struct.pack("i", minor)) |
| 45 |
fout.write(struct.pack("i", patch)) |
| 46 |
|
| 47 |
# Write model architecture parameters |
| 48 |
window_size = 512 |
| 49 |
fout.write(struct.pack("i", window_size)) |
| 50 |
context_size = 64 |
| 51 |
fout.write(struct.pack("i", context_size)) |
| 52 |
|
| 53 |
n_encoder_layers = 4 |
| 54 |
fout.write(struct.pack("i", n_encoder_layers)) |
| 55 |
|
| 56 |
# Write encoder dimensions |
| 57 |
input_channels = 129 |
| 58 |
encoder_in_channels = [input_channels, 128, 64, 64] |
| 59 |
encoder_out_channels = [128, 64, 64, 128] |
| 60 |
kernel_size = 3 |
| 61 |
|
| 62 |
for i in range(n_encoder_layers): |
| 63 |
fout.write(struct.pack("i", encoder_in_channels[i])) |
| 64 |
fout.write(struct.pack("i", encoder_out_channels[i])) |
| 65 |
fout.write(struct.pack("i", kernel_size)) |
| 66 |
|
| 67 |
# Write LSTM dimensions |
| 68 |
lstm_input_size = 128 |
| 69 |
lstm_hidden_size = 128 |
| 70 |
fout.write(struct.pack("i", lstm_input_size)) |
| 71 |
fout.write(struct.pack("i", lstm_hidden_size)) |
| 72 |
|
| 73 |
# Write final conv dimensions |
| 74 |
final_conv_in = 128 |
| 75 |
final_conv_out = 1 |
| 76 |
fout.write(struct.pack("i", final_conv_in)) |
| 77 |
fout.write(struct.pack("i", final_conv_out)) |
| 78 |
|
| 79 |
# Define tensor keys to write |
| 80 |
tensor_keys = [] |
| 81 |
|
| 82 |
# Encoder weights |
| 83 |
for i in range(n_encoder_layers): |
| 84 |
weight_key = f"_model.encoder.{i}.reparam_conv.weight" |
| 85 |
bias_key = f"_model.encoder.{i}.reparam_conv.bias" |
| 86 |
if weight_key in cleaned_dict and bias_key in cleaned_dict: |
| 87 |
tensor_keys.append(weight_key) |
| 88 |
tensor_keys.append(bias_key) |
| 89 |
|
| 90 |
# LSTM weights |
| 91 |
lstm_keys = [ |
| 92 |
"_model.decoder.rnn.weight_ih", |
| 93 |
"_model.decoder.rnn.weight_hh", |
| 94 |
"_model.decoder.rnn.bias_ih", |
| 95 |
"_model.decoder.rnn.bias_hh" |
| 96 |
] |
| 97 |
tensor_keys.extend([k for k in lstm_keys if k in cleaned_dict]) |
| 98 |
|
| 99 |
# Final conv weights |
| 100 |
final_keys = [ |
| 101 |
"_model.decoder.decoder.2.weight", |
| 102 |
"_model.decoder.decoder.2.bias" |
| 103 |
] |
| 104 |
tensor_keys.extend([k for k in final_keys if k in cleaned_dict]) |
| 105 |
|
| 106 |
# STFT basis - add this last |
| 107 |
stft_tensor = "_model.stft.forward_basis_buffer" |
| 108 |
tensor_keys.append(stft_tensor) |
| 109 |
|
| 110 |
print(f"Writing {len(tensor_keys)} tensors:") |
| 111 |
for key in tensor_keys: |
| 112 |
if key in cleaned_dict: |
| 113 |
print(f" - {key}: {cleaned_dict[key].shape}") |
| 114 |
else: |
| 115 |
print(f" - {key}: MISSING") |
| 116 |
|
| 117 |
# Process each tensor |
| 118 |
for key in tensor_keys: |
| 119 |
if key not in cleaned_dict: |
| 120 |
print(f"Warning: Missing tensor {key}, skipping") |
| 121 |
continue |
| 122 |
|
| 123 |
tensor = cleaned_dict[key] |
| 124 |
|
| 125 |
# Special handling for STFT tensor |
| 126 |
if key == "_model.stft.forward_basis_buffer": |
| 127 |
# Get the original numpy array without squeezing |
| 128 |
data = tensor.detach().cpu().numpy() |
| 129 |
# Ensure it has the expected shape |
| 130 |
print(f"STFT tensor original shape: {data.shape}") |
| 131 |
n_dims = 3 |
| 132 |
tensor_shape = [data.shape[2], data.shape[1], data.shape[0]] |
| 133 |
is_conv_weight = True |
| 134 |
else: |
| 135 |
# For other tensors, we can use standard processing |
| 136 |
data = tensor.detach().cpu().squeeze().numpy() |
| 137 |
tensor_shape = list(data.shape) |
| 138 |
|
| 139 |
# Ensure we have at most 4 dimensions for GGML |
| 140 |
n_dims = min(len(tensor_shape), 4) |
| 141 |
|
| 142 |
# Reverse dimensions for GGML |
| 143 |
tensor_shape = tensor_shape[:n_dims] |
| 144 |
tensor_shape.reverse() |
| 145 |
|
| 146 |
# Check if this is a convolution weight tensor |
| 147 |
is_conv_weight = "weight" in key and ("encoder" in key or "_model.decoder.decoder.2" in key) |
| 148 |
|
| 149 |
# Convert to float16 for convolution weights |
| 150 |
if is_conv_weight: |
| 151 |
data = data.astype(np.float16) |
| 152 |
ftype = 1 # float16 |
| 153 |
else: |
| 154 |
ftype = 0 # float32 |
| 155 |
|
| 156 |
# Debug printing of tensor info |
| 157 |
print(f"\nWriting tensor: {key}") |
| 158 |
print(f" Original shape: {tensor.shape}") |
| 159 |
print(f" Processed shape: {data.shape}") |
| 160 |
print(f" GGML dimensions: {n_dims}") |
| 161 |
print(f" GGML shape: {tensor_shape}") |
| 162 |
print(f" Type: {'float16' if ftype == 1 else 'float32'}") |
| 163 |
|
| 164 |
# Convert tensor name to bytes |
| 165 |
name_bytes = key.encode('utf-8') |
| 166 |
name_length = len(name_bytes) |
| 167 |
|
| 168 |
# Write tensor header |
| 169 |
fout.write(struct.pack("i", n_dims)) |
| 170 |
fout.write(struct.pack("i", name_length)) |
| 171 |
fout.write(struct.pack("i", ftype)) |
| 172 |
|
| 173 |
# Write tensor dimensions |
| 174 |
for i in range(n_dims): |
| 175 |
size = tensor_shape[i] if i < len(tensor_shape) else 1 |
| 176 |
fout.write(struct.pack("i", size)) |
| 177 |
print(f" Writing dimension {i}: {size}") |
| 178 |
|
| 179 |
# Write tensor name |
| 180 |
fout.write(name_bytes) |
| 181 |
|
| 182 |
# Write tensor data |
| 183 |
data.tofile(fout) |
| 184 |
|
| 185 |
print(f" Wrote {data.size * (2 if ftype==1 else 4)} bytes") |
| 186 |
|
| 187 |
print(f"\nDone! Model has been converted to GGML format: {output_file}") |
| 188 |
print(f"File size: {os.path.getsize(output_file)} bytes") |
| 189 |
|
| 190 |
if __name__ == "__main__": |
| 191 |
parser = argparse.ArgumentParser(description="Convert Silero-VAD PyTorch model to GGML format") |
| 192 |
parser.add_argument("--output", type=str, required=True, help="Path to output GGML model file") |
| 193 |
parser.add_argument("--print-tensors", action="store_true", help="Print tensor values", default=True) |
| 194 |
args = parser.parse_args() |
| 195 |
|
| 196 |
convert_silero_vad(args.output, args.print_tensors) |