AirLLM 2.8T LLM inference with single 4GB GPU
Python chinese-llmchinese-nlpfinetunegenerative-aiinstruct-gptinstruction-setllamallmloraopen-modelsopen-sourceopen-source-modelsqlora
fix: accept PyTorch tokenizer inputs in MLX generate
Vaishnavi220506be9c45e50969
diff --git a/air_llm/airllm/airllm_llama_mlx.py b/air_llm/airllm/airllm_llama_mlx.pyindex e47a0bd..0490ddb 100644--- a/air_llm/airllm/airllm_llama_mlx.py+++ b/air_llm/airllm/airllm_llama_mlx.py@@ -182,6 +182,23 @@ def sample(logits, temperature=0):else:return mx.random.categorical(logits * (1 / temperature))++def _coerce_to_mlx_array(x):+ """Convert common tokenizer outputs to an MLX array for embedding lookup."""+ if isinstance(x, mx.array):+ return x++ try:+ import torch+ except ImportError:+ torch = None++ if torch is not None and isinstance(x, torch.Tensor):+ x = x.detach().cpu().numpy()++ return mx.array(x)++class AirLLMLlamaMlx:# customize layer names here@@ -250,6 +267,7 @@ class AirLLMLlamaMlx:def generate(self, x, temperature=0, max_new_tokens=None, **kwargs):+ x = _coerce_to_mlx_array(x)tokens = []for token in self.model_generate(x, temperature=temperature):tokens.append(token)@@ -434,4 +452,4 @@ class AirLLMLlamaMlx:gc.collect()self.record_memory('after_lmhead')- yield y\ No newline at end of file+ yield ydiff --git a/air_llm/tests/test_mlx_input_types.py b/air_llm/tests/test_mlx_input_types.pynew file mode 100644index 0000000..a3b3c5f--- /dev/null+++ b/air_llm/tests/test_mlx_input_types.py@@ -0,0 +1,41 @@+import sys+import unittest++[email protected](sys.platform == "darwin", "MLX is only available on macOS")+class TestMlxInputTypes(unittest.TestCase):+ def test_torch_tensor_is_converted_for_embedding_lookup(self):+ import mlx.core as mx+ import torch++ from ..airllm.airllm_llama_mlx import _coerce_to_mlx_array++ input_ids = torch.tensor([[1, 2, 3]], dtype=torch.long)+ converted = _coerce_to_mlx_array(input_ids)++ self.assertIsInstance(converted, mx.array)+ self.assertEqual(converted.shape, (1, 3))+ self.assertEqual(converted.tolist(), [[1, 2, 3]])++ def test_existing_mlx_array_is_reused(self):+ import mlx.core as mx++ from ..airllm.airllm_llama_mlx import _coerce_to_mlx_array++ input_ids = mx.array([[1, 2, 3]])++ self.assertIs(_coerce_to_mlx_array(input_ids), input_ids)++ def test_python_sequence_is_converted(self):+ import mlx.core as mx++ from ..airllm.airllm_llama_mlx import _coerce_to_mlx_array++ converted = _coerce_to_mlx_array([[1, 2, 3]])++ self.assertIsInstance(converted, mx.array)+ self.assertEqual(converted.tolist(), [[1, 2, 3]])+++if __name__ == "__main__":+ unittest.main()