RecurrentGradientMachine.h 18.4 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Z
zhangjinchao01 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

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. */

#pragma once

Y
Yu Yang 已提交
17
#include <functional>
Z
zhangjinchao01 已提交
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
#include "GradientMachine.h"
#include "NeuralNetwork.h"

#include "paddle/utils/Locks.h"

namespace paddle {

/**
 * Private data class declares.
 * Used for user customized beam search.
 */
class BeamSearchControlCallbacks;
class BeamSearchStatisticsCallbacks;

class RecurrentGradientMachine : public NeuralNetwork {
public:
  RecurrentGradientMachine(const std::string& subModelName,
                           NeuralNetwork* rootNetwork);

  // Disable copy and assign.
  RecurrentGradientMachine(const RecurrentGradientMachine& other) = delete;
  RecurrentGradientMachine& operator=(const RecurrentGradientMachine& other) =
      delete;

  virtual ~RecurrentGradientMachine() {
    this->removeBeamSearchStatisticsCallbacks();
    this->removeBeamSearchControlCallbacks();
  }

47 48
  virtual void init(const ModelConfig& config,
                    ParamInitCallback callback,
Z
zhangjinchao01 已提交
49 50 51 52 53 54
                    const std::vector<ParameterType>& parameterTypes,
                    bool useGpu);

  virtual void prefetch(const std::vector<Argument>& inArgs);

  virtual void forward(const std::vector<Argument>& inArgs,
55 56
                       std::vector<Argument>* outArgs,
                       PassType passType);
Z
zhangjinchao01 已提交
57 58 59 60

  virtual void backward(const UpdateCallback& callback = nullptr);

  void forwardBackward(const std::vector<Argument>& inArgs,
61 62
                       std::vector<Argument>* outArgs,
                       PassType passType,
Z
zhangjinchao01 已提交
63 64 65
                       const UpdateCallback& callback);

  virtual void resetState() {}
Y
Yu Yang 已提交
66
  virtual void eval(Evaluator* evaluator) const;
Z
zhangjinchao01 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

  const std::vector<int>& getParameterIds() { return parameterIds_; }

  /**
   * @brief BeamSearchCandidatesAdjustCallback
   *
   * Adjust searching candidates to restrict beam search
   * searching within a limited subset of all possibile paths.
   *
   * The first parameter is the prefixes of all formed paths in current
   * beam search step, whose type is basically int[][].
   *
   * The second parameter is a pointer to the network used to generate sequence,
   * user can use this pointer to tranverse each layer in the network to
   * modify behaivors of a particular layer.
   *
   * The third parameter is an integer to indicate the iteration number of
   * beam search, so that user can customize different operations in different
   * beam search iterations.
   */
87 88
  typedef std::function<void(
      const std::vector<std::vector<int>*>&, NeuralNetwork*, const int)>
Z
zhangjinchao01 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
      BeamSearchCandidatesAdjustCallback;

  /**
   * @brief DropCallback
   *
   * Drop a whole prefix or one candidate in beam search or not.
   *
   * The first parameter is sequence index in a batch
   *
   * The second parameter is one path in beam search,
   * which is made up of node indices.
   *
   * The third parameter is probabilites for each node in this path.
   *
   * Return true if this prefix or candidate is expected to be dropped.
   */
105 106 107
  typedef std::function<bool(
      int seqId, const std::vector<int>&, const std::vector<real>&)>
      DropCallback;
Z
zhangjinchao01 已提交
108 109

  /**
L
liaogang 已提交
110 111 112 113 114 115 116 117 118 119 120 121
   * @brief NormOrDropNodeCallback
   *
   * Normalize a path's probabilities or just drop it by modifying path.logProb
   *
   * The first parameter is sequence index in a batch
   *
   * The second parameter is path.ids
   *
   * The third parameter is probabilites for each node in this path.
   *
   * The fourth parameter is the probability of the whole path.
   */
122 123 124
  typedef std::function<void(
      int seqId, const std::vector<int>&, std::vector<real>&, real*)>
      NormOrDropNodeCallback;
Z
zhangjinchao01 已提交
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

