Spaces:
Sleeping
Sleeping
File size: 9,615 Bytes
66c9c8a | 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 | /** Copyright (c) 2022 NVIDIA CORPORATION. All rights reserved.
* NVIDIA CORPORATION and its licensors retain all intellectual property
* and proprietary rights in and to this software, related documentation
* and any modifications thereto. Any use, reproduction, disclosure or
* distribution of this software and related documentation without an express
* license agreement from NVIDIA CORPORATION is strictly prohibited.
*/
#pragma once
namespace wp
{
CUDA_CALLABLE inline int dense_index(int stride, int i, int j)
{
return i*stride + j;
}
template <bool transpose>
CUDA_CALLABLE inline int dense_index(int rows, int cols, int i, int j)
{
if (transpose)
return j*rows + i;
else
return i*cols + j;
}
template <bool t1, bool t2, bool add>
CUDA_CALLABLE inline void dense_gemm_impl(int m, int n, int p, const float* __restrict__ A, const float* __restrict__ B, float* __restrict__ C)
{
for (int i=0; i < m; i++)
{
for (int j=0; j < n; ++j)
{
float sum = 0.0f;
for (int k=0; k < p; ++k)
{
sum += A[dense_index<t1>(m, p, i, k)]*B[dense_index<t2>(p, n, k, j)];
}
if (add)
C[i*n + j] += sum;
else
C[i*n + j] = sum;
}
}
}
template <bool add=false>
CUDA_CALLABLE inline void dense_gemm(int m, int n, int p, int t1, int t2, const array_t<float>& A, const array_t<float>& B, array_t<float>& C)
{
if (t1 == 0 && t2 == 0)
dense_gemm_impl<false, false, add>(m, n, p, A.data, B.data, C.data);
else if (t1 == 1 && t2 == 0)
dense_gemm_impl<true, false, add>(m, n, p, A.data, B.data, C.data);
else if (t1 == 0 && t2 == 1)
dense_gemm_impl<false, true, add>(m, n, p, A.data, B.data, C.data);
else if (t1 == 1 && t2 == 1)
dense_gemm_impl<true, true, add>(m, n, p, A.data, B.data, C.data);
}
void CUDA_CALLABLE inline dense_chol(int n, const array_t<float>& A, float regularization, array_t<float>& L)
{
for (int j=0; j < n; ++j)
{
float s = A.data[dense_index(n, j, j)] + regularization;
for (int k=0; k < j; ++k)
{
float r = L.data[dense_index(n, j, k)];
s -= r*r;
}
s = sqrt(s);
const float invS = 1.0f/s;
L.data[dense_index(n, j, j)] = s;
for (int i=j+1; i < n; ++i)
{
s = A.data[dense_index(n, i, j)];
for (int k=0; k < j; ++k)
{
s -= L.data[dense_index(n, i, k)]*L.data[dense_index(n, j, k)];
}
L.data[dense_index(n, i, j)] = s*invS;
}
}
}
// Solves (L*L^T)x = b given the Cholesky factor L
CUDA_CALLABLE inline void dense_subs(int n, const array_t<float>& L, const array_t<float>& b, array_t<float>& x)
{
// forward substitution
for (int i=0; i < n; ++i)
{
float s = b.data[i];
for (int j=0; j < i; ++j)
{
s -= L.data[dense_index(n, i, j)]*x.data[j];
}
x.data[i] = s/L.data[dense_index(n, i, i)];
}
// backward substitution
for (int i=n-1; i >= 0; --i)
{
float s = x.data[i];
for (int j=i+1; j < n; ++j)
{
s -= L.data[dense_index(n, j, i)]*x.data[j];
}
x.data[i] = s/L.data[dense_index(n, i, i)];
}
}
CUDA_CALLABLE inline void dense_solve(int n, const array_t<float>& A, const array_t<float>& L, const array_t<float>& b, array_t<float>& x)
{
dense_subs(n, L, b, x);
}
// CUDA_CALLABLE inline void print_matrix(const char* name, int m, int n, const float* data)
// {
// printf("%s = [", name);
// for (int i=0; i < m; ++i)
// {
// for (int j=0; j < n; ++j)
// {
// printf("%f ", data[dense_index(n, i, j)]);
// }
// printf(";\n");
// }
// printf("]\n");
// }
// adjoint methods
CUDA_CALLABLE inline void adj_dense_gemm(
int m, int n, int p, int t1, int t2, const array_t<float>& A, const array_t<float>& B, array_t<float>& C,
int adj_m, int adj_n, int adj_p, int adj_t1, int adj_t2, array_t<float>& adj_A, array_t<float>& adj_B, const array_t<float>& adj_C)
{
// print_matrix("A", m, p, A);
// print_matrix("B", p, n, B);
// printf("t1: %d t2: %d\n", t1, t2);
if (t1)
{
dense_gemm<true>(p, m, n, 0, 1, B, adj_C, adj_A);
dense_gemm<true>(p, n, m, int(!t1), 0, A, adj_C, adj_B);
}
else
{
dense_gemm<true>(m, p, n, 0, int(!t2), adj_C, B, adj_A);
dense_gemm<true>(p, n, m, int(!t1), 0, A, adj_C, adj_B);
}
}
CUDA_CALLABLE inline void adj_dense_chol(
int n, const array_t<float>& A, float regularization, array_t<float>& L,
int adj_n, const array_t<float>& adj_A, float adj_regularization, array_t<float>& adj_L)
{
// nop, use dense_solve to differentiate through (A^-1)b = x
}
CUDA_CALLABLE inline void adj_dense_subs(
int n, const array_t<float>& L, const array_t<float>& b, array_t<float>& x,
int adj_n, const array_t<float>& adj_L, const array_t<float>& adj_b, array_t<float>& adj_x)
{
// nop, use dense_solve to differentiate through (A^-1)b = x
}
CUDA_CALLABLE inline void adj_dense_solve(int n,
const array_t<float>& A, const array_t<float>& L, const array_t<float>& b, const array_t<float>& x,
int adj_n, array_t<float>& adj_A, array_t<float>& adj_L, array_t<float>& adj_b, const array_t<float>& adj_x)
{
// see https://people.maths.ox.ac.uk/gilesm/files/NA-08-01.pwp, section 2.3.1
dense_subs(n, L, adj_x, adj_b);
// A* = -adj_b*x^T
for (int i=0; i < n; ++i)
{
for (int j=0; j < n; ++j)
{
adj_A.data[dense_index(n, i, j)] += -adj_b.data[i]*x.data[j];
}
}
}
template <typename F>
CUDA_CALLABLE inline void mlp(const array_t<float>& weights, const array_t<float>& bias, F activation, int index, const array_t<float>& x, array_t<float>& out)
{
const int m = weights.shape[0];
const int n = weights.shape[1];
const int b = x.shape[1];
for (int i=0; i < m; ++i)
{
float tmp = bias.data[i];
for(int j=0; j < n; ++j)
{
tmp += weights.data[i*n + j]*x.data[index + b*j];
}
out.data[index + b*i] = activation(tmp);
}
}
template <typename F, typename AdjF>
CUDA_CALLABLE inline void adj_mlp(const array_t<float>& weights, const array_t<float>& bias, F activation, int index, const array_t<float>& x, array_t<float>& out,
array_t<float>& adj_weights, array_t<float>& adj_bias, AdjF adj_activation, int adj_index, array_t<float>& adj_x, array_t<float>& adj_out)
{
const int m = weights.shape[0];
const int n = weights.shape[1];
const int b = x.shape[1];
for (int i=0; i < m; ++i)
{
// recompute forward pass so we don't have to store pre-activation outputs
float tmp = bias.data[i];
for(int j=0; j < n; ++j)
{
tmp += weights.data[i*n + j]*x.data[index + b*j];
}
// adjoint w.r.t to acivation
float adj_f = 0.0f;
if (adj_out.data)
adj_activation(tmp, adj_f, adj_out.data[index + b*i]);
for (int j=0; j < n; ++j)
{
// adjoint w.r.t M_i
if (adj_weights.data)
atomic_add(&adj_weights.data[i*n + j], x.data[index + b*j]*adj_f); // todo: reduce these atomic stores using warp/block level reductions
// adjoint w.r.t x
if (adj_x.data)
atomic_add(&adj_x.data[index + b*j], weights.data[i*n + j]*adj_f);
}
// adjoint w.r.t b
if (adj_bias.data)
atomic_add(&adj_bias.data[i], adj_f);
}
}
// template <typename F>
// CUDA_CALLABLE inline void mlp(const array_t<float>& weights, const array_t<float>& bias, F activation, int m, int n, int b, int index, const array_t<float>& x, array_t<float>& out)
// {
// x += index*n;
// out += index*m;
// for (int i=0; i < m; ++i)
// {
// float tmp = bias[i];
// for(int j=0; j < n; ++j)
// {
// tmp += weights[i*n + j]*x[j];
// }
// out[i] = activation(tmp);
// }
// }
// template <typename F, typename AdjF>
// CUDA_CALLABLE inline void adj_mlp(const array_t<float>& weights, const array_t<float>& bias, F activation, int m, int n, int b, int index, const array_t<float>& x, const array_t<float>& out,
// array_t<float>& adj_weights, array_t<float>& adj_bias, AdjF adj_activation, int adj_m, int adj_n, int adj_b, int adj_index, array_t<float>& adj_x, array_t<float>& adj_out)
// {
// x += index*n;
// out += index*m;
// adj_x += index*n;
// adj_out += index*m;
// for (int i=0; i < m; ++i)
// {
// // recompute forward pass so we don't have to store pre-activation outputs
// float tmp = bias[i];
// for(int j=0; j < n; ++j)
// {
// tmp += weights[i*n + j]*x[index + b*j];
// }
// // adjoint w.r.t to acivation
// float adj_f = 0.0f;
// adj_activation(tmp, adj_f, adj_out[index + b*i]);
// for (int j=0; j < n; ++j)
// {
// // adjoint w.r.t M_i
// adj_weights[i*n + j] += x[j]*adj_f;
// // adjoint w.r.t x
// adj_x[index + b*j] += weights[i*n + j]*adj_f;
// }
// // adjoint w.r.t b
// adj_bias[i] += adj_f;
// }
// }
} // namespace wp |