-
Notifications
You must be signed in to change notification settings - Fork 1.9k
fix(ocr): prevent native binary crash and handle AVX2 CPUs (RR-1325) #1376
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
e72d6dd
c6bff6c
7dbe7c9
4530b67
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| import os | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔴 bug: nothing runs pytest against the repo-root |
||
| import sys | ||
| import unittest | ||
| from unittest.mock import patch, mock_open, MagicMock | ||
|
|
||
| # Add depends.py directory to path | ||
| DEPENDS_DIR = os.path.abspath( | ||
| os.path.join(os.path.dirname(__file__), '..', 'lib') | ||
| ) | ||
| sys.path.append(DEPENDS_DIR) | ||
|
|
||
| # Dynamically stub engLib using a scoped pattern to prevent session pollution | ||
| def _load_depends_module(): | ||
| """Load depends.py as a standalone module.""" | ||
| saved_englib = sys.modules.get('engLib') | ||
| try: | ||
| sys.modules['engLib'] = MagicMock() | ||
| import depends | ||
| return depends | ||
| finally: | ||
| if saved_englib is None: | ||
| sys.modules.pop('engLib', None) | ||
| else: | ||
| sys.modules['engLib'] = saved_englib | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 risk: module-level |
||
| depends = _load_depends_module() | ||
|
|
||
|
|
||
| class TestDepends(unittest.TestCase): | ||
| def setUp(self): | ||
| # Clear lru_cache before tests so we can evaluate multiple mock states per test | ||
| depends._is_x86_64_missing_avx2.cache_clear() | ||
|
|
||
| @patch('depends.platform.machine') | ||
| @patch('depends.platform.system') | ||
| def test_missing_avx2_non_x86(self, mock_system, mock_machine): | ||
| # If architecture is ARM (e.g. apple silicon without rosetta), it should return False | ||
| # as it doesn't even need to check for AVX2 instructions | ||
| mock_machine.return_value = 'arm64' | ||
| self.assertFalse(depends._is_x86_64_missing_avx2()) | ||
|
|
||
| mock_machine.return_value = 'aarch64' | ||
| self.assertFalse(depends._is_x86_64_missing_avx2()) | ||
|
|
||
| @patch('depends.subprocess.check_output') | ||
| @patch('depends.platform.system') | ||
| @patch('depends.platform.machine') | ||
| def test_missing_avx2_darwin_rosetta(self, mock_machine, mock_system, mock_subprocess): | ||
| mock_machine.return_value = 'x86_64' | ||
| mock_system.return_value = 'Darwin' | ||
|
|
||
| # Test case: sysctl returns 0 (missing AVX2) | ||
| mock_subprocess.return_value = '0\n' | ||
| depends._is_x86_64_missing_avx2.cache_clear() | ||
| self.assertTrue(depends._is_x86_64_missing_avx2()) | ||
|
|
||
| # Test case: sysctl returns 1 (has AVX2) | ||
| mock_subprocess.return_value = '1\n' | ||
| depends._is_x86_64_missing_avx2.cache_clear() | ||
| self.assertFalse(depends._is_x86_64_missing_avx2()) | ||
|
|
||
| # Test case: sysctl fails (failsafe to True) | ||
| mock_subprocess.side_effect = Exception('sysctl failed') | ||
| depends._is_x86_64_missing_avx2.cache_clear() | ||
| self.assertTrue(depends._is_x86_64_missing_avx2()) | ||
|
|
||
| @patch('depends.open', new_callable=mock_open) | ||
| @patch('depends.platform.system') | ||
| @patch('depends.platform.machine') | ||
| def test_missing_avx2_linux(self, mock_machine, mock_system, mock_file): | ||
| mock_machine.return_value = 'x86_64' | ||
| mock_system.return_value = 'Linux' | ||
|
|
||
| # Test case: cpuinfo has avx2 | ||
| mock_file.return_value.read.return_value = 'flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l3 cdp_l3 invpcid_single pti intel_ppin ssbd mba ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm mpx rdt_a avx512f avx512dq rdseed adx smap clflushopt clwb intel_pt avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local dtherm ida arat pln pts pku ospke md_clear pconfig stibp_always_on flush_l1d arch_capabilities' | ||
| depends._is_x86_64_missing_avx2.cache_clear() | ||
| self.assertFalse(depends._is_x86_64_missing_avx2()) | ||
|
|
||
| # Test case: cpuinfo does not have avx2 | ||
| mock_file.return_value.read.return_value = 'flags: fpu vme de pse tsc' | ||
| depends._is_x86_64_missing_avx2.cache_clear() | ||
| self.assertTrue(depends._is_x86_64_missing_avx2()) | ||
|
|
||
| # Test case: file open fails | ||
| mock_file.side_effect = Exception('Permission denied') | ||
| depends._is_x86_64_missing_avx2.cache_clear() | ||
| self.assertTrue(depends._is_x86_64_missing_avx2()) | ||
|
|
||
| @patch.dict('sys.modules', {'ctypes': MagicMock()}) | ||
| @patch('depends.platform.system') | ||
| @patch('depends.platform.machine') | ||
| def test_missing_avx2_windows(self, mock_machine, mock_system): | ||
| mock_machine.return_value = 'AMD64' # Windows uses AMD64 | ||
| mock_system.return_value = 'Windows' | ||
|
|
||
| # Test case: kernel32.IsProcessorFeaturePresent(40) returns True | ||
| mock_ctypes = sys.modules['ctypes'] | ||
| mock_kernel32 = MagicMock() | ||
| mock_kernel32.IsProcessorFeaturePresent.return_value = True | ||
| mock_ctypes.windll.kernel32 = mock_kernel32 | ||
|
|
||
| depends._is_x86_64_missing_avx2.cache_clear() | ||
| self.assertFalse(depends._is_x86_64_missing_avx2()) | ||
|
|
||
| # Test case: returns False | ||
| mock_kernel32.IsProcessorFeaturePresent.return_value = False | ||
| depends._is_x86_64_missing_avx2.cache_clear() | ||
| self.assertTrue(depends._is_x86_64_missing_avx2()) | ||
|
|
||
| # Test case: ctypes fails | ||
| mock_ctypes.windll.kernel32.IsProcessorFeaturePresent.side_effect = Exception('Failed') | ||
| depends._is_x86_64_missing_avx2.cache_clear() | ||
| self.assertTrue(depends._is_x86_64_missing_avx2()) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔵 nit: the variant decision is invisible — when a user reports slow polars or an Illegal-instruction crash, there's no trace of which branch ran. Add one
debug(f'polars variant: {"lts-cpu" if missing_avx2 else "standard"} (avx2={not missing_avx2})')(call_is_x86_64_missing_avx2()once into a local). Also worth updating the docstring above, which still documents only theuv/onnxruntimeexcludes. Optional:@functools.lru_cacheon_is_x86_64_missing_avx2— it currently re-runssysctl/cpuinfo reads on every excludes write (twice perdepends()call across all nodes at startup).