Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Runtime] Device API to query L2 cache size #15332

Merged
merged 5 commits into from
Jul 18, 2023
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
3 changes: 2 additions & 1 deletion include/tvm/runtime/device_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ enum DeviceAttrKind : int {
kMaxRegistersPerBlock = 9,
kGcnArch = 10,
kApiVersion = 11,
kDriverVersion = 12
kDriverVersion = 12,
kL2CacheSizeBytes = 13,
};

#ifdef TVM_KALLOC_ALIGNMENT
Expand Down
18 changes: 18 additions & 0 deletions python/tvm/_ffi/runtime_ctypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,24 @@ def driver_version(self):
"""
return self._GetDeviceAttr(self.device_type, self.device_id, 12)

@property
def l2_cache_size_bytes(self):
"""Return the size of the device L2 cache in bytes

Supported devices include CUDA/ROCM/OpenCL.

Returns
-------
l2_cache_size_bytes : int or None
The size of the device L2 cache in bytes returned by device runtime API.
Return None if the device does not support this feature.

Note
----
The value returned by opencl's API is smaller than actual device L2 cache size.
"""
return self._GetDeviceAttr(self.device_type, self.device_id, 13)

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

Expand Down
4 changes: 4 additions & 0 deletions python/tvm/target/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ def supports_cooperative_matrix(self):
def features(self):
return TargetFeatures(self)

@property
def l2_cache_size_bytes(self):
return int(self.attrs.get("l2_cache_size_bytes", 0))

def get_kind_attr(self, attr_name):
"""Get additional attribute about the target kind.

Expand Down
6 changes: 6 additions & 0 deletions src/runtime/cuda/cuda_device_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ class CUDADeviceAPI final : public DeviceAPI {
}
case kDriverVersion:
return;
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;
}
*rv = value;
}
Expand Down
2 changes: 2 additions & 0 deletions src/runtime/metal/metal_device_api.mm
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@
return;
case kDriverVersion:
return;
case kL2CacheSizeBytes:
return;
}
};
}
Expand Down
7 changes: 7 additions & 0 deletions src/runtime/opencl/opencl_device_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,13 @@ void OpenCLWorkspace::GetAttr(Device dev, DeviceAttrKind kind, TVMRetValue* rv)
*rv = std::string(value);
break;
}
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;
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/runtime/rocm/rocm_device_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ class ROCMDeviceAPI final : public DeviceAPI {
}
case kDriverVersion:
return;
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;
}
*rv = value;
}
Expand Down
3 changes: 3 additions & 0 deletions src/runtime/vulkan/vulkan_device_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ void VulkanDeviceAPI::GetAttr(Device dev, DeviceAttrKind kind, TVMRetValue* rv)
*rv = os.str();
break;
}

case kL2CacheSizeBytes:
break;
}
}

Expand Down