text
stringlengths 0
2.2M
|
---|
namespace fl {
|
namespace lib {
|
namespace cpu {
|
template <class Float>
|
size_t
|
ForceAlignmentCriterion<Float>::getWorkspaceSize(int B, int T, int N, int L) {
|
WorkspacePtrs<Float> dummy(nullptr, B, T, N, L);
|
return dummy.requiredSize;
|
}
|
template <class Float>
|
void ForceAlignmentCriterion<Float>::forward(
|
int B,
|
int T,
|
int N,
|
int _L,
|
CriterionScaleMode scaleMode,
|
const Float* _input,
|
const int* _target,
|
const int* targetSize,
|
const Float* trans,
|
Float* loss,
|
void* workspace) {
|
WorkspacePtrs<Float> ws(workspace, B, T, N, _L);
|
CriterionUtils<Float>::computeScale(B, T, N, scaleMode, targetSize, ws.scale);
|
#pragma omp parallel for num_threads(B)
|
for (int b = 0; b < B; ++b) {
|
auto* alpha = &ws.alpha[b * T * _L];
|
auto* input = &_input[b * T * N];
|
auto* target = &_target[b * _L];
|
auto* transBuf1 = &ws.transBuf1[b * _L];
|
auto* transBuf2 = &ws.transBuf2[b * _L];
|
int L = targetSize[b];
|
alpha[0] = input[target[0]];
|
for (int i = 0; i < L; ++i) {
|
transBuf1[i] = trans[target[i] * N + target[i]];
|
transBuf2[i] = i > 0 ? trans[target[i] * N + target[i - 1]] : 0;
|
}
|
for (int t = 1; t < T; ++t) {
|
auto* inputCur = &input[t * N];
|
auto* alphaPrev = &alpha[(t - 1) * L];
|
auto* alphaCur = &alpha[t * L];
|
int high = t < L ? t : L;
|
int low = T - t < L ? L - (T - t) : 1;
|
if (T - t >= L) {
|
alphaCur[0] = alphaPrev[0] + transBuf1[0] + inputCur[target[0]];
|
}
|
if (t < L) {
|
alphaCur[high] =
|
alphaPrev[high - 1] + transBuf2[high] + inputCur[target[high]];
|
}
|
for (int i = low; i < high; ++i) {
|
double s1 = alphaPrev[i] + transBuf1[i];
|
double s2 = alphaPrev[i - 1] + transBuf2[i];
|
// lse = logSumExp(s1, s2)
|
double lse =
|
s1 < s2 ? s2 + log1p(exp(s1 - s2)) : s1 + log1p(exp(s2 - s1));
|
alphaCur[i] = lse + inputCur[target[i]];
|
}
|
}
|
loss[b] = alpha[T * L - 1] * ws.scale[b];
|
}
|
}
|
template <class Float>
|
void ForceAlignmentCriterion<Float>::backward(
|
int B,
|
int T,
|
int N,
|
int _L,
|
const int* _target,
|
const int* targetSize,
|
const Float* grad,
|
Float* _inputGrad,
|
Float* transGrad,
|
void* workspace) {
|
WorkspacePtrs<Float> ws(workspace, B, T, N, _L);
|
setZero(_inputGrad, B * T * N);
|
setZero(transGrad, N * N);
|
setZero(ws.alphaGrad, B * T * _L);
|
setZero(ws.transBatchGrad, B * N * N);
|
setZero(ws.transBufGrad1, B * _L);
|
setZero(ws.transBufGrad2, B * _L);
|
#pragma omp parallel for num_threads(B)
|
for (int b = 0; b < B; ++b) {
|
auto* alpha = &ws.alpha[b * T * _L];
|
auto* alphaGrad = &ws.alphaGrad[b * T * _L];
|
auto* inputGrad = &_inputGrad[b * T * N];
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.