Repository navigation
Expand file tree
/
Copy pathds4_rocm_memory.h
More file actions
62 lines (53 loc) · 2.29 KB
/
Copy pathds4_rocm_memory.h
File metadata and controls
62 lines (53 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#ifndef DS4_ROCM_MEMORY_H
#define DS4_ROCM_MEMORY_H
#include <hip/hip_runtime.h>
#include "ds4_linux_memory.h"
static inline uint64_t ds4_rocm_model_arena_bytes(uint64_t need) {
/* Large tensor spans should not strand hundreds of MiB each. Small
* tensors still share an arena to avoid thousands of driver allocations. */
return need >= (256ull << 20) ? need : (1792ull << 20);
}
static inline bool ds4_rocm_uses_host_ram(void) {
int device;
hipDeviceProp_t prop;
return hipGetDevice(&device) == hipSuccess &&
hipGetDeviceProperties(&prop, device) == hipSuccess && prop.integrated;
}
static inline bool ds4_rocm_allocation_fits(size_t bytes, bool host = false) {
if (!host && !ds4_rocm_uses_host_ram()) return true;
uint64_t available;
const uint64_t reserve = 2ull << 30;
if (!ds4_linux_nonmovable_memory(&available)) {
fprintf(stderr, "ds4: ROCm cannot read usable host memory; allocation refused\n");
return false;
}
if (available >= reserve && bytes <= available - reserve) return true;
fprintf(stderr, "ds4: ROCm allocation refused: %.2f MiB requested, "
"%.2f GiB usable after excluding CMA, 2 GiB OS reserve\n",
(double)bytes / 1048576.0, (double)available / 1073741824.0);
return false;
}
template <typename T>
static inline hipError_t ds4_rocm_malloc(T **ptr, size_t bytes) {
if (!ds4_rocm_allocation_fits(bytes)) return hipErrorOutOfMemory;
return hipMalloc((void **)ptr, bytes);
}
template <typename T>
static inline hipError_t ds4_rocm_malloc_host(T **ptr, size_t bytes) {
if (!ds4_rocm_allocation_fits(bytes, true)) return hipErrorOutOfMemory;
return hipHostMalloc((void **)ptr, bytes);
}
template <typename T>
static inline hipError_t ds4_rocm_malloc_managed(T **ptr, size_t bytes) {
if (!ds4_rocm_allocation_fits(bytes, true)) return hipErrorOutOfMemory;
return hipMallocManaged((void **)ptr, bytes);
}
static inline hipError_t ds4_rocm_mem_get_info(size_t *free_b, size_t *total_b) {
hipError_t err = hipMemGetInfo(free_b, total_b);
if (err != hipSuccess || !ds4_rocm_uses_host_ram()) return err;
uint64_t available;
if (!ds4_linux_nonmovable_memory(&available)) return hipErrorUnknown;
if (available < *free_b) *free_b = (size_t)available;
return hipSuccess;
}
#endif