-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkron_left.c
More file actions
338 lines (295 loc) · 10.7 KB
/
kron_left.c
File metadata and controls
338 lines (295 loc) · 10.7 KB
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*
* Copyright 2026 Daniel Cederberg and William Zhang
*
* This file is part of the DNLP-differentiation-engine project.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "bivariate.h"
#include "subexpr.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
* Kronecker product: Z = kron(C, X) where C is a constant (m x n) matrix
* and X is a variable expression of shape (p x q).
*
* Output Z has shape (m*p, n*q), stored column-major as vec(Z) of length m*p*n*q.
*
* Key identity: Z[i*p+k, j*q+l] = C[i,j] * X[k,l]
* In column-major: vec(Z)[r] where r = (j*q+l)*(m*p) + i*p + k
* depends on vec(X)[s] where s = l*p + k, with coefficient C[i,j].
*
* Jacobian structure: each row r of J_Z is a scaled copy of row s of J_X.
* Only rows where C[i,j] != 0 are non-trivial.
*/
/* ------------------------------------------------------------------ */
/* Forward pass */
/* ------------------------------------------------------------------ */
static void forward(expr *node, const double *u)
{
kron_left_expr *kn = (kron_left_expr *) node;
expr *child = node->left;
CSR_Matrix *C = kn->C;
int m = kn->m, p = kn->p, q = kn->q;
int mp = m * p;
child->forward(child, u);
/* Zero output first */
memset(node->value, 0, (size_t) node->size * sizeof(double));
/* For each nonzero C[i,j], fill block: Z[i*p+k, j*q+l] = C[i,j] * X[k,l] */
for (int i = 0; i < m; i++)
{
for (int idx = C->p[i]; idx < C->p[i + 1]; idx++)
{
int j = C->i[idx];
double cij = C->x[idx];
for (int l = 0; l < q; l++)
{
int z_col_start = (j * q + l) * mp + i * p;
int x_col_start = l * p;
for (int k = 0; k < p; k++)
{
node->value[z_col_start + k] =
cij * child->value[x_col_start + k];
}
}
}
}
}
/* ------------------------------------------------------------------ */
/* Affine check */
/* ------------------------------------------------------------------ */
static bool is_affine(const expr *node)
{
return node->left->is_affine(node->left);
}
/* ------------------------------------------------------------------ */
/* Jacobian initialization */
/* ------------------------------------------------------------------ */
static void jacobian_init(expr *node)
{
kron_left_expr *kn = (kron_left_expr *) node;
expr *child = node->left;
CSR_Matrix *C = kn->C;
int m = kn->m, p = kn->p, q = kn->q;
int mp = m * p;
int out_size = node->size; /* m * p * n * q */
/* Initialize child's Jacobian */
child->jacobian_init(child);
CSR_Matrix *Jchild = child->jacobian;
/*
* Count total nnz: for each output row r corresponding to nonzero C[i,j],
* the nnz equals the nnz of child's Jacobian row s = l*p + k.
*/
int total_nnz = 0;
for (int i = 0; i < m; i++)
{
int row_nnz_C = C->p[i + 1] - C->p[i];
if (row_nnz_C == 0) continue;
for (int l = 0; l < q; l++)
{
for (int k = 0; k < p; k++)
{
int s = l * p + k; /* child row */
int child_row_nnz = Jchild->p[s + 1] - Jchild->p[s];
total_nnz += row_nnz_C * child_row_nnz;
}
}
}
/* Allocate Jacobian */
node->jacobian = new_csr_matrix(out_size, node->n_vars, total_nnz);
/* Allocate row_map and row_scale arrays for eval_jacobian */
kn->row_map = (int *) malloc((size_t) out_size * sizeof(int));
kn->row_scale_idx = (int *) malloc((size_t) out_size * sizeof(int));
/*
* Fill Jacobian sparsity pattern.
* Iterate in column-major order over Z:
* r = (j*q + l) * mp + i*p + k
* maps to child row s = l*p + k, with scale C[i,j]
*/
int nnz_idx = 0;
for (int r = 0; r < out_size; r++)
{
node->jacobian->p[r] = nnz_idx;
/* Decode r: r = col_Z * mp + row_Z */
int row_Z = r % mp; /* i*p + k */
int col_Z = r / mp; /* j*q + l */
int i = row_Z / p;
int k = row_Z % p;
int j = col_Z / q;
int l = col_Z % q;
int s = l * p + k; /* child Jacobian row */
kn->row_map[r] = s;
/* Find C[i,j] in CSR */
double cij = 0.0;
int cij_found = 0;
for (int idx = C->p[i]; idx < C->p[i + 1]; idx++)
{
if (C->i[idx] == j)
{
cij = C->x[idx];
kn->row_scale_idx[r] = idx;
cij_found = 1;
break;
}
}
if (!cij_found || cij == 0.0)
{
kn->row_scale_idx[r] = -1; /* mark as zero row */
continue;
}
/* Copy child's column indices for row s */
int child_start = Jchild->p[s];
int child_end = Jchild->p[s + 1];
int child_row_nnz = child_end - child_start;
memcpy(node->jacobian->i + nnz_idx, Jchild->i + child_start,
(size_t) child_row_nnz * sizeof(int));
nnz_idx += child_row_nnz;
}
node->jacobian->p[out_size] = nnz_idx;
node->jacobian->nnz = nnz_idx;
assert(nnz_idx == total_nnz);
}
/* ------------------------------------------------------------------ */
/* Jacobian evaluation */
/* ------------------------------------------------------------------ */
static void eval_jacobian(expr *node)
{
kron_left_expr *kn = (kron_left_expr *) node;
expr *child = node->left;
CSR_Matrix *C = kn->C;
CSR_Matrix *Jchild = child->jacobian;
CSR_Matrix *J = node->jacobian;
int out_size = node->size;
/* Evaluate child's Jacobian */
child->eval_jacobian(child);
/* Fill values: each active row r copies child row s scaled by C[i,j] */
for (int r = 0; r < out_size; r++)
{
int j_start = J->p[r];
int j_end = J->p[r + 1];
int row_nnz = j_end - j_start;
if (row_nnz == 0) continue;
int s = kn->row_map[r];
int c_idx = kn->row_scale_idx[r];
double cij = C->x[c_idx];
int child_start = Jchild->p[s];
for (int t = 0; t < row_nnz; t++)
{
J->x[j_start + t] = cij * Jchild->x[child_start + t];
}
}
}
/* ------------------------------------------------------------------ */
/* Weighted-sum Hessian initialization */
/* ------------------------------------------------------------------ */
static void wsum_hess_init(expr *node)
{
expr *child = node->left;
/* Initialize child's Hessian */
child->wsum_hess_init(child);
/* kron_left is linear in X, so Hessian has same sparsity as child */
node->wsum_hess =
new_csr_matrix(node->n_vars, node->n_vars, child->wsum_hess->nnz);
memcpy(node->wsum_hess->p, child->wsum_hess->p,
(size_t) (node->n_vars + 1) * sizeof(int));
memcpy(node->wsum_hess->i, child->wsum_hess->i,
(size_t) child->wsum_hess->nnz * sizeof(int));
/* Allocate workspace for reverse-mode weight accumulation */
node->dwork = (double *) calloc((size_t) child->size, sizeof(double));
}
/* ------------------------------------------------------------------ */
/* Weighted-sum Hessian evaluation */
/* ------------------------------------------------------------------ */
static void eval_wsum_hess(expr *node, const double *w)
{
kron_left_expr *kn = (kron_left_expr *) node;
expr *child = node->left;
CSR_Matrix *C = kn->C;
int m = kn->m, p = kn->p, q = kn->q;
int mp = m * p;
int child_size = child->size;
/*
* Reverse mode: w_child[s] = sum over active r mapping to s of (C[i,j] * w[r])
* This is the adjoint of the forward pass.
*/
memset(node->dwork, 0, (size_t) child_size * sizeof(double));
for (int i = 0; i < m; i++)
{
for (int idx = C->p[i]; idx < C->p[i + 1]; idx++)
{
int j = C->i[idx];
double cij = C->x[idx];
for (int l = 0; l < q; l++)
{
for (int k = 0; k < p; k++)
{
int r = (j * q + l) * mp + i * p + k;
int s = l * p + k;
node->dwork[s] += cij * w[r];
}
}
}
}
/* Delegate to child */
child->eval_wsum_hess(child, node->dwork);
memcpy(node->wsum_hess->x, child->wsum_hess->x,
(size_t) node->wsum_hess->nnz * sizeof(double));
}
/* ------------------------------------------------------------------ */
/* Cleanup */
/* ------------------------------------------------------------------ */
static void free_type_data(expr *node)
{
kron_left_expr *kn = (kron_left_expr *) node;
free_csr_matrix(kn->C);
free(kn->row_map);
free(kn->row_scale_idx);
kn->C = NULL;
kn->row_map = NULL;
kn->row_scale_idx = NULL;
}
/* ------------------------------------------------------------------ */
/* Constructor */
/* ------------------------------------------------------------------ */
expr *new_kron_left(expr *child, const CSR_Matrix *C, int p, int q)
{
int m = C->m;
int n = C->n;
/* Verify child dimensions */
if (child->size != p * q)
{
fprintf(stderr,
"Error in new_kron_left: child size %d != p*q = %d*%d = %d\n",
child->size, p, q, p * q);
exit(1);
}
/* Output: kron(C, X) has shape (m*p, n*q) */
int d1 = m * p;
int d2 = n * q;
kron_left_expr *kn = (kron_left_expr *) calloc(1, sizeof(kron_left_expr));
expr *node = &kn->base;
init_expr(node, d1, d2, child->n_vars, forward, jacobian_init, eval_jacobian,
is_affine, wsum_hess_init, eval_wsum_hess, free_type_data);
node->left = child;
expr_retain(child);
kn->m = m;
kn->n = n;
kn->p = p;
kn->q = q;
kn->C = new_csr(C);
kn->row_map = NULL;
kn->row_scale_idx = NULL;
return node;
}