Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/tvm/runtime/device_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ enum DeviceAttrKind : int {
kApiVersion = 11,
kDriverVersion = 12,
kL2CacheSizeBytes = 13,
kTotalGlobalMemory = 14,
};

#ifdef TVM_KALLOC_ALIGNMENT
Expand Down
14 changes: 14 additions & 0 deletions python/tvm/_ffi/runtime_ctypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,20 @@ def l2_cache_size_bytes(self):
"""
return self._GetDeviceAttr(self.device_type, self.device_id, 13)

@property
def total_global_memory(self):
"""Return size of the total global memory.

Supported devices include CUDA/ROCm/Metal/OpenCL.

Returns
-------
total_global_memory : int or None
Return the global memory available on device in bytes.
Return None if the device does not support this feature.
"""
return self._GetDeviceAttr(self.device_type, self.device_id, 14)

def texture_spatial_limit(self):
"""Returns limits for textures by spatial dimensions

Expand Down
10 changes: 9 additions & 1 deletion src/runtime/cuda/cuda_device_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,20 @@ class CUDADeviceAPI final : public DeviceAPI {
}
case kDriverVersion:
return;
case kL2CacheSizeBytes:
case kL2CacheSizeBytes: {
// Get size of device l2 cache size in bytes.
int l2_size = 0;
CUDA_CALL(cudaDeviceGetAttribute(&l2_size, cudaDevAttrL2CacheSize, dev.device_id));
*rv = l2_size;
return;
}
case kTotalGlobalMemory: {
cudaDeviceProp prop;
CUDA_CALL(cudaGetDeviceProperties(&prop, dev.device_id));
int64_t total_global_memory = prop.totalGlobalMem;
*rv = total_global_memory;
return;
}
}
*rv = value;
}
Expand Down
4 changes: 4 additions & 0 deletions src/runtime/metal/metal_device_api.mm
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@
return;
case kL2CacheSizeBytes:
return;
case kTotalGlobalMemory: {
*rv = static_cast<int64_t>([devices[dev.device_id] recommendedMaxWorkingSetSize]);
return;
}
}
};
}
Expand Down
10 changes: 9 additions & 1 deletion src/runtime/opencl/opencl_device_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,21 @@ void OpenCLWorkspace::GetAttr(Device dev, DeviceAttrKind kind, TVMRetValue* rv)
*rv = std::string(value);
break;
}
case kL2CacheSizeBytes:
case kL2CacheSizeBytes: {
// NOTE(Zihao): this API cannot reflect the real L2 cache size in both CUDA/AMD GPUs.
cl_ulong value;
OPENCL_CALL(clGetDeviceInfo(device_id, CL_DEVICE_GLOBAL_MEM_CACHE_SIZE, sizeof(value), &value,
nullptr));
*rv = static_cast<int64_t>(value);
break;
}
case kTotalGlobalMemory: {
cl_ulong total_global_memory;
OPENCL_CALL(clGetDeviceInfo(device_id, CL_DEVICE_GLOBAL_MEM_SIZE, sizeof(total_global_memory),
&total_global_memory, nullptr));
*rv = static_cast<int64_t>(total_global_memory);
return;
}
}
}

Expand Down
11 changes: 10 additions & 1 deletion src/runtime/rocm/rocm_device_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,20 @@ class ROCMDeviceAPI final : public DeviceAPI {
}
case kDriverVersion:
return;
case kL2CacheSizeBytes:
case kL2CacheSizeBytes: {
// Get size of device l2 cache size in bytes.
int l2_size;
ROCM_CALL(hipDeviceGetAttribute(&l2_size, hipDeviceAttributeL2CacheSize, device.device_id));
*rv = l2_size;
return;
}
case kTotalGlobalMemory: {
hipDeviceProp_t prop;
ROCM_CALL(hipGetDeviceProperties(&prop, device.device_id));
int64_t total_global_memory = prop.totalGlobalMem;
*rv = total_global_memory;
return;
}
}
*rv = value;
}
Expand Down
4 changes: 4 additions & 0 deletions src/runtime/vulkan/vulkan_device_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ void VulkanDeviceAPI::GetAttr(Device dev, DeviceAttrKind kind, TVMRetValue* rv)

case kL2CacheSizeBytes:
break;

case kTotalGlobalMemory: {
return;
}
}
}

Expand Down