Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# Copyright (c) Microsoft Corporation.  All rights reserved.
# Licensed under the MIT License.  See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
 
 
# Notes
# 1) The test cases in this file are for the following LLaMA-2 scenarios:
# - Microsoft rotary embeddings with interleaved = True
#   - Prompt generation
#   - Token generation
# - Hugging Face rotary embeddings (equal to Microsoft rotary embeddings with interleaved = False)
#   - Prompt generation
#   - Token generation
#
# 2) Shapes of position ids in ORT and `interleaved` for LLaMA-2 scenarios:
# - Microsoft model: When shape of position ids == (1), interleaved = True
# - Hugging Face model: When shape of position ids == (batch_size, sequence_length), interleaved = False
 
 
import unittest
from copy import deepcopy
 
import numpy as np
import torch
import torch.nn as nn
from onnx import TensorProto, helper
 
import onnxruntime as ort
 
 
class SampleInputConfig:
    def __init__(
        self,
        batch_size=2,
        sequence_length=8,
        num_heads=4,
        head_size=6,
        max_sequence_length=16,
    ):
        self.batch_size = batch_size
        self.sequence_length = sequence_length
        self.num_heads = num_heads
        self.head_size = head_size
        self.hidden_size = self.num_heads * self.head_size
        self.max_sequence_length = max_sequence_length
 
 
