File size: 9,115 Bytes
3eea21f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import os
from typing import List, Optional, Tuple

import onnxruntime as onnxrt
import requests
import torch
from PIL import Image
from transformers import AutoConfig, AutoProcessor, GenerationConfig, PreTrainedModel
from transformers.generation import GenerationMixin
from transformers.modeling_outputs import BaseModelOutput, CausalLMOutputWithPast

from optimum.utils import NormalizedConfigManager


os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"


device = torch.device("cpu")


model_name = "llava-1.5-7b-hf/"

processor = AutoProcessor.from_pretrained(model_name)
config = AutoConfig.from_pretrained(model_name)

prompt = "<image>\nUSER: What's the content of the image?\nASSISTANT:"
url = "https://www.ilankelman.org/stopsigns/australia.jpg"
image = Image.open(requests.get(url, stream=True).raw)

inputs = processor(text=prompt, images=image, return_tensors="pt")


class ORTModel(torch.nn.Module):
    def __init__(self, path, config):
        super().__init__()
        self._device = device
        self.config = config
        self.session = onnxrt.InferenceSession(path, providers=["CPUExecutionProvider"])

        self.input_names = {input_key.name: idx for idx, input_key in enumerate(self.session.get_inputs())}
        self.output_names = {output_key.name: idx for idx, output_key in enumerate(self.session.get_outputs())}


class ORTEncoder(ORTModel):
    def forward(
        self,
        input_ids: torch.FloatTensor,
        pixel_values: torch.FloatTensor,
        attention_mask: torch.LongTensor,
        **kwargs,
    ) -> BaseModelOutput:
        onnx_inputs = {
            "input_ids": input_ids.cpu().detach().numpy(),
            "pixel_values": pixel_values.cpu().detach().numpy(),
            "attention_mask": attention_mask.cpu().detach().numpy(),
        }

        # Run inference
        outputs = self.session.run(None, onnx_inputs)

        for i, output in enumerate(outputs):
            outputs[i] = torch.from_numpy(output).to(self._device)

        return (
            outputs[self.output_names["inputs_embeds"]],
            outputs[self.output_names["decoder_attention_mask"]],
            outputs[self.output_names["position_ids"]],
        )


class ORTDecoderProcessor(ORTModel):
    def forward(
        self,
        input_ids: torch.FloatTensor,
        attention_mask: torch.LongTensor,
        past_key_value: torch.FloatTensor,
        **kwargs,
    ) -> BaseModelOutput:
        onnx_inputs = {
            "input_ids": input_ids.cpu().detach().numpy(),
            "attention_mask": attention_mask.cpu().detach().numpy(),
            "past_key_values.0.key": past_key_value.cpu().detach().numpy(),
        }

        # Run inference
        outputs = self.session.run(None, onnx_inputs)

        for i, output in enumerate(outputs):
            outputs[i] = torch.from_numpy(output).to(self._device)

        return (
            outputs[self.output_names["inputs_embeds"]],
            outputs[self.output_names["decoder_attention_mask"]],
            outputs[self.output_names["position_ids"]],
        )


