@@ -47,104 +47,49 @@ bool CRFLayer::init(const LayerMap& layerMap,
4747 // We don't need sequenceStartPositions because each sample of output_ is
4848 // for the cost of one sequence.
4949 setNeedSequenceInfo (false );
50- if (useGpu_) {
51- tmpCpuInput_.reserve (inputLayers_.size ());
52- for (size_t i = 0 ; i < inputLayers_.size (); i++) {
53- tmpCpuInput_.push_back (Argument ());
54- }
55- }
50+
5651 return true ;
5752}
5853
5954void CRFLayer::forward (PassType passType) {
6055 Layer::forward (passType);
61- if (useGpu_) {
62- for (size_t i = 0 ; i < inputLayers_.size (); i++) {
63- tmpCpuInput_[i].resizeAndCopyFrom (getInput (i), false , HPPL_STREAM_1);
64- }
65- VectorPtr cpuParameterValue;
66- VectorPtr cpuParameterGradient;
67- cpuParameterValue =
68- Vector::create (parameter_->getBuf (PARAMETER_VALUE)->getSize (), false );
69- cpuParameterValue->
70- copyFrom (*parameter_->getBuf (PARAMETER_VALUE), HPPL_STREAM_1);
71- if (parameter_->getBuf (PARAMETER_GRADIENT)) {
72- cpuParameterGradient =
73- Vector::create (parameter_->getBuf (PARAMETER_GRADIENT)->getSize (),
74- false );
75- cpuParameterGradient->
76- copyFrom (*parameter_->getBuf (PARAMETER_GRADIENT), HPPL_STREAM_1);
77- } else {
78- cpuParameterGradient = nullptr ;
79- }
80- forwardImp (tmpCpuInput_[0 ], tmpCpuInput_[1 ], cpuParameterValue,
81- cpuParameterGradient);
82- parameter_->getBuf (PARAMETER_VALUE)->copyFrom (*cpuParameterValue,
83- HPPL_STREAM_1);
84- if (parameter_->getBuf (PARAMETER_GRADIENT)) {
85- parameter_->getBuf (PARAMETER_GRADIENT)->copyFrom (*cpuParameterGradient,
86- HPPL_STREAM_1);
87- }
88- } else {
89- forwardImp (getInput (0 ), getInput (1 ), parameter_->getBuf (PARAMETER_VALUE),
90- parameter_->getBuf (PARAMETER_GRADIENT));
91- }
92- }
9356
94- void CRFLayer::forwardImp ( const Argument&output,
95- const Argument& label,
96- VectorPtr parameterValue,
97- VectorPtr parameterGradient) {
57+ CHECK (!useGpu_) << " GPU is not supported " ;
58+
59+ const Argument& output = getInput ( 0 );
60+ const Argument& label = getInput ( 1 );
9861 CHECK (label.sequenceStartPositions );
9962 CHECK (label.ids );
10063
10164 int batchSize = output.getBatchSize ();
10265 size_t numSequences = label.sequenceStartPositions ->getSize () - 1 ;
10366 resizeOutput (numSequences, 1 );
104- std::vector<real> out (numSequences);
10567
10668 const int * starts = label.sequenceStartPositions ->getData (false );
10769 CHECK_EQ (starts[numSequences], batchSize);
108- VectorPtr cpuParameterValue;
109- VectorPtr cpuParameterGradient;
110-
11170
11271 for (size_t i = 0 ; i < numSequences; ++i) {
11372 if (i >= crfs_.size ()) {
11473 crfs_.emplace_back (numClasses_,
115- parameterValue ->getData (),
116- parameterGradient
117- ? parameterGradient ->getData ()
74+ parameter_-> getBuf (PARAMETER_VALUE) ->getData (),
75+ parameter_-> getBuf (PARAMETER_GRADIENT)
76+ ? parameter_-> getBuf (PARAMETER_GRADIENT) ->getData ()
11877 : nullptr );
11978 }
120- out [i] = crfs_[i].forward (
79+ output_. value -> getData () [i] = crfs_[i].forward (
12180 output.value ->getData () + numClasses_ * starts[i],
12281 label.ids ->getData () + starts[i], starts[i + 1 ] - starts[i]);
12382 }
124- output_. value -> copyFrom (out. data (), numSequences);
83+
12584 if (weightLayer_) {
12685 const MatrixPtr& weight = getInputValue (*weightLayer_);
12786 getOutputValue ()->dotMul (*getOutputValue (), *weight);
12887 }
12988}
13089
13190void CRFLayer::backward (const UpdateCallback &callback) {
132- (void )callback;
133- if (useGpu_) {
134- backwardImp (callback, tmpCpuInput_[0 ], tmpCpuInput_[1 ]);
135- const_cast <Argument&>(getInput (0 )).
136- resizeAndCopyFrom (tmpCpuInput_[0 ], true , HPPL_STREAM_1);
137- const_cast <Argument&>(getInput (1 )).
138- resizeAndCopyFrom (tmpCpuInput_[1 ], true , HPPL_STREAM_1);
139-
140- } else {
141- backwardImp (callback, getInput (0 ), getInput (1 ));
142- }
143- }
144-
145- void CRFLayer::backwardImp (const UpdateCallback& callback,
146- const Argument&output,
147- const Argument& label) {
91+ const Argument& output = getInput (0 );
92+ const Argument& label = getInput (1 );
14893 const int * starts = label.sequenceStartPositions ->getData (false );
14994 int numSequences = label.sequenceStartPositions ->getSize () - 1 ;
15095
@@ -159,9 +104,11 @@ void CRFLayer::backwardImp(const UpdateCallback& callback,
159104 grad->mulScalar (weight);
160105 }
161106 }
107+
162108 if (coeff_ != real (1 .0f )) {
163109 output.grad ->mulScalar (coeff_);
164110 }
111+
165112 parameter_->incUpdate (callback);
166113}
167114
0 commit comments