# LLaMA Hugging Face model
class LlamaHFRotaryEmbedding(nn.Module):
    def __init__(self, dim, max_position_embeddings=2048, base=10000, device="cpu"):
        super().__init__()
 
        self.dim = dim
        self.max_position_embeddings = max_position_embeddings
        self.base = base
        inv_freq = 1.0 / (self.base ** (torch.arange(0, self.dim, 2).float().to(device) / self.dim))
        self.register_buffer("inv_freq", inv_freq, persistent=False)
 
        # Build here to make `torch.jit.trace` work.
        self._set_cos_sin_cache(
            seq_len=max_position_embeddings, device=self.inv_freq.device, dtype=torch.get_default_dtype()
        )
 
    def _set_cos_sin_cache(self, seq_len, device, dtype):
        self.max_seq_len_cached = seq_len
        t = torch.arange(self.max_seq_len_cached, device=device, dtype=self.inv_freq.dtype)
 
        freqs = torch.einsum("i,j->ij", t, self.inv_freq)
        # Different from paper, but it uses a different permutation in order to obtain the same calculation
        emb = torch.cat((freqs, freqs), dim=-1)
        self.register_buffer("cos_cached", emb.cos()[None, None, :, :].to(dtype), persistent=False)
        self.register_buffer("sin_cached", emb.sin()[None, None, :, :].to(dtype), persistent=False)
 
    def get_cos_sin_cache(self, seq_len=None, device=torch.device("cpu"), dtype=torch.float32):  # noqa: B008
        # x: [bs, num_attention_heads, seq_len, head_size]
        if seq_len > self.max_seq_len_cached:
            self._set_cos_sin_cache(seq_len=seq_len, device=device, dtype=dtype)
 
        return (
            self.cos_cached[:, :, :seq_len, ...].to(dtype=dtype),
            self.sin_cached[:, :, :seq_len, ...].to(dtype=dtype),
        )
 
    def rotate_half(self, x):
        """Rotates half the hidden dims of the input."""
        x1 = x[..., : x.shape[-1] // 2]
        x2 = x[..., x.shape[-1] // 2 :]
        return torch.cat((-x2, x1), dim=-1)
 
    def apply_rope_bnsh(self, x, cos, sin, position_ids):
        # The first two dimensions of cos and sin are always 1, so we can `squeeze` them.
        cos = cos.squeeze(1).squeeze(0)  # [seq_len, dim]
        sin = sin.squeeze(1).squeeze(0)  # [seq_len, dim]
        cos = cos[position_ids].unsqueeze(1)  # [bs, 1, seq_len, dim]
        sin = sin[position_ids].unsqueeze(1)  # [bs, 1, seq_len, dim]
        x_embed = (x * cos) + (self.rotate_half(x) * sin)
        return x_embed
 
    def apply_rope_bsnh(self, x, cos, sin, position_ids):
        # Two dimensions of cos and sin are always 1, so we can `squeeze` them.
        cos = cos.squeeze()  # [seq_len, dim]
        sin = sin.squeeze()  # [seq_len, dim]
        cos = cos[position_ids].unsqueeze(2)  # [bs, seq_len, 1, dim]
        sin = sin[position_ids].unsqueeze(2)  # [bs, seq_len, 1, dim]
        x_embed = (x * cos) + (self.rotate_half(x) * sin)
        return x_embed
 
    def forward(self, x, cos, sin, pos_ids, x_format="bnsh"):
        if x_format == "bnsh":
            return self.apply_rope_bnsh(x, cos, sin, pos_ids)
        return self.apply_rope_bsnh(x, cos, sin, pos_ids)
 
 
# LLaMA Microsoft model
class LlamaMSRotaryEmbedding(nn.Module):
    def __init__(self, hidden_size, num_heads, max_sequence_length):
        super().__init__()
 
        self.hidden_size = hidden_size
        self.num_heads = num_heads
        self.max_sequence_length = max_sequence_length
 
    def get_cos_sin_cache(self, theta=10000.0, head_scale=1.0, device="cpu", dtype=torch.float32):
        hidden_size = self.hidden_size
        n_heads = self.num_heads
        max_seq_len = self.max_sequence_length
 
        # Precalculate rotary matrices for the sequence
        # According to "Attention Is All You Need", theta_i = 10000 ^ (2 * (i - 1)/dim), i in [1, 2, ..., dim//2]
        head_dim = head_scale * hidden_size / n_heads
 
        pos = torch.arange(0, 2 * (head_dim // 2), step=2, device=device, dtype=dtype)
        freqs = 1.0 / (theta ** (pos / head_dim))
 
        idx = torch.arange(max_seq_len, device=freqs.device)
        freqs = torch.outer(idx, freqs)
 
        cos = torch.reshape(torch.cos(freqs), [1, max_seq_len, 1, -1])
        sin = torch.reshape(torch.sin(freqs), [1, max_seq_len, 1, -1])
        dtype = torch.get_default_dtype()
 
        return cos.to(dtype), sin.to(dtype)
 
    def rotate_tensor(
        self,
        x: torch.Tensor,  # BxSxNxH
        cos: torch.Tensor,  # 1xSx1x(H/2)
        sin: torch.Tensor,  # 1xSx1x(H/2)
        pos: int,
        interleaved: bool,
    ):
        # Dimension of x is [batch_size, seq_len, n_heads, head_dim]
        rot_dim = 2 * cos.shape[3]
 
        # Dolly requires partial rotation
        x_rot = x[:, :, :, :rot_dim]
 
        if interleaved:
            x1 = x_rot[:, :, :, 0::2]
            x2 = x_rot[:, :, :, 1::2]
        else:
            half = x_rot.shape[-1] // 2
            x1 = x[:, :, :, 0:half]
            x2 = x[:, :, :, half : 2 * half]
 
        seq_len = x.shape[1]
        cos_x = cos[:, pos : pos + seq_len, :, :]
        sin_x = sin[:, pos : pos + seq_len, :, :]
 
        # cos_x: (1, S, 1, H/2)
        # sin_x: (1, S, 1, H/2)
        # x1: (B, S, N, H/2)
        # x2: (B, S, N, H/2)
        real = cos_x * x1 - sin_x * x2
        imag = sin_x * x1 + cos_x * x2
 
        if interleaved:
            x_rot[:, :, :, 0::2] = real
            x_rot[:, :, :, 1::2] = imag
        else:
            x_rot = torch.cat((real, imag), dim=-1)
 
        return torch.cat((x_rot, x[:, :, :, rot_dim:]), dim=-1)
 
    def forward(self, x, cos, sin, pos, interleaved):
        return self.rotate_tensor(x, cos, sin, pos, interleaved)
 
 
class TestLlamaRotaryEmbedding(unittest.TestCase):
    def setUp(self):
        self.config = SampleInputConfig()
        self.llama_hf = LlamaHFRotaryEmbedding(self.config.head_size, self.config.max_sequence_length)
        self.llama_ms = LlamaMSRotaryEmbedding(
            self.config.hidden_size, self.config.num_heads, self.config.max_sequence_length
        )
 
        seed = 2
        np.random.seed(seed)
        torch.manual_seed(seed)
        torch.set_printoptions(sci_mode=False)
 
    def create_onnx_graph(self, x_shape, pos_shape, cos, sin, interleaved):
        inputs = [
            helper.make_tensor_value_info(
                name="input",
                elem_type=TensorProto.FLOAT,
                shape=list(x_shape),
            ),
            helper.make_tensor_value_info(
                name="position_ids",
                elem_type=TensorProto.INT64,
                shape=list(pos_shape),
            ),
        ]
        outputs = [
            helper.make_tensor_value_info(
                name="output",
                elem_type=TensorProto.FLOAT,
                shape=list(x_shape),
            ),
        ]
 
        initializers = [
            helper.make_tensor(
                name="cos_cache",
                data_type=TensorProto.FLOAT,
                dims=list(torch.squeeze(cos).shape),
                vals=cos.flatten().tolist(),
            ),
            helper.make_tensor(
                name="sin_cache",
                data_type=TensorProto.FLOAT,
                dims=list(torch.squeeze(sin).shape),
                vals=sin.flatten().tolist(),
            ),
        ]
        nodes = [
            helper.make_node(
                op_type="RotaryEmbedding",
                inputs=["input", "position_ids", "cos_cache", "sin_cache"],
                outputs=["output"],
                interleaved=interleaved,
                name="RotaryEmbedding_0",
                domain="com.microsoft",
            ),
        ]
 
        graph = helper.make_graph(
            nodes=nodes,
            name="RotaryEmbedding_Graph",
            inputs=inputs,
            outputs=outputs,
            initializer=initializers,
        )
        opset_import = helper.make_opsetid(domain="com.microsoft", version=1)
        model = helper.make_model(graph, opset_imports=[opset_import])
        return model.SerializeToString()
 
    def get_eps(self):
        eps = ["CPUExecutionProvider", "CUDAExecutionProvider"]
        return list(filter(lambda ep: ep in ort.get_available_providers(), eps))
 
    def run_ort_ep_tests(self, onnx_graph, inputs_ort, expected_output_bsnh, transposed=False):
        eps = self.get_eps()
        for ep in eps:
            sess = ort.InferenceSession(onnx_graph, providers=[ep])
            output_ort = sess.run(None, inputs_ort)[0]
            if not transposed:
                output_ort = output_ort.reshape(
                    (self.config.batch_size, inputs_ort["input"].shape[1], self.config.num_heads, self.config.head_size)
                )
 
            # Compare outputs as BxSxNxH
            self.assertTrue(np.allclose(expected_output_bsnh, output_ort))
 
    # apply_rope(x_bnsh) == apply_rope(x_bsnh).transpose(1,2)
    def test_hf_bnsh_and_hf_bsnh(self):
        x_bnsh = torch.randn(
            self.config.batch_size, self.config.num_heads, self.config.sequence_length, self.config.head_size
        )
        cos_hf, sin_hf = self.llama_hf.get_cos_sin_cache(self.config.sequence_length)
        pos_hf = torch.stack([torch.arange(0, self.config.sequence_length) for _ in range(self.config.batch_size)])
 
        x_bnsh_after_rope = self.llama_hf(x_bnsh, cos_hf, sin_hf, pos_hf)  # output is BxNxSxH
        x_bsnh_after_rope = self.llama_hf(
            x_bnsh.transpose(1, 2), cos_hf.transpose(1, 2), sin_hf.transpose(1, 2), pos_hf, "bsnh"
        )  # output is BxSxNxH
 
        self.assertTrue(torch.allclose(x_bnsh_after_rope, x_bsnh_after_rope.transpose(1, 2)))
 
    # HF rotary == MSFT rotary non-interleaved
    def test_hf_rotary_and_msft_rotary_noninterleaved(self):
        x_bnsh = torch.randn(
            self.config.batch_size, self.config.num_heads, self.config.sequence_length, self.config.head_size
        )
        cos_hf, sin_hf = self.llama_hf.get_cos_sin_cache(self.config.sequence_length)
        pos_hf = torch.stack([torch.arange(0, self.config.sequence_length) for _ in range(self.config.batch_size)])
        output_hf = self.llama_hf(x_bnsh, cos_hf, sin_hf, pos_hf)  # output is BxNxSxH
 
        x_bsnh = x_bnsh.transpose(1, 2)
        x_bsd = deepcopy(x_bsnh)  # deepcopy to avoid changes made by self.llama_ms forward pass
        cos_ms, sin_ms = self.llama_ms.get_cos_sin_cache()
        pos_ms = 0
        output_ms = (
            self.llama_ms(x_bsd, cos_ms, sin_ms, pos_ms, interleaved=False).detach().cpu().numpy()  # output is BxSxNxH
        )
 
        # Compare caches as Mx(H/2)
        self.assertTrue(
            torch.allclose(self.llama_hf.cos_cached.squeeze()[:, : (self.config.head_size // 2)], cos_ms.squeeze())
        )
        self.assertTrue(
            torch.allclose(self.llama_hf.sin_cached.squeeze()[:, : (self.config.head_size // 2)], sin_ms.squeeze())
        )
 
        # Compare outputs as BxSxNxH
        self.assertTrue(np.allclose(output_hf.transpose(1, 2).detach().cpu().numpy(), output_ms))
 
    # Prompt step, interleaved = true, pos ids shape = (1)
    def test_msft_prompt_rotary_interleaved(self):
        # Calculated this way to match the data in rotary_embedding_op_test.cc
        x_bnsh = torch.randn(
            self.config.batch_size, self.config.num_heads, self.config.sequence_length, self.config.head_size
        )
        x_bsnh = x_bnsh.transpose(1, 2)
        x_bsd = deepcopy(x_bsnh)  # deepcopy to avoid changes made by self.llama_ms forward pass
        cos_ms, sin_ms = self.llama_ms.get_cos_sin_cache()
        pos_ms = 0
        output_ms = self.llama_ms(deepcopy(x_bsnh), cos_ms, sin_ms, pos_ms, interleaved=True).detach().cpu().numpy()
 
        x_bsd = x_bsd.reshape(self.config.batch_size, self.config.sequence_length, self.config.hidden_size)
        pos_ms = torch.tensor([pos_ms])
        onnx_graph = self.create_onnx_graph(x_bsd.shape, pos_ms.shape, cos_ms, sin_ms, interleaved=True)
        inputs_ort = {
            "input": x_bsd.detach().cpu().numpy(),
            "position_ids": pos_ms.detach().cpu().numpy(),
        }
 
        # Compare inputs/outputs as BxSxNxH
        self.assertTrue(np.allclose(x_bsnh.flatten(), x_bsd.flatten()))
        self.run_ort_ep_tests(onnx_graph, inputs_ort, output_ms)
 
    # Token generation step, interleaved = true, pos ids shape = (1)
    def test_msft_token_rotary_interleaved(self):
        # Calculated this way to match the data in rotary_embedding_op_test.cc
        x_bnsh = torch.randn(
            self.config.batch_size, self.config.num_heads, self.config.sequence_length, self.config.head_size
        )
        x_bsnh = x_bnsh.transpose(1, 2)
        x_bsd = deepcopy(x_bsnh)  # deepcopy to avoid changes made by self.llama_ms forward pass
        cos_ms, sin_ms = self.llama_ms.get_cos_sin_cache()
        pos_ms = 2
        output_ms = self.llama_ms(deepcopy(x_bsnh), cos_ms, sin_ms, pos_ms, interleaved=True).detach().cpu().numpy()
 
        x_bsd = x_bsd.reshape(self.config.batch_size, self.config.sequence_length, self.config.hidden_size)
        pos_ms = torch.tensor([pos_ms])
        onnx_graph = self.create_onnx_graph(x_bsd.shape, pos_ms.shape, cos_ms, sin_ms, interleaved=True)
        inputs_ort = {
            "input": x_bsd.detach().cpu().numpy(),
            "position_ids": pos_ms.detach().cpu().numpy(),
        }
 
        # Compare inputs/outputs as BxSxNxH
        self.assertTrue(np.allclose(x_bsnh.flatten(), x_bsd.flatten()))
        self.run_ort_ep_tests(onnx_graph, inputs_ort, output_ms)
 
    # Prompt step, interleaved = false, pos ids shape = (batch_size, sequence_length)
    def test_hf_prompt_rotary_batched_pos_ids(self):
        x_bnsh = torch.randn(
            self.config.batch_size, self.config.num_heads, self.config.sequence_length, self.config.head_size
        )
        cos_hf, sin_hf = self.llama_hf.get_cos_sin_cache(self.config.sequence_length)
        pos_ids = torch.stack([torch.arange(0, self.config.sequence_length) for _ in range(self.config.batch_size)])
        output_hf = self.llama_hf(x_bnsh, cos_hf, sin_hf, pos_ids)  # output is BxNxSxH
 
        x_bsnh = x_bnsh.transpose(1, 2)
        x_bsd = x_bsnh.reshape(self.config.batch_size, self.config.sequence_length, self.config.hidden_size)
        cos_ms, sin_ms = self.llama_ms.get_cos_sin_cache()
        onnx_graph = self.create_onnx_graph(x_bsd.shape, pos_ids.shape, cos_ms, sin_ms, interleaved=False)
        inputs_ort = {
            "input": x_bsd.detach().cpu().numpy(),
            "position_ids": pos_ids.detach().cpu().numpy(),
        }
 
        self.run_ort_ep_tests(onnx_graph, inputs_ort, output_hf.transpose(1, 2).detach().cpu().numpy())
 
    # Token generation step, interleaved = false, pos ids shape = (batch_size, sequence_length)
    def test_hf_token_rotary_batched_pos_ids(self):
        x_bnsh = torch.randn(self.config.batch_size, self.config.num_heads, 1, self.config.head_size)
        cos_hf, sin_hf = self.llama_hf.get_cos_sin_cache(self.config.sequence_length)
        pos_ids = torch.stack([torch.tensor([2]) for _ in range(self.config.batch_size)])
        output_hf = self.llama_hf(x_bnsh, cos_hf, sin_hf, pos_ids)  # output is BxNxSxH
 
        x_bsnh = x_bnsh.transpose(1, 2)
        x_bsd = x_bsnh.reshape(self.config.batch_size, 1, self.config.hidden_size)
        cos_ms, sin_ms = self.llama_ms.get_cos_sin_cache()
        onnx_graph = self.create_onnx_graph(x_bsd.shape, pos_ids.shape, cos_ms, sin_ms, interleaved=False)
        inputs_ort = {
            "input": x_bsd.detach().cpu().numpy(),
            "position_ids": pos_ids.detach().cpu().numpy(),
        }
 
        # Compare outputs as BxSxNxH
        self.run_ort_ep_tests(onnx_graph, inputs_ort, output_hf.transpose(1, 2).detach().cpu().numpy())
 
    # Bonus test: Prompt step, interleaved = false, pos ids shape = (1)
    def test_hf_prompt_rotary_one_pos_id(self):
        x_bnsh = torch.randn(
            self.config.batch_size, self.config.num_heads, self.config.sequence_length, self.config.head_size
        )
        cos_hf, sin_hf = self.llama_hf.get_cos_sin_cache(self.config.sequence_length)
        pos_hf = torch.stack([torch.arange(0, self.config.sequence_length) for _ in range(self.config.batch_size)])
        output_hf = self.llama_hf(x_bnsh, cos_hf, sin_hf, pos_hf)  # output is BxNxSxH
 
        x_bsnh = x_bnsh.transpose(1, 2)
        x_bsd = x_bsnh.reshape(self.config.batch_size, self.config.sequence_length, self.config.hidden_size)
        cos_ms, sin_ms = self.llama_ms.get_cos_sin_cache()
        pos_ms = torch.tensor([0])
        onnx_graph = self.create_onnx_graph(x_bsd.shape, pos_ms.shape, cos_ms, sin_ms, interleaved=False)
        inputs_ort = {
            "input": x_bsd.detach().cpu().numpy(),
            "position_ids": pos_ms.detach().cpu().numpy(),
        }
 
        # Compare outputs as BxSxNxH
        self.run_ort_ep_tests(onnx_graph, inputs_ort, output_hf.transpose(1, 2).detach().cpu().numpy())
 
    # Bonus test: Token generation step, interleaved = false, pos ids shape = (1)
    def test_hf_token_rotary_one_pos_id(self):
        x_bnsh = torch.randn(self.config.batch_size, self.config.num_heads, 1, self.config.head_size)
        cos_hf, sin_hf = self.llama_hf.get_cos_sin_cache(self.config.sequence_length)
        pos_ids = torch.stack([torch.tensor([2]) for _ in range(self.config.batch_size)])
        output_hf = self.llama_hf(x_bnsh, cos_hf, sin_hf, pos_ids)  # output is BxNxSxH
 
        x_bsnh = x_bnsh.transpose(1, 2)
        x_bsd = x_bsnh.reshape(self.config.batch_size, 1, self.config.hidden_size)
        cos_ms, sin_ms = self.llama_ms.get_cos_sin_cache()
        pos_ms = torch.tensor([2])
        onnx_graph = self.create_onnx_graph(x_bsd.shape, pos_ms.shape, cos_ms, sin_ms, interleaved=False)
        inputs_ort = {
            "input": x_bsd.detach().cpu().numpy(),
            "position_ids": pos_ms.detach().cpu().numpy(),
        }
 
        # Compare outputs as BxSxNxH
        self.run_ort_ep_tests(onnx_graph, inputs_ort, output_hf.transpose(1, 2).detach().cpu().numpy())
 
    # Bonus test: Prompt step, interleaved = false, pos ids shape = (1), transposed
    def test_hf_prompt_rotary_one_pos_id_transposed(self):
        x_bnsh = torch.randn(
            self.config.batch_size, self.config.num_heads, self.config.sequence_length, self.config.head_size
        )
        cos_hf, sin_hf = self.llama_hf.get_cos_sin_cache(self.config.sequence_length)
        pos_hf = torch.stack([torch.arange(0, self.config.sequence_length) for _ in range(self.config.batch_size)])
        output_hf = self.llama_hf(x_bnsh, cos_hf, sin_hf, pos_hf)  # output is BxNxSxH
 
        cos_ms, sin_ms = self.llama_ms.get_cos_sin_cache()
        pos_ms = torch.tensor([0])
        onnx_graph = self.create_onnx_graph(x_bnsh.shape, pos_ms.shape, cos_ms, sin_ms, interleaved=False)
        inputs_ort = {
            "input": x_bnsh.detach().cpu().numpy(),
            "position_ids": pos_ms.detach().cpu().numpy(),
        }
 
        # Compare outputs as BxNxSxH
        self.run_ort_ep_tests(onnx_graph, inputs_ort, output_hf.detach().cpu().numpy(), transposed=True)
 
    # Bonus test: Token generation step, interleaved = false, pos ids shape = (1), transposed
    def test_hf_token_rotary_one_pos_id_transposed(self):
        x_bnsh = torch.randn(self.config.batch_size, self.config.num_heads, 1, self.config.head_size)
        cos_hf, sin_hf = self.llama_hf.get_cos_sin_cache(self.config.sequence_length)
        pos_ids = torch.stack([torch.tensor([2]) for _ in range(self.config.batch_size)])
        output_hf = self.llama_hf(x_bnsh, cos_hf, sin_hf, pos_ids)  # output is BxSxNxH
 
        cos_ms, sin_ms = self.llama_ms.get_cos_sin_cache()
        pos_ms = torch.tensor([2])
        onnx_graph = self.create_onnx_graph(x_bnsh.shape, pos_ms.shape, cos_ms, sin_ms, interleaved=False)
        inputs_ort = {
            "input": x_bnsh.detach().cpu().numpy(),
            "position_ids": pos_ms.detach().cpu().numpy(),
        }
 
        # Set tranposed=True to compare outputs as BxSxNxH
        self.run_ort_ep_tests(onnx_graph, inputs_ort, output_hf.detach().cpu().numpy(), transposed=True)
 
 
if __name__ == "__main__":
    unittest.main()