Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦
C
fix(convert): reject fmt2 output aliases of the source directory
Rudy Celeklidf7cd6765529
diff --git a/c/tests/test_fmt2_output_guard.py b/c/tests/test_fmt2_output_guard.pynew file mode 100644index 00000000..dfa6b6ac--- /dev/null+++ b/c/tests/test_fmt2_output_guard.py@@ -0,0 +1,51 @@+import importlib.util+import json+import os+from pathlib import Path+import struct+import sys+import tempfile+import types+import unittest+from unittest import mock++TOOLS = Path(__file__).resolve().parents[1] / 'tools'+sys.path.insert(0, str(TOOLS))+spec = importlib.util.spec_from_file_location('fmt2_guard_test', TOOLS / 'convert_fmt4_to_fmt2.py')+converter = importlib.util.module_from_spec(spec)+with mock.patch.dict(sys.modules, {'numpy': types.ModuleType('numpy')}):+ spec.loader.exec_module(converter)++class Fmt2OutputGuardTest(unittest.TestCase):+ def test_existing_directory_alias_is_rejected_before_reading_or_writing(self):+ with tempfile.TemporaryDirectory() as directory:+ source = Path(directory, 'source')+ source.mkdir()+ alias = Path(directory, 'alias')+ try:+ alias.symlink_to(source, target_is_directory=True)+ except NotImplementedError as error:+ self.skipTest(str(error))+ except OSError as error:+ if os.name == 'nt' and getattr(error, 'winerror', None) == 1314:+ self.skipTest(str(error))+ raise+ config = source / 'config.json'+ config.write_text('{"hidden_size": 128, "vocab_size": 2}')+ # Valid grouped-int4 tensor: 128 packed bytes + four f32 scales.+ header = json.dumps({'model.embed_tokens.weight': {'dtype': 'U8', 'shape': [128], 'data_offsets': [0, 128]},+ 'model.embed_tokens.weight.qs': {'dtype': 'F32', 'shape': [4], 'data_offsets': [128, 144]}}).encode()+ header += b' ' * (-len(header) % 8)+ shard = source / 'model-00001.safetensors'+ original = struct.pack('<Q', len(header)) + header + bytes([0x99]) * 128 + struct.pack('<4f', 1, 1, 1, 1)+ shard.write_bytes(original)+ for output in (source, alias):+ with self.subTest(output=output), \+ mock.patch.object(sys, 'argv', ['converter', '--indir', str(source), '--outdir', str(output), '--workers', '1']), \+ mock.patch.object(converter, 'plan_shard', side_effect=AssertionError('in-place conversion reached shard planning')) as plan:+ with self.assertRaisesRegex(SystemExit, '--indir and --outdir must differ'):+ converter.main()+ plan.assert_not_called()+ self.assertEqual(shard.read_bytes(), original)+ self.assertEqual(config.read_text(), '{"hidden_size": 128, "vocab_size": 2}')+ self.assertFalse(Path(str(shard) + '.tmp').exists())diff --git a/c/tools/convert_fmt4_to_fmt2.py b/c/tools/convert_fmt4_to_fmt2.pyindex 8cca6960..998588e5 100644--- a/c/tools/convert_fmt4_to_fmt2.py+++ b/c/tools/convert_fmt4_to_fmt2.py@@ -309,7 +309,7 @@ def main():if not a.indir or not a.outdir:ap.error("--indir and --outdir are required")a.outdir = os.path.expanduser(a.outdir)- if os.path.abspath(a.indir) == os.path.abspath(a.outdir):+ if os.path.realpath(a.indir) == os.path.realpath(a.outdir):die("--indir and --outdir must differ (in-place conversion would corrupt the source)")cfg_path = os.path.join(a.indir, "config.json")if not os.path.exists(cfg_path):