  /**
   * @brief Register beam search control callbacks. Used for prediction.
   *
   * @param queryBeamSearch: Give the sequences already formed, return the
   * nodes expected to be expanded.
   * Input: A pointer to an array holding pathes which have been expanded
   * Return: A pointer to an array holding nodes wanted to be expanded.
   *
   * @param dropOneNode: Early drop a node in one beam search step.
   * Given the path formed and probability history, decide whether a node
   * should be dropped or not.
   *
   * @param stopBeamSearch: Early stop a path in one beam search step.
   * Given the path and probability history, decide whether a path
   * should be dropped or not.
   */
  void registerBeamSearchControlCallbacks(
      const BeamSearchCandidatesAdjustCallback& adjustBeamSearch,
      const NormOrDropNodeCallback& normOrDropNode,
      const DropCallback& stopBeamSearch);

  /**
   * @brief Remove user costumized beam search callbacks,
   *
   * make sequence generation acts like normal beam search.
   */
  void removeBeamSearchControlCallbacks();

  /**
   * @brief EachStepCallback
   *
   * Invoke with beam search step.
   */
  typedef std::function<void(int)> EachStepCallback;

  /**
   * @brief register statistics methods for performance profile of beam search.
   *
   * @param onEachStepStarted: invoke once a beam search step starts.
   * Its input is index of the beam search step.
   *
   * @param onEachStepStoped: invoke once a beam search step ends.
   * Its input is index of the beam search step.
   */
  void registerBeamSearchStatisticsCallbacks(
      const EachStepCallback& onEachStepStarted,
      const EachStepCallback& onEachStepStoped);

  /**
   * @brief Remove beam search callbacks.
   */
  void removeBeamSearchStatisticsCallbacks();

  /**
   * @brief Stop beam search for current source.
   *
   * Will restart beam search in the next forward
   */
  void stopBeamSearch();

  struct Path {
    /**
     * @brief ids, path of beam search.
     */
    std::vector<int> ids;

    /**
     * @brief logProb, current probability of path.
     */
    real logProb;

    int machineId;  // index of sample in frame
    int topIndex;   // index of MaxIdLayer output in one sample
199
    int seqId;      // index of sequence in batch generation
Z
zhangjinchao01 已提交
200 201 202 203 204 205 206 207 208 209 210 211 212
    std::vector<int> machineIdVec;

    /**
     * @brief A record of each node's probality in a formed path in beam search.
     *
     * @note  It could be empty when history is not recorded. If the history is
     *        wanted to be recorded, recordHistory() MUST be invoked first.
     */
    std::vector<real> probHistory;

    /**
     * @brief Path default ctor, first logProb is 0.
     */
213 214 215 216
    Path() {
      logProb = 0;
      seqId = 0;
    }
Z
zhangjinchao01 已提交
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
    explicit Path(size_t seqId) : seqId(seqId) { logProb = 0; }

    /**
     * @brief Create a new path based on an old path and
     * a new node with probability.
     *
     * @param old       old path
     * @param newId     index of the new node
     * @param logProb   probability of the new node.
     * @param machineId sample index of a frame in RNN
     * @param topIndex  index of MaxIdLayer output in one sample
     */
    Path(Path& old, int newId, real logProb, int machineId, int topIndex)
        : ids(old.ids),
          logProb(old.logProb + logProb),
          machineId(machineId),
          topIndex(topIndex),
          seqId(old.seqId) {
      ids.push_back(newId);
      if (!old.probHistory.empty()) {
        this->probHistory = old.probHistory;
        // probHistory store current prob, not sum
        this->probHistory.push_back(logProb);
      }
    }

    /**
     * @brief operator <
     *
     * Path a < Path b means log probability of a is smaller than that of b
     */
    bool operator<(const Path& other) const {
      return (logProb < other.logProb);
    }

