File size: 5,118 Bytes
cb5d5f8 |
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 |
#!/usr/bin/env python3
"""
Test script to verify deployment fallback mechanisms work correctly.
"""
import sys
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def test_quantization_detection():
"""Test quantization detection logic without actual model loading."""
# Import the function we need
from backend_service import get_quantization_config
test_cases = [
# Standard models - should return None
("microsoft/DialoGPT-medium", None, "Standard model, no quantization"),
("deepseek-ai/DeepSeek-R1-0528-Qwen3-8B", None, "Standard model, no quantization"),
# Quantized models - should return quantization config
("unsloth/Mistral-Nemo-Instruct-2407-bnb-4bit", "quantized", "4-bit quantized model"),
("unsloth/DeepSeek-R1-0528-Qwen3-8B-GGUF", "quantized", "GGUF quantized model"),
("something-4bit-test", "quantized", "Generic 4-bit model"),
("test-bnb-model", "quantized", "BitsAndBytes model"),
]
results = []
logger.info("π§ͺ Testing quantization detection logic...")
logger.info("="*60)
for model_name, expected_type, description in test_cases:
logger.info(f"\nπ Testing: {model_name}")
logger.info(f" Expected: {description}")
try:
quant_config = get_quantization_config(model_name)
if expected_type is None:
# Should be None for standard models
if quant_config is None:
logger.info(f"β
PASS: No quantization detected (as expected)")
results.append((model_name, "PASS", "Correctly detected standard model"))
else:
logger.error(f"β FAIL: Unexpected quantization config: {quant_config}")
results.append((model_name, "FAIL", f"Unexpected quantization: {quant_config}"))
else:
# Should have quantization config
if quant_config is not None:
logger.info(f"β
PASS: Quantization detected: {quant_config}")
results.append((model_name, "PASS", f"Correctly detected quantization: {quant_config}"))
else:
logger.error(f"β FAIL: Expected quantization but got None")
results.append((model_name, "FAIL", "Expected quantization but got None"))
except Exception as e:
logger.error(f"β ERROR: Exception during test: {e}")
results.append((model_name, "ERROR", str(e)))
# Print summary
logger.info("\n" + "="*60)
logger.info("π QUANTIZATION DETECTION TEST SUMMARY")
logger.info("="*60)
pass_count = 0
for model_name, status, details in results:
if status == "PASS":
status_emoji = "β
"
pass_count += 1
elif status == "FAIL":
status_emoji = "β"
else:
status_emoji = "β οΈ"
logger.info(f"{status_emoji} {model_name}: {status}")
if status != "PASS":
logger.info(f" Details: {details}")
total_count = len(results)
logger.info(f"\nπ Results: {pass_count}/{total_count} tests passed")
if pass_count == total_count:
logger.info("π All quantization detection tests passed!")
return True
else:
logger.warning("β οΈ Some quantization detection tests failed")
return False
def test_imports():
"""Test that we can import required modules."""
logger.info("π§ͺ Testing imports...")
try:
from backend_service import get_quantization_config
logger.info("β
Successfully imported get_quantization_config")
# Test that transformers is available
from transformers import AutoTokenizer, AutoModelForCausalLM
logger.info("β
Successfully imported transformers")
# Test bitsandbytes import handling
try:
from transformers import BitsAndBytesConfig
logger.info("β
BitsAndBytesConfig import successful")
except ImportError as e:
logger.info(f"π BitsAndBytesConfig import failed (expected in some environments): {e}")
return True
except Exception as e:
logger.error(f"β Import test failed: {e}")
return False
if __name__ == "__main__":
logger.info("π Starting deployment fallback mechanism tests...")
# Test imports first
import_success = test_imports()
if not import_success:
logger.error("β Import tests failed, cannot continue")
sys.exit(1)
# Test quantization detection
quant_success = test_quantization_detection()
if quant_success:
logger.info("\nπ All deployment fallback tests passed!")
logger.info("π‘ Your deployment should handle quantized models gracefully")
sys.exit(0)
else:
logger.error("\nβ Some tests failed")
sys.exit(1)
|