void ContextProjectionBackward<DEVICE_TYPE_CPU>(const CpuMatrix& out_grad_mat, CpuMatrix& in_grad_mat, CpuMatrix& w_grad_mat, const CpuIVector& seq_vec, size_t context_length, int context_start, size_t begin_pad, bool is_padding, size_t total_pad) { size_t input_dim = in_grad_mat ? in_grad_mat.getWidth() : w_grad_mat ? w_grad_mat.getWidth() : 0; const int* starts = seq_vec.getData(); size_t num_sequences = seq_vec.getSize() - 1; for (size_t i = 0; i < num_sequences; ++i) { for (size_t j = 0; j < context_length; ++j) { int begin = starts[i] + context_start + j; int end = starts[i + 1] + context_start + j; int dst_begin = starts[i]; int dst_end = starts[i + 1]; if (begin < starts[i]) { int64_t pad_size = std::min(starts[i] - begin, starts[i + 1] - starts[i]); if (is_padding && w_grad_mat) { MatrixPtr mat = const_cast<CpuMatrix&>(out_grad_mat) .subMatrix(starts[i], pad_size); MatrixPtr sub = w_grad_mat.subMatrix(j, pad_size); sub->addAtOffset(*mat, j * input_dim); } dst_begin = starts[i] + pad_size; begin = starts[i]; } if (end > starts[i + 1]) { int64_t pad_size = std::min(end - starts[i + 1], starts[i + 1] - starts[i]); if (is_padding && w_grad_mat) { MatrixPtr mat = const_cast<CpuMatrix&>(out_grad_mat) .subMatrix(starts[i + 1] - pad_size, pad_size); MatrixPtr sub = w_grad_mat.subMatrix( begin_pad + context_start + j - pad_size, pad_size); sub->addAtOffset(*mat, j * input_dim); } dst_end = starts[i + 1] - pad_size; end = starts[i + 1]; } if (end <= begin) continue; if (!in_grad_mat) continue; MatrixPtr src = in_grad_mat.subMatrix(begin, end - begin); MatrixPtr dst = const_cast<CpuMatrix&>(out_grad_mat) .subMatrix(dst_begin, dst_end - dst_begin); src->addAtOffset(*dst, j * input_dim); } } }
void ContextProjectionForward<DEVICE_TYPE_CPU>(CpuMatrix& out_mat, const CpuMatrix& input_mat, const CpuMatrix& weight_mat, const CpuIVector& seq_vec, size_t context_length, int context_start, size_t begin_pad) { const int* starts = seq_vec.getData(); const size_t num_sequences = seq_vec.getSize() - 1; for (size_t i = 0; i < num_sequences; ++i) { for (size_t j = 0; j < context_length; ++j) { int begin = starts[i] + context_start + j; int end = starts[i + 1] + context_start + j; int dst_begin = starts[i]; int dst_end = starts[i + 1]; if (begin < starts[i]) { int64_t pad_size = std::min(starts[i] - begin, starts[i + 1] - starts[i]); MatrixPtr mat = out_mat.subMatrix(starts[i], pad_size); if (weight_mat) { MatrixPtr sub = const_cast<CpuMatrix&>(weight_mat).subMatrix(j, pad_size); mat->addAtOffset(*sub, j * input_mat.getWidth()); } dst_begin = starts[i] + pad_size; begin = starts[i]; } if (end > starts[i + 1]) { int64_t pad_size = std::min(end - starts[i + 1], starts[i + 1] - starts[i]); MatrixPtr mat = out_mat.subMatrix(starts[i + 1] - pad_size, pad_size); if (weight_mat) { MatrixPtr sub = const_cast<CpuMatrix&>(weight_mat) .subMatrix(begin_pad + context_start + j - pad_size, pad_size); mat->addAtOffset(*sub, j * input_mat.getWidth()); } dst_end = starts[i + 1] - pad_size; end = starts[i + 1]; } if (end <= begin) continue; MatrixPtr src = const_cast<CpuMatrix&>(input_mat).subMatrix(begin, end - begin); MatrixPtr dst = out_mat.subMatrix(dst_begin, dst_end - dst_begin); dst->addAtOffset(*src, j * input_mat.getWidth()); } } }
void FunctionApi<DEVICE_TYPE_CPU>(CpuMatrix& output, const CpuMatrix& input) { EXPECT_EQ(output.getHeight(), 100U); EXPECT_EQ(output.getWidth(), 200U); }