Spaces:
Sleeping
Sleeping
File size: 11,616 Bytes
6510698 |
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
"""
Unit tests for model configuration components.
"""
import pytest
import torch
from unittest.mock import patch, MagicMock
import os
import sys
from pathlib import Path
# Add project root to path
project_root = Path(__file__).parent.parent.parent
sys.path.insert(0, str(project_root))
from app.config.model_config import (
ModelConfig,
EnvironmentDetector,
DependencyValidator
)
class TestModelConfig:
"""Test ModelConfig dataclass."""
def test_model_config_creation(self):
"""Test ModelConfig creation with valid parameters."""
config = ModelConfig(
model_id="test/model",
revision="main",
dtype=torch.float32,
device_map="cpu",
attn_implementation="eager",
low_cpu_mem_usage=True,
trust_remote_code=True
)
assert config.model_id == "test/model"
assert config.revision == "main"
assert config.dtype == torch.float32
assert config.device_map == "cpu"
assert config.attn_implementation == "eager"
assert config.low_cpu_mem_usage is True
assert config.trust_remote_code is True
@patch('torch.cuda.is_available')
def test_is_gpu_available_true(self, mock_cuda):
"""Test GPU availability detection when CUDA is available."""
mock_cuda.return_value = True
config = ModelConfig(
model_id="test/model",
revision=None,
dtype=torch.float32,
device_map="auto",
attn_implementation="sdpa",
low_cpu_mem_usage=False,
trust_remote_code=True
)
assert config.is_gpu_available is True
@patch('torch.cuda.is_available')
def test_is_gpu_available_false(self, mock_cuda):
"""Test GPU availability detection when CUDA is not available."""
mock_cuda.return_value = False
config = ModelConfig(
model_id="test/model",
revision=None,
dtype=torch.float32,
device_map="cpu",
attn_implementation="eager",
low_cpu_mem_usage=True,
trust_remote_code=True
)
assert config.is_gpu_available is False
@patch('torch.cuda.is_available')
@patch('torch.cuda.device_count')
@patch('torch.cuda.current_device')
@patch('torch.cuda.get_device_name')
@patch('torch.cuda.memory_allocated')
@patch('torch.cuda.memory_reserved')
def test_device_info_gpu(self, mock_mem_reserved, mock_mem_allocated,
mock_device_name, mock_current_device,
mock_device_count, mock_cuda):
"""Test device info when GPU is available."""
mock_cuda.return_value = True
mock_device_count.return_value = 1
mock_current_device.return_value = 0
mock_device_name.return_value = "Test GPU"
mock_mem_allocated.return_value = 1024
mock_mem_reserved.return_value = 2048
config = ModelConfig(
model_id="test/model",
revision=None,
dtype=torch.float32,
device_map="auto",
attn_implementation="sdpa",
low_cpu_mem_usage=False,
trust_remote_code=True
)
device_info = config.device_info
assert device_info["cuda_available"] is True
assert device_info["device_count"] == 1
assert device_info["current_device"] == 0
assert device_info["device_name"] == "Test GPU"
assert device_info["memory_allocated"] == 1024
assert device_info["memory_reserved"] == 2048
@patch('torch.cuda.is_available')
def test_device_info_cpu(self, mock_cuda):
"""Test device info when only CPU is available."""
mock_cuda.return_value = False
config = ModelConfig(
model_id="test/model",
revision=None,
dtype=torch.float32,
device_map="cpu",
attn_implementation="eager",
low_cpu_mem_usage=True,
trust_remote_code=True
)
device_info = config.device_info
assert device_info["cuda_available"] is False
assert device_info["device_count"] == 0
assert device_info["current_device"] is None
class TestEnvironmentDetector:
"""Test EnvironmentDetector class."""
@patch('torch.cuda.is_available')
@patch('torch.__version__', '2.0.0')
def test_detect_environment_cpu(self, mock_cuda):
"""Test environment detection on CPU-only system."""
mock_cuda.return_value = False
with patch('importlib.import_module') as mock_import:
# Mock successful einops import
mock_einops = MagicMock()
mock_einops.__version__ = "0.7.0"
def import_side_effect(name):
if name == "einops":
return mock_einops
elif name == "flash_attn":
raise ImportError("No module named 'flash_attn'")
return MagicMock()
mock_import.side_effect = import_side_effect
env_info = EnvironmentDetector.detect_environment()
assert env_info["cuda_available"] is False
assert env_info["cuda_version"] is None
assert env_info["torch_version"] == "2.0.0"
@patch('torch.cuda.is_available')
@patch('torch.version.cuda', '11.8')
def test_detect_environment_gpu(self, mock_cuda):
"""Test environment detection on GPU system."""
mock_cuda.return_value = True
env_info = EnvironmentDetector.detect_environment()
assert env_info["cuda_available"] is True
assert env_info["cuda_version"] == "11.8"
@patch('torch.cuda.is_available')
@patch.dict(os.environ, {}, clear=True)
def test_create_model_config_cpu_defaults(self, mock_cuda):
"""Test model config creation with CPU defaults."""
mock_cuda.return_value = False
config = EnvironmentDetector.create_model_config()
assert config.model_id == "microsoft/Phi-3.5-MoE-instruct"
assert config.revision is None
assert config.dtype == torch.float32
assert config.device_map == "cpu"
assert config.attn_implementation == "eager"
assert config.low_cpu_mem_usage is True
assert config.trust_remote_code is True
@patch('torch.cuda.is_available')
@patch.dict(os.environ, {}, clear=True)
def test_create_model_config_gpu_defaults(self, mock_cuda):
"""Test model config creation with GPU defaults."""
mock_cuda.return_value = True
config = EnvironmentDetector.create_model_config()
assert config.model_id == "microsoft/Phi-3.5-MoE-instruct"
assert config.revision is None
assert config.dtype == torch.bfloat16
assert config.device_map == "auto"
assert config.attn_implementation == "sdpa"
assert config.low_cpu_mem_usage is False
assert config.trust_remote_code is True
@patch('torch.cuda.is_available')
@patch.dict(os.environ, {
'HF_MODEL_ID': 'custom/model',
'HF_REVISION': 'abc123'
})
def test_create_model_config_with_env_vars(self, mock_cuda):
"""Test model config creation with environment variables."""
mock_cuda.return_value = False
config = EnvironmentDetector.create_model_config()
assert config.model_id == "custom/model"
assert config.revision == "abc123"
def test_create_model_config_with_parameters(self):
"""Test model config creation with explicit parameters."""
config = EnvironmentDetector.create_model_config(
model_id="explicit/model",
revision="explicit123"
)
assert config.model_id == "explicit/model"
assert config.revision == "explicit123"
class TestDependencyValidator:
"""Test DependencyValidator class."""
def test_required_packages_list(self):
"""Test that required packages list is properly defined."""
required = DependencyValidator.REQUIRED_PACKAGES
assert "transformers" in required
assert "accelerate" in required
assert "einops" in required
assert "huggingface_hub" in required
assert "gradio" in required
assert "torch" in required
def test_optional_packages_list(self):
"""Test that optional packages list is properly defined."""
optional = DependencyValidator.OPTIONAL_PACKAGES
assert "flash_attn" in optional
@patch('importlib.import_module')
def test_validate_dependencies_all_available(self, mock_import):
"""Test dependency validation when all packages are available."""
mock_import.return_value = MagicMock()
results = DependencyValidator.validate_dependencies()
for package in DependencyValidator.REQUIRED_PACKAGES:
assert results[package] is True
for package in DependencyValidator.OPTIONAL_PACKAGES:
assert results[package] is True
@patch('importlib.import_module')
def test_validate_dependencies_some_missing(self, mock_import):
"""Test dependency validation when some packages are missing."""
def import_side_effect(name):
if name == "flash_attn":
raise ImportError(f"No module named '{name}'")
return MagicMock()
mock_import.side_effect = import_side_effect
results = DependencyValidator.validate_dependencies()
# Required packages should be available
for package in DependencyValidator.REQUIRED_PACKAGES:
assert results[package] is True
# flash_attn should be missing
assert results["flash_attn"] is False
@patch('importlib.import_module')
def test_get_missing_required_packages(self, mock_import):
"""Test getting list of missing required packages."""
def import_side_effect(name):
if name in ["transformers", "einops"]:
raise ImportError(f"No module named '{name}'")
return MagicMock()
mock_import.side_effect = import_side_effect
missing = DependencyValidator.get_missing_required_packages()
assert "transformers" in missing
assert "einops" in missing
assert "accelerate" not in missing # Should be available
@patch('importlib.import_module')
def test_is_environment_ready_true(self, mock_import):
"""Test environment readiness when all required packages are available."""
mock_import.return_value = MagicMock()
assert DependencyValidator.is_environment_ready() is True
@patch('importlib.import_module')
def test_is_environment_ready_false(self, mock_import):
"""Test environment readiness when required packages are missing."""
def import_side_effect(name):
if name == "einops":
raise ImportError(f"No module named '{name}'")
return MagicMock()
mock_import.side_effect = import_side_effect
assert DependencyValidator.is_environment_ready() is False
if __name__ == "__main__":
pytest.main([__file__])
|