class ORTDecoder(ORTModel):
    def __init__(self, path, config):
        super().__init__(path, config)

        self.normalized_config = NormalizedConfigManager.get_normalized_config_class(config.text_config.model_type)(
            config.text_config
        )
        self.generation_config = GenerationConfig.from_model_config(config)

        self.key_value_input_names = [key for key in self.input_names if (".key" in key) or (".value" in key)]
        self.key_value_output_names = [key for key in self.output_names if (".key" in key) or (".value" in key)]

        self.num_pkv = 2

    def prepare_pkv(self, batch_size: int):
        if self.config.text_config.model_type in {"mistral", "llama"}:
            num_attention_heads = self.normalized_config.num_key_value_heads
        else:
            num_attention_heads = self.normalized_config.num_attention_heads

        embed_size_per_head = self.normalized_config.hidden_size // self.normalized_config.num_attention_heads

        shape = (batch_size, num_attention_heads, 0, embed_size_per_head)
        key_or_value = torch.zeros(shape, dtype=torch.float32)

        past_key_values = tuple(key_or_value for _ in range(len(self.key_value_input_names)))

        return past_key_values

    def forward(
        self,
        attention_mask: torch.LongTensor,
        position_ids: torch.LongTensor,
        inputs_embeds: torch.FloatTensor,
        past_key_values: Tuple[Tuple[torch.FloatTensor]] = None,
    ) -> CausalLMOutputWithPast:
        onnx_inputs = {
            "attention_mask": attention_mask.cpu().detach().numpy(),
            "position_ids": position_ids.cpu().detach().numpy(),
            "inputs_embeds": inputs_embeds.cpu().detach().numpy(),
        }

        if past_key_values is None:
            past_key_values = self.prepare_pkv(inputs_embeds.shape[0])
        else:
            past_key_values = tuple(
                past_key_value for pkv_per_layer in past_key_values for past_key_value in pkv_per_layer
            )

        for input_name, past_key_value in zip(self.key_value_input_names, past_key_values):
            onnx_inputs[input_name] = past_key_value.cpu().detach().numpy()

        # Run inference
        outputs = self.session.run(None, onnx_inputs)

        logits = torch.from_numpy(outputs[self.output_names["logits"]])

        past_key_values = tuple(
            torch.from_numpy(outputs[self.output_names[key]]) for key in self.key_value_output_names
        )

        past_key_values = tuple(
            past_key_values[i : i + self.num_pkv] for i in range(0, len(past_key_values), self.num_pkv)
        )

        return CausalLMOutputWithPast(logits=logits, past_key_values=past_key_values)


class ORTModelForLLava(PreTrainedModel, GenerationMixin):
    def __init__(self, *args, **kwargs):
        config = AutoConfig.from_pretrained(model_name)
        super().__init__(config)

        self.config = config
        self._device = device

        self.vision_tower = ORTEncoder(model_name + "encoder_model.onnx", config)
        self.language_model = ORTDecoder(model_name + "decoder_model.onnx", config)
        self.decoder_input_processor = ORTDecoderProcessor(model_name + "decoder_input_processor_model.onnx", config)

    def forward(
        self,
        input_ids: torch.LongTensor = None,
        pixel_values: torch.FloatTensor = None,
        attention_mask: Optional[torch.Tensor] = None,
        past_key_values: Optional[List[torch.FloatTensor]] = None,
        **kwargs,
    ) -> CausalLMOutputWithPast:
        if past_key_values is None:
            inputs_embeds, attention_mask, position_ids = self.vision_tower(
                input_ids=input_ids,
                pixel_values=pixel_values,
                attention_mask=attention_mask,
            )
        else:
            inputs_embeds, attention_mask, position_ids = self.decoder_input_processor(
                input_ids=input_ids,
                attention_mask=attention_mask,
                past_key_value=past_key_values[0][0][:, :, :, 0],
            )

        # Decode
        decoder_outputs = self.language_model(
            attention_mask=attention_mask,
            position_ids=position_ids,
            inputs_embeds=inputs_embeds,
            past_key_values=past_key_values,
        )

        return decoder_outputs

    def can_generate(self):
        return True

    def prepare_inputs_for_generation(
        self, input_ids, past_key_values=None, inputs_embeds=None, pixel_values=None, attention_mask=None, **kwargs
    ):
        if past_key_values is not None:
            cache_length = past_length = past_key_values[0][0].shape[2]

            if attention_mask is not None and attention_mask.shape[1] > input_ids.shape[1]:
                input_ids = input_ids[:, -(attention_mask.shape[1] - past_length) :]
            elif past_length < input_ids.shape[1]:
                input_ids = input_ids[:, past_length:]
            elif self.config.image_token_index in input_ids:
                input_ids = input_ids[:, input_ids.shape[1] - 1 :]
            if cache_length < past_length and attention_mask is not None:
                attention_mask = attention_mask[:, -(cache_length + input_ids.shape[1]) :]

        model_inputs = {"input_ids": input_ids}

        model_inputs.update(
            {
                "past_key_values": past_key_values,
                "use_cache": kwargs.get("use_cache"),
                "attention_mask": attention_mask,
                "pixel_values": pixel_values,
            }
        )
        return model_inputs

    @property
    def device(self) -> torch.device:
        return self._device

    @device.setter
    def device(self, value: torch.device):
        self._device = value

    def to(self, device):
        self.device = device
        return self


model = ORTModelForLLava()

generated_ids = model.generate(**inputs, max_length=30)
out = processor.batch_decode(generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]

print(out)