Replace insert with std::rotate_copy for RingBuffer (#165348)

Pull Request resolved: https://github.com/pytorch/pytorch/pull/165348
Approved by: https://github.com/eqy, https://github.com/Skylion007
This commit is contained in:
Lakshay Garg 2025-10-14 05:11:25 +00:00 committed by PyTorch MergeBot
parent 33bfec27ff
commit 496adf9f9c

View File

@ -1080,19 +1080,12 @@ class RingBuffer {
void getEntries(std::vector<T>& result) const {
std::lock_guard<std::mutex> lk(alloc_trace_lock);
result.reserve(alloc_trace->size());
result.insert(
result.end(),
alloc_trace->begin() +
static_cast<typename std::vector<T>::difference_type>(
alloc_trace_next),
alloc_trace->end());
result.insert(
result.end(),
result.reserve(result.size() + alloc_trace->size());
std::rotate_copy(
alloc_trace->begin(),
alloc_trace->begin() +
static_cast<typename std::vector<T>::difference_type>(
alloc_trace_next));
std::next(alloc_trace->begin(), alloc_trace_next),
alloc_trace->end(),
std::back_inserter(result));
}
void clear() {
@ -4466,10 +4459,7 @@ struct BackendStaticInitializer {
if (kv[0] == "backend") {
#ifdef USE_ROCM
// convenience for ROCm users to allow either CUDA or HIP env var
if (kv[1] ==
"cud"
"aMallocAsync" ||
kv[1] == "hipMallocAsync")
if (kv[1] == "cudaMallocAsync" || kv[1] == "hipMallocAsync")
#else
if (kv[1] == "cudaMallocAsync")
#endif
@ -4491,9 +4481,7 @@ struct BackendStaticInitializer {
// HIPAllocatorMasqueradingAsCUDA because it needs to happen during static
// initialization, and doing so there may introduce static initialization
// order (SIOF) issues.
#define HIP_MASQUERADING_AS_CUDA \
"cud" \
"a"
#define HIP_MASQUERADING_AS_CUDA "cuda"
at::SetAllocator(c10::Device(HIP_MASQUERADING_AS_CUDA).type(), r, 0);
allocator.store(r);
#undef HIP_MASQUERADING_AS_CUDA