    static bool greaterPath(const Path& a, const Path& b) { return (b < a); }

    /**
     * @brief Start recording history in this path.
     */
    void recordHistory() { this->probHistory.push_back(this->logProb); }

    /**
     * @brief Adjust probability for DIY beam search interface.
     * In normal situation, it will do nothing.
     *
     * @param calc_id: the object id for DIY beam search interface.
     * @param atEos: at end of sequence or not.
     */
    void adjustProb(int calc_id, bool atEos = false);

    /**
     * @brief isDropable indacating whether the current node will be
     * dropped or not in beam search.
     *
     * @note: if logProb is -inf, current node will be dropped.
     * @return true to drop the current node.
     */
    bool isDropable() const { return std::isinf(logProb) && logProb < 0; }
  };

  /**
   * @brief access beam search results.
   * @return beam search results.
   */
  const std::vector<std::vector<Path>>& getFinalPaths() const {
    return this->finalPaths_;
  }

protected:
287 288 289 290 291 292 293 294 295 296
  std::vector<Argument::SeqInfo> commonSeqInfo_;
  ICpuGpuVectorPtr sequenceStartPositions_;
  void calcSequenceStartPositions();
  void checkInputConsistency(int inlinkId,
                             const std::vector<Argument::SeqInfo>& seqInfo);
  void reorganizeInput(PassType passType);
  void reorganizeOutput(PassType passType);
  void connectFrames(PassType passType);
  void calcNumSequencesAtEachStep();

Z
zhangjinchao01 已提交
297 298 299 300 301 302 303 304 305 306 307
  void resizeOrCreateFrames(int numFrames);
  void resizeBootFrame(int numSequences);

  void generateSequence();
  void oneWaySearch(size_t batchSize);
  void beamSearch(size_t batchSize);

  struct InFrameLine {
    std::string linkName;
    LayerPtr inLayer;
    std::vector<LayerPtr> agents;  // Scatter Agents to reform batch input
308
    Argument outArg;               // scatter output argument
Z
zhangjinchao01 已提交
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 335 336
  };
  std::vector<InFrameLine> inFrameLines_;

  struct OutFrameLine {
    std::string layerName;
    LayerPtr agentLayer;
    std::vector<LayerPtr> frames;
  };
  std::vector<OutFrameLine> outFrameLines_;

  struct MemoryFrameLine {
    std::string layerName;
    std::string linkName;
    LayerPtr bootLayer;  // actually used biasLayer or rootAgent
    LayerPtr biasLayer;
    LayerPtr rootLayer;  // layer in root network to boot this memory
    LayerPtr rootAgent;  // agent to link rootLayer
    std::vector<LayerPtr> frames;
    std::vector<LayerPtr> agents;
    std::vector<LayerPtr> scatterAgents;  // scatter agent used by beam search
    Argument outArg;                      // scatter output argument
    // Different memoryFrameLine have different element as follows
    IVectorPtr allIds;  // scattered id of realLayer
    ICpuGpuVectorPtr
        sequenceStartPositions;  // scattered sequenceStartPositions
  };
  std::vector<MemoryFrameLine> memoryFrameLines_;

337 338 339
  // Each inFrameLines(inlinks) has its own info(elements) below,
  // and all outFrameLines(outlinks) share the info with one inFrameLine,
  // which is assigned by targetInfoInlinkId_.
Z
zhangjinchao01 已提交
340
  struct Info {
341 342 343 344 345 346 347
    // The original positions in the original batch
    IVectorPtr allIds;  // scattered id of realLayer [batchSize]

