Download test_enhanced_fallback.py from cong182/firstAI: direct link, hf CLI and curl.
- Browser
- Download file 3.36 kB
-
https://huggingface.co/spaces/cong182/firstAI/resolve/db8cd85abd6bedbbcf5f3f371400a8f749ff3d93/test_enhanced_fallback.py
- Command line
-
hf download hf://spaces/cong182/firstAI@db8cd85abd6bedbbcf5f3f371400a8f749ff3d93/test_enhanced_fallback.py
-
curl -L -o test_enhanced_fallback.py https://huggingface.co/spaces/cong182/firstAI/resolve/db8cd85abd6bedbbcf5f3f371400a8f749ff3d93/test_enhanced_fallback.py
3.36 kB
| #!/usr/bin/env python3 | |
| """ | |
| Test script to verify enhanced fallback mechanisms for pre-quantized models. | |
| This simulates the production deployment scenario where bitsandbytes package metadata is missing. | |
| """ | |
| import sys | |
| import logging | |
| import os | |
| # Set up logging | |
| logging.basicConfig(level=logging.INFO) | |
| logger = logging.getLogger(__name__) | |
| def test_pre_quantized_model_fallback(): | |
| """Test loading a pre-quantized model without bitsandbytes package metadata.""" | |
| logger.info("π§ͺ Testing enhanced fallback for pre-quantized models...") | |
| # Set the problematic model as environment variable | |
| os.environ["AI_MODEL"] = "unsloth/Mistral-Nemo-Instruct-2407-bnb-4bit" | |
| try: | |
| from backend_service import current_model, get_quantization_config | |
| from transformers import AutoConfig, AutoTokenizer, AutoModelForCausalLM | |
| logger.info(f"π Testing model: {current_model}") | |
| # Test quantization detection | |
| quant_config = get_quantization_config(current_model) | |
| if quant_config: | |
| logger.info(f"β Quantization config detected: {type(quant_config).__name__}") | |
| else: | |
| logger.info("π No quantization config (bitsandbytes not available)") | |
| # Test the enhanced fallback mechanism | |
| logger.info("π§ Testing enhanced config-based fallback...") | |
| try: | |
| # This simulates what happens in the lifespan function | |
| config = AutoConfig.from_pretrained(current_model, trust_remote_code=True) | |
| logger.info(f"β Successfully loaded config: {type(config).__name__}") | |
| # Check for quantization config in the model config | |
| if hasattr(config, 'quantization_config'): | |
| logger.info(f"π Found quantization_config in model config: {config.quantization_config}") | |
| # Remove it to prevent bitsandbytes errors | |
| config.quantization_config = None | |
| logger.info("π« Removed quantization_config from model config") | |
| else: | |
| logger.info("π No quantization_config found in model config") | |
| # Test tokenizer loading | |
| logger.info("π₯ Testing tokenizer loading...") | |
| tokenizer = AutoTokenizer.from_pretrained(current_model) | |
| logger.info(f"β Tokenizer loaded successfully: {len(tokenizer)} tokens") | |
| # Note: We won't actually load the full model in the test to save time/memory | |
| logger.info("β Enhanced fallback mechanism validated successfully!") | |
| return True | |
| except Exception as e: | |
| logger.error(f"β Enhanced fallback test failed: {e}") | |
| return False | |
| except Exception as e: | |
| logger.error(f"β Test setup failed: {e}") | |
| return False | |
| if __name__ == "__main__": | |
| logger.info("π Starting enhanced fallback mechanism test...") | |
| success = test_pre_quantized_model_fallback() | |
| if success: | |
| logger.info("\nπ Enhanced fallback test passed!") | |
| logger.info("π‘ The deployment should now handle pre-quantized models correctly") | |
| else: | |
| logger.error("\nβ Enhanced fallback test failed") | |
| sys.exit(0 if success else 1) | |