Skip to content

Commit ea21e03

Browse files
authored
Revert "cuda: reset cuda context after reading memory size (#23935)" (#24715)
This reverts commit 0f7fada.
1 parent d5376cf commit ea21e03

1 file changed

Lines changed: 9 additions & 66 deletions

File tree

ggml/src/ggml-cuda/ggml-cuda.cu

Lines changed: 9 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -622,18 +622,6 @@ ggml_backend_cuda_context::~ggml_backend_cuda_context() {
622622

623623
// cuda buffer
624624

625-
struct ggml_backend_cuda_device_context {
626-
int device;
627-
std::string name;
628-
std::string description;
629-
std::string pci_bus_id;
630-
int op_offload_min_batch_size;
631-
#if !defined(GGML_USE_HIP) && !defined(GGML_USE_MUSA)
632-
std::mutex device_mutex;
633-
int active_count = 0;
634-
#endif // !defined(GGML_USE_HIP) && !defined(GGML_USE_MUSA)
635-
};
636-
637625
struct ggml_backend_cuda_buffer_context {
638626
int device;
639627
void * dev_ptr = nullptr;
@@ -651,13 +639,6 @@ struct ggml_backend_cuda_buffer_context {
651639

652640
static void ggml_backend_cuda_buffer_free_buffer(ggml_backend_buffer_t buffer) {
653641
ggml_backend_cuda_buffer_context * ctx = (ggml_backend_cuda_buffer_context *)buffer->context;
654-
655-
#if !defined(GGML_USE_HIP) && !defined(GGML_USE_MUSA)
656-
ggml_backend_cuda_device_context * dev_ctx = (ggml_backend_cuda_device_context *) buffer->buft->device->context;
657-
std::lock_guard<std::mutex> lock(dev_ctx->device_mutex);
658-
dev_ctx->active_count--;
659-
#endif // !defined(GGML_USE_HIP) && !defined(GGML_USE_MUSA)
660-
661642
delete ctx;
662643
}
663644

@@ -810,12 +791,6 @@ static ggml_backend_buffer_t ggml_backend_cuda_buffer_type_alloc_buffer(ggml_bac
810791

811792
ggml_backend_cuda_buffer_context * ctx = new ggml_backend_cuda_buffer_context(buft_ctx->device, dev_ptr);
812793

813-
#if !defined(GGML_USE_HIP) && !defined(GGML_USE_MUSA)
814-
ggml_backend_cuda_device_context * dev_ctx = (ggml_backend_cuda_device_context *) buft->device->context;
815-
std::lock_guard<std::mutex> lock(dev_ctx->device_mutex);
816-
dev_ctx->active_count++;
817-
#endif // !defined(GGML_USE_HIP) && !defined(GGML_USE_MUSA)
818-
819794
return ggml_backend_buffer_init(buft, ggml_backend_cuda_buffer_interface, ctx, size);
820795
}
821796

@@ -1515,12 +1490,6 @@ static bool ggml_backend_buft_is_cuda_host(ggml_backend_buffer_type_t buft) {
15151490
}
15161491

15171492
static void ggml_backend_cuda_host_buffer_free_buffer(ggml_backend_buffer_t buffer) {
1518-
#if !defined(GGML_USE_HIP) && !defined(GGML_USE_MUSA)
1519-
ggml_backend_cuda_device_context * dev_ctx = (ggml_backend_cuda_device_context *) buffer->buft->device->context;
1520-
std::lock_guard<std::mutex> lock(dev_ctx->device_mutex);
1521-
dev_ctx->active_count--;
1522-
#endif // !defined(GGML_USE_HIP) && !defined(GGML_USE_MUSA)
1523-
15241493
CUDA_CHECK(cudaFreeHost(buffer->context));
15251494
}
15261495

@@ -1529,8 +1498,6 @@ static void * ggml_cuda_host_malloc(size_t size) {
15291498
return nullptr;
15301499
}
15311500

1532-
ggml_cuda_set_device(0); // cudaMallocHost can create the implicit CUDA device context, make sure that this is consistently done on device 0.
1533-
15341501
void * ptr = nullptr;
15351502
cudaError_t err = cudaMallocHost((void **) &ptr, size);
15361503
if (err != cudaSuccess) {
@@ -1556,12 +1523,6 @@ static ggml_backend_buffer_t ggml_backend_cuda_host_buffer_type_alloc_buffer(ggm
15561523
buffer->buft = buft;
15571524
buffer->iface.free_buffer = ggml_backend_cuda_host_buffer_free_buffer;
15581525

1559-
#if !defined(GGML_USE_HIP) && !defined(GGML_USE_MUSA)
1560-
ggml_backend_cuda_device_context * dev_ctx = (ggml_backend_cuda_device_context *) buft->device->context;
1561-
std::lock_guard<std::mutex> lock(dev_ctx->device_mutex);
1562-
dev_ctx->active_count++;
1563-
#endif // !defined(GGML_USE_HIP) && !defined(GGML_USE_MUSA)
1564-
15651526
return buffer;
15661527
}
15671528

@@ -3179,12 +3140,6 @@ static const char * ggml_backend_cuda_get_name(ggml_backend_t backend) {
31793140
static void ggml_backend_cuda_free(ggml_backend_t backend) {
31803141
ggml_backend_cuda_context * cuda_ctx = (ggml_backend_cuda_context *)backend->context;
31813142

3182-
#if !defined(GGML_USE_HIP) && !defined(GGML_USE_MUSA)
3183-
ggml_backend_cuda_device_context * dev_ctx = (ggml_backend_cuda_device_context *) backend->device->context;
3184-
std::lock_guard<std::mutex> lock(dev_ctx->device_mutex);
3185-
dev_ctx->active_count--;
3186-
#endif // !defined(GGML_USE_HIP) && !defined(GGML_USE_MUSA)
3187-
31883143
delete cuda_ctx;
31893144
delete backend;
31903145
}
@@ -4916,6 +4871,14 @@ void ggml_backend_cuda_unregister_host_buffer(void * buffer) {
49164871

49174872
// backend device
49184873

4874+
struct ggml_backend_cuda_device_context {
4875+
int device;
4876+
std::string name;
4877+
std::string description;
4878+
std::string pci_bus_id;
4879+
int op_offload_min_batch_size;
4880+
};
4881+
49194882
static const char * ggml_backend_cuda_device_get_name(ggml_backend_dev_t dev) {
49204883
ggml_backend_cuda_device_context * ctx = (ggml_backend_cuda_device_context *)dev->context;
49214884
return ctx->name.c_str();
@@ -5004,11 +4967,6 @@ static bool ggml_backend_cuda_get_available_uma_memory(long * available_memory_k
50044967

50054968
static void ggml_backend_cuda_device_get_memory(ggml_backend_dev_t dev, size_t * free, size_t * total) {
50064969
ggml_backend_cuda_device_context * ctx = (ggml_backend_cuda_device_context *)dev->context;
5007-
5008-
#if !defined(GGML_USE_HIP) && !defined(GGML_USE_MUSA)
5009-
std::lock_guard<std::mutex> lock(ctx->device_mutex);
5010-
#endif // !defined(GGML_USE_HIP) && !defined(GGML_USE_MUSA)
5011-
50124970
ggml_cuda_set_device(ctx->device);
50134971
CUDA_CHECK(cudaMemGetInfo(free, total));
50144972

@@ -5035,13 +4993,6 @@ static void ggml_backend_cuda_device_get_memory(ggml_backend_dev_t dev, size_t *
50354993
}
50364994
#endif // defined(__linux__)
50374995

5038-
#if !defined(GGML_USE_HIP) && !defined(GGML_USE_MUSA)
5039-
// If no backends or buffers are active, the cudaMemGetInfo call above lazily created a CUDA
5040-
// context that permanently consumes VRAM. Reset the device to free it.
5041-
if (ctx->active_count == 0) {
5042-
CUDA_CHECK(cudaDeviceReset());
5043-
}
5044-
#endif // !defined(GGML_USE_HIP) && !defined(GGML_USE_MUSA)
50454996
}
50464997

50474998
static enum ggml_backend_dev_type ggml_backend_cuda_device_get_type(ggml_backend_dev_t dev) {
@@ -5745,21 +5696,13 @@ ggml_backend_t ggml_backend_cuda_init(int device) {
57455696
return nullptr;
57465697
}
57475698

5748-
ggml_backend_dev_t dev = ggml_backend_reg_dev_get(ggml_backend_cuda_reg(), device);
5749-
57505699
ggml_backend_t cuda_backend = new ggml_backend {
57515700
/* .guid = */ ggml_backend_cuda_guid(),
57525701
/* .iface = */ ggml_backend_cuda_interface,
5753-
/* .device = */ dev,
5702+
/* .device = */ ggml_backend_reg_dev_get(ggml_backend_cuda_reg(), device),
57545703
/* .context = */ ctx,
57555704
};
57565705

5757-
#if !defined(GGML_USE_HIP) && !defined(GGML_USE_MUSA)
5758-
ggml_backend_cuda_device_context * dev_ctx = (ggml_backend_cuda_device_context *) dev->context;
5759-
std::lock_guard<std::mutex> lock(dev_ctx->device_mutex);
5760-
dev_ctx->active_count++;
5761-
#endif // !defined(GGML_USE_HIP) && !defined(GGML_USE_MUSA)
5762-
57635706
return cuda_backend;
57645707
}
57655708

0 commit comments

Comments
 (0)