    // index of allIds for each step [maxSequenceLength_]
    // idIndex[i] is the total length of the first i sequences
    std::vector<int> idIndex;

Z
zhangjinchao01 已提交
348
    ICpuGpuVectorPtr
349
        sequenceStartPositions;         // scattered sequenceStartPositions
Z
zhangjinchao01 已提交
350 351
    std::vector<int> seqStartPosIndex;  // index of sequenceStartPositions
  };
352
  std::vector<Info> info_;  // for input
Z
zhangjinchao01 已提交
353

354 355
  // numSeqs_[i] is the number sequences which is longer than i (for sequence
  // data) or has more than i subsequences (for subsequence data)
356
  // Equivalently, numSeqs_[i] is the number of sequences at step i;
357 358
  std::vector<int> numSeqs_;

359
  std::vector<std::vector<Argument::SeqInfo>> seqInfos_;
Z
zhangjinchao01 已提交
360

361
  void checkOutputConsistency(OutFrameLine& outFrameLine);
362 363

  /* create scattered id infomation for all realLayer of inFrameLines one time.
L
liaogang 已提交
364 365 366
   *  If hasSubseq, will also create scattered sequenceStartPositions infomation
   *  for all realLayer of inFrameLines one time.
   */
367 368
  void createInFrameInfo(int inlinks_id,
                         const Argument& input,
369
                         PassType passType);
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
  void createInFrameInfo_nonseq(int inlinks_id,
                                const Argument& input,
                                PassType passType);
  void createInFrameInfo_seq(int inlinks_id,
                             const Argument& input,
                             PassType passType);
  void createInFrameInfo_subseq(int inlinks_id,
                                const Argument& input,
                                PassType passType);

  void createOutFrameInfo(OutFrameLine& outFrameLine,
                          Info& info,
                          ICpuGpuVectorPtr& sequenceStartPositions,
                          ICpuGpuVectorPtr& subSequenceStartPositions);
  void createOutFrameInfo_seq(OutFrameLine& outFrameLine,
                              Info& info,
                              ICpuGpuVectorPtr& sequenceStartPositions,
                              ICpuGpuVectorPtr& subSequenceStartPositions);
  void createOutFrameInfo_subseq(OutFrameLine& outFrameLine,
                                 Info& info,
                                 ICpuGpuVectorPtr& sequenceStartPositions,
                                 ICpuGpuVectorPtr& subSequenceStartPositions);
Z
zhangjinchao01 已提交
392 393 394 395 396 397

  void createMemoryFrameInfo(MemoryFrameLine* memoryFrameLine,
                             PassType passType);

  void copyScattedId(std::vector<int>& srcIds, IVectorPtr* dstIds, int size);

398 399 400 401
  void selectRowsOneTime(LayerPtr layer,
                         const IVectorPtr& allIds,
                         Argument* arg,
                         PassType passType);
Z
zhangjinchao01 已提交
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416

  void createSeqPos(const std::vector<int>& sequenceStartPosition,
                    ICpuGpuVectorPtr* sequenceStartPositions);

  // for generator
  struct EosFrameLine {
    std::vector<LayerPtr> layers;
  };
  std::unique_ptr<EosFrameLine> eosFrameLine_;

  struct Generator {
    GeneratorConfig config;
    std::vector<int> ids;  // store generated sequences
    Argument outArg;       // final output argument
  };
X
xuwei06 已提交
417
  bool generating_;
Z
zhangjinchao01 已提交
418 419 420 421 422 423
  Generator generator_;

  std::vector<std::unique_ptr<NeuralNetwork>> frames_;

  NeuralNetwork* rootNetwork_;
  bool reversed_;
424

425
  int maxSequenceLength_;  // Max top-level length
Z
zhangjinchao01 已提交
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
  bool useGpu_;
  bool stopBeamSearch_;

  std::vector<int>
      parameterIds_;  // parameters actually used by this Layer Group

  std::unique_ptr<Evaluator> evaluator_;  // frame printers in this layer group

  // store final argument of outFrameLines_
  std::vector<Argument> dataArgs_;
  // store each frame's output argument of outFrameLines_
  std::vector<std::vector<Argument>> dataArgsFrame_;
  size_t dataArgsSize_;  // size of dataArgs_ = size of dataArgsFrame_

