mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-20 21:14:14 +08:00
Revert "Add DeviceAllocator as the base device allocator (#138222)"
This reverts commit f7a66da5f9f6b8b75119b1ee8ce9ddc23e15570e. Reverted https://github.com/pytorch/pytorch/pull/138222 on behalf of https://github.com/jithunnair-amd due to Broke ROCm periodic runs on MI300 e.g. https://github.com/pytorch/pytorch/actions/runs/16764977800/job/47470050573 ([comment](https://github.com/pytorch/pytorch/pull/138222#issuecomment-3164941815))
This commit is contained in:
@ -2,6 +2,7 @@
|
||||
#include <ATen/cuda/CUDAGraph.h>
|
||||
#include <ATen/cuda/Exceptions.h>
|
||||
#include <ATen/Functions.h>
|
||||
#include <c10/cuda/CUDACachingAllocator.h>
|
||||
#include <c10/cuda/CUDAFunctions.h>
|
||||
|
||||
#include <cstddef>
|
||||
|
@ -2,7 +2,6 @@
|
||||
|
||||
#include <ATen/Tensor.h>
|
||||
#include <c10/core/Device.h>
|
||||
#include <c10/cuda/CUDACachingAllocator.h>
|
||||
#include <c10/cuda/CUDAGraphsC10Utils.h>
|
||||
#include <c10/cuda/CUDAStream.h>
|
||||
#include <c10/util/flat_hash_map.h>
|
||||
|
@ -1,10 +0,0 @@
|
||||
#include <c10/core/CachingDeviceAllocator.h>
|
||||
|
||||
namespace c10 {
|
||||
|
||||
// Ensures proper DLL export of this pure virtual base class on Windows,
|
||||
// since it's mainly used in other DLLs outside c10.dll.
|
||||
DeviceAllocator::DeviceAllocator() = default;
|
||||
DeviceAllocator::~DeviceAllocator() = default;
|
||||
|
||||
} // namespace c10
|
@ -1,7 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <c10/core/Allocator.h>
|
||||
#include <c10/core/Stream.h>
|
||||
|
||||
namespace c10::CachingDeviceAllocator {
|
||||
|
||||
@ -60,55 +59,3 @@ struct DeviceStats {
|
||||
};
|
||||
|
||||
} // namespace c10::CachingDeviceAllocator
|
||||
|
||||
namespace c10 {
|
||||
|
||||
using CaptureId_t = unsigned long long;
|
||||
|
||||
// first is set if the instance is created by Graph mode capture_begin.
|
||||
// second is set if the instance is created by Graph mode graph_pool_handle.
|
||||
using MempoolId_t = std::pair<CaptureId_t, CaptureId_t>;
|
||||
|
||||
struct C10_API DeviceAllocator : public c10::Allocator {
|
||||
DeviceAllocator();
|
||||
~DeviceAllocator() override;
|
||||
|
||||
// Returns true if the allocator has been properly initialized and is ready
|
||||
// for use
|
||||
virtual bool initialized() = 0;
|
||||
|
||||
// Releases all cached device memory from the specified memory pool back to
|
||||
// the system
|
||||
virtual void emptyCache(MempoolId_t mempool_id = {0, 0}) = 0;
|
||||
|
||||
// Associates a memory allocation with a stream to establish dependency
|
||||
// tracking. Prevents memory reuse until all operations on the specified
|
||||
// stream complete
|
||||
virtual void recordStream(const DataPtr& ptr, c10::Stream stream) = 0;
|
||||
|
||||
// Retrieves comprehensive memory statistics for the specified device,
|
||||
// including allocation patterns, usage metrics
|
||||
virtual CachingDeviceAllocator::DeviceStats getDeviceStats(
|
||||
c10::DeviceIndex device) = 0;
|
||||
|
||||
// Resets cumulative allocation statistics for the specified device to zero
|
||||
virtual void resetAccumulatedStats(c10::DeviceIndex device) = 0;
|
||||
|
||||
// Resets peak memory usage statistics for the specified device
|
||||
virtual void resetPeakStats(c10::DeviceIndex device) = 0;
|
||||
};
|
||||
|
||||
// This function is used to get the DeviceAllocator for a specific device type
|
||||
// and keep backward compatibility with c10::GetAllocator.
|
||||
C10_API inline DeviceAllocator* getDeviceAllocator(const DeviceType& t) {
|
||||
TORCH_CHECK(
|
||||
t != DeviceType::CPU,
|
||||
"getDeviceAllocator is not supported for CPU device type.");
|
||||
auto* allocator = c10::GetAllocator(t);
|
||||
auto* device_allocator = dynamic_cast<DeviceAllocator*>(allocator);
|
||||
TORCH_INTERNAL_ASSERT(
|
||||
device_allocator, "Allocator for ", t, " is not a DeviceAllocator.");
|
||||
return device_allocator;
|
||||
}
|
||||
|
||||
} // namespace c10
|
||||
|
@ -4118,18 +4118,7 @@ struct BackendStaticInitializer {
|
||||
|
||||
BackendStaticInitializer() {
|
||||
auto r = parseEnvForBackend();
|
||||
// Register this HIP allocator as the CUDA allocator to allow it to work
|
||||
// with both c10::GetAllocator(kCUDA) and c10::getDeviceAllocator(kCUDA)
|
||||
// APIs. We don't perform this masquerading inside
|
||||
// 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"
|
||||
at::SetAllocator(c10::Device(HIP_MASQUERADING_AS_CUDA).type(), r, 0);
|
||||
allocator.store(r);
|
||||
#undef HIP_MASQUERADING_AS_CUDA
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -202,24 +202,25 @@ struct ShareableHandle {
|
||||
std::string handle;
|
||||
};
|
||||
|
||||
class CUDAAllocator : public DeviceAllocator {
|
||||
class CUDAAllocator : public Allocator {
|
||||
public:
|
||||
virtual void* raw_alloc(size_t nbytes) = 0;
|
||||
virtual void* raw_alloc_with_stream(size_t nbytes, cudaStream_t stream) = 0;
|
||||
virtual void raw_delete(void* ptr) = 0;
|
||||
virtual void init(int device_count) = 0;
|
||||
virtual bool initialized() = 0;
|
||||
virtual double getMemoryFraction(c10::DeviceIndex device) = 0;
|
||||
virtual void setMemoryFraction(double fraction, c10::DeviceIndex device) = 0;
|
||||
virtual void emptyCache(MempoolId_t mempool_id = {0, 0}) = 0;
|
||||
virtual void enable(bool value) = 0;
|
||||
virtual bool isEnabled() const = 0;
|
||||
virtual void cacheInfo(c10::DeviceIndex device, size_t* largestBlock) = 0;
|
||||
virtual void* getBaseAllocation(void* ptr, size_t* size) = 0;
|
||||
// Keep for BC only
|
||||
virtual void recordStream(const DataPtr& ptr, CUDAStream stream) = 0;
|
||||
void recordStream(const DataPtr& ptr, c10::Stream stream) override {
|
||||
CUDAStream cuda_stream = CUDAStream(stream);
|
||||
recordStream(ptr, cuda_stream);
|
||||
}
|
||||
virtual void recordStream(const DataPtr&, CUDAStream stream) = 0;
|
||||
virtual c10::CachingDeviceAllocator::DeviceStats getDeviceStats(
|
||||
c10::DeviceIndex device) = 0;
|
||||
virtual void resetAccumulatedStats(c10::DeviceIndex device) = 0;
|
||||
virtual void resetPeakStats(c10::DeviceIndex device) = 0;
|
||||
virtual SnapshotInfo snapshot(MempoolId_t mempool_id = {0, 0}) = 0;
|
||||
virtual void beginAllocateToPool(
|
||||
c10::DeviceIndex device,
|
||||
@ -524,10 +525,6 @@ inline void enablePeerAccess(
|
||||
|
||||
namespace c10::cuda {
|
||||
|
||||
// Keep BC only
|
||||
using c10::CaptureId_t;
|
||||
using c10::MempoolId_t;
|
||||
|
||||
// MemPool represents a pool of memory in a caching allocator. Currently,
|
||||
// it's just the ID of the pool object maintained in the CUDACachingAllocator.
|
||||
//
|
||||
|
@ -9,6 +9,12 @@
|
||||
|
||||
namespace c10::cuda {
|
||||
|
||||
using CaptureId_t = unsigned long long;
|
||||
|
||||
// first is set if the instance is created by CUDAGraph::capture_begin.
|
||||
// second is set if the instance is created by at::cuda::graph_pool_handle.
|
||||
using MempoolId_t = std::pair<CaptureId_t, CaptureId_t>;
|
||||
|
||||
// RAII guard for "cudaStreamCaptureMode", a thread-local value
|
||||
// that controls the error-checking strictness of a capture.
|
||||
struct C10_CUDA_API CUDAStreamCaptureModeGuard {
|
||||
|
@ -539,7 +539,7 @@ class DeviceCachingAllocator {
|
||||
|
||||
static void local_raw_delete(void* ptr);
|
||||
|
||||
class XPUAllocator : public DeviceAllocator {
|
||||
class XPUAllocator : public Allocator {
|
||||
private:
|
||||
std::mutex mutex;
|
||||
ska::flat_hash_map<void*, Block*> allocated_blocks;
|
||||
@ -575,10 +575,6 @@ class XPUAllocator : public DeviceAllocator {
|
||||
}
|
||||
}
|
||||
|
||||
bool initialized() override {
|
||||
return !device_allocators.empty();
|
||||
}
|
||||
|
||||
void malloc(
|
||||
void** devPtr,
|
||||
DeviceIndex device,
|
||||
@ -613,13 +609,13 @@ class XPUAllocator : public DeviceAllocator {
|
||||
}
|
||||
}
|
||||
|
||||
void emptyCache(MempoolId_t mempool_id [[maybe_unused]] = {0, 0}) override {
|
||||
void emptyCache() {
|
||||
for (auto& da : device_allocators) {
|
||||
da->emptyCache();
|
||||
}
|
||||
}
|
||||
|
||||
void recordStream(const DataPtr& ptr, c10::Stream stream) override {
|
||||
void recordStream(const DataPtr& ptr, XPUStream stream) {
|
||||
if (!ptr.get()) {
|
||||
return;
|
||||
}
|
||||
@ -629,8 +625,7 @@ class XPUAllocator : public DeviceAllocator {
|
||||
|
||||
Block* block = get_allocated_block(ptr.get());
|
||||
TORCH_CHECK(block, "No allocated block can be found.");
|
||||
c10::xpu::XPUStream xpu_stream{stream};
|
||||
device_allocators[block->device]->recordStream(block, xpu_stream);
|
||||
device_allocators[block->device]->recordStream(block, stream);
|
||||
}
|
||||
|
||||
DataPtr allocate(size_t size) override {
|
||||
@ -683,17 +678,17 @@ class XPUAllocator : public DeviceAllocator {
|
||||
": did you call init?");
|
||||
}
|
||||
|
||||
DeviceStats getDeviceStats(DeviceIndex device) override {
|
||||
DeviceStats getDeviceStats(DeviceIndex device) {
|
||||
assertValidDevice(device);
|
||||
return device_allocators[device]->getStats();
|
||||
}
|
||||
|
||||
void resetPeakStats(DeviceIndex device) override {
|
||||
void resetPeakStats(DeviceIndex device) {
|
||||
assertValidDevice(device);
|
||||
device_allocators[device]->resetPeakStats();
|
||||
}
|
||||
|
||||
void resetAccumulatedStats(DeviceIndex device) override {
|
||||
void resetAccumulatedStats(DeviceIndex device) {
|
||||
assertValidDevice(device);
|
||||
device_allocators[device]->resetAccumulatedStats();
|
||||
}
|
||||
|
Reference in New Issue
Block a user