Spaces:
Running
Running
Aaryan Parik
fix: harden HF Space Docker runtime, session isolation, hardware profiles, and tests
0a7b128 Download test_local_inference.py from SnippyCodes/Libratio: direct link, hf CLI and curl.
- Browser
- Download file 3.1 kB
-
https://huggingface.co/spaces/SnippyCodes/Libratio/resolve/main/test_local_inference.py
- Command line
-
hf download hf://spaces/SnippyCodes/Libratio/test_local_inference.py
-
curl -L -o test_local_inference.py https://huggingface.co/spaces/SnippyCodes/Libratio/resolve/main/test_local_inference.py
3.1 kB
| """ | |
| Test suite for Local Arm64 Inference Engine. | |
| Verifies JSON parsing, task routing, and mock output structures. | |
| """ | |
| import os | |
| import unittest | |
| import json | |
| from environment.local_arm_engine import LocalArmInferenceEngine | |
| class TestLocalArmInferenceEngine(unittest.TestCase): | |
| def setUp(self): | |
| # Force mock mode by setting model path to "mock" | |
| self.engine = LocalArmInferenceEngine(model_path="mock") | |
| def test_mock_initialization(self): | |
| self.assertTrue(self.engine.is_mock) | |
| self.assertIsNone(self.engine.llm) | |
| def test_precision_task_generation(self): | |
| # Setup observation input | |
| obs = { | |
| "cluster": { | |
| "hardware_profile": "nvidia_h100", | |
| "vendor": "NVIDIA", | |
| "total_memory_gb": 320 | |
| }, | |
| "your_model": { | |
| "model_id": "model_a", | |
| "name": "LLaMA-3-13B" | |
| } | |
| } | |
| user_prompt = f"state:\n{json.dumps(obs)}\n\nProvide your action as JSON with precision_strategy." | |
| result = self.engine.generate_json("system_prompt", user_prompt) | |
| self.assertIn("precision_strategy", result) | |
| self.assertIn("reasoning", result) | |
| self.assertEqual(result["precision_strategy"]["ffn"], "FP8") | |
| def test_tpu_precision_task_generation(self): | |
| # Setup TPU observation input | |
| obs = { | |
| "cluster": { | |
| "hardware_profile": "google_tpu_v5p", | |
| "vendor": "Google", | |
| "total_memory_gb": 380 | |
| } | |
| } | |
| user_prompt = f"state:\n{json.dumps(obs)}\n\nProvide your action as JSON with precision_strategy." | |
| result = self.engine.generate_json("system_prompt", user_prompt) | |
| self.assertIn("precision_strategy", result) | |
| self.assertEqual(result["precision_strategy"]["ffn"], "INT8") | |
| self.assertIn("TPU detected", result["reasoning"]) | |
| def test_oversight_task_generation(self): | |
| user_prompt = "action_type: continue_monitoring or flag_instability. Provide JSON." | |
| result = self.engine.generate_json("system_prompt", user_prompt) | |
| self.assertIn("action_type", result) | |
| self.assertEqual(result["action_type"], "continue_monitoring") | |
| self.assertIsNone(result["flagged_model"]) | |
| def test_json_parsing_resilience(self): | |
| # Test cleaning raw output wrapped in markdown code blocks | |
| raw_markdown = "```json\n{\"precision_strategy\": {\"embedding\": \"FP32\"}, \"reasoning\": \"ok\"}\n```" | |
| parsed = self.engine._parse_json_from_text(raw_markdown) | |
| self.assertEqual(parsed.get("reasoning"), "ok") | |
| self.assertEqual(parsed.get("precision_strategy", {}).get("embedding"), "FP32") | |
| # Test parsing with trailing commas | |
| raw_trailing_comma = "{\"precision_strategy\": {\"embedding\": \"FP32\"},}" | |
| parsed = self.engine._parse_json_from_text(raw_trailing_comma) | |
| self.assertEqual(parsed.get("precision_strategy", {}).get("embedding"), "FP32") | |
| if __name__ == "__main__": | |
| unittest.main() | |