Changesets · Branches · Tags · Compare
| Changeset | 420929cf5fe037302e6ceadc6c4103b9fddfa56ba8d74e3f8e790bf88041f03d |
|---|---|
| Author | Prince Canuma <prince.gdt@gmail.com> · 2024-04-16 17:30 +02:00 |
| Committer | Prince Canuma <prince.gdt@gmail.com> · 2024-04-16 17:30 +02:00 |
| Parents | de4ba1ff361c |
8 files changed
modified src/generate.py +0 −2
| @@ -1,5 +1,3 @@ | ||
| 1 | -# Copyright © 2024 Apple Inc. | |
| 2 | - | |
| 3 | 1 | import argparse |
| 4 | 2 | import codecs |
| 5 | 3 | from pathlib import Path |
modified src/models/llava/language.py +0 −1
| @@ -1,4 +1,3 @@ | ||
| 1 | -# Copyright © 2024 Apple Inc. | |
| 2 | 1 | |
| 3 | 2 | import inspect |
| 4 | 3 | from dataclasses import dataclass |
modified src/models/llava/llava.py +1 −1
| @@ -1,4 +1,4 @@ | ||
| 1 | -# Copyright © 2024 Apple Inc. | |
| 1 | + | |
| 2 | 2 | |
| 3 | 3 | import glob |
| 4 | 4 | import inspect |
modified src/models/llava/vision.py +1 −1
| @@ -1,4 +1,4 @@ | ||
| 1 | -# Copyright © 2024 Apple Inc. | |
| 1 | + | |
| 2 | 2 | |
| 3 | 3 | import inspect |
| 4 | 4 | import math |
deleted src/models/nanoLlava/test.py +0 −161
| @@ -1,161 +0,0 @@ | ||
| 1 | -# Copyright © 2024 Apple Inc. | |
| 2 | - | |
| 3 | -import unittest | |
| 4 | - | |
| 5 | -import mlx.core as mx | |
| 6 | -import requests | |
| 7 | -import torch | |
| 8 | -from PIL import Image | |
| 9 | -from transformers import AutoProcessor, AutoModelForCausalLM | |
| 10 | - | |
| 11 | -from nanoLlava import NanoLlavaModel | |
| 12 | - | |
| 13 | -MODEL_PATH = "qnguyen3/nanoLLaVA" | |
| 14 | -PROMPT = "<image>\nWhat are these?" | |
| 15 | -IMAGE_FILE = "http://images.cocodataset.org/val2017/000000039769.jpg" | |
| 16 | - | |
| 17 | - | |
| 18 | -def load_mlx_models(path): | |
| 19 | - model = NanoLlavaModel.from_pretrained(path) | |
| 20 | - return model | |
| 21 | - | |
| 22 | - | |
| 23 | -def load_hf_models(path): | |
| 24 | - model = AutoModelForCausalLM.from_pretrained(path) | |
| 25 | - model.eval() | |
| 26 | - return model | |
| 27 | - | |
| 28 | - | |
| 29 | -class TestVisionTower(unittest.TestCase): | |
| 30 | - @classmethod | |
| 31 | - def setUpClass(cls): | |
| 32 | - cls.mx_nanoLlava = load_mlx_models(MODEL_PATH) | |
| 33 | - cls.hf_nanoLlava = load_hf_models(MODEL_PATH) | |
| 34 | - cls.proc = AutoProcessor.from_pretrained(MODEL_PATH) | |
| 35 | - | |
| 36 | - def test_image_features(self): | |
| 37 | - raw_image = Image.open(requests.get(IMAGE_FILE, stream=True).raw) | |
| 38 | - vision_feature_layer = -2 | |
| 39 | - with torch.no_grad(): | |
| 40 | - pixel_values = self.proc(PROMPT, raw_image, return_tensors="pt")[ | |
| 41 | - "pixel_values" | |
| 42 | - ] | |
| 43 | - | |
| 44 | - hf_pixel_values = pixel_values | |
| 45 | - mx_pixel_values = mx.array(pixel_values.numpy()).transpose(0, 2, 3, 1) | |
| 46 | - | |
| 47 | - _, _, hidden_states = self.mx_nanoLlava.vision_tower( | |
| 48 | - mx_pixel_values, | |
| 49 | - output_hidden_states=True, | |
| 50 | - ) | |
| 51 | - | |
| 52 | - mx_elected_image_feature = hidden_states[vision_feature_layer] | |
| 53 | - mx_image_features = self.mx_nanoLlava.mm_projector( | |
| 54 | - mx_elected_image_feature | |
| 55 | - ) | |
| 56 | - | |
| 57 | - hf_image_outputs = self.hf_nanoLlava.vision_tower( | |
| 58 | - hf_pixel_values, output_hidden_states=True | |
| 59 | - ) | |
| 60 | - hf_elected_image_feature = hf_image_outputs.hidden_states[ | |
| 61 | - vision_feature_layer | |
| 62 | - ] | |
| 63 | - hf_image_features = self.hf_nanoLlava.mm_projector( | |
| 64 | - hf_elected_image_feature | |
| 65 | - ) | |
| 66 | - | |
| 67 | - self.assertTrue( | |
| 68 | - mx.allclose( | |
| 69 | - mx_image_features, | |
| 70 | - mx.array(hf_image_features.numpy()), | |
| 71 | - atol=1e-2, | |
| 72 | - ) | |
| 73 | - ) | |
| 74 | - | |
| 75 | - | |
| 76 | -class TestLlava(unittest.TestCase): | |
| 77 | - @classmethod | |
| 78 | - def setUpClass(cls): | |
| 79 | - cls.mx_nanoLlava = load_mlx_models(MODEL_PATH) | |
| 80 | - cls.hf_nanoLlava = load_hf_models(MODEL_PATH) | |
| 81 | - cls.proc = AutoProcessor.from_pretrained(MODEL_PATH) | |
| 82 | - | |
| 83 | - def test_merge_input_ids_with_image_features(self): | |
| 84 | - raw_image = Image.open(requests.get(IMAGE_FILE, stream=True).raw) | |
| 85 | - vision_feature_layer = -2 | |
| 86 | - with torch.no_grad(): | |
| 87 | - values = self.proc(PROMPT, raw_image, return_tensors="pt") | |
| 88 | - pixel_values = values["pixel_values"] | |
| 89 | - input_ids = values["input_ids"] | |
| 90 | - | |
| 91 | - hf_pixel_values = pixel_values | |
| 92 | - mx_pixel_values = mx.array(pixel_values.numpy()).transpose(0, 2, 3, 1) | |
| 93 | - | |
| 94 | - _, _, hidden_states = self.mx_nanoLlava.vision_tower( | |
| 95 | - mx_pixel_values, | |
| 96 | - output_hidden_states=True, | |
| 97 | - ) | |
| 98 | - mx_input_ids = mx.array(input_ids.numpy()) | |
| 99 | - mx_elected_image_feature = hidden_states[vision_feature_layer] | |
| 100 | - mx_image_features = self.mx_nanoLlava.mm_projector( | |
| 101 | - mx_elected_image_feature | |
| 102 | - ) | |
| 103 | - mx_inputs_embeds = self.mx_nanoLlava.language_model.model.embed_tokens( | |
| 104 | - mx_input_ids | |
| 105 | - ) | |
| 106 | - mx_final_embedding = self.mx_nanoLlava._prepare_inputs_for_multimodal( | |
| 107 | - mx_image_features, mx_inputs_embeds, mx_input_ids | |
| 108 | - ) | |
| 109 | - | |
| 110 | - hf_image_outputs = self.hf_nanoLlava.vision_tower( | |
| 111 | - hf_pixel_values, output_hidden_states=True | |
| 112 | - ) | |
| 113 | - hf_elected_image_feature = hf_image_outputs.hidden_states[ | |
| 114 | - vision_feature_layer | |
| 115 | - ] | |
| 116 | - hf_image_features = self.hf_nanoLlava.mm_projector( | |
| 117 | - hf_elected_image_feature | |
| 118 | - ) | |
| 119 | - hf_inputs_embeds = self.hf_nanoLlava.get_input_embeddings()(input_ids) | |
| 120 | - hf_final_embedding, _, _, _ = ( | |
| 121 | - self.hf_nanoLlava._prepare_inputs_for_multimodal( | |
| 122 | - hf_image_features, | |
| 123 | - hf_inputs_embeds, | |
| 124 | - input_ids, | |
| 125 | - attention_mask=input_ids, | |
| 126 | - labels=torch.ones_like(input_ids), | |
| 127 | - ) | |
| 128 | - ) | |
| 129 | - | |
| 130 | - self.assertTrue( | |
| 131 | - mx.allclose( | |
| 132 | - mx_final_embedding, | |
| 133 | - mx.array(hf_final_embedding.numpy()), | |
| 134 | - atol=1e-1, | |
| 135 | - ) | |
| 136 | - ) | |
| 137 | - | |
| 138 | - def test_generated_tokens(self): | |
| 139 | - raw_image = Image.open(requests.get(IMAGE_FILE, stream=True).raw) | |
| 140 | - with torch.no_grad(): | |
| 141 | - hf_inputs = self.proc(PROMPT, raw_image, return_tensors="pt") | |
| 142 | - hf_outputs = self.hf_nanoLlava(**hf_inputs) | |
| 143 | - hf_logits = hf_outputs.logits | |
| 144 | - | |
| 145 | - mx_inputs = self.proc(PROMPT, raw_image, return_tensors="np") | |
| 146 | - pixel_values = mx.array(mx_inputs["pixel_values"]) | |
| 147 | - input_ids = mx.array(mx_inputs["input_ids"]) | |
| 148 | - | |
| 149 | - mx_logits, _ = self.mx_nanoLlava(input_ids, pixel_values) | |
| 150 | - | |
| 151 | - self.assertTrue( | |
| 152 | - mx.allclose( | |
| 153 | - mx_logits[:, -1, :].argmax(axis=-1), | |
| 154 | - mx.array(hf_logits.numpy())[:, -1, :].argmax(axis=-1), | |
| 155 | - atol=1e-2, | |
| 156 | - ) | |
| 157 | - ) | |
| 158 | - | |
| 159 | - | |
| 160 | -if __name__ == "__main__": | |
| 161 | - unittest.main() |
modified src/models/nanoLlava/vision.py +1 −1
| @@ -1,4 +1,4 @@ | ||
| 1 | -# Copyright © 2024 Apple Inc. | |
| 1 | + | |
| 2 | 2 | |
| 3 | 3 | import inspect |
| 4 | 4 | import math |
modified src/utils.py +0 −1
| @@ -1,4 +1,3 @@ | ||
| 1 | -# Copyright © 2023-2024 Apple Inc. | |
| 2 | 1 | |
| 3 | 2 | import copy |
| 4 | 3 | import re |
added src/version.py +3 −0
| @@ -0,0 +1,3 @@ | ||
| 1 | + | |
| 2 | + | |
| 3 | +__version__ = "0.0.1" |