ptrendx commented on a change in pull request #14627: Speed up SequenceReverse
URL: https://github.com/apache/incubator-mxnet/pull/14627#discussion_r272997237
##########
File path: src/operator/sequence_reverse-inl.h
##########
@@ -73,31 +73,28 @@ struct ReverseKernel {
const index_t batch_size,
const index_t other_dim, const index_t numel,
const IType *const indices) {
- for (index_t batch = 0; batch < batch_size; ++batch) {
- const index_t num_seq =
- indices ? static_cast<index_t>(indices[batch]) : max_seq_len;
- const index_t padded_periods = max_seq_len - num_seq;
- // padded part
- if (padded_periods > 0 && i < static_cast<int>(padded_periods)) {
- const int padded_in_offset =
- (i + num_seq) * batch_size * other_dim + batch * other_dim;
-
- for (index_t j = 0; j < other_dim; ++j) {
- KERNEL_ASSIGN(out_data[padded_in_offset + j], req,
- in_data[padded_in_offset + j]);
- }
- }
- // unpadded part
- if (i < static_cast<int>(num_seq)) {
- const int in_offset = i * batch_size * other_dim + batch * other_dim;
- const int out_offset =
- numel - (i + 1 + padded_periods) * batch_size * other_dim +
- batch * other_dim;
-
- for (index_t j = 0; j < other_dim; ++j) {
- KERNEL_ASSIGN(out_data[out_offset + j], req, in_data[in_offset + j]);
- }
- }
+ const index_t batch = i / (max_seq_len * other_dim);
+ const int id = (i / other_dim) % max_seq_len;
+ const index_t j = i % other_dim;
+ const index_t num_seq =
+ indices ? static_cast<index_t>(indices[batch]) : max_seq_len;
+ const index_t padded_periods = max_seq_len - num_seq;
+ // padded part
+ if (padded_periods > 0 && id < static_cast<int>(padded_periods)) {
+ const int padded_in_offset =
+ (id + num_seq) * batch_size * other_dim + batch * other_dim;
+
+ KERNEL_ASSIGN(out_data[padded_in_offset + j], req,
Review comment:
Done.
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services