|
| 1 | +#include "affine.h" |
| 2 | +#include "subexpr.h" |
| 3 | +#include "utils/mini_numpy.h" |
| 4 | +#include <assert.h> |
| 5 | +#include <stdio.h> |
| 6 | +#include <stdlib.h> |
| 7 | +#include <string.h> |
| 8 | + |
| 9 | +/* Broadcast expands an array to a larger shape by replicating along dimensions. |
| 10 | + * Supports three types: |
| 11 | + * 1. "row": (1, n) -> (m, n) - replicate rows |
| 12 | + * 2. "col": (m, 1) -> (m, n) - replicate columns |
| 13 | + * 3. "scalar": (1, 1) -> (m, n) - replicate in both dimensions |
| 14 | + */ |
| 15 | + |
| 16 | +static void forward(expr *node, const double *u) |
| 17 | +{ |
| 18 | + expr *x = node->left; |
| 19 | + broadcast_expr *bcast = (broadcast_expr *) node; |
| 20 | + |
| 21 | + x->forward(x, u); |
| 22 | + |
| 23 | + if (bcast->type == BROADCAST_ROW) |
| 24 | + { |
| 25 | + /* (1, n) -> (m, n): replicate row m times */ |
| 26 | + for (int j = 0; j < bcast->n; j++) |
| 27 | + { |
| 28 | + for (int i = 0; i < bcast->m; i++) |
| 29 | + { |
| 30 | + node->value[i + j * bcast->m] = x->value[j]; |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + else if (bcast->type == BROADCAST_COL) |
| 35 | + { |
| 36 | + /* (m, 1) -> (m, n): replicate column n times */ |
| 37 | + for (int j = 0; j < bcast->n; j++) |
| 38 | + { |
| 39 | + memcpy(node->value + j * bcast->m, x->value, bcast->m * sizeof(double)); |
| 40 | + } |
| 41 | + } |
| 42 | + else |
| 43 | + { |
| 44 | + /* (1, 1) -> (m, n): fill with scalar value */ |
| 45 | + for (int k = 0; k < node->size; k++) |
| 46 | + { |
| 47 | + node->value[k] = x->value[0]; |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +static void jacobian_init(expr *node) |
| 53 | +{ |
| 54 | + expr *x = node->left; |
| 55 | + x->jacobian_init(x); |
| 56 | + broadcast_expr *bcast = (broadcast_expr *) node; |
| 57 | + int total_nnz; |
| 58 | + |
| 59 | + // -------------------------------------------------------------------- |
| 60 | + // count number of nonzeros |
| 61 | + // -------------------------------------------------------------------- |
| 62 | + if (bcast->type == BROADCAST_ROW) |
| 63 | + { |
| 64 | + /* Row broadcast: (1, n) -> (m, n) */ |
| 65 | + total_nnz = x->jacobian->nnz * bcast->m; |
| 66 | + } |
| 67 | + else if (bcast->type == BROADCAST_COL) |
| 68 | + { |
| 69 | + /* Column broadcast: (m, 1) -> (m, n) */ |
| 70 | + total_nnz = x->jacobian->nnz * bcast->n; |
| 71 | + } |
| 72 | + else |
| 73 | + { |
| 74 | + /* Scalar broadcast: (1, 1) -> (m, n) */ |
| 75 | + total_nnz = x->jacobian->nnz * bcast->m * bcast->n; |
| 76 | + } |
| 77 | + |
| 78 | + node->jacobian = new_csr_matrix(node->size, node->n_vars, total_nnz); |
| 79 | + |
| 80 | + // --------------------------------------------------------------------- |
| 81 | + // fill sparsity pattern |
| 82 | + // --------------------------------------------------------------------- |
| 83 | + CSR_Matrix *Jx = x->jacobian; |
| 84 | + CSR_Matrix *J = node->jacobian; |
| 85 | + J->nnz = 0; |
| 86 | + |
| 87 | + if (bcast->type == BROADCAST_ROW) |
| 88 | + { |
| 89 | + for (int i = 0; i < bcast->n; i++) |
| 90 | + { |
| 91 | + int nnz_in_row = Jx->p[i + 1] - Jx->p[i]; |
| 92 | + |
| 93 | + /* copy columns indices */ |
| 94 | + tile_int(J->i + J->nnz, Jx->i + Jx->p[i], nnz_in_row, bcast->m); |
| 95 | + |
| 96 | + /* set row pointers */ |
| 97 | + for (int rep = 0; rep < bcast->m; rep++) |
| 98 | + { |
| 99 | + J->p[i * bcast->m + rep] = J->nnz; |
| 100 | + J->nnz += nnz_in_row; |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + else if (bcast->type == BROADCAST_COL) |
| 105 | + { |
| 106 | + |
| 107 | + /* copy column indices */ |
| 108 | + tile_int(J->i, Jx->i, Jx->nnz, bcast->n); |
| 109 | + |
| 110 | + /* set row pointers */ |
| 111 | + int offset = 0; |
| 112 | + for (int i = 0; i < bcast->n; i++) |
| 113 | + { |
| 114 | + for (int j = 0; j < bcast->m; j++) |
| 115 | + { |
| 116 | + J->p[i * bcast->m + j] = offset; |
| 117 | + offset += Jx->p[1] - Jx->p[0]; |
| 118 | + } |
| 119 | + } |
| 120 | + assert(offset == total_nnz); |
| 121 | + J->p[node->size] = total_nnz; |
| 122 | + } |
| 123 | + else |
| 124 | + { |
| 125 | + /* copy column indices */ |
| 126 | + tile_int(J->i, Jx->i, Jx->nnz, bcast->m * bcast->n); |
| 127 | + |
| 128 | + /* set row pointers */ |
| 129 | + int offset = 0; |
| 130 | + int nnz = Jx->p[1] - Jx->p[0]; |
| 131 | + for (int i = 0; i < bcast->m * bcast->n; i++) |
| 132 | + { |
| 133 | + J->p[i] = offset; |
| 134 | + offset += nnz; |
| 135 | + } |
| 136 | + assert(offset == total_nnz); |
| 137 | + J->p[node->size] = total_nnz; |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +static void eval_jacobian(expr *node) |
| 142 | +{ |
| 143 | + node->left->eval_jacobian(node->left); |
| 144 | + |
| 145 | + broadcast_expr *bcast = (broadcast_expr *) node; |
| 146 | + CSR_Matrix *Jx = node->left->jacobian; |
| 147 | + CSR_Matrix *J = node->jacobian; |
| 148 | + J->nnz = 0; |
| 149 | + |
| 150 | + if (bcast->type == BROADCAST_ROW) |
| 151 | + { |
| 152 | + for (int i = 0; i < bcast->n; i++) |
| 153 | + { |
| 154 | + int nnz_in_row = Jx->p[i + 1] - Jx->p[i]; |
| 155 | + tile_double(J->x + J->nnz, Jx->x + Jx->p[i], nnz_in_row, bcast->m); |
| 156 | + J->nnz += nnz_in_row * bcast->m; |
| 157 | + } |
| 158 | + } |
| 159 | + else if (bcast->type == BROADCAST_COL) |
| 160 | + { |
| 161 | + tile_double(J->x, Jx->x, Jx->nnz, bcast->n); |
| 162 | + } |
| 163 | + else |
| 164 | + { |
| 165 | + tile_double(J->x, Jx->x, Jx->nnz, bcast->m * bcast->n); |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +static void wsum_hess_init(expr *node) |
| 170 | +{ |
| 171 | + expr *x = node->left; |
| 172 | + x->wsum_hess_init(x); |
| 173 | + |
| 174 | + /* Same sparsity as child - weights get summed */ |
| 175 | + node->wsum_hess = new_csr_matrix(node->n_vars, node->n_vars, x->wsum_hess->nnz); |
| 176 | + memcpy(node->wsum_hess->p, x->wsum_hess->p, (x->wsum_hess->m + 1) * sizeof(int)); |
| 177 | + memcpy(node->wsum_hess->i, x->wsum_hess->i, x->wsum_hess->nnz * sizeof(int)); |
| 178 | + |
| 179 | + /* allocate space for weight vector */ |
| 180 | + node->dwork = malloc(node->size * sizeof(double)); |
| 181 | +} |
| 182 | + |
| 183 | +static void eval_wsum_hess(expr *node, const double *w) |
| 184 | +{ |
| 185 | + broadcast_expr *bcast = (broadcast_expr *) node; |
| 186 | + expr *x = node->left; |
| 187 | + |
| 188 | + /* Zero out the work array first */ |
| 189 | + memset(node->dwork, 0, x->size * sizeof(double)); |
| 190 | + |
| 191 | + if (bcast->type == BROADCAST_ROW) |
| 192 | + { |
| 193 | + /* (1, n) -> (m, n): each input element has m weights to sum */ |
| 194 | + for (int j = 0; j < bcast->n; j++) |
| 195 | + { |
| 196 | + for (int i = 0; i < bcast->m; i++) |
| 197 | + { |
| 198 | + node->dwork[j] += w[i + j * bcast->m]; |
| 199 | + } |
| 200 | + } |
| 201 | + } |
| 202 | + else if (bcast->type == BROADCAST_COL) |
| 203 | + { |
| 204 | + /* (m, 1) -> (m, n): each input element has n weights to sum */ |
| 205 | + for (int j = 0; j < bcast->n; j++) |
| 206 | + { |
| 207 | + for (int i = 0; i < bcast->m; i++) |
| 208 | + { |
| 209 | + node->dwork[i] += w[i + j * bcast->m]; |
| 210 | + } |
| 211 | + } |
| 212 | + } |
| 213 | + else |
| 214 | + { |
| 215 | + /* (1, 1) -> (m, n): scalar has m*n weights to sum */ |
| 216 | + node->dwork[0] = 0.0; |
| 217 | + for (int k = 0; k < bcast->m * bcast->n; k++) |
| 218 | + { |
| 219 | + node->dwork[0] += w[k]; |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + x->eval_wsum_hess(x, node->dwork); |
| 224 | + memcpy(node->wsum_hess->x, x->wsum_hess->x, x->wsum_hess->nnz * sizeof(double)); |
| 225 | +} |
| 226 | + |
| 227 | +static bool is_affine(const expr *node) |
| 228 | +{ |
| 229 | + return node->left->is_affine(node->left); |
| 230 | +} |
| 231 | + |
| 232 | +expr *new_broadcast(expr *child, int target_d1, int target_d2) |
| 233 | +{ |
| 234 | + // --------------------------------------------------------------------------- |
| 235 | + // determine broadcast type |
| 236 | + // --------------------------------------------------------------------------- |
| 237 | + broadcast_type type; |
| 238 | + int m = target_d1; |
| 239 | + int n = target_d2; |
| 240 | + |
| 241 | + if (child->d1 == 1 && child->d2 == n) |
| 242 | + { |
| 243 | + type = BROADCAST_ROW; |
| 244 | + } |
| 245 | + else if (child->d1 == m && child->d2 == 1) |
| 246 | + { |
| 247 | + type = BROADCAST_COL; |
| 248 | + } |
| 249 | + else if (child->d1 == 1 && child->d2 == 1) |
| 250 | + { |
| 251 | + type = BROADCAST_SCALAR; |
| 252 | + } |
| 253 | + else |
| 254 | + { |
| 255 | + assert(false); |
| 256 | + } |
| 257 | + |
| 258 | + broadcast_expr *bcast = (broadcast_expr *) calloc(1, sizeof(broadcast_expr)); |
| 259 | + expr *node = (expr *) bcast; |
| 260 | + |
| 261 | + // -------------------------------------------------------------------------- |
| 262 | + // initialize the rest of the expression |
| 263 | + // -------------------------------------------------------------------------- |
| 264 | + init_expr(node, target_d1, target_d2, child->n_vars, forward, jacobian_init, |
| 265 | + eval_jacobian, is_affine, NULL); |
| 266 | + node->left = child; |
| 267 | + expr_retain(child); |
| 268 | + node->wsum_hess_init = wsum_hess_init; |
| 269 | + node->eval_wsum_hess = eval_wsum_hess; |
| 270 | + bcast->type = type; |
| 271 | + bcast->m = m; |
| 272 | + bcast->n = n; |
| 273 | + |
| 274 | + return node; |
| 275 | +} |
0 commit comments