hl_sequence.h 8.8 KB
Newer Older
Z
zhangjinchao01 已提交
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
/* Copyright (c) 2016 Baidu, Inc. All Rights Reserve.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#ifndef HL_SEQUENCE_H_
#define HL_SEQUENCE_H_

#include "hl_base.h"

/**
 * @brief   Maximum sequence forward.
 *
 * @param[in]   input           each sequence contains some instances.
 * @param[in]   sequence        sequence index..
 * @param[out]  output          max instance in this sequence.
 * @param[out]  index           index of max instance.
 * @param[in]   numSequences    size of sequence[in].
 * @param[in]   dim             input dimension.
 *
 */
extern void hl_max_sequence_forward(real* input,
                                    const int* sequence,
                                    real* output,
34
                                    int* index,
Z
zhangjinchao01 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47
                                    int numSequences,
                                    int dim);

/**
 * @brief   Maximum sequence backward.
 *
 * @param[in]   outputGrad      output gradient.
 * @param[in]   index           index of max instance.
 * @param[out]  inputGrad       input gradient.
 * @param[in]   numSequences    size of sequence[in].
 * @param[in]   dim             input dimension.
 *
 */
48 49
extern void hl_max_sequence_backward(
    real* outputGrad, int* index, real* inputGrad, int numSequences, int dim);
Z
zhangjinchao01 已提交
50 51 52 53 54 55 56 57 58 59 60 61

/**
 * @brief   Context projection forward.
 *
 * @param[in]   input           input sequence.
 * @param[in]   sequence        sequence index.
 * @param[in]   weightData      padding data.
 * @param[out]  output          output sequence.
 * @param[in]   numSequences    number of sequences.
 * @param[in]   inputDim        input sequence dimension.
 * @param[in]   contextLength   context length.
 * @param[in]   contextStart    context start.
62 63
 * @param[in]   beginPad        number of extra timesteps added at the
 * beginning.
Z
zhangjinchao01 已提交
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
 * @param[in]   isPadding       trainable padding.
 *
 */
extern void hl_context_projection_forward(real* input,
                                          const int* sequence,
                                          real* weightData,
                                          real* output,
                                          int numSequences,
                                          int inputDim,
                                          int contextLength,
                                          int contextStart,
                                          int beginPad,
                                          bool isPadding);

/**
 * @brief   Context projection backward data.
 *
 * @param[in]   outputGrad      output gradient.
 * @param[in]   sequence        sequence index.
 * @param[out]  inputGrad       input gradient.
 * @param[in]   numSequences    number of sequences.
 * @param[in]   inputDim        input sequence dimension.
 * @param[in]   contextLength   context length.
 * @param[in]   contextStart    context start.
 *
 */
extern void hl_context_projection_backward_data(real* outputGrad,
                                                const int* sequence,
                                                real* inputGrad,
                                                int numSequences,
                                                int inputDim,
                                                int contextLength,
                                                int contextStart);

/**
 * @brief   Context projection backward weight.
 *
 * @param[in]   outputGrad      output gradient.
 * @param[in]   sequence        sequence index.
 * @param[out]  weightGrad      weight gradient.
 * @param[in]   numSequences    number of sequences.
 * @param[in]   weightDim       input sequence dimension.
 * @param[in]   totalPad        number of extra timesteps.
 * @param[in]   contextLength   context length.
 * @param[in]   contextStart    context start.
109 110
 * @param[in]   beginPad        number of extra timesteps added at the
 * beginning.
Z
zhangjinchao01 已提交
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
 *
 */
extern void hl_context_projection_backward_weight(real* outputGrad,
                                                  const int* sequence,
                                                  real* weightGrad,
                                                  int numSequences,
                                                  int weightDim,
                                                  int totalPad,
                                                  int contextLength,
                                                  int contextStart,
                                                  int beginPad);

/**
 * @brief   Memory copy from sequence to batch.
 *
 * if seq2batch == true
 *
 *    copy from sequence to batch: batch[i] = sequence[batchIndex[i]].
 *
 * if seq2batch == false
 *
 *    copy from batch to sequence: sequence[batchIndex[i]] = batch[i].
 *
 * @param[in,out]   batch       batch matrix.
 * @param[in,out]   sequence    equence matrix.
 * @param[in]       batchIndex  index vector.
 * @param[in]       seqWidth    width of sequence.
 * @param[in]       batchCount  number of batchIndex.
 * @param[in]       seq2batch   copy direction.
 *
 */
142 143 144
extern void hl_sequence2batch_copy(real* batch,
                                   real* sequence,
                                   const int* batchIndex,
Z
zhangjinchao01 已提交
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
                                   int seqWidth,
                                   int batchCount,
                                   bool seq2batch);

/**
 * @brief   Add sequence to batch.
 *
 * if seq2batch == true
 *
 *    add sequence to batch: batch[i] = sequence[batchIndex[i]].
 *
 * if seq2batch == false
 *
 *    add batch to sequence: sequence[batchIndex[i]] = batch[i].
 *
 * @param[in,out]   batch       batch matrix.
 * @param[in,out]   sequence    equence matrix.
 * @param[in]       batchIndex  index vector.
 * @param[in]       seqWidth    width of sequence.
 * @param[in]       batchCount  number of batchIndex.
 * @param[in]       seq2batch   copy direction.
 *
 */
168 169 170
extern void hl_sequence2batch_add(real* batch,
                                  real* sequence,
                                  int* batchIndex,
Z
zhangjinchao01 已提交
171 172 173 174
                                  int seqWidth,
                                  int batchCount,
                                  bool seq2batch);

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
/**
 * @brief   Memory copy from sequence to batch,
 *          while padding all sequences to the same length.
 *
 * if seq2batch == true
 *
 *    copy from sequence to batch:
 *        batch[i] = sequence[sequenceStartPositions[i]]
 *
 * if seq2batch == false
 *
 *    copy from batch to sequence:
 *        sequence[sequenceStartPositions[i]] = batch[i]
 *
 * @param[in,out]   batch                   batch matrix.
 * @param[in,out]   sequence                sequence matrix.
 * @param[in]       sequenceStartPositions  index vector.
 * @param[in]       sequenceWidth           width of sequence.
 * @param[in]       maxSequenceLength       maximum length of sequences.
 * @param[in]       numSequences            number of sequences.
 * @param[in]       normByTimes             whether dividing sequence's length.
 * @param[in]       seq2batch               copy direction.
 *
 */
extern void hl_sequence2batch_copy_padding(real* batch,
                                           real* sequence,
                                           const int* sequenceStartPositions,
                                           const size_t sequenceWidth,
                                           const size_t maxSequenceLength,
                                           const size_t numSequences,
                                           bool normByTimes,
                                           bool seq2batch);

Z
zhangjinchao01 已提交
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
/**
 * @brief  dst = Op(src), src is sequence.
 *
 * mode = 0, Op is average.
 *
 * mode = 1, Op is sum.
 *
 * mode = 2, Op is sum(src)/sqrt(N), N is sequence length.
 *
 * @param[in,out]   dst       destination data.
 * @param[in]       src       source data.
 * @param[in]       starts    sequence start positions.
 * @param[in]       height    height of dst data.
 * @param[in]       width     width of dst data.
 * @param[in]       mode      0: avreage,
 *                            1: sum,
 *                            2: divide by square root
 *                            of sequenceLength
 */
extern void hl_sequence_avg_forward(real* dst,
                                    real* src,
                                    const int* starts,
                                    int height,
                                    int width,
                                    const int mode);

#endif /* HL_SEQUENCE_H_ */