1+ import platform
2+
13import torch
24from loguru import logger
35from torch .utils .data import BatchSampler , DataLoader , Dataset
1820"""Registry for dataset classes."""
1921
2022
23+ def is_ram_cache_supported () -> bool :
24+ """Check if RAM cache is supported on the current platform.
25+
26+ Returns:
27+ bool: True if RAM cache is supported (Linux only), False otherwise.
28+ """
29+ return platform .system () == "Linux"
30+
31+
32+ def validate_ram_cache_config (use_ram_cache : bool , verbose : bool = True ) -> bool :
33+ """Validate RAM cache configuration for current platform.
34+
35+ Args:
36+ use_ram_cache: Whether RAM cache is requested.
37+ verbose: Whether to log warnings.
38+
39+ Returns:
40+ bool: True if RAM cache can be used, False otherwise.
41+ """
42+ if use_ram_cache and not is_ram_cache_supported ():
43+ if verbose :
44+ logger .warning (
45+ f"RAM cache is only supported on Linux. Current OS: { platform .system ()} . "
46+ "Disabling RAM cache."
47+ )
48+ return False
49+ return use_ram_cache
50+
51+
2152def build_dataset (
2253 cfg : DatasetConfig ,
2354 transforms : Compose | None ,
@@ -37,11 +68,14 @@ def build_dataset(
3768
3869 Raises:
3970 AssertionError: If RAM cache size exceeds available shared memory.
71+ RuntimeError: If RAM cache is requested on non-Linux systems.
4072 """
41- if use_ram_cache :
42- assert ram_cache_size_gb <= get_free_shm_size () / BYTES_PER_GIB , (
43- "RAM Cache size is too large"
44- )
73+ # Validate RAM cache configuration
74+ use_ram_cache = validate_ram_cache_config (use_ram_cache )
75+
76+ if use_ram_cache and ram_cache_size_gb is not None :
77+ if ram_cache_size_gb > get_free_shm_size () / BYTES_PER_GIB :
78+ raise RuntimeError ("RAM Cache size is too large" )
4579 cache = TensorCache (size_limit_gb = ram_cache_size_gb )
4680 logger .info (f"Use RAM Cache: { ram_cache_size_gb } GB" )
4781 else :
0 commit comments