Skip to content

Commit 42465ca

Browse files
PerkzZhengazahed98
authored andcommitted
fix: swap tensor bug (NVIDIA#683)
1 parent 0db54f9 commit 42465ca

9 files changed

Lines changed: 24 additions & 6 deletions

src/fastertransformer/layers/TensorParallelGeluFfnLayer.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ void TensorParallelGeluFfnLayer<T>::forward(TensorMap* output_tensors,
4545
if (enable_custom_all_reduce_ && custom_all_reduce_comm_ != nullptr) {
4646
use_custom_all_reduce_kernel =
4747
custom_all_reduce_comm_->swapInternalBuffer(&swap_tensors, token_num * hidden_units);
48+
output_tensors->at("ffn_output").data = swap_tensors[0].data;
4849
}
4950

5051
GeluFfnLayer<T>::forward(output_tensors, input_tensors, ffn_weights);
@@ -57,6 +58,7 @@ void TensorParallelGeluFfnLayer<T>::forward(TensorMap* output_tensors,
5758
}
5859
else {
5960
custom_all_reduce_comm_->customAllReduce(token_num * hidden_units, GeluFfnLayer<T>::stream_);
61+
output_tensors->at("ffn_output").data = swap_tensors[0].data;
6062
}
6163
sync_check_cuda_error();
6264
}

src/fastertransformer/layers/TensorParallelReluFfnLayer.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ void TensorParallelReluFfnLayer<T>::forward(TensorMap* output_tensors,
4545
if (enable_custom_all_reduce_ && custom_all_reduce_comm_ != nullptr) {
4646
use_custom_all_reduce_kernel =
4747
custom_all_reduce_comm_->swapInternalBuffer(&swap_tensors, token_num * hidden_units);
48+
output_tensors->at("ffn_output").data = swap_tensors[0].data;
4849
}
4950

5051
ReluFfnLayer<T>::forward(output_tensors, input_tensors, ffn_weights);
@@ -57,6 +58,7 @@ void TensorParallelReluFfnLayer<T>::forward(TensorMap* output_tensors,
5758
}
5859
else {
5960
custom_all_reduce_comm_->customAllReduce(token_num * hidden_units, ReluFfnLayer<T>::stream_);
61+
output_tensors->at("ffn_output").data = swap_tensors[0].data;
6062
}
6163
sync_check_cuda_error();
6264
}

src/fastertransformer/layers/TensorParallelSiluFfnLayer.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ void TensorParallelSiluFfnLayer<T>::forward(TensorMap* output_tensors,
4444
if (enable_custom_all_reduce_ && custom_all_reduce_comm_ != nullptr) {
4545
use_custom_all_reduce_kernel =
4646
custom_all_reduce_comm_->swapInternalBuffer(&swap_tensors, token_num * hidden_units);
47+
output_tensors->at("ffn_output").data = swap_tensors[0].data;
4748
}
4849

4950
SiluFfnLayer<T>::forward(output_tensors, input_tensors, ffn_weights);
@@ -55,6 +56,7 @@ void TensorParallelSiluFfnLayer<T>::forward(TensorMap* output_tensors,
5556
}
5657
else {
5758
custom_all_reduce_comm_->customAllReduce(token_num * hidden_units, SiluFfnLayer<T>::stream_);
59+
output_tensors->at("ffn_output").data = swap_tensors[0].data;
5860
}
5961
sync_check_cuda_error();
6062
}

src/fastertransformer/layers/attention_layers/TensorParallelDecoderCrossAttentionLayer.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,12 @@ void TensorParallelDecoderCrossAttentionLayer<T>::forward(TensorMap*
104104
// value_cache [batch, head_num, max_seq_len, size_per_head]
105105

106106
const size_t size = output_tensors->at("hidden_features").size();
107+
std::vector<Tensor> reduce_tensor{output_tensors->at("hidden_features")};
107108

108109
bool use_custom_all_reduce_kernel = false;
109110
if (enable_custom_all_reduce_ && custom_all_reduce_comm_ != nullptr) {
110-
std::vector<Tensor> reduce_tensor{output_tensors->at("hidden_features")};
111111
use_custom_all_reduce_kernel = custom_all_reduce_comm_->swapInternalBuffer(&reduce_tensor, size);
112+
output_tensors->at("hidden_features").data = reduce_tensor[0].data;
112113
}
113114

114115
DecoderCrossAttentionLayer<T>::forward(output_tensors, input_tensors, attention_weights);
@@ -121,6 +122,7 @@ void TensorParallelDecoderCrossAttentionLayer<T>::forward(TensorMap*
121122
}
122123
else {
123124
custom_all_reduce_comm_->customAllReduce(size, DecoderCrossAttentionLayer<T>::stream_);
125+
output_tensors->at("hidden_features").data = reduce_tensor[0].data;
124126
}
125127
sync_check_cuda_error();
126128
}

src/fastertransformer/layers/attention_layers/TensorParallelDecoderSelfAttentionLayer.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,12 @@ void TensorParallelDecoderSelfAttentionLayer<T>::forward(TensorMap*
200200
// value_cache [batch, head_num, max_seq_len, size_per_head]
201201

202202
const size_t size = output_tensors->at("hidden_features").size();
203+
std::vector<Tensor> reduce_tensor{output_tensors->at("hidden_features")};
203204

204205
bool use_custom_all_reduce_kernel = false;
205206
if (enable_custom_all_reduce_ && custom_all_reduce_comm_ != nullptr && do_all_reduce_) {
206-
std::vector<Tensor> reduce_tensor{output_tensors->at("hidden_features")};
207207
use_custom_all_reduce_kernel = custom_all_reduce_comm_->swapInternalBuffer(&reduce_tensor, size);
208+
output_tensors->at("hidden_features").data = reduce_tensor[0].data;
208209
}
209210

210211
DecoderSelfAttentionLayer<T>::forward(output_tensors, input_tensors, attention_weights);
@@ -217,6 +218,7 @@ void TensorParallelDecoderSelfAttentionLayer<T>::forward(TensorMap*
217218
}
218219
else {
219220
custom_all_reduce_comm_->customAllReduce(size, DecoderSelfAttentionLayer<T>::stream_);
221+
output_tensors->at("hidden_features").data = reduce_tensor[0].data;
220222
}
221223
sync_check_cuda_error();
222224
}

src/fastertransformer/layers/attention_layers/TensorParallelDisentangledAttentionLayer.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@ void TensorParallelDisentangledAttentionLayer<T>::forward(TensorMap*
3535
// For more information, please refer to DisentangledAttentionLayer
3636

3737
const size_t size = output_tensors->at("hidden_features").size();
38+
std::vector<Tensor> hidden_features_reduce = {output_tensors->at("hidden_features")};
3839

3940
bool use_custom_all_reduce_kernel = false;
4041
if (enable_custom_all_reduce_ && custom_all_reduce_comm_ != nullptr) {
41-
std::vector<Tensor> hidden_features_reduce = {output_tensors->at("hidden_features")};
4242
use_custom_all_reduce_kernel = custom_all_reduce_comm_->swapInternalBuffer(&hidden_features_reduce, size);
43+
output_tensors->at("hidden_features").data = hidden_features_reduce[0].data;
4344
}
4445

4546
DisentangledAttentionLayer<T>::forward(output_tensors, input_tensors, attention_weights);
@@ -52,6 +53,7 @@ void TensorParallelDisentangledAttentionLayer<T>::forward(TensorMap*
5253
}
5354
else {
5455
custom_all_reduce_comm_->customAllReduce(size, DisentangledAttentionLayer<T>::stream_);
56+
output_tensors->at("hidden_features").data = hidden_features_reduce[0].data;
5557
}
5658
sync_check_cuda_error();
5759
}

src/fastertransformer/layers/attention_layers/TensorParallelGptContextAttentionLayer.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@ void TensorParallelGptContextAttentionLayer<T>::forward(TensorMap*
3535
// value_cache [batch, local_head_num, max_seq_len, size_per_head]
3636

3737
const size_t size = output_tensors->at("hidden_features").size();
38+
std::vector<Tensor> reduce_tensor{output_tensors->at("hidden_features")};
3839

3940
bool use_custom_all_reduce_kernel = false;
4041
if (do_all_reduce_ && enable_custom_all_reduce_ && custom_all_reduce_comm_ != nullptr) {
41-
std::vector<Tensor> reduce_tensor{output_tensors->at("hidden_features")};
4242
use_custom_all_reduce_kernel = custom_all_reduce_comm_->swapInternalBuffer(&reduce_tensor, size);
43+
output_tensors->at("hidden_features").data = reduce_tensor[0].data;
4344
}
4445

4546
GptContextAttentionLayer<T>::forward(output_tensors, input_tensors, attention_weights);
@@ -52,6 +53,7 @@ void TensorParallelGptContextAttentionLayer<T>::forward(TensorMap*
5253
}
5354
else {
5455
custom_all_reduce_comm_->customAllReduce(size, GptContextAttentionLayer<T>::stream_);
56+
output_tensors->at("hidden_features").data = reduce_tensor[0].data;
5557
}
5658
sync_check_cuda_error();
5759
}

src/fastertransformer/layers/attention_layers/TensorParallelUnfusedAttentionLayer.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@ void TensorParallelUnfusedAttentionLayer<T>::forward(TensorMap* o
3737
// For more information, please refer to UnfusedAttentionLayer
3838

3939
const size_t size = output_tensors->at("hidden_features").size();
40+
std::vector<Tensor> hidden_features_reduce = {output_tensors->at("hidden_features")};
4041

4142
bool use_custom_all_reduce_kernel = false;
4243
if (enable_custom_all_reduce_ && custom_all_reduce_comm_ != nullptr) {
43-
std::vector<Tensor> hidden_features_reduce = {output_tensors->at("hidden_features")};
4444
use_custom_all_reduce_kernel = custom_all_reduce_comm_->swapInternalBuffer(&hidden_features_reduce, size);
45+
output_tensors->at("hidden_features").data = hidden_features_reduce[0].data;
4546
}
4647

4748
UnfusedAttentionLayer<T>::forward(output_tensors, input_tensors, attention_weights);
@@ -53,6 +54,7 @@ void TensorParallelUnfusedAttentionLayer<T>::forward(TensorMap* o
5354
}
5455
else {
5556
custom_all_reduce_comm_->customAllReduce(size, UnfusedAttentionLayer<T>::stream_);
57+
output_tensors->at("hidden_features").data = hidden_features_reduce[0].data;
5658
}
5759
sync_check_cuda_error();
5860
}

src/fastertransformer/models/bert/Bert.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,10 +510,11 @@ void Bert<T>::forward(TensorMap* output_tensors, TensorMap* input_tensors, const
510510
Tensor{MEMORY_GPU, data_type, std::vector<size_t>{h_token_num, hidden_units_}, attn_out_buf_}}});
511511

512512
bool use_custom_all_reduce_kernel = false;
513+
std::vector<Tensor> hidden_features{attn_output_tensors.at("hidden_features")};
513514
if (enable_custom_all_reduce_ && custom_all_reduce_comm_ != nullptr) {
514-
std::vector<Tensor> hidden_features{attn_output_tensors.at("hidden_features")};
515515
use_custom_all_reduce_kernel =
516516
custom_all_reduce_comm_->swapInternalBuffer(&hidden_features, h_token_num * hidden_units_);
517+
attn_output_tensors.at("hidden_features").data = hidden_features[0].data;
517518
}
518519

519520
if (attention_type == AttentionType::FUSED_MHA || attention_type == AttentionType::FUSED_PADDED_MHA) {
@@ -535,6 +536,7 @@ void Bert<T>::forward(TensorMap* output_tensors, TensorMap* input_tensors, const
535536
}
536537
else {
537538
custom_all_reduce_comm_->customAllReduce(h_token_num * hidden_units_, stream_);
539+
attn_output_tensors.at("hidden_features").data = hidden_features[0].data;
538540
}
539541
sync_check_cuda_error();
540542
}

0 commit comments

Comments
 (0)