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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
/*
vq_binary_switch.c
David Rowe Dec 2021
C implementation of [1], that re-arranges VQ indexes so they are robust to single
bit errors.
[1] Pseudo Gray Coding, Zeger & Gersho 1990
*/
#include <assert.h>
#include <getopt.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include "mbest.h"
#define MAX_DIM 20
#define MAX_ENTRIES 4096
// equation (33) of [1], total cost of all hamming distance 1 vectors of vq index k
float cost_of_distance_one(float *vq, int n, int dim, float *prob, int k, int st, int en, int verbose) {
int log2N = log2(n);
float c = 0.0;
for (int b=0; b<log2N; b++) {
unsigned int index_neighbour = k ^ (1<<b);
float dist = 0.0;
for(int i=st; i<=en; i++)
dist += pow(vq[k*dim+i] - vq[index_neighbour*dim+i], 2.0);
c += prob[k]*dist;
if (verbose)
printf("k: %d b: %d index_neighbour: %d dist: %f prob: %f c: %f \n", k, b, index_neighbour, dist, prob[k], c);
}
return c;
}
// equation (39) of [1]
float distortion_of_current_mapping(float *vq, int n, int dim, float *prob, int st, int en) {
float d = 0.0;
for(int k=0; k<n; k++)
d += cost_of_distance_one(vq, n, dim, prob, k, st, en, 0);
return d;
}
// we sort the cost array c[], returning the indexes of sorted elements
float c[MAX_ENTRIES];
/* Note how the compare function compares the values of the
* array to be sorted. The passed value to this function
* by `qsort' are actually the `idx' array elements.
*/
int compare_increase (const void * a, const void * b) {
int aa = *((int *) a), bb = *((int *) b);
if (c[aa] < c[bb]) {
return 1;
} else if (c[aa] == c[bb]) {
return 0;
} else {
return -1;
}
}
void sort_c(int *idx, const size_t n) {
for (size_t i=0; i<n; i++) idx[i] = i;
qsort(idx, n, sizeof(int), compare_increase);
}
void swap(float *vq, int dim, float *prob, int index1, int index2) {
float tmp[dim];
for(int i=0; i<dim; i++) tmp[i] = vq[index1*dim+i];
for(int i=0; i<dim; i++) vq[index1*dim+i] = vq[index2*dim+i];
for(int i=0; i<dim; i++) vq[index2*dim+i] = tmp[i];
tmp[0] = prob[index1];
prob[index1] = prob[index2];
prob[index2] = tmp[0];
}
int main(int argc, char *argv[]) {
float vq[MAX_DIM*MAX_ENTRIES];
int dim = MAX_DIM;
int max_iter = INT_MAX;
int st = -1;
int en = -1;
int verbose = 0;
int n = 0;
int fast_en = 0;
char prob_fn[80]="";
int o = 0; int opt_idx = 0;
while (o != -1) {
static struct option long_opts[] = {
{"prob", required_argument, 0, 'p'},
{"st", required_argument, 0, 't'},
{"en", required_argument, 0, 'e'},
{0, 0, 0, 0}
};
o = getopt_long(argc,argv,"hd:m:vt:e:n:fp:",long_opts,&opt_idx);
switch (o) {
case 'd':
dim = atoi(optarg);
assert(dim <= MAX_DIM);
break;
case 'm':
max_iter = atoi(optarg);
break;
case 't':
st = atoi(optarg);
break;
case 'e':
en = atoi(optarg);
break;
case 'f':
fast_en = 1;
break;
case 'n':
n = atoi(optarg);
break;
case 'p':
strcpy(prob_fn,optarg);
break;
case 'v':
verbose = 1;
break;
help:
fprintf(stderr, "\n");
fprintf(stderr, "usage: %s -d dimension [-m max_iterations -v --st Kst --en Ken -n nVQ] vq_in.f32 vq_out.f32\n", argv[0]);
fprintf(stderr, "\n");
fprintf(stderr, "-n nVQ Run with just the first nVQ entries of the VQ\n");
fprintf(stderr, "--st Kst Start vector element for error calculation (default 0)\n");
fprintf(stderr, "--en Ken End vector element for error calculation (default K-1)\n");
fprintf(stderr, "--prob probFile f32 file of probabilities for each VQ element (default 1.0)\n");
fprintf(stderr, "-v verbose\n");
exit(1);
}
}
int dx = optind;
if ((argc - dx) < 2) {
fprintf(stderr, "Too few arguments\n");
goto help;
}
if (dim == 0) goto help;
/* default to measuring error on entire vector */
if (st == -1) st = 0;
if (en == -1) en = dim-1;
/* load VQ quantiser file --------------------*/
fprintf(stderr, "loading %s ... ", argv[dx]);
FILE *fq=fopen(argv[dx], "rb");
if (fq == NULL) {
fprintf(stderr, "Couldn't open: %s\n", argv[dx]);
exit(1);
}
if (n==0) {
/* count how many entries m of dimension k are in this VQ file */
float dummy[dim];
while (fread(dummy, sizeof(float), dim, fq) == (size_t)dim)
n++;
assert(n <= MAX_ENTRIES);
fprintf(stderr, "%d entries of vectors width %d\n", n, dim);
rewind(fq);
}
/* load VQ into memory */
int nrd = fread(vq, sizeof(float), n*dim, fq);
assert(nrd == n*dim);
fclose(fq);
/* set probability of each vector to 1.0 as default */
float prob[n];
for(int l=0; l<n; l++) prob[l] = 1.0;
if (strlen(prob_fn)) {
fprintf(stderr, "Reading probability file: %s\n", prob_fn);
FILE *fp = fopen(prob_fn,"rb");
assert(fp != NULL);
int nrd = fread(prob, sizeof(float), n, fp);
assert(nrd == n);
fclose(fp);
float sum = 0.0;
for(int l=0; l<n; l++) sum += prob[l];
fprintf(stderr, "sum = %f\n", sum);
}
int iteration = 0;
int i = 0;
int finished = 0;
int switches = 0;
int log2N = log2(n);
float distortion0 = distortion_of_current_mapping(vq, n, dim, prob, st, en);
fprintf(stderr, "distortion0: %f\n", distortion0);
while(!finished) {
// generate a list A(i) of which vectors have the largest cost of bit errors
for(int k=0; k<n; k++) {
c[k] = cost_of_distance_one(vq, n, dim, prob, k, st, en, verbose);
}
int A[n];
sort_c(A, n);
// Try switching each vector with A(i)
float best_delta = 0; int best_j = 0;
for(int j=1; j<n; j++) {
float distortion1, distortion2, delta = 0.0;
// we can't switch with ourself
if (j != A[i]) {
if (fast_en) {
// subtract just those contributions to delta that will change
delta -= cost_of_distance_one(vq, n, dim, prob, A[i], st, en, verbose);
delta -= cost_of_distance_one(vq, n, dim, prob, j, st, en, verbose);
for (int b=0; b<log2N; b++) {
unsigned int index_neighbour;
index_neighbour = A[i] ^ (1<<b);
if ((index_neighbour != j) && (index_neighbour != A[i]))
delta -= cost_of_distance_one(vq, n, dim, prob, index_neighbour, st, en, verbose);
index_neighbour = j ^ (1<<b);
if ((index_neighbour != j) && (index_neighbour != A[i]))
delta -= cost_of_distance_one(vq, n, dim, prob, index_neighbour, st, en, verbose);
}
}
else
distortion1 = distortion_of_current_mapping(vq, n, dim, prob, st, en);
// switch vq entries A(i) and j
swap(vq, dim, prob, A[i], j);
if (fast_en) {
// add just those contributions to delta that will change
delta += cost_of_distance_one(vq, n, dim, prob, A[i], st, en, verbose);
delta += cost_of_distance_one(vq, n, dim, prob, j, st, en, verbose);
for (int b=0; b<log2N; b++) {
unsigned int index_neighbour;
index_neighbour = A[i] ^ (1<<b);
if ((index_neighbour != j) && (index_neighbour != A[i]))
delta += cost_of_distance_one(vq, n, dim, prob, index_neighbour, st, en, verbose);
index_neighbour = j ^ (1<<b);
if ((index_neighbour != j) && (index_neighbour != A[i]))
delta += cost_of_distance_one(vq, n, dim, prob, index_neighbour, st, en, verbose);
}
}
else {
distortion2 = distortion_of_current_mapping(vq, n, dim, prob, st, en);
delta = distortion2 - distortion1;
}
if (delta < 0.0) {
if (fabs(delta) > best_delta) {
best_delta = fabs(delta);
best_j = j;
}
}
// unswitch
swap(vq, dim, prob, A[i], j);
}
} //next j
// printf("best_delta: %f best_j: %d\n", best_delta, best_j);
if (best_delta == 0.0) {
// Hmm, no improvement, lets try the next vector in the sorted cost list
if (i == n-1) finished = 1; else i++;
} else {
// OK keep the switch that minimised the distortion
swap(vq, dim, prob, A[i], best_j);
switches++;
// save results
FILE *fq=fopen(argv[dx+1], "wb");
if (fq == NULL) {
fprintf(stderr, "Couldn't open: %s\n", argv[dx+1]);
exit(1);
}
int nwr = fwrite(vq, sizeof(float), n*dim, fq);
assert(nwr == n*dim);
fclose(fq);
// set up for next iteration
iteration++;
float distortion = distortion_of_current_mapping(vq, n, dim, prob, st, en);
fprintf(stderr, "it: %3d dist: %f %3.2f i: %3d sw: %3d\n", iteration, distortion,
distortion/distortion0, i, switches);
if (iteration >= max_iter) finished = 1;
i = 0;
}
}
return 0;
}
|