|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +import logging |
| 8 | +import unittest |
| 9 | + |
| 10 | +import torch |
| 11 | +from executorch.examples.models.lfm2_5_vl.export_lfm2_5_vl import export_all |
| 12 | +from executorch.examples.models.lfm2_5_vl.model import IMAGE_SIZE, MAX_SEQ_LEN, Lfm2p5VlModel |
| 13 | + |
| 14 | +# import order matters: portable_lib must come first so its static op registry |
| 15 | +# is in place before custom_ops registers against it. |
| 16 | +from executorch.extension.pybindings.portable_lib import ( # noqa # usort: skip |
| 17 | + _load_for_executorch_from_buffer, |
| 18 | +) |
| 19 | +from executorch.extension.llm.custom_ops import custom_ops # noqa # usort: skip |
| 20 | +from executorch.kernels import quantized # noqa # usort: skip |
| 21 | + |
| 22 | +logging.basicConfig(level=logging.INFO) |
| 23 | +logger = logging.getLogger(__name__) |
| 24 | + |
| 25 | +MODEL_DIR = "LiquidAI/LFM2-VL-1.6B" |
| 26 | + |
| 27 | + |
| 28 | +class TestLfm2p5Vl(unittest.TestCase): |
| 29 | + @classmethod |
| 30 | + def setUpClass(cls): |
| 31 | + cls.lfm2_model = Lfm2p5VlModel(model_dir=MODEL_DIR) |
| 32 | + cls.lfm2 = cls.lfm2_model.get_eager_model().eval() |
| 33 | + |
| 34 | + def test_vision_encoder_shape(self): |
| 35 | + """Vision encoder must produce [1, 256, 2048] embeddings.""" |
| 36 | + pixels = torch.randint(0, 256, (1, 3, IMAGE_SIZE, IMAGE_SIZE), dtype=torch.float32) |
| 37 | + with torch.no_grad(): |
| 38 | + embeds = self.lfm2.image_embedding(pixels) |
| 39 | + self.assertEqual(embeds.shape, (1, 256, 2048)) |
| 40 | + |
| 41 | + def test_prefill_output_shape(self): |
| 42 | + """Prefill must return (seq_len: int, logits [1, vocab_size]).""" |
| 43 | + prompt_before, pixels, prompt_after = self.lfm2_model.get_inputs_for_prefill() |
| 44 | + with torch.no_grad(): |
| 45 | + seq_len, logits = self.lfm2.prefill(prompt_before, pixels, prompt_after) |
| 46 | + self.assertIsInstance(seq_len, int) |
| 47 | + self.assertEqual(logits.shape[-1], 65536) |
| 48 | + |
| 49 | + def test_export_methods(self): |
| 50 | + """Exported PTE must contain the three named methods and metadata.""" |
| 51 | + et_program = export_all( |
| 52 | + model_dir=MODEL_DIR, |
| 53 | + output=None, # in-memory only |
| 54 | + _return_program=True, |
| 55 | + ) |
| 56 | + self.assertIn("vision_encoder", et_program.methods) |
| 57 | + self.assertIn("token_embedding", et_program.methods) |
| 58 | + self.assertIn("text_decoder", et_program.methods) |
| 59 | + |
| 60 | + def test_export_and_run(self): |
| 61 | + """Export to PTE and run a short prefill + decode loop end-to-end.""" |
| 62 | + et_program = export_all( |
| 63 | + model_dir=MODEL_DIR, |
| 64 | + output=None, |
| 65 | + _return_program=True, |
| 66 | + ) |
| 67 | + module = _load_for_executorch_from_buffer(et_program.buffer) |
| 68 | + |
| 69 | + prompt_before, pixels, prompt_after = self.lfm2_model.get_inputs_for_prefill() |
| 70 | + start_pos = 0 |
| 71 | + |
| 72 | + # Embed and prefill tokens before image |
| 73 | + before_embeds = module.run_method("token_embedding", (prompt_before,))[0] |
| 74 | + module.run_method( |
| 75 | + "text_decoder", |
| 76 | + (before_embeds, torch.arange(start_pos, start_pos + before_embeds.shape[1])), |
| 77 | + ) |
| 78 | + start_pos += before_embeds.shape[1] |
| 79 | + |
| 80 | + # Vision encoder |
| 81 | + image_embeds = module.run_method("vision_encoder", (pixels,))[0] |
| 82 | + module.run_method( |
| 83 | + "text_decoder", |
| 84 | + (image_embeds, torch.arange(start_pos, start_pos + image_embeds.shape[1])), |
| 85 | + ) |
| 86 | + start_pos += image_embeds.shape[1] |
| 87 | + |
| 88 | + # Embed and prefill tokens after image |
| 89 | + after_embeds = module.run_method("token_embedding", (prompt_after,))[0] |
| 90 | + logits = module.run_method( |
| 91 | + "text_decoder", |
| 92 | + (after_embeds, torch.arange(start_pos, start_pos + after_embeds.shape[1])), |
| 93 | + )[0] |
| 94 | + start_pos += after_embeds.shape[1] |
| 95 | + |
| 96 | + # Decode a few tokens — just check we get valid token IDs |
| 97 | + new_tokens = [torch.argmax(logits).item()] |
| 98 | + for i in range(3): |
| 99 | + token_embed = module.run_method( |
| 100 | + "token_embedding", |
| 101 | + (torch.tensor([[new_tokens[i]]], dtype=torch.int64),), |
| 102 | + )[0] |
| 103 | + logits = module.run_method( |
| 104 | + "text_decoder", |
| 105 | + (token_embed, torch.tensor([start_pos + i], dtype=torch.int64)), |
| 106 | + )[0] |
| 107 | + new_tokens.append(torch.argmax(logits).item()) |
| 108 | + |
| 109 | + self.assertEqual(len(new_tokens), 4) |
| 110 | + for tok in new_tokens: |
| 111 | + self.assertGreaterEqual(tok, 0) |
| 112 | + self.assertLess(tok, 65536) |
| 113 | + |
| 114 | + |
| 115 | +if __name__ == "__main__": |
| 116 | + unittest.main() |
0 commit comments