|
| 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, use_f16=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 | + if not key.startswith("_model."): |
| 18 | + key = "_model." + clean_key |
| 19 | + cleaned_dict[key] = value |
| 20 | + |
| 21 | + base, ext = os.path.splitext(output_path) |
| 22 | + output_file = f"{base}-v{silero_version}-ggml{ext}" |
| 23 | + print(f"Saving GGML Silero-VAD model to {output_file}") |
| 24 | + |
| 25 | + print("\nTensors to be written:") |
| 26 | + for key, tensor in cleaned_dict.items(): |
| 27 | + print(f"{key}: {tensor.shape}") |
| 28 | + print() |
| 29 | + |
| 30 | + fout = open(output_file, "wb") |
| 31 | + |
| 32 | + # Write magic and version |
| 33 | + fout.write(struct.pack("i", 0x67676d6c)) # "ggml" in hex |
| 34 | + |
| 35 | + # Write the use_f16 flag |
| 36 | + fout.write(struct.pack("i", 1 if use_f16 else 0)) |
| 37 | + |
| 38 | + n_encoder_layers = 4 |
| 39 | + fout.write(struct.pack("i", n_encoder_layers)) |
| 40 | + |
| 41 | + # Write encoder dimensions |
| 42 | + input_channels = 129 |
| 43 | + encoder_in_channels = [input_channels, 128, 64, 64] |
| 44 | + encoder_out_channels = [128, 64, 64, 128] |
| 45 | + kernel_size = 3 |
| 46 | + |
| 47 | + for i in range(n_encoder_layers): |
| 48 | + fout.write(struct.pack("i", encoder_in_channels[i])) |
| 49 | + fout.write(struct.pack("i", encoder_out_channels[i])) |
| 50 | + fout.write(struct.pack("i", kernel_size)) |
| 51 | + |
| 52 | + # Write LSTM dimensions |
| 53 | + lstm_input_size = 128 |
| 54 | + lstm_hidden_size = 128 |
| 55 | + fout.write(struct.pack("i", lstm_input_size)) |
| 56 | + fout.write(struct.pack("i", lstm_hidden_size)) |
| 57 | + |
| 58 | + # Write final conv dimensions |
| 59 | + final_conv_in = 128 |
| 60 | + final_conv_out = 1 |
| 61 | + fout.write(struct.pack("i", final_conv_in)) |
| 62 | + fout.write(struct.pack("i", final_conv_out)) |
| 63 | + |
| 64 | + print("Writing model weights:") |
| 65 | + |
| 66 | + tensor_keys_to_write = [] |
| 67 | + |
| 68 | + for i in range(n_encoder_layers): |
| 69 | + weight_key = f"_model.encoder.{i}.reparam_conv.weight" |
| 70 | + bias_key = f"_model.encoder.{i}.reparam_conv.bias" |
| 71 | + if weight_key in cleaned_dict and bias_key in cleaned_dict: |
| 72 | + tensor_keys_to_write.append(weight_key) |
| 73 | + tensor_keys_to_write.append(bias_key) |
| 74 | + |
| 75 | + lstm_keys = [ |
| 76 | + "_model.decoder.rnn.weight_ih", |
| 77 | + "_model.decoder.rnn.weight_hh", |
| 78 | + "_model.decoder.rnn.bias_ih", |
| 79 | + "_model.decoder.rnn.bias_hh" |
| 80 | + ] |
| 81 | + tensor_keys_to_write.extend([k for k in lstm_keys if k in cleaned_dict]) |
| 82 | + |
| 83 | + final_keys = [ |
| 84 | + "_model.decoder.decoder.2.weight", |
| 85 | + "_model.decoder.decoder.2.bias" |
| 86 | + ] |
| 87 | + tensor_keys_to_write.extend([k for k in final_keys if k in cleaned_dict]) |
| 88 | + |
| 89 | + stft_tensor = "_model.stft.forward_basis_buffer" |
| 90 | + tensor_keys_to_write.extend([stft_tensor]) |
| 91 | + |
| 92 | + for name in tensor_keys_to_write: |
| 93 | + if name not in cleaned_dict: |
| 94 | + print(f"Warning: Missing tensor {name}, skipping") |
| 95 | + continue |
| 96 | + |
| 97 | + tensor = cleaned_dict[name] |
| 98 | + data = tensor.squeeze().numpy() |
| 99 | + print(f"Processing variable: {name} with shape: {data.shape}") |
| 100 | + |
| 101 | + if name.endswith(".reparam_conv.weight") and len(data.shape) == 3: |
| 102 | + print(f" Keeping original convolution weight shape: {data.shape}") |
| 103 | + |
| 104 | + # Determine if we should use float16 or float32 |
| 105 | + ftype = 1 # default to float16 |
| 106 | + if not use_f16 or len(data.shape) < 2 or name.endswith(".bias"): |
| 107 | + ftype = 0 # use float32 |
| 108 | + if use_f16: |
| 109 | + print(f" Converting to float32") |
| 110 | + else: |
| 111 | + data = data.astype(np.float16) |
| 112 | + |
| 113 | + n_dims = len(data.shape) |
| 114 | + |
| 115 | + # Write header |
| 116 | + str_bytes = name.encode('utf-8') |
| 117 | + fout.write(struct.pack("iii", n_dims, len(str_bytes), ftype)) |
| 118 | + |
| 119 | + for i in range(n_dims): |
| 120 | + fout.write(struct.pack("i", data.shape[n_dims - 1 - i])) |
| 121 | + |
| 122 | + fout.write(str_bytes) |
| 123 | + |
| 124 | + data.tofile(fout) |
| 125 | + |
| 126 | + fout.close() |
| 127 | + print(f"Done! Model has been converted to GGML format: {output_file}") |
| 128 | + |
| 129 | +if __name__ == "__main__": |
| 130 | + parser = argparse.ArgumentParser(description="Convert Silero-VAD PyTorch model to GGML format") |
| 131 | + parser.add_argument("--output", type=str, required=True, help="Path to output GGML model file") |
| 132 | + parser.add_argument("--use-f16", action="store_true", help="Use float16 precision", default=True) |
| 133 | + args = parser.parse_args() |
| 134 | + |
| 135 | + convert_silero_vad(args.output, args.use_f16) |
0 commit comments