-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a sequence slice layer #3367
Add a sequence slice layer #3367
Conversation
ebcb685
to
b6b8c8e
Compare
f5d69ca
to
9e6e0a0
Compare
9e6e0a0
to
b97f020
Compare
// (with -1 as a special token). Storing indices information in real-typed | ||
// Matrix leads to converting real to int. This is very dangerous if a user | ||
// fills this matrix himself, invalid data may occur. | ||
// The selected indices should be stored in an int-typed matrix. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- 101行matrices是复数。
- real type, int type。
- 104行Matrix小写
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
real-typed 是构词法,等于形容词,这个也可以吧?不过这一段注释重新完善修改过。
// (with -1 as a special token). Storing indices information in real-typed | ||
// Matrix leads to converting real to int. This is very dangerous if a user | ||
// fills this matrix himself, invalid data may occur. | ||
// The selected indices should be stored in an int-typed matrix. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这段注释和KmaxSeqScoreLayer.cpp里的那段一样,保留一个就够了吧。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这些注释确实是一样的,多个文件都需统一改一下,但彼此又是独立的。
这里有一个real强制转int,比较危险,我建议还是留着。
void SequenceSliceLayer::checkInputs() { | ||
const Argument& inputSeq = getInput(0); | ||
CHECK(inputSeq.hasSeq()) << "The first input of sequence slic layer " | ||
<< "must be a sequence."; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
slic->slice笔误
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
CHECK_EQ(static_cast<size_t>(indices1->getHeight()), | ||
inputSeq.hasSubseq() ? inputSeq.getNumSubSequences() | ||
: inputSeq.getNumSequences()) | ||
<< "Height of the second input should be equal to number of sequence " |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The second input height should be equal to the number of sequences
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The second input height 这个也有语法问题吧,应该用of 或者 's 来表示所有格。
} else { | ||
startIdsOnCpu_ = nullptr; | ||
endIdsOnCpu_ = getInputValue(1); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
91行-97行:
startIdsOnCpu_ = config_.select_first() ? getInputValue(1) : nullptr;
endIdsOnCpu_ = config_.select_first() ? nullptr : getInputValue(1);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这样改的话,同一个条件需要判断两次条件,但关系不大。。。done.
if (starts) { | ||
if (starts->getElement(rowIdx, k) == -1.) break; | ||
} else if (ends->getElement(rowIdx, k) == -1.) | ||
break; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
156-159行,是判断两个break的条件,等价于?:
if (starts && starts->getElement(rowIdx, k) == -1.) break;
if (ends && ends->getElement(rowIdx, k) == -1.) break;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
改成短路逻辑了,done。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
会引入一次额外的逻辑判断,不过影响不大。。。
// (with -1 as a special token). Storing indices information in real-typed | ||
// Matrix leads to converting real to int. This is very dangerous if a user | ||
// fills this matrix himself, invalid data may occur. | ||
// The selected indices should be stored in an int-typed matrix. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这段注释和上面的也重复了
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里还是需要保存这一段注释,因为这几个文件是各自独立的。对注释的语法做了修改。
number of elements. | ||
|
||
If start or end indices contains more than one elements, the input sequence | ||
will be sliced for multiple times. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里最好给例子,特别来说明start和end有多余1个元素的情况下,应该怎么输入,输出是什么
if isinstance(starts, list): | ||
assert len(starts) == 1, ( | ||
'the start indices for sequence slice layer cannot ' | ||
'be a list having more than one element.') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我看layer.py里面写 If start or end indices contains more than one elements。
那这里又说只能是1个?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里是检测 starts 里面只给了一个layer,而这个layer 本身的 Argument.value 里面可以写多个要去裁切的位点。
if starts is not None: | ||
self.config.select_first = True | ||
else: | ||
self.config.select_first = False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2720-2723行
self.config.select_first = True if starts else False
rowIndice_->copyFrom(selectedRows_.data(), selectedRows_.size()); | ||
} else { | ||
rowIndice_ = | ||
IVector::create(selectedRows_.data(), selectedRows_.size(), useGpu_); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
180-185可以直接,cpu和gpu都相同吧:
rowIndice_ = IVector::create(selectedRows_.size(), useGpu_);
rowIndice_->copyFrom(selectedRows_.data(), selectedRows_.size());
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里是想CPU下面避免一次拷贝。如果改成一样,CPU下需要进行一次不必要的拷贝。
MatrixPtr inputSeqGrad = getInputGrad(0); | ||
MatrixPtr outputGrad = getOutputGrad(); | ||
|
||
outputGrad->addToRows(*inputSeqGrad, *rowIndice_); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这三行可以合并成一行:
getOutputGrad()->addToRows(*getInputGrad(0), *rowIndice_);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
follow comments.
// (with -1 as a special token). Storing indices information in real-typed | ||
// Matrix leads to converting real to int. This is very dangerous if a user | ||
// fills this matrix himself, invalid data may occur. | ||
// The selected indices should be stored in an int-typed matrix. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
real-typed 是构词法,等于形容词,这个也可以吧?不过这一段注释重新完善修改过。
void SequenceSliceLayer::checkInputs() { | ||
const Argument& inputSeq = getInput(0); | ||
CHECK(inputSeq.hasSeq()) << "The first input of sequence slic layer " | ||
<< "must be a sequence."; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
CHECK_EQ(static_cast<size_t>(indices1->getHeight()), | ||
inputSeq.hasSubseq() ? inputSeq.getNumSubSequences() | ||
: inputSeq.getNumSequences()) | ||
<< "Height of the second input should be equal to number of sequence " |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The second input height 这个也有语法问题吧,应该用of 或者 's 来表示所有格。
} else { | ||
startIdsOnCpu_ = nullptr; | ||
endIdsOnCpu_ = getInputValue(1); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这样改的话,同一个条件需要判断两次条件,但关系不大。。。done.
false /* trans */, | ||
false /* useGpu */); | ||
endIdsOnCpu_->copyFrom(*indices2); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不是一样,91 - 97 是 cpu下,105 - 138 行在gpu下会执行到。把cpu下的从这个函数中移除了。
if (starts) { | ||
if (starts->getElement(rowIdx, k) == -1.) break; | ||
} else if (ends->getElement(rowIdx, k) == -1.) | ||
break; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
改成短路逻辑了,done。
rowIndice_->copyFrom(selectedRows_.data(), selectedRows_.size()); | ||
} else { | ||
rowIndice_ = | ||
IVector::create(selectedRows_.data(), selectedRows_.size(), useGpu_); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里是想CPU下面避免一次拷贝。如果改成一样,CPU下需要进行一次不必要的拷贝。
MatrixPtr inputSeqGrad = getInputGrad(0); | ||
MatrixPtr outputGrad = getOutputGrad(); | ||
|
||
outputGrad->addToRows(*inputSeqGrad, *rowIndice_); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done.
// (with -1 as a special token). Storing indices information in real-typed | ||
// Matrix leads to converting real to int. This is very dangerous if a user | ||
// fills this matrix himself, invalid data may occur. | ||
// The selected indices should be stored in an int-typed matrix. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里还是需要保存这一段注释,因为这几个文件是各自独立的。对注释的语法做了修改。
if isinstance(starts, list): | ||
assert len(starts) == 1, ( | ||
'the start indices for sequence slice layer cannot ' | ||
'be a list having more than one element.') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里是检测 starts 里面只给了一个layer,而这个layer 本身的 Argument.value 里面可以写多个要去裁切的位点。
6bc93b3
to
377401f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
add a seq_slice_layer which will return one or several sub-sequences from the input sequence layer given start and end indices.