  IVectorPtr cpuId_;
  MatrixPtr cpuProb_;
  IVectorPtr cpuEos_;

private:
  /*
   * @return beam size in beam search
   */
  size_t getBeamSize() { return generator_.config.beam_size(); }

  /*
   * @return number of sequence in a batch in generation
   */
  size_t getGenBatchSize();

  /*
   * @brief store output of the machineCur-th frame during generation, for
   * creating the final outlink after the entire generation process is finished.
   *
   * In generation, if the layer group has more than 1 outlink, the first
   * one is reserved to store the generated word indices, the others are data
   * outlinks, that can be used like a common layer in the network.
   *
   * @param machineCur : index to access the layer group frame in
   * currrent generation step.
   */
  void copyDataOutlinkFrame(size_t machineCur);

  /*
   * @brief In generation, if the layer group has more than 1 outlink, outlinks
   * except the first one are data outlinks. This function creates the data
   * outlinks.
   * @note In beam search, only one generated sequence with the hightest log
   * probabilites are retained.
   * @param machineIdVec : select a row of output matrix in each frame
   * that the generation process expanded.
   */
477
  void createDataOutlink(std::vector<int>& machineIdVec);
Z
zhangjinchao01 已提交
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503

  /*
   * @brief used in beam search, connect previous frame to form recurrent link
   * @param stepId : iteration number of generation process.
   * It equals to the length of longest half-generated sequence.
   * @param paths : half-generated paths that are going to be expanded
   * in current beam search iteration.
   */
  void connectPrevFrame(int stepId, std::vector<Path>& paths);

  /*
   * @brief used in beam search, forward current recurrent frame
   * @param machineCur : index to access the layer group frame in
   * currrent generation step.
   */
  void forwardFrame(int machineCur);

  /*
   * @brief reduce all expanded paths to beam size.
   *
   * @param newPaths : newPaths[totalExpandCount : ] stores all expanded paths
   * for the seqId-th sequence
   * @param seqId : sequence index in a batch
   * @param totalExpandCount : number of already shrinked paths in newPaths
   * @return size of retained paths at the end of a beam search iteration
   */
504 505
  size_t beamShrink(std::vector<Path>& newPaths,
                    size_t seqId,
Z
zhangjinchao01 已提交
506 507 508 509 510 511 512 513 514
                    size_t totalExpandCount);

  /*
   * @brief expand a single path to expandWidth new paths
   * with highest probability
   * @param curPath : path to be expanded
   * @param curPathId : index of curPath in member newPaths
   * @param expandWidth : number of paths to be expanded
   */
515 516 517 518
  void singlePathExpand(Path& curPath,
                        size_t curPathId,
                        std::vector<Path>& newPaths,
                        size_t expandWidth);
Z
zhangjinchao01 已提交
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545

  /*
   * @brief A new beam search iteration. Each half-generated paths in previous
   * beam search iteration are further expanded to beam_size new paths
   * with highest probabilities, and then all the expanded paths are again
   * reduced to beam_size paths according to their log probabilities.
   * @param paths : half-generated paths in previous iteration.
   * @param newPaths : paths expanded and then reduces in current iteration.
   */
  void beamExpand(std::vector<Path>& paths, std::vector<Path>& newPaths);

  /*
   * @brief fill sequence start positions and some other information that are
   * uesed by the "text_printer" evaluator.
   */
  void fillGenOutputs();

  std::vector<int> machineIds_;
  std::vector<int> topIds_;
  std::vector<int> seqIds_;
  std::vector<int> batchMachineIdVec_;
  std::vector<std::vector<Path>> finalPaths_;
  std::vector<real> minFinalPathLogProb_;
  BeamSearchControlCallbacks* beamSearchCtrlCallbacks_;
  BeamSearchStatisticsCallbacks* beamSearchStatistics_;
};
}  // namespace paddle