Skip to main content

WTS Bridge

The .wts (Weights) file format is a plain text-formatted weight storage file used primarily as an intermediate format for TensorRT.

Functionality

Unlike binary formats like .pt or .onnx, .wts files store neural network weight parameters in a human-readable textual format. This is particularly useful for:

  • Custom Layers: When ONNX doesn't support a specific layer, you can manually extract weights and build a custom TensorRT builder.
  • Debugging: Inspecting weights in a human-readable format.
  • Fine-grained Optimization Control: Controlling every aspect of weight conversion.

File Structure

The .wts file follows a simple format:

<number_of_layers>
<layer_name_1> <num_weights_1> <weight_values_1...>
<layer_name_2> <num_weights_2> <weight_values_2...>
...

Example:

3
conv1.weight 324 0.123 -0.456 0.789 ...
conv1.bias 16 0.01 -0.02 0.03 ...
fc1.weight 10240 0.234 -0.567 0.890 ...

Creating WTS Files from PyTorch

import torch

def generate_wts(pytorch_model, output_file):
named_parameters = pytorch_model.named_parameters()
num_layers = len(list(named_parameters))

with open(output_file, 'w') as f:
f.write(f"{num_layers}\n")

for name, param in pytorch_model.named_parameters():
param = param.cpu().detach().numpy().astype('float32')
flat_weights = param.flatten()
num_weights = len(flat_weights)

f.write(f"{name} {num_weights} ")
weights_str = " ".join([str(w) for w in flat_weights])
f.write(weights_str + "\n")

Parsing WTS Files in C++

#include <fstream>
#include <map>
#include <vector>

std::map<std::string, std::vector<float>> loadWeights(const std::string& file) {
std::map<std::string, std::vector<float>> weight_map;
std::ifstream input(file);

int num_layers;
input >> num_layers;

for (int i = 0; i < num_layers; ++i) {
std::string layer_name;
int num_weights;
input >> layer_name >> num_weights;

std::vector<float> weights(num_weights);
for (int j = 0; j < num_weights; ++j) {
input >> weights[j];
}

weight_map[layer_name] = weights;
}

return weight_map;
}
Recommendation

While .wts provides maximum transparency, it should be used only as a backup when the standard .pt.onnx.engine path fails.

Pros and Cons

ProsCons
Transparency: Easy to debug and validate weight values.Large File Size: 3-4x larger than binary formats.
Fine-grained Control: Great for custom layer conversion.No Architecture Info: Only stores weights, not the graph.
Framework-Independent: Not bound by specific versions.Precision Loss: Minor errors during text conversion.