|
| 1 | +#include <cmath> |
| 2 | +#include <cstring> |
| 3 | +#include <limits> |
| 4 | +#include <pmmintrin.h> |
| 5 | + |
| 6 | +#define UNUSED(x) (void)(x) |
| 7 | +#define DEBUG_ONLY(x) (void)(x) |
| 8 | + |
| 9 | +#ifdef COMPILER_GCC |
| 10 | + |
| 11 | +#include "UnixSal.h" |
| 12 | +#define EXPORT_API(ret) extern "C" __attribute__((visibility("default"))) ret |
| 13 | + |
| 14 | +#else |
| 15 | +#include <intrin.h> |
| 16 | +#define EXPORT_API(ret) extern "C" __declspec(dllexport) ret __stdcall |
| 17 | +#endif |
| 18 | + |
| 19 | +EXPORT_API(void) CalculateIntermediateVariablesNative(int fieldCount, int latentDim, int count, _In_ int * fieldIndices, _In_ int * featureIndices, _In_ float * featureValues, |
| 20 | + _In_ float * linearWeights, _In_ float * latentWeights, _Inout_ float * latentSum, _Out_ float * response) |
| 21 | +{ |
| 22 | + const int m = fieldCount; |
| 23 | + const int d = latentDim; |
| 24 | + const int c = count; |
| 25 | + const int * pf = fieldIndices; |
| 26 | + const int * pi = featureIndices; |
| 27 | + const float * px = featureValues; |
| 28 | + const float * pw = linearWeights; |
| 29 | + const float * pv = latentWeights; |
| 30 | + float * pq = latentSum; |
| 31 | + float linearResponse = 0; |
| 32 | + float latentResponse = 0; |
| 33 | + |
| 34 | + memset(pq, 0, sizeof(float) * m * m * d); |
| 35 | + __m128 _y = _mm_setzero_ps(); |
| 36 | + __m128 _tmp = _mm_setzero_ps(); |
| 37 | + |
| 38 | + for (int i = 0; i < c; i++) |
| 39 | + { |
| 40 | + const int f = pf[i]; |
| 41 | + const int j = pi[i]; |
| 42 | + linearResponse += pw[j] * px[i]; |
| 43 | + |
| 44 | + const __m128 _x = _mm_load1_ps(px + i); |
| 45 | + const __m128 _xx = _mm_mul_ps(_x, _x); |
| 46 | + |
| 47 | + // tmp -= <v_j,f, v_j,f> * x * x |
| 48 | + const int vBias = j * m * d + f * d; |
| 49 | + const float * vjf = pv + vBias; |
| 50 | + for (int k = 0; k + 4 <= d; k += 4) |
| 51 | + { |
| 52 | + const __m128 _v = _mm_load_ps(vjf + k); |
| 53 | + _tmp = _mm_sub_ps(_tmp, _mm_mul_ps(_mm_mul_ps(_v, _v), _xx)); |
| 54 | + } |
| 55 | + |
| 56 | + for (int fprime = 0; fprime < m; fprime++) |
| 57 | + { |
| 58 | + const int vBias = j * m * d + fprime * d; |
| 59 | + const int qBias = f * m * d + fprime * d; |
| 60 | + const float * vjfprime = pv + vBias; |
| 61 | + float * qffprime = pq + qBias; |
| 62 | + |
| 63 | + // q_f,f' += v_j,f' * x |
| 64 | + for (int k = 0; k + 4 <= d; k += 4) |
| 65 | + { |
| 66 | + const __m128 _v = _mm_load_ps(vjfprime + k); |
| 67 | + __m128 _q = _mm_load_ps(qffprime + k); |
| 68 | + _q = _mm_add_ps(_q, _mm_mul_ps(_v, _x)); |
| 69 | + _mm_store_ps(qffprime + k, _q); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + for (int f = 0; f < m; f++) |
| 75 | + { |
| 76 | + // tmp += <q_f,f, q_f,f> |
| 77 | + const float * qff = pq + f * m * d + f * d; |
| 78 | + for (int k = 0; k + 4 <= d; k += 4) |
| 79 | + { |
| 80 | + __m128 _qff = _mm_load_ps(qff + k); |
| 81 | + _tmp = _mm_add_ps(_tmp, _mm_mul_ps(_qff, _qff)); |
| 82 | + } |
| 83 | + |
| 84 | + // y += <q_f,f', q_f',f>, f != f' |
| 85 | + for (int fprime = f + 1; fprime < m; fprime++) |
| 86 | + { |
| 87 | + const float * qffprime = pq + f * m * d + fprime * d; |
| 88 | + const float * qfprimef = pq + fprime * m * d + f * d; |
| 89 | + for (int k = 0; k + 4 <= d; k += 4) |
| 90 | + { |
| 91 | + __m128 _qffprime = _mm_load_ps(qffprime + k); |
| 92 | + __m128 _qfprimef = _mm_load_ps(qfprimef + k); |
| 93 | + _y = _mm_add_ps(_y, _mm_mul_ps(_qffprime, _qfprimef)); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + _y = _mm_add_ps(_y, _mm_mul_ps(_mm_set_ps1(0.5f), _tmp)); |
| 99 | + _tmp = _mm_add_ps(_y, _mm_movehl_ps(_y, _y)); |
| 100 | + _y = _mm_add_ps(_tmp, _mm_shuffle_ps(_tmp, _tmp, 1)); // the lowest slot is the response value |
| 101 | + _mm_store_ss(&latentResponse, _y); |
| 102 | + *response = linearResponse + latentResponse; |
| 103 | +} |
| 104 | + |
| 105 | +EXPORT_API(void) CalculateGradientAndUpdateNative(float lambdaLinear, float lambdaLatent, float learningRate, int fieldCount, int latentDim, float weight, int count, |
| 106 | + _In_ int* /*const*/ fieldIndices, _In_ int* /*const*/ featureIndices, _In_ float* /*const*/ featureValues, _In_ float* /*const*/ latentSum, float slope, |
| 107 | + _Inout_ float* linearWeights, _Inout_ float* latentWeights, _Inout_ float* linearAccumulatedSquaredGrads, _Inout_ float* latentAccumulatedSquaredGrads) |
| 108 | +{ |
| 109 | + const int m = fieldCount; |
| 110 | + const int d = latentDim; |
| 111 | + const int c = count; |
| 112 | + const int * pf = fieldIndices; |
| 113 | + const int * pi = featureIndices; |
| 114 | + const float * px = featureValues; |
| 115 | + const float * pq = latentSum; |
| 116 | + float * pw = linearWeights; |
| 117 | + float * pv = latentWeights; |
| 118 | + float * phw = linearAccumulatedSquaredGrads; |
| 119 | + float * phv = latentAccumulatedSquaredGrads; |
| 120 | + |
| 121 | + const __m128 _wei = _mm_set_ps1(weight); |
| 122 | + const __m128 _s = _mm_set_ps1(slope); |
| 123 | + const __m128 _lr = _mm_set_ps1(learningRate); |
| 124 | + const __m128 _lambdav = _mm_set_ps1(lambdaLatent); |
| 125 | + |
| 126 | + for (int i = 0; i < count; i++) |
| 127 | + { |
| 128 | + const int f = pf[i]; |
| 129 | + const int j = pi[i]; |
| 130 | + |
| 131 | + // update linear term w_j |
| 132 | + float g = weight * (lambdaLinear * pw[j] + slope * px[i]); |
| 133 | + phw[j] += g * g; |
| 134 | + pw[j] -= learningRate / sqrt(phw[j]) * g; |
| 135 | + |
| 136 | + // update latent term, v_j,f', f'=1,...,m |
| 137 | + const __m128 _x = _mm_load1_ps(px + i); |
| 138 | + for (int fprime = 0; fprime < m; fprime++) |
| 139 | + { |
| 140 | + float * vjfprime = pv + j * m * d + fprime * d; |
| 141 | + float * hvjfprime = phv + j * m * d + fprime * d; |
| 142 | + const float * qfprimef = pq + fprime * m * d + f * d; |
| 143 | + const __m128 _sx = _mm_mul_ps(_s, _x); |
| 144 | + |
| 145 | + for (int k = 0; k + 4 <= d; k += 4) |
| 146 | + { |
| 147 | + __m128 _v = _mm_load_ps(vjfprime + k); |
| 148 | + __m128 _q = _mm_load_ps(qfprimef + k); |
| 149 | + __m128 _g = _mm_mul_ps(_lambdav, _v); |
| 150 | + if (fprime != f) |
| 151 | + _g = _mm_add_ps(_g, _mm_mul_ps(_sx, _q)); |
| 152 | + else |
| 153 | + _g = _mm_add_ps(_g, _mm_mul_ps(_sx, _mm_sub_ps(_q, _mm_mul_ps(_v, _x)))); |
| 154 | + _g = _mm_mul_ps(_wei, _g); |
| 155 | + |
| 156 | + const __m128 _h = _mm_add_ps(_mm_load_ps(hvjfprime + k), _mm_mul_ps(_g, _g)); |
| 157 | + _v = _mm_sub_ps(_v, _mm_mul_ps(_lr, _mm_mul_ps(_mm_rsqrt_ps(_h), _g))); |
| 158 | + _mm_store_ps(vjfprime + k, _v); |
| 159 | + _mm_store_ps(hvjfprime + k, _h); |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | +} |
0 commit comments