跳到内容

vllm_gaudi.v1.worker.hpu_model_runner

HPU_TORCH_DTYPE_TO_STR_DTYPE module-attribute

HPU_TORCH_DTYPE_TO_STR_DTYPE = {
    float32: "float32",
    bfloat16: "bfloat16",
    float16: "float16",
    float8_e4m3fn: "fp8_e4m3",
}

Mergeable module-attribute

_TYPE_CACHE module-attribute

_TYPE_CACHE: dict[str, dict[str, Any]] = {}

empty_list module-attribute

empty_list: Callable[[], list] = lambda: field(
    default_factory=list
)

hpu_buffer module-attribute

hpu_buffer: list[list[Tensor]] = []

logger module-attribute

logger = logger()

AsyncHPUModelRunnerOutput

Bases: AsyncModelRunnerOutput

Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
class AsyncHPUModelRunnerOutput(AsyncModelRunnerOutput):

    def __init__(
        self,
        model_runner_output: ModelRunnerOutput,
        sampled_token_ids: torch.Tensor,
        invalid_req_indices: list[int],
        async_output_copy_stream: torch.hpu.Stream,
    ):
        self._model_runner_output = model_runner_output
        self._invalid_req_indices = invalid_req_indices

        # Keep a reference to the device tensor to avoid it being
        # deallocated until we finish copying it to the host.
        self._sampled_token_ids = sampled_token_ids

        self._async_copy_ready_event = torch.hpu.Event()
        default_stream = torch.hpu.current_stream()
        with torch.hpu.stream(async_output_copy_stream):
            async_output_copy_stream.wait_stream(default_stream)
            self._sampled_token_ids_cpu = self._sampled_token_ids.to('cpu', non_blocking=True)
            self._async_copy_ready_event.record()

    def get_output(self) -> ModelRunnerOutput:
        """Copy the device tensors to the host and return a ModelRunnerOutput.

        This function blocks until the copy is finished.
        """

        # Release the device tensor once the copy has completed
        self._async_copy_ready_event.synchronize()

        sampled_token_ids_np = self._sampled_token_ids_cpu.numpy()
        valid_sampled_token_ids = [sampled_token_ids_np[i] for i in range(len(sampled_token_ids_np))]
        del self._sampled_token_ids
        for i in self._invalid_req_indices:
            if i < len(valid_sampled_token_ids):
                valid_sampled_token_ids[i] = np.array([], dtype=np.int32)

        output = self._model_runner_output
        output.sampled_token_ids[:len(valid_sampled_token_ids)] = valid_sampled_token_ids
        return output

_async_copy_ready_event instance-attribute

_async_copy_ready_event = Event()

_invalid_req_indices instance-attribute

_invalid_req_indices = invalid_req_indices

_model_runner_output instance-attribute

_model_runner_output = model_runner_output

_sampled_token_ids instance-attribute

_sampled_token_ids = sampled_token_ids

_sampled_token_ids_cpu instance-attribute

_sampled_token_ids_cpu = to('cpu', non_blocking=True)

__init__

__init__(
    model_runner_output: ModelRunnerOutput,
    sampled_token_ids: Tensor,
    invalid_req_indices: list[int],
    async_output_copy_stream: Stream,
)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def __init__(
    self,
    model_runner_output: ModelRunnerOutput,
    sampled_token_ids: torch.Tensor,
    invalid_req_indices: list[int],
    async_output_copy_stream: torch.hpu.Stream,
):
    self._model_runner_output = model_runner_output
    self._invalid_req_indices = invalid_req_indices

    # Keep a reference to the device tensor to avoid it being
    # deallocated until we finish copying it to the host.
    self._sampled_token_ids = sampled_token_ids

    self._async_copy_ready_event = torch.hpu.Event()
    default_stream = torch.hpu.current_stream()
    with torch.hpu.stream(async_output_copy_stream):
        async_output_copy_stream.wait_stream(default_stream)
        self._sampled_token_ids_cpu = self._sampled_token_ids.to('cpu', non_blocking=True)
        self._async_copy_ready_event.record()

get_output

get_output() -> ModelRunnerOutput

将设备张量复制到主机并返回 ModelRunnerOutput。

此函数会阻塞直到复制完成。

Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def get_output(self) -> ModelRunnerOutput:
    """Copy the device tensors to the host and return a ModelRunnerOutput.

    This function blocks until the copy is finished.
    """

    # Release the device tensor once the copy has completed
    self._async_copy_ready_event.synchronize()

    sampled_token_ids_np = self._sampled_token_ids_cpu.numpy()
    valid_sampled_token_ids = [sampled_token_ids_np[i] for i in range(len(sampled_token_ids_np))]
    del self._sampled_token_ids
    for i in self._invalid_req_indices:
        if i < len(valid_sampled_token_ids):
            valid_sampled_token_ids[i] = np.array([], dtype=np.int32)

    output = self._model_runner_output
    output.sampled_token_ids[:len(valid_sampled_token_ids)] = valid_sampled_token_ids
    return output

BatchContents dataclass

Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
@dataclass
class BatchContents:
    req_ids: list[str] = empty_list()
    token_ids: list[list[int]] = empty_list()
    context_lens: list[int] = empty_list()
    prompt_lens: list[int] = empty_list()
    blocks: list[list[int]] = empty_list()
    logits_positions: list[list[int]] = empty_list()

    def get_num_tokens(self):
        return [len(t) for t in self.token_ids]

    def clone(self):
        return BatchContents(req_ids=self.req_ids.copy(),
                             token_ids=[t.copy() for t in self.token_ids],
                             context_lens=self.context_lens.copy(),
                             blocks=[b.copy() for b in self.blocks],
                             logits_positions=[lp.copy() for lp in self.logits_positions])

blocks class-attribute instance-attribute

blocks: list[list[int]] = empty_list()

context_lens class-attribute instance-attribute

context_lens: list[int] = empty_list()

logits_positions class-attribute instance-attribute

logits_positions: list[list[int]] = empty_list()

prompt_lens class-attribute instance-attribute

prompt_lens: list[int] = empty_list()

req_ids class-attribute instance-attribute

req_ids: list[str] = empty_list()

token_ids class-attribute instance-attribute

token_ids: list[list[int]] = empty_list()

__init__

__init__(
    req_ids: list[str] = empty_list(),
    token_ids: list[list[int]] = empty_list(),
    context_lens: list[int] = empty_list(),
    prompt_lens: list[int] = empty_list(),
    blocks: list[list[int]] = empty_list(),
    logits_positions: list[list[int]] = empty_list(),
) -> None

clone

clone()
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def clone(self):
    return BatchContents(req_ids=self.req_ids.copy(),
                         token_ids=[t.copy() for t in self.token_ids],
                         context_lens=self.context_lens.copy(),
                         blocks=[b.copy() for b in self.blocks],
                         logits_positions=[lp.copy() for lp in self.logits_positions])

get_num_tokens

get_num_tokens()
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def get_num_tokens(self):
    return [len(t) for t in self.token_ids]

BucketingFailedException

Bases: Exception

Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
class BucketingFailedException(Exception):
    pass

DecodeData dataclass

Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
@dataclass
class DecodeData:
    input_tokens: Optional[torch.Tensor] = None
    input_positions: Optional[torch.Tensor] = None
    attn_metadata: Optional[HPUAttentionMetadataV1] = None

attn_metadata class-attribute instance-attribute

attn_metadata: Optional[HPUAttentionMetadataV1] = None

input_positions class-attribute instance-attribute

input_positions: Optional[Tensor] = None

input_tokens class-attribute instance-attribute

input_tokens: Optional[Tensor] = None

__init__

__init__(
    input_tokens: Optional[Tensor] = None,
    input_positions: Optional[Tensor] = None,
    attn_metadata: Optional[HPUAttentionMetadataV1] = None,
) -> None

DecodeInputData dataclass

Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
@dataclass
class DecodeInputData:
    num_decodes: int
    token_ids: Optional[torch.Tensor] = None
    position_ids: Optional[torch.Tensor] = None
    attn_metadata: Optional[HPUAttentionMetadataV1] = None
    logits_indices: Optional[torch.Tensor] = None
    spec_decode_metadata: Optional[SpecDecodeMetadata] = None

attn_metadata class-attribute instance-attribute

attn_metadata: Optional[HPUAttentionMetadataV1] = None

logits_indices class-attribute instance-attribute

logits_indices: Optional[Tensor] = None

num_decodes instance-attribute

num_decodes: int

position_ids class-attribute instance-attribute

position_ids: Optional[Tensor] = None

spec_decode_metadata class-attribute instance-attribute

spec_decode_metadata: Optional[SpecDecodeMetadata] = None

token_ids class-attribute instance-attribute

token_ids: Optional[Tensor] = None

__init__

__init__(
    num_decodes: int,
    token_ids: Optional[Tensor] = None,
    position_ids: Optional[Tensor] = None,
    attn_metadata: Optional[HPUAttentionMetadataV1] = None,
    logits_indices: Optional[Tensor] = None,
    spec_decode_metadata: Optional[
        SpecDecodeMetadata
    ] = None,
) -> None

HPUModelRunner

Bases: KVConnectorModelRunnerMixin

Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
class HPUModelRunner(KVConnectorModelRunnerMixin):

    def __init__(
        self,
        vllm_config: VllmConfig,
        device: torch.device = 'hpu',
        is_driver_worker: bool = False,
    ):
        # TODO: use ModelRunnerBase.__init__(self, vllm_config=vllm_config)
        environment.set_vllm_config(vllm_config)
        finalize_config()
        self.vllm_config = vllm_config
        self.model_config = vllm_config.model_config
        self.cache_config = vllm_config.cache_config
        self.lora_config = vllm_config.lora_config
        self.load_config = vllm_config.load_config
        self.parallel_config = vllm_config.parallel_config
        self.scheduler_config = vllm_config.scheduler_config
        self.speculative_config = vllm_config.speculative_config
        self.observability_config = vllm_config.observability_config
        self.is_driver_worker = is_driver_worker
        self.use_aux_hidden_state_outputs = False
        self.supports_mm_inputs = False

        self.sampler = Sampler()

        # NOTE(kzawora) update_env is a hack to work around VLLMKVCache in
        # hpu-extension which selects fetch_from_cache implementation based
        # on env vars... this should be fixed in the future
        self.enable_bucketing = get_config().use_bucketing
        self.use_contiguous_pa = get_config().use_contiguous_pa
        self.skip_warmup = get_config().skip_warmup

        model_config = self.model_config
        cache_config = self.cache_config
        scheduler_config = self.scheduler_config
        self.device = device
        self.pin_memory = is_pin_memory_available()
        self.dtype = self.model_config.dtype
        if cache_config.cache_dtype == "auto":
            self.kv_cache_dtype = self.dtype
        else:
            self.kv_cache_dtype = STR_DTYPE_TO_TORCH_DTYPE[cache_config.cache_dtype]
        self.kv_cache_dtype_str = HPU_TORCH_DTYPE_TO_STR_DTYPE[self.kv_cache_dtype]
        self.is_pooling_model = model_config.pooler_config is not None

        self.sliding_window = model_config.get_sliding_window()
        self.interleaved_sliding_window = is_interleaved(vllm_config.model_config.hf_text_config)
        self.block_size = cache_config.block_size
        self.max_model_len = model_config.max_model_len
        self.max_num_blocks_per_req = cdiv(self.max_model_len, self.block_size)
        # Override settings when profiling a single prefill/decode
        # We can do such barbaric changes because we close vllm after the profiling
        prompt_profile_cfg, decode_profile_cfg = self._read_profiling_cfg()
        if prompt_profile_cfg or decode_profile_cfg:
            self.scheduler_config.max_num_seqs = self.max_model_len
            if prompt_profile_cfg:
                self.scheduler_config.max_num_batched_tokens = prompt_profile_cfg[0] * prompt_profile_cfg[1]
        self.max_num_tokens = scheduler_config.max_num_batched_tokens
        # Cached outputs.
        ## universal buffer for input_ids and positions ##
        ## necessary being used by spec decode by following GPU impl ##
        self._draft_token_ids: Optional[Union[list[list[int]], torch.Tensor]] = None
        self.input_ids_cpu = torch.zeros(self.max_num_tokens,
                                         dtype=torch.int32,
                                         device="cpu",
                                         pin_memory=self.pin_memory)
        self.positions_cpu = torch.zeros(self.max_num_tokens,
                                         dtype=torch.int64,
                                         device="cpu",
                                         pin_memory=self.pin_memory)
        self.positions_np = self.positions_cpu.numpy()
        ###############################################################

        # Model-related.
        self.num_attn_layers = self.model_config.get_num_layers_by_block_type(self.parallel_config, "attention")
        self.num_query_heads = self.model_config.get_num_attention_heads(self.parallel_config)
        self.num_kv_heads = self.model_config.get_num_kv_heads(self.parallel_config)
        self.head_size = self.model_config.get_head_size()
        self.hidden_size = self.model_config.get_hidden_size()
        self.is_pooling_model = model_config.pooler_config is not None
        logger.debug("model config: ", self.model_config)

        self.attn_backend = get_attn_backend(
            self.head_size,
            self.dtype,
            self.kv_cache_dtype_str,
            self.block_size,
            use_mla=self.model_config.use_mla,
        )

        # Mult-modal-related.
        self.mm_registry = MULTIMODAL_REGISTRY
        self.uses_mrope = model_config.uses_mrope
        self.supports_mm_inputs = self.mm_registry.supports_multimodal_inputs(model_config)
        if self.supports_mm_inputs:
            self.is_mm_embed = self._make_buffer(self.max_num_tokens, dtype=torch.bool)
        self.is_multimodal_raw_input_supported = (model_config.is_multimodal_raw_input_only_model)

        # Lazy initialization
        # self.model: nn.Module  # set after load_model
        self.kv_caches: list[torch.Tensor] = []
        self.inc_initialized_successfully = False
        self._is_inc_finalized = False

        # mm_hash -> encoder_output
        self.encoder_cache: dict[str, torch.Tensor] = {}
        # Set up speculative decoding.
        # NOTE(Chendi): Speculative decoding is only enabled for the last rank
        # in the pipeline parallel group.
        if self.speculative_config:
            if self.speculative_config.num_speculative_tokens > 1:
                raise NotImplementedError("Speculative decoding with num_speculative_tokens > 1 is "
                                          "not supported on HPU.")
            if self.speculative_config.method == "ngram":
                self.drafter = NgramProposer(self.vllm_config)
            elif self.speculative_config.use_eagle():
                if self.speculative_config.num_speculative_tokens > 1:
                    logger.warning("EagleProposer only supports num_speculative_tokens=1. "
                                   "Overriding the config.")
                    self.speculative_config.num_speculative_tokens = 1
                self.drafter = EagleProposer(self.vllm_config, self.device, self)  # type: ignore
                if self.speculative_config.method == "eagle3":
                    self.use_aux_hidden_state_outputs = True
            elif self.speculative_config.method == "medusa":
                raise NotImplementedError("Medusa speculative decoding is not supported on HPU.")
            else:
                raise ValueError("Unknown speculative decoding method: "
                                 f"{self.speculative_config.method}")
            self.rejection_sampler = RejectionSampler(self.sampler)

        # Keep in int64 to avoid overflow with long context
        self.max_num_reqs = self.scheduler_config.max_num_seqs

        # Keep in int64 to avoid overflow with long context
        self.arange_np = np.arange(max(self.max_num_reqs + 1, self.max_model_len, self.max_num_tokens), dtype=np.int64)

        # Request states.
        self.requests: dict[str, CachedRequestState] = {}
        # Persistent batch.
        self.input_batch = InputBatch(
            max_num_reqs=self.scheduler_config.max_num_seqs,
            max_model_len=self.max_model_len,
            max_num_batched_tokens=self.max_num_tokens,
            device=self.device,
            pin_memory=self.pin_memory,
            vocab_size=self.model_config.get_vocab_size(),
            block_sizes=[self.block_size],
            kernel_block_sizes=[self.block_size],
            is_spec_decode=bool(self.vllm_config.speculative_config),
            logitsprocs=build_logitsprocs(self.vllm_config, self.device, self.pin_memory, self.is_pooling_model,
                                          self.vllm_config.model_config.logits_processors),
        )

        self.use_async_scheduling = self.scheduler_config.async_scheduling
        # Cache token ids on device to avoid h2d copies
        self.input_ids_hpu = torch.zeros(
            self.max_num_tokens, dtype=torch.int32, device=self.device,
            pin_memory=self.pin_memory) if self.use_async_scheduling else None
        self.async_output_copy_stream = torch.hpu.Stream() if \
            self.use_async_scheduling else None
        assert not (self.use_async_scheduling and (self.speculative_config is not None)), \
            "Speculative decoding is not supported with async scheduling."
        self.mem_margin = None
        self.unified_attn = get_config().unified_attn
        self.use_merged_prefill = get_config().merged_prefill

        self.use_hpu_graph = not self.model_config.enforce_eager
        self.max_batch_size = self.scheduler_config.max_num_seqs
        self.max_num_seqs = self.scheduler_config.max_num_seqs
        self.max_cudagraph_capture_size = self.vllm_config.compilation_config.max_cudagraph_capture_size
        if prompt_profile_cfg:
            self.max_prefill_batch_size = prompt_profile_cfg[0]
        else:
            self.max_prefill_batch_size = with_default(get_config().VLLM_PROMPT_BS_BUCKET_MAX, 1)
        self.seen_configs: set = set()
        self.max_num_batched_tokens = \
            self.scheduler_config.max_num_batched_tokens
        self.use_prefix_caching = (self.vllm_config.cache_config.enable_prefix_caching)
        self.bucketing_manager = HPUBucketingManager()
        max_num_prefill_seqs = self.max_num_seqs if self.use_merged_prefill \
                               else self.max_prefill_batch_size
        if self.enable_bucketing:
            logger.info("Bucketing is ON.")
            self.bucketing_manager.initialize(max_num_seqs=self.max_num_seqs,
                                              max_num_prefill_seqs=max_num_prefill_seqs,
                                              block_size=self.block_size,
                                              max_num_batched_tokens=self.max_num_batched_tokens,
                                              max_model_len=self.max_model_len)
            self.graphed_buckets: set[Any] = set()
            self.graphed_multimodal_buckets: set[Any] = set()
        else:
            logger.info("Bucketing is OFF.")
        self._PAD_SLOT_ID = -1
        self._PAD_BLOCK_ID = -1
        self._tokenizer = init_tokenizer_from_configs(model_config=vllm_config.model_config)

        if self.vllm_config.parallel_config.data_parallel_size > 1 and htorch.utils.internal.is_lazy(
        ) and not self.model_config.enforce_eager:
            from vllm import envs
            # disable device group for dp synchronization when hpu graph is
            # turned on since it's not captured and causes issues
            envs.VLLM_DISABLE_NCCL_FOR_DP_SYNCHRONIZATION = True

        self.logits_rounding = 1
        # High-level profiler
        self.profiler = HabanaHighLevelProfiler()
        self.profiler_counter_helper = HabanaProfilerCounterHelper()

        self.defragmenter = OnlineDefragmenter()
        self.debug_fwd = init_debug_logger('fwd')

        self.get_dp_padding = partial(get_dp_padding,
                                      dp_size=self.parallel_config.data_parallel_size,
                                      dp_rank=self.parallel_config.data_parallel_rank)

        self.scheduler_output: SchedulerOutput | None = None
        self.warmup_mode: bool = False
        self.batch_changed: bool = False
        # WA for chunked attention support
        self.model_has_chunked_attention = False

        assert not (self.unified_attn and not self.use_contiguous_pa), 'Unified attn requires contiguous_pa!'
        assert not (self.unified_attn and not self.use_merged_prefill), 'Unified attn requires merged_prefill!'

    def _make_buffer(self, *size: Union[int, torch.SymInt], dtype: torch.dtype, numpy: bool = True) -> CpuGpuBuffer:
        return CpuGpuBuffer(*size, dtype=dtype, device=self.device, pin_memory=self.pin_memory, with_numpy=numpy)

    def unified_bucketing_fn(self, is_causal, query_len, shared_blocks, unique_blocks, logits):
        if not get_config().use_bucketing:
            return query_len, shared_blocks, unique_blocks, logits

        new_bucket = self.bucketing_manager.find_unified_bucket(query_len, shared_blocks, unique_blocks, is_causal)
        return (new_bucket[0], new_bucket[1], new_bucket[2], self.max_num_seqs)

    def create_lora_mask(self, input_tokens: torch.Tensor, lora_ids: list[int], is_prompt: bool):
        '''
        This is a helper function to create the mask for lora computations.
        Lora Mask is needed to ensure we match the correct lora weights for the
        for the request.
        For Prompt phase we have
        lora_mask with shape (batch_size * seq_len, max_loras * max_rank)
        lora_logits_mask with shape (batch_size, max_loras * max_rank)
        For Decode phase we have both
        lora_mask and lora_logits_mask with shape
        (batch_size, max_loras * max_rank)
        '''
        lora_mask: torch.Tensor = None
        lora_logits_mask: torch.Tensor = None
        lora_index = 0

        if self.lora_config:
            if is_prompt:
                lora_mask = torch.zeros(
                    input_tokens.shape[0] * input_tokens.shape[1],
                    (self.lora_config.max_loras) *\
                        self.lora_config.max_lora_rank,
                    dtype=self.lora_config.lora_dtype)
                lora_logits_mask = torch.zeros(input_tokens.shape[0],
                                               (self.lora_config.max_loras) * self.lora_config.max_lora_rank,
                                               dtype=self.lora_config.lora_dtype)

                ones = torch.ones(input_tokens.shape[1],
                                  self.lora_config.max_lora_rank,
                                  dtype=self.lora_config.lora_dtype)
                logit_ones = torch.ones(1, self.lora_config.max_lora_rank, dtype=self.lora_config.lora_dtype)

                for i in range(len(lora_ids)):
                    if lora_ids[i] == 0:
                        continue
                    lora_index = self.lora_manager._adapter_manager.\
                        lora_index_to_id.index(lora_ids[i])
                    start_row = i * input_tokens.shape[1]
                    end_row = start_row + input_tokens.shape[1]
                    start_col = lora_index * self.lora_config.max_lora_rank
                    end_col = start_col + self.lora_config.max_lora_rank
                    lora_mask[start_row:end_row, start_col:end_col] = ones
                    lora_logits_mask[i, start_col:end_col] = logit_ones
                lora_mask = lora_mask.to('hpu')
                lora_logits_mask = lora_logits_mask.to('hpu')
            else:
                lora_mask = torch.zeros(input_tokens.shape[0],
                                        (self.lora_config.max_loras) * self.lora_config.max_lora_rank,
                                        dtype=self.lora_config.lora_dtype)
                ones = torch.ones(1, self.lora_config.max_lora_rank, dtype=self.lora_config.lora_dtype)
                for i in range(len(lora_ids)):
                    if lora_ids[i] == 0:
                        continue
                    lora_index = self.lora_manager._adapter_manager.\
                        lora_index_to_id.index(lora_ids[i])
                    start_pos = lora_index * self.lora_config.max_lora_rank
                    end_pos = start_pos + self.lora_config.max_lora_rank
                    lora_mask[i, start_pos:end_pos] = ones
                lora_mask = lora_mask.to('hpu')
                lora_logits_mask = lora_mask

        return lora_mask, lora_logits_mask

    def load_lora_model(self, model: nn.Module, vllm_config: VllmConfig, device: str) -> nn.Module:
        if not supports_lora(model):
            raise ValueError(f"{model.__class__.__name__} does not support LoRA yet.")

        if supports_multimodal(model):
            logger.warning("Regarding multimodal models, vLLM currently "
                           "only supports adding LoRA to language model.")

        # Add LoRA Manager to the Model Runner
        self.lora_manager = LRUCacheWorkerLoRAManager(
            vllm_config,
            device,
            model.embedding_modules,
            model.embedding_padding_modules,
        )
        return self.lora_manager.create_lora_manager(model)

    def set_active_loras(self, lora_requests: set[LoRARequest], lora_mapping: LoRAMapping) -> None:
        if not self.lora_manager:
            raise RuntimeError("LoRA is not enabled.")
        self.lora_manager.set_active_adapters(lora_requests, lora_mapping)

    def remove_all_loras(self):
        if not self.lora_manager:
            raise RuntimeError("LoRA is not enabled.")
        self.lora_manager.remove_all_adapters()

    def get_kv_cache_spec(self) -> dict[str, KVCacheSpec]:
        """
        Generates the KVCacheSpec by parsing the kv cache format from each
        Attention module in the static forward context.
        Returns:
            KVCacheSpec: A dictionary mapping layer names to their KV cache
            format. Layers that do not need KV cache are not included.
        """

        forward_ctx = self.vllm_config.compilation_config.static_forward_context
        block_size = self.vllm_config.cache_config.block_size
        kv_cache_spec: dict[str, KVCacheSpec] = {}
        cache_dtype_str = self.vllm_config.cache_config.cache_dtype
        for layer_name, attn_module in forward_ctx.items():
            if isinstance(attn_module, FusedMoE):
                continue

            # TODO: Support other attention modules, e.g., sliding window,
            # cross-attention
            if isinstance(attn_module, Attention):
                if attn_module.attn_type == AttentionType.DECODER:
                    kv_cache_spec[layer_name] = FullAttentionSpec(block_size=block_size,
                                                                  num_kv_heads=attn_module.num_kv_heads,
                                                                  head_size=attn_module.head_size,
                                                                  dtype=self.kv_cache_dtype)
                elif attn_module.attn_type in (AttentionType.ENCODER, AttentionType.ENCODER_ONLY):
                    # encoder-only attention does not need KV cache.
                    continue
                elif attn_module.attn_type == AttentionType.ENCODER_DECODER:
                    raise NotImplementedError
                else:
                    raise ValueError(f"Unknown attention type: {attn_module.attn_type}")
            elif isinstance(attn_module, MLAAttention):
                if layer_name in kv_cache_spec:
                    continue
                kv_cache_spec[layer_name] = MLAAttentionSpec(
                    block_size=block_size,
                    num_kv_heads=1,
                    head_size=attn_module.head_size,
                    dtype=self.kv_cache_dtype,
                    cache_dtype_str=cache_dtype_str,
                )

        return kv_cache_spec

    def _update_states(self, scheduler_output: "SchedulerOutput") -> bool:
        """Update the cached states and the persistent batch with the scheduler
        output.

        The updated states are used by the `_prepare_inputs` function to create
        the input GPU tensors for the model.

        The SamplingMetadata is updated and copied to the GPU if there is a
        new/resumed/paused/finished request in the batch.
        """
        # Remove finished requests from the cached states.
        for req_id in scheduler_output.finished_req_ids:
            self.requests.pop(req_id, None)

        # Remove the finished requests from the persistent batch.
        # NOTE(woosuk): There could be an edge case where finished_req_ids and
        # scheduled_req_ids overlap. This happens when a request is aborted and
        # then resubmitted with the same ID. In this case, we treat them as two
        # distinct requests - clearing the cached states for the first request
        # and handling the second as a new request.
        removed_req_indices: list[int] = []
        for req_id in scheduler_output.finished_req_ids:
            req_index = self.input_batch.remove_request(req_id)
            if req_index is not None:
                removed_req_indices.append(req_index)
            if req_id in self.input_batch.req_type:
                del self.input_batch.req_type[req_id]

        # Free the cached encoder outputs.
        for mm_hash in scheduler_output.free_encoder_mm_hashes:
            self.encoder_cache.pop(mm_hash, None)

        # Remove the unscheduled requests from the persistent batch.
        # NOTE(woosuk): The unscheduled requests are either preempted requests
        # or running requests that are not scheduled in this step. We remove
        # them from the persistent batch but keep their cached states since
        # they will be scheduled again sometime in the future.
        scheduled_req_ids = scheduler_output.num_scheduled_tokens.keys()
        cached_req_ids = self.input_batch.req_id_to_index.keys()
        unscheduled_req_ids = cached_req_ids - scheduled_req_ids
        # NOTE(woosuk): The persistent batch optimization assumes that
        # consecutive batches contain mostly the same requests. If batches
        # have low request overlap (e.g., alternating between two distinct
        # sets of requests), this optimization becomes very inefficient.
        for req_id in unscheduled_req_ids:
            req_index = self.input_batch.remove_request(req_id)
            assert req_index is not None
            removed_req_indices.append(req_index)

        req_ids_to_add: list[str] = []
        # Add new requests to the cached states.
        for new_req_data in scheduler_output.scheduled_new_reqs:
            req_id = new_req_data.req_id
            sampling_params = new_req_data.sampling_params
            pooling_params = new_req_data.pooling_params
            if sampling_params and \
                sampling_params.sampling_type == SamplingType.RANDOM_SEED:
                generator = torch.Generator(device=self.device)
                generator.manual_seed(sampling_params.seed)
            else:
                generator = None
            if pooling_params:
                assert (task := pooling_params.task) is not None, ("You did not set `task` in the API")

                model = cast(VllmModelForPooling, self.model)
                to_update = model.pooler.get_pooling_updates(task)
                assert to_update is not None, (f"{pooling_params.task=} is not supported by the model")
                to_update.apply(pooling_params)

            self.requests[req_id] = CachedRequestState(
                req_id=req_id,
                prompt_token_ids=new_req_data.prompt_token_ids,
                mm_features=new_req_data.mm_features,
                sampling_params=sampling_params,
                pooling_params=pooling_params,
                generator=generator,
                block_ids=new_req_data.block_ids,
                num_computed_tokens=new_req_data.num_computed_tokens,
                output_token_ids=[],
                lora_request=new_req_data.lora_request,
            )

            # Only relevant for models using M-RoPE (e.g, Qwen2-VL)
            if self.uses_mrope:
                self.requests[req_id].mrope_positions, \
                    self.requests[req_id].mrope_position_delta = \
                    self.model.model.get_mrope_input_positions(
                        self.requests[req_id].prompt_token_ids,
                        self.requests[req_id].mm_features
                )

            req_ids_to_add.append(req_id)
        # Update the states of the running/resumed requests.
        is_last_rank = get_pp_group().is_last_rank
        req_data = scheduler_output.scheduled_cached_reqs
        for i, req_id in enumerate(req_data.req_ids):
            req_state = self.requests[req_id]
            num_computed_tokens = req_data.num_computed_tokens[i]
            new_block_ids = req_data.new_block_ids[i]
            resumed_from_preemption = req_id in getattr(req_data, "resumed_req_ids", set())
            num_output_tokens = req_data.num_output_tokens[i]
            req_state.num_computed_tokens = num_computed_tokens

            if not is_last_rank:
                # When using PP, the scheduler sends the sampled tokens back,
                # because there's no direct communication between the first-
                # stage worker and the last-stage worker.
                new_token_ids = req_data.new_token_ids[i]
                # Add the sampled token(s) from the previous step (if any).
                # This doesn't include "unverified" tokens like spec tokens.
                num_new_tokens = (num_computed_tokens + len(new_token_ids) - req_state.num_tokens)
                if num_new_tokens == 1:
                    # Avoid slicing list in most common case.
                    req_state.output_token_ids.append(new_token_ids[-1])
                elif num_new_tokens > 0:
                    req_state.output_token_ids.extend(new_token_ids[-num_new_tokens:])

            # Update the block IDs.
            if not resumed_from_preemption:
                if new_block_ids is not None:
                    # Append the new blocks to the existing block IDs.
                    for block_ids, new_ids in zip(req_state.block_ids, new_block_ids):
                        block_ids.extend(new_ids)
            else:
                assert new_block_ids is not None
                # The request is resumed from preemption.
                # Replace the existing block IDs with the new ones.
                req_state.block_ids = new_block_ids

            req_index = self.input_batch.req_id_to_index.get(req_id)
            if req_index is None:
                # The request is not in the persistent batch.
                # The request was either preempted and resumed later, or was not
                # scheduled in the previous step and needs to be added again.

                if self.use_async_scheduling and num_output_tokens > 0:
                    # We must recover the output token ids for resumed requests in the
                    # async scheduling case, so that correct input_ids are obtained.
                    resumed_token_ids = req_data.all_token_ids[req_id]
                    req_state.output_token_ids = resumed_token_ids[-num_output_tokens:]

                req_ids_to_add.append(req_id)
                continue

            # Update the persistent batch.
            self.input_batch.num_computed_tokens_cpu[req_index] = (num_computed_tokens)
            if new_block_ids is not None:
                self.input_batch.block_table.append_row(new_block_ids, req_index)

            # For the last rank, we don't need to update the token_ids_cpu
            # because the sampled tokens are already cached.
            if not is_last_rank:
                # Add new_token_ids to token_ids_cpu.
                start_token_index = num_computed_tokens
                end_token_index = num_computed_tokens + len(new_token_ids)
                self.input_batch.token_ids_cpu[req_index, start_token_index:end_token_index] = new_token_ids
                self.input_batch.num_tokens_no_spec[req_index] = end_token_index
                # NOTE(woosuk): `num_tokens` here may include spec decode tokens
                self.input_batch.num_tokens[req_index] = end_token_index
            # Add spec_token_ids to token_ids_cpu.
            spec_token_ids = \
                scheduler_output.scheduled_spec_decode_tokens.get(
                    req_id, ())
            if spec_token_ids:
                num_spec_tokens = len(spec_token_ids)
                start_index = self.input_batch.num_tokens_no_spec[req_index]
                end_token_index = start_index + num_spec_tokens
                self.input_batch.token_ids_cpu[req_index, start_index:end_token_index] = spec_token_ids
                # NOTE(woosuk): `num_tokens` here may include spec tokens.
                self.input_batch.num_tokens[req_index] += num_spec_tokens

        # Check if the batch has changed. If not, we can skip copying the
        # sampling metadata from CPU to GPU.
        batch_changed = len(removed_req_indices) > 0 or len(req_ids_to_add) > 0

        # Add the new or resumed requests to the persistent batch.
        # The smaller empty indices are filled first.
        removed_req_indices = sorted(removed_req_indices, reverse=True)
        for req_id in req_ids_to_add:
            req_state = self.requests[req_id]
            req_index = removed_req_indices.pop() if removed_req_indices else None
            self.input_batch.add_request(req_state, req_index)

        # Condense the batched states if there are empty indices.
        if removed_req_indices:
            self.input_batch.condense(removed_req_indices)

        if batch_changed:
            self.input_batch.refresh_sampling_metadata()
        return batch_changed

    def _extract_mm_kwargs(
        self,
        scheduler_output: "SchedulerOutput",
    ) -> BatchedTensorInputs:
        if self.is_multimodal_raw_input_supported:  # noqa: SIM102
            if scheduler_output:
                mm_kwargs = list[MultiModalKwargsItem]()
                for req in scheduler_output.scheduled_new_reqs:
                    req_mm_kwargs = req.mm_kwargs
                    if not isinstance(req_mm_kwargs, list):
                        req_mm_kwargs = list(req_mm_kwargs)
                    mm_kwargs.extend(req_mm_kwargs)

                # Input all modalities at once
                self.model.model = cast(SupportsMultiModal, self.model.model)
                mm_kwargs_combined: BatchedTensorInputs = {}
                for _, _, mm_kwargs_group in group_mm_kwargs_by_modality(
                        mm_kwargs,
                        device=self.device,
                        pin_memory=self.pin_memory,
                        merge_by_field_config=self.model.model.merge_by_field_config,
                ):
                    mm_kwargs_combined.update(mm_kwargs_group)

                return mm_kwargs_combined

        return {}

    # source: vllm/v1/worker/gpu_model_runner.py
    def _execute_mm_encoder(self, scheduler_output: "SchedulerOutput", req_ids: list[str]):
        scheduled_encoder_inputs = scheduler_output.scheduled_encoder_inputs
        if not scheduled_encoder_inputs:
            return

        # Batch the multi-modal inputs.
        mm_kwargs = list[MultiModalKwargsItem]()
        # List of tuple (mm_hash, pos_info)
        mm_hashes_pos = list[tuple[str, PlaceholderRange]]()
        for req_id in req_ids:
            encoder_input_ids = scheduled_encoder_inputs.get(req_id, None)
            if not encoder_input_ids:
                continue
            req_state = self.requests[req_id]

            for mm_input_id in encoder_input_ids:
                mm_feature = req_state.mm_features[mm_input_id]
                mm_hash = mm_feature.identifier
                mm_kwargs.append(mm_feature.data)
                mm_hashes_pos.append((mm_hash, mm_feature.mm_position))

        if not mm_kwargs:
            return

        # Batch mm inputs as much as we can: if a request in the batch has
        # multiple modalities or a different modality than the previous one,
        # we process it separately to preserve item order.

        # TODO (attafosu): Follow-up on the resolution to this.
        # The ordering of the encoder outputs needs to match the request ids
        # after fetching the embeddings.
        # For now, we'll restrict mm support to just a single prefill at a time - # noqa E501
        # Or that requests in the batch should have distinct modalities,

        # FIXME(ywang96): This is a hacky way to deal with multiple modalities
        # in the same batch while still being able to benefit from batching
        # multimodal inputs. The proper solution should be reordering the
        # encoder outputs.
        encoder_outputs = []
        self.model.model = cast(SupportsMultiModal, self.model.model)
        for _, num_items, mm_kwargs_group in group_mm_kwargs_by_modality(
                mm_kwargs,
                device=self.device,
                pin_memory=self.pin_memory,
                merge_by_field_config=self.model.model.merge_by_field_config,
        ):
            # Run the encoder.
            # `curr_group_outputs` is either of the following:
            # 1. A tensor of shape (num_items, feature_size, hidden_size)
            # in case feature_size is fixed across all multimodal items.
            # 2. A list or tuple (length: num_items) of tensors, each of shape
            # (feature_size, hidden_size) in case the feature size is dynamic
            # depending on the input multimodal items.
            curr_group_outputs = self.model.embed_multimodal(**mm_kwargs_group)

            sanity_check_mm_encoder_outputs(
                curr_group_outputs,
                expected_num_items=num_items,
            )

            for output in curr_group_outputs:
                encoder_outputs.append(output)

        # FIXME (attafosu) Reorder the encoder outputs to match the request ids.
        # This will be necessary after mm prefill batching constraints are removed # noqa E501

        # Cache the encoder outputs.
        for (mm_hash, pos_info), output in zip(
                mm_hashes_pos,
                encoder_outputs,
        ):
            if req_id not in self.encoder_cache:
                self.encoder_cache[req_id] = {}

            self.encoder_cache[mm_hash] = scatter_mm_placeholders(
                output,
                is_embed=pos_info.is_embed.to(
                    device=output.device) if pos_info.is_embed is not None else pos_info.is_embed,
            )

    # modified from: vllm/v1/worker/gpu_model_runner.py
    def _gather_mm_embeddings(
        self,
        scheduler_output: "SchedulerOutput",
        req_ids: list[str],
        shift_computed_tokens: int = 0,
        total_num_scheduled_tokens: Optional[int] = None,
    ) -> tuple[list[torch.Tensor], torch.Tensor]:
        total_num_scheduled_tokens = total_num_scheduled_tokens or scheduler_output.total_num_scheduled_tokens

        mm_embeds = list[torch.Tensor]()
        is_mm_embed = self.is_mm_embed.cpu
        is_mm_embed[:total_num_scheduled_tokens] = False

        req_start_idx = 0
        for req_id in req_ids:
            num_scheduled_tokens = scheduler_output.num_scheduled_tokens[req_id]
            req_state = self.requests[req_id]
            num_computed_tokens = \
                req_state.num_computed_tokens + shift_computed_tokens
            for mm_feature in req_state.mm_features:
                pos_info = mm_feature.mm_position
                start_pos = pos_info.offset
                num_encoder_tokens = pos_info.length

                # The encoder output is needed if the two ranges overlap:
                # [num_computed_tokens,
                #  num_computed_tokens + num_scheduled_tokens) and
                # [start_pos, start_pos + num_encoder_tokens)
                if start_pos >= num_computed_tokens + num_scheduled_tokens:
                    # The encoder output is not needed in this step.
                    break
                if start_pos + num_encoder_tokens <= num_computed_tokens:
                    # The encoder output is already processed and stored
                    # in the decoder's KV cache.
                    continue

                start_idx = max(num_computed_tokens - start_pos, 0)
                end_idx = min(num_computed_tokens - start_pos + num_scheduled_tokens, num_encoder_tokens)
                assert start_idx < end_idx
                mm_hash = mm_feature.identifier
                encoder_output = self.encoder_cache.get(mm_hash, None)
                assert encoder_output is not None,\
                      f"Encoder cache miss for {mm_hash}."
                encoder_output = self.encoder_cache[mm_hash]

                if (is_embed := pos_info.is_embed) is not None:
                    is_embed = is_embed[start_idx:end_idx]

                mm_embeds_item = gather_mm_placeholders(
                    encoder_output[start_idx:end_idx],
                    is_embed=is_embed,
                )
                req_start_pos = req_start_idx + start_pos - num_computed_tokens
                is_mm_embed[req_start_pos+start_idx:req_start_pos + end_idx] \
                    = True

                # Only whole mm items are processed
                mm_embeds.append(mm_embeds_item)
            req_start_idx += num_scheduled_tokens

        is_mm_embed = self.is_mm_embed.copy_to_gpu(total_num_scheduled_tokens)

        return mm_embeds, is_mm_embed

    def get_model(self) -> torch.nn.Module:
        if isinstance(self.model, HpuModelAdapter):
            return self.model.model
        assert self.model is not None
        return self.model

    def is_decoder_only(self, req_id) -> bool:
        return bool(req_id in self.input_batch.req_type and \
            self.input_batch.req_type[req_id] == "decode")

    def maybe_set_chunked_attention_layers(self, model):
        if hasattr(model.config, 'text_config'):  # noqa: SIM102
            if hasattr(model.config.text_config, 'attention_chunk_size'):  # noqa: SIM102
                if model.config.text_config.attention_chunk_size > 0:
                    self.model_has_chunked_attention = True
                    try:
                        for layer in model.language_model.model.layers:
                            if "ChunkedLocalAttention" in layer.self_attn.attn.get_attn_backend().__name__:
                                layer.self_attn.attn.impl.is_chunked_attention = True
                    except Exception:
                        pass

    def _get_prompts_and_decodes(
        self,
        scheduler_output: "SchedulerOutput",
    ) -> PromptDecodeInfo:
        total_num_scheduled_tokens = scheduler_output.total_num_scheduled_tokens
        assert total_num_scheduled_tokens > 0
        num_reqs = self.input_batch.num_reqs
        assert num_reqs > 0
        #TODO: remove later

        requests_type = {}
        if scheduler_output.kv_connector_metadata:
            for req in scheduler_output.kv_connector_metadata.reqs_to_save:
                requests_type[req] = 'prefill'
            for req in scheduler_output.kv_connector_metadata.reqs_to_recv:
                requests_type[req] = 'decode'
            requests = scheduler_output.kv_connector_metadata.reqs_to_save | \
                        scheduler_output.kv_connector_metadata.reqs_to_recv
        else:
            requests = None

        # Traverse decodes first
        decode_req_ids = []
        num_computed_tokens_decode = []
        for i in range(num_reqs):
            req_id = self.input_batch.req_ids[i]
            assert req_id is not None
            # P case assigment
            if requests is not None and req_id not in self.input_batch.req_type:
                for request in requests:
                    if request == req_id:
                        self.input_batch.req_type[req_id] = requests_type[req_id]
                        break

            num_computed_tokens = self.input_batch.num_computed_tokens_cpu[i]
            num_prompt_tokens = self.input_batch.num_prompt_tokens[i]
            num_scheduled_tokens = scheduler_output.num_scheduled_tokens[req_id]
            if num_computed_tokens < num_prompt_tokens and \
                not self.is_decoder_only(req_id):
                # This is prompt
                break

            # This is decode
            # NOTE(chendi): To support spec decode,
            # we don't assume num_scheduled_tokens == 1.

            decode_req_ids.append(req_id)
            num_computed_tokens_decode.append(int(num_computed_tokens + 1))

        if self.profiler.enabled:
            self.profiler_counter_helper.capture_decode_seq_stats(num_computed_tokens_decode)

        # Traverse prompts
        prompt_req_ids = []
        prompt_scheduled_tokens = []
        for i in range(len(decode_req_ids), num_reqs):
            req_id = self.input_batch.req_ids[i]
            assert req_id is not None

            num_computed_tokens = self.input_batch.num_computed_tokens_cpu[i]
            num_prompt_tokens = self.input_batch.num_prompt_tokens[i]
            num_scheduled_tokens = scheduler_output.num_scheduled_tokens[req_id]

            # Must be prompt
            assert num_computed_tokens < num_prompt_tokens
            # NOTE(kzawora): In preempted sequences, num_output_tokens can be > 0, and still be a valid prefill

            prompt_req_ids.append(req_id)
            prompt_scheduled_tokens.append(num_scheduled_tokens)

        return PromptDecodeInfo(prompt_req_ids, decode_req_ids, prompt_scheduled_tokens)

    def _generate_req_id_output_token_ids_lst(self,
                                              request_ids: Optional[list[str]] = None,
                                              pad_to: Optional[int] = None,
                                              logits_reqs=None):
        req_id_output_token_ids: dict[str, list[int]] = \
            {req_id: req.output_token_ids
                for req_id, req in self.requests.items()}
        if request_ids is not None:
            req_id_output_token_ids = {req_id: req_id_output_token_ids[req_id] for req_id in request_ids}
        req_id_output_token_ids_lst = list(req_id_output_token_ids.items())
        if logits_reqs and len(req_id_output_token_ids_lst) > len(logits_reqs):
            # Merged prefill case: remove requests without logits
            req_id_output_token_ids_lst = [r for r in req_id_output_token_ids_lst if r[0] in logits_reqs]
        else:
            if pad_to is not None and len(req_id_output_token_ids_lst) > 0:
                while len(req_id_output_token_ids_lst) < pad_to:
                    req_id_output_token_ids_lst.append(req_id_output_token_ids_lst[0])
        return req_id_output_token_ids_lst

    def _prepare_sampling(self,
                          batch_changed: bool,
                          request_ids: Union[None, list[str]] = None,
                          pad_to: Optional[int] = None,
                          logits_reqs=None) -> SamplingMetadata:
        # Create the sampling metadata.
        req_id_output_token_ids_lst = \
            self._generate_req_id_output_token_ids_lst(request_ids, \
                                                       pad_to, logits_reqs)
        sampling_metadata = self.input_batch.make_selective_sampling_metadata(req_id_output_token_ids_lst,
                                                                              skip_copy=not batch_changed)
        return sampling_metadata

    def get_habana_paged_attn_buffers(self, block_tables, slot_mapping, batch_size):
        last_block_usage = [slot[0] % self.block_size + 1 for slot in slot_mapping]
        block_groups = [[i] * len(bt) for i, bt in enumerate(block_tables)]
        block_usage = [[self.block_size] * (len(bt) - 1) + [lbu] for bt, lbu in zip(block_tables, last_block_usage)
                       if bt]
        block_list = flatten(block_tables)
        block_groups = flatten(block_groups)
        block_usage = flatten(block_usage)
        assert len(block_list) == len(block_groups)
        assert len(block_list) == len(block_usage)

        padding_fn = None
        block_bucket_size: int
        if self.use_contiguous_pa:
            block_bucket_size = max(max(block_list) + 1, len(block_list))
            block_bucket_size = \
                self.bucketing_manager.find_decode_bucket(batch_size,
                                                          block_bucket_size)[2]
            block_bucket_size += self.get_dp_padding(block_bucket_size)

            indices: list[Any]
            indices = [None] * block_bucket_size
            for i, bid in enumerate(block_list):
                indices[bid] = i

            def padding_fn(tensor, pad_value):
                return gather_list(tensor, indices, pad_value)
        else:
            block_bucket_size = \
                self.bucketing_manager.find_decode_bucket(batch_size,
                                                          len(block_list))[2]
            block_bucket_size += self.get_dp_padding(block_bucket_size)

            def padding_fn(tensor, pad_value):
                return pad_list(tensor, block_bucket_size, itertools.repeat(pad_value))

        block_list = padding_fn(block_list, self._PAD_BLOCK_ID)
        block_groups = padding_fn(block_groups, -1)
        block_usage = padding_fn(block_usage, 1)

        block_list = torch.tensor(block_list, dtype=torch.long, device='cpu')
        block_groups = torch.tensor(block_groups, dtype=torch.long, device='cpu')
        block_usage = torch.tensor(block_usage, dtype=self.model_config.dtype, device='cpu')
        return block_list, block_groups, block_usage

    def _align_and_pad_mrope_positions(self, req_ids: list[str], context_lens: list[int], query_lens: list[int],
                                       bucketing: tuple[int, int], padding_gen: int) -> torch.Tensor:
        target_bs, target_len = bucketing
        out_shape = (3, target_len) if target_bs == 1 \
            else (target_bs, target_len)

        mrope_position_tensor = torch.full(out_shape, padding_gen, dtype=torch.int32, device='cpu')
        dst_start = 0
        dst_end = dst_start
        for b_idx, req_id in enumerate(req_ids):
            cl = context_lens[b_idx]
            qsl = query_lens[b_idx]
            assert self.requests[req_id].mrope_positions is not None
            input_mrope_position = \
                self.requests[req_id].mrope_positions[:, cl:cl + qsl] # type: ignore[index]
            dst_end = dst_start + qsl
            mrope_position_tensor[:, dst_start:dst_end].copy_(input_mrope_position, non_blocking=True)

            # Update dst_start depending on if pos_ids of requests are meant to be adjacent # noqa 501
            if target_bs == 1:
                dst_start = dst_end
            else:
                dst_start += target_len
        return mrope_position_tensor

    def _skip_bucketing(self, seq_lens, num_blocks):
        return (len(seq_lens), 0, 0)

    def _bucketize_merged_prompt(self, seq_lens, num_blocks):
        seq = sum(seq_lens)
        num_blocks = sum(num_blocks)
        seq = self.bucketing_manager.find_prompt_bucket(1, seq, num_blocks)[1]
        num_blocks = round_up(num_blocks, 32)
        return (1, seq, num_blocks)

    def _bucketize_2d_prompt(self, seq_lens, num_blocks):
        bs = len(seq_lens)
        if bs > self.max_prefill_batch_size:
            raise BucketingFailedException
        seq = max(seq_lens)
        num_blocks = max(num_blocks) if len(num_blocks) > 0 else 0
        bs, seq, num_blocks = self.bucketing_manager.find_prompt_bucket(bs, seq, num_blocks)
        return (bs, seq, num_blocks)

    def _get_prompt_bucketing_fn(self):
        if self.unified_attn:
            return self._skip_bucketing
        elif self.use_merged_prefill:
            return self._bucketize_merged_prompt
        else:
            return self._bucketize_2d_prompt

    def _can_merge_prefill_contents(self, lhs, rhs):
        combined_num_tokens = lhs.get_num_tokens() + rhs.get_num_tokens()
        bucketing_fn = self._get_prompt_bucketing_fn()
        try:
            target_bs, target_seq, target_blocks = bucketing_fn(combined_num_tokens, [])
        except BucketingFailedException:
            return False
        target_bs, target_seq, target_blocks = bucketing_fn(combined_num_tokens, [])
        return target_bs <= self.max_prefill_batch_size and\
            target_bs * target_seq <= self.max_num_tokens

    def _extract_prefill_batch_contents(self, num_prefills, num_decodes, num_scheduled_tokens, warmup=False):
        # DECODES are the first num_decodes REQUESTS.
        # PREFILLS are the next num_reqs - num_decodes REQUESTS.
        num_reqs = num_prefills + num_decodes
        block_table_cpu_tensor = self.input_batch.block_table[0].get_cpu_tensor()
        all_batch_contents = [BatchContents()]

        for batch_idx in range(num_decodes, num_reqs):
            req_id = self.input_batch.req_ids[batch_idx]
            seq_num_computed_tokens = self.input_batch.num_computed_tokens_cpu[batch_idx]
            seq_num_scheduled_tokens = num_scheduled_tokens[batch_idx]

            token_ids = self.input_batch.token_ids_cpu[batch_idx, seq_num_computed_tokens:seq_num_computed_tokens +
                                                       seq_num_scheduled_tokens].tolist()

            num_blocks = round_up(seq_num_computed_tokens + seq_num_scheduled_tokens,
                                  self.block_size) // self.block_size
            blocks = block_table_cpu_tensor[batch_idx, :num_blocks].tolist()
            if not warmup:
                blocks = [self.defragmenter.resolve(b) for b in blocks]
            #NOTE(kzawora): In non-preemption scenario,
            # self.input_batch.num_prompt_tokens[batch_idx] == self.input_batch.num_tokens[batch_idx].
            # In preemption scenario num_tokens will also include the tokens emitted before preemption
            num_prompt_tokens = self.input_batch.num_prompt_tokens[batch_idx]
            num_output_logits = max(0, seq_num_computed_tokens + seq_num_scheduled_tokens - num_prompt_tokens + 1)
            logits_positions = list(range(seq_num_scheduled_tokens - num_output_logits, seq_num_scheduled_tokens))

            new_batch_contents = BatchContents(
                req_ids=[req_id],
                token_ids=[token_ids],
                context_lens=[seq_num_computed_tokens],
                prompt_lens=[num_prompt_tokens],
                blocks=[blocks],
                logits_positions=[logits_positions],
            )
            if self._can_merge_prefill_contents(all_batch_contents[-1], new_batch_contents):
                merge_contents(all_batch_contents[-1], new_batch_contents)
            else:
                all_batch_contents.append(new_batch_contents)

        num_real_prefill_batches = 0
        for content in all_batch_contents:
            if len(content.req_ids) > 0:
                num_real_prefill_batches += 1

        num_pad_across_dp = self.get_dp_padding(num_real_prefill_batches)
        return all_batch_contents, num_pad_across_dp

    def _make_attn_bias(self, context_groups, token_groups):
        dtype = self.dtype
        is_causal = True  # TODO: add support for non-causal tasks
        context_groups = torch.tensor(context_groups, device='cpu', dtype=torch.int16)
        context_groups = context_groups.repeat_interleave(self.block_size, dim=-1)
        context_len = context_groups.size(-1)
        token_groups = torch.tensor(token_groups, device='cpu', dtype=torch.int16)
        num_queries = token_groups.size(-1)
        seq_groups = torch.cat([context_groups, token_groups], dim=-1)
        attn_mask = seq_groups.unflatten(-1, (1, -1)) != token_groups.unflatten(-1, (-1, 1))
        if is_causal:
            causal_mask = torch.ones(num_queries, num_queries, device='cpu', dtype=torch.bool)
            causal_mask = torch.triu(causal_mask, diagonal=1).unsqueeze(0)
            attn_mask[:, :, context_len:].logical_or_(causal_mask)
        attn_mask = attn_mask.to(dtype).masked_fill_(attn_mask, -math.inf)

        return attn_mask.unflatten(0, (1, -1))

    def _form_prefill_batch(self, contents):
        if len(contents.req_ids) == 0:
            return PrefillInputData()

        token_ids = contents.token_ids
        req_ids = contents.req_ids
        query_lens = [len(tids) for tids in contents.token_ids]
        if self.profiler.enabled:
            self.profiler_counter_helper.capture_prompt_seq_stats(query_lens)
        context_lens = contents.context_lens

        token_positions = [list(range(cl, cl + ql)) for cl, ql in zip(context_lens, query_lens)]

        block_assignment = [[divmod(pos, self.block_size) for pos in positions] for positions in token_positions]

        token_slots = [[blocks[bi] * self.block_size + bo for bi, bo in assignment]
                       for blocks, assignment in zip(contents.blocks, block_assignment)]
        token_groups = [[i] * len(tid) for i, tid in enumerate(token_ids)]
        num_context_blocks = [round_up(ctx_len, self.block_size) // self.block_size for ctx_len in context_lens]
        context_blocks: list = [blocks[:num] for blocks, num in zip(contents.blocks, num_context_blocks)]
        num_context_blocks = [len(b) for b in context_blocks]
        context_groups = [[i] * b for i, b in enumerate(num_context_blocks)]
        has_context = sum(context_lens) > 0
        target_bs, target_seq, target_blocks = self._get_prompt_bucketing_fn()(query_lens, num_context_blocks)

        target_bs += self.get_dp_padding(target_bs)
        target_seq += self.get_dp_padding(target_seq)
        target_blocks += self.get_dp_padding(target_blocks)

        # NOTE: If model does not support multimodal inputs, we pad here.
        # For models with multimodal support, we may want to get embeddings
        # for the valid tokens before padding.
        # This would require getting multimodal input embeddings here as well
        token_ids = align_and_pad(contents.token_ids, (target_bs, target_seq), itertools.repeat(-1))
        # Update query_lens and context_lens after padding
        query_lens.extend([0] * (target_bs - len(query_lens)))
        context_lens.extend([0] * (target_bs - len(context_lens)))

        # If the model uses M-RoPE, we need to fill
        # and pad the M-RoPE positions for the scheduled prefill tokens
        if self.uses_mrope:
            token_positions = self._align_and_pad_mrope_positions(
                contents.req_ids,
                context_lens,
                query_lens,
                (target_bs, target_seq),
                -1,
            )

        else:
            token_positions = align_and_pad(token_positions, (target_bs, target_seq), itertools.repeat(-1))
        token_slots = align_and_pad(token_slots, (target_bs, target_seq), itertools.repeat(-1))
        token_groups = align_and_pad(token_groups, (target_bs, target_seq), itertools.repeat(-1))
        context_blocks = align_and_pad(context_blocks, (target_bs, target_blocks), itertools.repeat(-1))
        context_groups = align_and_pad(context_groups, (target_bs, target_blocks), itertools.repeat(-1))

        # TODO: cycle through dummy slots and blocks
        # dummy_slots = itertools.cycle(
        #    range(self._PAD_SLOT_ID, self._PAD_SLOT_ID + self.block_size))

        cur_offset = 0
        logits_indices = []
        logits_requests = []
        for req_id, qlen, log_pos in zip(req_ids, query_lens, contents.logits_positions):
            source = [cur_offset + x for x in log_pos]
            dest = [req_id] * len(log_pos)
            logits_indices.extend(source)
            logits_requests.extend(dest)
            if self.use_merged_prefill:
                cur_offset += qlen
            else:
                cur_offset += len(token_ids[0])

        attn_bias = None
        if self.use_merged_prefill:
            attn_bias = self._make_attn_bias(context_groups, token_groups)
            attn_bias = attn_bias.to('hpu', non_blocking=True)
        else:
            attn_bias = None

        logits_indices = pad_list(logits_indices, round_up(len(logits_indices), self.logits_rounding),
                                  itertools.repeat(-1))

        query_lens = async_h2d_copy(query_lens, dtype=torch.int32)
        token_ids = async_h2d_copy(token_ids, dtype=torch.int32)
        token_positions = async_h2d_copy(token_positions, dtype=torch.int32)
        token_slots = async_h2d_copy(token_slots, dtype=torch.int64)
        logits_indices = async_h2d_copy(logits_indices, dtype=torch.int32)
        context_lens = async_h2d_copy(context_lens, dtype=torch.int32)
        context_blocks_t: Optional[torch.tensor]
        context_blocks_t = async_h2d_copy(context_blocks, dtype=torch.int32).flatten() if has_context else None

        attn_metadata = HPUAttentionMetadataV1.make_prefill_metadata(seq_lens_tensor=query_lens,
                                                                     context_lens_tensor=context_lens,
                                                                     slot_mapping=token_slots,
                                                                     block_list=context_blocks_t,
                                                                     attn_bias=attn_bias,
                                                                     block_size=self.block_size)
        return PrefillInputData(request_ids=[req_ids],
                                prompt_lens=[query_lens],
                                token_ids=[token_ids],
                                position_ids=[token_positions],
                                attn_metadata=[attn_metadata],
                                logits_indices=[logits_indices],
                                logits_requests=[logits_requests])

    def _form_unified_prefill_batch(self, contents):
        if len(contents.req_ids) == 0:
            return PrefillInputData()

        token_ids = contents.token_ids
        req_ids = contents.req_ids
        query_lens = [len(tids) for tids in contents.token_ids]
        prompt_lens = contents.prompt_lens
        if self.profiler.enabled:
            self.profiler_counter_helper.capture_prompt_seq_stats(query_lens)
        context_lens = contents.context_lens

        batch_data = create_unified_batch(
            token_ids=token_ids,
            block_size=self.block_size,
            block_table=contents.blocks,
            context_lengths=context_lens,
            query_lengths=query_lens,
            prompt_lengths=prompt_lens,
            dtype=self.dtype,
            contiguous_kv=self.use_contiguous_pa,
            bucketing_fn=self.unified_bucketing_fn,
            get_dp_padding_fn=self.get_dp_padding,
        )

        (token_ids_t, token_positions_t, logits_indices_t, logits_groups, attn_metadata) = batch_data
        logits_requests = [req_ids[lg] for lg in logits_groups]
        return PrefillInputData(request_ids=[req_ids],
                                prompt_lens=[None],
                                token_ids=[token_ids_t.unsqueeze(0)],
                                attn_metadata=[attn_metadata],
                                position_ids=[token_positions_t.unsqueeze(0)],
                                logits_indices=[logits_indices_t],
                                logits_requests=[logits_requests])

    def _create_dummy_prefill_batch_contents(self, num_prefills: int) -> list[PrefillInputData]:
        req_id = str(-1)
        context_len = 0
        query_len = 128
        prompt_tokens = 128
        token_ids = list(int(i) for i in range(prompt_tokens))
        num_blocks = round_up(context_len + query_len, self.block_size) // self.block_size
        blocks = [0] * num_blocks
        num_output_logits = context_len + query_len - prompt_tokens + 1
        logits_positions = list(range(query_len - num_output_logits, query_len))

        new_batch_contents = BatchContents(
            req_ids=[req_id],
            token_ids=[token_ids],
            context_lens=[context_len],
            blocks=[blocks],
            logits_positions=[logits_positions],
        )

        outputs = [self._form_prefill_batch(new_batch_contents.clone()) for _ in range(num_prefills)]
        return outputs

    def _prepare_prefill_inputs(self, num_prefills, num_decodes,
                                num_scheduled_tokens: list[int]) -> tuple[PrefillInputData, Optional[PrefillInputData]]:
        all_batch_contents, num_pad_across_dp = \
            self._extract_prefill_batch_contents(
                num_prefills, num_decodes, num_scheduled_tokens)
        all_batches = [self._form_prefill_batch(bc) for bc in all_batch_contents]
        merge_contents(all_batches[0], *all_batches[1:])

        dummy_prefill_input_batches = None
        if num_pad_across_dp > 0:
            dummy_prefill_input_batches = \
                self._create_dummy_prefill_batch_contents(num_pad_across_dp)
            merge_contents(dummy_prefill_input_batches[0], *dummy_prefill_input_batches[1:])
        return all_batches[0], dummy_prefill_input_batches[0] if dummy_prefill_input_batches else None

    def _prepare_unified_prefill_inputs(self,
                                        num_prefills,
                                        num_decodes,
                                        num_scheduled_tokens: list[int],
                                        warmup=False) -> tuple[PrefillInputData, None]:

        all_batch_contents, _ = self._extract_prefill_batch_contents(num_prefills, num_decodes, num_scheduled_tokens,
                                                                     warmup)
        all_batches = [self._form_unified_prefill_batch(bc) for bc in all_batch_contents]
        merge_contents(all_batches[0], *all_batches[1:])
        return all_batches[0], None

    def _create_decode_input_data(self,
                                  num_decodes,
                                  num_scheduled_tokens,
                                  context_lens,
                                  block_table_cpu_tensor,
                                  scheduler_output=None) -> DecodeInputData:
        # NOTE(kzawora): the +1 is what causes this entire thing to work,
        # as in the paged attention, we don't fetch just the context from cache,
        # but also kvs for the current token
        num_blocks = np.ceil((context_lens + 1) / self.block_size).astype(np.int32).tolist()

        # PAD FOR STATIC SHAPES.
        padded_batch_size: int
        padded_batch_size = self.bucketing_manager.find_decode_bucket(num_decodes, sum(num_blocks))[0]

        # dp aware padding
        padded_batch_size += self.get_dp_padding(padded_batch_size)

        num_tokens_per_req = num_scheduled_tokens[:num_decodes]
        num_tokens = max(num_tokens_per_req)
        total_num_scheduled_tokens = sum(num_tokens_per_req)
        num_tokens_per_req = num_tokens_per_req + [0] * (padded_batch_size - num_decodes)

        block_tables_list = []
        for i, n in enumerate(num_blocks):
            seq_block_table = block_table_cpu_tensor[i, :n].tolist()
            assert len(seq_block_table) == n
            block_tables_list.extend([seq_block_table] * num_tokens)

        ###################################
        # initialize positions with padding
        # POSITIONS. [batch, num_tokens]
        # NOTE(Chendi): Follow GPU_Model_Runner to use global
        # self.positions_cpu, which updated in prepare_inputs from
        # self.input_batch.num_computed_tokens_cpu[req_indices]
        positions = torch.zeros((padded_batch_size, num_tokens), dtype=torch.int32)
        if num_tokens == 1:
            positions[:num_decodes] = self.positions_cpu[:num_decodes].view(-1, 1)
        else:
            # per request using universal self.positions_cpu then pad
            position_split_tensors = torch.split(self.positions_cpu[:total_num_scheduled_tokens], num_tokens_per_req)
            positions[:num_decodes] = \
                pad_sequence(list(position_split_tensors),
                                batch_first=True,
                                padding_value=0)[:num_decodes]

        padded_index = torch.zeros((padded_batch_size, num_tokens), dtype=torch.int64)
        index = positions.to(torch.int64)[:num_decodes]
        padded_index[:num_decodes] = index

        input_mrope_positions_list: list[list[int]] = [[] for _ in range(3)]
        if self.uses_mrope:
            for idx, req_id in enumerate(self.input_batch.req_ids[:num_decodes]):
                seq_data = self.requests[req_id]
                context_len = context_lens[idx]
                position = context_len
                if seq_data.mrope_position_delta is not None:
                    seq_data.mrope_position_delta = int(seq_data.mrope_position_delta)
                    pos_for_mrope = MRotaryEmbedding \
                        .get_next_input_positions(
                            seq_data.mrope_position_delta,
                            context_len=context_len,
                            seq_len=context_len + 1)
                else:
                    pos_for_mrope = [[position]] * 3
                for idx in range(3):
                    input_mrope_positions_list[idx].extend(pos_for_mrope[idx])

            positions = torch.tensor(input_mrope_positions_list, dtype=torch.int32, device='cpu')

            # Pad the right side of input_mrope_positions by padded_batch_size
            pad_size = padded_batch_size - positions.size(1)
            if pad_size > 0:
                positions = F.pad(positions, (0, pad_size), value=-1, mode='constant')

        ###################################
        # initialize token_ids with padding
        # TOKEN_IDS. [batch, num_tokens]
        # NOTE(Chendi): Follow GPU_Model_Runner to use global
        # self.input_ids_cpu, which updated in prepare_inputs from
        # self.input_batch.token_ids_cpu[:total_num_scheduled_tokens]
        token_ids = torch.zeros((padded_batch_size, num_tokens), dtype=torch.int32)
        if num_tokens == 1:
            token_ids[:num_decodes] = self.input_ids_cpu[:num_decodes].view(-1, 1)
        else:
            token_ids_split_tensors = torch.split(self.input_ids_cpu[:total_num_scheduled_tokens], num_tokens_per_req)
            token_ids[:num_decodes] = \
                pad_sequence(list(token_ids_split_tensors),
                                batch_first=True,
                                padding_value=0)[:num_decodes]

        ###################################
        # SLOT_MAPPING [batch, 1]
        # The "slot" is the "physical index" of a token in the KV cache.
        # Look up the block_idx in the block table (logical<>physical map)
        # to compute this.
        block_number = torch.ones((padded_batch_size, num_tokens), dtype=torch.int32) * self._PAD_BLOCK_ID
        block_number[:num_decodes] = torch.gather(input=block_table_cpu_tensor, dim=1, index=(index // self.block_size))
        block_number.apply_(self.defragmenter.resolve)

        block_offsets = padded_index % self.block_size
        slot_mapping = block_number * self.block_size + block_offsets
        # set an out of range value for the padding tokens so that they
        # are ignored when inserting into the KV cache.
        slot_mapping = slot_mapping[:padded_batch_size]
        dummy_slots = itertools.cycle(range(self._PAD_SLOT_ID, self._PAD_SLOT_ID + self.block_size))
        slot_mapping[num_decodes:].apply_(lambda _, ds=dummy_slots: next(ds))

        #####################################
        # NOTE(Chendi): Since we can't actually do num_tokens = 2,
        # convert to [batch_size * num_tokens, 1]
        if num_tokens > 1:
            token_ids = token_ids.view(-1, 1)
            positions = padded_index.view(-1, 1)
            slot_mapping = slot_mapping.view(-1, 1)

        logits_indices = torch.zeros(padded_batch_size, dtype=torch.int32, device='cpu')

        # NOTE(Chendi): num_tokens might be > 1 in spec decode case,
        # example:
        # num_scheduled_tokens = [2, 1, 2, 1]
        # padded tokens_id = \
        #     [[tok_0, tok_1], [tok_2, pad], [tok_4, tok_4], [tok_6, pad]]
        # num_tokens = 2
        # query_start_loc_list = [2, 3, 6, 7]
        # query_start_loc_cpu = [0, 2, 3, 6, 7]
        # logits_indices = [1, 2, 5, 6] => the last token of each request
        query_start_loc_list = [i * num_tokens + n for i, n in enumerate(num_scheduled_tokens[:num_decodes])]
        query_start_loc_cpu = torch.empty((padded_batch_size + 1, ),
                                          dtype=torch.int32,
                                          device="cpu",
                                          pin_memory=self.pin_memory)
        query_start_loc_np = query_start_loc_cpu.numpy()
        query_start_loc_np[0] = 0
        query_start_loc_np[1:num_decodes + 1] = np.array(query_start_loc_list)

        logits_indices[:num_decodes] = query_start_loc_cpu[1:num_decodes + 1] - 1

        positions_device = async_h2d_copy(positions, device=self.device)
        block_tables_list = self.defragmenter.resolve_all(block_tables_list)

        # CONTEXT_LENS [batch_size]
        block_list, block_groups, block_usage = \
            self.get_habana_paged_attn_buffers(
                block_tables_list,
                slot_mapping.tolist(),
                padded_batch_size * num_tokens
            )

        if self.interleaved_sliding_window and self.sliding_window is not None and self.sliding_window > 0:
            sliding_block_size = (self.sliding_window // self.block_size)
            window_block_tables = [block_table[-sliding_block_size:] for block_table in block_tables_list]
            window_block_list, window_block_groups, window_block_usage = \
                self.get_habana_paged_attn_buffers(
                    window_block_tables, slot_mapping.tolist(),
                    padded_batch_size * num_tokens)

        if self.model_has_chunked_attention:
            chunk_size = (self.model.model.config.text_config.attention_chunk_size // self.block_size)
            seq_lens_block = [len(block_table) for block_table in block_tables_list]
            num_seq_chunks = [math.ceil(sl / chunk_size) - 1 for sl in seq_lens_block]
            block_tables_chunk = [
                block_table[num_seq_chunks[i] * chunk_size:] for i, block_table in enumerate(block_tables_list)
            ]
            chunked_block_list, chunked_block_groups, chunked_block_usage = \
                self.get_habana_paged_attn_buffers(
                    block_tables_chunk, slot_mapping.tolist(),
                    padded_batch_size * num_tokens)

        # CPU<>HPU sync *should not* happen here.
        block_list_device = async_h2d_copy(block_list, device=self.device)
        block_usage_device = async_h2d_copy(block_usage, device=self.device)
        block_groups_device = async_h2d_copy(block_groups, device=self.device)
        slot_mapping_device = async_h2d_copy(slot_mapping, device=self.device)
        window_block_list_device = async_h2d_copy(
            window_block_list,
            device=self.device) if self.interleaved_sliding_window and self.sliding_window is not None else None
        window_block_usage_device = async_h2d_copy(
            window_block_usage,
            device=self.device) if self.interleaved_sliding_window and self.sliding_window is not None else None
        window_block_groups_device = async_h2d_copy(
            window_block_groups,
            device=self.device) if self.interleaved_sliding_window and self.sliding_window is not None else None
        chunked_block_list_device = async_h2d_copy(chunked_block_list,
                                                   device=self.device) if self.model_has_chunked_attention else None
        chunked_block_usage_device = async_h2d_copy(chunked_block_usage,
                                                    device=self.device) if self.model_has_chunked_attention else None
        chunked_block_groups_device = async_h2d_copy(chunked_block_groups,
                                                     device=self.device) if self.model_has_chunked_attention else None

        token_ids_device = async_h2d_copy(token_ids, device=self.device)
        # when DP also enabled, some DP ranks will exeucte dummy run with empty
        # SchedulerOutput, in this case we need skip the prepare_input_ids
        if self.use_async_scheduling and scheduler_output is not None:
            self._prepare_input_ids(scheduler_output)
            if num_tokens == 1:
                token_ids_device[:num_decodes] = self.input_ids_hpu[:num_decodes].view(-1, 1)
            else:
                token_ids_split_tensors = torch.split(self.input_ids_hpu[:total_num_scheduled_tokens],
                                                      num_tokens_per_req)
                token_ids_device[:num_decodes] = \
                    pad_sequence(list(token_ids_split_tensors),
                                    batch_first=True,
                                    padding_value=0)[:num_decodes]

            #####################################
            # NOTE(Chendi): Since we can't actually do num_tokens = 2,
            # convert to [batch_size * num_tokens, 1]
            if num_tokens > 1:
                token_ids_device = token_ids_device.view(-1, 1)

        # call prepare_spec_decode_inputs to get the logits indices and
        if scheduler_output is not None:
            logits_indices, spec_decode_metadata = self._prepare_spec_decode_inputs(scheduler_output, logits_indices,
                                                                                    token_ids_device, num_tokens)
        else:
            spec_decode_metadata = None
        logits_indices_device = async_h2d_copy(logits_indices, device=self.device)

        attn_metadata = HPUAttentionMetadataV1.make_decode_metadata(
            block_list=block_list_device,
            block_usage=block_usage_device,
            block_groups=block_groups_device,
            input_positions=None,
            slot_mapping=slot_mapping_device,
            block_size=self.block_size,
            window_block_list=window_block_list_device,
            window_block_usage=window_block_usage_device,
            window_block_groups=window_block_groups_device,
            chunked_block_list=chunked_block_list_device,
            chunked_block_usage=chunked_block_usage_device,
            chunked_block_groups=chunked_block_groups_device,
        )

        return DecodeInputData(num_decodes=num_decodes,
                               token_ids=token_ids_device,
                               position_ids=positions_device,
                               logits_indices=logits_indices_device,
                               attn_metadata=attn_metadata,
                               spec_decode_metadata=spec_decode_metadata)

    def _prepare_decode_inputs(self,
                               num_decodes,
                               num_scheduled_tokens,
                               scheduler_output=None) -> tuple[DecodeInputData, Optional[DecodeInputData]]:
        # Decodes run as one single padded batch with shape [batch, 1]
        #
        # We need to set _PAD_SLOT_ID for the padding tokens in the
        # slot_mapping, such that the attention KV cache insertion
        # logic knows to ignore those indicies. Otherwise, the
        # padding data can be dummy since we have a causal mask.

        num_pad_across_dp = self.get_dp_padding(num_decodes)
        if num_decodes == 0:
            if num_pad_across_dp > 0:
                dummy_decode_input_data = self._create_dummy_decode_input_data()
                return DecodeInputData(num_decodes=0), dummy_decode_input_data
            return DecodeInputData(num_decodes=0), None
        return self._create_decode_input_data(num_decodes, num_scheduled_tokens,
                                              self.input_batch.num_computed_tokens_cpu[:num_decodes],
                                              self.input_batch.block_table[0].get_cpu_tensor(), scheduler_output), None

    def _create_dummy_decode_input_data(self) -> DecodeInputData:
        # create dummy decode input data with batch size 1
        num_dummy_decodes = 1
        num_dummy_scheduled_tokens = [1]
        context_lens = np.array([128])
        block_table_cpu_tensor = torch.zeros([self._PAD_BLOCK_ID], dtype=torch.int32).reshape(1, -1)
        return self._create_decode_input_data(num_dummy_decodes, num_dummy_scheduled_tokens, context_lens,
                                              block_table_cpu_tensor)

    def _get_cumsum_and_arange(
        self,
        num_tokens: np.ndarray,
        cumsum_dtype: Optional[np.dtype] = None,
    ) -> tuple[np.ndarray, np.ndarray]:
        """Get the cumulative sum and batched arange of the given array.
        # E.g., [2, 5, 3] -> ([2, 7, 10], [0, 1, 0, 1, 2, 3, 4, 0, 1, 2])
        # Equivalent to but faster than:
        # np.concatenate([np.arange(n) for n in num_tokens])
        """
        # Step 1. [2, 5, 3] -> [2, 7, 10]
        cu_num_tokens = np.cumsum(num_tokens, dtype=cumsum_dtype)
        total_num_tokens = cu_num_tokens[-1]
        # Step 2. [2, 7, 10] -> [0, 0, 2, 2, 2, 2, 2, 7, 7, 7]
        cumsums_offsets = np.repeat(cu_num_tokens - num_tokens, num_tokens)
        # Step 3. [0, 1, 0, 1, 2, 3, 4, 0, 1, 2]
        arange = self.arange_np[:total_num_tokens] - cumsums_offsets

        return cu_num_tokens, arange

    def _prepare_spec_decode_inputs(self, scheduler_output, logits_indices, token_ids_device, max_num_sampled_tokens):
        use_spec_decode = len(scheduler_output.scheduled_spec_decode_tokens) > 0
        if not use_spec_decode:
            spec_decode_metadata = None
        else:
            # Get the number of draft tokens for each request.
            # Iterate over the dictionary rather than all requests since not all
            # requests have draft tokens.
            num_draft_tokens = np.zeros(logits_indices.numel(), dtype=np.int32)
            for req_id, draft_token_ids_in_req in (scheduler_output.scheduled_spec_decode_tokens.items()):
                req_idx = self.input_batch.req_id_to_index[req_id]
                num_draft_tokens[req_idx] = len(draft_token_ids_in_req)

            num_sampled_tokens = num_draft_tokens + 1

            cu_num_sampled_tokens, arange = self._get_cumsum_and_arange(num_sampled_tokens, cumsum_dtype=np.int32)

            logits_indices = []
            bonus_logits_indices = []
            target_logits_indices = []
            for batch_id, n_tokens in enumerate(num_sampled_tokens):
                for i in range(n_tokens - 1):
                    logits_indices.append(batch_id * max_num_sampled_tokens + i)
                    target_logits_indices.append(batch_id * max_num_sampled_tokens + i)
                bonus_logits_indices.append(batch_id * max_num_sampled_tokens + n_tokens - 1)
                logits_indices.append(batch_id * max_num_sampled_tokens + n_tokens - 1)
                if n_tokens < max_num_sampled_tokens:
                    logits_indices.extend([-1] * (max_num_sampled_tokens - n_tokens))
                    target_logits_indices.extend([-1] * (max_num_sampled_tokens - n_tokens))
            logits_indices = np.array(logits_indices, dtype=np.int32)
            bonus_logits_indices = np.array(bonus_logits_indices, dtype=np.int32)
            target_logits_indices = np.array(target_logits_indices, dtype=np.int32)

            cu_num_draft_tokens, arange = self._get_cumsum_and_arange(num_draft_tokens, cumsum_dtype=np.int32)

            # TODO: Optimize the CPU -> GPU copy.
            cu_num_draft_tokens = torch.from_numpy(cu_num_draft_tokens).to(self.device, non_blocking=True)
            cu_num_sampled_tokens = torch.from_numpy(cu_num_sampled_tokens).to(self.device, non_blocking=True)

            ##################################################
            logits_indices = torch.from_numpy(logits_indices)
            target_logits_indices_device = \
                torch.from_numpy(target_logits_indices).to(
                self.device, non_blocking=True)
            bonus_logits_indices_device = \
                torch.from_numpy(bonus_logits_indices).to(
                self.device, non_blocking=True)
            draft_token_ids = token_ids_device[target_logits_indices_device + 1]

            spec_decode_metadata = SpecDecodeMetadata(
                draft_token_ids=draft_token_ids,
                num_draft_tokens=num_draft_tokens.tolist(),
                cu_num_draft_tokens=cu_num_draft_tokens,
                cu_num_sampled_tokens=cu_num_sampled_tokens,
                target_logits_indices=target_logits_indices_device,
                bonus_logits_indices=bonus_logits_indices_device,
                logits_indices=logits_indices,
            )
        return logits_indices, spec_decode_metadata

    def _prepare_unified_decode_inputs(self, num_decodes, num_scheduled_tokens, warmup_mode=False):

        if num_decodes == 0:
            return DecodeInputData(num_decodes=0), None

        context_lens = self.input_batch.num_computed_tokens_cpu[:num_decodes]
        query_lengths = [1] * num_decodes
        prompt_lengths = self.input_batch.num_prompt_tokens[:num_decodes]
        token_ids_cpu = self.input_batch.token_ids_cpu
        block_table_cpu_tensor = self.input_batch.block_table[0].get_cpu_tensor()
        num_blocks = [
            math.ceil((ctx_len + q_len) / self.block_size) for ctx_len, q_len in zip(context_lens, query_lengths)
        ]
        block_table = [block_table_cpu_tensor[i, :nb].tolist() for i, nb in enumerate(num_blocks)]
        if not warmup_mode:
            block_table = self.defragmenter.resolve_all(block_table)
        token_ids = [[token_ids_cpu[i, ctx_len]] for i, ctx_len in enumerate(context_lens)]

        batch_data = create_unified_batch(
            token_ids=token_ids,
            block_size=self.block_size,
            block_table=block_table,
            context_lengths=context_lens,
            query_lengths=[1] * num_decodes,
            prompt_lengths=prompt_lengths,
            dtype=self.dtype,
            contiguous_kv=self.use_contiguous_pa,
            bucketing_fn=self.unified_bucketing_fn,
            get_dp_padding_fn=self.get_dp_padding,
        )
        (token_ids_t, token_positions_t, logits_indices_t, logits_groups, attn_metadata) = batch_data
        decode_input_data = DecodeInputData(
            num_decodes=num_decodes,
            token_ids=token_ids_t.unsqueeze(-1),
            position_ids=token_positions_t.unsqueeze(-1),
            logits_indices=logits_indices_t,
            attn_metadata=attn_metadata,
        )
        return decode_input_data, None

    def _prepare_input_ids(self, scheduler_output: "SchedulerOutput") -> None:
        """Prepare the input IDs for the current batch.

        Carefully handles the `prev_sampled_token_ids` which can be cached
        from the previous engine iteration, in which case those tokens on the
        GPU need to be copied into the corresponding slots into input_ids."""

        if self.input_batch.prev_sampled_token_ids is None:
            return

        # Compute cu_num_tokens from scheduler_output
        req_ids = self.input_batch.req_ids
        tokens = [scheduler_output.num_scheduled_tokens[i] for i in req_ids]
        num_scheduled_tokens = np.array(tokens, dtype=np.int32)
        cu_num_tokens, arange = self._get_cumsum_and_arange(num_scheduled_tokens)

        # Async scheduling case, where some decode requests from the previous
        # iteration won't have entries in input_ids_cpu and need to be copied
        # on the GPU from prev_sampled_token_ids.
        prev_req_id_to_index = self.input_batch.prev_req_id_to_index
        assert prev_req_id_to_index is not None
        flattened_indices = []
        prev_common_req_indices = []
        indices_match = True
        max_flattened_index = -1
        for req_id, cur_index in self.input_batch.req_id_to_index.items():
            if (self.input_batch.prev_sampled_token_ids_invalid_indices is not None
                    and req_id in self.input_batch.prev_sampled_token_ids_invalid_indices):
                # This request was in the previous batch but its
                # prev_sampled_token_ids is invalid
                continue
            if (prev_index := prev_req_id_to_index.get(req_id)) is not None:
                prev_common_req_indices.append(prev_index)
                # We need to compute the flattened input_ids index of the
                # last token in each common request.
                flattened_index = cu_num_tokens[cur_index].item() - 1
                flattened_indices.append(flattened_index)
                indices_match &= (prev_index == flattened_index)
                max_flattened_index = max(max_flattened_index, flattened_index)
        num_commmon_tokens = len(flattened_indices)
        if num_commmon_tokens == 0:
            # No requests in common with the previous iteration
            # So input_ids_cpu will have all the input ids.
            return

        prev_sampled_token_ids = self.input_batch.prev_sampled_token_ids

        if indices_match and max_flattened_index == (num_commmon_tokens - 1):
            # Common-case optimization: the batch is unchanged
            # and no reordering happened.
            # The indices are both the same permutation of 0..N-1
            self.input_ids_hpu[:len(flattened_indices)].copy_(prev_sampled_token_ids[:len(flattened_indices)])
            return

        # Upload the index tensors asynchronously
        # so the scatter can be non-blocking
        input_ids_index_tensor = torch.tensor(flattened_indices, dtype=torch.int64).to(self.device, non_blocking=True)
        if prev_sampled_token_ids.size(0) <= len(prev_common_req_indices):
            prev_common_req_indices = prev_common_req_indices[:prev_sampled_token_ids.size(0)]
        prev_common_req_indices_tensor = torch.tensor(prev_common_req_indices, dtype=torch.int64).to(self.device,
                                                                                                     non_blocking=True)
        self.input_ids_hpu.scatter_(dim=0,
                                    index=input_ids_index_tensor,
                                    src=prev_sampled_token_ids[prev_common_req_indices_tensor])

    def _prepare_inputs(
        self,
        scheduler_output: "SchedulerOutput",
        num_prefills,
        num_decodes,
        warmup=False,
    ):

        total_num_scheduled_tokens = scheduler_output.total_num_scheduled_tokens
        assert total_num_scheduled_tokens > 0

        num_reqs = num_prefills + num_decodes

        ###############################################
        # NOTE(Chendi): Follow GPU_Model_Runner to use set global
        # self.input_ids_cpu and self.positions_cpu
        req_ids = self.input_batch.req_ids
        tokens = [scheduler_output.num_scheduled_tokens[i] for i in req_ids]
        num_scheduled_tokens = np.array(tokens, dtype=np.int32)
        req_indices = np.repeat(self.arange_np[:num_reqs], num_scheduled_tokens)
        positions_np = self.positions_np[:total_num_scheduled_tokens]
        cu_num_tokens, arange = self._get_cumsum_and_arange(num_scheduled_tokens)
        np.add(self.input_batch.num_computed_tokens_cpu[req_indices], arange, out=positions_np)
        token_indices = (positions_np + req_indices * self.input_batch.token_ids_cpu.shape[1])
        torch.index_select(self.input_batch.token_ids_cpu_tensor.flatten(),
                           0,
                           torch.from_numpy(token_indices),
                           out=self.input_ids_cpu[:total_num_scheduled_tokens])
        ###############################################

        # Get the number of scheduled tokens for each request.
        # TODO: The Python loop can be slow. Optimize.
        num_scheduled_tokens = []
        num_prompt_tokens = []
        for idx, req_id in enumerate(self.input_batch.req_ids[:num_reqs]):
            assert req_id is not None
            seq_num_scheduled_tokens = scheduler_output.num_scheduled_tokens[req_id]
            seq_num_prompt_tokens = self.input_batch.num_prompt_tokens[idx]
            num_scheduled_tokens.append(seq_num_scheduled_tokens)
            num_prompt_tokens.append(seq_num_prompt_tokens)
        return (self._prepare_prefill_inputs(num_prefills, num_decodes, num_scheduled_tokens),
                self._prepare_decode_inputs(num_decodes, num_scheduled_tokens, scheduler_output))

    def _seq_len(self, attn_metadata):
        return attn_metadata.seq_len()

    def _num_blocks(self, attn_metadata):
        return attn_metadata.num_blocks()

    def _check_config(self, batch_size, seq_len, num_blocks, attn_metadata, warmup_mode):
        cfg: tuple[Any, ...] | None = None
        if self.unified_attn:
            phase = "prompt" if attn_metadata.is_prompt else "decode"
            shared = attn_metadata.shared_blocks.size(0) if attn_metadata.shared_blocks is not None else 0
            unique = attn_metadata.unique_blocks if attn_metadata.unique_blocks else 0
            is_causal = 1 if attn_metadata.causal_bias is not None else 0
            cfg = (seq_len, shared, unique)
            seen = cfg in self.seen_configs
            self.seen_configs.add(cfg)
            if not seen and not warmup_mode:
                logger.warning("Configuration: (query, shared_blocks, unique_blocks) %s, (%s) was not warmed-up!", \
                               cfg, 'causal' if is_causal else 'not causal')
        else:
            phase = "prompt" if attn_metadata.is_prompt else "decode"
            cfg = (phase, batch_size, seq_len, num_blocks)
            if self.debug_fwd:
                self.debug_fwd(cfg)
            seen = cfg in self.seen_configs
            self.seen_configs.add(cfg)
            if not seen and not warmup_mode:
                logger.warning("Configuration: %s was not warmed-up!", cfg)

    def _get_unified_config(self, attn_metadata, logits_indices):
        has_causal = 'c' if attn_metadata.causal_bias is not None else '-'
        has_shared = 's' if attn_metadata.shared_bias is not None else '-'
        has_unique = 'u' if attn_metadata.unique_bias is not None else '-'
        phase = has_causal + has_shared + has_unique
        qlen = attn_metadata.slot_mapping.size(0)
        num_shared_blocks = attn_metadata.shared_blocks.size(0) if attn_metadata.shared_blocks is not None else 0
        num_unique_blocks = attn_metadata.unique_blocks
        num_logits = logits_indices.size(0)
        cfg = (phase, qlen, num_shared_blocks, num_unique_blocks, num_logits)
        return cfg

    def _check_unified_config(self, attn_metadata, logits_indices, warmup_mode):
        cfg = self._get_unified_config(attn_metadata, logits_indices)
        if self.debug_fwd:
            self.debug_fwd(cfg)
        seen = cfg in self.seen_configs
        self.seen_configs.add(cfg)
        if not seen and not warmup_mode:
            logger.warning("Configuration: %s was not warmed-up!", cfg)

    def _execute_model_generic(self,
                               token_ids,
                               position_ids,
                               attn_metadata,
                               logits_indices,
                               kv_caches,
                               lora_logits_mask,
                               lora_mask,
                               warmup_mode=False,
                               inputs_embeds=None,
                               model_mm_kwargs=None):
        # FORWARD.
        batch_size = token_ids.size(0)
        seq_len = self._seq_len(attn_metadata)
        num_blocks = self._num_blocks(attn_metadata)
        if not self.unified_attn:
            self._check_config(batch_size, seq_len, num_blocks, attn_metadata, warmup_mode)
        else:
            self._check_unified_config(attn_metadata, logits_indices, warmup_mode)
        additional_kwargs = {}
        if htorch.utils.internal.is_lazy():
            use_graphs = self._use_graphs()
            if self.max_cudagraph_capture_size is not None and batch_size * seq_len > self.max_cudagraph_capture_size:
                use_graphs = False
            additional_kwargs.update({"bypass_hpu_graphs": not use_graphs})
        else:
            # no hpu graphs for t.compile?
            use_graphs = False
        if self.model_has_chunked_attention:
            additional_kwargs.update({"model_has_chunked_attention": True})
        trimmed_attn_metadata = attn_metadata if self.unified_attn else trim_attn_metadata(attn_metadata)
        if self.is_driver_worker:
            model_event_name = ("model_forward_"
                                f"bs{batch_size}_"
                                f"seq{seq_len}_"
                                f"ctx{num_blocks}_"
                                f"graphs{'T' if use_graphs else 'F'}")
        else:
            model_event_name = 'model_executable'
        with self.profiler.record_event('internal', model_event_name):
            hidden_states = self.model.forward(input_ids=token_ids,
                                               positions=position_ids,
                                               attn_metadata=trimmed_attn_metadata,
                                               kv_caches=kv_caches,
                                               inputs_embeds=inputs_embeds,
                                               model_mm_kwargs=model_mm_kwargs,
                                               lora_mask=lora_mask,
                                               **additional_kwargs)
        # NOTE(kzawora): returning hidden_states is required in prompt logprobs
        # scenarios, as they will do logit processing on their own
        if self.use_aux_hidden_state_outputs:
            non_flattened_hidden_states, aux_hidden_states = hidden_states
            hidden_states = non_flattened_hidden_states
        else:
            non_flattened_hidden_states = hidden_states
            aux_hidden_states = None

        hidden_states = hidden_states.view(-1, hidden_states.shape[-1])
        hidden_states = hidden_states[logits_indices]
        LoraMask.setLoraMask(lora_logits_mask)
        with self.profiler.record_event('internal', ('compute_logits'
                                                     f'{batch_size}_'
                                                     f'seq{seq_len}_ctx'
                                                     f'{num_blocks}')):
            logits = self.model.compute_logits(hidden_states)
        return non_flattened_hidden_states, aux_hidden_states, \
            hidden_states, logits

    def _get_prompt_logprobs_dict(
        self,
        hidden_states: torch.Tensor,
        scheduler_output: "SchedulerOutput",
    ) -> dict[str, Optional[LogprobsTensors]]:
        num_prompt_logprobs_dict = self.input_batch.num_prompt_logprobs
        if not num_prompt_logprobs_dict:
            return {}

        prompt_logprobs_dict: dict[str, Optional[LogprobsTensors]] = {}

        # Since prompt logprobs are a rare feature, prioritize simple,
        # maintainable loop over optimal performance.
        completed_prefill_reqs = []
        for i, (req_id, num_prompt_logprobs) in enumerate(num_prompt_logprobs_dict.items()):

            num_tokens = scheduler_output.num_scheduled_tokens[req_id]

            # Get metadata for this request.
            request = self.requests[req_id]
            num_prompt_tokens = len(request.prompt_token_ids)
            prompt_token_ids = torch.tensor(request.prompt_token_ids).to(self.device, non_blocking=True)

            # Determine number of logits to retrieve.
            start_tok = request.num_computed_tokens + 1
            num_remaining_tokens = num_prompt_tokens - start_tok
            if num_tokens < num_remaining_tokens:
                # This is a chunk, more tokens remain.
                num_logits = num_tokens
            else:
                # This is the last chunk of prompt tokens to return.
                num_logits = num_remaining_tokens
                completed_prefill_reqs.append(req_id)

            # Get the logits corresponding to this req's prompt tokens.
            # If this is a partial request (i.e. chunked prefill),
            # then there is prompt logprob generated for each index.
            prompt_hidden_states = hidden_states[i, :num_logits]
            logits = self.model.compute_logits(prompt_hidden_states)

            # Get the "target" tokens for each index. For prompt at index i,
            # the token at prompt index i+1 is the "sampled" token we want
            # to gather the logprob for.
            tgt_token_ids = prompt_token_ids[start_tok:start_tok + num_logits]

            # Compute prompt logprobs.
            logprobs = self.sampler.compute_logprobs(logits)
            token_ids, logprobs, ranks = self.sampler.gather_logprobs(logprobs, num_prompt_logprobs, tgt_token_ids)

            # Transfer GPU->CPU async.
            prompt_logprobs_dict[req_id] = LogprobsTensors(
                token_ids.to("cpu", non_blocking=True),
                logprobs.to("cpu", non_blocking=True),
                ranks.to("cpu", non_blocking=True),
            )

        # Remove requests that have completed prefill from the batch
        # num_prompt_logprobs_dict.
        for req_id in completed_prefill_reqs:
            del num_prompt_logprobs_dict[req_id]

        # Must synchronize the non-blocking GPU->CPU transfers.
        torch.hpu.synchronize()

        return prompt_logprobs_dict

    def _is_quant_with_inc(self):
        quant_config = os.getenv("QUANT_CONFIG", None) is not None
        return (self.model_config.quantization == "inc" or quant_config)

    # Copied from vllm/v1/worker/gpu_model_runner.py
    def apply_grammar_bitmask(
        self,
        scheduler_output: "SchedulerOutput",
        grammar_output: GrammarOutput,
        logits: torch.Tensor,
    ):

        grammar_bitmask = grammar_output.grammar_bitmask

        # We receive the structured output bitmask from the scheduler,
        # compacted to contain bitmasks only for structured output requests.
        # The order of the requests in the bitmask is not guaranteed to be the
        # same as the order of the requests in the gpu runner's batch. We need
        # to sort the bitmask to match the order of the requests used here.

        # Get the batch indices of the structured output requests.
        # Keep track of the number of speculative tokens scheduled for every
        # request in the batch, as the logit indices are offset by this amount.
        struct_out_req_batch_indices: dict[str, int] = {}
        cumulative_offset = 0
        seq = sorted(self.input_batch.req_id_to_index.items(), key=lambda x: x[1])
        for req_id, batch_index in seq:
            logit_index = batch_index + cumulative_offset
            cumulative_offset += len(scheduler_output.scheduled_spec_decode_tokens.get(req_id, []))
            if req_id in grammar_output.structured_output_request_ids:
                struct_out_req_batch_indices[req_id] = logit_index

        out_indices = []

        # Reorder the bitmask to match the order of the requests in the batch.
        sorted_bitmask = np.zeros_like(grammar_bitmask, shape=(logits.shape[0], grammar_bitmask.shape[1]))
        cumulative_index = 0

        for req_id in grammar_output.structured_output_request_ids:
            logit_index = struct_out_req_batch_indices[req_id]
            num_spec_tokens = len(scheduler_output.scheduled_spec_decode_tokens.get(req_id, []))
            for i in range(1 + num_spec_tokens):
                sorted_bitmask[logit_index + i] = \
                    grammar_bitmask[cumulative_index + i]
                out_indices.append(logit_index + i)
            cumulative_index += 1 + num_spec_tokens

        # Copy async to device as tensor.
        grammar_bitmask = torch.from_numpy(sorted_bitmask).to(logits.device, non_blocking=True)

        # If the grammar bitmask and the logits have the same shape
        # we don't need to pass indices to the kernel,
        # since the bitmask is already aligned with the logits.
        skip_out_indices = grammar_bitmask.shape[0] == logits.shape[0]

        index_tensor = None
        if not skip_out_indices:
            # xgrammar expects a python list of indices but it will actually work with
            # a tensor. If we copy the tensor ourselves here we can do it in a non_blocking
            # manner and there should be no cpu sync within xgrammar.
            index_tensor = torch.tensor(out_indices, dtype=torch.int32, device="cpu", pin_memory=True)
            index_tensor = index_tensor.to(logits.device, non_blocking=True)

        # Serialization of np.ndarray is much more efficient than a tensor,
        # so we receive it in that format.
        #grammar_bitmask = torch.from_numpy(grammar_bitmask).contiguous()

        # Force use of the torch.compile implementation from xgrammar to work
        # around issues with the Triton kernel in concurrent structured output
        # scenarios. See PR #19565 and issues #19493, #18376 for details.

        # xgr_torch_compile.apply_token_bitmask_inplace_torch_compile(
        #     logits,
        #     grammar_bitmask.to(self.device, non_blocking=True),
        #     indices=out_indices if not skip_out_indices else None,
        # )

        # NOTE(tianmu-li): xgr_torch_compile uses torch.inductor by default.
        # Have to use the CPU backend, which has its overhead.
        logits_cpu = logits.cpu().to(torch.float32)
        '''xgr_cpu.apply_token_bitmask_inplace_cpu(
            logits_cpu,
            grammar_bitmask.to("cpu"),
            indices=out_indices if not skip_out_indices else None,
        )'''
        xgr_cpu.apply_token_bitmask_inplace_cpu(logits_cpu, grammar_bitmask.to("cpu"), indices=index_tensor)
        logits.copy_(logits_cpu.to(self.device, non_blocking=True).to(logits.dtype))

    def _configure_lora(self, input, requests, req_ids, is_prompt):
        lora_mask = None
        lora_logits_mask = None
        if self.lora_config:
            if is_prompt:
                lora_requests = [] if req_ids else requests
                lora_ids = []
                lora_index_mapping = []
                lora_prompt_mapping = []
                for i, r_id in enumerate(req_ids):
                    lora_requests.append(requests[r_id].lora_request)
                for lora_req in lora_requests:
                    lora_id = lora_req.lora_int_id if lora_req else 0
                    lora_index_mapping += [lora_id] * (input.shape[1])
                    #TODO: This may need to change when logprobs
                    # sampling is enabled
                    lora_prompt_mapping += [lora_id]
                    lora_ids.append(lora_id)
            else:
                lora_requests = []
                # lora_ids, lora_index_mapping, lora_prompt_mapping
                # filled with 0 (indicating no lora) to account for
                # any padding
                lora_ids = [0] * input.shape[0]
                lora_index_mapping = [0] * input.shape[0]
                lora_prompt_mapping = [0] * input.shape[0]
                for i, r_id in enumerate(req_ids):
                    lora_requests.append(requests[r_id].lora_request)

                for i, lora_req in enumerate(lora_requests):
                    lora_id = lora_req.lora_int_id if lora_req else 0
                    lora_index_mapping[i] = lora_id
                    lora_prompt_mapping[i] = lora_id
                    lora_ids[i] = lora_id

            # is_prefill should always be "False" for HPU
            lora_mapping = LoRAMapping(lora_index_mapping, lora_prompt_mapping, is_prefill=False)
            self.set_active_loras(lora_requests, lora_mapping)
            lora_mask, lora_logits_mask = self.create_lora_mask(input, lora_ids, is_prompt)

        return lora_mask, lora_logits_mask

    def _run_sampling(self,
                      batch_changed: bool,
                      logits_device: torch.Tensor,
                      request_ids: Optional[list[str]] = None,
                      pad_to: Optional[int] = None,
                      logits_requests=None) -> tuple[torch.Tensor, SamplingMetadata]:
        htorch.core.mark_step()
        sampling_metadata = self._prepare_sampling(batch_changed, request_ids, pad_to, logits_requests)
        sampler_output = self.sampler(logits=logits_device, sampling_metadata=sampling_metadata)
        htorch.core.mark_step()
        return sampler_output, sampling_metadata

    def _pool(
        self,
        hidden_states: torch.Tensor,
        num_scheduled_tokens: int,
        num_scheduled_tokens_np: np.ndarray,
    ) -> ModelRunnerOutput:
        assert self.input_batch.num_reqs ==\
            len(self.input_batch.pooling_params), \
        "Either all or none of the requests in" \
        " a batch must be pooling request"
        hidden_states = hidden_states[:num_scheduled_tokens]

        pooling_metadata = self.input_batch.pooling_metadata
        pooling_metadata.build_pooling_cursor(num_scheduled_tokens_np.tolist(), device=hidden_states.device)

        num_reqs = self.input_batch.num_reqs

        seq_lens = (
            torch.tensor(self.input_batch.num_prompt_tokens[:num_reqs], dtype=torch.int32, device=self.device) +
            torch.tensor(self.input_batch.num_computed_tokens_cpu[:num_reqs], dtype=torch.int32, device=self.device))
        raw_pooler_output = self.model.pooler(hidden_states=hidden_states, pooling_metadata=pooling_metadata)
        raw_pooler_output = json_map_leaves(
            lambda x: x.to("cpu", non_blocking=True),
            raw_pooler_output,
        )

        pooler_output: list[Optional[torch.Tensor]] = []
        for raw_output, seq_len, prompt_len in zip(raw_pooler_output, seq_lens, pooling_metadata.prompt_lens):

            if seq_len == prompt_len:
                pooler_output.append(raw_output)
            else:
                pooler_output.append(None)

        return ModelRunnerOutput(
            req_ids=[self.input_batch.req_ids],
            req_id_to_index=self.input_batch.req_id_to_index,
            sampled_token_ids=[],
            logprobs=None,
            prompt_logprobs_dict={},
            pooler_output=pooler_output,
            kv_connector_output=None,
        )

    def _prepare_inputs_for_pooling(self, scheduler_output):
        """Gather inputs, positions, slot mapping, and build attn_metadata"""
        num_scheduled_tokens = []
        input_ids_list = []
        num_computed_tokens_cpu = self.input_batch.num_computed_tokens_cpu
        num_reqs = self.input_batch.num_reqs

        # Collect token ids and scheduled lengths
        for idx, req_id in enumerate(self.input_batch.req_ids[:num_reqs]):
            seq_num_scheduled = scheduler_output.num_scheduled_tokens[req_id]
            num_scheduled_tokens.append(seq_num_scheduled)

            scheduled_req = scheduler_output.scheduled_new_reqs[idx]
            token_ids = torch.as_tensor(scheduled_req.prompt_token_ids, dtype=torch.long).flatten()
            input_ids_list.append(token_ids)

        input_ids = torch.cat(input_ids_list, dim=0).to(self.device)

        # Absolute positions
        absolute_positions = []
        for i, n in enumerate(num_scheduled_tokens):
            prefix = num_computed_tokens_cpu[i]
            absolute_positions.append(prefix + np.arange(n, dtype=np.int64))
        position_ids = torch.from_numpy(np.concatenate(absolute_positions)).to(self.device)

        # Slot mapping + metadata
        total_scheduled_tokens = sum(num_scheduled_tokens)
        slot_mapping = torch.arange(total_scheduled_tokens, dtype=torch.long, device="hpu:0")
        seq_lens_tensor = torch.tensor([total_scheduled_tokens], device='hpu:0', dtype=torch.int32)
        context_lens_tensor = torch.tensor([0], device='hpu:0', dtype=torch.int32)

        attn_metadata = HPUAttentionMetadataV1.make_prefill_metadata(
            seq_lens_tensor=seq_lens_tensor,
            context_lens_tensor=context_lens_tensor,
            slot_mapping=slot_mapping,
            block_list=None,
            attn_bias=None,
            block_size=self.block_size,
        )

        return input_ids, position_ids, num_scheduled_tokens, attn_metadata, \
            total_scheduled_tokens

    @torch.inference_mode()
    def run_defragmenter(self, scheduler_output: "SchedulerOutput", warmup_mode: bool = False):
        if self.defragmenter.enabled and self.kv_caches and not warmup_mode:
            new = {req.req_id: flatten(req.block_ids) for req in scheduler_output.scheduled_new_reqs if req.block_ids}
            #TODO: Add support for preempted blocks
            cached = {
                req_id: flatten(new_block_ids)
                for req_id, new_block_ids in zip(scheduler_output.scheduled_cached_reqs.req_ids,
                                                 scheduler_output.scheduled_cached_reqs.new_block_ids) if new_block_ids
            }
            self.defragmenter.update_state(new | cached, scheduler_output.finished_req_ids)
            self.defragmenter.defragment()

    def prepare_unified_batch(self, scheduler_output):
        num_reqs = len(self.input_batch.req_ids)
        num_computed_tokens = self.input_batch.num_computed_tokens_cpu_tensor[:num_reqs]
        num_prompt_tokens = torch.from_numpy(self.input_batch.num_prompt_tokens[:num_reqs])
        num_scheduled_tokens = torch.tensor(
            [scheduler_output.num_scheduled_tokens[req_id] for req_id in self.input_batch._req_ids],
            dtype=torch.int32,
            device='cpu')
        max_seq = (num_computed_tokens + num_scheduled_tokens).max()
        max_blocks = (max_seq + self.block_size - 1) // self.block_size
        all_token_ids = self.input_batch.token_ids_cpu_tensor[:num_reqs, :max_seq]
        # TODO: check if it's safe to always slice on first dim
        block_table = self.input_batch.block_table[0].get_cpu_tensor()[:num_reqs, :max_blocks].clone().to(torch.int64)
        if self.defragmenter.enabled:
            block_table.apply_(self.defragmenter.resolve)

        return create_unified_batch(self.input_batch.req_ids, all_token_ids, num_computed_tokens, num_scheduled_tokens,
                                    num_prompt_tokens, block_table, self.block_size, self.dtype,
                                    self.unified_attn_persistent_ctx, self.unified_bucketing_fn, self.get_dp_padding)

    @torch.inference_mode()
    def unified_execute_model(self,
                              scheduler_output: "SchedulerOutput",
                              grammar_output: "GrammarOutput" = None,
                              warmup_mode: bool = False) -> ModelRunnerOutput:
        batch_changed = self.batch_changed
        with self.profiler.record_event('internal', 'prepare_unified_batch'):
            batch = self.prepare_unified_batch(scheduler_output)
        htorch.core.mark_step()
        if self.is_driver_worker:
            unified_attn_cfg = self._get_unified_config(batch.attn_metadata, batch.logits_indices)
            (phase, qlen, num_shared_blocks, num_unique_blocks, num_logits) = unified_attn_cfg
            model_event_name = (
                f"model_forward_{phase}_qlen{qlen}_nsb{num_shared_blocks}_nub{num_unique_blocks}_nlog{num_logits}")
        else:
            model_event_name = 'model_executable'
        with self.profiler.record_event('internal', model_event_name):
            non_flattened_hidden_states, aux_hidden_states, hidden_states, logits_device = \
                self._execute_model_generic(
                    token_ids=batch.token_ids.unsqueeze(-1),
                    position_ids=batch.token_positions.unsqueeze(-1),
                    attn_metadata=batch.attn_metadata,
                    logits_indices=batch.logits_indices,
                    kv_caches=self.kv_caches,
                    lora_logits_mask=None,
                    lora_mask=None,
                    warmup_mode=warmup_mode)
        selected_req_ids = [batch.req_ids_cpu[idx] for idx in batch.logits_groups_cpu.tolist()]
        htorch.core.mark_step()
        with self.profiler.record_event('internal', 'unified_sampler'):
            sampling_metadata = self._prepare_sampling(batch_changed, selected_req_ids, pad_to=logits_device.shape[0])
            sampler_output = self.sampler(logits=logits_device, sampling_metadata=sampling_metadata)

        with self.profiler.record_event('internal', 'unified_postprocess'):
            sampled_token_ids_cpu = sampler_output.sampled_token_ids.cpu()

            sampled_token_ids_np = sampled_token_ids_cpu.numpy()
            sampled_token_ids: list[np.ndarray] = [np.array([], dtype=np.int32) for _ in batch.req_ids_cpu]
            for req_id, tokens_array in zip(selected_req_ids, sampled_token_ids_np):
                idx = self.input_batch.req_id_to_index[req_id]
                sampled_token_ids[idx] = tokens_array

            #TODO: add support for multi-token output
            assert sampled_token_ids_cpu.size(1) == 1, 'Currently only single token output is supported!'
            sampled_token_ids_cpu = sampled_token_ids_cpu.flatten()
            htorch.core.mark_step()

            sampled_token_ids_cpu = sampled_token_ids_cpu.index_select(0, batch.logits_groups_cpu)
            self.input_batch.token_ids_cpu_tensor.index_put_((batch.logits_groups_cpu, batch.new_token_positions_cpu),
                                                             sampled_token_ids_cpu)

            ######### UPDATE REQUEST STATE WITH GENERATED TOKENS #########
            num_reqs = len(selected_req_ids)
            for req_id in self.input_batch.req_ids[:num_reqs]:
                req_state = self.requests[req_id]
                i = self.input_batch.req_id_to_index[req_id]
                seq_len = (req_state.num_computed_tokens + scheduler_output.num_scheduled_tokens[req_id])
                token_ids = sampled_token_ids[i]
                num_tokens = len(token_ids)
                self.input_batch.token_ids_cpu[i, seq_len:seq_len + num_tokens] = token_ids
                self.input_batch.num_tokens[i] += len(token_ids)
                req_state.output_token_ids.extend(token_ids.tolist())

        model_runner_output = ModelRunnerOutput(
            req_ids=batch.req_ids_cpu,
            req_id_to_index=self.input_batch.req_id_to_index,
            sampled_token_ids=sampled_token_ids,
            logprobs=None,
            prompt_logprobs_dict={},
            pooler_output=[],
        )

        return model_runner_output

    @torch.inference_mode()
    def execute_model(
        self,
        scheduler_output: "SchedulerOutput",
        warmup_mode: bool = False,
    ) -> ModelRunnerOutput | None:

        self.run_defragmenter(scheduler_output, warmup_mode)

        batch_changed = self._update_states(scheduler_output)
        if not scheduler_output.total_num_scheduled_tokens:
            if not has_kv_transfer_group() or warmup_mode:
                # Return empty ModelRunnerOuptut if there's no work to do.
                return EMPTY_MODEL_RUNNER_OUTPUT
            # For D case, wait until kv finish load here
            return self.kv_connector_no_forward(scheduler_output, self.vllm_config)
        self.scheduler_output = scheduler_output
        self.warmup_mode = warmup_mode
        self.batch_changed = batch_changed

        return None

    @torch.inference_mode()
    def sample_tokens(self, grammar_output: "GrammarOutput | None") -> ModelRunnerOutput | AsyncModelRunnerOutput:
        if self.scheduler_output is None:
            # Nothing to do (PP non-final rank case), output isn't used.
            return None  # noqa
        scheduler_output = self.scheduler_output
        warmup_mode = self.warmup_mode
        self.scheduler_output = None
        self.warmup_mode = False

        if self.unified_attn:
            return self.unified_execute_model(scheduler_output, warmup_mode=warmup_mode)

        # NOTE(kzawora): Since scheduler doesn't differentiate between prefills
        # and decodes, we must handle mixed batches. In _update_states we make
        # sure that first self.input_batch.num_decodes requests are decodes,
        # and remaining ones until the end are prefills. _update_states also
        # handles changes in request cache based on scheduler outputs and
        # previous iterations (e.g. keeping block tables and context lengths up
        # to date, creating, pruning and updating request caches,
        # and some more stuff)

        # If num_decodes == self.input_batch.num_reqs, then batch is all decode, and only a single decode forward pass will be executed in this method. # noqa
        # If num_decodes == 0, then batch is all prefill, and only prefill forward passes will be executed  in this method. # noqa
        # If neither apply, then batch is mixed, and both prefill and decode forward passes will be executed in this method. # noqa

        # First, we will execute all decodes (if any) in a single batch,
        # then we'll execute prefills in batches of up to max_prefill_batch_size elements. # noqa
        # All shapes used in forward passes are bucketed appropriately to mitigate risk of graph recompilations. # noqa

        # We perform sampling directly after executing each forward pass
        # Everything is done asynchronously - the only sync point is the place
        # where we copy the generated tokens back to the host.

        # Example: If a batch has 6 requests, 3 prefills and 3 decodes, the unprocessed sequences in batch will be laid as follows: # noqa
        # [D0, D1, D2, P0, P1, P2]
        # If we assume max_prefill_batch_size=2, the flow of this method will look as follows: # noqa
        # prepare_inputs: bucket [D0, D1, D2] -> [D0, D1, D2, 0] (BS=4 bucket, 1 seq padding) # noqa
        # prepare_inputs: bucket [P0, P1, P2] -> [P0, P1], [P2] (BS=2 + BS=1 bucket, no seqs padding) # noqa
        # decode forward pass BS4 [D0, D1, D2, 0]
        # decode compute_logits BS4 [D0, D1, D2, 0]
        # decode sampler BS4 [D0, D1, D2, 0] -> [tokD0, tokD1, tokD2, 0]
        # prefill[iter 0] forward pass BS2 [P0, P1]
        # prefill[iter 0] compute_logits BS2 [P0, P1]
        # prefill[iter 0] sampler BS2 [P0, P1] -> [tokP0, tokP1]
        # prefill[iter 1] forward pass BS1 [P0, P1]
        # prefill[iter 1] compute_logits BS1 [P0, P1]
        # prefill[iter 1] sampler BS1 [P0, P1] -> [tokP2]
        # prefill concat sampler results [tokP0, tokP1], [tokP2] -> [tokP0, tokP1, tokP2] # noqa
        # Join the prefill and decode on device into [tokD0, tokD1, tokD2, 0, tokP0, tokP1, tokP2] # noqa
        # Transfer [tokD0, tokD1, tokD2, 0, tokP0, tokP1, tokP2] to CPU
        # On CPU, sanitize [tokD0, tokD1, tokD2, 0, tokP0, tokP1, tokP2] -> [tokD0, tokD1, tokD2, tokP0, tokP1, tokP2] # noqa
        # Return [tokD0, tokD1, tokD2, tokP0, tokP1, tokP2]

        # Example2: Same thing, but with max_prefill_batch_size=4:
        # prepare_inputs: bucket [D0, D1, D2] -> [D0, D1, D2, 0] (BS=4 bucket, 1 seq padding) # noqa
        # prepare_inputs: bucket [P0, P1, P2] -> [P0, P1, P2, 0] (BS=4 bucket, 1 seq padding) # noqa
        # decode forward pass BS4 [D0, D1, D2, 0]
        # decode compute_logits BS4 [D0, D1, D2, 0]
        # decode sampler BS4 [D0, D1, D2, 0] -> [tokD0, tokD1, tokD2, 0]
        # prefill[iter 0] forward pass BS4 [P0, P1, P2, 0]
        # prefill[iter 0] compute_logits BS4 [P0, P1, P2, 0]
        # prefill[iter 0] sampler BS4 [P0, P1, P2, 0] -> [tokP0, tokP1, tokP2, 0] # noqa
        # Join the prefill and decode on device into [tokD0, tokD1, tokD2, 0, tokP0, tokP1, tokP2, 0] # noqa
        # Transfer [tokD0, tokD1, tokD2, 0, tokP0, tokP1, tokP2, 0] to CPU
        # On CPU, sanitize [tokD0, tokD1, tokD2, 0, tokP0, tokP1, tokP2, 0] -> [tokD0, tokD1, tokD2, tokP0, tokP1, tokP2] # noqa
        # Return [tokD0, tokD1, tokD2, tokP0, tokP1, tokP2]

        batch_changed = self.batch_changed
        if self.input_batch.pooling_params:
            (input_ids, position_ids, num_scheduled_tokens, attn_metadata,
             total_scheduled_tokens) = self._prepare_inputs_for_pooling(scheduler_output)

            with set_forward_context(attn_metadata, self.vllm_config):
                hidden_states = self.model.forward(
                    input_ids=input_ids,
                    positions=position_ids,
                )

            flattened = hidden_states.view(-1, hidden_states.shape[-1])
            pooled_output = self._pool(
                flattened,
                total_scheduled_tokens,
                np.array(num_scheduled_tokens, dtype=np.int32),
            )
            return pooled_output
        # If necessary, swap decodes/prompts to have all decodes on the start

        ensure_decodes_first(self.input_batch)
        # Prepare prompts/decodes info
        pd_info = self._get_prompts_and_decodes(scheduler_output)
        num_decodes = len(pd_info.decode_req_ids)
        num_prefills = len(pd_info.prompt_req_ids)
        num_reqs = num_decodes + num_prefills
        with self.profiler.record_event('internal', 'prepare_input_tensors'):
            prefill_input_data, decode_input_data = self._prepare_inputs(scheduler_output, num_prefills, num_decodes,
                                                                         warmup_mode)
        prefill_data, \
            dummy_prefill_input_data_batches_across_dp = prefill_input_data
        num_pad_prefill_batch_across_dp = \
            0 if dummy_prefill_input_data_batches_across_dp is None \
            else len(dummy_prefill_input_data_batches_across_dp.request_ids)
        decode_data, dummy_decode_input_data_across_dp = decode_input_data
        #FIXME(kzawora): Currently there's no handling of logprobs. Fix that
        # later.
        prefill_sampled_token_ids = []
        prefill_sampled_requests = []
        decode_sampled_token_ids = []
        decode_sampled_requests = []
        #if not has_kv_transfer_group():
        #    assert not (num_prefills > 0 and num_decodes > 0)
        # skip kv_connector if dummy run
        if not warmup_mode:
            with set_forward_context(None, self.vllm_config):
                self.maybe_setup_kv_connector(scheduler_output)
        finished_sending, finished_recving = set(), set()

        # NOTE(Chendi): used by spec decode draft model, since we are doing
        # prefill one by one, so save hidden states as list
        non_flattened_hidden_states_prefills = []
        aux_hidden_states_prefills = []
        sample_hidden_states_prefills = []
        decode_sampled_token_ids_device = None
        # NOTE(tianmu-li): For structured output, combine logits before
        # postprocessing. Should it be done for all requests?
        structured_output = False
        spec_decode_num_tokens = None
        if grammar_output is not None:
            logits_prompt = []
            logits_decode = []
            structured_output = True
        if self.use_async_scheduling:
            invalid_req_indices = []
        ######################### PREFILLS #########################
        if num_prefills > 0:
            htorch.core.mark_step()
            for idx, (req_id, prompt_len, token_ids, position_ids, attn_metadata, logits_indices,
                      logits_requests) in enumerate(zip(*shallow_tuple(prefill_data))):

                inputs_embeds = None
                model_mm_kwargs = None
                if self.supports_mm_inputs:
                    # Run the multimodal encoder if any.
                    with self.profiler.record_event('internal', 'prepare_input_encoders'):
                        self._execute_mm_encoder(scheduler_output, req_id)
                    htorch.core.mark_step()

                    mm_embeds, is_mm_embed = self._gather_mm_embeddings(scheduler_output,
                                                                        req_id,
                                                                        total_num_scheduled_tokens=token_ids.shape[-1])
                    htorch.core.mark_step()

                    # TODO: Only get embeddings for valid token_ids. Ignore token_ids[<pad_idxs>] # noqa E501
                    # This may require moving multimodal input preps into _prepare_inputs,        # noqa E501
                    # to avoid padding issues.
                    inputs_embeds = self.model.embed_input_ids(
                        token_ids,
                        multimodal_embeddings=mm_embeds,
                        is_multimodal=is_mm_embed,
                    )

                    model_mm_kwargs = self._extract_mm_kwargs(scheduler_output)
                    model_mm_kwargs = MultiModalKwargs.as_kwargs(
                        model_mm_kwargs,
                        device=self.device,
                    )

                lora_mask, lora_logits_mask = self._configure_lora(token_ids, self.requests, req_id, True)

                self.event_start = self.profiler.get_timestamp_us()
                self.profiler.start("internal", "prefill")
                # NOTE(tianmu-li): Align behavior of incomplete prompt with gpu_model_runner
                # If logits_indices is smaller than req_id, the last request is a chunked prompt request that
                # hasn't finished in this step. We add the last token position to logits_indices to ensure
                # the last token of the chunk is sampled. This sampled token will be discarded later
                if logits_indices.shape[0] < len(req_id):
                    if structured_output or self.use_async_scheduling:
                        # When there are multiple requests in the batch (e.g. self.use_merged_prefill=True),
                        # the last token position is the sum of all prompt lengths - 1
                        # This logic also holds when there is only one request in the batch
                        logits_indices_append = torch.tensor([torch.sum(prompt_len) - 1],
                                                             device=token_ids.device,
                                                             dtype=torch.int32)
                        logits_indices = torch.cat([logits_indices, logits_indices_append])
                    if self.use_async_scheduling:
                        # Discard partial prefill logit for async scheduling
                        # Depends on 1 decode token/batch
                        prefill_start_idx = num_decodes
                        invalid_req_indices.append(prefill_start_idx + idx)
                htorch.core.mark_step()
                non_flattened_hidden_states, aux_hidden_states, \
                    sample_hidden_states, logits_device = \
                    self._execute_model_generic(
                        token_ids, position_ids, attn_metadata, logits_indices,
                        self.kv_caches,
                        lora_logits_mask,
                        lora_mask,
                        inputs_embeds=inputs_embeds,
                        model_mm_kwargs=model_mm_kwargs,
                        warmup_mode=warmup_mode,)
                htorch.core.mark_step()
                non_flattened_hidden_states_prefills.append(non_flattened_hidden_states)
                if self.use_aux_hidden_state_outputs:
                    aux_hidden_states_prefills.append(aux_hidden_states)
                sample_hidden_states_prefills.append(sample_hidden_states)
                # Skip separate sampling for structured output
                if structured_output:
                    logits_prompt.append(logits_device)
                    prefill_sampled_requests.extend(logits_requests)
                else:
                    # If there are no logits, there is nothing to sample.
                    # This can happen with chunked prefill when a chunk does
                    # not complete the prompt and no logits are generated.
                    if logits_device.numel() > 0:
                        with self.profiler.record_event('internal', "sampler"):
                            sampler_output, sampling_metadata = self._run_sampling(batch_changed, logits_device, req_id,
                                                                                   logits_device.shape[0],
                                                                                   logits_requests)
                            prefill_sampled_token_ids.append(sampler_output.sampled_token_ids.flatten())
                            prefill_sampled_requests.extend(logits_requests)
                if self.is_driver_worker and self.profiler.enabled:
                    # Stop recording 'execute_model_generic' event
                    self.profiler.end()
                    event_end = self.profiler.get_timestamp_us()
                    counters = self.profiler_counter_helper.get_counter_dict(cache_config=self.cache_config,
                                                                             duration=event_end - self.event_start,
                                                                             seq_len=self._seq_len(attn_metadata),
                                                                             batch_size_padded=token_ids.size(0),
                                                                             real_batch_size=len(req_id),
                                                                             prompt_batch_idx=idx,
                                                                             is_prompt=True)
                    self.profiler.record_counter(self.event_start, counters)
            if not warmup_mode:
                self.maybe_wait_for_kv_save()

            if self.is_driver_worker and self.profiler.enabled:
                self.profiler_counter_helper.reset_prompt_seq_stats()

        if num_pad_prefill_batch_across_dp > 0:
            for idx, (req_id, prompt_len, token_ids, position_ids, attn_metadata, logits_indices,
                      logits_requests) in enumerate(zip(*shallow_tuple(dummy_prefill_input_data_batches_across_dp))):
                htorch.core.mark_step()
                _, _, _, dummy_logits_device = \
                self._execute_model_generic(
                    token_ids,
                    position_ids,
                    attn_metadata,
                    logits_indices,
                    self.kv_caches,
                    None,
                    None,
                    warmup_mode=warmup_mode)
                htorch.core.mark_step()

        ######################### DECODES #########################
        # Decodes run as one single batch with [padded_decode_bs, 1]
        if num_decodes > 0:
            assert decode_data is not None
            lora_mask, lora_logits_mask = self._configure_lora(decode_data.token_ids, self.requests,
                                                               pd_info.decode_req_ids, False)
            self.event_start = self.profiler.get_timestamp_us()
            self.profiler.start("internal", "decode")
            htorch.core.mark_step()
            non_flattened_hidden_states, aux_hidden_states, \
                sample_hidden_states, logits_device = \
                    self._execute_model_generic(
                decode_data.token_ids,
                decode_data.position_ids,
                decode_data.attn_metadata,
                decode_data.logits_indices,
                self.kv_caches,
                lora_logits_mask,
                lora_mask,
                warmup_mode=warmup_mode)
            htorch.core.mark_step()

            if structured_output:
                logits_decode.append(logits_device[:num_decodes])
                decode_sampled_requests.extend(self.input_batch.req_ids[:num_decodes])
            else:
                with self.profiler.record_event('internal', "sampler"):
                    ##### Sampling Start #####
                    spec_decode_metadata = decode_data.spec_decode_metadata
                    sampler_output, sampling_metadata = self._run_sampling(
                        batch_changed, logits_device
                        if spec_decode_metadata is None else logits_device[spec_decode_metadata.bonus_logits_indices],
                        pd_info.decode_req_ids, logits_device.shape[0])

                    if spec_decode_metadata is None:
                        decode_sampled_token_ids.append(sampler_output.sampled_token_ids.flatten())
                    else:
                        # Handling spec decode sampling.
                        sampler_output = self.rejection_sampler(
                            spec_decode_metadata,
                            None,  # draft_probs
                            logits_device,
                            sampling_metadata,
                        )
                        sampled_token_ids = sampler_output.sampled_token_ids
                        decode_sampled_token_ids = \
                            self.rejection_sampler.parse_output(
                                sampled_token_ids,
                                self.input_batch.vocab_size,
                        )
                        # convert decode_sampled_token_ids as list of tensor
                        spec_decode_num_tokens = [len(v) for v in decode_sampled_token_ids]
                        decode_sampled_token_ids = [
                            torch.tensor(v, device="cpu").int() for v in decode_sampled_token_ids
                        ]
                        decode_sampled_token_ids_device = \
                            sampled_token_ids.to("hpu", non_blocking=True)
                    decode_sampled_requests.extend(self.input_batch.req_ids[:num_decodes])
                    ##### Sampling End #####

            if self.is_driver_worker and self.profiler.enabled:
                # Stop recording 'execute_model' event
                self.profiler.end()
                event_end = self.profiler.get_timestamp_us()
                counters = self.profiler_counter_helper.get_counter_dict(
                    cache_config=self.cache_config,
                    duration=event_end - self.event_start,
                    seq_len=self._seq_len(decode_data.attn_metadata),
                    batch_size_padded= \
                        decode_data.token_ids.size(0), # type: ignore
                    real_batch_size=decode_data.num_decodes,
                    prompt_batch_idx=None,
                    is_prompt=False)
                self.profiler.record_counter(self.event_start, counters)

        elif dummy_decode_input_data_across_dp is not None:
            htorch.core.mark_step()
            _, _, _, dummy_logits_device = self._execute_model_generic(dummy_decode_input_data_across_dp.token_ids,
                                                                       dummy_decode_input_data_across_dp.position_ids,
                                                                       dummy_decode_input_data_across_dp.attn_metadata,
                                                                       dummy_decode_input_data_across_dp.logits_indices,
                                                                       self.kv_caches,
                                                                       None,
                                                                       None,
                                                                       warmup_mode=warmup_mode)
            htorch.core.mark_step()

        if structured_output:
            # Scheduler places cached before prompt
            logits_combined = logits_decode + logits_prompt
            logits = torch.cat(logits_combined, dim=0)
            # Apply structured output bitmasks if present
            if grammar_output:
                self.apply_grammar_bitmask(scheduler_output, grammar_output, logits)
            sampler_output, _sampling_metadata = self._run_sampling(batch_changed, logits,
                                                                    pd_info.prompt_req_ids + pd_info.decode_req_ids,
                                                                    logits.shape[0])
            # Deal with the case of incomplete prompt
            for i in range(logits.shape[0] - num_decodes):
                prefill_sampled_token_ids.append(sampler_output.sampled_token_ids[num_decodes + i].flatten())
            decode_sampled_token_ids.append(sampler_output.sampled_token_ids[:num_decodes].flatten())
        elif self.use_async_scheduling:
            # For async scheduling: keep tokens on HPU and avoid CPU sync
            # Concatenate decode and prefill tokens on HPU
            if decode_sampled_token_ids or prefill_sampled_token_ids:
                decode_sampled_token_ids = [tensor[:num_decodes] for tensor in decode_sampled_token_ids]
                # Note: this will cause an issue with the current spec decode impl, as they are on different devices
                sampled_token_ids = torch.cat(decode_sampled_token_ids + prefill_sampled_token_ids).view(-1, 1)
            else:
                sampled_token_ids = torch.empty((0, 1), dtype=torch.int32, device=self.device)

        # Copy some objects so they don't get modified after returning.
        # This is important when using async scheduling.
        req_ids_output_copy = self.input_batch.req_ids.copy()
        req_id_to_index_output_copy = \
            self.input_batch.req_id_to_index.copy()

        max_req_index = max(self.input_batch.req_id_to_index.values())
        postprocessed_sampled_token_ids: list[np.ndarray] = [
            np.array([], dtype=np.int32) for _ in range(max_req_index + 1)
        ]
        if self.use_async_scheduling:
            self.input_batch.prev_sampled_token_ids = sampled_token_ids.flatten()
            # self.input_batch.prev_sampled_token_ids_invalid_indices
            invalid_req_indices_set = set(invalid_req_indices)
            self.input_batch.prev_sampled_token_ids_invalid_indices = \
                invalid_req_indices_set
            self.input_batch.prev_req_id_to_index = {
                req_id: i
                for i, req_id in enumerate(self.input_batch.req_ids) if i not in invalid_req_indices_set
            }
            # For the output, postprocessed_sampled_token_ids will be filled during serialization
        else:
            prefill_sampled_token_ids_device = prefill_sampled_token_ids
            # From this point onward, all operations are done on CPU.
            # We already have tokens. Let's copy the data to
            # CPU as is, and then discard padded tokens.
            with self.profiler.record_event('internal', "sampler_postprocessing"):
                prefill_sampled_token_ids = [tensor.cpu() for tensor in prefill_sampled_token_ids]
                if spec_decode_num_tokens is not None:
                    decode_sampled_token_ids = [tensor.cpu() for tensor in decode_sampled_token_ids]
                else:
                    decode_sampled_token_ids = [tensor.cpu()[:num_decodes] for tensor in decode_sampled_token_ids]
                if decode_sampled_token_ids + prefill_sampled_token_ids:
                    sampled_token_ids_tensor = torch.cat(decode_sampled_token_ids + prefill_sampled_token_ids)
                    sampled_token_ids_np = sampled_token_ids_tensor.cpu().numpy().flatten()
                else:
                    sampled_token_ids_np = np.array([], dtype=np.int32)
                sampled_token_requests = \
                    decode_sampled_requests + prefill_sampled_requests
                max_req_index = max(self.input_batch.req_id_to_index.values())
                # NOTE(Chendi): in post-processing, spec_decode might
                # return more than 1 token during decode.
                start_idx = 0
                for i, req_id in enumerate(sampled_token_requests):
                    num_tokens = spec_decode_num_tokens[
                        i] if spec_decode_num_tokens is not None and i < num_decodes else 1
                    req_idx = self.input_batch.req_id_to_index[req_id]
                    postprocessed_sampled_token_ids[req_idx] = np.array(sampled_token_ids_np[start_idx:start_idx +
                                                                                             num_tokens],
                                                                        dtype=np.int32)
                    start_idx += num_tokens

        ################## RETURN ##################
        # NOTE(kzawora): idk what happens if part of batch doesn't have logprobs

        ######### UPDATE REQUEST STATE WITH GENERATED TOKENS #########
        for req_id in self.input_batch.req_ids[:num_reqs]:
            req_state = self.requests[req_id]
            i = self.input_batch.req_id_to_index[req_id]
            seq_len = (req_state.num_computed_tokens + scheduler_output.num_scheduled_tokens[req_id])
            token_ids = postprocessed_sampled_token_ids[i]
            num_tokens = len(token_ids)
            self.input_batch.token_ids_cpu[i, seq_len:seq_len + num_tokens] = token_ids
            self.input_batch.num_tokens[i] += len(token_ids)

        # NOTE(chendi): enable cache based on PR(#20291)
        # Cache the sampled tokens in the model runner, so that the scheduler
        # doesn't need to send them back.
        # NOTE(woosuk): As an exception, when using PP, the scheduler sends
        # the sampled tokens back, because there's no direct communication
        # between the first-stage worker and the last-stage worker.
        for req_idx, sampled_ids in enumerate(postprocessed_sampled_token_ids[:num_reqs]):
            if sampled_ids is None:
                continue

            start_idx = self.input_batch.num_tokens_no_spec[req_idx]
            end_idx = start_idx + len(sampled_ids)
            # NOTE(adobrzyn): assert for full max prompt length including
            # max_model_len and one token that's going to be generated
            # especially needed for biggest prompt in warm-up phase
            full_max_prompt = self.max_model_len + 1
            assert end_idx <= full_max_prompt, ("Sampled token IDs exceed the max model length. "
                                                f"Total number of tokens: {end_idx} > max_model_len: "
                                                f"{full_max_prompt}")

            self.input_batch.token_ids_cpu[req_idx, start_idx:end_idx] = sampled_ids
            self.input_batch.num_tokens_no_spec[req_idx] = end_idx
            self.input_batch.num_tokens[req_idx] = end_idx
            req_id = self.input_batch.req_ids[req_idx]
            req_state = self.requests[req_id]
            req_state.output_token_ids.extend(sampled_ids)

        ################## Spec Decode ##################
        # Now, we will call drafter to propose draft token ids
        if self.speculative_config:
            self._draft_token_ids = self.propose_draft_token_ids(
                scheduler_output, postprocessed_sampled_token_ids, prefill_sampled_token_ids_device,
                decode_sampled_token_ids_device, sampling_metadata, non_flattened_hidden_states, sample_hidden_states,
                aux_hidden_states, non_flattened_hidden_states_prefills, sample_hidden_states_prefills,
                aux_hidden_states_prefills, num_decodes, prefill_data if num_prefills > 0 else None,
                decode_data if num_decodes > 0 else None)
        ################## Spec Decode end ##################

        # Create output.
        all_req_ids = pd_info.decode_req_ids + pd_info.prompt_req_ids
        # prompt_logprobs_dict: dict[
        #    str, Optional[LogprobsTensors]] = self._get_prompt_logprobs_dict(
        #        prefill_hidden_states_device, scheduler_output)
        prompt_logprobs_dict: dict[str, Optional[LogprobsTensors]] = {}
        all_req_ids = pd_info.decode_req_ids + pd_info.prompt_req_ids
        logprobs = None

        if not warmup_mode:
            finished_sending, finished_recving = self.get_finished_kv_transfers(scheduler_output)

        if self.use_async_scheduling:
            model_runner_output = ModelRunnerOutput(
                req_ids=req_ids_output_copy,  # CHECK
                req_id_to_index=req_id_to_index_output_copy,
                sampled_token_ids=postprocessed_sampled_token_ids,
                logprobs=logprobs,
                prompt_logprobs_dict=prompt_logprobs_dict,  # type: ignore[arg-type]
                pooler_output=[],
                kv_connector_output=KVConnectorOutput(
                    finished_sending=finished_sending,
                    finished_recving=finished_recving,
                ))
            return AsyncHPUModelRunnerOutput(
                model_runner_output=model_runner_output,
                sampled_token_ids=sampled_token_ids,
                invalid_req_indices=invalid_req_indices,
                async_output_copy_stream=self.async_output_copy_stream,
            )
        model_runner_output = ModelRunnerOutput(
            req_ids=all_req_ids,
            req_id_to_index=self.input_batch.req_id_to_index,
            sampled_token_ids=postprocessed_sampled_token_ids,
            logprobs=logprobs,
            prompt_logprobs_dict=prompt_logprobs_dict,  # type: ignore[arg-type]
            pooler_output=[],
            kv_connector_output=KVConnectorOutput(
                finished_sending=finished_sending,
                finished_recving=finished_recving,
            ))
        if has_kv_transfer_group():
            get_kv_transfer_group().clear_connector_metadata()

        return model_runner_output

    def load_model(self) -> None:
        import habana_frameworks.torch.core as htcore
        if self.model_config.quantization == 'inc' or \
                self.model_config.quantization == 'fp8':
            htcore.hpu_set_env()
        logger.info("Starting to load model %s...", self.model_config.model)
        with HabanaMemoryProfiler() as m:  # noqa: SIM117
            self.model = get_model(vllm_config=self.vllm_config)
            if self.lora_config:
                self.model = self.load_lora_model(self.model, self.vllm_config, self.device)
        self.model_memory_usage = m.consumed_device_memory
        logger.info("Loading model weights took %.4f GB", self.model_memory_usage / float(2**30))

        if self._is_quant_with_inc():
            logger.info("Preparing model with INC..")
            with HabanaMemoryProfiler() as m_inc:
                from neural_compressor.torch.quantization import (FP8Config, convert, prepare)
                config = FP8Config.from_json_file(os.getenv("QUANT_CONFIG", ""))
                disable_mark_scales_as_const = os.getenv("VLLM_DISABLE_MARK_SCALES_AS_CONST", "false") in ("1", "true")
                self._inc_preprocess()
                if config.measure:
                    self.model = prepare(self.model, config)
                elif config.quantize:
                    self.model = convert(self.model, config)
                else:
                    raise ValueError("Unknown quantization config mode,"
                                     "please validate quantization config file")
                if not disable_mark_scales_as_const:
                    htcore.hpu_initialize(self.model, mark_only_scales_as_const=True)
            self.inc_initialized_successfully = True
            self.model_memory_usage = m_inc.consumed_device_memory
            logger.info("Preparing model with INC took %.4f GB", self.model_memory_usage / float(2**30))
        elif not is_fake_hpu():
            self.model = self.model.to("hpu")
            htcore.mark_step()
        self.maybe_set_chunked_attention_layers(self.model)
        hidden_layer_markstep_interval = int(os.getenv('VLLM_CONFIG_HIDDEN_LAYERS', '1'))
        model_config = getattr(self.model, "config", None)
        modify_model_layers(self.model,
                            get_target_layer_suffix_list(model_config.model_type if model_config is not None else None),
                            hidden_layer_markstep_interval)
        torch.hpu.synchronize()

        if not self.is_pooling_model:
            with HabanaMemoryProfiler() as m:
                self.model = _maybe_wrap_in_hpu_graph(
                    self.model,
                    vllm_config=self.vllm_config,
                )
        self.model_memory_usage = m.consumed_device_memory
        logger.info("Wrapping in HPUGraph took %.4f GB", self.model_memory_usage / float(2**30))

        ########### Spec Decode model ############
        if hasattr(self, "drafter"):
            with HabanaMemoryProfiler() as m:  # noqa: SIM117
                logger.info("Loading drafter model %s...", self.vllm_config.speculative_config.draft_model_config)
                self.drafter.load_model(self.model.model)
                if self.use_aux_hidden_state_outputs:
                    if supports_eagle3(self.model.model):
                        self.model.model.set_aux_hidden_state_layers(
                            self.model.model.get_eagle3_aux_hidden_state_layers())
                    else:
                        raise RuntimeError("Model does not support EAGLE3 interface but "
                                           "aux_hidden_state_outputs was requested")
            self.model_memory_usage = m.consumed_device_memory
            logger.info("Loading drafter model weights took %.4f GB", self.model_memory_usage / float(2**30))
            if hasattr(self.drafter, "model"):
                self.drafter.model = self.drafter.model.to("hpu")
                torch.hpu.synchronize()
                with HabanaMemoryProfiler() as m:  # noqa: SIM117
                    self.drafter.model = _maybe_wrap_in_hpu_graph(self.drafter.model, vllm_config=self.vllm_config)
                self.model_memory_usage = m.consumed_device_memory
                logger.info("Wrapping in HPUGraph took %.4f GB", self.model_memory_usage / float(2**30))
        #############################################

        with HabanaMemoryProfiler() as m:
            self._maybe_compile(self.model)
        self.model_memory_usage = m.consumed_device_memory
        logger.info("Compilation took %.4f GB", self.model_memory_usage / float(2**30))

    def _maybe_compile(self, *args, **kwargs):
        """Entrypoint for a torch.compilation of the model"""
        if (not is_fake_hpu() and not htorch.utils.internal.is_lazy()
                and not self.vllm_config.model_config.enforce_eager):
            # force_parameter_static_shapes = False  alows to use dynamic
            # shapes on tensors added to module via register_buffer()
            torch._dynamo.config.force_parameter_static_shapes = False
            self.compile_config = HPUCompileConfig()
            if self.compile_config.regional_compilation:
                self._compile_methods()
                self.regional_compilation_layers_list = [RMSNorm, VocabParallelEmbedding]
                self._regional_compilation(self.model)
                self.sampler = self._compile(self.sampler)
            else:
                self.model = self._compile(self.model)

    def _compile_methods(self):
        """
        Compile methods which are not part of the compiled model i.e. those
        which will not be compiled during model's compilation.
        """
        compiled_methods = ['_update_metadata', '_rotary_prepare_cos_sin']
        for method_name in compiled_methods:
            method = getattr(self.model, method_name, None)
            if method is not None:
                self._compile_region(self.model, method_name, method)

    def _regional_compilation(self, module, parent_module=None, module_name=None):
        """
        Recursively traverses a PyTorch module and compiles its regions, which
        can be one of two:
        1. Children of the nn.ModuleList
        2. Member of regional_compilation_layers_list
        """
        if isinstance(module, torch.nn.ModuleList):
            for children_name, children_module in module.named_children():
                self._compile_region(module, children_name, children_module)
        elif any(isinstance(module, layer) for layer in self.regional_compilation_layers_list):
            self._compile_region(
                parent_module,
                module_name,
                module,
            )
        else:
            for children_name, children_module in module.named_children():
                self._regional_compilation(children_module, module, children_name)

    def _compile_region(self, model, name, module):
        module = self._compile(module)
        setattr(model, name, module)

    def _compile(self, module):
        return torch.compile(module, **self.compile_config.get_compile_args())

    def _use_graphs(self):
        return not self.model_config.enforce_eager

    def _remove_duplicate_submodules(self):
        model = self.get_model()
        if hasattr(model, "model"):
            for layer in self.get_model().model.layers:
                self_attn = layer.self_attn
                # delete attr kv_b_proj in self_attn,
                # as they have been transferred to the MLAImpl.
                if hasattr(self_attn, "mla_attn"):
                    mla_attn = self_attn.mla_attn
                    duplicate_mods = [
                        "kv_a_proj_with_mqa",
                        "q_proj",
                        "kv_b_proj",
                        "o_proj",
                        "fused_qkv_a_proj",
                        "q_b_proj",
                    ]
                    for m in duplicate_mods:
                        if hasattr(self_attn, m) and hasattr(mla_attn, m):
                            delattr(self_attn, m)
                    if hasattr(mla_attn, "mla_attn") and hasattr(mla_attn.mla_attn, "impl"):
                        mla_impl = mla_attn.mla_attn.impl
                        duplicate_mods = ["kv_b_proj"]
                        for m in duplicate_mods:
                            if hasattr(mla_attn, m) and hasattr(mla_impl, m):
                                delattr(mla_attn, m)

    def _inc_preprocess(self):
        self._remove_duplicate_submodules()

    def log_graph_warmup_summary(self, buckets, is_prompt, total_mem):
        phase = f'Graph/{"Prompt" if is_prompt else "Decode"}'
        msg = (f'{phase} captured:{len(buckets)} '
               f'used_mem:{format_bytes(total_mem)}')
        logger.info(msg)

    def log_warmup(self, phase, i, max_i, first_dim, second_dim, third_dim, causal=False):
        free_mem = format_bytes(HabanaMemoryProfiler.current_free_device_memory())
        if self.unified_attn:
            msg = (f"[Warmup][{phase}][{i + 1}/{max_i}] "
                   f"query_len:{first_dim} "
                   f"shared_blocks:{second_dim} "
                   f"unique_blocks:{third_dim} "
                   f"({'causal' if causal else 'non causal'}) "
                   f"free_mem:{free_mem}")
        else:
            msg = (f"[Warmup][{phase}][{i + 1}/{max_i}] "
                   f"batch_size:{first_dim} "
                   f"query_len:{second_dim} "
                   f"num_blocks:{third_dim} "
                   f"free_mem:{free_mem}")
        tqdm.write(msg)

    def log_warmup_multimodal(self, phase, i, max_i, batch_size, seq_len, img_args):
        free_mem = format_bytes(HabanaMemoryProfiler.current_free_device_memory())
        msg = (f"[Warmup][{phase}][{i+1}/{max_i}] "
               f"batch_size:{batch_size} "
               f"seq_len:{seq_len} "
               f"img_args:{img_args} "
               f"free_mem:{free_mem}")
        logger.info(msg)

    def warmup_sampler(self):
        """
        Warmup the sampler with different temperature, top-p, and top-k values.
        """
        # Choose batch sizes for warmup based on bucketing
        test_batch_sizes = list(dict.fromkeys([0, 1] + [bucket[0] for bucket in self.bucketing_manager.decode_buckets]))

        # Test different sampling configurations
        sampling_configs = [
            # (temperature, top_p, top_k, batch_changed)
            (0.0, 1.0, 0, True),  # Greedy sampling
            (1.0, 1.0, 0, True),  # Random sampling with temp=1.0
            (0.7, 0.9, 50, True),  # Common creative settings
            (0.3, 0.95, 20, True),  # Conservative settings
            (1.2, 0.8, 100, True),  # High temperature settings
            (0.8, 0.85, 0, True),  # Different top-p sampling
            (0.0, 1.0, 0, False),  # Greedy sampling
            (1.0, 1.0, 0, False),  # Random sampling with temp=1.0
            (0.7, 0.9, 50, False),  # Common creative settings
            (0.3, 0.95, 20, False),  # Conservative settings
            (1.2, 0.8, 100, False),  # High temperature settings
            (0.8, 0.85, 0, False),  # Different top-p sampling
        ]

        logger.info("Warming up sampler with batch sizes: %s and following configs:", test_batch_sizes)
        for temp, top_p, top_k, batch_changed in sampling_configs:
            logger.info("temp=%s, top_p=%s, top_k=%s, batch_changed=%s", temp, top_p, top_k, batch_changed)
        logger.info("Starting sampler warmup...")

        for batch_size in test_batch_sizes:
            dummy_hidden_states = torch.randn(batch_size, self.hidden_size, dtype=self.dtype, device=self.device)
            if self.lora_config:
                lora_logits_mask = torch.zeros(batch_size,
                                               (self.lora_config.max_loras) * self.lora_config.max_lora_rank,
                                               dtype=self.lora_config.lora_dtype).to('hpu')
                LoraMask.setLoraMask(lora_logits_mask)
            dummy_logits = self.model.compute_logits(dummy_hidden_states)

            # Create dummy requests for this specific configuration
            dummy_req_ids = [f"warmup_req_{batch_size}_{i}" for i in range(max(1, batch_size))]

            for i, req_id in enumerate(dummy_req_ids):
                self.requests[req_id] = CachedRequestState(
                    req_id=req_id,
                    prompt_token_ids=list(range(10)),  # Dummy prompt
                    mm_features=[],
                    sampling_params=SamplingParams(),
                    pooling_params=None,
                    generator=None,
                    block_ids=[[0]],
                    num_computed_tokens=10,
                    output_token_ids=[],
                )
                self.input_batch.req_id_to_index[req_id] = i

            for temp, top_p, top_k, batch_changed in sampling_configs:
                # Add dummy requests to cache with consistent sampling params
                for i, req_id in enumerate(dummy_req_ids):
                    self.requests[req_id].sampling_params = SamplingParams(
                        temperature=temp,
                        top_p=top_p,
                        top_k=top_k,
                    )

                    if temp == 0.0:  # Greedy sampling
                        self.input_batch.greedy_reqs.add(req_id)
                    else:  # Random sampling
                        self.input_batch.random_reqs.add(req_id)

                self.input_batch.req_output_token_ids = [
                    item[1] for item in self._generate_req_id_output_token_ids_lst(dummy_req_ids, pad_to=batch_size)
                ]
                self.input_batch.refresh_sampling_metadata()

                _sampler_output, _sampling_metadata = self._run_sampling(batch_changed=batch_changed,
                                                                         logits_device=dummy_logits,
                                                                         request_ids=dummy_req_ids,
                                                                         pad_to=dummy_logits.shape[0])

                # Cleanup after sampling
                self.input_batch.greedy_reqs = set()
                self.input_batch.random_reqs = set()
                self.input_batch.req_output_token_ids = []

            # Cleanup after batch has been warmed up
            self.input_batch.req_id_to_index = {}
            self.requests = {}

        # Final synchronization to ensure all operations are completed
        torch.hpu.synchronize()

        logger.info("Sampler warmup completed successfully")

    def warmup_defragmenter(self):
        """Warm up defragmentation swap graphs for different thresholds.

        We execute a minimal swap (1 pair) which will be padded internally to the
        requested threshold size. Thresholds chosen to mirror potential production
        values: 8, 16, 32, 64, 128, 256, 512.
        """
        # If defragmenter is disabled or cache utils not prepared, skip.
        if not getattr(self.defragmenter, 'enabled', False):
            return
        if self.defragmenter.cache_utils is None:
            return

        thresholds = self.defragmenter.to_swap_pad_thresholds

        logger.info("Warming up defragmenter with thresholds: %s", thresholds)

        # Use simple valid block ids present in caches (assume at least 2 blocks allocated when kv caches created)
        # We only need distinct ids for a swap. They will be scaled by block_size inside swap.
        # If for some reason only 1 block exists, skip warmup gracefully.
        try:
            k_cache = self.defragmenter.cache_utils.kv_caches[0][0]
            num_blocks_available = k_cache.shape[0] // self.block_size
        except Exception:
            num_blocks_available = 0
        if num_blocks_available < 2:
            logger.warning("Skipping defragmenter warmup, insufficient blocks (%s)", num_blocks_available)
            return

        # Minimal pair to trigger a swap path
        to_swap = [(1, 0)]

        for th in thresholds:
            self.defragmenter.cache_utils.swap(to_swap, th)

        # If the number of swaps was odd, do one more to make it even and return to original state.
        if len(thresholds) % 2 == 1:
            self.defragmenter.cache_utils.swap(to_swap, thresholds[0])

        logger.info("Defragmenter warmup completed successfully")

    def warmup_graphs(self, buckets, is_prompt, kv_caches, starting_mem=0, total_batch_seq=0.001):
        total_mem = starting_mem
        idx = 0
        num_candidates = len(buckets)
        captured_all = True
        developer_settings = get_config().VLLM_DEVELOPER_MODE
        phase = 'Prompt' if is_prompt else 'Decode'
        desc = f'{phase} warmup processing: '
        with tqdm(total=num_candidates, desc=desc, unit="item") as pbar:
            for idx, (batch_size, seq_len, num_blocks) in enumerate(reversed(buckets)):
                if seq_len > self.max_num_tokens:
                    continue
                # Graph memory usage is proportional to seq dimension in a batch
                if is_prompt:
                    batch_seq = batch_size * seq_len * num_blocks if num_blocks else batch_size * seq_len
                else:
                    batch_seq = batch_size

                graphed_bucket = (batch_size, seq_len, num_blocks, is_prompt)
                if graphed_bucket in self.graphed_buckets:
                    continue
                self.graphed_buckets.add(graphed_bucket)
                if developer_settings:
                    self.log_warmup(phase, idx, num_candidates, batch_size, seq_len, num_blocks)
                prompt_cfg, decode_cfg = None, None
                with HabanaMemoryProfiler() as mem_prof:
                    if is_prompt:
                        prompt_cfg = (batch_size, seq_len, num_blocks)
                    else:
                        decode_cfg = (batch_size, 1, num_blocks)
                    self._prepare_dummy_scenario(prompt_cfg, decode_cfg)
                # TODO(kzawora): align_workers
                used_mem = mem_prof.consumed_device_memory
                total_mem += used_mem
                total_batch_seq += batch_seq

                pbar.set_postfix_str(f"{idx}/{num_candidates}")
                pbar.update(1)

        return total_mem, total_batch_seq, captured_all

    def warmup_unified_graphs(self, buckets, kv_cache):
        idx = 0
        num_candidates = len(buckets)
        developer_settings = get_config().VLLM_DEVELOPER_MODE
        with tqdm(total=num_candidates, desc="Unified Attention warmup", unit="item") as pbar:
            for idx, (query, shared_ctx, unique_ctx, is_causal) in enumerate(reversed(buckets)):
                unified_cfg = (query, shared_ctx, unique_ctx, is_causal)
                if unified_cfg in self.graphed_buckets:
                    continue
                self.graphed_buckets.add(unified_cfg)
                if developer_settings:
                    self.log_warmup("Unified CFG", idx, num_candidates, query, shared_ctx, unique_ctx, is_causal)
                self._prepare_dummy_unified_scenario(unified_cfg)
                pbar.set_postfix_str(f"{idx}/{num_candidates}")
                pbar.update(1)

    def _add_dummy_request(self,
                           requests,
                           num_scheduled_tokens,
                           num_computed_tokens,
                           total_tokens,
                           scheduled_tokens,
                           is_prompt,
                           block_id=0):
        num_blocks = round_up(total_tokens, self.block_size) // self.block_size
        prompt_token_ids = list(range(total_tokens))

        req_id = f'{len(requests)}'
        block_ids = [block_id] * num_blocks
        sampling_params = SamplingParams(temperature=0.0)

        req = NewRequestData(
            req_id=req_id,
            prompt_token_ids=prompt_token_ids,
            mm_features=[],
            sampling_params=sampling_params,
            pooling_params=None,
            block_ids=[block_ids],
            num_computed_tokens=num_computed_tokens,
            lora_request=None,
        )
        requests.append(req)
        if is_prompt:
            num_scheduled_tokens[req_id] = len(prompt_token_ids) - num_computed_tokens
        else:
            num_scheduled_tokens[req_id] = scheduled_tokens

    def _add_dummy_unified_request(self, requests, is_prompt, is_unique, block_num, num_computed_tokens,
                                   num_scheduled_tokens, scheduled_tokens):
        from vllm.v1.core.sched.output import NewRequestData

        req_id = f'{len(requests)}'
        sampling_params = SamplingParams(temperature=0.0)
        num_computed_tokens = max(len(block_num) * self.block_size - 1, 0)

        if is_prompt:
            # num_computed_tokens + num_scheduled_tokens <= prompt token ids
            prompt_token_ids = num_computed_tokens + num_scheduled_tokens + 1
        else:
            # num_computed_tokens + num_scheduled_tokens > prompt_token_ids
            prompt_token_ids = num_computed_tokens + num_scheduled_tokens - 1
        prompt_token_ids = list(range(prompt_token_ids))

        req = NewRequestData(
            req_id=req_id,
            prompt_token_ids=prompt_token_ids,
            mm_features=[],
            sampling_params=sampling_params,
            pooling_params=None,
            block_ids=[block_num],
            num_computed_tokens=num_computed_tokens,
            lora_request=None,
        )

        requests.append(req)
        scheduled_tokens[req_id] = num_scheduled_tokens

    @staticmethod
    def _generate_seq_lengths(num_samples, num_blocks, block_size):
        assert num_samples <= num_blocks
        blocks = [num_blocks // num_samples] * num_samples
        missing_blocks = num_blocks - sum(blocks)
        for i in range(missing_blocks):
            blocks[i] += 1
        seq_lengths = [b * block_size - 1 for b in blocks]
        return seq_lengths

    def distribute_sum_evenly(self, total_sum, max_length):
        '''
        Return a balanced list of ints that sums up to total_sum.
        List cannot be longer than max_length.
        '''
        base, remain = divmod(total_sum, max_length)
        result = [base] * max_length

        for i in range(remain):
            result[i] += 1

        return result

    def get_merged_prefill_seq_lens(self, query_len, ctx_blocks):
        '''
        Get seperate sequence lengths from merged layout to individual 
        samples.
        Returns list of sequence length (including query and context) and
        context lengths.
        '''
        ctx_list = self.distribute_sum_evenly(ctx_blocks, self.max_num_seqs)
        query_list = self.distribute_sum_evenly(query_len, self.max_num_seqs)
        prompt_list = [q + c * self.block_size for q, c in zip(query_list, ctx_list)]
        ctx_list = ctx_list if len(ctx_list) > 0 else [0] * len(prompt_list)
        return prompt_list, ctx_list

    def _prepare_dummy_unified_scenario(self, unified_cfg):
        requests: list[NewRequestData] = []
        scheduled_tokens: dict[str, int] = {}

        query_len, shared_ctx_len, unique_ctx_len, is_causal = unified_cfg
        num_computed_tokens = (shared_ctx_len + unique_ctx_len) * self.block_size

        if is_causal:
            decode_reqs_query = []
            decode_reqs_blocks = []
            prompt_reqs_query = []
            prompt_reqs_blocks: list = []

            all_shared_blocks_ids = [block for block in range(shared_ctx_len)]
            unique_block = unique_ctx_len - 1
            # do not use unique block id
            if unique_block in all_shared_blocks_ids:
                all_shared_blocks_ids.remove(unique_ctx_len - 1)
                all_shared_blocks_ids.append(shared_ctx_len + 1)

            #add unique
            if unique_ctx_len > 0:
                decode_reqs_query.append(1)
                decode_reqs_blocks.append([unique_ctx_len - 1])
            prompts_number = self.max_num_seqs - len(decode_reqs_query)
            remaining_query = query_len - sum(decode_reqs_query)

            q, r = divmod(remaining_query, prompts_number)
            prompt_reqs_query = [q + (1 if i < r else 0) for i in range(prompts_number)]
            prompt_reqs_blocks = [[] for _ in range(len(prompt_reqs_query))]
            for idx, query in enumerate(prompt_reqs_query):
                available_space_for_ctx = math.floor((self.max_model_len - query) // self.block_size)
                if len(all_shared_blocks_ids) >= available_space_for_ctx:
                    prompt_reqs_blocks[idx] = all_shared_blocks_ids[:available_space_for_ctx]
                    del all_shared_blocks_ids[:available_space_for_ctx]
                else:
                    prompt_reqs_blocks[idx] = all_shared_blocks_ids
                    break
            if unique_ctx_len > 0:
                self._add_dummy_unified_request(requests, False, True, [unique_ctx_len - 1], num_computed_tokens, 1,
                                                scheduled_tokens)

            for query, blocks in zip(prompt_reqs_query, prompt_reqs_blocks):
                self._add_dummy_unified_request(requests, True, False, blocks, num_computed_tokens, query,
                                                scheduled_tokens)
        else:
            remaining_samples = query_len
            base = shared_ctx_len // remaining_samples
            remain = shared_ctx_len % remaining_samples
            all_shared_blocks_ids = [block for block in range(shared_ctx_len)]
            unique_block = unique_ctx_len - 1
            # do not use unique block id
            if unique_block in all_shared_blocks_ids:
                all_shared_blocks_ids.remove(unique_ctx_len - 1)
                all_shared_blocks_ids.append(shared_ctx_len + 1)

            # distribute evenly across sublists
            split_shared_blocks_ids: list[list[int]] = [[] for _ in range(remaining_samples)]
            idx = 0
            for i in range(remaining_samples):
                size = base + (1 if i < remain else 0)
                for _ in range(size):
                    split_shared_blocks_ids[i].append(all_shared_blocks_ids[idx])
                    idx += 1

            # make sure that all blocks are shared = in at least two decodes
            for i, block in enumerate(all_shared_blocks_ids):
                target = (i + 1) % remaining_samples
                if block not in split_shared_blocks_ids[target]:
                    split_shared_blocks_ids[target].append(block)

            # add unique id
            if unique_ctx_len > 0:
                min_idx = min(range(remaining_samples), key=lambda j: len(split_shared_blocks_ids[j]))
                split_shared_blocks_ids[min_idx].append(unique_block)

            for i in range(len(split_shared_blocks_ids)):
                if not split_shared_blocks_ids[i]:
                    if unique_block - i >= 0:
                        split_shared_blocks_ids[i] = [unique_block - i]
                    else:
                        split_shared_blocks_ids[i] = [all_shared_blocks_ids[0]]

            for request_blocks in split_shared_blocks_ids:
                self._add_dummy_unified_request(requests, False, False, request_blocks, num_computed_tokens, 1,
                                                scheduled_tokens)

        self._execute_dummy_scenario(requests, scheduled_tokens)

    def _prepare_dummy_scenario(self, prompt_cfg, decode_cfg):
        requests: list[NewRequestData] = []
        scheduled_tokens: dict[str, int] = {}

        if prompt_cfg:
            prompt_bs, prompt_query_len, prompt_num_blocks = prompt_cfg
            prompt_ctx_len = prompt_num_blocks * self.block_size
            prompt_total_tokens = [prompt_query_len + prompt_ctx_len]
            prompt_num_context_blocks = [prompt_num_blocks]
            if self.max_model_len < sum(prompt_total_tokens) \
                and self.use_merged_prefill:
                # split query and ctx in merged prefill case
                prompt_total_tokens, prompt_num_context_blocks = \
                     self.get_merged_prefill_seq_lens(prompt_query_len,
                                                 prompt_num_blocks)
            for _ in range(prompt_bs):
                for tokens, context_len in zip(prompt_total_tokens, prompt_num_context_blocks):
                    self._add_dummy_request(requests,
                                            scheduled_tokens,
                                            num_computed_tokens=(context_len * self.block_size),
                                            total_tokens=tokens,
                                            scheduled_tokens=prompt_query_len,
                                            is_prompt=True)
        if decode_cfg:
            decode_bs, decode_query_len, decode_num_blocks = decode_cfg
            if self.use_contiguous_pa:
                decode_seq_lengths = [self.block_size] * decode_bs
                block_id = decode_num_blocks - 1
            else:
                decode_seq_lengths = self._generate_seq_lengths(decode_bs, decode_num_blocks, self.block_size)
                block_id = 0
            for dsl in decode_seq_lengths:
                self._add_dummy_request(requests,
                                        scheduled_tokens,
                                        num_computed_tokens=dsl,
                                        total_tokens=dsl,
                                        scheduled_tokens=1,
                                        is_prompt=False,
                                        block_id=block_id)
        self._execute_dummy_scenario(requests, scheduled_tokens)

    def _execute_dummy_scenario(self, requests, scheduled_tokens):
        from vllm.v1.core.sched.output import (SchedulerOutput, CachedRequestData)

        sched_output = SchedulerOutput(
            scheduled_new_reqs=requests,
            scheduled_cached_reqs=CachedRequestData.make_empty(),
            num_scheduled_tokens=scheduled_tokens,
            total_num_scheduled_tokens=sum(scheduled_tokens.values()),
            scheduled_spec_decode_tokens={},
            scheduled_encoder_inputs={},
            num_common_prefix_blocks=0,
            finished_req_ids=set(),
            free_encoder_mm_hashes=[],
        )
        cleanup = SchedulerOutput(
            scheduled_new_reqs=[],
            scheduled_cached_reqs=CachedRequestData.make_empty(),
            num_scheduled_tokens={},
            total_num_scheduled_tokens=0,
            scheduled_spec_decode_tokens={},
            scheduled_encoder_inputs={},
            num_common_prefix_blocks=0,
            finished_req_ids=set(req.req_id for req in requests),
            free_encoder_mm_hashes=[],
        )
        self.execute_model(sched_output, warmup_mode=True)
        self.sample_tokens(None)
        self.execute_model(cleanup, warmup_mode=True)

    def _generate_profiling(self, prompt_cfg, decode_cfg):
        steps = 3
        profiler = setup_profiler(warmup=steps - 1, active=1)
        if prompt_cfg and prompt_cfg not in self.bucketing_manager.prompt_buckets:
            self.bucketing_manager.prompt_buckets.insert(0, prompt_cfg)
        elif decode_cfg and decode_cfg not in self.bucketing_manager.decode_buckets:
            self.bucketing_manager.decode_buckets.insert(0, decode_cfg)
        torch.hpu.synchronize()
        profiler.start()
        for _ in range(steps):
            self._prepare_dummy_scenario(prompt_cfg, decode_cfg)
            torch.hpu.synchronize()
            profiler.step()
        profiler.stop()

    @staticmethod
    def _parse_profile_cfg(profile_cfg):
        if profile_cfg:
            return tuple(map(int, profile_cfg.split(',')))
        return None

    @staticmethod
    def _parse_legacy_profile_cfg(profile_cfg):
        if profile_cfg:
            cfg = profile_cfg.split('_')
            assert cfg[0] in ['prompt', 'decode']
            return (cfg[0], int(cfg[1]), int(cfg[2]), cfg[3] == 't')
        return None

    def _read_profiling_cfg(self):
        prompt_cfg = self._parse_profile_cfg(os.environ.get('VLLM_PROFILE_PROMPT', None))
        decode_cfg = self._parse_profile_cfg(os.environ.get('VLLM_PROFILE_DECODE', None))
        legacy_cfg = self._parse_legacy_profile_cfg(os.environ.get('VLLM_PT_PROFILE', None))
        if legacy_cfg and not (prompt_cfg or decode_cfg):
            phase, bs, seq_or_blocks, use_graphs = legacy_cfg
            assert use_graphs != self.model_config.enforce_eager, \
                "'use_graphs' is out of sync with model config. " \
                "Either change the flag or change vllm engine parameters"
            if phase == 'prompt':
                prompt_cfg = (bs, seq_or_blocks, 0)
            else:
                decode_cfg = (bs, seq_or_blocks)
        # align with current bucketing
        if decode_cfg:
            decode_cfg = (decode_cfg[0], 1, decode_cfg[1])
        return prompt_cfg, decode_cfg

    def _get_mm_dummy_batch(
        self,
        modality: str,
        max_items_per_batch: int,
    ) -> BatchedTensorInputs:
        """Dummy data for profiling and precompiling multimodal models."""
        assert self.mm_budget is not None

        dummy_decoder_data = self.mm_registry.get_decoder_dummy_data(
            model_config=self.model_config,
            seq_len=self.max_model_len,
            mm_counts={modality: 1},
            cache=self.mm_budget.cache,
        )
        dummy_mm_data = dummy_decoder_data.multi_modal_data

        # Result in the maximum GPU consumption of the model
        dummy_mm_item = dummy_mm_data[modality][0]
        dummy_mm_items = [dummy_mm_item] * max_items_per_batch

        self.model.model = cast(SupportsMultiModal, self.model.model)
        return next(mm_kwargs_group for _, _, mm_kwargs_group in group_mm_kwargs_by_modality(
            dummy_mm_items,
            device=self.device,
            pin_memory=self.pin_memory,
            merge_by_field_config=self.model.model.merge_by_field_config,
        ))

    def warmup_multimodal_graphs(self, buckets):

        phase = 'Graph/Multimodal'
        from vllm.v1.worker.utils import MultiModalBudget
        self.mm_budget = MultiModalBudget(
            self.model_config,
            self.scheduler_config,
            self.mm_registry,
        ) if self.supports_mm_inputs else None

        #self.mm_budget.mm_limits : {'image': 2}
        for modality, max_items in self.mm_budget.mm_limits.items():
            phase = f'Graph/Multimodal({modality})'
            num_candidates = len(buckets)

            for idx, img_arg in enumerate(buckets):
                # Create dummy batch of multimodal inputs.
                batched_dummy_mm_inputs = self._get_mm_dummy_batch(
                    modality,
                    img_arg,
                )
                #htorch.core.mark_step()
                # Run multimodal encoder.
                dummy_encoder_outputs = \
                     self.model.embed_multimodal(
                     **batched_dummy_mm_inputs)
                #htorch.core.mark_step()

                sanity_check_mm_encoder_outputs(
                    dummy_encoder_outputs,
                    expected_num_items=img_arg,
                )

                self.graphed_buckets.add(img_arg)
                self.log_warmup_multimodal(phase, idx, num_candidates, 1, 0, img_arg)

    @torch.inference_mode()
    def warmup_model(self) -> None:
        if not self.enable_bucketing:
            return

        if self.unified_attn:
            self.bucketing_manager.generate_unified_buckets()
        else:
            self.bucketing_manager.generate_prompt_buckets()
            self.bucketing_manager.generate_decode_buckets()

            if self.supports_mm_inputs:
                # Delayed multimodal buckets during warmup until model is loaded.
                from vllm_gaudi.extension.bucketing.vision import HPUVisionBucketManager
                self.get_model().vision_bucket_manager = HPUVisionBucketManager(self.model_config.model)
                msg = (f"Multimodal bucket : {self.get_model().vision_bucket_manager.multimodal_buckets}")
                logger.info(msg)

            max_bucket = max(self.bucketing_manager.decode_buckets[-1][0], self.bucketing_manager.prompt_buckets[-1][0])
            if max_bucket > self.input_batch.max_num_reqs:
                input_batch_bkp = self.input_batch
                self.input_batch = InputBatch(
                    max_num_reqs=self.bucketing_manager.decode_buckets[-1][0],
                    max_model_len=self.max_model_len,
                    max_num_batched_tokens=self.max_num_tokens,
                    device=self.device,
                    pin_memory=self.pin_memory,
                    vocab_size=self.model_config.get_vocab_size(),
                    block_sizes=[self.block_size],
                    kernel_block_sizes=[self.block_size],
                    logitsprocs=build_logitsprocs(self.vllm_config, self.device, self.pin_memory, self.is_pooling_model,
                                                  self.vllm_config.model_config.logits_processors),
                )

        self.defragmenter.initialize(self.kv_caches, self.block_size)

        prompt_profile_cfg, decode_profile_cfg = self._read_profiling_cfg()
        if prompt_profile_cfg or decode_profile_cfg:
            self._generate_profiling(prompt_profile_cfg, decode_profile_cfg)
            raise AssertionError("Finished profiling")
        kv_caches = self.kv_caches

        if not htorch.utils.internal.is_lazy() and not self.model_config.enforce_eager:
            multiplier = 5 if self.compile_config.regional_compilation else 1
            cache_size_limit = 1 + multiplier * (len(self.bucketing_manager.prompt_buckets) +
                                                 len(self.bucketing_manager.decode_buckets))
            torch._dynamo.config.cache_size_limit = max(cache_size_limit, torch._dynamo.config.cache_size_limit)
            # Multiply by 8 to follow the original default ratio between
            # the cache_size_limit and accumulated_cache_size_limit
            torch._dynamo.config.accumulated_cache_size_limit = max(cache_size_limit * 8,
                                                                    torch._dynamo.config.accumulated_cache_size_limit)
            # NOTE(kzawora): I'm not exactly sure why, but if we don't set this in unified attention to a high enough
            # value, we'll see warmup mode bypassing compilation and execute everything eagerly.
            if self.unified_attn:
                torch._dynamo.config.accumulated_recompile_limit = sys.maxsize
                torch._dynamo.config.recompile_limit = sys.maxsize

        if self.skip_warmup:
            logger.info("Skipping warmup...")
            return

        self.profiler.start('internal', 'warmup')
        start_mem = HabanaMemoryProfiler.current_device_memory_usage()
        start_time = time.perf_counter()

        # Most model's multimodal embedding has to be run without COMPILE ONLY mode.
        if self.supports_mm_inputs:
            self.warmup_multimodal_graphs(self.get_model().vision_bucket_manager.multimodal_buckets)

        compile_only_mode_context = functools.partial(bc.env_setting, "PT_COMPILE_ONLY_MODE", True)
        can_use_compile_only_mode = True
        try:
            with compile_only_mode_context():
                pass
            logger.debug("Using PT_COMPILE_ONLY_MODE.")
        except KeyError:
            can_use_compile_only_mode = False
            logger.warning('Cannot use PT_COMPILE_ONLY_MODE. '
                           'Warmup time will be negatively impacted. '
                           'Please update Gaudi Software Suite.')
        with compile_only_mode_context() if can_use_compile_only_mode else contextlib.nullcontext():
            if not self.model_config.enforce_eager:
                assert self.mem_margin is not None, \
                    ("HabanaWorker.determine_num_available_blocks needs "
                     "to be called before warming up the model.")

                self.warmup_sampler()
                self.warmup_defragmenter()

                # TODO(kzawora): align_workers
                if self.unified_attn:
                    self.warmup_unified_graphs(self.bucketing_manager.unified_buckets, kv_caches)
                else:
                    mem_post_prompt, prompt_batch_seq, prompt_captured_all = \
                        self.warmup_graphs(
                            self.bucketing_manager.prompt_buckets, True, kv_caches)
                    mem_post_decode, decode_batch_seq, decode_captured_all = \
                        self.warmup_graphs(
                            self.bucketing_manager.decode_buckets, False, kv_caches)

                    self.log_graph_warmup_summary(self.bucketing_manager.prompt_buckets, True, mem_post_prompt)
                    self.log_graph_warmup_summary(self.bucketing_manager.decode_buckets, False, mem_post_decode)

        end_time = time.perf_counter()
        end_mem = HabanaMemoryProfiler.current_device_memory_usage()
        if os.getenv('VLLM_FULL_WARMUP', 'false').strip().lower() in ("1", "true"):
            # Since the model is warmed up for all possible tensor sizes,
            # Dynamo can skip checking the guards
            torch.compiler.set_stance(skip_guard_eval_unsafe=True)
        elapsed_time = end_time - start_time
        msg = (f"Warmup finished in {elapsed_time:.0f} secs, "
               f"allocated {format_bytes(end_mem - start_mem)} of device memory")
        logger.info(msg)
        self.profiler.end()

        if not self.unified_attn and max_bucket > self.input_batch.max_num_reqs:
            self.input_batch = input_batch_bkp
        # NOTE(kzawora): This is a nasty workaround - for whatever cache_utils-related reason,
        # reusing defragmenter used in warmup causes accuracy drops, which is why we re-create
        # and re-initialize it.
        self.defragmenter = OnlineDefragmenter()
        self.defragmenter.initialize(self.kv_caches, self.block_size)

    def shutdown_inc(self):
        can_finalize_inc = self._is_quant_with_inc() and \
            (self.model.model is not None) and \
            self.inc_initialized_successfully and \
            not self._is_inc_finalized
        if can_finalize_inc:
            from neural_compressor.torch.quantization import (finalize_calibration)
            finalize_calibration(self.model.model)
            self._is_inc_finalized = True

    def __del__(self):
        self.shutdown_inc()

    @torch.inference_mode()
    def profile_run(self) -> None:
        return

    def _dummy_run(self, max_num_batched_tokens: int) -> None:
        assert max_num_batched_tokens == 1
        # when P/D disagg used, add dummy prefill run for prefiller instance
        if has_kv_transfer_group() and self.vllm_config.kv_transfer_config.is_kv_producer:
            prompt_cfg = 1, 1, 1
            decode_cfg = None
        else:
            prompt_cfg = None
            decode_cfg = 1, 1, 1
        # add dummy run
        self._prepare_dummy_scenario(prompt_cfg, decode_cfg)
        return

    def initialize_kv_cache(self, kv_cache_config: KVCacheConfig) -> None:
        """
        Initialize KV cache based on `kv_cache_config`.
        Args:
            kv_cache_config: Configuration for the KV cache, including the KV
            cache size of each layer
        """
        if len(kv_cache_config.kv_cache_groups) > 1:
            raise NotImplementedError("Hybrid models with more than one KV cache type are not "
                                      "supported yet.")

        # build a map from layer_name -> KVCacheTensor
        tensor_map: dict[str, KVCacheTensor] = {}
        for tensor in kv_cache_config.kv_cache_tensors:
            for lname in tensor.shared_by:
                tensor_map[lname] = tensor

        kv_caches: dict[str, torch.Tensor] = {}
        kv_cache_sizes = {}
        for kv_cache_tensor in kv_cache_config.kv_cache_tensors:
            assert len(kv_cache_tensor.shared_by) == 1
            kv_cache_sizes[kv_cache_tensor.shared_by[0]] = kv_cache_tensor.size

        for kv_cache_group in kv_cache_config.kv_cache_groups:
            kv_cache_spec = kv_cache_group.kv_cache_spec
            for kv_cache_tensor in kv_cache_config.kv_cache_tensors:
                assert kv_cache_tensor.size % kv_cache_spec.page_size_bytes == 0
                num_blocks = \
                    kv_cache_tensor.size // kv_cache_spec.page_size_bytes
                # `num_blocks` is the number of blocks the model runner can use.
                # `kv_cache_config.num_blocks` is the number of blocks that
                # KVCacheManager may allocate.
                # Since different GPUs may have different number of layers and
                # different memory capacities, `num_blocks` can be different on
                # different GPUs, and `kv_cache_config.num_blocks` is set to
                # the min of all `num_blocks`. Verify it here.
                assert num_blocks >= kv_cache_config.num_blocks
                if isinstance(kv_cache_spec, FullAttentionSpec):
                    kv_cache_shape = self.attn_backend.get_kv_cache_shape(num_blocks + 1, kv_cache_spec.block_size,
                                                                          kv_cache_spec.num_kv_heads,
                                                                          kv_cache_spec.head_size)
                    v_cache_shape = None if self.model_config.use_mla \
                        else kv_cache_shape
                    dtype = kv_cache_spec.dtype
                    key_cache = torch.zeros(kv_cache_shape, dtype=dtype, device=self.device)
                    if v_cache_shape is not None:
                        value_cache = torch.zeros(v_cache_shape, dtype=dtype, device=self.device)
                    else:
                        value_cache = None
                    for layer_name in kv_cache_tensor.shared_by:
                        kv_caches[layer_name] = (key_cache, value_cache)
                else:
                    # TODO: add new branches when introducing more types of
                    # KV cache specs.
                    raise ValueError("Unknown KV cache spec type.")
            layer_names = set()
            for group in kv_cache_config.kv_cache_groups:
                layer_names.update(group.layer_names)
            assert layer_names == set(kv_caches.keys()), "Some layers are not correctly initialized"
        bind_kv_cache(kv_caches, self.vllm_config.compilation_config.static_forward_context, self.kv_caches)

        if self.enable_bucketing:
            self.bucketing_manager.num_hpu_blocks = num_blocks
        self._PAD_BLOCK_ID = num_blocks
        self._PAD_SLOT_ID = num_blocks * self.block_size

        if has_kv_transfer_group():
            get_kv_transfer_group().register_kv_caches(self.get_kv_caches_4D(kv_caches))
            if self.vllm_config.kv_transfer_config.kv_buffer_device == "cpu":
                get_kv_transfer_group().set_host_xfer_buffer_ops(copy_kv_blocks)
            global hpu_buffer
        if self.unified_attn:
            with HabanaMemoryProfiler() as m:
                from vllm_gaudi.extension.unified_batch import UnifiedBatchPersistentContext
                max_num_shared_blocks = math.ceil(num_blocks * get_config().unified_attn_shared_cache_ratio)
                self.unified_attn_persistent_ctx = UnifiedBatchPersistentContext(self.max_num_batched_tokens,
                                                                                 max_num_shared_blocks, num_blocks,
                                                                                 self.block_size, dtype)
            logger.info("Allocating unified persistent batch took %.4f GB of host memory",
                        m.consumed_host_memory / float(2**30))

        htorch.hpu.synchronize()

    def get_kv_caches_4D(self, kv_caches) -> dict[str, torch.Tensor]:
        kv_caches_4D: dict[str, torch.Tensor] = {}
        for layer_name, cache_or_cachelist in kv_caches.items():
            kv_cache_per_layer = []
            for cache in cache_or_cachelist:
                if cache is None:
                    continue
                kv_cache_per_layer.append(cache.view(-1, self.block_size, *cache.shape[1:]))
                #NOTE(Chendi): Do not remove, call torch data_ptr to record physical address
                cache.data_ptr()
            kv_caches_4D[layer_name] = TensorTuple(tuple(kv_cache_per_layer)) \
                if len(kv_cache_per_layer) == 2 else kv_cache_per_layer[0]
        return kv_caches_4D

    def get_supported_generation_tasks(self) -> list[GenerationTask]:
        model = self.get_model()
        supported_tasks = list[GenerationTask]()

        if is_text_generation_model(model):
            supported_tasks.append("generate")

        if supports_transcription(model):
            if model.supports_transcription_only:
                return ["transcription"]

            supported_tasks.append("transcription")

        return supported_tasks

    def get_supported_pooling_tasks(self) -> list[PoolingTask]:
        model = self.get_model()
        if not is_pooling_model(model):
            return []

        return list(model.pooler.get_supported_tasks())

    def get_supported_tasks(self) -> tuple[SupportedTask, ...]:
        tasks = list[SupportedTask]()

        if self.model_config.runner_type == "generate":
            tasks.extend(self.get_supported_generation_tasks())
        if self.model_config.runner_type == "pooling":
            tasks.extend(self.get_supported_pooling_tasks())

        return tuple(tasks)

    def _get_nans_in_logits(
        self,
        logits: Optional[torch.Tensor],
    ) -> dict[str, int]:
        try:
            if logits is None:
                return {req_id: 0 for req_id in self.input_batch.req_ids}

            num_nans_in_logits = {}
            num_nans_for_index = logits.isnan().sum(dim=-1).cpu().numpy()
            for req_id in self.input_batch.req_ids:
                req_index = self.input_batch.req_id_to_index[req_id]
                num_nans_in_logits[req_id] = (int(num_nans_for_index[req_index])
                                              if num_nans_for_index is not None and req_index < logits.shape[0] else 0)
            return num_nans_in_logits
        except IndexError:
            return {}

    def update_config(self, overrides: dict[str, Any]) -> None:
        allowed_config_names = {"load_config", "model_config"}
        for config_name, config_overrides in overrides.items():
            assert config_name in allowed_config_names, \
                f"Config `{config_name}` not supported. " \
                f"Allowed configs: {allowed_config_names}"
            config = getattr(self, config_name)
            new_config = update_config(config, config_overrides)
            setattr(self, config_name, new_config)

    def reload_weights(self) -> None:
        assert getattr(self, "model", None) is not None, \
            "Cannot reload weights before model is loaded."
        model_loader = get_model_loader(self.load_config)
        logger.info("Reloading weights inplace...")
        model_loader.load_weights(self.model, model_config=self.model_config)
        torch.hpu.synchronize()

    def take_draft_token_ids(self) -> Optional[DraftTokenIds]:
        if self._draft_token_ids is None:
            return None
        req_ids = self.input_batch.req_ids
        if isinstance(self._draft_token_ids, torch.Tensor):
            draft_token_ids = self._draft_token_ids.tolist()
        else:
            draft_token_ids = self._draft_token_ids
        self._draft_token_ids = None
        return DraftTokenIds(req_ids, draft_token_ids)

    def propose_draft_token_ids(
        self,
        scheduler_output: "SchedulerOutput",
        sampled_token_ids: list[list[int]],
        prefill_sampled_token_ids_tensor: torch.Tensor,
        decode_sampled_token_ids_tensor: torch.Tensor,
        sampling_metadata: SamplingMetadata,
        hidden_states: torch.Tensor,
        sample_hidden_states: torch.Tensor,
        aux_hidden_states: Optional[torch.Tensor],
        hidden_states_prefills: list[torch.Tensor],
        sample_hidden_states_prefills: list[torch.Tensor],
        aux_hidden_states_prefills: list[Optional[torch.Tensor]],
        num_decodes: int,
        prefill_data: Optional[PrefillInputData] = None,
        decode_data: Optional[DecodeInputData] = None,
    ) -> Union[list[list[int]], torch.Tensor]:
        if self.speculative_config.method == "ngram":
            assert isinstance(self.drafter, NgramProposer)
            draft_token_ids = self.propose_ngram_draft_token_ids(sampled_token_ids)
        elif self.speculative_config.use_eagle():
            assert isinstance(self.drafter, EagleProposer)

            def execute_drafter_model(target_token_ids, target_positions, target_hidden_states, last_token_indices,
                                      common_attn_metadata):
                if self.drafter.method == "eagle3":
                    assert isinstance(self.drafter.model.model, Eagle3LlamaForCausalLM)
                    target_hidden_states = \
                        self.drafter.model.model.combine_hidden_states(
                        target_hidden_states)
                    assert target_hidden_states.shape[-1] == self.hidden_size
                #htorch.core.mark_step()
                ret_hidden_states = self.drafter.model(
                    input_ids=target_token_ids,
                    positions=target_positions,
                    hidden_states=target_hidden_states,
                    inputs_embeds=None,
                    attn_metadata=common_attn_metadata,
                )
                #htorch.core.mark_step()
                if self.drafter.method in ("deepseek_mtp", "ernie_mtp"):
                    last_hidden_states = ret_hidden_states
                    hidden_states = last_hidden_states
                else:
                    last_hidden_states, hidden_states = ret_hidden_states
                last_hidden_states = last_hidden_states.view(-1, last_hidden_states.shape[-1])
                sample_hidden_states = last_hidden_states[last_token_indices]
                logits = self.drafter.model.compute_logits(sample_hidden_states)
                draft_token_ids = logits.argmax(dim=-1)
                return draft_token_ids, hidden_states

            draft_token_ids = None
            if decode_data is not None:
                assert decode_data.spec_decode_metadata is not None
                assert decode_data.position_ids is not None
                num_draft_tokens = \
                    decode_data.spec_decode_metadata.num_draft_tokens
                max_num_draft_tokens = max(num_draft_tokens)
                common_attn_metadata = decode_data.attn_metadata

                num_picked_token_indices = []
                last_token_indices = []
                starting_index = 0
                num_rejected_tokens = [
                    n + 1 - len(sampled_token_ids[i]) if n > 0 else 0 for i, n in enumerate(num_draft_tokens)
                ]
                for i, n in enumerate(num_draft_tokens):
                    r = num_rejected_tokens[i]
                    step = max_num_draft_tokens + 1
                    for j in range(step):
                        if j == n - r:
                            last_token_indices.append(starting_index + j)
                        if j < n + 1 - r:
                            num_picked_token_indices.append(starting_index + j)
                        else:
                            num_picked_token_indices.append(-1)
                    starting_index += step
                hidden_states_indices = torch.tensor(num_picked_token_indices, device=self.device)
                last_token_indices = torch.tensor(last_token_indices, device=self.device)

                target_token_ids = decode_sampled_token_ids_tensor.reshape(-1, 1)[hidden_states_indices]
                target_positions = decode_data.position_ids[hidden_states_indices]

                if self.use_aux_hidden_state_outputs and \
                    aux_hidden_states is not None:
                    target_hidden_states = torch.cat([h[hidden_states_indices] for h in aux_hidden_states], dim=-1)
                else:
                    target_hidden_states = hidden_states[hidden_states_indices]

                if target_hidden_states.dim() == 2:
                    target_hidden_states = target_hidden_states.unsqueeze(1)
                draft_token_ids, hidden_states = execute_drafter_model(target_token_ids, target_positions,
                                                                       target_hidden_states, last_token_indices,
                                                                       common_attn_metadata)

                draft_token_ids = draft_token_ids[:num_decodes]
            # handle prefill
            if prefill_data is not None:
                # Currently, prefill is done one by one
                draft_token_ids_prefill = []
                hidden_states_prefill = []

                for idx, (req_id, prompt_len, token_ids, position_ids, attn_metadata, logits_indices,
                          logits_requests) in enumerate(zip(*shallow_tuple(prefill_data))):
                    hidden_states = hidden_states_prefills[idx]
                    if self.use_aux_hidden_state_outputs:
                        aux_hidden_states = aux_hidden_states_prefills[idx]
                        target_hidden_states = torch.cat(aux_hidden_states, dim=-1)
                    else:
                        target_hidden_states = hidden_states
                    next_token_ids = prefill_sampled_token_ids_tensor[idx]
                    # Follow GPU to shift input_tokens by one to the left
                    # to match hidden_states
                    token_ids = token_ids.squeeze()
                    target_token_ids = token_ids.clone()
                    target_token_ids[:-1].copy_(token_ids[1:])
                    target_token_ids[logits_indices] = next_token_ids
                    target_token_ids = target_token_ids.unsqueeze(0)
                    if target_hidden_states.dim() == 2:
                        target_hidden_states = target_hidden_states.unsqueeze(0)
                    _draft_token_ids, _hidden_states = execute_drafter_model(target_token_ids, position_ids,
                                                                             target_hidden_states, logits_indices,
                                                                             attn_metadata)
                    draft_token_ids_prefill.append(_draft_token_ids)
                    hidden_states_prefill.append(_hidden_states)
                if draft_token_ids is None:
                    draft_token_ids = torch.cat(draft_token_ids_prefill, dim=0)
                    hidden_states = torch.cat(hidden_states_prefill, dim=0)
                else:
                    draft_token_ids = torch.cat([draft_token_ids] + draft_token_ids_prefill, dim=0)
                    hidden_states = torch.cat([hidden_states] + hidden_states_prefill, dim=0)

            # Early exit if there is only one draft token to be generated.
            # [batch_size, 1]

            if self.speculative_config.num_speculative_tokens == 1:
                return draft_token_ids.view(-1, 1)  # type: ignore

        return draft_token_ids

    def propose_ngram_draft_token_ids(
        self,
        sampled_token_ids: list[list[int]],
    ) -> list[list[int]]:
        draft_token_ids = self.drafter.propose(sampled_token_ids, self.input_batch.req_ids,
                                               self.input_batch.num_tokens_no_spec, self.input_batch.token_ids_cpu,
                                               self.input_batch.spec_decode_unsupported_reqs)
        # swipe draft_token_ids_native replacing [] to [-1]
        for i in range(len(draft_token_ids)):
            if len(draft_token_ids[i]) == 0:
                draft_token_ids[i] = [-1]
        return draft_token_ids

_PAD_BLOCK_ID instance-attribute

_PAD_BLOCK_ID = -1

_PAD_SLOT_ID instance-attribute

_PAD_SLOT_ID = -1

_draft_token_ids instance-attribute

_draft_token_ids: Optional[
    Union[list[list[int]], Tensor]
] = None

_is_inc_finalized instance-attribute

_is_inc_finalized = False

_tokenizer instance-attribute

_tokenizer = init_tokenizer_from_configs(
    model_config=model_config
)

arange_np instance-attribute

arange_np = arange(
    max(max_num_reqs + 1, max_model_len, max_num_tokens),
    dtype=int64,
)

async_output_copy_stream instance-attribute

async_output_copy_stream = (
    Stream() if use_async_scheduling else None
)

attn_backend instance-attribute

attn_backend = get_attn_backend(
    head_size,
    dtype,
    kv_cache_dtype_str,
    block_size,
    use_mla=use_mla,
)

batch_changed instance-attribute

batch_changed: bool = False

block_size instance-attribute

block_size = block_size

bucketing_manager instance-attribute

bucketing_manager = HPUBucketingManager()

cache_config instance-attribute

cache_config = cache_config

debug_fwd instance-attribute

debug_fwd = init_debug_logger('fwd')

defragmenter instance-attribute

defragmenter = OnlineDefragmenter()

device instance-attribute

device = device

drafter instance-attribute

drafter = NgramProposer(vllm_config)

dtype instance-attribute

dtype = dtype

enable_bucketing instance-attribute

enable_bucketing = use_bucketing

encoder_cache instance-attribute

encoder_cache: dict[str, Tensor] = {}

get_dp_padding instance-attribute

get_dp_padding = partial(
    get_dp_padding,
    dp_size=data_parallel_size,
    dp_rank=data_parallel_rank,
)

graphed_buckets instance-attribute

graphed_buckets: set[Any] = set()

graphed_multimodal_buckets instance-attribute

graphed_multimodal_buckets: set[Any] = set()

head_size instance-attribute

head_size = get_head_size()

hidden_size instance-attribute

hidden_size = get_hidden_size()

inc_initialized_successfully instance-attribute

inc_initialized_successfully = False

input_batch instance-attribute

input_batch = InputBatch(
    max_num_reqs=max_num_seqs,
    max_model_len=max_model_len,
    max_num_batched_tokens=max_num_tokens,
    device=device,
    pin_memory=pin_memory,
    vocab_size=get_vocab_size(),
    block_sizes=[block_size],
    kernel_block_sizes=[block_size],
    is_spec_decode=bool(speculative_config),
    logitsprocs=build_logitsprocs(
        vllm_config,
        device,
        pin_memory,
        is_pooling_model,
        logits_processors,
    ),
)

input_ids_cpu instance-attribute

input_ids_cpu = zeros(
    max_num_tokens,
    dtype=int32,
    device="cpu",
    pin_memory=pin_memory,
)

input_ids_hpu instance-attribute

input_ids_hpu = (
    zeros(
        max_num_tokens,
        dtype=int32,
        device=device,
        pin_memory=pin_memory,
    )
    if use_async_scheduling
    else None
)

interleaved_sliding_window instance-attribute

interleaved_sliding_window = is_interleaved(hf_text_config)

is_driver_worker instance-attribute

is_driver_worker = is_driver_worker

is_mm_embed instance-attribute

is_mm_embed = _make_buffer(max_num_tokens, dtype=bool)

is_multimodal_raw_input_supported instance-attribute

is_multimodal_raw_input_supported = (
    is_multimodal_raw_input_only_model
)

is_pooling_model instance-attribute

is_pooling_model = pooler_config is not None

kv_cache_dtype instance-attribute

kv_cache_dtype = dtype

kv_cache_dtype_str instance-attribute

kv_cache_dtype_str = HPU_TORCH_DTYPE_TO_STR_DTYPE[
    kv_cache_dtype
]

kv_caches instance-attribute

kv_caches: list[Tensor] = []

load_config instance-attribute

load_config = load_config

logits_rounding instance-attribute

logits_rounding = 1

lora_config instance-attribute

lora_config = lora_config

max_batch_size instance-attribute

max_batch_size = max_num_seqs

max_cudagraph_capture_size instance-attribute

max_cudagraph_capture_size = max_cudagraph_capture_size

max_model_len instance-attribute

max_model_len = max_model_len

max_num_batched_tokens instance-attribute

max_num_batched_tokens = max_num_batched_tokens

max_num_blocks_per_req instance-attribute

max_num_blocks_per_req = cdiv(max_model_len, block_size)

max_num_reqs instance-attribute

max_num_reqs = max_num_seqs

max_num_seqs instance-attribute

max_num_seqs = max_num_seqs

max_num_tokens instance-attribute

max_num_tokens = max_num_batched_tokens

max_prefill_batch_size instance-attribute

max_prefill_batch_size = prompt_profile_cfg[0]

mem_margin instance-attribute

mem_margin = None

mm_registry instance-attribute

mm_registry = MULTIMODAL_REGISTRY

model_config instance-attribute

model_config = model_config

model_has_chunked_attention instance-attribute

model_has_chunked_attention = False

num_attn_layers instance-attribute

num_attn_layers = get_num_layers_by_block_type(
    parallel_config, "attention"
)

num_kv_heads instance-attribute

num_kv_heads = get_num_kv_heads(parallel_config)

num_query_heads instance-attribute

num_query_heads = get_num_attention_heads(parallel_config)

observability_config instance-attribute

observability_config = observability_config

parallel_config instance-attribute

parallel_config = parallel_config

pin_memory instance-attribute

pin_memory = is_pin_memory_available()

positions_cpu instance-attribute

positions_cpu = zeros(
    max_num_tokens,
    dtype=int64,
    device="cpu",
    pin_memory=pin_memory,
)

positions_np instance-attribute

positions_np = numpy()

profiler instance-attribute

profiler_counter_helper instance-attribute

profiler_counter_helper = HabanaProfilerCounterHelper()

rejection_sampler instance-attribute

rejection_sampler = RejectionSampler(sampler)

requests instance-attribute

requests: dict[str, CachedRequestState] = {}

sampler instance-attribute

sampler = Sampler()

scheduler_config instance-attribute

scheduler_config = scheduler_config

scheduler_output instance-attribute

scheduler_output: SchedulerOutput | None = None

seen_configs instance-attribute

seen_configs: set = set()

skip_warmup instance-attribute

skip_warmup = skip_warmup

sliding_window instance-attribute

sliding_window = get_sliding_window()

speculative_config instance-attribute

speculative_config = speculative_config

supports_mm_inputs instance-attribute

supports_mm_inputs = supports_multimodal_inputs(
    model_config
)

unified_attn instance-attribute

unified_attn = unified_attn

use_async_scheduling instance-attribute

use_async_scheduling = async_scheduling

use_aux_hidden_state_outputs instance-attribute

use_aux_hidden_state_outputs = False

use_contiguous_pa instance-attribute

use_contiguous_pa = use_contiguous_pa

use_hpu_graph instance-attribute

use_hpu_graph = not enforce_eager

use_merged_prefill instance-attribute

use_merged_prefill = merged_prefill

use_prefix_caching instance-attribute

use_prefix_caching = enable_prefix_caching

uses_mrope instance-attribute

uses_mrope = uses_mrope

vllm_config instance-attribute

vllm_config = vllm_config

warmup_mode instance-attribute

warmup_mode: bool = False

__del__

__del__()
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def __del__(self):
    self.shutdown_inc()

__init__

__init__(
    vllm_config: VllmConfig,
    device: device = "hpu",
    is_driver_worker: bool = False,
)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def __init__(
    self,
    vllm_config: VllmConfig,
    device: torch.device = 'hpu',
    is_driver_worker: bool = False,
):
    # TODO: use ModelRunnerBase.__init__(self, vllm_config=vllm_config)
    environment.set_vllm_config(vllm_config)
    finalize_config()
    self.vllm_config = vllm_config
    self.model_config = vllm_config.model_config
    self.cache_config = vllm_config.cache_config
    self.lora_config = vllm_config.lora_config
    self.load_config = vllm_config.load_config
    self.parallel_config = vllm_config.parallel_config
    self.scheduler_config = vllm_config.scheduler_config
    self.speculative_config = vllm_config.speculative_config
    self.observability_config = vllm_config.observability_config
    self.is_driver_worker = is_driver_worker
    self.use_aux_hidden_state_outputs = False
    self.supports_mm_inputs = False

    self.sampler = Sampler()

    # NOTE(kzawora) update_env is a hack to work around VLLMKVCache in
    # hpu-extension which selects fetch_from_cache implementation based
    # on env vars... this should be fixed in the future
    self.enable_bucketing = get_config().use_bucketing
    self.use_contiguous_pa = get_config().use_contiguous_pa
    self.skip_warmup = get_config().skip_warmup

    model_config = self.model_config
    cache_config = self.cache_config
    scheduler_config = self.scheduler_config
    self.device = device
    self.pin_memory = is_pin_memory_available()
    self.dtype = self.model_config.dtype
    if cache_config.cache_dtype == "auto":
        self.kv_cache_dtype = self.dtype
    else:
        self.kv_cache_dtype = STR_DTYPE_TO_TORCH_DTYPE[cache_config.cache_dtype]
    self.kv_cache_dtype_str = HPU_TORCH_DTYPE_TO_STR_DTYPE[self.kv_cache_dtype]
    self.is_pooling_model = model_config.pooler_config is not None

    self.sliding_window = model_config.get_sliding_window()
    self.interleaved_sliding_window = is_interleaved(vllm_config.model_config.hf_text_config)
    self.block_size = cache_config.block_size
    self.max_model_len = model_config.max_model_len
    self.max_num_blocks_per_req = cdiv(self.max_model_len, self.block_size)
    # Override settings when profiling a single prefill/decode
    # We can do such barbaric changes because we close vllm after the profiling
    prompt_profile_cfg, decode_profile_cfg = self._read_profiling_cfg()
    if prompt_profile_cfg or decode_profile_cfg:
        self.scheduler_config.max_num_seqs = self.max_model_len
        if prompt_profile_cfg:
            self.scheduler_config.max_num_batched_tokens = prompt_profile_cfg[0] * prompt_profile_cfg[1]
    self.max_num_tokens = scheduler_config.max_num_batched_tokens
    # Cached outputs.
    ## universal buffer for input_ids and positions ##
    ## necessary being used by spec decode by following GPU impl ##
    self._draft_token_ids: Optional[Union[list[list[int]], torch.Tensor]] = None
    self.input_ids_cpu = torch.zeros(self.max_num_tokens,
                                     dtype=torch.int32,
                                     device="cpu",
                                     pin_memory=self.pin_memory)
    self.positions_cpu = torch.zeros(self.max_num_tokens,
                                     dtype=torch.int64,
                                     device="cpu",
                                     pin_memory=self.pin_memory)
    self.positions_np = self.positions_cpu.numpy()
    ###############################################################

    # Model-related.
    self.num_attn_layers = self.model_config.get_num_layers_by_block_type(self.parallel_config, "attention")
    self.num_query_heads = self.model_config.get_num_attention_heads(self.parallel_config)
    self.num_kv_heads = self.model_config.get_num_kv_heads(self.parallel_config)
    self.head_size = self.model_config.get_head_size()
    self.hidden_size = self.model_config.get_hidden_size()
    self.is_pooling_model = model_config.pooler_config is not None
    logger.debug("model config: ", self.model_config)

    self.attn_backend = get_attn_backend(
        self.head_size,
        self.dtype,
        self.kv_cache_dtype_str,
        self.block_size,
        use_mla=self.model_config.use_mla,
    )

    # Mult-modal-related.
    self.mm_registry = MULTIMODAL_REGISTRY
    self.uses_mrope = model_config.uses_mrope
    self.supports_mm_inputs = self.mm_registry.supports_multimodal_inputs(model_config)
    if self.supports_mm_inputs:
        self.is_mm_embed = self._make_buffer(self.max_num_tokens, dtype=torch.bool)
    self.is_multimodal_raw_input_supported = (model_config.is_multimodal_raw_input_only_model)

    # Lazy initialization
    # self.model: nn.Module  # set after load_model
    self.kv_caches: list[torch.Tensor] = []
    self.inc_initialized_successfully = False
    self._is_inc_finalized = False

    # mm_hash -> encoder_output
    self.encoder_cache: dict[str, torch.Tensor] = {}
    # Set up speculative decoding.
    # NOTE(Chendi): Speculative decoding is only enabled for the last rank
    # in the pipeline parallel group.
    if self.speculative_config:
        if self.speculative_config.num_speculative_tokens > 1:
            raise NotImplementedError("Speculative decoding with num_speculative_tokens > 1 is "
                                      "not supported on HPU.")
        if self.speculative_config.method == "ngram":
            self.drafter = NgramProposer(self.vllm_config)
        elif self.speculative_config.use_eagle():
            if self.speculative_config.num_speculative_tokens > 1:
                logger.warning("EagleProposer only supports num_speculative_tokens=1. "
                               "Overriding the config.")
                self.speculative_config.num_speculative_tokens = 1
            self.drafter = EagleProposer(self.vllm_config, self.device, self)  # type: ignore
            if self.speculative_config.method == "eagle3":
                self.use_aux_hidden_state_outputs = True
        elif self.speculative_config.method == "medusa":
            raise NotImplementedError("Medusa speculative decoding is not supported on HPU.")
        else:
            raise ValueError("Unknown speculative decoding method: "
                             f"{self.speculative_config.method}")
        self.rejection_sampler = RejectionSampler(self.sampler)

    # Keep in int64 to avoid overflow with long context
    self.max_num_reqs = self.scheduler_config.max_num_seqs

    # Keep in int64 to avoid overflow with long context
    self.arange_np = np.arange(max(self.max_num_reqs + 1, self.max_model_len, self.max_num_tokens), dtype=np.int64)

    # Request states.
    self.requests: dict[str, CachedRequestState] = {}
    # Persistent batch.
    self.input_batch = InputBatch(
        max_num_reqs=self.scheduler_config.max_num_seqs,
        max_model_len=self.max_model_len,
        max_num_batched_tokens=self.max_num_tokens,
        device=self.device,
        pin_memory=self.pin_memory,
        vocab_size=self.model_config.get_vocab_size(),
        block_sizes=[self.block_size],
        kernel_block_sizes=[self.block_size],
        is_spec_decode=bool(self.vllm_config.speculative_config),
        logitsprocs=build_logitsprocs(self.vllm_config, self.device, self.pin_memory, self.is_pooling_model,
                                      self.vllm_config.model_config.logits_processors),
    )

    self.use_async_scheduling = self.scheduler_config.async_scheduling
    # Cache token ids on device to avoid h2d copies
    self.input_ids_hpu = torch.zeros(
        self.max_num_tokens, dtype=torch.int32, device=self.device,
        pin_memory=self.pin_memory) if self.use_async_scheduling else None
    self.async_output_copy_stream = torch.hpu.Stream() if \
        self.use_async_scheduling else None
    assert not (self.use_async_scheduling and (self.speculative_config is not None)), \
        "Speculative decoding is not supported with async scheduling."
    self.mem_margin = None
    self.unified_attn = get_config().unified_attn
    self.use_merged_prefill = get_config().merged_prefill

    self.use_hpu_graph = not self.model_config.enforce_eager
    self.max_batch_size = self.scheduler_config.max_num_seqs
    self.max_num_seqs = self.scheduler_config.max_num_seqs
    self.max_cudagraph_capture_size = self.vllm_config.compilation_config.max_cudagraph_capture_size
    if prompt_profile_cfg:
        self.max_prefill_batch_size = prompt_profile_cfg[0]
    else:
        self.max_prefill_batch_size = with_default(get_config().VLLM_PROMPT_BS_BUCKET_MAX, 1)
    self.seen_configs: set = set()
    self.max_num_batched_tokens = \
        self.scheduler_config.max_num_batched_tokens
    self.use_prefix_caching = (self.vllm_config.cache_config.enable_prefix_caching)
    self.bucketing_manager = HPUBucketingManager()
    max_num_prefill_seqs = self.max_num_seqs if self.use_merged_prefill \
                           else self.max_prefill_batch_size
    if self.enable_bucketing:
        logger.info("Bucketing is ON.")
        self.bucketing_manager.initialize(max_num_seqs=self.max_num_seqs,
                                          max_num_prefill_seqs=max_num_prefill_seqs,
                                          block_size=self.block_size,
                                          max_num_batched_tokens=self.max_num_batched_tokens,
                                          max_model_len=self.max_model_len)
        self.graphed_buckets: set[Any] = set()
        self.graphed_multimodal_buckets: set[Any] = set()
    else:
        logger.info("Bucketing is OFF.")
    self._PAD_SLOT_ID = -1
    self._PAD_BLOCK_ID = -1
    self._tokenizer = init_tokenizer_from_configs(model_config=vllm_config.model_config)

    if self.vllm_config.parallel_config.data_parallel_size > 1 and htorch.utils.internal.is_lazy(
    ) and not self.model_config.enforce_eager:
        from vllm import envs
        # disable device group for dp synchronization when hpu graph is
        # turned on since it's not captured and causes issues
        envs.VLLM_DISABLE_NCCL_FOR_DP_SYNCHRONIZATION = True

    self.logits_rounding = 1
    # High-level profiler
    self.profiler = HabanaHighLevelProfiler()
    self.profiler_counter_helper = HabanaProfilerCounterHelper()

    self.defragmenter = OnlineDefragmenter()
    self.debug_fwd = init_debug_logger('fwd')

    self.get_dp_padding = partial(get_dp_padding,
                                  dp_size=self.parallel_config.data_parallel_size,
                                  dp_rank=self.parallel_config.data_parallel_rank)

    self.scheduler_output: SchedulerOutput | None = None
    self.warmup_mode: bool = False
    self.batch_changed: bool = False
    # WA for chunked attention support
    self.model_has_chunked_attention = False

    assert not (self.unified_attn and not self.use_contiguous_pa), 'Unified attn requires contiguous_pa!'
    assert not (self.unified_attn and not self.use_merged_prefill), 'Unified attn requires merged_prefill!'

_add_dummy_request

_add_dummy_request(
    requests,
    num_scheduled_tokens,
    num_computed_tokens,
    total_tokens,
    scheduled_tokens,
    is_prompt,
    block_id=0,
)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _add_dummy_request(self,
                       requests,
                       num_scheduled_tokens,
                       num_computed_tokens,
                       total_tokens,
                       scheduled_tokens,
                       is_prompt,
                       block_id=0):
    num_blocks = round_up(total_tokens, self.block_size) // self.block_size
    prompt_token_ids = list(range(total_tokens))

    req_id = f'{len(requests)}'
    block_ids = [block_id] * num_blocks
    sampling_params = SamplingParams(temperature=0.0)

    req = NewRequestData(
        req_id=req_id,
        prompt_token_ids=prompt_token_ids,
        mm_features=[],
        sampling_params=sampling_params,
        pooling_params=None,
        block_ids=[block_ids],
        num_computed_tokens=num_computed_tokens,
        lora_request=None,
    )
    requests.append(req)
    if is_prompt:
        num_scheduled_tokens[req_id] = len(prompt_token_ids) - num_computed_tokens
    else:
        num_scheduled_tokens[req_id] = scheduled_tokens

_add_dummy_unified_request

_add_dummy_unified_request(
    requests,
    is_prompt,
    is_unique,
    block_num,
    num_computed_tokens,
    num_scheduled_tokens,
    scheduled_tokens,
)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _add_dummy_unified_request(self, requests, is_prompt, is_unique, block_num, num_computed_tokens,
                               num_scheduled_tokens, scheduled_tokens):
    from vllm.v1.core.sched.output import NewRequestData

    req_id = f'{len(requests)}'
    sampling_params = SamplingParams(temperature=0.0)
    num_computed_tokens = max(len(block_num) * self.block_size - 1, 0)

    if is_prompt:
        # num_computed_tokens + num_scheduled_tokens <= prompt token ids
        prompt_token_ids = num_computed_tokens + num_scheduled_tokens + 1
    else:
        # num_computed_tokens + num_scheduled_tokens > prompt_token_ids
        prompt_token_ids = num_computed_tokens + num_scheduled_tokens - 1
    prompt_token_ids = list(range(prompt_token_ids))

    req = NewRequestData(
        req_id=req_id,
        prompt_token_ids=prompt_token_ids,
        mm_features=[],
        sampling_params=sampling_params,
        pooling_params=None,
        block_ids=[block_num],
        num_computed_tokens=num_computed_tokens,
        lora_request=None,
    )

    requests.append(req)
    scheduled_tokens[req_id] = num_scheduled_tokens

_align_and_pad_mrope_positions

_align_and_pad_mrope_positions(
    req_ids: list[str],
    context_lens: list[int],
    query_lens: list[int],
    bucketing: tuple[int, int],
    padding_gen: int,
) -> Tensor
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _align_and_pad_mrope_positions(self, req_ids: list[str], context_lens: list[int], query_lens: list[int],
                                   bucketing: tuple[int, int], padding_gen: int) -> torch.Tensor:
    target_bs, target_len = bucketing
    out_shape = (3, target_len) if target_bs == 1 \
        else (target_bs, target_len)

    mrope_position_tensor = torch.full(out_shape, padding_gen, dtype=torch.int32, device='cpu')
    dst_start = 0
    dst_end = dst_start
    for b_idx, req_id in enumerate(req_ids):
        cl = context_lens[b_idx]
        qsl = query_lens[b_idx]
        assert self.requests[req_id].mrope_positions is not None
        input_mrope_position = \
            self.requests[req_id].mrope_positions[:, cl:cl + qsl] # type: ignore[index]
        dst_end = dst_start + qsl
        mrope_position_tensor[:, dst_start:dst_end].copy_(input_mrope_position, non_blocking=True)

        # Update dst_start depending on if pos_ids of requests are meant to be adjacent # noqa 501
        if target_bs == 1:
            dst_start = dst_end
        else:
            dst_start += target_len
    return mrope_position_tensor

_bucketize_2d_prompt

_bucketize_2d_prompt(seq_lens, num_blocks)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _bucketize_2d_prompt(self, seq_lens, num_blocks):
    bs = len(seq_lens)
    if bs > self.max_prefill_batch_size:
        raise BucketingFailedException
    seq = max(seq_lens)
    num_blocks = max(num_blocks) if len(num_blocks) > 0 else 0
    bs, seq, num_blocks = self.bucketing_manager.find_prompt_bucket(bs, seq, num_blocks)
    return (bs, seq, num_blocks)

_bucketize_merged_prompt

_bucketize_merged_prompt(seq_lens, num_blocks)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _bucketize_merged_prompt(self, seq_lens, num_blocks):
    seq = sum(seq_lens)
    num_blocks = sum(num_blocks)
    seq = self.bucketing_manager.find_prompt_bucket(1, seq, num_blocks)[1]
    num_blocks = round_up(num_blocks, 32)
    return (1, seq, num_blocks)

_can_merge_prefill_contents

_can_merge_prefill_contents(lhs, rhs)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _can_merge_prefill_contents(self, lhs, rhs):
    combined_num_tokens = lhs.get_num_tokens() + rhs.get_num_tokens()
    bucketing_fn = self._get_prompt_bucketing_fn()
    try:
        target_bs, target_seq, target_blocks = bucketing_fn(combined_num_tokens, [])
    except BucketingFailedException:
        return False
    target_bs, target_seq, target_blocks = bucketing_fn(combined_num_tokens, [])
    return target_bs <= self.max_prefill_batch_size and\
        target_bs * target_seq <= self.max_num_tokens

_check_config

_check_config(
    batch_size,
    seq_len,
    num_blocks,
    attn_metadata,
    warmup_mode,
)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _check_config(self, batch_size, seq_len, num_blocks, attn_metadata, warmup_mode):
    cfg: tuple[Any, ...] | None = None
    if self.unified_attn:
        phase = "prompt" if attn_metadata.is_prompt else "decode"
        shared = attn_metadata.shared_blocks.size(0) if attn_metadata.shared_blocks is not None else 0
        unique = attn_metadata.unique_blocks if attn_metadata.unique_blocks else 0
        is_causal = 1 if attn_metadata.causal_bias is not None else 0
        cfg = (seq_len, shared, unique)
        seen = cfg in self.seen_configs
        self.seen_configs.add(cfg)
        if not seen and not warmup_mode:
            logger.warning("Configuration: (query, shared_blocks, unique_blocks) %s, (%s) was not warmed-up!", \
                           cfg, 'causal' if is_causal else 'not causal')
    else:
        phase = "prompt" if attn_metadata.is_prompt else "decode"
        cfg = (phase, batch_size, seq_len, num_blocks)
        if self.debug_fwd:
            self.debug_fwd(cfg)
        seen = cfg in self.seen_configs
        self.seen_configs.add(cfg)
        if not seen and not warmup_mode:
            logger.warning("Configuration: %s was not warmed-up!", cfg)

_check_unified_config

_check_unified_config(
    attn_metadata, logits_indices, warmup_mode
)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _check_unified_config(self, attn_metadata, logits_indices, warmup_mode):
    cfg = self._get_unified_config(attn_metadata, logits_indices)
    if self.debug_fwd:
        self.debug_fwd(cfg)
    seen = cfg in self.seen_configs
    self.seen_configs.add(cfg)
    if not seen and not warmup_mode:
        logger.warning("Configuration: %s was not warmed-up!", cfg)

_compile

_compile(module)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _compile(self, module):
    return torch.compile(module, **self.compile_config.get_compile_args())

_compile_methods

_compile_methods()

Compile methods which are not part of the compiled model i.e. those which will not be compiled during model's compilation.

Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _compile_methods(self):
    """
    Compile methods which are not part of the compiled model i.e. those
    which will not be compiled during model's compilation.
    """
    compiled_methods = ['_update_metadata', '_rotary_prepare_cos_sin']
    for method_name in compiled_methods:
        method = getattr(self.model, method_name, None)
        if method is not None:
            self._compile_region(self.model, method_name, method)

_compile_region

_compile_region(model, name, module)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _compile_region(self, model, name, module):
    module = self._compile(module)
    setattr(model, name, module)

_configure_lora

_configure_lora(input, requests, req_ids, is_prompt)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _configure_lora(self, input, requests, req_ids, is_prompt):
    lora_mask = None
    lora_logits_mask = None
    if self.lora_config:
        if is_prompt:
            lora_requests = [] if req_ids else requests
            lora_ids = []
            lora_index_mapping = []
            lora_prompt_mapping = []
            for i, r_id in enumerate(req_ids):
                lora_requests.append(requests[r_id].lora_request)
            for lora_req in lora_requests:
                lora_id = lora_req.lora_int_id if lora_req else 0
                lora_index_mapping += [lora_id] * (input.shape[1])
                #TODO: This may need to change when logprobs
                # sampling is enabled
                lora_prompt_mapping += [lora_id]
                lora_ids.append(lora_id)
        else:
            lora_requests = []
            # lora_ids, lora_index_mapping, lora_prompt_mapping
            # filled with 0 (indicating no lora) to account for
            # any padding
            lora_ids = [0] * input.shape[0]
            lora_index_mapping = [0] * input.shape[0]
            lora_prompt_mapping = [0] * input.shape[0]
            for i, r_id in enumerate(req_ids):
                lora_requests.append(requests[r_id].lora_request)

            for i, lora_req in enumerate(lora_requests):
                lora_id = lora_req.lora_int_id if lora_req else 0
                lora_index_mapping[i] = lora_id
                lora_prompt_mapping[i] = lora_id
                lora_ids[i] = lora_id

        # is_prefill should always be "False" for HPU
        lora_mapping = LoRAMapping(lora_index_mapping, lora_prompt_mapping, is_prefill=False)
        self.set_active_loras(lora_requests, lora_mapping)
        lora_mask, lora_logits_mask = self.create_lora_mask(input, lora_ids, is_prompt)

    return lora_mask, lora_logits_mask

_create_decode_input_data

_create_decode_input_data(
    num_decodes,
    num_scheduled_tokens,
    context_lens,
    block_table_cpu_tensor,
    scheduler_output=None,
) -> DecodeInputData
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _create_decode_input_data(self,
                              num_decodes,
                              num_scheduled_tokens,
                              context_lens,
                              block_table_cpu_tensor,
                              scheduler_output=None) -> DecodeInputData:
    # NOTE(kzawora): the +1 is what causes this entire thing to work,
    # as in the paged attention, we don't fetch just the context from cache,
    # but also kvs for the current token
    num_blocks = np.ceil((context_lens + 1) / self.block_size).astype(np.int32).tolist()

    # PAD FOR STATIC SHAPES.
    padded_batch_size: int
    padded_batch_size = self.bucketing_manager.find_decode_bucket(num_decodes, sum(num_blocks))[0]

    # dp aware padding
    padded_batch_size += self.get_dp_padding(padded_batch_size)

    num_tokens_per_req = num_scheduled_tokens[:num_decodes]
    num_tokens = max(num_tokens_per_req)
    total_num_scheduled_tokens = sum(num_tokens_per_req)
    num_tokens_per_req = num_tokens_per_req + [0] * (padded_batch_size - num_decodes)

    block_tables_list = []
    for i, n in enumerate(num_blocks):
        seq_block_table = block_table_cpu_tensor[i, :n].tolist()
        assert len(seq_block_table) == n
        block_tables_list.extend([seq_block_table] * num_tokens)

    ###################################
    # initialize positions with padding
    # POSITIONS. [batch, num_tokens]
    # NOTE(Chendi): Follow GPU_Model_Runner to use global
    # self.positions_cpu, which updated in prepare_inputs from
    # self.input_batch.num_computed_tokens_cpu[req_indices]
    positions = torch.zeros((padded_batch_size, num_tokens), dtype=torch.int32)
    if num_tokens == 1:
        positions[:num_decodes] = self.positions_cpu[:num_decodes].view(-1, 1)
    else:
        # per request using universal self.positions_cpu then pad
        position_split_tensors = torch.split(self.positions_cpu[:total_num_scheduled_tokens], num_tokens_per_req)
        positions[:num_decodes] = \
            pad_sequence(list(position_split_tensors),
                            batch_first=True,
                            padding_value=0)[:num_decodes]

    padded_index = torch.zeros((padded_batch_size, num_tokens), dtype=torch.int64)
    index = positions.to(torch.int64)[:num_decodes]
    padded_index[:num_decodes] = index

    input_mrope_positions_list: list[list[int]] = [[] for _ in range(3)]
    if self.uses_mrope:
        for idx, req_id in enumerate(self.input_batch.req_ids[:num_decodes]):
            seq_data = self.requests[req_id]
            context_len = context_lens[idx]
            position = context_len
            if seq_data.mrope_position_delta is not None:
                seq_data.mrope_position_delta = int(seq_data.mrope_position_delta)
                pos_for_mrope = MRotaryEmbedding \
                    .get_next_input_positions(
                        seq_data.mrope_position_delta,
                        context_len=context_len,
                        seq_len=context_len + 1)
            else:
                pos_for_mrope = [[position]] * 3
            for idx in range(3):
                input_mrope_positions_list[idx].extend(pos_for_mrope[idx])

        positions = torch.tensor(input_mrope_positions_list, dtype=torch.int32, device='cpu')

        # Pad the right side of input_mrope_positions by padded_batch_size
        pad_size = padded_batch_size - positions.size(1)
        if pad_size > 0:
            positions = F.pad(positions, (0, pad_size), value=-1, mode='constant')

    ###################################
    # initialize token_ids with padding
    # TOKEN_IDS. [batch, num_tokens]
    # NOTE(Chendi): Follow GPU_Model_Runner to use global
    # self.input_ids_cpu, which updated in prepare_inputs from
    # self.input_batch.token_ids_cpu[:total_num_scheduled_tokens]
    token_ids = torch.zeros((padded_batch_size, num_tokens), dtype=torch.int32)
    if num_tokens == 1:
        token_ids[:num_decodes] = self.input_ids_cpu[:num_decodes].view(-1, 1)
    else:
        token_ids_split_tensors = torch.split(self.input_ids_cpu[:total_num_scheduled_tokens], num_tokens_per_req)
        token_ids[:num_decodes] = \
            pad_sequence(list(token_ids_split_tensors),
                            batch_first=True,
                            padding_value=0)[:num_decodes]

    ###################################
    # SLOT_MAPPING [batch, 1]
    # The "slot" is the "physical index" of a token in the KV cache.
    # Look up the block_idx in the block table (logical<>physical map)
    # to compute this.
    block_number = torch.ones((padded_batch_size, num_tokens), dtype=torch.int32) * self._PAD_BLOCK_ID
    block_number[:num_decodes] = torch.gather(input=block_table_cpu_tensor, dim=1, index=(index // self.block_size))
    block_number.apply_(self.defragmenter.resolve)

    block_offsets = padded_index % self.block_size
    slot_mapping = block_number * self.block_size + block_offsets
    # set an out of range value for the padding tokens so that they
    # are ignored when inserting into the KV cache.
    slot_mapping = slot_mapping[:padded_batch_size]
    dummy_slots = itertools.cycle(range(self._PAD_SLOT_ID, self._PAD_SLOT_ID + self.block_size))
    slot_mapping[num_decodes:].apply_(lambda _, ds=dummy_slots: next(ds))

    #####################################
    # NOTE(Chendi): Since we can't actually do num_tokens = 2,
    # convert to [batch_size * num_tokens, 1]
    if num_tokens > 1:
        token_ids = token_ids.view(-1, 1)
        positions = padded_index.view(-1, 1)
        slot_mapping = slot_mapping.view(-1, 1)

    logits_indices = torch.zeros(padded_batch_size, dtype=torch.int32, device='cpu')

    # NOTE(Chendi): num_tokens might be > 1 in spec decode case,
    # example:
    # num_scheduled_tokens = [2, 1, 2, 1]
    # padded tokens_id = \
    #     [[tok_0, tok_1], [tok_2, pad], [tok_4, tok_4], [tok_6, pad]]
    # num_tokens = 2
    # query_start_loc_list = [2, 3, 6, 7]
    # query_start_loc_cpu = [0, 2, 3, 6, 7]
    # logits_indices = [1, 2, 5, 6] => the last token of each request
    query_start_loc_list = [i * num_tokens + n for i, n in enumerate(num_scheduled_tokens[:num_decodes])]
    query_start_loc_cpu = torch.empty((padded_batch_size + 1, ),
                                      dtype=torch.int32,
                                      device="cpu",
                                      pin_memory=self.pin_memory)
    query_start_loc_np = query_start_loc_cpu.numpy()
    query_start_loc_np[0] = 0
    query_start_loc_np[1:num_decodes + 1] = np.array(query_start_loc_list)

    logits_indices[:num_decodes] = query_start_loc_cpu[1:num_decodes + 1] - 1

    positions_device = async_h2d_copy(positions, device=self.device)
    block_tables_list = self.defragmenter.resolve_all(block_tables_list)

    # CONTEXT_LENS [batch_size]
    block_list, block_groups, block_usage = \
        self.get_habana_paged_attn_buffers(
            block_tables_list,
            slot_mapping.tolist(),
            padded_batch_size * num_tokens
        )

    if self.interleaved_sliding_window and self.sliding_window is not None and self.sliding_window > 0:
        sliding_block_size = (self.sliding_window // self.block_size)
        window_block_tables = [block_table[-sliding_block_size:] for block_table in block_tables_list]
        window_block_list, window_block_groups, window_block_usage = \
            self.get_habana_paged_attn_buffers(
                window_block_tables, slot_mapping.tolist(),
                padded_batch_size * num_tokens)

    if self.model_has_chunked_attention:
        chunk_size = (self.model.model.config.text_config.attention_chunk_size // self.block_size)
        seq_lens_block = [len(block_table) for block_table in block_tables_list]
        num_seq_chunks = [math.ceil(sl / chunk_size) - 1 for sl in seq_lens_block]
        block_tables_chunk = [
            block_table[num_seq_chunks[i] * chunk_size:] for i, block_table in enumerate(block_tables_list)
        ]
        chunked_block_list, chunked_block_groups, chunked_block_usage = \
            self.get_habana_paged_attn_buffers(
                block_tables_chunk, slot_mapping.tolist(),
                padded_batch_size * num_tokens)

    # CPU<>HPU sync *should not* happen here.
    block_list_device = async_h2d_copy(block_list, device=self.device)
    block_usage_device = async_h2d_copy(block_usage, device=self.device)
    block_groups_device = async_h2d_copy(block_groups, device=self.device)
    slot_mapping_device = async_h2d_copy(slot_mapping, device=self.device)
    window_block_list_device = async_h2d_copy(
        window_block_list,
        device=self.device) if self.interleaved_sliding_window and self.sliding_window is not None else None
    window_block_usage_device = async_h2d_copy(
        window_block_usage,
        device=self.device) if self.interleaved_sliding_window and self.sliding_window is not None else None
    window_block_groups_device = async_h2d_copy(
        window_block_groups,
        device=self.device) if self.interleaved_sliding_window and self.sliding_window is not None else None
    chunked_block_list_device = async_h2d_copy(chunked_block_list,
                                               device=self.device) if self.model_has_chunked_attention else None
    chunked_block_usage_device = async_h2d_copy(chunked_block_usage,
                                                device=self.device) if self.model_has_chunked_attention else None
    chunked_block_groups_device = async_h2d_copy(chunked_block_groups,
                                                 device=self.device) if self.model_has_chunked_attention else None

    token_ids_device = async_h2d_copy(token_ids, device=self.device)
    # when DP also enabled, some DP ranks will exeucte dummy run with empty
    # SchedulerOutput, in this case we need skip the prepare_input_ids
    if self.use_async_scheduling and scheduler_output is not None:
        self._prepare_input_ids(scheduler_output)
        if num_tokens == 1:
            token_ids_device[:num_decodes] = self.input_ids_hpu[:num_decodes].view(-1, 1)
        else:
            token_ids_split_tensors = torch.split(self.input_ids_hpu[:total_num_scheduled_tokens],
                                                  num_tokens_per_req)
            token_ids_device[:num_decodes] = \
                pad_sequence(list(token_ids_split_tensors),
                                batch_first=True,
                                padding_value=0)[:num_decodes]

        #####################################
        # NOTE(Chendi): Since we can't actually do num_tokens = 2,
        # convert to [batch_size * num_tokens, 1]
        if num_tokens > 1:
            token_ids_device = token_ids_device.view(-1, 1)

    # call prepare_spec_decode_inputs to get the logits indices and
    if scheduler_output is not None:
        logits_indices, spec_decode_metadata = self._prepare_spec_decode_inputs(scheduler_output, logits_indices,
                                                                                token_ids_device, num_tokens)
    else:
        spec_decode_metadata = None
    logits_indices_device = async_h2d_copy(logits_indices, device=self.device)

    attn_metadata = HPUAttentionMetadataV1.make_decode_metadata(
        block_list=block_list_device,
        block_usage=block_usage_device,
        block_groups=block_groups_device,
        input_positions=None,
        slot_mapping=slot_mapping_device,
        block_size=self.block_size,
        window_block_list=window_block_list_device,
        window_block_usage=window_block_usage_device,
        window_block_groups=window_block_groups_device,
        chunked_block_list=chunked_block_list_device,
        chunked_block_usage=chunked_block_usage_device,
        chunked_block_groups=chunked_block_groups_device,
    )

    return DecodeInputData(num_decodes=num_decodes,
                           token_ids=token_ids_device,
                           position_ids=positions_device,
                           logits_indices=logits_indices_device,
                           attn_metadata=attn_metadata,
                           spec_decode_metadata=spec_decode_metadata)

_create_dummy_decode_input_data

_create_dummy_decode_input_data() -> DecodeInputData
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _create_dummy_decode_input_data(self) -> DecodeInputData:
    # create dummy decode input data with batch size 1
    num_dummy_decodes = 1
    num_dummy_scheduled_tokens = [1]
    context_lens = np.array([128])
    block_table_cpu_tensor = torch.zeros([self._PAD_BLOCK_ID], dtype=torch.int32).reshape(1, -1)
    return self._create_decode_input_data(num_dummy_decodes, num_dummy_scheduled_tokens, context_lens,
                                          block_table_cpu_tensor)

_create_dummy_prefill_batch_contents

_create_dummy_prefill_batch_contents(
    num_prefills: int,
) -> list[PrefillInputData]
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _create_dummy_prefill_batch_contents(self, num_prefills: int) -> list[PrefillInputData]:
    req_id = str(-1)
    context_len = 0
    query_len = 128
    prompt_tokens = 128
    token_ids = list(int(i) for i in range(prompt_tokens))
    num_blocks = round_up(context_len + query_len, self.block_size) // self.block_size
    blocks = [0] * num_blocks
    num_output_logits = context_len + query_len - prompt_tokens + 1
    logits_positions = list(range(query_len - num_output_logits, query_len))

    new_batch_contents = BatchContents(
        req_ids=[req_id],
        token_ids=[token_ids],
        context_lens=[context_len],
        blocks=[blocks],
        logits_positions=[logits_positions],
    )

    outputs = [self._form_prefill_batch(new_batch_contents.clone()) for _ in range(num_prefills)]
    return outputs

_dummy_run

_dummy_run(max_num_batched_tokens: int) -> None
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _dummy_run(self, max_num_batched_tokens: int) -> None:
    assert max_num_batched_tokens == 1
    # when P/D disagg used, add dummy prefill run for prefiller instance
    if has_kv_transfer_group() and self.vllm_config.kv_transfer_config.is_kv_producer:
        prompt_cfg = 1, 1, 1
        decode_cfg = None
    else:
        prompt_cfg = None
        decode_cfg = 1, 1, 1
    # add dummy run
    self._prepare_dummy_scenario(prompt_cfg, decode_cfg)
    return

_execute_dummy_scenario

_execute_dummy_scenario(requests, scheduled_tokens)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _execute_dummy_scenario(self, requests, scheduled_tokens):
    from vllm.v1.core.sched.output import (SchedulerOutput, CachedRequestData)

    sched_output = SchedulerOutput(
        scheduled_new_reqs=requests,
        scheduled_cached_reqs=CachedRequestData.make_empty(),
        num_scheduled_tokens=scheduled_tokens,
        total_num_scheduled_tokens=sum(scheduled_tokens.values()),
        scheduled_spec_decode_tokens={},
        scheduled_encoder_inputs={},
        num_common_prefix_blocks=0,
        finished_req_ids=set(),
        free_encoder_mm_hashes=[],
    )
    cleanup = SchedulerOutput(
        scheduled_new_reqs=[],
        scheduled_cached_reqs=CachedRequestData.make_empty(),
        num_scheduled_tokens={},
        total_num_scheduled_tokens=0,
        scheduled_spec_decode_tokens={},
        scheduled_encoder_inputs={},
        num_common_prefix_blocks=0,
        finished_req_ids=set(req.req_id for req in requests),
        free_encoder_mm_hashes=[],
    )
    self.execute_model(sched_output, warmup_mode=True)
    self.sample_tokens(None)
    self.execute_model(cleanup, warmup_mode=True)

_execute_mm_encoder

_execute_mm_encoder(
    scheduler_output: SchedulerOutput, req_ids: list[str]
)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _execute_mm_encoder(self, scheduler_output: "SchedulerOutput", req_ids: list[str]):
    scheduled_encoder_inputs = scheduler_output.scheduled_encoder_inputs
    if not scheduled_encoder_inputs:
        return

    # Batch the multi-modal inputs.
    mm_kwargs = list[MultiModalKwargsItem]()
    # List of tuple (mm_hash, pos_info)
    mm_hashes_pos = list[tuple[str, PlaceholderRange]]()
    for req_id in req_ids:
        encoder_input_ids = scheduled_encoder_inputs.get(req_id, None)
        if not encoder_input_ids:
            continue
        req_state = self.requests[req_id]

        for mm_input_id in encoder_input_ids:
            mm_feature = req_state.mm_features[mm_input_id]
            mm_hash = mm_feature.identifier
            mm_kwargs.append(mm_feature.data)
            mm_hashes_pos.append((mm_hash, mm_feature.mm_position))

    if not mm_kwargs:
        return

    # Batch mm inputs as much as we can: if a request in the batch has
    # multiple modalities or a different modality than the previous one,
    # we process it separately to preserve item order.

    # TODO (attafosu): Follow-up on the resolution to this.
    # The ordering of the encoder outputs needs to match the request ids
    # after fetching the embeddings.
    # For now, we'll restrict mm support to just a single prefill at a time - # noqa E501
    # Or that requests in the batch should have distinct modalities,

    # FIXME(ywang96): This is a hacky way to deal with multiple modalities
    # in the same batch while still being able to benefit from batching
    # multimodal inputs. The proper solution should be reordering the
    # encoder outputs.
    encoder_outputs = []
    self.model.model = cast(SupportsMultiModal, self.model.model)
    for _, num_items, mm_kwargs_group in group_mm_kwargs_by_modality(
            mm_kwargs,
            device=self.device,
            pin_memory=self.pin_memory,
            merge_by_field_config=self.model.model.merge_by_field_config,
    ):
        # Run the encoder.
        # `curr_group_outputs` is either of the following:
        # 1. A tensor of shape (num_items, feature_size, hidden_size)
        # in case feature_size is fixed across all multimodal items.
        # 2. A list or tuple (length: num_items) of tensors, each of shape
        # (feature_size, hidden_size) in case the feature size is dynamic
        # depending on the input multimodal items.
        curr_group_outputs = self.model.embed_multimodal(**mm_kwargs_group)

        sanity_check_mm_encoder_outputs(
            curr_group_outputs,
            expected_num_items=num_items,
        )

        for output in curr_group_outputs:
            encoder_outputs.append(output)

    # FIXME (attafosu) Reorder the encoder outputs to match the request ids.
    # This will be necessary after mm prefill batching constraints are removed # noqa E501

    # Cache the encoder outputs.
    for (mm_hash, pos_info), output in zip(
            mm_hashes_pos,
            encoder_outputs,
    ):
        if req_id not in self.encoder_cache:
            self.encoder_cache[req_id] = {}

        self.encoder_cache[mm_hash] = scatter_mm_placeholders(
            output,
            is_embed=pos_info.is_embed.to(
                device=output.device) if pos_info.is_embed is not None else pos_info.is_embed,
        )

_execute_model_generic

_execute_model_generic(
    token_ids,
    position_ids,
    attn_metadata,
    logits_indices,
    kv_caches,
    lora_logits_mask,
    lora_mask,
    warmup_mode=False,
    inputs_embeds=None,
    model_mm_kwargs=None,
)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _execute_model_generic(self,
                           token_ids,
                           position_ids,
                           attn_metadata,
                           logits_indices,
                           kv_caches,
                           lora_logits_mask,
                           lora_mask,
                           warmup_mode=False,
                           inputs_embeds=None,
                           model_mm_kwargs=None):
    # FORWARD.
    batch_size = token_ids.size(0)
    seq_len = self._seq_len(attn_metadata)
    num_blocks = self._num_blocks(attn_metadata)
    if not self.unified_attn:
        self._check_config(batch_size, seq_len, num_blocks, attn_metadata, warmup_mode)
    else:
        self._check_unified_config(attn_metadata, logits_indices, warmup_mode)
    additional_kwargs = {}
    if htorch.utils.internal.is_lazy():
        use_graphs = self._use_graphs()
        if self.max_cudagraph_capture_size is not None and batch_size * seq_len > self.max_cudagraph_capture_size:
            use_graphs = False
        additional_kwargs.update({"bypass_hpu_graphs": not use_graphs})
    else:
        # no hpu graphs for t.compile?
        use_graphs = False
    if self.model_has_chunked_attention:
        additional_kwargs.update({"model_has_chunked_attention": True})
    trimmed_attn_metadata = attn_metadata if self.unified_attn else trim_attn_metadata(attn_metadata)
    if self.is_driver_worker:
        model_event_name = ("model_forward_"
                            f"bs{batch_size}_"
                            f"seq{seq_len}_"
                            f"ctx{num_blocks}_"
                            f"graphs{'T' if use_graphs else 'F'}")
    else:
        model_event_name = 'model_executable'
    with self.profiler.record_event('internal', model_event_name):
        hidden_states = self.model.forward(input_ids=token_ids,
                                           positions=position_ids,
                                           attn_metadata=trimmed_attn_metadata,
                                           kv_caches=kv_caches,
                                           inputs_embeds=inputs_embeds,
                                           model_mm_kwargs=model_mm_kwargs,
                                           lora_mask=lora_mask,
                                           **additional_kwargs)
    # NOTE(kzawora): returning hidden_states is required in prompt logprobs
    # scenarios, as they will do logit processing on their own
    if self.use_aux_hidden_state_outputs:
        non_flattened_hidden_states, aux_hidden_states = hidden_states
        hidden_states = non_flattened_hidden_states
    else:
        non_flattened_hidden_states = hidden_states
        aux_hidden_states = None

    hidden_states = hidden_states.view(-1, hidden_states.shape[-1])
    hidden_states = hidden_states[logits_indices]
    LoraMask.setLoraMask(lora_logits_mask)
    with self.profiler.record_event('internal', ('compute_logits'
                                                 f'{batch_size}_'
                                                 f'seq{seq_len}_ctx'
                                                 f'{num_blocks}')):
        logits = self.model.compute_logits(hidden_states)
    return non_flattened_hidden_states, aux_hidden_states, \
        hidden_states, logits

_extract_mm_kwargs

_extract_mm_kwargs(
    scheduler_output: SchedulerOutput,
) -> BatchedTensorInputs
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _extract_mm_kwargs(
    self,
    scheduler_output: "SchedulerOutput",
) -> BatchedTensorInputs:
    if self.is_multimodal_raw_input_supported:  # noqa: SIM102
        if scheduler_output:
            mm_kwargs = list[MultiModalKwargsItem]()
            for req in scheduler_output.scheduled_new_reqs:
                req_mm_kwargs = req.mm_kwargs
                if not isinstance(req_mm_kwargs, list):
                    req_mm_kwargs = list(req_mm_kwargs)
                mm_kwargs.extend(req_mm_kwargs)

            # Input all modalities at once
            self.model.model = cast(SupportsMultiModal, self.model.model)
            mm_kwargs_combined: BatchedTensorInputs = {}
            for _, _, mm_kwargs_group in group_mm_kwargs_by_modality(
                    mm_kwargs,
                    device=self.device,
                    pin_memory=self.pin_memory,
                    merge_by_field_config=self.model.model.merge_by_field_config,
            ):
                mm_kwargs_combined.update(mm_kwargs_group)

            return mm_kwargs_combined

    return {}

_extract_prefill_batch_contents

_extract_prefill_batch_contents(
    num_prefills,
    num_decodes,
    num_scheduled_tokens,
    warmup=False,
)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _extract_prefill_batch_contents(self, num_prefills, num_decodes, num_scheduled_tokens, warmup=False):
    # DECODES are the first num_decodes REQUESTS.
    # PREFILLS are the next num_reqs - num_decodes REQUESTS.
    num_reqs = num_prefills + num_decodes
    block_table_cpu_tensor = self.input_batch.block_table[0].get_cpu_tensor()
    all_batch_contents = [BatchContents()]

    for batch_idx in range(num_decodes, num_reqs):
        req_id = self.input_batch.req_ids[batch_idx]
        seq_num_computed_tokens = self.input_batch.num_computed_tokens_cpu[batch_idx]
        seq_num_scheduled_tokens = num_scheduled_tokens[batch_idx]

        token_ids = self.input_batch.token_ids_cpu[batch_idx, seq_num_computed_tokens:seq_num_computed_tokens +
                                                   seq_num_scheduled_tokens].tolist()

        num_blocks = round_up(seq_num_computed_tokens + seq_num_scheduled_tokens,
                              self.block_size) // self.block_size
        blocks = block_table_cpu_tensor[batch_idx, :num_blocks].tolist()
        if not warmup:
            blocks = [self.defragmenter.resolve(b) for b in blocks]
        #NOTE(kzawora): In non-preemption scenario,
        # self.input_batch.num_prompt_tokens[batch_idx] == self.input_batch.num_tokens[batch_idx].
        # In preemption scenario num_tokens will also include the tokens emitted before preemption
        num_prompt_tokens = self.input_batch.num_prompt_tokens[batch_idx]
        num_output_logits = max(0, seq_num_computed_tokens + seq_num_scheduled_tokens - num_prompt_tokens + 1)
        logits_positions = list(range(seq_num_scheduled_tokens - num_output_logits, seq_num_scheduled_tokens))

        new_batch_contents = BatchContents(
            req_ids=[req_id],
            token_ids=[token_ids],
            context_lens=[seq_num_computed_tokens],
            prompt_lens=[num_prompt_tokens],
            blocks=[blocks],
            logits_positions=[logits_positions],
        )
        if self._can_merge_prefill_contents(all_batch_contents[-1], new_batch_contents):
            merge_contents(all_batch_contents[-1], new_batch_contents)
        else:
            all_batch_contents.append(new_batch_contents)

    num_real_prefill_batches = 0
    for content in all_batch_contents:
        if len(content.req_ids) > 0:
            num_real_prefill_batches += 1

    num_pad_across_dp = self.get_dp_padding(num_real_prefill_batches)
    return all_batch_contents, num_pad_across_dp

_form_prefill_batch

_form_prefill_batch(contents)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _form_prefill_batch(self, contents):
    if len(contents.req_ids) == 0:
        return PrefillInputData()

    token_ids = contents.token_ids
    req_ids = contents.req_ids
    query_lens = [len(tids) for tids in contents.token_ids]
    if self.profiler.enabled:
        self.profiler_counter_helper.capture_prompt_seq_stats(query_lens)
    context_lens = contents.context_lens

    token_positions = [list(range(cl, cl + ql)) for cl, ql in zip(context_lens, query_lens)]

    block_assignment = [[divmod(pos, self.block_size) for pos in positions] for positions in token_positions]

    token_slots = [[blocks[bi] * self.block_size + bo for bi, bo in assignment]
                   for blocks, assignment in zip(contents.blocks, block_assignment)]
    token_groups = [[i] * len(tid) for i, tid in enumerate(token_ids)]
    num_context_blocks = [round_up(ctx_len, self.block_size) // self.block_size for ctx_len in context_lens]
    context_blocks: list = [blocks[:num] for blocks, num in zip(contents.blocks, num_context_blocks)]
    num_context_blocks = [len(b) for b in context_blocks]
    context_groups = [[i] * b for i, b in enumerate(num_context_blocks)]
    has_context = sum(context_lens) > 0
    target_bs, target_seq, target_blocks = self._get_prompt_bucketing_fn()(query_lens, num_context_blocks)

    target_bs += self.get_dp_padding(target_bs)
    target_seq += self.get_dp_padding(target_seq)
    target_blocks += self.get_dp_padding(target_blocks)

    # NOTE: If model does not support multimodal inputs, we pad here.
    # For models with multimodal support, we may want to get embeddings
    # for the valid tokens before padding.
    # This would require getting multimodal input embeddings here as well
    token_ids = align_and_pad(contents.token_ids, (target_bs, target_seq), itertools.repeat(-1))
    # Update query_lens and context_lens after padding
    query_lens.extend([0] * (target_bs - len(query_lens)))
    context_lens.extend([0] * (target_bs - len(context_lens)))

    # If the model uses M-RoPE, we need to fill
    # and pad the M-RoPE positions for the scheduled prefill tokens
    if self.uses_mrope:
        token_positions = self._align_and_pad_mrope_positions(
            contents.req_ids,
            context_lens,
            query_lens,
            (target_bs, target_seq),
            -1,
        )

    else:
        token_positions = align_and_pad(token_positions, (target_bs, target_seq), itertools.repeat(-1))
    token_slots = align_and_pad(token_slots, (target_bs, target_seq), itertools.repeat(-1))
    token_groups = align_and_pad(token_groups, (target_bs, target_seq), itertools.repeat(-1))
    context_blocks = align_and_pad(context_blocks, (target_bs, target_blocks), itertools.repeat(-1))
    context_groups = align_and_pad(context_groups, (target_bs, target_blocks), itertools.repeat(-1))

    # TODO: cycle through dummy slots and blocks
    # dummy_slots = itertools.cycle(
    #    range(self._PAD_SLOT_ID, self._PAD_SLOT_ID + self.block_size))

    cur_offset = 0
    logits_indices = []
    logits_requests = []
    for req_id, qlen, log_pos in zip(req_ids, query_lens, contents.logits_positions):
        source = [cur_offset + x for x in log_pos]
        dest = [req_id] * len(log_pos)
        logits_indices.extend(source)
        logits_requests.extend(dest)
        if self.use_merged_prefill:
            cur_offset += qlen
        else:
            cur_offset += len(token_ids[0])

    attn_bias = None
    if self.use_merged_prefill:
        attn_bias = self._make_attn_bias(context_groups, token_groups)
        attn_bias = attn_bias.to('hpu', non_blocking=True)
    else:
        attn_bias = None

    logits_indices = pad_list(logits_indices, round_up(len(logits_indices), self.logits_rounding),
                              itertools.repeat(-1))

    query_lens = async_h2d_copy(query_lens, dtype=torch.int32)
    token_ids = async_h2d_copy(token_ids, dtype=torch.int32)
    token_positions = async_h2d_copy(token_positions, dtype=torch.int32)
    token_slots = async_h2d_copy(token_slots, dtype=torch.int64)
    logits_indices = async_h2d_copy(logits_indices, dtype=torch.int32)
    context_lens = async_h2d_copy(context_lens, dtype=torch.int32)
    context_blocks_t: Optional[torch.tensor]
    context_blocks_t = async_h2d_copy(context_blocks, dtype=torch.int32).flatten() if has_context else None

    attn_metadata = HPUAttentionMetadataV1.make_prefill_metadata(seq_lens_tensor=query_lens,
                                                                 context_lens_tensor=context_lens,
                                                                 slot_mapping=token_slots,
                                                                 block_list=context_blocks_t,
                                                                 attn_bias=attn_bias,
                                                                 block_size=self.block_size)
    return PrefillInputData(request_ids=[req_ids],
                            prompt_lens=[query_lens],
                            token_ids=[token_ids],
                            position_ids=[token_positions],
                            attn_metadata=[attn_metadata],
                            logits_indices=[logits_indices],
                            logits_requests=[logits_requests])

_form_unified_prefill_batch

_form_unified_prefill_batch(contents)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _form_unified_prefill_batch(self, contents):
    if len(contents.req_ids) == 0:
        return PrefillInputData()

    token_ids = contents.token_ids
    req_ids = contents.req_ids
    query_lens = [len(tids) for tids in contents.token_ids]
    prompt_lens = contents.prompt_lens
    if self.profiler.enabled:
        self.profiler_counter_helper.capture_prompt_seq_stats(query_lens)
    context_lens = contents.context_lens

    batch_data = create_unified_batch(
        token_ids=token_ids,
        block_size=self.block_size,
        block_table=contents.blocks,
        context_lengths=context_lens,
        query_lengths=query_lens,
        prompt_lengths=prompt_lens,
        dtype=self.dtype,
        contiguous_kv=self.use_contiguous_pa,
        bucketing_fn=self.unified_bucketing_fn,
        get_dp_padding_fn=self.get_dp_padding,
    )

    (token_ids_t, token_positions_t, logits_indices_t, logits_groups, attn_metadata) = batch_data
    logits_requests = [req_ids[lg] for lg in logits_groups]
    return PrefillInputData(request_ids=[req_ids],
                            prompt_lens=[None],
                            token_ids=[token_ids_t.unsqueeze(0)],
                            attn_metadata=[attn_metadata],
                            position_ids=[token_positions_t.unsqueeze(0)],
                            logits_indices=[logits_indices_t],
                            logits_requests=[logits_requests])

_gather_mm_embeddings

_gather_mm_embeddings(
    scheduler_output: SchedulerOutput,
    req_ids: list[str],
    shift_computed_tokens: int = 0,
    total_num_scheduled_tokens: Optional[int] = None,
) -> tuple[list[Tensor], Tensor]
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _gather_mm_embeddings(
    self,
    scheduler_output: "SchedulerOutput",
    req_ids: list[str],
    shift_computed_tokens: int = 0,
    total_num_scheduled_tokens: Optional[int] = None,
) -> tuple[list[torch.Tensor], torch.Tensor]:
    total_num_scheduled_tokens = total_num_scheduled_tokens or scheduler_output.total_num_scheduled_tokens

    mm_embeds = list[torch.Tensor]()
    is_mm_embed = self.is_mm_embed.cpu
    is_mm_embed[:total_num_scheduled_tokens] = False

    req_start_idx = 0
    for req_id in req_ids:
        num_scheduled_tokens = scheduler_output.num_scheduled_tokens[req_id]
        req_state = self.requests[req_id]
        num_computed_tokens = \
            req_state.num_computed_tokens + shift_computed_tokens
        for mm_feature in req_state.mm_features:
            pos_info = mm_feature.mm_position
            start_pos = pos_info.offset
            num_encoder_tokens = pos_info.length

            # The encoder output is needed if the two ranges overlap:
            # [num_computed_tokens,
            #  num_computed_tokens + num_scheduled_tokens) and
            # [start_pos, start_pos + num_encoder_tokens)
            if start_pos >= num_computed_tokens + num_scheduled_tokens:
                # The encoder output is not needed in this step.
                break
            if start_pos + num_encoder_tokens <= num_computed_tokens:
                # The encoder output is already processed and stored
                # in the decoder's KV cache.
                continue

            start_idx = max(num_computed_tokens - start_pos, 0)
            end_idx = min(num_computed_tokens - start_pos + num_scheduled_tokens, num_encoder_tokens)
            assert start_idx < end_idx
            mm_hash = mm_feature.identifier
            encoder_output = self.encoder_cache.get(mm_hash, None)
            assert encoder_output is not None,\
                  f"Encoder cache miss for {mm_hash}."
            encoder_output = self.encoder_cache[mm_hash]

            if (is_embed := pos_info.is_embed) is not None:
                is_embed = is_embed[start_idx:end_idx]

            mm_embeds_item = gather_mm_placeholders(
                encoder_output[start_idx:end_idx],
                is_embed=is_embed,
            )
            req_start_pos = req_start_idx + start_pos - num_computed_tokens
            is_mm_embed[req_start_pos+start_idx:req_start_pos + end_idx] \
                = True

            # Only whole mm items are processed
            mm_embeds.append(mm_embeds_item)
        req_start_idx += num_scheduled_tokens

    is_mm_embed = self.is_mm_embed.copy_to_gpu(total_num_scheduled_tokens)

    return mm_embeds, is_mm_embed

_generate_profiling

_generate_profiling(prompt_cfg, decode_cfg)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _generate_profiling(self, prompt_cfg, decode_cfg):
    steps = 3
    profiler = setup_profiler(warmup=steps - 1, active=1)
    if prompt_cfg and prompt_cfg not in self.bucketing_manager.prompt_buckets:
        self.bucketing_manager.prompt_buckets.insert(0, prompt_cfg)
    elif decode_cfg and decode_cfg not in self.bucketing_manager.decode_buckets:
        self.bucketing_manager.decode_buckets.insert(0, decode_cfg)
    torch.hpu.synchronize()
    profiler.start()
    for _ in range(steps):
        self._prepare_dummy_scenario(prompt_cfg, decode_cfg)
        torch.hpu.synchronize()
        profiler.step()
    profiler.stop()

_generate_req_id_output_token_ids_lst

_generate_req_id_output_token_ids_lst(
    request_ids: Optional[list[str]] = None,
    pad_to: Optional[int] = None,
    logits_reqs=None,
)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _generate_req_id_output_token_ids_lst(self,
                                          request_ids: Optional[list[str]] = None,
                                          pad_to: Optional[int] = None,
                                          logits_reqs=None):
    req_id_output_token_ids: dict[str, list[int]] = \
        {req_id: req.output_token_ids
            for req_id, req in self.requests.items()}
    if request_ids is not None:
        req_id_output_token_ids = {req_id: req_id_output_token_ids[req_id] for req_id in request_ids}
    req_id_output_token_ids_lst = list(req_id_output_token_ids.items())
    if logits_reqs and len(req_id_output_token_ids_lst) > len(logits_reqs):
        # Merged prefill case: remove requests without logits
        req_id_output_token_ids_lst = [r for r in req_id_output_token_ids_lst if r[0] in logits_reqs]
    else:
        if pad_to is not None and len(req_id_output_token_ids_lst) > 0:
            while len(req_id_output_token_ids_lst) < pad_to:
                req_id_output_token_ids_lst.append(req_id_output_token_ids_lst[0])
    return req_id_output_token_ids_lst

_generate_seq_lengths staticmethod

_generate_seq_lengths(num_samples, num_blocks, block_size)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
@staticmethod
def _generate_seq_lengths(num_samples, num_blocks, block_size):
    assert num_samples <= num_blocks
    blocks = [num_blocks // num_samples] * num_samples
    missing_blocks = num_blocks - sum(blocks)
    for i in range(missing_blocks):
        blocks[i] += 1
    seq_lengths = [b * block_size - 1 for b in blocks]
    return seq_lengths

_get_cumsum_and_arange

_get_cumsum_and_arange(
    num_tokens: ndarray,
    cumsum_dtype: Optional[dtype] = None,
) -> tuple[ndarray, ndarray]

Get the cumulative sum and batched arange of the given array.

E.g., [2, 5, 3] -> ([2, 7, 10], [0, 1, 0, 1, 2, 3, 4, 0, 1, 2])

Equivalent to but faster than:

np.concatenate([np.arange(n) for n in num_tokens])

Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _get_cumsum_and_arange(
    self,
    num_tokens: np.ndarray,
    cumsum_dtype: Optional[np.dtype] = None,
) -> tuple[np.ndarray, np.ndarray]:
    """Get the cumulative sum and batched arange of the given array.
    # E.g., [2, 5, 3] -> ([2, 7, 10], [0, 1, 0, 1, 2, 3, 4, 0, 1, 2])
    # Equivalent to but faster than:
    # np.concatenate([np.arange(n) for n in num_tokens])
    """
    # Step 1. [2, 5, 3] -> [2, 7, 10]
    cu_num_tokens = np.cumsum(num_tokens, dtype=cumsum_dtype)
    total_num_tokens = cu_num_tokens[-1]
    # Step 2. [2, 7, 10] -> [0, 0, 2, 2, 2, 2, 2, 7, 7, 7]
    cumsums_offsets = np.repeat(cu_num_tokens - num_tokens, num_tokens)
    # Step 3. [0, 1, 0, 1, 2, 3, 4, 0, 1, 2]
    arange = self.arange_np[:total_num_tokens] - cumsums_offsets

    return cu_num_tokens, arange

_get_mm_dummy_batch

_get_mm_dummy_batch(
    modality: str, max_items_per_batch: int
) -> BatchedTensorInputs

Dummy data for profiling and precompiling multimodal models.

Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _get_mm_dummy_batch(
    self,
    modality: str,
    max_items_per_batch: int,
) -> BatchedTensorInputs:
    """Dummy data for profiling and precompiling multimodal models."""
    assert self.mm_budget is not None

    dummy_decoder_data = self.mm_registry.get_decoder_dummy_data(
        model_config=self.model_config,
        seq_len=self.max_model_len,
        mm_counts={modality: 1},
        cache=self.mm_budget.cache,
    )
    dummy_mm_data = dummy_decoder_data.multi_modal_data

    # Result in the maximum GPU consumption of the model
    dummy_mm_item = dummy_mm_data[modality][0]
    dummy_mm_items = [dummy_mm_item] * max_items_per_batch

    self.model.model = cast(SupportsMultiModal, self.model.model)
    return next(mm_kwargs_group for _, _, mm_kwargs_group in group_mm_kwargs_by_modality(
        dummy_mm_items,
        device=self.device,
        pin_memory=self.pin_memory,
        merge_by_field_config=self.model.model.merge_by_field_config,
    ))

_get_nans_in_logits

_get_nans_in_logits(
    logits: Optional[Tensor],
) -> dict[str, int]
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _get_nans_in_logits(
    self,
    logits: Optional[torch.Tensor],
) -> dict[str, int]:
    try:
        if logits is None:
            return {req_id: 0 for req_id in self.input_batch.req_ids}

        num_nans_in_logits = {}
        num_nans_for_index = logits.isnan().sum(dim=-1).cpu().numpy()
        for req_id in self.input_batch.req_ids:
            req_index = self.input_batch.req_id_to_index[req_id]
            num_nans_in_logits[req_id] = (int(num_nans_for_index[req_index])
                                          if num_nans_for_index is not None and req_index < logits.shape[0] else 0)
        return num_nans_in_logits
    except IndexError:
        return {}

_get_prompt_bucketing_fn

_get_prompt_bucketing_fn()
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _get_prompt_bucketing_fn(self):
    if self.unified_attn:
        return self._skip_bucketing
    elif self.use_merged_prefill:
        return self._bucketize_merged_prompt
    else:
        return self._bucketize_2d_prompt

_get_prompt_logprobs_dict

_get_prompt_logprobs_dict(
    hidden_states: Tensor, scheduler_output: SchedulerOutput
) -> dict[str, Optional[LogprobsTensors]]
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _get_prompt_logprobs_dict(
    self,
    hidden_states: torch.Tensor,
    scheduler_output: "SchedulerOutput",
) -> dict[str, Optional[LogprobsTensors]]:
    num_prompt_logprobs_dict = self.input_batch.num_prompt_logprobs
    if not num_prompt_logprobs_dict:
        return {}

    prompt_logprobs_dict: dict[str, Optional[LogprobsTensors]] = {}

    # Since prompt logprobs are a rare feature, prioritize simple,
    # maintainable loop over optimal performance.
    completed_prefill_reqs = []
    for i, (req_id, num_prompt_logprobs) in enumerate(num_prompt_logprobs_dict.items()):

        num_tokens = scheduler_output.num_scheduled_tokens[req_id]

        # Get metadata for this request.
        request = self.requests[req_id]
        num_prompt_tokens = len(request.prompt_token_ids)
        prompt_token_ids = torch.tensor(request.prompt_token_ids).to(self.device, non_blocking=True)

        # Determine number of logits to retrieve.
        start_tok = request.num_computed_tokens + 1
        num_remaining_tokens = num_prompt_tokens - start_tok
        if num_tokens < num_remaining_tokens:
            # This is a chunk, more tokens remain.
            num_logits = num_tokens
        else:
            # This is the last chunk of prompt tokens to return.
            num_logits = num_remaining_tokens
            completed_prefill_reqs.append(req_id)

        # Get the logits corresponding to this req's prompt tokens.
        # If this is a partial request (i.e. chunked prefill),
        # then there is prompt logprob generated for each index.
        prompt_hidden_states = hidden_states[i, :num_logits]
        logits = self.model.compute_logits(prompt_hidden_states)

        # Get the "target" tokens for each index. For prompt at index i,
        # the token at prompt index i+1 is the "sampled" token we want
        # to gather the logprob for.
        tgt_token_ids = prompt_token_ids[start_tok:start_tok + num_logits]

        # Compute prompt logprobs.
        logprobs = self.sampler.compute_logprobs(logits)
        token_ids, logprobs, ranks = self.sampler.gather_logprobs(logprobs, num_prompt_logprobs, tgt_token_ids)

        # Transfer GPU->CPU async.
        prompt_logprobs_dict[req_id] = LogprobsTensors(
            token_ids.to("cpu", non_blocking=True),
            logprobs.to("cpu", non_blocking=True),
            ranks.to("cpu", non_blocking=True),
        )

    # Remove requests that have completed prefill from the batch
    # num_prompt_logprobs_dict.
    for req_id in completed_prefill_reqs:
        del num_prompt_logprobs_dict[req_id]

    # Must synchronize the non-blocking GPU->CPU transfers.
    torch.hpu.synchronize()

    return prompt_logprobs_dict

_get_prompts_and_decodes

_get_prompts_and_decodes(
    scheduler_output: SchedulerOutput,
) -> PromptDecodeInfo
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _get_prompts_and_decodes(
    self,
    scheduler_output: "SchedulerOutput",
) -> PromptDecodeInfo:
    total_num_scheduled_tokens = scheduler_output.total_num_scheduled_tokens
    assert total_num_scheduled_tokens > 0
    num_reqs = self.input_batch.num_reqs
    assert num_reqs > 0
    #TODO: remove later

    requests_type = {}
    if scheduler_output.kv_connector_metadata:
        for req in scheduler_output.kv_connector_metadata.reqs_to_save:
            requests_type[req] = 'prefill'
        for req in scheduler_output.kv_connector_metadata.reqs_to_recv:
            requests_type[req] = 'decode'
        requests = scheduler_output.kv_connector_metadata.reqs_to_save | \
                    scheduler_output.kv_connector_metadata.reqs_to_recv
    else:
        requests = None

    # Traverse decodes first
    decode_req_ids = []
    num_computed_tokens_decode = []
    for i in range(num_reqs):
        req_id = self.input_batch.req_ids[i]
        assert req_id is not None
        # P case assigment
        if requests is not None and req_id not in self.input_batch.req_type:
            for request in requests:
                if request == req_id:
                    self.input_batch.req_type[req_id] = requests_type[req_id]
                    break

        num_computed_tokens = self.input_batch.num_computed_tokens_cpu[i]
        num_prompt_tokens = self.input_batch.num_prompt_tokens[i]
        num_scheduled_tokens = scheduler_output.num_scheduled_tokens[req_id]
        if num_computed_tokens < num_prompt_tokens and \
            not self.is_decoder_only(req_id):
            # This is prompt
            break

        # This is decode
        # NOTE(chendi): To support spec decode,
        # we don't assume num_scheduled_tokens == 1.

        decode_req_ids.append(req_id)
        num_computed_tokens_decode.append(int(num_computed_tokens + 1))

    if self.profiler.enabled:
        self.profiler_counter_helper.capture_decode_seq_stats(num_computed_tokens_decode)

    # Traverse prompts
    prompt_req_ids = []
    prompt_scheduled_tokens = []
    for i in range(len(decode_req_ids), num_reqs):
        req_id = self.input_batch.req_ids[i]
        assert req_id is not None

        num_computed_tokens = self.input_batch.num_computed_tokens_cpu[i]
        num_prompt_tokens = self.input_batch.num_prompt_tokens[i]
        num_scheduled_tokens = scheduler_output.num_scheduled_tokens[req_id]

        # Must be prompt
        assert num_computed_tokens < num_prompt_tokens
        # NOTE(kzawora): In preempted sequences, num_output_tokens can be > 0, and still be a valid prefill

        prompt_req_ids.append(req_id)
        prompt_scheduled_tokens.append(num_scheduled_tokens)

    return PromptDecodeInfo(prompt_req_ids, decode_req_ids, prompt_scheduled_tokens)

_get_unified_config

_get_unified_config(attn_metadata, logits_indices)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _get_unified_config(self, attn_metadata, logits_indices):
    has_causal = 'c' if attn_metadata.causal_bias is not None else '-'
    has_shared = 's' if attn_metadata.shared_bias is not None else '-'
    has_unique = 'u' if attn_metadata.unique_bias is not None else '-'
    phase = has_causal + has_shared + has_unique
    qlen = attn_metadata.slot_mapping.size(0)
    num_shared_blocks = attn_metadata.shared_blocks.size(0) if attn_metadata.shared_blocks is not None else 0
    num_unique_blocks = attn_metadata.unique_blocks
    num_logits = logits_indices.size(0)
    cfg = (phase, qlen, num_shared_blocks, num_unique_blocks, num_logits)
    return cfg

_inc_preprocess

_inc_preprocess()
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _inc_preprocess(self):
    self._remove_duplicate_submodules()

_is_quant_with_inc

_is_quant_with_inc()
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _is_quant_with_inc(self):
    quant_config = os.getenv("QUANT_CONFIG", None) is not None
    return (self.model_config.quantization == "inc" or quant_config)

_make_attn_bias

_make_attn_bias(context_groups, token_groups)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _make_attn_bias(self, context_groups, token_groups):
    dtype = self.dtype
    is_causal = True  # TODO: add support for non-causal tasks
    context_groups = torch.tensor(context_groups, device='cpu', dtype=torch.int16)
    context_groups = context_groups.repeat_interleave(self.block_size, dim=-1)
    context_len = context_groups.size(-1)
    token_groups = torch.tensor(token_groups, device='cpu', dtype=torch.int16)
    num_queries = token_groups.size(-1)
    seq_groups = torch.cat([context_groups, token_groups], dim=-1)
    attn_mask = seq_groups.unflatten(-1, (1, -1)) != token_groups.unflatten(-1, (-1, 1))
    if is_causal:
        causal_mask = torch.ones(num_queries, num_queries, device='cpu', dtype=torch.bool)
        causal_mask = torch.triu(causal_mask, diagonal=1).unsqueeze(0)
        attn_mask[:, :, context_len:].logical_or_(causal_mask)
    attn_mask = attn_mask.to(dtype).masked_fill_(attn_mask, -math.inf)

    return attn_mask.unflatten(0, (1, -1))

_make_buffer

_make_buffer(
    *size: Union[int, SymInt],
    dtype: dtype,
    numpy: bool = True,
) -> CpuGpuBuffer
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _make_buffer(self, *size: Union[int, torch.SymInt], dtype: torch.dtype, numpy: bool = True) -> CpuGpuBuffer:
    return CpuGpuBuffer(*size, dtype=dtype, device=self.device, pin_memory=self.pin_memory, with_numpy=numpy)

_maybe_compile

_maybe_compile(*args, **kwargs)

Entrypoint for a torch.compilation of the model

Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _maybe_compile(self, *args, **kwargs):
    """Entrypoint for a torch.compilation of the model"""
    if (not is_fake_hpu() and not htorch.utils.internal.is_lazy()
            and not self.vllm_config.model_config.enforce_eager):
        # force_parameter_static_shapes = False  alows to use dynamic
        # shapes on tensors added to module via register_buffer()
        torch._dynamo.config.force_parameter_static_shapes = False
        self.compile_config = HPUCompileConfig()
        if self.compile_config.regional_compilation:
            self._compile_methods()
            self.regional_compilation_layers_list = [RMSNorm, VocabParallelEmbedding]
            self._regional_compilation(self.model)
            self.sampler = self._compile(self.sampler)
        else:
            self.model = self._compile(self.model)

_num_blocks

_num_blocks(attn_metadata)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _num_blocks(self, attn_metadata):
    return attn_metadata.num_blocks()

_parse_legacy_profile_cfg staticmethod

_parse_legacy_profile_cfg(profile_cfg)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
@staticmethod
def _parse_legacy_profile_cfg(profile_cfg):
    if profile_cfg:
        cfg = profile_cfg.split('_')
        assert cfg[0] in ['prompt', 'decode']
        return (cfg[0], int(cfg[1]), int(cfg[2]), cfg[3] == 't')
    return None

_parse_profile_cfg staticmethod

_parse_profile_cfg(profile_cfg)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
@staticmethod
def _parse_profile_cfg(profile_cfg):
    if profile_cfg:
        return tuple(map(int, profile_cfg.split(',')))
    return None

_pool

_pool(
    hidden_states: Tensor,
    num_scheduled_tokens: int,
    num_scheduled_tokens_np: ndarray,
) -> ModelRunnerOutput
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _pool(
    self,
    hidden_states: torch.Tensor,
    num_scheduled_tokens: int,
    num_scheduled_tokens_np: np.ndarray,
) -> ModelRunnerOutput:
    assert self.input_batch.num_reqs ==\
        len(self.input_batch.pooling_params), \
    "Either all or none of the requests in" \
    " a batch must be pooling request"
    hidden_states = hidden_states[:num_scheduled_tokens]

    pooling_metadata = self.input_batch.pooling_metadata
    pooling_metadata.build_pooling_cursor(num_scheduled_tokens_np.tolist(), device=hidden_states.device)

    num_reqs = self.input_batch.num_reqs

    seq_lens = (
        torch.tensor(self.input_batch.num_prompt_tokens[:num_reqs], dtype=torch.int32, device=self.device) +
        torch.tensor(self.input_batch.num_computed_tokens_cpu[:num_reqs], dtype=torch.int32, device=self.device))
    raw_pooler_output = self.model.pooler(hidden_states=hidden_states, pooling_metadata=pooling_metadata)
    raw_pooler_output = json_map_leaves(
        lambda x: x.to("cpu", non_blocking=True),
        raw_pooler_output,
    )

    pooler_output: list[Optional[torch.Tensor]] = []
    for raw_output, seq_len, prompt_len in zip(raw_pooler_output, seq_lens, pooling_metadata.prompt_lens):

        if seq_len == prompt_len:
            pooler_output.append(raw_output)
        else:
            pooler_output.append(None)

    return ModelRunnerOutput(
        req_ids=[self.input_batch.req_ids],
        req_id_to_index=self.input_batch.req_id_to_index,
        sampled_token_ids=[],
        logprobs=None,
        prompt_logprobs_dict={},
        pooler_output=pooler_output,
        kv_connector_output=None,
    )

_prepare_decode_inputs

_prepare_decode_inputs(
    num_decodes, num_scheduled_tokens, scheduler_output=None
) -> tuple[DecodeInputData, Optional[DecodeInputData]]
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _prepare_decode_inputs(self,
                           num_decodes,
                           num_scheduled_tokens,
                           scheduler_output=None) -> tuple[DecodeInputData, Optional[DecodeInputData]]:
    # Decodes run as one single padded batch with shape [batch, 1]
    #
    # We need to set _PAD_SLOT_ID for the padding tokens in the
    # slot_mapping, such that the attention KV cache insertion
    # logic knows to ignore those indicies. Otherwise, the
    # padding data can be dummy since we have a causal mask.

    num_pad_across_dp = self.get_dp_padding(num_decodes)
    if num_decodes == 0:
        if num_pad_across_dp > 0:
            dummy_decode_input_data = self._create_dummy_decode_input_data()
            return DecodeInputData(num_decodes=0), dummy_decode_input_data
        return DecodeInputData(num_decodes=0), None
    return self._create_decode_input_data(num_decodes, num_scheduled_tokens,
                                          self.input_batch.num_computed_tokens_cpu[:num_decodes],
                                          self.input_batch.block_table[0].get_cpu_tensor(), scheduler_output), None

_prepare_dummy_scenario

_prepare_dummy_scenario(prompt_cfg, decode_cfg)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _prepare_dummy_scenario(self, prompt_cfg, decode_cfg):
    requests: list[NewRequestData] = []
    scheduled_tokens: dict[str, int] = {}

    if prompt_cfg:
        prompt_bs, prompt_query_len, prompt_num_blocks = prompt_cfg
        prompt_ctx_len = prompt_num_blocks * self.block_size
        prompt_total_tokens = [prompt_query_len + prompt_ctx_len]
        prompt_num_context_blocks = [prompt_num_blocks]
        if self.max_model_len < sum(prompt_total_tokens) \
            and self.use_merged_prefill:
            # split query and ctx in merged prefill case
            prompt_total_tokens, prompt_num_context_blocks = \
                 self.get_merged_prefill_seq_lens(prompt_query_len,
                                             prompt_num_blocks)
        for _ in range(prompt_bs):
            for tokens, context_len in zip(prompt_total_tokens, prompt_num_context_blocks):
                self._add_dummy_request(requests,
                                        scheduled_tokens,
                                        num_computed_tokens=(context_len * self.block_size),
                                        total_tokens=tokens,
                                        scheduled_tokens=prompt_query_len,
                                        is_prompt=True)
    if decode_cfg:
        decode_bs, decode_query_len, decode_num_blocks = decode_cfg
        if self.use_contiguous_pa:
            decode_seq_lengths = [self.block_size] * decode_bs
            block_id = decode_num_blocks - 1
        else:
            decode_seq_lengths = self._generate_seq_lengths(decode_bs, decode_num_blocks, self.block_size)
            block_id = 0
        for dsl in decode_seq_lengths:
            self._add_dummy_request(requests,
                                    scheduled_tokens,
                                    num_computed_tokens=dsl,
                                    total_tokens=dsl,
                                    scheduled_tokens=1,
                                    is_prompt=False,
                                    block_id=block_id)
    self._execute_dummy_scenario(requests, scheduled_tokens)

_prepare_dummy_unified_scenario

_prepare_dummy_unified_scenario(unified_cfg)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _prepare_dummy_unified_scenario(self, unified_cfg):
    requests: list[NewRequestData] = []
    scheduled_tokens: dict[str, int] = {}

    query_len, shared_ctx_len, unique_ctx_len, is_causal = unified_cfg
    num_computed_tokens = (shared_ctx_len + unique_ctx_len) * self.block_size

    if is_causal:
        decode_reqs_query = []
        decode_reqs_blocks = []
        prompt_reqs_query = []
        prompt_reqs_blocks: list = []

        all_shared_blocks_ids = [block for block in range(shared_ctx_len)]
        unique_block = unique_ctx_len - 1
        # do not use unique block id
        if unique_block in all_shared_blocks_ids:
            all_shared_blocks_ids.remove(unique_ctx_len - 1)
            all_shared_blocks_ids.append(shared_ctx_len + 1)

        #add unique
        if unique_ctx_len > 0:
            decode_reqs_query.append(1)
            decode_reqs_blocks.append([unique_ctx_len - 1])
        prompts_number = self.max_num_seqs - len(decode_reqs_query)
        remaining_query = query_len - sum(decode_reqs_query)

        q, r = divmod(remaining_query, prompts_number)
        prompt_reqs_query = [q + (1 if i < r else 0) for i in range(prompts_number)]
        prompt_reqs_blocks = [[] for _ in range(len(prompt_reqs_query))]
        for idx, query in enumerate(prompt_reqs_query):
            available_space_for_ctx = math.floor((self.max_model_len - query) // self.block_size)
            if len(all_shared_blocks_ids) >= available_space_for_ctx:
                prompt_reqs_blocks[idx] = all_shared_blocks_ids[:available_space_for_ctx]
                del all_shared_blocks_ids[:available_space_for_ctx]
            else:
                prompt_reqs_blocks[idx] = all_shared_blocks_ids
                break
        if unique_ctx_len > 0:
            self._add_dummy_unified_request(requests, False, True, [unique_ctx_len - 1], num_computed_tokens, 1,
                                            scheduled_tokens)

        for query, blocks in zip(prompt_reqs_query, prompt_reqs_blocks):
            self._add_dummy_unified_request(requests, True, False, blocks, num_computed_tokens, query,
                                            scheduled_tokens)
    else:
        remaining_samples = query_len
        base = shared_ctx_len // remaining_samples
        remain = shared_ctx_len % remaining_samples
        all_shared_blocks_ids = [block for block in range(shared_ctx_len)]
        unique_block = unique_ctx_len - 1
        # do not use unique block id
        if unique_block in all_shared_blocks_ids:
            all_shared_blocks_ids.remove(unique_ctx_len - 1)
            all_shared_blocks_ids.append(shared_ctx_len + 1)

        # distribute evenly across sublists
        split_shared_blocks_ids: list[list[int]] = [[] for _ in range(remaining_samples)]
        idx = 0
        for i in range(remaining_samples):
            size = base + (1 if i < remain else 0)
            for _ in range(size):
                split_shared_blocks_ids[i].append(all_shared_blocks_ids[idx])
                idx += 1

        # make sure that all blocks are shared = in at least two decodes
        for i, block in enumerate(all_shared_blocks_ids):
            target = (i + 1) % remaining_samples
            if block not in split_shared_blocks_ids[target]:
                split_shared_blocks_ids[target].append(block)

        # add unique id
        if unique_ctx_len > 0:
            min_idx = min(range(remaining_samples), key=lambda j: len(split_shared_blocks_ids[j]))
            split_shared_blocks_ids[min_idx].append(unique_block)

        for i in range(len(split_shared_blocks_ids)):
            if not split_shared_blocks_ids[i]:
                if unique_block - i >= 0:
                    split_shared_blocks_ids[i] = [unique_block - i]
                else:
                    split_shared_blocks_ids[i] = [all_shared_blocks_ids[0]]

        for request_blocks in split_shared_blocks_ids:
            self._add_dummy_unified_request(requests, False, False, request_blocks, num_computed_tokens, 1,
                                            scheduled_tokens)

    self._execute_dummy_scenario(requests, scheduled_tokens)

_prepare_input_ids

_prepare_input_ids(
    scheduler_output: SchedulerOutput,
) -> None

Prepare the input IDs for the current batch.

小心处理prev_sampled_token_ids,它可以从上一个引擎迭代中缓存,在这种情况下,GPU上的那些token需要被复制到input_ids的相应槽中。

Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _prepare_input_ids(self, scheduler_output: "SchedulerOutput") -> None:
    """Prepare the input IDs for the current batch.

    Carefully handles the `prev_sampled_token_ids` which can be cached
    from the previous engine iteration, in which case those tokens on the
    GPU need to be copied into the corresponding slots into input_ids."""

    if self.input_batch.prev_sampled_token_ids is None:
        return

    # Compute cu_num_tokens from scheduler_output
    req_ids = self.input_batch.req_ids
    tokens = [scheduler_output.num_scheduled_tokens[i] for i in req_ids]
    num_scheduled_tokens = np.array(tokens, dtype=np.int32)
    cu_num_tokens, arange = self._get_cumsum_and_arange(num_scheduled_tokens)

    # Async scheduling case, where some decode requests from the previous
    # iteration won't have entries in input_ids_cpu and need to be copied
    # on the GPU from prev_sampled_token_ids.
    prev_req_id_to_index = self.input_batch.prev_req_id_to_index
    assert prev_req_id_to_index is not None
    flattened_indices = []
    prev_common_req_indices = []
    indices_match = True
    max_flattened_index = -1
    for req_id, cur_index in self.input_batch.req_id_to_index.items():
        if (self.input_batch.prev_sampled_token_ids_invalid_indices is not None
                and req_id in self.input_batch.prev_sampled_token_ids_invalid_indices):
            # This request was in the previous batch but its
            # prev_sampled_token_ids is invalid
            continue
        if (prev_index := prev_req_id_to_index.get(req_id)) is not None:
            prev_common_req_indices.append(prev_index)
            # We need to compute the flattened input_ids index of the
            # last token in each common request.
            flattened_index = cu_num_tokens[cur_index].item() - 1
            flattened_indices.append(flattened_index)
            indices_match &= (prev_index == flattened_index)
            max_flattened_index = max(max_flattened_index, flattened_index)
    num_commmon_tokens = len(flattened_indices)
    if num_commmon_tokens == 0:
        # No requests in common with the previous iteration
        # So input_ids_cpu will have all the input ids.
        return

    prev_sampled_token_ids = self.input_batch.prev_sampled_token_ids

    if indices_match and max_flattened_index == (num_commmon_tokens - 1):
        # Common-case optimization: the batch is unchanged
        # and no reordering happened.
        # The indices are both the same permutation of 0..N-1
        self.input_ids_hpu[:len(flattened_indices)].copy_(prev_sampled_token_ids[:len(flattened_indices)])
        return

    # Upload the index tensors asynchronously
    # so the scatter can be non-blocking
    input_ids_index_tensor = torch.tensor(flattened_indices, dtype=torch.int64).to(self.device, non_blocking=True)
    if prev_sampled_token_ids.size(0) <= len(prev_common_req_indices):
        prev_common_req_indices = prev_common_req_indices[:prev_sampled_token_ids.size(0)]
    prev_common_req_indices_tensor = torch.tensor(prev_common_req_indices, dtype=torch.int64).to(self.device,
                                                                                                 non_blocking=True)
    self.input_ids_hpu.scatter_(dim=0,
                                index=input_ids_index_tensor,
                                src=prev_sampled_token_ids[prev_common_req_indices_tensor])

_prepare_inputs

_prepare_inputs(
    scheduler_output: SchedulerOutput,
    num_prefills,
    num_decodes,
    warmup=False,
)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _prepare_inputs(
    self,
    scheduler_output: "SchedulerOutput",
    num_prefills,
    num_decodes,
    warmup=False,
):

    total_num_scheduled_tokens = scheduler_output.total_num_scheduled_tokens
    assert total_num_scheduled_tokens > 0

    num_reqs = num_prefills + num_decodes

    ###############################################
    # NOTE(Chendi): Follow GPU_Model_Runner to use set global
    # self.input_ids_cpu and self.positions_cpu
    req_ids = self.input_batch.req_ids
    tokens = [scheduler_output.num_scheduled_tokens[i] for i in req_ids]
    num_scheduled_tokens = np.array(tokens, dtype=np.int32)
    req_indices = np.repeat(self.arange_np[:num_reqs], num_scheduled_tokens)
    positions_np = self.positions_np[:total_num_scheduled_tokens]
    cu_num_tokens, arange = self._get_cumsum_and_arange(num_scheduled_tokens)
    np.add(self.input_batch.num_computed_tokens_cpu[req_indices], arange, out=positions_np)
    token_indices = (positions_np + req_indices * self.input_batch.token_ids_cpu.shape[1])
    torch.index_select(self.input_batch.token_ids_cpu_tensor.flatten(),
                       0,
                       torch.from_numpy(token_indices),
                       out=self.input_ids_cpu[:total_num_scheduled_tokens])
    ###############################################

    # Get the number of scheduled tokens for each request.
    # TODO: The Python loop can be slow. Optimize.
    num_scheduled_tokens = []
    num_prompt_tokens = []
    for idx, req_id in enumerate(self.input_batch.req_ids[:num_reqs]):
        assert req_id is not None
        seq_num_scheduled_tokens = scheduler_output.num_scheduled_tokens[req_id]
        seq_num_prompt_tokens = self.input_batch.num_prompt_tokens[idx]
        num_scheduled_tokens.append(seq_num_scheduled_tokens)
        num_prompt_tokens.append(seq_num_prompt_tokens)
    return (self._prepare_prefill_inputs(num_prefills, num_decodes, num_scheduled_tokens),
            self._prepare_decode_inputs(num_decodes, num_scheduled_tokens, scheduler_output))

_prepare_inputs_for_pooling

_prepare_inputs_for_pooling(scheduler_output)

收集输入、位置、槽映射,并构建attn_metadata

Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _prepare_inputs_for_pooling(self, scheduler_output):
    """Gather inputs, positions, slot mapping, and build attn_metadata"""
    num_scheduled_tokens = []
    input_ids_list = []
    num_computed_tokens_cpu = self.input_batch.num_computed_tokens_cpu
    num_reqs = self.input_batch.num_reqs

    # Collect token ids and scheduled lengths
    for idx, req_id in enumerate(self.input_batch.req_ids[:num_reqs]):
        seq_num_scheduled = scheduler_output.num_scheduled_tokens[req_id]
        num_scheduled_tokens.append(seq_num_scheduled)

        scheduled_req = scheduler_output.scheduled_new_reqs[idx]
        token_ids = torch.as_tensor(scheduled_req.prompt_token_ids, dtype=torch.long).flatten()
        input_ids_list.append(token_ids)

    input_ids = torch.cat(input_ids_list, dim=0).to(self.device)

    # Absolute positions
    absolute_positions = []
    for i, n in enumerate(num_scheduled_tokens):
        prefix = num_computed_tokens_cpu[i]
        absolute_positions.append(prefix + np.arange(n, dtype=np.int64))
    position_ids = torch.from_numpy(np.concatenate(absolute_positions)).to(self.device)

    # Slot mapping + metadata
    total_scheduled_tokens = sum(num_scheduled_tokens)
    slot_mapping = torch.arange(total_scheduled_tokens, dtype=torch.long, device="hpu:0")
    seq_lens_tensor = torch.tensor([total_scheduled_tokens], device='hpu:0', dtype=torch.int32)
    context_lens_tensor = torch.tensor([0], device='hpu:0', dtype=torch.int32)

    attn_metadata = HPUAttentionMetadataV1.make_prefill_metadata(
        seq_lens_tensor=seq_lens_tensor,
        context_lens_tensor=context_lens_tensor,
        slot_mapping=slot_mapping,
        block_list=None,
        attn_bias=None,
        block_size=self.block_size,
    )

    return input_ids, position_ids, num_scheduled_tokens, attn_metadata, \
        total_scheduled_tokens

_prepare_prefill_inputs

_prepare_prefill_inputs(
    num_prefills,
    num_decodes,
    num_scheduled_tokens: list[int],
) -> tuple[PrefillInputData, Optional[PrefillInputData]]
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _prepare_prefill_inputs(self, num_prefills, num_decodes,
                            num_scheduled_tokens: list[int]) -> tuple[PrefillInputData, Optional[PrefillInputData]]:
    all_batch_contents, num_pad_across_dp = \
        self._extract_prefill_batch_contents(
            num_prefills, num_decodes, num_scheduled_tokens)
    all_batches = [self._form_prefill_batch(bc) for bc in all_batch_contents]
    merge_contents(all_batches[0], *all_batches[1:])

    dummy_prefill_input_batches = None
    if num_pad_across_dp > 0:
        dummy_prefill_input_batches = \
            self._create_dummy_prefill_batch_contents(num_pad_across_dp)
        merge_contents(dummy_prefill_input_batches[0], *dummy_prefill_input_batches[1:])
    return all_batches[0], dummy_prefill_input_batches[0] if dummy_prefill_input_batches else None

_prepare_sampling

_prepare_sampling(
    batch_changed: bool,
    request_ids: Union[None, list[str]] = None,
    pad_to: Optional[int] = None,
    logits_reqs=None,
) -> SamplingMetadata
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _prepare_sampling(self,
                      batch_changed: bool,
                      request_ids: Union[None, list[str]] = None,
                      pad_to: Optional[int] = None,
                      logits_reqs=None) -> SamplingMetadata:
    # Create the sampling metadata.
    req_id_output_token_ids_lst = \
        self._generate_req_id_output_token_ids_lst(request_ids, \
                                                   pad_to, logits_reqs)
    sampling_metadata = self.input_batch.make_selective_sampling_metadata(req_id_output_token_ids_lst,
                                                                          skip_copy=not batch_changed)
    return sampling_metadata

_prepare_spec_decode_inputs

_prepare_spec_decode_inputs(
    scheduler_output,
    logits_indices,
    token_ids_device,
    max_num_sampled_tokens,
)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _prepare_spec_decode_inputs(self, scheduler_output, logits_indices, token_ids_device, max_num_sampled_tokens):
    use_spec_decode = len(scheduler_output.scheduled_spec_decode_tokens) > 0
    if not use_spec_decode:
        spec_decode_metadata = None
    else:
        # Get the number of draft tokens for each request.
        # Iterate over the dictionary rather than all requests since not all
        # requests have draft tokens.
        num_draft_tokens = np.zeros(logits_indices.numel(), dtype=np.int32)
        for req_id, draft_token_ids_in_req in (scheduler_output.scheduled_spec_decode_tokens.items()):
            req_idx = self.input_batch.req_id_to_index[req_id]
            num_draft_tokens[req_idx] = len(draft_token_ids_in_req)

        num_sampled_tokens = num_draft_tokens + 1

        cu_num_sampled_tokens, arange = self._get_cumsum_and_arange(num_sampled_tokens, cumsum_dtype=np.int32)

        logits_indices = []
        bonus_logits_indices = []
        target_logits_indices = []
        for batch_id, n_tokens in enumerate(num_sampled_tokens):
            for i in range(n_tokens - 1):
                logits_indices.append(batch_id * max_num_sampled_tokens + i)
                target_logits_indices.append(batch_id * max_num_sampled_tokens + i)
            bonus_logits_indices.append(batch_id * max_num_sampled_tokens + n_tokens - 1)
            logits_indices.append(batch_id * max_num_sampled_tokens + n_tokens - 1)
            if n_tokens < max_num_sampled_tokens:
                logits_indices.extend([-1] * (max_num_sampled_tokens - n_tokens))
                target_logits_indices.extend([-1] * (max_num_sampled_tokens - n_tokens))
        logits_indices = np.array(logits_indices, dtype=np.int32)
        bonus_logits_indices = np.array(bonus_logits_indices, dtype=np.int32)
        target_logits_indices = np.array(target_logits_indices, dtype=np.int32)

        cu_num_draft_tokens, arange = self._get_cumsum_and_arange(num_draft_tokens, cumsum_dtype=np.int32)

        # TODO: Optimize the CPU -> GPU copy.
        cu_num_draft_tokens = torch.from_numpy(cu_num_draft_tokens).to(self.device, non_blocking=True)
        cu_num_sampled_tokens = torch.from_numpy(cu_num_sampled_tokens).to(self.device, non_blocking=True)

        ##################################################
        logits_indices = torch.from_numpy(logits_indices)
        target_logits_indices_device = \
            torch.from_numpy(target_logits_indices).to(
            self.device, non_blocking=True)
        bonus_logits_indices_device = \
            torch.from_numpy(bonus_logits_indices).to(
            self.device, non_blocking=True)
        draft_token_ids = token_ids_device[target_logits_indices_device + 1]

        spec_decode_metadata = SpecDecodeMetadata(
            draft_token_ids=draft_token_ids,
            num_draft_tokens=num_draft_tokens.tolist(),
            cu_num_draft_tokens=cu_num_draft_tokens,
            cu_num_sampled_tokens=cu_num_sampled_tokens,
            target_logits_indices=target_logits_indices_device,
            bonus_logits_indices=bonus_logits_indices_device,
            logits_indices=logits_indices,
        )
    return logits_indices, spec_decode_metadata

_prepare_unified_decode_inputs

_prepare_unified_decode_inputs(
    num_decodes, num_scheduled_tokens, warmup_mode=False
)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _prepare_unified_decode_inputs(self, num_decodes, num_scheduled_tokens, warmup_mode=False):

    if num_decodes == 0:
        return DecodeInputData(num_decodes=0), None

    context_lens = self.input_batch.num_computed_tokens_cpu[:num_decodes]
    query_lengths = [1] * num_decodes
    prompt_lengths = self.input_batch.num_prompt_tokens[:num_decodes]
    token_ids_cpu = self.input_batch.token_ids_cpu
    block_table_cpu_tensor = self.input_batch.block_table[0].get_cpu_tensor()
    num_blocks = [
        math.ceil((ctx_len + q_len) / self.block_size) for ctx_len, q_len in zip(context_lens, query_lengths)
    ]
    block_table = [block_table_cpu_tensor[i, :nb].tolist() for i, nb in enumerate(num_blocks)]
    if not warmup_mode:
        block_table = self.defragmenter.resolve_all(block_table)
    token_ids = [[token_ids_cpu[i, ctx_len]] for i, ctx_len in enumerate(context_lens)]

    batch_data = create_unified_batch(
        token_ids=token_ids,
        block_size=self.block_size,
        block_table=block_table,
        context_lengths=context_lens,
        query_lengths=[1] * num_decodes,
        prompt_lengths=prompt_lengths,
        dtype=self.dtype,
        contiguous_kv=self.use_contiguous_pa,
        bucketing_fn=self.unified_bucketing_fn,
        get_dp_padding_fn=self.get_dp_padding,
    )
    (token_ids_t, token_positions_t, logits_indices_t, logits_groups, attn_metadata) = batch_data
    decode_input_data = DecodeInputData(
        num_decodes=num_decodes,
        token_ids=token_ids_t.unsqueeze(-1),
        position_ids=token_positions_t.unsqueeze(-1),
        logits_indices=logits_indices_t,
        attn_metadata=attn_metadata,
    )
    return decode_input_data, None

_prepare_unified_prefill_inputs

_prepare_unified_prefill_inputs(
    num_prefills,
    num_decodes,
    num_scheduled_tokens: list[int],
    warmup=False,
) -> tuple[PrefillInputData, None]
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _prepare_unified_prefill_inputs(self,
                                    num_prefills,
                                    num_decodes,
                                    num_scheduled_tokens: list[int],
                                    warmup=False) -> tuple[PrefillInputData, None]:

    all_batch_contents, _ = self._extract_prefill_batch_contents(num_prefills, num_decodes, num_scheduled_tokens,
                                                                 warmup)
    all_batches = [self._form_unified_prefill_batch(bc) for bc in all_batch_contents]
    merge_contents(all_batches[0], *all_batches[1:])
    return all_batches[0], None

_read_profiling_cfg

_read_profiling_cfg()
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _read_profiling_cfg(self):
    prompt_cfg = self._parse_profile_cfg(os.environ.get('VLLM_PROFILE_PROMPT', None))
    decode_cfg = self._parse_profile_cfg(os.environ.get('VLLM_PROFILE_DECODE', None))
    legacy_cfg = self._parse_legacy_profile_cfg(os.environ.get('VLLM_PT_PROFILE', None))
    if legacy_cfg and not (prompt_cfg or decode_cfg):
        phase, bs, seq_or_blocks, use_graphs = legacy_cfg
        assert use_graphs != self.model_config.enforce_eager, \
            "'use_graphs' is out of sync with model config. " \
            "Either change the flag or change vllm engine parameters"
        if phase == 'prompt':
            prompt_cfg = (bs, seq_or_blocks, 0)
        else:
            decode_cfg = (bs, seq_or_blocks)
    # align with current bucketing
    if decode_cfg:
        decode_cfg = (decode_cfg[0], 1, decode_cfg[1])
    return prompt_cfg, decode_cfg

_regional_compilation

_regional_compilation(
    module, parent_module=None, module_name=None
)

递归遍历PyTorch模块并编译其区域,区域可以是以下两种之一:1. nn.ModuleList的子节点 2. regional_compilation_layers_list的成员

Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _regional_compilation(self, module, parent_module=None, module_name=None):
    """
    Recursively traverses a PyTorch module and compiles its regions, which
    can be one of two:
    1. Children of the nn.ModuleList
    2. Member of regional_compilation_layers_list
    """
    if isinstance(module, torch.nn.ModuleList):
        for children_name, children_module in module.named_children():
            self._compile_region(module, children_name, children_module)
    elif any(isinstance(module, layer) for layer in self.regional_compilation_layers_list):
        self._compile_region(
            parent_module,
            module_name,
            module,
        )
    else:
        for children_name, children_module in module.named_children():
            self._regional_compilation(children_module, module, children_name)

_remove_duplicate_submodules

_remove_duplicate_submodules()
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _remove_duplicate_submodules(self):
    model = self.get_model()
    if hasattr(model, "model"):
        for layer in self.get_model().model.layers:
            self_attn = layer.self_attn
            # delete attr kv_b_proj in self_attn,
            # as they have been transferred to the MLAImpl.
            if hasattr(self_attn, "mla_attn"):
                mla_attn = self_attn.mla_attn
                duplicate_mods = [
                    "kv_a_proj_with_mqa",
                    "q_proj",
                    "kv_b_proj",
                    "o_proj",
                    "fused_qkv_a_proj",
                    "q_b_proj",
                ]
                for m in duplicate_mods:
                    if hasattr(self_attn, m) and hasattr(mla_attn, m):
                        delattr(self_attn, m)
                if hasattr(mla_attn, "mla_attn") and hasattr(mla_attn.mla_attn, "impl"):
                    mla_impl = mla_attn.mla_attn.impl
                    duplicate_mods = ["kv_b_proj"]
                    for m in duplicate_mods:
                        if hasattr(mla_attn, m) and hasattr(mla_impl, m):
                            delattr(mla_attn, m)

_run_sampling

_run_sampling(
    batch_changed: bool,
    logits_device: Tensor,
    request_ids: Optional[list[str]] = None,
    pad_to: Optional[int] = None,
    logits_requests=None,
) -> tuple[Tensor, SamplingMetadata]
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _run_sampling(self,
                  batch_changed: bool,
                  logits_device: torch.Tensor,
                  request_ids: Optional[list[str]] = None,
                  pad_to: Optional[int] = None,
                  logits_requests=None) -> tuple[torch.Tensor, SamplingMetadata]:
    htorch.core.mark_step()
    sampling_metadata = self._prepare_sampling(batch_changed, request_ids, pad_to, logits_requests)
    sampler_output = self.sampler(logits=logits_device, sampling_metadata=sampling_metadata)
    htorch.core.mark_step()
    return sampler_output, sampling_metadata

_seq_len

_seq_len(attn_metadata)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _seq_len(self, attn_metadata):
    return attn_metadata.seq_len()

_skip_bucketing

_skip_bucketing(seq_lens, num_blocks)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _skip_bucketing(self, seq_lens, num_blocks):
    return (len(seq_lens), 0, 0)

_update_states

_update_states(scheduler_output: SchedulerOutput) -> bool

使用调度器的输出更新缓存状态和持久化批次。

更新后的状态由_prepare_inputs函数用于创建模型的输入GPU张量。

如果批次中有新请求/已恢复/已暂停/已完成的请求,则会更新SamplingMetadata并将其复制到GPU。

Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _update_states(self, scheduler_output: "SchedulerOutput") -> bool:
    """Update the cached states and the persistent batch with the scheduler
    output.

    The updated states are used by the `_prepare_inputs` function to create
    the input GPU tensors for the model.

    The SamplingMetadata is updated and copied to the GPU if there is a
    new/resumed/paused/finished request in the batch.
    """
    # Remove finished requests from the cached states.
    for req_id in scheduler_output.finished_req_ids:
        self.requests.pop(req_id, None)

    # Remove the finished requests from the persistent batch.
    # NOTE(woosuk): There could be an edge case where finished_req_ids and
    # scheduled_req_ids overlap. This happens when a request is aborted and
    # then resubmitted with the same ID. In this case, we treat them as two
    # distinct requests - clearing the cached states for the first request
    # and handling the second as a new request.
    removed_req_indices: list[int] = []
    for req_id in scheduler_output.finished_req_ids:
        req_index = self.input_batch.remove_request(req_id)
        if req_index is not None:
            removed_req_indices.append(req_index)
        if req_id in self.input_batch.req_type:
            del self.input_batch.req_type[req_id]

    # Free the cached encoder outputs.
    for mm_hash in scheduler_output.free_encoder_mm_hashes:
        self.encoder_cache.pop(mm_hash, None)

    # Remove the unscheduled requests from the persistent batch.
    # NOTE(woosuk): The unscheduled requests are either preempted requests
    # or running requests that are not scheduled in this step. We remove
    # them from the persistent batch but keep their cached states since
    # they will be scheduled again sometime in the future.
    scheduled_req_ids = scheduler_output.num_scheduled_tokens.keys()
    cached_req_ids = self.input_batch.req_id_to_index.keys()
    unscheduled_req_ids = cached_req_ids - scheduled_req_ids
    # NOTE(woosuk): The persistent batch optimization assumes that
    # consecutive batches contain mostly the same requests. If batches
    # have low request overlap (e.g., alternating between two distinct
    # sets of requests), this optimization becomes very inefficient.
    for req_id in unscheduled_req_ids:
        req_index = self.input_batch.remove_request(req_id)
        assert req_index is not None
        removed_req_indices.append(req_index)

    req_ids_to_add: list[str] = []
    # Add new requests to the cached states.
    for new_req_data in scheduler_output.scheduled_new_reqs:
        req_id = new_req_data.req_id
        sampling_params = new_req_data.sampling_params
        pooling_params = new_req_data.pooling_params
        if sampling_params and \
            sampling_params.sampling_type == SamplingType.RANDOM_SEED:
            generator = torch.Generator(device=self.device)
            generator.manual_seed(sampling_params.seed)
        else:
            generator = None
        if pooling_params:
            assert (task := pooling_params.task) is not None, ("You did not set `task` in the API")

            model = cast(VllmModelForPooling, self.model)
            to_update = model.pooler.get_pooling_updates(task)
            assert to_update is not None, (f"{pooling_params.task=} is not supported by the model")
            to_update.apply(pooling_params)

        self.requests[req_id] = CachedRequestState(
            req_id=req_id,
            prompt_token_ids=new_req_data.prompt_token_ids,
            mm_features=new_req_data.mm_features,
            sampling_params=sampling_params,
            pooling_params=pooling_params,
            generator=generator,
            block_ids=new_req_data.block_ids,
            num_computed_tokens=new_req_data.num_computed_tokens,
            output_token_ids=[],
            lora_request=new_req_data.lora_request,
        )

        # Only relevant for models using M-RoPE (e.g, Qwen2-VL)
        if self.uses_mrope:
            self.requests[req_id].mrope_positions, \
                self.requests[req_id].mrope_position_delta = \
                self.model.model.get_mrope_input_positions(
                    self.requests[req_id].prompt_token_ids,
                    self.requests[req_id].mm_features
            )

        req_ids_to_add.append(req_id)
    # Update the states of the running/resumed requests.
    is_last_rank = get_pp_group().is_last_rank
    req_data = scheduler_output.scheduled_cached_reqs
    for i, req_id in enumerate(req_data.req_ids):
        req_state = self.requests[req_id]
        num_computed_tokens = req_data.num_computed_tokens[i]
        new_block_ids = req_data.new_block_ids[i]
        resumed_from_preemption = req_id in getattr(req_data, "resumed_req_ids", set())
        num_output_tokens = req_data.num_output_tokens[i]
        req_state.num_computed_tokens = num_computed_tokens

        if not is_last_rank:
            # When using PP, the scheduler sends the sampled tokens back,
            # because there's no direct communication between the first-
            # stage worker and the last-stage worker.
            new_token_ids = req_data.new_token_ids[i]
            # Add the sampled token(s) from the previous step (if any).
            # This doesn't include "unverified" tokens like spec tokens.
            num_new_tokens = (num_computed_tokens + len(new_token_ids) - req_state.num_tokens)
            if num_new_tokens == 1:
                # Avoid slicing list in most common case.
                req_state.output_token_ids.append(new_token_ids[-1])
            elif num_new_tokens > 0:
                req_state.output_token_ids.extend(new_token_ids[-num_new_tokens:])

        # Update the block IDs.
        if not resumed_from_preemption:
            if new_block_ids is not None:
                # Append the new blocks to the existing block IDs.
                for block_ids, new_ids in zip(req_state.block_ids, new_block_ids):
                    block_ids.extend(new_ids)
        else:
            assert new_block_ids is not None
            # The request is resumed from preemption.
            # Replace the existing block IDs with the new ones.
            req_state.block_ids = new_block_ids

        req_index = self.input_batch.req_id_to_index.get(req_id)
        if req_index is None:
            # The request is not in the persistent batch.
            # The request was either preempted and resumed later, or was not
            # scheduled in the previous step and needs to be added again.

            if self.use_async_scheduling and num_output_tokens > 0:
                # We must recover the output token ids for resumed requests in the
                # async scheduling case, so that correct input_ids are obtained.
                resumed_token_ids = req_data.all_token_ids[req_id]
                req_state.output_token_ids = resumed_token_ids[-num_output_tokens:]

            req_ids_to_add.append(req_id)
            continue

        # Update the persistent batch.
        self.input_batch.num_computed_tokens_cpu[req_index] = (num_computed_tokens)
        if new_block_ids is not None:
            self.input_batch.block_table.append_row(new_block_ids, req_index)

        # For the last rank, we don't need to update the token_ids_cpu
        # because the sampled tokens are already cached.
        if not is_last_rank:
            # Add new_token_ids to token_ids_cpu.
            start_token_index = num_computed_tokens
            end_token_index = num_computed_tokens + len(new_token_ids)
            self.input_batch.token_ids_cpu[req_index, start_token_index:end_token_index] = new_token_ids
            self.input_batch.num_tokens_no_spec[req_index] = end_token_index
            # NOTE(woosuk): `num_tokens` here may include spec decode tokens
            self.input_batch.num_tokens[req_index] = end_token_index
        # Add spec_token_ids to token_ids_cpu.
        spec_token_ids = \
            scheduler_output.scheduled_spec_decode_tokens.get(
                req_id, ())
        if spec_token_ids:
            num_spec_tokens = len(spec_token_ids)
            start_index = self.input_batch.num_tokens_no_spec[req_index]
            end_token_index = start_index + num_spec_tokens
            self.input_batch.token_ids_cpu[req_index, start_index:end_token_index] = spec_token_ids
            # NOTE(woosuk): `num_tokens` here may include spec tokens.
            self.input_batch.num_tokens[req_index] += num_spec_tokens

    # Check if the batch has changed. If not, we can skip copying the
    # sampling metadata from CPU to GPU.
    batch_changed = len(removed_req_indices) > 0 or len(req_ids_to_add) > 0

    # Add the new or resumed requests to the persistent batch.
    # The smaller empty indices are filled first.
    removed_req_indices = sorted(removed_req_indices, reverse=True)
    for req_id in req_ids_to_add:
        req_state = self.requests[req_id]
        req_index = removed_req_indices.pop() if removed_req_indices else None
        self.input_batch.add_request(req_state, req_index)

    # Condense the batched states if there are empty indices.
    if removed_req_indices:
        self.input_batch.condense(removed_req_indices)

    if batch_changed:
        self.input_batch.refresh_sampling_metadata()
    return batch_changed

_use_graphs

_use_graphs()
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _use_graphs(self):
    return not self.model_config.enforce_eager

apply_grammar_bitmask

apply_grammar_bitmask(
    scheduler_output: SchedulerOutput,
    grammar_output: GrammarOutput,
    logits: Tensor,
)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def apply_grammar_bitmask(
    self,
    scheduler_output: "SchedulerOutput",
    grammar_output: GrammarOutput,
    logits: torch.Tensor,
):

    grammar_bitmask = grammar_output.grammar_bitmask

    # We receive the structured output bitmask from the scheduler,
    # compacted to contain bitmasks only for structured output requests.
    # The order of the requests in the bitmask is not guaranteed to be the
    # same as the order of the requests in the gpu runner's batch. We need
    # to sort the bitmask to match the order of the requests used here.

    # Get the batch indices of the structured output requests.
    # Keep track of the number of speculative tokens scheduled for every
    # request in the batch, as the logit indices are offset by this amount.
    struct_out_req_batch_indices: dict[str, int] = {}
    cumulative_offset = 0
    seq = sorted(self.input_batch.req_id_to_index.items(), key=lambda x: x[1])
    for req_id, batch_index in seq:
        logit_index = batch_index + cumulative_offset
        cumulative_offset += len(scheduler_output.scheduled_spec_decode_tokens.get(req_id, []))
        if req_id in grammar_output.structured_output_request_ids:
            struct_out_req_batch_indices[req_id] = logit_index

    out_indices = []

    # Reorder the bitmask to match the order of the requests in the batch.
    sorted_bitmask = np.zeros_like(grammar_bitmask, shape=(logits.shape[0], grammar_bitmask.shape[1]))
    cumulative_index = 0

    for req_id in grammar_output.structured_output_request_ids:
        logit_index = struct_out_req_batch_indices[req_id]
        num_spec_tokens = len(scheduler_output.scheduled_spec_decode_tokens.get(req_id, []))
        for i in range(1 + num_spec_tokens):
            sorted_bitmask[logit_index + i] = \
                grammar_bitmask[cumulative_index + i]
            out_indices.append(logit_index + i)
        cumulative_index += 1 + num_spec_tokens

    # Copy async to device as tensor.
    grammar_bitmask = torch.from_numpy(sorted_bitmask).to(logits.device, non_blocking=True)

    # If the grammar bitmask and the logits have the same shape
    # we don't need to pass indices to the kernel,
    # since the bitmask is already aligned with the logits.
    skip_out_indices = grammar_bitmask.shape[0] == logits.shape[0]

    index_tensor = None
    if not skip_out_indices:
        # xgrammar expects a python list of indices but it will actually work with
        # a tensor. If we copy the tensor ourselves here we can do it in a non_blocking
        # manner and there should be no cpu sync within xgrammar.
        index_tensor = torch.tensor(out_indices, dtype=torch.int32, device="cpu", pin_memory=True)
        index_tensor = index_tensor.to(logits.device, non_blocking=True)

    # Serialization of np.ndarray is much more efficient than a tensor,
    # so we receive it in that format.
    #grammar_bitmask = torch.from_numpy(grammar_bitmask).contiguous()

    # Force use of the torch.compile implementation from xgrammar to work
    # around issues with the Triton kernel in concurrent structured output
    # scenarios. See PR #19565 and issues #19493, #18376 for details.

    # xgr_torch_compile.apply_token_bitmask_inplace_torch_compile(
    #     logits,
    #     grammar_bitmask.to(self.device, non_blocking=True),
    #     indices=out_indices if not skip_out_indices else None,
    # )

    # NOTE(tianmu-li): xgr_torch_compile uses torch.inductor by default.
    # Have to use the CPU backend, which has its overhead.
    logits_cpu = logits.cpu().to(torch.float32)
    '''xgr_cpu.apply_token_bitmask_inplace_cpu(
        logits_cpu,
        grammar_bitmask.to("cpu"),
        indices=out_indices if not skip_out_indices else None,
    )'''
    xgr_cpu.apply_token_bitmask_inplace_cpu(logits_cpu, grammar_bitmask.to("cpu"), indices=index_tensor)
    logits.copy_(logits_cpu.to(self.device, non_blocking=True).to(logits.dtype))

create_lora_mask

create_lora_mask(
    input_tokens: Tensor,
    lora_ids: list[int],
    is_prompt: bool,
)

这是为lora计算创建掩码的辅助函数。lora掩码用于确保我们为请求匹配正确的lora权重。对于Prompt阶段,我们有lora_mask,形状为(batch_size * seq_len, max_loras * max_rank),lora_logits_mask,形状为(batch_size, max_loras * max_rank)。对于Decode阶段,我们同时拥有lora_mask和lora_logits_mask,形状均为(batch_size, max_loras * max_rank)。

Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def create_lora_mask(self, input_tokens: torch.Tensor, lora_ids: list[int], is_prompt: bool):
    '''
    This is a helper function to create the mask for lora computations.
    Lora Mask is needed to ensure we match the correct lora weights for the
    for the request.
    For Prompt phase we have
    lora_mask with shape (batch_size * seq_len, max_loras * max_rank)
    lora_logits_mask with shape (batch_size, max_loras * max_rank)
    For Decode phase we have both
    lora_mask and lora_logits_mask with shape
    (batch_size, max_loras * max_rank)
    '''
    lora_mask: torch.Tensor = None
    lora_logits_mask: torch.Tensor = None
    lora_index = 0

    if self.lora_config:
        if is_prompt:
            lora_mask = torch.zeros(
                input_tokens.shape[0] * input_tokens.shape[1],
                (self.lora_config.max_loras) *\
                    self.lora_config.max_lora_rank,
                dtype=self.lora_config.lora_dtype)
            lora_logits_mask = torch.zeros(input_tokens.shape[0],
                                           (self.lora_config.max_loras) * self.lora_config.max_lora_rank,
                                           dtype=self.lora_config.lora_dtype)

            ones = torch.ones(input_tokens.shape[1],
                              self.lora_config.max_lora_rank,
                              dtype=self.lora_config.lora_dtype)
            logit_ones = torch.ones(1, self.lora_config.max_lora_rank, dtype=self.lora_config.lora_dtype)

            for i in range(len(lora_ids)):
                if lora_ids[i] == 0:
                    continue
                lora_index = self.lora_manager._adapter_manager.\
                    lora_index_to_id.index(lora_ids[i])
                start_row = i * input_tokens.shape[1]
                end_row = start_row + input_tokens.shape[1]
                start_col = lora_index * self.lora_config.max_lora_rank
                end_col = start_col + self.lora_config.max_lora_rank
                lora_mask[start_row:end_row, start_col:end_col] = ones
                lora_logits_mask[i, start_col:end_col] = logit_ones
            lora_mask = lora_mask.to('hpu')
            lora_logits_mask = lora_logits_mask.to('hpu')
        else:
            lora_mask = torch.zeros(input_tokens.shape[0],
                                    (self.lora_config.max_loras) * self.lora_config.max_lora_rank,
                                    dtype=self.lora_config.lora_dtype)
            ones = torch.ones(1, self.lora_config.max_lora_rank, dtype=self.lora_config.lora_dtype)
            for i in range(len(lora_ids)):
                if lora_ids[i] == 0:
                    continue
                lora_index = self.lora_manager._adapter_manager.\
                    lora_index_to_id.index(lora_ids[i])
                start_pos = lora_index * self.lora_config.max_lora_rank
                end_pos = start_pos + self.lora_config.max_lora_rank
                lora_mask[i, start_pos:end_pos] = ones
            lora_mask = lora_mask.to('hpu')
            lora_logits_mask = lora_mask

    return lora_mask, lora_logits_mask

distribute_sum_evenly

distribute_sum_evenly(total_sum, max_length)

返回一个总和为total_sum的整数平衡列表。列表长度不能超过max_length。

Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def distribute_sum_evenly(self, total_sum, max_length):
    '''
    Return a balanced list of ints that sums up to total_sum.
    List cannot be longer than max_length.
    '''
    base, remain = divmod(total_sum, max_length)
    result = [base] * max_length

    for i in range(remain):
        result[i] += 1

    return result

execute_model

execute_model(
    scheduler_output: SchedulerOutput,
    warmup_mode: bool = False,
) -> ModelRunnerOutput | None
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
@torch.inference_mode()
def execute_model(
    self,
    scheduler_output: "SchedulerOutput",
    warmup_mode: bool = False,
) -> ModelRunnerOutput | None:

    self.run_defragmenter(scheduler_output, warmup_mode)

    batch_changed = self._update_states(scheduler_output)
    if not scheduler_output.total_num_scheduled_tokens:
        if not has_kv_transfer_group() or warmup_mode:
            # Return empty ModelRunnerOuptut if there's no work to do.
            return EMPTY_MODEL_RUNNER_OUTPUT
        # For D case, wait until kv finish load here
        return self.kv_connector_no_forward(scheduler_output, self.vllm_config)
    self.scheduler_output = scheduler_output
    self.warmup_mode = warmup_mode
    self.batch_changed = batch_changed

    return None

get_habana_paged_attn_buffers

get_habana_paged_attn_buffers(
    block_tables, slot_mapping, batch_size
)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def get_habana_paged_attn_buffers(self, block_tables, slot_mapping, batch_size):
    last_block_usage = [slot[0] % self.block_size + 1 for slot in slot_mapping]
    block_groups = [[i] * len(bt) for i, bt in enumerate(block_tables)]
    block_usage = [[self.block_size] * (len(bt) - 1) + [lbu] for bt, lbu in zip(block_tables, last_block_usage)
                   if bt]
    block_list = flatten(block_tables)
    block_groups = flatten(block_groups)
    block_usage = flatten(block_usage)
    assert len(block_list) == len(block_groups)
    assert len(block_list) == len(block_usage)

    padding_fn = None
    block_bucket_size: int
    if self.use_contiguous_pa:
        block_bucket_size = max(max(block_list) + 1, len(block_list))
        block_bucket_size = \
            self.bucketing_manager.find_decode_bucket(batch_size,
                                                      block_bucket_size)[2]
        block_bucket_size += self.get_dp_padding(block_bucket_size)

        indices: list[Any]
        indices = [None] * block_bucket_size
        for i, bid in enumerate(block_list):
            indices[bid] = i

        def padding_fn(tensor, pad_value):
            return gather_list(tensor, indices, pad_value)
    else:
        block_bucket_size = \
            self.bucketing_manager.find_decode_bucket(batch_size,
                                                      len(block_list))[2]
        block_bucket_size += self.get_dp_padding(block_bucket_size)

        def padding_fn(tensor, pad_value):
            return pad_list(tensor, block_bucket_size, itertools.repeat(pad_value))

    block_list = padding_fn(block_list, self._PAD_BLOCK_ID)
    block_groups = padding_fn(block_groups, -1)
    block_usage = padding_fn(block_usage, 1)

    block_list = torch.tensor(block_list, dtype=torch.long, device='cpu')
    block_groups = torch.tensor(block_groups, dtype=torch.long, device='cpu')
    block_usage = torch.tensor(block_usage, dtype=self.model_config.dtype, device='cpu')
    return block_list, block_groups, block_usage

get_kv_cache_spec

get_kv_cache_spec() -> dict[str, KVCacheSpec]

通过解析静态前向上下文中的每个Attention模块的KV缓存格式来生成KVCacheSpec。返回:KVCacheSpec:一个字典,将层名映射到它们的KV缓存格式。不需要KV缓存的层不包含在内。

Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def get_kv_cache_spec(self) -> dict[str, KVCacheSpec]:
    """
    Generates the KVCacheSpec by parsing the kv cache format from each
    Attention module in the static forward context.
    Returns:
        KVCacheSpec: A dictionary mapping layer names to their KV cache
        format. Layers that do not need KV cache are not included.
    """

    forward_ctx = self.vllm_config.compilation_config.static_forward_context
    block_size = self.vllm_config.cache_config.block_size
    kv_cache_spec: dict[str, KVCacheSpec] = {}
    cache_dtype_str = self.vllm_config.cache_config.cache_dtype
    for layer_name, attn_module in forward_ctx.items():
        if isinstance(attn_module, FusedMoE):
            continue

        # TODO: Support other attention modules, e.g., sliding window,
        # cross-attention
        if isinstance(attn_module, Attention):
            if attn_module.attn_type == AttentionType.DECODER:
                kv_cache_spec[layer_name] = FullAttentionSpec(block_size=block_size,
                                                              num_kv_heads=attn_module.num_kv_heads,
                                                              head_size=attn_module.head_size,
                                                              dtype=self.kv_cache_dtype)
            elif attn_module.attn_type in (AttentionType.ENCODER, AttentionType.ENCODER_ONLY):
                # encoder-only attention does not need KV cache.
                continue
            elif attn_module.attn_type == AttentionType.ENCODER_DECODER:
                raise NotImplementedError
            else:
                raise ValueError(f"Unknown attention type: {attn_module.attn_type}")
        elif isinstance(attn_module, MLAAttention):
            if layer_name in kv_cache_spec:
                continue
            kv_cache_spec[layer_name] = MLAAttentionSpec(
                block_size=block_size,
                num_kv_heads=1,
                head_size=attn_module.head_size,
                dtype=self.kv_cache_dtype,
                cache_dtype_str=cache_dtype_str,
            )

    return kv_cache_spec

get_kv_caches_4D

get_kv_caches_4D(kv_caches) -> dict[str, Tensor]
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def get_kv_caches_4D(self, kv_caches) -> dict[str, torch.Tensor]:
    kv_caches_4D: dict[str, torch.Tensor] = {}
    for layer_name, cache_or_cachelist in kv_caches.items():
        kv_cache_per_layer = []
        for cache in cache_or_cachelist:
            if cache is None:
                continue
            kv_cache_per_layer.append(cache.view(-1, self.block_size, *cache.shape[1:]))
            #NOTE(Chendi): Do not remove, call torch data_ptr to record physical address
            cache.data_ptr()
        kv_caches_4D[layer_name] = TensorTuple(tuple(kv_cache_per_layer)) \
            if len(kv_cache_per_layer) == 2 else kv_cache_per_layer[0]
    return kv_caches_4D

get_merged_prefill_seq_lens

get_merged_prefill_seq_lens(query_len, ctx_blocks)

从合并的布局中获取独立的序列长度到单个样本。返回序列长度(包括查询和上下文)以及上下文长度的列表。

Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def get_merged_prefill_seq_lens(self, query_len, ctx_blocks):
    '''
    Get seperate sequence lengths from merged layout to individual 
    samples.
    Returns list of sequence length (including query and context) and
    context lengths.
    '''
    ctx_list = self.distribute_sum_evenly(ctx_blocks, self.max_num_seqs)
    query_list = self.distribute_sum_evenly(query_len, self.max_num_seqs)
    prompt_list = [q + c * self.block_size for q, c in zip(query_list, ctx_list)]
    ctx_list = ctx_list if len(ctx_list) > 0 else [0] * len(prompt_list)
    return prompt_list, ctx_list

get_model

get_model() -> Module
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def get_model(self) -> torch.nn.Module:
    if isinstance(self.model, HpuModelAdapter):
        return self.model.model
    assert self.model is not None
    return self.model

get_supported_generation_tasks

get_supported_generation_tasks() -> list[GenerationTask]
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def get_supported_generation_tasks(self) -> list[GenerationTask]:
    model = self.get_model()
    supported_tasks = list[GenerationTask]()

    if is_text_generation_model(model):
        supported_tasks.append("generate")

    if supports_transcription(model):
        if model.supports_transcription_only:
            return ["transcription"]

        supported_tasks.append("transcription")

    return supported_tasks

get_supported_pooling_tasks

get_supported_pooling_tasks() -> list[PoolingTask]
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def get_supported_pooling_tasks(self) -> list[PoolingTask]:
    model = self.get_model()
    if not is_pooling_model(model):
        return []

    return list(model.pooler.get_supported_tasks())

get_supported_tasks

get_supported_tasks() -> tuple[SupportedTask, ...]
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def get_supported_tasks(self) -> tuple[SupportedTask, ...]:
    tasks = list[SupportedTask]()

    if self.model_config.runner_type == "generate":
        tasks.extend(self.get_supported_generation_tasks())
    if self.model_config.runner_type == "pooling":
        tasks.extend(self.get_supported_pooling_tasks())

    return tuple(tasks)

initialize_kv_cache

initialize_kv_cache(kv_cache_config: KVCacheConfig) -> None

根据kv_cache_config初始化KV缓存。参数:kv_cache_config:KV缓存的配置,包括每个层的KV缓存大小

Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def initialize_kv_cache(self, kv_cache_config: KVCacheConfig) -> None:
    """
    Initialize KV cache based on `kv_cache_config`.
    Args:
        kv_cache_config: Configuration for the KV cache, including the KV
        cache size of each layer
    """
    if len(kv_cache_config.kv_cache_groups) > 1:
        raise NotImplementedError("Hybrid models with more than one KV cache type are not "
                                  "supported yet.")

    # build a map from layer_name -> KVCacheTensor
    tensor_map: dict[str, KVCacheTensor] = {}
    for tensor in kv_cache_config.kv_cache_tensors:
        for lname in tensor.shared_by:
            tensor_map[lname] = tensor

    kv_caches: dict[str, torch.Tensor] = {}
    kv_cache_sizes = {}
    for kv_cache_tensor in kv_cache_config.kv_cache_tensors:
        assert len(kv_cache_tensor.shared_by) == 1
        kv_cache_sizes[kv_cache_tensor.shared_by[0]] = kv_cache_tensor.size

    for kv_cache_group in kv_cache_config.kv_cache_groups:
        kv_cache_spec = kv_cache_group.kv_cache_spec
        for kv_cache_tensor in kv_cache_config.kv_cache_tensors:
            assert kv_cache_tensor.size % kv_cache_spec.page_size_bytes == 0
            num_blocks = \
                kv_cache_tensor.size // kv_cache_spec.page_size_bytes
            # `num_blocks` is the number of blocks the model runner can use.
            # `kv_cache_config.num_blocks` is the number of blocks that
            # KVCacheManager may allocate.
            # Since different GPUs may have different number of layers and
            # different memory capacities, `num_blocks` can be different on
            # different GPUs, and `kv_cache_config.num_blocks` is set to
            # the min of all `num_blocks`. Verify it here.
            assert num_blocks >= kv_cache_config.num_blocks
            if isinstance(kv_cache_spec, FullAttentionSpec):
                kv_cache_shape = self.attn_backend.get_kv_cache_shape(num_blocks + 1, kv_cache_spec.block_size,
                                                                      kv_cache_spec.num_kv_heads,
                                                                      kv_cache_spec.head_size)
                v_cache_shape = None if self.model_config.use_mla \
                    else kv_cache_shape
                dtype = kv_cache_spec.dtype
                key_cache = torch.zeros(kv_cache_shape, dtype=dtype, device=self.device)
                if v_cache_shape is not None:
                    value_cache = torch.zeros(v_cache_shape, dtype=dtype, device=self.device)
                else:
                    value_cache = None
                for layer_name in kv_cache_tensor.shared_by:
                    kv_caches[layer_name] = (key_cache, value_cache)
            else:
                # TODO: add new branches when introducing more types of
                # KV cache specs.
                raise ValueError("Unknown KV cache spec type.")
        layer_names = set()
        for group in kv_cache_config.kv_cache_groups:
            layer_names.update(group.layer_names)
        assert layer_names == set(kv_caches.keys()), "Some layers are not correctly initialized"
    bind_kv_cache(kv_caches, self.vllm_config.compilation_config.static_forward_context, self.kv_caches)

    if self.enable_bucketing:
        self.bucketing_manager.num_hpu_blocks = num_blocks
    self._PAD_BLOCK_ID = num_blocks
    self._PAD_SLOT_ID = num_blocks * self.block_size

    if has_kv_transfer_group():
        get_kv_transfer_group().register_kv_caches(self.get_kv_caches_4D(kv_caches))
        if self.vllm_config.kv_transfer_config.kv_buffer_device == "cpu":
            get_kv_transfer_group().set_host_xfer_buffer_ops(copy_kv_blocks)
        global hpu_buffer
    if self.unified_attn:
        with HabanaMemoryProfiler() as m:
            from vllm_gaudi.extension.unified_batch import UnifiedBatchPersistentContext
            max_num_shared_blocks = math.ceil(num_blocks * get_config().unified_attn_shared_cache_ratio)
            self.unified_attn_persistent_ctx = UnifiedBatchPersistentContext(self.max_num_batched_tokens,
                                                                             max_num_shared_blocks, num_blocks,
                                                                             self.block_size, dtype)
        logger.info("Allocating unified persistent batch took %.4f GB of host memory",
                    m.consumed_host_memory / float(2**30))

    htorch.hpu.synchronize()

is_decoder_only

is_decoder_only(req_id) -> bool
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def is_decoder_only(self, req_id) -> bool:
    return bool(req_id in self.input_batch.req_type and \
        self.input_batch.req_type[req_id] == "decode")

load_lora_model

load_lora_model(
    model: Module, vllm_config: VllmConfig, device: str
) -> Module
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def load_lora_model(self, model: nn.Module, vllm_config: VllmConfig, device: str) -> nn.Module:
    if not supports_lora(model):
        raise ValueError(f"{model.__class__.__name__} does not support LoRA yet.")

    if supports_multimodal(model):
        logger.warning("Regarding multimodal models, vLLM currently "
                       "only supports adding LoRA to language model.")

    # Add LoRA Manager to the Model Runner
    self.lora_manager = LRUCacheWorkerLoRAManager(
        vllm_config,
        device,
        model.embedding_modules,
        model.embedding_padding_modules,
    )
    return self.lora_manager.create_lora_manager(model)

load_model

load_model() -> None
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def load_model(self) -> None:
    import habana_frameworks.torch.core as htcore
    if self.model_config.quantization == 'inc' or \
            self.model_config.quantization == 'fp8':
        htcore.hpu_set_env()
    logger.info("Starting to load model %s...", self.model_config.model)
    with HabanaMemoryProfiler() as m:  # noqa: SIM117
        self.model = get_model(vllm_config=self.vllm_config)
        if self.lora_config:
            self.model = self.load_lora_model(self.model, self.vllm_config, self.device)
    self.model_memory_usage = m.consumed_device_memory
    logger.info("Loading model weights took %.4f GB", self.model_memory_usage / float(2**30))

    if self._is_quant_with_inc():
        logger.info("Preparing model with INC..")
        with HabanaMemoryProfiler() as m_inc:
            from neural_compressor.torch.quantization import (FP8Config, convert, prepare)
            config = FP8Config.from_json_file(os.getenv("QUANT_CONFIG", ""))
            disable_mark_scales_as_const = os.getenv("VLLM_DISABLE_MARK_SCALES_AS_CONST", "false") in ("1", "true")
            self._inc_preprocess()
            if config.measure:
                self.model = prepare(self.model, config)
            elif config.quantize:
                self.model = convert(self.model, config)
            else:
                raise ValueError("Unknown quantization config mode,"
                                 "please validate quantization config file")
            if not disable_mark_scales_as_const:
                htcore.hpu_initialize(self.model, mark_only_scales_as_const=True)
        self.inc_initialized_successfully = True
        self.model_memory_usage = m_inc.consumed_device_memory
        logger.info("Preparing model with INC took %.4f GB", self.model_memory_usage / float(2**30))
    elif not is_fake_hpu():
        self.model = self.model.to("hpu")
        htcore.mark_step()
    self.maybe_set_chunked_attention_layers(self.model)
    hidden_layer_markstep_interval = int(os.getenv('VLLM_CONFIG_HIDDEN_LAYERS', '1'))
    model_config = getattr(self.model, "config", None)
    modify_model_layers(self.model,
                        get_target_layer_suffix_list(model_config.model_type if model_config is not None else None),
                        hidden_layer_markstep_interval)
    torch.hpu.synchronize()

    if not self.is_pooling_model:
        with HabanaMemoryProfiler() as m:
            self.model = _maybe_wrap_in_hpu_graph(
                self.model,
                vllm_config=self.vllm_config,
            )
    self.model_memory_usage = m.consumed_device_memory
    logger.info("Wrapping in HPUGraph took %.4f GB", self.model_memory_usage / float(2**30))

    ########### Spec Decode model ############
    if hasattr(self, "drafter"):
        with HabanaMemoryProfiler() as m:  # noqa: SIM117
            logger.info("Loading drafter model %s...", self.vllm_config.speculative_config.draft_model_config)
            self.drafter.load_model(self.model.model)
            if self.use_aux_hidden_state_outputs:
                if supports_eagle3(self.model.model):
                    self.model.model.set_aux_hidden_state_layers(
                        self.model.model.get_eagle3_aux_hidden_state_layers())
                else:
                    raise RuntimeError("Model does not support EAGLE3 interface but "
                                       "aux_hidden_state_outputs was requested")
        self.model_memory_usage = m.consumed_device_memory
        logger.info("Loading drafter model weights took %.4f GB", self.model_memory_usage / float(2**30))
        if hasattr(self.drafter, "model"):
            self.drafter.model = self.drafter.model.to("hpu")
            torch.hpu.synchronize()
            with HabanaMemoryProfiler() as m:  # noqa: SIM117
                self.drafter.model = _maybe_wrap_in_hpu_graph(self.drafter.model, vllm_config=self.vllm_config)
            self.model_memory_usage = m.consumed_device_memory
            logger.info("Wrapping in HPUGraph took %.4f GB", self.model_memory_usage / float(2**30))
    #############################################

    with HabanaMemoryProfiler() as m:
        self._maybe_compile(self.model)
    self.model_memory_usage = m.consumed_device_memory
    logger.info("Compilation took %.4f GB", self.model_memory_usage / float(2**30))

log_graph_warmup_summary

log_graph_warmup_summary(buckets, is_prompt, total_mem)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def log_graph_warmup_summary(self, buckets, is_prompt, total_mem):
    phase = f'Graph/{"Prompt" if is_prompt else "Decode"}'
    msg = (f'{phase} captured:{len(buckets)} '
           f'used_mem:{format_bytes(total_mem)}')
    logger.info(msg)

log_warmup

log_warmup(
    phase,
    i,
    max_i,
    first_dim,
    second_dim,
    third_dim,
    causal=False,
)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def log_warmup(self, phase, i, max_i, first_dim, second_dim, third_dim, causal=False):
    free_mem = format_bytes(HabanaMemoryProfiler.current_free_device_memory())
    if self.unified_attn:
        msg = (f"[Warmup][{phase}][{i + 1}/{max_i}] "
               f"query_len:{first_dim} "
               f"shared_blocks:{second_dim} "
               f"unique_blocks:{third_dim} "
               f"({'causal' if causal else 'non causal'}) "
               f"free_mem:{free_mem}")
    else:
        msg = (f"[Warmup][{phase}][{i + 1}/{max_i}] "
               f"batch_size:{first_dim} "
               f"query_len:{second_dim} "
               f"num_blocks:{third_dim} "
               f"free_mem:{free_mem}")
    tqdm.write(msg)

log_warmup_multimodal

log_warmup_multimodal(
    phase, i, max_i, batch_size, seq_len, img_args
)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def log_warmup_multimodal(self, phase, i, max_i, batch_size, seq_len, img_args):
    free_mem = format_bytes(HabanaMemoryProfiler.current_free_device_memory())
    msg = (f"[Warmup][{phase}][{i+1}/{max_i}] "
           f"batch_size:{batch_size} "
           f"seq_len:{seq_len} "
           f"img_args:{img_args} "
           f"free_mem:{free_mem}")
    logger.info(msg)

maybe_set_chunked_attention_layers

maybe_set_chunked_attention_layers(model)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def maybe_set_chunked_attention_layers(self, model):
    if hasattr(model.config, 'text_config'):  # noqa: SIM102
        if hasattr(model.config.text_config, 'attention_chunk_size'):  # noqa: SIM102
            if model.config.text_config.attention_chunk_size > 0:
                self.model_has_chunked_attention = True
                try:
                    for layer in model.language_model.model.layers:
                        if "ChunkedLocalAttention" in layer.self_attn.attn.get_attn_backend().__name__:
                            layer.self_attn.attn.impl.is_chunked_attention = True
                except Exception:
                    pass

prepare_unified_batch

prepare_unified_batch(scheduler_output)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def prepare_unified_batch(self, scheduler_output):
    num_reqs = len(self.input_batch.req_ids)
    num_computed_tokens = self.input_batch.num_computed_tokens_cpu_tensor[:num_reqs]
    num_prompt_tokens = torch.from_numpy(self.input_batch.num_prompt_tokens[:num_reqs])
    num_scheduled_tokens = torch.tensor(
        [scheduler_output.num_scheduled_tokens[req_id] for req_id in self.input_batch._req_ids],
        dtype=torch.int32,
        device='cpu')
    max_seq = (num_computed_tokens + num_scheduled_tokens).max()
    max_blocks = (max_seq + self.block_size - 1) // self.block_size
    all_token_ids = self.input_batch.token_ids_cpu_tensor[:num_reqs, :max_seq]
    # TODO: check if it's safe to always slice on first dim
    block_table = self.input_batch.block_table[0].get_cpu_tensor()[:num_reqs, :max_blocks].clone().to(torch.int64)
    if self.defragmenter.enabled:
        block_table.apply_(self.defragmenter.resolve)

    return create_unified_batch(self.input_batch.req_ids, all_token_ids, num_computed_tokens, num_scheduled_tokens,
                                num_prompt_tokens, block_table, self.block_size, self.dtype,
                                self.unified_attn_persistent_ctx, self.unified_bucketing_fn, self.get_dp_padding)

profile_run

profile_run() -> None
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
@torch.inference_mode()
def profile_run(self) -> None:
    return

propose_draft_token_ids

propose_draft_token_ids(
    scheduler_output: SchedulerOutput,
    sampled_token_ids: list[list[int]],
    prefill_sampled_token_ids_tensor: Tensor,
    decode_sampled_token_ids_tensor: Tensor,
    sampling_metadata: SamplingMetadata,
    hidden_states: Tensor,
    sample_hidden_states: Tensor,
    aux_hidden_states: Optional[Tensor],
    hidden_states_prefills: list[Tensor],
    sample_hidden_states_prefills: list[Tensor],
    aux_hidden_states_prefills: list[Optional[Tensor]],
    num_decodes: int,
    prefill_data: Optional[PrefillInputData] = None,
    decode_data: Optional[DecodeInputData] = None,
) -> Union[list[list[int]], Tensor]
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def propose_draft_token_ids(
    self,
    scheduler_output: "SchedulerOutput",
    sampled_token_ids: list[list[int]],
    prefill_sampled_token_ids_tensor: torch.Tensor,
    decode_sampled_token_ids_tensor: torch.Tensor,
    sampling_metadata: SamplingMetadata,
    hidden_states: torch.Tensor,
    sample_hidden_states: torch.Tensor,
    aux_hidden_states: Optional[torch.Tensor],
    hidden_states_prefills: list[torch.Tensor],
    sample_hidden_states_prefills: list[torch.Tensor],
    aux_hidden_states_prefills: list[Optional[torch.Tensor]],
    num_decodes: int,
    prefill_data: Optional[PrefillInputData] = None,
    decode_data: Optional[DecodeInputData] = None,
) -> Union[list[list[int]], torch.Tensor]:
    if self.speculative_config.method == "ngram":
        assert isinstance(self.drafter, NgramProposer)
        draft_token_ids = self.propose_ngram_draft_token_ids(sampled_token_ids)
    elif self.speculative_config.use_eagle():
        assert isinstance(self.drafter, EagleProposer)

        def execute_drafter_model(target_token_ids, target_positions, target_hidden_states, last_token_indices,
                                  common_attn_metadata):
            if self.drafter.method == "eagle3":
                assert isinstance(self.drafter.model.model, Eagle3LlamaForCausalLM)
                target_hidden_states = \
                    self.drafter.model.model.combine_hidden_states(
                    target_hidden_states)
                assert target_hidden_states.shape[-1] == self.hidden_size
            #htorch.core.mark_step()
            ret_hidden_states = self.drafter.model(
                input_ids=target_token_ids,
                positions=target_positions,
                hidden_states=target_hidden_states,
                inputs_embeds=None,
                attn_metadata=common_attn_metadata,
            )
            #htorch.core.mark_step()
            if self.drafter.method in ("deepseek_mtp", "ernie_mtp"):
                last_hidden_states = ret_hidden_states
                hidden_states = last_hidden_states
            else:
                last_hidden_states, hidden_states = ret_hidden_states
            last_hidden_states = last_hidden_states.view(-1, last_hidden_states.shape[-1])
            sample_hidden_states = last_hidden_states[last_token_indices]
            logits = self.drafter.model.compute_logits(sample_hidden_states)
            draft_token_ids = logits.argmax(dim=-1)
            return draft_token_ids, hidden_states

        draft_token_ids = None
        if decode_data is not None:
            assert decode_data.spec_decode_metadata is not None
            assert decode_data.position_ids is not None
            num_draft_tokens = \
                decode_data.spec_decode_metadata.num_draft_tokens
            max_num_draft_tokens = max(num_draft_tokens)
            common_attn_metadata = decode_data.attn_metadata

            num_picked_token_indices = []
            last_token_indices = []
            starting_index = 0
            num_rejected_tokens = [
                n + 1 - len(sampled_token_ids[i]) if n > 0 else 0 for i, n in enumerate(num_draft_tokens)
            ]
            for i, n in enumerate(num_draft_tokens):
                r = num_rejected_tokens[i]
                step = max_num_draft_tokens + 1
                for j in range(step):
                    if j == n - r:
                        last_token_indices.append(starting_index + j)
                    if j < n + 1 - r:
                        num_picked_token_indices.append(starting_index + j)
                    else:
                        num_picked_token_indices.append(-1)
                starting_index += step
            hidden_states_indices = torch.tensor(num_picked_token_indices, device=self.device)
            last_token_indices = torch.tensor(last_token_indices, device=self.device)

            target_token_ids = decode_sampled_token_ids_tensor.reshape(-1, 1)[hidden_states_indices]
            target_positions = decode_data.position_ids[hidden_states_indices]

            if self.use_aux_hidden_state_outputs and \
                aux_hidden_states is not None:
                target_hidden_states = torch.cat([h[hidden_states_indices] for h in aux_hidden_states], dim=-1)
            else:
                target_hidden_states = hidden_states[hidden_states_indices]

            if target_hidden_states.dim() == 2:
                target_hidden_states = target_hidden_states.unsqueeze(1)
            draft_token_ids, hidden_states = execute_drafter_model(target_token_ids, target_positions,
                                                                   target_hidden_states, last_token_indices,
                                                                   common_attn_metadata)

            draft_token_ids = draft_token_ids[:num_decodes]
        # handle prefill
        if prefill_data is not None:
            # Currently, prefill is done one by one
            draft_token_ids_prefill = []
            hidden_states_prefill = []

            for idx, (req_id, prompt_len, token_ids, position_ids, attn_metadata, logits_indices,
                      logits_requests) in enumerate(zip(*shallow_tuple(prefill_data))):
                hidden_states = hidden_states_prefills[idx]
                if self.use_aux_hidden_state_outputs:
                    aux_hidden_states = aux_hidden_states_prefills[idx]
                    target_hidden_states = torch.cat(aux_hidden_states, dim=-1)
                else:
                    target_hidden_states = hidden_states
                next_token_ids = prefill_sampled_token_ids_tensor[idx]
                # Follow GPU to shift input_tokens by one to the left
                # to match hidden_states
                token_ids = token_ids.squeeze()
                target_token_ids = token_ids.clone()
                target_token_ids[:-1].copy_(token_ids[1:])
                target_token_ids[logits_indices] = next_token_ids
                target_token_ids = target_token_ids.unsqueeze(0)
                if target_hidden_states.dim() == 2:
                    target_hidden_states = target_hidden_states.unsqueeze(0)
                _draft_token_ids, _hidden_states = execute_drafter_model(target_token_ids, position_ids,
                                                                         target_hidden_states, logits_indices,
                                                                         attn_metadata)
                draft_token_ids_prefill.append(_draft_token_ids)
                hidden_states_prefill.append(_hidden_states)
            if draft_token_ids is None:
                draft_token_ids = torch.cat(draft_token_ids_prefill, dim=0)
                hidden_states = torch.cat(hidden_states_prefill, dim=0)
            else:
                draft_token_ids = torch.cat([draft_token_ids] + draft_token_ids_prefill, dim=0)
                hidden_states = torch.cat([hidden_states] + hidden_states_prefill, dim=0)

        # Early exit if there is only one draft token to be generated.
        # [batch_size, 1]

        if self.speculative_config.num_speculative_tokens == 1:
            return draft_token_ids.view(-1, 1)  # type: ignore

    return draft_token_ids

propose_ngram_draft_token_ids

propose_ngram_draft_token_ids(
    sampled_token_ids: list[list[int]],
) -> list[list[int]]
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def propose_ngram_draft_token_ids(
    self,
    sampled_token_ids: list[list[int]],
) -> list[list[int]]:
    draft_token_ids = self.drafter.propose(sampled_token_ids, self.input_batch.req_ids,
                                           self.input_batch.num_tokens_no_spec, self.input_batch.token_ids_cpu,
                                           self.input_batch.spec_decode_unsupported_reqs)
    # swipe draft_token_ids_native replacing [] to [-1]
    for i in range(len(draft_token_ids)):
        if len(draft_token_ids[i]) == 0:
            draft_token_ids[i] = [-1]
    return draft_token_ids

reload_weights

reload_weights() -> None
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def reload_weights(self) -> None:
    assert getattr(self, "model", None) is not None, \
        "Cannot reload weights before model is loaded."
    model_loader = get_model_loader(self.load_config)
    logger.info("Reloading weights inplace...")
    model_loader.load_weights(self.model, model_config=self.model_config)
    torch.hpu.synchronize()

remove_all_loras

remove_all_loras()
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def remove_all_loras(self):
    if not self.lora_manager:
        raise RuntimeError("LoRA is not enabled.")
    self.lora_manager.remove_all_adapters()

run_defragmenter

run_defragmenter(
    scheduler_output: SchedulerOutput,
    warmup_mode: bool = False,
)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
@torch.inference_mode()
def run_defragmenter(self, scheduler_output: "SchedulerOutput", warmup_mode: bool = False):
    if self.defragmenter.enabled and self.kv_caches and not warmup_mode:
        new = {req.req_id: flatten(req.block_ids) for req in scheduler_output.scheduled_new_reqs if req.block_ids}
        #TODO: Add support for preempted blocks
        cached = {
            req_id: flatten(new_block_ids)
            for req_id, new_block_ids in zip(scheduler_output.scheduled_cached_reqs.req_ids,
                                             scheduler_output.scheduled_cached_reqs.new_block_ids) if new_block_ids
        }
        self.defragmenter.update_state(new | cached, scheduler_output.finished_req_ids)
        self.defragmenter.defragment()

sample_tokens

sample_tokens(
    grammar_output: GrammarOutput | None,
) -> ModelRunnerOutput | AsyncModelRunnerOutput
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
@torch.inference_mode()
def sample_tokens(self, grammar_output: "GrammarOutput | None") -> ModelRunnerOutput | AsyncModelRunnerOutput:
    if self.scheduler_output is None:
        # Nothing to do (PP non-final rank case), output isn't used.
        return None  # noqa
    scheduler_output = self.scheduler_output
    warmup_mode = self.warmup_mode
    self.scheduler_output = None
    self.warmup_mode = False

    if self.unified_attn:
        return self.unified_execute_model(scheduler_output, warmup_mode=warmup_mode)

    # NOTE(kzawora): Since scheduler doesn't differentiate between prefills
    # and decodes, we must handle mixed batches. In _update_states we make
    # sure that first self.input_batch.num_decodes requests are decodes,
    # and remaining ones until the end are prefills. _update_states also
    # handles changes in request cache based on scheduler outputs and
    # previous iterations (e.g. keeping block tables and context lengths up
    # to date, creating, pruning and updating request caches,
    # and some more stuff)

    # If num_decodes == self.input_batch.num_reqs, then batch is all decode, and only a single decode forward pass will be executed in this method. # noqa
    # If num_decodes == 0, then batch is all prefill, and only prefill forward passes will be executed  in this method. # noqa
    # If neither apply, then batch is mixed, and both prefill and decode forward passes will be executed in this method. # noqa

    # First, we will execute all decodes (if any) in a single batch,
    # then we'll execute prefills in batches of up to max_prefill_batch_size elements. # noqa
    # All shapes used in forward passes are bucketed appropriately to mitigate risk of graph recompilations. # noqa

    # We perform sampling directly after executing each forward pass
    # Everything is done asynchronously - the only sync point is the place
    # where we copy the generated tokens back to the host.

    # Example: If a batch has 6 requests, 3 prefills and 3 decodes, the unprocessed sequences in batch will be laid as follows: # noqa
    # [D0, D1, D2, P0, P1, P2]
    # If we assume max_prefill_batch_size=2, the flow of this method will look as follows: # noqa
    # prepare_inputs: bucket [D0, D1, D2] -> [D0, D1, D2, 0] (BS=4 bucket, 1 seq padding) # noqa
    # prepare_inputs: bucket [P0, P1, P2] -> [P0, P1], [P2] (BS=2 + BS=1 bucket, no seqs padding) # noqa
    # decode forward pass BS4 [D0, D1, D2, 0]
    # decode compute_logits BS4 [D0, D1, D2, 0]
    # decode sampler BS4 [D0, D1, D2, 0] -> [tokD0, tokD1, tokD2, 0]
    # prefill[iter 0] forward pass BS2 [P0, P1]
    # prefill[iter 0] compute_logits BS2 [P0, P1]
    # prefill[iter 0] sampler BS2 [P0, P1] -> [tokP0, tokP1]
    # prefill[iter 1] forward pass BS1 [P0, P1]
    # prefill[iter 1] compute_logits BS1 [P0, P1]
    # prefill[iter 1] sampler BS1 [P0, P1] -> [tokP2]
    # prefill concat sampler results [tokP0, tokP1], [tokP2] -> [tokP0, tokP1, tokP2] # noqa
    # Join the prefill and decode on device into [tokD0, tokD1, tokD2, 0, tokP0, tokP1, tokP2] # noqa
    # Transfer [tokD0, tokD1, tokD2, 0, tokP0, tokP1, tokP2] to CPU
    # On CPU, sanitize [tokD0, tokD1, tokD2, 0, tokP0, tokP1, tokP2] -> [tokD0, tokD1, tokD2, tokP0, tokP1, tokP2] # noqa
    # Return [tokD0, tokD1, tokD2, tokP0, tokP1, tokP2]

    # Example2: Same thing, but with max_prefill_batch_size=4:
    # prepare_inputs: bucket [D0, D1, D2] -> [D0, D1, D2, 0] (BS=4 bucket, 1 seq padding) # noqa
    # prepare_inputs: bucket [P0, P1, P2] -> [P0, P1, P2, 0] (BS=4 bucket, 1 seq padding) # noqa
    # decode forward pass BS4 [D0, D1, D2, 0]
    # decode compute_logits BS4 [D0, D1, D2, 0]
    # decode sampler BS4 [D0, D1, D2, 0] -> [tokD0, tokD1, tokD2, 0]
    # prefill[iter 0] forward pass BS4 [P0, P1, P2, 0]
    # prefill[iter 0] compute_logits BS4 [P0, P1, P2, 0]
    # prefill[iter 0] sampler BS4 [P0, P1, P2, 0] -> [tokP0, tokP1, tokP2, 0] # noqa
    # Join the prefill and decode on device into [tokD0, tokD1, tokD2, 0, tokP0, tokP1, tokP2, 0] # noqa
    # Transfer [tokD0, tokD1, tokD2, 0, tokP0, tokP1, tokP2, 0] to CPU
    # On CPU, sanitize [tokD0, tokD1, tokD2, 0, tokP0, tokP1, tokP2, 0] -> [tokD0, tokD1, tokD2, tokP0, tokP1, tokP2] # noqa
    # Return [tokD0, tokD1, tokD2, tokP0, tokP1, tokP2]

    batch_changed = self.batch_changed
    if self.input_batch.pooling_params:
        (input_ids, position_ids, num_scheduled_tokens, attn_metadata,
         total_scheduled_tokens) = self._prepare_inputs_for_pooling(scheduler_output)

        with set_forward_context(attn_metadata, self.vllm_config):
            hidden_states = self.model.forward(
                input_ids=input_ids,
                positions=position_ids,
            )

        flattened = hidden_states.view(-1, hidden_states.shape[-1])
        pooled_output = self._pool(
            flattened,
            total_scheduled_tokens,
            np.array(num_scheduled_tokens, dtype=np.int32),
        )
        return pooled_output
    # If necessary, swap decodes/prompts to have all decodes on the start

    ensure_decodes_first(self.input_batch)
    # Prepare prompts/decodes info
    pd_info = self._get_prompts_and_decodes(scheduler_output)
    num_decodes = len(pd_info.decode_req_ids)
    num_prefills = len(pd_info.prompt_req_ids)
    num_reqs = num_decodes + num_prefills
    with self.profiler.record_event('internal', 'prepare_input_tensors'):
        prefill_input_data, decode_input_data = self._prepare_inputs(scheduler_output, num_prefills, num_decodes,
                                                                     warmup_mode)
    prefill_data, \
        dummy_prefill_input_data_batches_across_dp = prefill_input_data
    num_pad_prefill_batch_across_dp = \
        0 if dummy_prefill_input_data_batches_across_dp is None \
        else len(dummy_prefill_input_data_batches_across_dp.request_ids)
    decode_data, dummy_decode_input_data_across_dp = decode_input_data
    #FIXME(kzawora): Currently there's no handling of logprobs. Fix that
    # later.
    prefill_sampled_token_ids = []
    prefill_sampled_requests = []
    decode_sampled_token_ids = []
    decode_sampled_requests = []
    #if not has_kv_transfer_group():
    #    assert not (num_prefills > 0 and num_decodes > 0)
    # skip kv_connector if dummy run
    if not warmup_mode:
        with set_forward_context(None, self.vllm_config):
            self.maybe_setup_kv_connector(scheduler_output)
    finished_sending, finished_recving = set(), set()

    # NOTE(Chendi): used by spec decode draft model, since we are doing
    # prefill one by one, so save hidden states as list
    non_flattened_hidden_states_prefills = []
    aux_hidden_states_prefills = []
    sample_hidden_states_prefills = []
    decode_sampled_token_ids_device = None
    # NOTE(tianmu-li): For structured output, combine logits before
    # postprocessing. Should it be done for all requests?
    structured_output = False
    spec_decode_num_tokens = None
    if grammar_output is not None:
        logits_prompt = []
        logits_decode = []
        structured_output = True
    if self.use_async_scheduling:
        invalid_req_indices = []
    ######################### PREFILLS #########################
    if num_prefills > 0:
        htorch.core.mark_step()
        for idx, (req_id, prompt_len, token_ids, position_ids, attn_metadata, logits_indices,
                  logits_requests) in enumerate(zip(*shallow_tuple(prefill_data))):

            inputs_embeds = None
            model_mm_kwargs = None
            if self.supports_mm_inputs:
                # Run the multimodal encoder if any.
                with self.profiler.record_event('internal', 'prepare_input_encoders'):
                    self._execute_mm_encoder(scheduler_output, req_id)
                htorch.core.mark_step()

                mm_embeds, is_mm_embed = self._gather_mm_embeddings(scheduler_output,
                                                                    req_id,
                                                                    total_num_scheduled_tokens=token_ids.shape[-1])
                htorch.core.mark_step()

                # TODO: Only get embeddings for valid token_ids. Ignore token_ids[<pad_idxs>] # noqa E501
                # This may require moving multimodal input preps into _prepare_inputs,        # noqa E501
                # to avoid padding issues.
                inputs_embeds = self.model.embed_input_ids(
                    token_ids,
                    multimodal_embeddings=mm_embeds,
                    is_multimodal=is_mm_embed,
                )

                model_mm_kwargs = self._extract_mm_kwargs(scheduler_output)
                model_mm_kwargs = MultiModalKwargs.as_kwargs(
                    model_mm_kwargs,
                    device=self.device,
                )

            lora_mask, lora_logits_mask = self._configure_lora(token_ids, self.requests, req_id, True)

            self.event_start = self.profiler.get_timestamp_us()
            self.profiler.start("internal", "prefill")
            # NOTE(tianmu-li): Align behavior of incomplete prompt with gpu_model_runner
            # If logits_indices is smaller than req_id, the last request is a chunked prompt request that
            # hasn't finished in this step. We add the last token position to logits_indices to ensure
            # the last token of the chunk is sampled. This sampled token will be discarded later
            if logits_indices.shape[0] < len(req_id):
                if structured_output or self.use_async_scheduling:
                    # When there are multiple requests in the batch (e.g. self.use_merged_prefill=True),
                    # the last token position is the sum of all prompt lengths - 1
                    # This logic also holds when there is only one request in the batch
                    logits_indices_append = torch.tensor([torch.sum(prompt_len) - 1],
                                                         device=token_ids.device,
                                                         dtype=torch.int32)
                    logits_indices = torch.cat([logits_indices, logits_indices_append])
                if self.use_async_scheduling:
                    # Discard partial prefill logit for async scheduling
                    # Depends on 1 decode token/batch
                    prefill_start_idx = num_decodes
                    invalid_req_indices.append(prefill_start_idx + idx)
            htorch.core.mark_step()
            non_flattened_hidden_states, aux_hidden_states, \
                sample_hidden_states, logits_device = \
                self._execute_model_generic(
                    token_ids, position_ids, attn_metadata, logits_indices,
                    self.kv_caches,
                    lora_logits_mask,
                    lora_mask,
                    inputs_embeds=inputs_embeds,
                    model_mm_kwargs=model_mm_kwargs,
                    warmup_mode=warmup_mode,)
            htorch.core.mark_step()
            non_flattened_hidden_states_prefills.append(non_flattened_hidden_states)
            if self.use_aux_hidden_state_outputs:
                aux_hidden_states_prefills.append(aux_hidden_states)
            sample_hidden_states_prefills.append(sample_hidden_states)
            # Skip separate sampling for structured output
            if structured_output:
                logits_prompt.append(logits_device)
                prefill_sampled_requests.extend(logits_requests)
            else:
                # If there are no logits, there is nothing to sample.
                # This can happen with chunked prefill when a chunk does
                # not complete the prompt and no logits are generated.
                if logits_device.numel() > 0:
                    with self.profiler.record_event('internal', "sampler"):
                        sampler_output, sampling_metadata = self._run_sampling(batch_changed, logits_device, req_id,
                                                                               logits_device.shape[0],
                                                                               logits_requests)
                        prefill_sampled_token_ids.append(sampler_output.sampled_token_ids.flatten())
                        prefill_sampled_requests.extend(logits_requests)
            if self.is_driver_worker and self.profiler.enabled:
                # Stop recording 'execute_model_generic' event
                self.profiler.end()
                event_end = self.profiler.get_timestamp_us()
                counters = self.profiler_counter_helper.get_counter_dict(cache_config=self.cache_config,
                                                                         duration=event_end - self.event_start,
                                                                         seq_len=self._seq_len(attn_metadata),
                                                                         batch_size_padded=token_ids.size(0),
                                                                         real_batch_size=len(req_id),
                                                                         prompt_batch_idx=idx,
                                                                         is_prompt=True)
                self.profiler.record_counter(self.event_start, counters)
        if not warmup_mode:
            self.maybe_wait_for_kv_save()

        if self.is_driver_worker and self.profiler.enabled:
            self.profiler_counter_helper.reset_prompt_seq_stats()

    if num_pad_prefill_batch_across_dp > 0:
        for idx, (req_id, prompt_len, token_ids, position_ids, attn_metadata, logits_indices,
                  logits_requests) in enumerate(zip(*shallow_tuple(dummy_prefill_input_data_batches_across_dp))):
            htorch.core.mark_step()
            _, _, _, dummy_logits_device = \
            self._execute_model_generic(
                token_ids,
                position_ids,
                attn_metadata,
                logits_indices,
                self.kv_caches,
                None,
                None,
                warmup_mode=warmup_mode)
            htorch.core.mark_step()

    ######################### DECODES #########################
    # Decodes run as one single batch with [padded_decode_bs, 1]
    if num_decodes > 0:
        assert decode_data is not None
        lora_mask, lora_logits_mask = self._configure_lora(decode_data.token_ids, self.requests,
                                                           pd_info.decode_req_ids, False)
        self.event_start = self.profiler.get_timestamp_us()
        self.profiler.start("internal", "decode")
        htorch.core.mark_step()
        non_flattened_hidden_states, aux_hidden_states, \
            sample_hidden_states, logits_device = \
                self._execute_model_generic(
            decode_data.token_ids,
            decode_data.position_ids,
            decode_data.attn_metadata,
            decode_data.logits_indices,
            self.kv_caches,
            lora_logits_mask,
            lora_mask,
            warmup_mode=warmup_mode)
        htorch.core.mark_step()

        if structured_output:
            logits_decode.append(logits_device[:num_decodes])
            decode_sampled_requests.extend(self.input_batch.req_ids[:num_decodes])
        else:
            with self.profiler.record_event('internal', "sampler"):
                ##### Sampling Start #####
                spec_decode_metadata = decode_data.spec_decode_metadata
                sampler_output, sampling_metadata = self._run_sampling(
                    batch_changed, logits_device
                    if spec_decode_metadata is None else logits_device[spec_decode_metadata.bonus_logits_indices],
                    pd_info.decode_req_ids, logits_device.shape[0])

                if spec_decode_metadata is None:
                    decode_sampled_token_ids.append(sampler_output.sampled_token_ids.flatten())
                else:
                    # Handling spec decode sampling.
                    sampler_output = self.rejection_sampler(
                        spec_decode_metadata,
                        None,  # draft_probs
                        logits_device,
                        sampling_metadata,
                    )
                    sampled_token_ids = sampler_output.sampled_token_ids
                    decode_sampled_token_ids = \
                        self.rejection_sampler.parse_output(
                            sampled_token_ids,
                            self.input_batch.vocab_size,
                    )
                    # convert decode_sampled_token_ids as list of tensor
                    spec_decode_num_tokens = [len(v) for v in decode_sampled_token_ids]
                    decode_sampled_token_ids = [
                        torch.tensor(v, device="cpu").int() for v in decode_sampled_token_ids
                    ]
                    decode_sampled_token_ids_device = \
                        sampled_token_ids.to("hpu", non_blocking=True)
                decode_sampled_requests.extend(self.input_batch.req_ids[:num_decodes])
                ##### Sampling End #####

        if self.is_driver_worker and self.profiler.enabled:
            # Stop recording 'execute_model' event
            self.profiler.end()
            event_end = self.profiler.get_timestamp_us()
            counters = self.profiler_counter_helper.get_counter_dict(
                cache_config=self.cache_config,
                duration=event_end - self.event_start,
                seq_len=self._seq_len(decode_data.attn_metadata),
                batch_size_padded= \
                    decode_data.token_ids.size(0), # type: ignore
                real_batch_size=decode_data.num_decodes,
                prompt_batch_idx=None,
                is_prompt=False)
            self.profiler.record_counter(self.event_start, counters)

    elif dummy_decode_input_data_across_dp is not None:
        htorch.core.mark_step()
        _, _, _, dummy_logits_device = self._execute_model_generic(dummy_decode_input_data_across_dp.token_ids,
                                                                   dummy_decode_input_data_across_dp.position_ids,
                                                                   dummy_decode_input_data_across_dp.attn_metadata,
                                                                   dummy_decode_input_data_across_dp.logits_indices,
                                                                   self.kv_caches,
                                                                   None,
                                                                   None,
                                                                   warmup_mode=warmup_mode)
        htorch.core.mark_step()

    if structured_output:
        # Scheduler places cached before prompt
        logits_combined = logits_decode + logits_prompt
        logits = torch.cat(logits_combined, dim=0)
        # Apply structured output bitmasks if present
        if grammar_output:
            self.apply_grammar_bitmask(scheduler_output, grammar_output, logits)
        sampler_output, _sampling_metadata = self._run_sampling(batch_changed, logits,
                                                                pd_info.prompt_req_ids + pd_info.decode_req_ids,
                                                                logits.shape[0])
        # Deal with the case of incomplete prompt
        for i in range(logits.shape[0] - num_decodes):
            prefill_sampled_token_ids.append(sampler_output.sampled_token_ids[num_decodes + i].flatten())
        decode_sampled_token_ids.append(sampler_output.sampled_token_ids[:num_decodes].flatten())
    elif self.use_async_scheduling:
        # For async scheduling: keep tokens on HPU and avoid CPU sync
        # Concatenate decode and prefill tokens on HPU
        if decode_sampled_token_ids or prefill_sampled_token_ids:
            decode_sampled_token_ids = [tensor[:num_decodes] for tensor in decode_sampled_token_ids]
            # Note: this will cause an issue with the current spec decode impl, as they are on different devices
            sampled_token_ids = torch.cat(decode_sampled_token_ids + prefill_sampled_token_ids).view(-1, 1)
        else:
            sampled_token_ids = torch.empty((0, 1), dtype=torch.int32, device=self.device)

    # Copy some objects so they don't get modified after returning.
    # This is important when using async scheduling.
    req_ids_output_copy = self.input_batch.req_ids.copy()
    req_id_to_index_output_copy = \
        self.input_batch.req_id_to_index.copy()

    max_req_index = max(self.input_batch.req_id_to_index.values())
    postprocessed_sampled_token_ids: list[np.ndarray] = [
        np.array([], dtype=np.int32) for _ in range(max_req_index + 1)
    ]
    if self.use_async_scheduling:
        self.input_batch.prev_sampled_token_ids = sampled_token_ids.flatten()
        # self.input_batch.prev_sampled_token_ids_invalid_indices
        invalid_req_indices_set = set(invalid_req_indices)
        self.input_batch.prev_sampled_token_ids_invalid_indices = \
            invalid_req_indices_set
        self.input_batch.prev_req_id_to_index = {
            req_id: i
            for i, req_id in enumerate(self.input_batch.req_ids) if i not in invalid_req_indices_set
        }
        # For the output, postprocessed_sampled_token_ids will be filled during serialization
    else:
        prefill_sampled_token_ids_device = prefill_sampled_token_ids
        # From this point onward, all operations are done on CPU.
        # We already have tokens. Let's copy the data to
        # CPU as is, and then discard padded tokens.
        with self.profiler.record_event('internal', "sampler_postprocessing"):
            prefill_sampled_token_ids = [tensor.cpu() for tensor in prefill_sampled_token_ids]
            if spec_decode_num_tokens is not None:
                decode_sampled_token_ids = [tensor.cpu() for tensor in decode_sampled_token_ids]
            else:
                decode_sampled_token_ids = [tensor.cpu()[:num_decodes] for tensor in decode_sampled_token_ids]
            if decode_sampled_token_ids + prefill_sampled_token_ids:
                sampled_token_ids_tensor = torch.cat(decode_sampled_token_ids + prefill_sampled_token_ids)
                sampled_token_ids_np = sampled_token_ids_tensor.cpu().numpy().flatten()
            else:
                sampled_token_ids_np = np.array([], dtype=np.int32)
            sampled_token_requests = \
                decode_sampled_requests + prefill_sampled_requests
            max_req_index = max(self.input_batch.req_id_to_index.values())
            # NOTE(Chendi): in post-processing, spec_decode might
            # return more than 1 token during decode.
            start_idx = 0
            for i, req_id in enumerate(sampled_token_requests):
                num_tokens = spec_decode_num_tokens[
                    i] if spec_decode_num_tokens is not None and i < num_decodes else 1
                req_idx = self.input_batch.req_id_to_index[req_id]
                postprocessed_sampled_token_ids[req_idx] = np.array(sampled_token_ids_np[start_idx:start_idx +
                                                                                         num_tokens],
                                                                    dtype=np.int32)
                start_idx += num_tokens

    ################## RETURN ##################
    # NOTE(kzawora): idk what happens if part of batch doesn't have logprobs

    ######### UPDATE REQUEST STATE WITH GENERATED TOKENS #########
    for req_id in self.input_batch.req_ids[:num_reqs]:
        req_state = self.requests[req_id]
        i = self.input_batch.req_id_to_index[req_id]
        seq_len = (req_state.num_computed_tokens + scheduler_output.num_scheduled_tokens[req_id])
        token_ids = postprocessed_sampled_token_ids[i]
        num_tokens = len(token_ids)
        self.input_batch.token_ids_cpu[i, seq_len:seq_len + num_tokens] = token_ids
        self.input_batch.num_tokens[i] += len(token_ids)

    # NOTE(chendi): enable cache based on PR(#20291)
    # Cache the sampled tokens in the model runner, so that the scheduler
    # doesn't need to send them back.
    # NOTE(woosuk): As an exception, when using PP, the scheduler sends
    # the sampled tokens back, because there's no direct communication
    # between the first-stage worker and the last-stage worker.
    for req_idx, sampled_ids in enumerate(postprocessed_sampled_token_ids[:num_reqs]):
        if sampled_ids is None:
            continue

        start_idx = self.input_batch.num_tokens_no_spec[req_idx]
        end_idx = start_idx + len(sampled_ids)
        # NOTE(adobrzyn): assert for full max prompt length including
        # max_model_len and one token that's going to be generated
        # especially needed for biggest prompt in warm-up phase
        full_max_prompt = self.max_model_len + 1
        assert end_idx <= full_max_prompt, ("Sampled token IDs exceed the max model length. "
                                            f"Total number of tokens: {end_idx} > max_model_len: "
                                            f"{full_max_prompt}")

        self.input_batch.token_ids_cpu[req_idx, start_idx:end_idx] = sampled_ids
        self.input_batch.num_tokens_no_spec[req_idx] = end_idx
        self.input_batch.num_tokens[req_idx] = end_idx
        req_id = self.input_batch.req_ids[req_idx]
        req_state = self.requests[req_id]
        req_state.output_token_ids.extend(sampled_ids)

    ################## Spec Decode ##################
    # Now, we will call drafter to propose draft token ids
    if self.speculative_config:
        self._draft_token_ids = self.propose_draft_token_ids(
            scheduler_output, postprocessed_sampled_token_ids, prefill_sampled_token_ids_device,
            decode_sampled_token_ids_device, sampling_metadata, non_flattened_hidden_states, sample_hidden_states,
            aux_hidden_states, non_flattened_hidden_states_prefills, sample_hidden_states_prefills,
            aux_hidden_states_prefills, num_decodes, prefill_data if num_prefills > 0 else None,
            decode_data if num_decodes > 0 else None)
    ################## Spec Decode end ##################

    # Create output.
    all_req_ids = pd_info.decode_req_ids + pd_info.prompt_req_ids
    # prompt_logprobs_dict: dict[
    #    str, Optional[LogprobsTensors]] = self._get_prompt_logprobs_dict(
    #        prefill_hidden_states_device, scheduler_output)
    prompt_logprobs_dict: dict[str, Optional[LogprobsTensors]] = {}
    all_req_ids = pd_info.decode_req_ids + pd_info.prompt_req_ids
    logprobs = None

    if not warmup_mode:
        finished_sending, finished_recving = self.get_finished_kv_transfers(scheduler_output)

    if self.use_async_scheduling:
        model_runner_output = ModelRunnerOutput(
            req_ids=req_ids_output_copy,  # CHECK
            req_id_to_index=req_id_to_index_output_copy,
            sampled_token_ids=postprocessed_sampled_token_ids,
            logprobs=logprobs,
            prompt_logprobs_dict=prompt_logprobs_dict,  # type: ignore[arg-type]
            pooler_output=[],
            kv_connector_output=KVConnectorOutput(
                finished_sending=finished_sending,
                finished_recving=finished_recving,
            ))
        return AsyncHPUModelRunnerOutput(
            model_runner_output=model_runner_output,
            sampled_token_ids=sampled_token_ids,
            invalid_req_indices=invalid_req_indices,
            async_output_copy_stream=self.async_output_copy_stream,
        )
    model_runner_output = ModelRunnerOutput(
        req_ids=all_req_ids,
        req_id_to_index=self.input_batch.req_id_to_index,
        sampled_token_ids=postprocessed_sampled_token_ids,
        logprobs=logprobs,
        prompt_logprobs_dict=prompt_logprobs_dict,  # type: ignore[arg-type]
        pooler_output=[],
        kv_connector_output=KVConnectorOutput(
            finished_sending=finished_sending,
            finished_recving=finished_recving,
        ))
    if has_kv_transfer_group():
        get_kv_transfer_group().clear_connector_metadata()

    return model_runner_output

set_active_loras

set_active_loras(
    lora_requests: set[LoRARequest],
    lora_mapping: LoRAMapping,
) -> None
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def set_active_loras(self, lora_requests: set[LoRARequest], lora_mapping: LoRAMapping) -> None:
    if not self.lora_manager:
        raise RuntimeError("LoRA is not enabled.")
    self.lora_manager.set_active_adapters(lora_requests, lora_mapping)

shutdown_inc

shutdown_inc()
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def shutdown_inc(self):
    can_finalize_inc = self._is_quant_with_inc() and \
        (self.model.model is not None) and \
        self.inc_initialized_successfully and \
        not self._is_inc_finalized
    if can_finalize_inc:
        from neural_compressor.torch.quantization import (finalize_calibration)
        finalize_calibration(self.model.model)
        self._is_inc_finalized = True

take_draft_token_ids

take_draft_token_ids() -> Optional[DraftTokenIds]
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def take_draft_token_ids(self) -> Optional[DraftTokenIds]:
    if self._draft_token_ids is None:
        return None
    req_ids = self.input_batch.req_ids
    if isinstance(self._draft_token_ids, torch.Tensor):
        draft_token_ids = self._draft_token_ids.tolist()
    else:
        draft_token_ids = self._draft_token_ids
    self._draft_token_ids = None
    return DraftTokenIds(req_ids, draft_token_ids)

unified_bucketing_fn

unified_bucketing_fn(
    is_causal,
    query_len,
    shared_blocks,
    unique_blocks,
    logits,
)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def unified_bucketing_fn(self, is_causal, query_len, shared_blocks, unique_blocks, logits):
    if not get_config().use_bucketing:
        return query_len, shared_blocks, unique_blocks, logits

    new_bucket = self.bucketing_manager.find_unified_bucket(query_len, shared_blocks, unique_blocks, is_causal)
    return (new_bucket[0], new_bucket[1], new_bucket[2], self.max_num_seqs)

unified_execute_model

unified_execute_model(
    scheduler_output: SchedulerOutput,
    grammar_output: GrammarOutput = None,
    warmup_mode: bool = False,
) -> ModelRunnerOutput
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
@torch.inference_mode()
def unified_execute_model(self,
                          scheduler_output: "SchedulerOutput",
                          grammar_output: "GrammarOutput" = None,
                          warmup_mode: bool = False) -> ModelRunnerOutput:
    batch_changed = self.batch_changed
    with self.profiler.record_event('internal', 'prepare_unified_batch'):
        batch = self.prepare_unified_batch(scheduler_output)
    htorch.core.mark_step()
    if self.is_driver_worker:
        unified_attn_cfg = self._get_unified_config(batch.attn_metadata, batch.logits_indices)
        (phase, qlen, num_shared_blocks, num_unique_blocks, num_logits) = unified_attn_cfg
        model_event_name = (
            f"model_forward_{phase}_qlen{qlen}_nsb{num_shared_blocks}_nub{num_unique_blocks}_nlog{num_logits}")
    else:
        model_event_name = 'model_executable'
    with self.profiler.record_event('internal', model_event_name):
        non_flattened_hidden_states, aux_hidden_states, hidden_states, logits_device = \
            self._execute_model_generic(
                token_ids=batch.token_ids.unsqueeze(-1),
                position_ids=batch.token_positions.unsqueeze(-1),
                attn_metadata=batch.attn_metadata,
                logits_indices=batch.logits_indices,
                kv_caches=self.kv_caches,
                lora_logits_mask=None,
                lora_mask=None,
                warmup_mode=warmup_mode)
    selected_req_ids = [batch.req_ids_cpu[idx] for idx in batch.logits_groups_cpu.tolist()]
    htorch.core.mark_step()
    with self.profiler.record_event('internal', 'unified_sampler'):
        sampling_metadata = self._prepare_sampling(batch_changed, selected_req_ids, pad_to=logits_device.shape[0])
        sampler_output = self.sampler(logits=logits_device, sampling_metadata=sampling_metadata)

    with self.profiler.record_event('internal', 'unified_postprocess'):
        sampled_token_ids_cpu = sampler_output.sampled_token_ids.cpu()

        sampled_token_ids_np = sampled_token_ids_cpu.numpy()
        sampled_token_ids: list[np.ndarray] = [np.array([], dtype=np.int32) for _ in batch.req_ids_cpu]
        for req_id, tokens_array in zip(selected_req_ids, sampled_token_ids_np):
            idx = self.input_batch.req_id_to_index[req_id]
            sampled_token_ids[idx] = tokens_array

        #TODO: add support for multi-token output
        assert sampled_token_ids_cpu.size(1) == 1, 'Currently only single token output is supported!'
        sampled_token_ids_cpu = sampled_token_ids_cpu.flatten()
        htorch.core.mark_step()

        sampled_token_ids_cpu = sampled_token_ids_cpu.index_select(0, batch.logits_groups_cpu)
        self.input_batch.token_ids_cpu_tensor.index_put_((batch.logits_groups_cpu, batch.new_token_positions_cpu),
                                                         sampled_token_ids_cpu)

        ######### UPDATE REQUEST STATE WITH GENERATED TOKENS #########
        num_reqs = len(selected_req_ids)
        for req_id in self.input_batch.req_ids[:num_reqs]:
            req_state = self.requests[req_id]
            i = self.input_batch.req_id_to_index[req_id]
            seq_len = (req_state.num_computed_tokens + scheduler_output.num_scheduled_tokens[req_id])
            token_ids = sampled_token_ids[i]
            num_tokens = len(token_ids)
            self.input_batch.token_ids_cpu[i, seq_len:seq_len + num_tokens] = token_ids
            self.input_batch.num_tokens[i] += len(token_ids)
            req_state.output_token_ids.extend(token_ids.tolist())

    model_runner_output = ModelRunnerOutput(
        req_ids=batch.req_ids_cpu,
        req_id_to_index=self.input_batch.req_id_to_index,
        sampled_token_ids=sampled_token_ids,
        logprobs=None,
        prompt_logprobs_dict={},
        pooler_output=[],
    )

    return model_runner_output

update_config

update_config(overrides: dict[str, Any]) -> None
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def update_config(self, overrides: dict[str, Any]) -> None:
    allowed_config_names = {"load_config", "model_config"}
    for config_name, config_overrides in overrides.items():
        assert config_name in allowed_config_names, \
            f"Config `{config_name}` not supported. " \
            f"Allowed configs: {allowed_config_names}"
        config = getattr(self, config_name)
        new_config = update_config(config, config_overrides)
        setattr(self, config_name, new_config)

warmup_defragmenter

warmup_defragmenter()

为不同阈值预热碎片整理交换图。

我们执行一个最小交换(1对),它将被内部填充到请求的阈值大小。选择的阈值以反映潜在的生产值:8、16、32、64、128、256、512。

Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def warmup_defragmenter(self):
    """Warm up defragmentation swap graphs for different thresholds.

    We execute a minimal swap (1 pair) which will be padded internally to the
    requested threshold size. Thresholds chosen to mirror potential production
    values: 8, 16, 32, 64, 128, 256, 512.
    """
    # If defragmenter is disabled or cache utils not prepared, skip.
    if not getattr(self.defragmenter, 'enabled', False):
        return
    if self.defragmenter.cache_utils is None:
        return

    thresholds = self.defragmenter.to_swap_pad_thresholds

    logger.info("Warming up defragmenter with thresholds: %s", thresholds)

    # Use simple valid block ids present in caches (assume at least 2 blocks allocated when kv caches created)
    # We only need distinct ids for a swap. They will be scaled by block_size inside swap.
    # If for some reason only 1 block exists, skip warmup gracefully.
    try:
        k_cache = self.defragmenter.cache_utils.kv_caches[0][0]
        num_blocks_available = k_cache.shape[0] // self.block_size
    except Exception:
        num_blocks_available = 0
    if num_blocks_available < 2:
        logger.warning("Skipping defragmenter warmup, insufficient blocks (%s)", num_blocks_available)
        return

    # Minimal pair to trigger a swap path
    to_swap = [(1, 0)]

    for th in thresholds:
        self.defragmenter.cache_utils.swap(to_swap, th)

    # If the number of swaps was odd, do one more to make it even and return to original state.
    if len(thresholds) % 2 == 1:
        self.defragmenter.cache_utils.swap(to_swap, thresholds[0])

    logger.info("Defragmenter warmup completed successfully")

warmup_graphs

warmup_graphs(
    buckets,
    is_prompt,
    kv_caches,
    starting_mem=0,
    total_batch_seq=0.001,
)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def warmup_graphs(self, buckets, is_prompt, kv_caches, starting_mem=0, total_batch_seq=0.001):
    total_mem = starting_mem
    idx = 0
    num_candidates = len(buckets)
    captured_all = True
    developer_settings = get_config().VLLM_DEVELOPER_MODE
    phase = 'Prompt' if is_prompt else 'Decode'
    desc = f'{phase} warmup processing: '
    with tqdm(total=num_candidates, desc=desc, unit="item") as pbar:
        for idx, (batch_size, seq_len, num_blocks) in enumerate(reversed(buckets)):
            if seq_len > self.max_num_tokens:
                continue
            # Graph memory usage is proportional to seq dimension in a batch
            if is_prompt:
                batch_seq = batch_size * seq_len * num_blocks if num_blocks else batch_size * seq_len
            else:
                batch_seq = batch_size

            graphed_bucket = (batch_size, seq_len, num_blocks, is_prompt)
            if graphed_bucket in self.graphed_buckets:
                continue
            self.graphed_buckets.add(graphed_bucket)
            if developer_settings:
                self.log_warmup(phase, idx, num_candidates, batch_size, seq_len, num_blocks)
            prompt_cfg, decode_cfg = None, None
            with HabanaMemoryProfiler() as mem_prof:
                if is_prompt:
                    prompt_cfg = (batch_size, seq_len, num_blocks)
                else:
                    decode_cfg = (batch_size, 1, num_blocks)
                self._prepare_dummy_scenario(prompt_cfg, decode_cfg)
            # TODO(kzawora): align_workers
            used_mem = mem_prof.consumed_device_memory
            total_mem += used_mem
            total_batch_seq += batch_seq

            pbar.set_postfix_str(f"{idx}/{num_candidates}")
            pbar.update(1)

    return total_mem, total_batch_seq, captured_all

warmup_model

warmup_model() -> None
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
@torch.inference_mode()
def warmup_model(self) -> None:
    if not self.enable_bucketing:
        return

    if self.unified_attn:
        self.bucketing_manager.generate_unified_buckets()
    else:
        self.bucketing_manager.generate_prompt_buckets()
        self.bucketing_manager.generate_decode_buckets()

        if self.supports_mm_inputs:
            # Delayed multimodal buckets during warmup until model is loaded.
            from vllm_gaudi.extension.bucketing.vision import HPUVisionBucketManager
            self.get_model().vision_bucket_manager = HPUVisionBucketManager(self.model_config.model)
            msg = (f"Multimodal bucket : {self.get_model().vision_bucket_manager.multimodal_buckets}")
            logger.info(msg)

        max_bucket = max(self.bucketing_manager.decode_buckets[-1][0], self.bucketing_manager.prompt_buckets[-1][0])
        if max_bucket > self.input_batch.max_num_reqs:
            input_batch_bkp = self.input_batch
            self.input_batch = InputBatch(
                max_num_reqs=self.bucketing_manager.decode_buckets[-1][0],
                max_model_len=self.max_model_len,
                max_num_batched_tokens=self.max_num_tokens,
                device=self.device,
                pin_memory=self.pin_memory,
                vocab_size=self.model_config.get_vocab_size(),
                block_sizes=[self.block_size],
                kernel_block_sizes=[self.block_size],
                logitsprocs=build_logitsprocs(self.vllm_config, self.device, self.pin_memory, self.is_pooling_model,
                                              self.vllm_config.model_config.logits_processors),
            )

    self.defragmenter.initialize(self.kv_caches, self.block_size)

    prompt_profile_cfg, decode_profile_cfg = self._read_profiling_cfg()
    if prompt_profile_cfg or decode_profile_cfg:
        self._generate_profiling(prompt_profile_cfg, decode_profile_cfg)
        raise AssertionError("Finished profiling")
    kv_caches = self.kv_caches

    if not htorch.utils.internal.is_lazy() and not self.model_config.enforce_eager:
        multiplier = 5 if self.compile_config.regional_compilation else 1
        cache_size_limit = 1 + multiplier * (len(self.bucketing_manager.prompt_buckets) +
                                             len(self.bucketing_manager.decode_buckets))
        torch._dynamo.config.cache_size_limit = max(cache_size_limit, torch._dynamo.config.cache_size_limit)
        # Multiply by 8 to follow the original default ratio between
        # the cache_size_limit and accumulated_cache_size_limit
        torch._dynamo.config.accumulated_cache_size_limit = max(cache_size_limit * 8,
                                                                torch._dynamo.config.accumulated_cache_size_limit)
        # NOTE(kzawora): I'm not exactly sure why, but if we don't set this in unified attention to a high enough
        # value, we'll see warmup mode bypassing compilation and execute everything eagerly.
        if self.unified_attn:
            torch._dynamo.config.accumulated_recompile_limit = sys.maxsize
            torch._dynamo.config.recompile_limit = sys.maxsize

    if self.skip_warmup:
        logger.info("Skipping warmup...")
        return

    self.profiler.start('internal', 'warmup')
    start_mem = HabanaMemoryProfiler.current_device_memory_usage()
    start_time = time.perf_counter()

    # Most model's multimodal embedding has to be run without COMPILE ONLY mode.
    if self.supports_mm_inputs:
        self.warmup_multimodal_graphs(self.get_model().vision_bucket_manager.multimodal_buckets)

    compile_only_mode_context = functools.partial(bc.env_setting, "PT_COMPILE_ONLY_MODE", True)
    can_use_compile_only_mode = True
    try:
        with compile_only_mode_context():
            pass
        logger.debug("Using PT_COMPILE_ONLY_MODE.")
    except KeyError:
        can_use_compile_only_mode = False
        logger.warning('Cannot use PT_COMPILE_ONLY_MODE. '
                       'Warmup time will be negatively impacted. '
                       'Please update Gaudi Software Suite.')
    with compile_only_mode_context() if can_use_compile_only_mode else contextlib.nullcontext():
        if not self.model_config.enforce_eager:
            assert self.mem_margin is not None, \
                ("HabanaWorker.determine_num_available_blocks needs "
                 "to be called before warming up the model.")

            self.warmup_sampler()
            self.warmup_defragmenter()

            # TODO(kzawora): align_workers
            if self.unified_attn:
                self.warmup_unified_graphs(self.bucketing_manager.unified_buckets, kv_caches)
            else:
                mem_post_prompt, prompt_batch_seq, prompt_captured_all = \
                    self.warmup_graphs(
                        self.bucketing_manager.prompt_buckets, True, kv_caches)
                mem_post_decode, decode_batch_seq, decode_captured_all = \
                    self.warmup_graphs(
                        self.bucketing_manager.decode_buckets, False, kv_caches)

                self.log_graph_warmup_summary(self.bucketing_manager.prompt_buckets, True, mem_post_prompt)
                self.log_graph_warmup_summary(self.bucketing_manager.decode_buckets, False, mem_post_decode)

    end_time = time.perf_counter()
    end_mem = HabanaMemoryProfiler.current_device_memory_usage()
    if os.getenv('VLLM_FULL_WARMUP', 'false').strip().lower() in ("1", "true"):
        # Since the model is warmed up for all possible tensor sizes,
        # Dynamo can skip checking the guards
        torch.compiler.set_stance(skip_guard_eval_unsafe=True)
    elapsed_time = end_time - start_time
    msg = (f"Warmup finished in {elapsed_time:.0f} secs, "
           f"allocated {format_bytes(end_mem - start_mem)} of device memory")
    logger.info(msg)
    self.profiler.end()

    if not self.unified_attn and max_bucket > self.input_batch.max_num_reqs:
        self.input_batch = input_batch_bkp
    # NOTE(kzawora): This is a nasty workaround - for whatever cache_utils-related reason,
    # reusing defragmenter used in warmup causes accuracy drops, which is why we re-create
    # and re-initialize it.
    self.defragmenter = OnlineDefragmenter()
    self.defragmenter.initialize(self.kv_caches, self.block_size)

warmup_multimodal_graphs

warmup_multimodal_graphs(buckets)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def warmup_multimodal_graphs(self, buckets):

    phase = 'Graph/Multimodal'
    from vllm.v1.worker.utils import MultiModalBudget
    self.mm_budget = MultiModalBudget(
        self.model_config,
        self.scheduler_config,
        self.mm_registry,
    ) if self.supports_mm_inputs else None

    #self.mm_budget.mm_limits : {'image': 2}
    for modality, max_items in self.mm_budget.mm_limits.items():
        phase = f'Graph/Multimodal({modality})'
        num_candidates = len(buckets)

        for idx, img_arg in enumerate(buckets):
            # Create dummy batch of multimodal inputs.
            batched_dummy_mm_inputs = self._get_mm_dummy_batch(
                modality,
                img_arg,
            )
            #htorch.core.mark_step()
            # Run multimodal encoder.
            dummy_encoder_outputs = \
                 self.model.embed_multimodal(
                 **batched_dummy_mm_inputs)
            #htorch.core.mark_step()

            sanity_check_mm_encoder_outputs(
                dummy_encoder_outputs,
                expected_num_items=img_arg,
            )

            self.graphed_buckets.add(img_arg)
            self.log_warmup_multimodal(phase, idx, num_candidates, 1, 0, img_arg)

warmup_sampler

warmup_sampler()

使用不同的温度、top-p和top-k值来预热采样器。

Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def warmup_sampler(self):
    """
    Warmup the sampler with different temperature, top-p, and top-k values.
    """
    # Choose batch sizes for warmup based on bucketing
    test_batch_sizes = list(dict.fromkeys([0, 1] + [bucket[0] for bucket in self.bucketing_manager.decode_buckets]))

    # Test different sampling configurations
    sampling_configs = [
        # (temperature, top_p, top_k, batch_changed)
        (0.0, 1.0, 0, True),  # Greedy sampling
        (1.0, 1.0, 0, True),  # Random sampling with temp=1.0
        (0.7, 0.9, 50, True),  # Common creative settings
        (0.3, 0.95, 20, True),  # Conservative settings
        (1.2, 0.8, 100, True),  # High temperature settings
        (0.8, 0.85, 0, True),  # Different top-p sampling
        (0.0, 1.0, 0, False),  # Greedy sampling
        (1.0, 1.0, 0, False),  # Random sampling with temp=1.0
        (0.7, 0.9, 50, False),  # Common creative settings
        (0.3, 0.95, 20, False),  # Conservative settings
        (1.2, 0.8, 100, False),  # High temperature settings
        (0.8, 0.85, 0, False),  # Different top-p sampling
    ]

    logger.info("Warming up sampler with batch sizes: %s and following configs:", test_batch_sizes)
    for temp, top_p, top_k, batch_changed in sampling_configs:
        logger.info("temp=%s, top_p=%s, top_k=%s, batch_changed=%s", temp, top_p, top_k, batch_changed)
    logger.info("Starting sampler warmup...")

    for batch_size in test_batch_sizes:
        dummy_hidden_states = torch.randn(batch_size, self.hidden_size, dtype=self.dtype, device=self.device)
        if self.lora_config:
            lora_logits_mask = torch.zeros(batch_size,
                                           (self.lora_config.max_loras) * self.lora_config.max_lora_rank,
                                           dtype=self.lora_config.lora_dtype).to('hpu')
            LoraMask.setLoraMask(lora_logits_mask)
        dummy_logits = self.model.compute_logits(dummy_hidden_states)

        # Create dummy requests for this specific configuration
        dummy_req_ids = [f"warmup_req_{batch_size}_{i}" for i in range(max(1, batch_size))]

        for i, req_id in enumerate(dummy_req_ids):
            self.requests[req_id] = CachedRequestState(
                req_id=req_id,
                prompt_token_ids=list(range(10)),  # Dummy prompt
                mm_features=[],
                sampling_params=SamplingParams(),
                pooling_params=None,
                generator=None,
                block_ids=[[0]],
                num_computed_tokens=10,
                output_token_ids=[],
            )
            self.input_batch.req_id_to_index[req_id] = i

        for temp, top_p, top_k, batch_changed in sampling_configs:
            # Add dummy requests to cache with consistent sampling params
            for i, req_id in enumerate(dummy_req_ids):
                self.requests[req_id].sampling_params = SamplingParams(
                    temperature=temp,
                    top_p=top_p,
                    top_k=top_k,
                )

                if temp == 0.0:  # Greedy sampling
                    self.input_batch.greedy_reqs.add(req_id)
                else:  # Random sampling
                    self.input_batch.random_reqs.add(req_id)

            self.input_batch.req_output_token_ids = [
                item[1] for item in self._generate_req_id_output_token_ids_lst(dummy_req_ids, pad_to=batch_size)
            ]
            self.input_batch.refresh_sampling_metadata()

            _sampler_output, _sampling_metadata = self._run_sampling(batch_changed=batch_changed,
                                                                     logits_device=dummy_logits,
                                                                     request_ids=dummy_req_ids,
                                                                     pad_to=dummy_logits.shape[0])

            # Cleanup after sampling
            self.input_batch.greedy_reqs = set()
            self.input_batch.random_reqs = set()
            self.input_batch.req_output_token_ids = []

        # Cleanup after batch has been warmed up
        self.input_batch.req_id_to_index = {}
        self.requests = {}

    # Final synchronization to ensure all operations are completed
    torch.hpu.synchronize()

    logger.info("Sampler warmup completed successfully")

warmup_unified_graphs

warmup_unified_graphs(buckets, kv_cache)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def warmup_unified_graphs(self, buckets, kv_cache):
    idx = 0
    num_candidates = len(buckets)
    developer_settings = get_config().VLLM_DEVELOPER_MODE
    with tqdm(total=num_candidates, desc="Unified Attention warmup", unit="item") as pbar:
        for idx, (query, shared_ctx, unique_ctx, is_causal) in enumerate(reversed(buckets)):
            unified_cfg = (query, shared_ctx, unique_ctx, is_causal)
            if unified_cfg in self.graphed_buckets:
                continue
            self.graphed_buckets.add(unified_cfg)
            if developer_settings:
                self.log_warmup("Unified CFG", idx, num_candidates, query, shared_ctx, unique_ctx, is_causal)
            self._prepare_dummy_unified_scenario(unified_cfg)
            pbar.set_postfix_str(f"{idx}/{num_candidates}")
            pbar.update(1)

HpuModelAdapter

基类:Module, KVConnectorModelRunnerMixin

Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
class HpuModelAdapter(torch.nn.Module, KVConnectorModelRunnerMixin):

    def __init__(self, model, vllm_config):
        super().__init__()
        self.model = model
        self.prefill_use_fusedsdpa = get_config().prompt_attn_impl == 'fsdpa_impl'
        self.recompute_cos_sin = os.getenv('VLLM_COS_SIN_RECOMPUTE', 'false').lower() in ['1', 'true']
        self.vllm_config = vllm_config
        self.block_size = vllm_config.cache_config.block_size
        self.dtype = vllm_config.model_config.dtype
        self._rotary_embed_module = self._get_rotary_embedding_module(self.model)
        self._rotary_prepare_cos_sin = self._get_prepare_cos_sin()
        self.unified_attn = get_config().unified_attn
        self.unified_attn_persistent_ctx = None
        self.flatten_input = get_config().flatten_input
        self.is_mm_optimized = is_mm_optimized(self.model)
        self.sliding_window = vllm_config.model_config.get_sliding_window()
        self.interleaved_sliding_window = is_interleaved(vllm_config.model_config.hf_text_config)
        if self.interleaved_sliding_window and self.sliding_window is not None:
            self.use_window_sdpa = os.getenv("PT_HPU_SDPA_QKV_SLICE_MODE_FWD", "false").strip().lower() in ("1", "true")
            self.slice_size = int(os.getenv("PT_HPU_SDPA_BC_FACTOR", "1024"))
            self.slice_thld = int(os.environ.get('VLLM_FUSEDSDPA_SLIDE_THLD', '8192'))

        # for DP
        self.dummy_num_input_tokens = -1
        self.num_tokens_across_dp = [self.dummy_num_input_tokens] * self.vllm_config.parallel_config.data_parallel_size
        self.dummy_num_tokens_across_dp_cpu = torch.tensor(self.num_tokens_across_dp, device='cpu', dtype=torch.int32)

        # Vision embedding can be also wrapped in HPU graph once all the dynamic shape is removed.
        # Performance can be greatly improved.
        if htorch.utils.internal.is_lazy() and \
           MULTIMODAL_REGISTRY.supports_multimodal_inputs(vllm_config.model_config) and self.is_mm_optimized:
            if hasattr(self.model, 'vision_tower'):
                self.model.vision_tower = htorch.hpu.wrap_in_hpu_graph(self.model.vision_tower,
                                                                       disable_tensor_cache=False)
            if hasattr(self.model, 'multi_modal_projector'):
                self.model.multi_modal_projector = \
                        htorch.hpu.wrap_in_hpu_graph( \
                        self.model.multi_modal_projector, \
                        disable_tensor_cache=True)

    def _get_rotary_embedding_module(self, model: torch.nn.Module):
        """
        Dynamically get the RotaryEmbedding layer in the model.
        This function will recursively search through the module
        hierarchy to find and return a RotaryEmbedding layer.
        If no such layer is found, it returns None.
        """
        if model is None:
            return None

        if model.__class__.__name__.endswith("RotaryEmbedding"):
            return model

        if hasattr(model, 'children'):
            for child in model.children():
                result = self._get_rotary_embedding_module(child)
                if result is not None:
                    return result
        return None

    def _get_prepare_cos_sin(self):
        if self._rotary_embed_module is not None and hasattr(self._rotary_embed_module, 'prepare_cos_sin'):
            return self._rotary_embed_module.prepare_cos_sin
        return None

    def _reset_rotary_cos_sin(self):
        if hasattr(self._rotary_embed_module, "cos"):
            delattr(self._rotary_embed_module, "cos")
        if hasattr(self._rotary_embed_module, "sin"):
            delattr(self._rotary_embed_module, "sin")

    def _set_attn_bias(self, attn_metadata, batch_size, seq_len, device, dtype):
        if (attn_metadata is None or (self.prefill_use_fusedsdpa and attn_metadata.block_list is None)
                or not attn_metadata.is_prompt):
            return attn_metadata

        if attn_metadata.attn_bias is not None:
            return attn_metadata

        prefill_metadata = attn_metadata

        seq_lens_t = prefill_metadata.seq_lens_tensor
        context_lens_t = prefill_metadata.context_lens_tensor

        block_list = attn_metadata.block_list
        max_context_len = (block_list.size(-1) // batch_size if block_list is not None else 0)
        max_context_len = max_context_len * self.block_size
        past_mask = torch.arange(0, max_context_len, dtype=torch.int32, device=device)
        past_mask = (past_mask.view(1, -1).expand(batch_size, -1).ge(context_lens_t.view(-1, 1)).view(
            batch_size, 1, -1).expand(batch_size, seq_len, -1).view(batch_size, 1, seq_len, -1))

        len_mask = (torch.arange(0, seq_len, device=device, dtype=torch.int32).view(1, seq_len).ge(
            seq_lens_t.unsqueeze(-1)).view(batch_size, 1, 1, seq_len))
        causal_mask = torch.triu(torch.ones((batch_size, 1, seq_len, seq_len), device=device, dtype=torch.bool),
                                 diagonal=1)
        mask = causal_mask.logical_or(len_mask)
        mask = torch.concat((past_mask, mask), dim=-1)
        attn_bias = (torch.zeros_like(mask, dtype=dtype).masked_fill_(mask, -math.inf))
        attn_metadata = custom_tuple_replace(prefill_metadata, "TrimmedAttentionMetadata", attn_bias=attn_bias)
        return attn_metadata

    def _set_attn_bias_for_sliding_window(self, attn_metadata, batch_size, seq_len, window_size, device, dtype):

        if (attn_metadata is None or not attn_metadata.is_prompt):
            return attn_metadata

        prefill_metadata = attn_metadata
        shift = 0

        # FusedSDPA with window_size is only supported when the seq_len is multiple of the slice_size
        if self.prefill_use_fusedsdpa and self.use_window_sdpa and \
            seq_len >= self.slice_thld and self.slice_size != 0 and \
            seq_len % self.slice_size == 0 and attn_metadata.block_list is None:
            # no need to set sliding window mask, just use built-in window-sdpa
            return attn_metadata

        if self.prefill_use_fusedsdpa and attn_metadata.block_list is not None:
            context_lens_t = prefill_metadata.context_lens_tensor

            block_list = attn_metadata.block_list
            max_context_len = (block_list.size(-1) // batch_size if block_list is not None else 0)
            max_context_len = max_context_len * self.block_size

            invalid_lens_t = context_lens_t - window_size + torch.arange(seq_len, device=device) - 1
            past_indices = torch.arange(max_context_len, device=device)
            past_mask = ((past_indices.unsqueeze(0) > invalid_lens_t.unsqueeze(-1)) &
                         (past_indices.unsqueeze(0) < context_lens_t.unsqueeze(-1).unsqueeze(0))).unsqueeze(1)

            # Create boolean sliding window mask
            causal_mask = torch.tril(torch.ones(seq_len, seq_len, dtype=torch.bool, device=device), diagonal=shift)
            causal_mask = torch.triu(causal_mask, diagonal=shift - window_size + 1)
            causal_mask = causal_mask.view(batch_size, 1, seq_len, seq_len)

            # TODO: Investigate further - Removing Padding cause accuracy issue
            # seq_lens_t = prefill_metadata.seq_lens_tensor
            # len_mask = (torch.arange(0, seq_len, device=device, dtype=torch.int32).view(1, seq_len).lt(
            #     seq_lens_t.unsqueeze(-1)).view(batch_size, 1, 1, seq_len))
            # causal_mask = causal_mask.logical_and(len_mask)

            mask = torch.concat((past_mask, causal_mask), dim=-1)
            attn_bias = torch.where(mask, torch.tensor(0.0, dtype=dtype, device=device),
                                    torch.tensor(float('-inf'), dtype=dtype, device=device))
        else:
            # CAUSAL MASK without removing padding (CAUSAL+sliding window)
            # removing padding cause accuracy issue for images input
            tensor = torch.full((batch_size, 1, seq_len, seq_len), device=device, dtype=dtype, fill_value=1)
            mask = torch.tril(tensor, diagonal=shift)
            mask = torch.triu(mask, diagonal=shift - window_size + 1)
            attn_bias = torch.log(mask)

        attn_metadata = prefill_metadata._replace(window_attn_bias=attn_bias)
        return attn_metadata

    def _set_attn_bias_for_chunked_attention(self, attn_metadata, batch_size, seq_len, chunk_size, device, dtype):
        if (attn_metadata is None or not attn_metadata.is_prompt):
            return attn_metadata

        prefill_metadata = attn_metadata
        shift = 0

        if self.prefill_use_fusedsdpa and attn_metadata.block_list is not None:

            context_lens_t = prefill_metadata.context_lens_tensor
            block_list = prefill_metadata.block_list
            max_context_len = (block_list.size(-1) // batch_size if block_list is not None else 0)
            max_context_len = max_context_len * self.block_size
            query_positions = torch.arange(seq_len, device=device)
            total_token_positions = context_lens_t.unsqueeze(-1) + query_positions.unsqueeze(0)
            which_chunk = (total_token_positions // chunk_size)
            chunk_start_positions = which_chunk * chunk_size
            invalid_lens_t = chunk_start_positions - 1

            past_indices = torch.arange(max_context_len, device=device)
            past_mask = (
                (past_indices.unsqueeze(0).unsqueeze(0) > invalid_lens_t.unsqueeze(-1)) &
                (past_indices.unsqueeze(0).unsqueeze(0) < context_lens_t.unsqueeze(-1).unsqueeze(-1))).unsqueeze(1)

            causal_mask = torch.tril(torch.ones(seq_len, seq_len, dtype=torch.bool, device=device), diagonal=shift)
            query_chunk_ids = which_chunk[0]
            same_chunk_mask = query_chunk_ids.unsqueeze(0) == query_chunk_ids.unsqueeze(1)

            causal_mask = causal_mask & same_chunk_mask
            causal_mask = causal_mask.unsqueeze(0).unsqueeze(0).expand(batch_size, 1, seq_len, seq_len)

            mask = torch.concat((past_mask, causal_mask), dim=-1)
            attn_bias = torch.where(mask, torch.tensor(0.0, dtype=dtype, device=device),
                                    torch.tensor(float('-inf'), dtype=dtype, device=device))
        else:
            tensor = torch.full((batch_size, 1, seq_len, seq_len), device=device, dtype=dtype, fill_value=1)
            mask = torch.tril(tensor, diagonal=shift)
            idx = torch.arange(seq_len, device=device)
            chunk_id = idx // chunk_size
            same_chunk = chunk_id.unsqueeze(0) == chunk_id.unsqueeze(1)
            same_chunk = same_chunk.unsqueeze(0).unsqueeze(0)
            mask = torch.where(same_chunk, mask, torch.tensor(0.0, dtype=dtype, device=device))
            attn_bias = torch.log(mask)

        attn_metadata = custom_tuple_replace(prefill_metadata, "TrimmedAttentionMetadata", chunked_attn_bias=attn_bias)
        return attn_metadata

    def _set_block_mapping(self,
                           metadata,
                           batch_size,
                           device,
                           dtype,
                           is_window_block=False,
                           update_for_chunked_attention=False):
        if is_window_block:
            block_usage = metadata.window_block_usage
            block_groups = metadata.window_block_groups
        elif update_for_chunked_attention:
            block_usage = metadata.chunked_block_usage
            block_groups = metadata.chunked_block_groups
        else:
            block_usage = metadata.block_usage
            block_groups = metadata.block_groups

        mask = torch.arange(0, self.block_size, device=device, dtype=torch.int32).unsqueeze(0)
        mask = mask >= block_usage.unsqueeze(-1)
        attn_bias = (torch.zeros_like(mask, dtype=dtype).masked_fill_(mask, -math.inf))

        if not is_fake_hpu():
            block_mapping = torch.nn.functional.one_hot(block_groups, num_classes=batch_size)
        else:
            # Unfortunately one_hot on CPU
            # doesn't handle out of bounds classes so we need to convert
            # all negative values to 0 (block_mapping) or bs (block_groups)
            block_groups = block_groups.to(torch.long)
            block_mapping = torch.nn.functional.relu(block_groups)
            block_mapping = torch.nn.functional.one_hot(block_mapping, num_classes=batch_size)
            oob_values = block_groups.lt(0)
            block_mapping.masked_fill_(oob_values.unsqueeze(-1), 0)
            block_groups.masked_fill_(oob_values, batch_size)
            if is_window_block:
                metadata = custom_tuple_replace(metadata, "TrimmedAttentionMetadata", window_block_groups=block_groups)
            else:
                metadata = custom_tuple_replace(metadata, "TrimmedAttentionMetadata", block_groups=block_groups)
        block_mapping = block_mapping.to(dtype)
        if is_window_block:
            metadata = custom_tuple_replace(metadata,
                                            "TrimmedAttentionMetadata",
                                            window_block_mapping=block_mapping,
                                            window_attn_bias=attn_bias)
        elif update_for_chunked_attention:
            metadata = custom_tuple_replace(metadata,
                                            "TrimmedAttentionMetadata",
                                            chunked_block_mapping=block_mapping,
                                            chunked_attn_bias=attn_bias)
        else:
            metadata = custom_tuple_replace(metadata,
                                            "TrimmedAttentionMetadata",
                                            block_mapping=block_mapping,
                                            attn_bias=attn_bias)
        return metadata

    def _update_metadata(self, attn_metadata, batch_size, seq_len, device, dtype, model_has_chunked_attention=False):
        if attn_metadata.is_prompt:
            attn_metadata = self._set_attn_bias(attn_metadata, batch_size, seq_len, device, dtype)
            if self.interleaved_sliding_window and self.sliding_window is not None:
                attn_metadata = self._set_attn_bias_for_sliding_window(attn_metadata, batch_size, seq_len,
                                                                       self.sliding_window, device, dtype)
            if model_has_chunked_attention:
                attn_metadata = self._set_attn_bias_for_chunked_attention(
                    attn_metadata, batch_size, seq_len, self.model.config.text_config.attention_chunk_size, device,
                    dtype)
        else:
            attn_metadata = self._set_block_mapping(attn_metadata, batch_size, device, dtype)
            if model_has_chunked_attention:
                attn_metadata = self._set_block_mapping(attn_metadata,
                                                        batch_size,
                                                        device,
                                                        dtype,
                                                        update_for_chunked_attention=True)
            if self.interleaved_sliding_window and self.sliding_window is not None:
                attn_metadata = self._set_block_mapping(attn_metadata, batch_size, device, dtype, True)
        return attn_metadata

    def forward(self, *args, **kwargs):
        # TODO(kzawora): something goes VERY WRONG when operating on
        # kwargs['attn_metadata'].slot_mapping, compared to untrimmed metadata
        kwargs = kwargs.copy()
        #        selected_token_indices = kwargs.pop('selected_token_indices')
        if 'lora_mask' in kwargs:
            lora_mask = kwargs['lora_mask']
            LoraMask.setLoraMask(lora_mask)
            kwargs.pop('lora_mask')
        if 'warmup_mode' in kwargs:
            kwargs.pop('warmup_mode')
        input_ids = kwargs['input_ids']
        model_has_chunked_attention = kwargs.pop('model_has_chunked_attention', False)
        if not self.unified_attn:
            kwargs['attn_metadata'] = self._update_metadata(kwargs['attn_metadata'], input_ids.size(0),
                                                            input_ids.size(1), input_ids.device, self.dtype,
                                                            model_has_chunked_attention)
        if self._rotary_prepare_cos_sin is not None:
            self._rotary_prepare_cos_sin(kwargs['positions'], recompute_cos_sin=self.recompute_cos_sin)
        attn_meta = kwargs.pop('attn_metadata')
        if 'kv_caches' in kwargs:
            kwargs.pop('kv_caches')

        # If multimodal inputs, update kwargs
        model_mm_kwargs = kwargs.pop('model_mm_kwargs', None)
        if model_mm_kwargs is not None:
            kwargs.update(model_mm_kwargs)

        num_real_tokens = input_ids.size(0) * input_ids.size(1)

        if self.flatten_input:
            kwargs['input_ids'] = input_ids.view(-1)
        # here num_tokens and num_tokens_across_dp are dummy values which are
        # used to skip sync in forward_context between DP ranks
        with set_forward_context(attn_meta,
                                 self.vllm_config,
                                 num_tokens=self.dummy_num_input_tokens,
                                 num_tokens_across_dp=self.dummy_num_tokens_across_dp_cpu), set_hpu_dp_metadata(
                                     self.vllm_config, num_real_tokens):
            hidden_states = self.model(*args, **kwargs)
            if self._rotary_prepare_cos_sin is not None:
                self._reset_rotary_cos_sin()
        return hidden_states

    def embed_input_ids(self, input_ids, multimodal_embeddings=None, is_multimodal=False):
        return self.model.embed_input_ids(input_ids=input_ids,
                                          multimodal_embeddings=multimodal_embeddings,
                                          is_multimodal=is_multimodal)

    def embed_multimodal(self, **batched_mm_inputs):
        return self.model.embed_multimodal(**batched_mm_inputs)

    def compute_logits(self, *args, **kwargs):
        return self.model.compute_logits(*args, **kwargs)

    # def sample(self, *args, **kwargs):
    #    return self.sampler(*args, **kwargs)

    def generate_proposals(self, *args, **kwargs):
        return self.model.generate_proposals(*args, **kwargs)

_rotary_embed_module instance-attribute

_rotary_embed_module = _get_rotary_embedding_module(model)

_rotary_prepare_cos_sin instance-attribute

_rotary_prepare_cos_sin = _get_prepare_cos_sin()

block_size instance-attribute

block_size = block_size

dtype instance-attribute

dtype = dtype

dummy_num_input_tokens instance-attribute

dummy_num_input_tokens = -1

dummy_num_tokens_across_dp_cpu instance-attribute

dummy_num_tokens_across_dp_cpu = tensor(
    num_tokens_across_dp, device="cpu", dtype=int32
)

flatten_input instance-attribute

flatten_input = flatten_input

interleaved_sliding_window instance-attribute

interleaved_sliding_window = is_interleaved(hf_text_config)

is_mm_optimized instance-attribute

is_mm_optimized = is_mm_optimized(model)

model instance-attribute

model = model

num_tokens_across_dp instance-attribute

num_tokens_across_dp = [
    dummy_num_input_tokens
] * data_parallel_size

prefill_use_fusedsdpa instance-attribute

prefill_use_fusedsdpa = prompt_attn_impl == 'fsdpa_impl'

recompute_cos_sin instance-attribute

recompute_cos_sin = lower() in ['1', 'true']

slice_size instance-attribute

slice_size = int(getenv('PT_HPU_SDPA_BC_FACTOR', '1024'))

slice_thld instance-attribute

slice_thld = int(get('VLLM_FUSEDSDPA_SLIDE_THLD', '8192'))

sliding_window instance-attribute

sliding_window = get_sliding_window()

unified_attn instance-attribute

unified_attn = unified_attn

unified_attn_persistent_ctx instance-attribute

unified_attn_persistent_ctx = None

use_window_sdpa instance-attribute

use_window_sdpa = lower() in ('1', 'true')

vllm_config instance-attribute

vllm_config = vllm_config

__init__

__init__(model, vllm_config)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def __init__(self, model, vllm_config):
    super().__init__()
    self.model = model
    self.prefill_use_fusedsdpa = get_config().prompt_attn_impl == 'fsdpa_impl'
    self.recompute_cos_sin = os.getenv('VLLM_COS_SIN_RECOMPUTE', 'false').lower() in ['1', 'true']
    self.vllm_config = vllm_config
    self.block_size = vllm_config.cache_config.block_size
    self.dtype = vllm_config.model_config.dtype
    self._rotary_embed_module = self._get_rotary_embedding_module(self.model)
    self._rotary_prepare_cos_sin = self._get_prepare_cos_sin()
    self.unified_attn = get_config().unified_attn
    self.unified_attn_persistent_ctx = None
    self.flatten_input = get_config().flatten_input
    self.is_mm_optimized = is_mm_optimized(self.model)
    self.sliding_window = vllm_config.model_config.get_sliding_window()
    self.interleaved_sliding_window = is_interleaved(vllm_config.model_config.hf_text_config)
    if self.interleaved_sliding_window and self.sliding_window is not None:
        self.use_window_sdpa = os.getenv("PT_HPU_SDPA_QKV_SLICE_MODE_FWD", "false").strip().lower() in ("1", "true")
        self.slice_size = int(os.getenv("PT_HPU_SDPA_BC_FACTOR", "1024"))
        self.slice_thld = int(os.environ.get('VLLM_FUSEDSDPA_SLIDE_THLD', '8192'))

    # for DP
    self.dummy_num_input_tokens = -1
    self.num_tokens_across_dp = [self.dummy_num_input_tokens] * self.vllm_config.parallel_config.data_parallel_size
    self.dummy_num_tokens_across_dp_cpu = torch.tensor(self.num_tokens_across_dp, device='cpu', dtype=torch.int32)

    # Vision embedding can be also wrapped in HPU graph once all the dynamic shape is removed.
    # Performance can be greatly improved.
    if htorch.utils.internal.is_lazy() and \
       MULTIMODAL_REGISTRY.supports_multimodal_inputs(vllm_config.model_config) and self.is_mm_optimized:
        if hasattr(self.model, 'vision_tower'):
            self.model.vision_tower = htorch.hpu.wrap_in_hpu_graph(self.model.vision_tower,
                                                                   disable_tensor_cache=False)
        if hasattr(self.model, 'multi_modal_projector'):
            self.model.multi_modal_projector = \
                    htorch.hpu.wrap_in_hpu_graph( \
                    self.model.multi_modal_projector, \
                    disable_tensor_cache=True)

_get_prepare_cos_sin

_get_prepare_cos_sin()
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _get_prepare_cos_sin(self):
    if self._rotary_embed_module is not None and hasattr(self._rotary_embed_module, 'prepare_cos_sin'):
        return self._rotary_embed_module.prepare_cos_sin
    return None

_get_rotary_embedding_module

_get_rotary_embedding_module(model: Module)

动态获取模型中的RotaryEmbedding层。此函数将递归搜索模块层级结构,以查找并返回RotaryEmbedding层。如果未找到此类层,则返回None。

Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _get_rotary_embedding_module(self, model: torch.nn.Module):
    """
    Dynamically get the RotaryEmbedding layer in the model.
    This function will recursively search through the module
    hierarchy to find and return a RotaryEmbedding layer.
    If no such layer is found, it returns None.
    """
    if model is None:
        return None

    if model.__class__.__name__.endswith("RotaryEmbedding"):
        return model

    if hasattr(model, 'children'):
        for child in model.children():
            result = self._get_rotary_embedding_module(child)
            if result is not None:
                return result
    return None

_reset_rotary_cos_sin

_reset_rotary_cos_sin()
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _reset_rotary_cos_sin(self):
    if hasattr(self._rotary_embed_module, "cos"):
        delattr(self._rotary_embed_module, "cos")
    if hasattr(self._rotary_embed_module, "sin"):
        delattr(self._rotary_embed_module, "sin")

_set_attn_bias

_set_attn_bias(
    attn_metadata, batch_size, seq_len, device, dtype
)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _set_attn_bias(self, attn_metadata, batch_size, seq_len, device, dtype):
    if (attn_metadata is None or (self.prefill_use_fusedsdpa and attn_metadata.block_list is None)
            or not attn_metadata.is_prompt):
        return attn_metadata

    if attn_metadata.attn_bias is not None:
        return attn_metadata

    prefill_metadata = attn_metadata

    seq_lens_t = prefill_metadata.seq_lens_tensor
    context_lens_t = prefill_metadata.context_lens_tensor

    block_list = attn_metadata.block_list
    max_context_len = (block_list.size(-1) // batch_size if block_list is not None else 0)
    max_context_len = max_context_len * self.block_size
    past_mask = torch.arange(0, max_context_len, dtype=torch.int32, device=device)
    past_mask = (past_mask.view(1, -1).expand(batch_size, -1).ge(context_lens_t.view(-1, 1)).view(
        batch_size, 1, -1).expand(batch_size, seq_len, -1).view(batch_size, 1, seq_len, -1))

    len_mask = (torch.arange(0, seq_len, device=device, dtype=torch.int32).view(1, seq_len).ge(
        seq_lens_t.unsqueeze(-1)).view(batch_size, 1, 1, seq_len))
    causal_mask = torch.triu(torch.ones((batch_size, 1, seq_len, seq_len), device=device, dtype=torch.bool),
                             diagonal=1)
    mask = causal_mask.logical_or(len_mask)
    mask = torch.concat((past_mask, mask), dim=-1)
    attn_bias = (torch.zeros_like(mask, dtype=dtype).masked_fill_(mask, -math.inf))
    attn_metadata = custom_tuple_replace(prefill_metadata, "TrimmedAttentionMetadata", attn_bias=attn_bias)
    return attn_metadata

_set_attn_bias_for_chunked_attention

_set_attn_bias_for_chunked_attention(
    attn_metadata,
    batch_size,
    seq_len,
    chunk_size,
    device,
    dtype,
)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _set_attn_bias_for_chunked_attention(self, attn_metadata, batch_size, seq_len, chunk_size, device, dtype):
    if (attn_metadata is None or not attn_metadata.is_prompt):
        return attn_metadata

    prefill_metadata = attn_metadata
    shift = 0

    if self.prefill_use_fusedsdpa and attn_metadata.block_list is not None:

        context_lens_t = prefill_metadata.context_lens_tensor
        block_list = prefill_metadata.block_list
        max_context_len = (block_list.size(-1) // batch_size if block_list is not None else 0)
        max_context_len = max_context_len * self.block_size
        query_positions = torch.arange(seq_len, device=device)
        total_token_positions = context_lens_t.unsqueeze(-1) + query_positions.unsqueeze(0)
        which_chunk = (total_token_positions // chunk_size)
        chunk_start_positions = which_chunk * chunk_size
        invalid_lens_t = chunk_start_positions - 1

        past_indices = torch.arange(max_context_len, device=device)
        past_mask = (
            (past_indices.unsqueeze(0).unsqueeze(0) > invalid_lens_t.unsqueeze(-1)) &
            (past_indices.unsqueeze(0).unsqueeze(0) < context_lens_t.unsqueeze(-1).unsqueeze(-1))).unsqueeze(1)

        causal_mask = torch.tril(torch.ones(seq_len, seq_len, dtype=torch.bool, device=device), diagonal=shift)
        query_chunk_ids = which_chunk[0]
        same_chunk_mask = query_chunk_ids.unsqueeze(0) == query_chunk_ids.unsqueeze(1)

        causal_mask = causal_mask & same_chunk_mask
        causal_mask = causal_mask.unsqueeze(0).unsqueeze(0).expand(batch_size, 1, seq_len, seq_len)

        mask = torch.concat((past_mask, causal_mask), dim=-1)
        attn_bias = torch.where(mask, torch.tensor(0.0, dtype=dtype, device=device),
                                torch.tensor(float('-inf'), dtype=dtype, device=device))
    else:
        tensor = torch.full((batch_size, 1, seq_len, seq_len), device=device, dtype=dtype, fill_value=1)
        mask = torch.tril(tensor, diagonal=shift)
        idx = torch.arange(seq_len, device=device)
        chunk_id = idx // chunk_size
        same_chunk = chunk_id.unsqueeze(0) == chunk_id.unsqueeze(1)
        same_chunk = same_chunk.unsqueeze(0).unsqueeze(0)
        mask = torch.where(same_chunk, mask, torch.tensor(0.0, dtype=dtype, device=device))
        attn_bias = torch.log(mask)

    attn_metadata = custom_tuple_replace(prefill_metadata, "TrimmedAttentionMetadata", chunked_attn_bias=attn_bias)
    return attn_metadata

_set_attn_bias_for_sliding_window

_set_attn_bias_for_sliding_window(
    attn_metadata,
    batch_size,
    seq_len,
    window_size,
    device,
    dtype,
)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _set_attn_bias_for_sliding_window(self, attn_metadata, batch_size, seq_len, window_size, device, dtype):

    if (attn_metadata is None or not attn_metadata.is_prompt):
        return attn_metadata

    prefill_metadata = attn_metadata
    shift = 0

    # FusedSDPA with window_size is only supported when the seq_len is multiple of the slice_size
    if self.prefill_use_fusedsdpa and self.use_window_sdpa and \
        seq_len >= self.slice_thld and self.slice_size != 0 and \
        seq_len % self.slice_size == 0 and attn_metadata.block_list is None:
        # no need to set sliding window mask, just use built-in window-sdpa
        return attn_metadata

    if self.prefill_use_fusedsdpa and attn_metadata.block_list is not None:
        context_lens_t = prefill_metadata.context_lens_tensor

        block_list = attn_metadata.block_list
        max_context_len = (block_list.size(-1) // batch_size if block_list is not None else 0)
        max_context_len = max_context_len * self.block_size

        invalid_lens_t = context_lens_t - window_size + torch.arange(seq_len, device=device) - 1
        past_indices = torch.arange(max_context_len, device=device)
        past_mask = ((past_indices.unsqueeze(0) > invalid_lens_t.unsqueeze(-1)) &
                     (past_indices.unsqueeze(0) < context_lens_t.unsqueeze(-1).unsqueeze(0))).unsqueeze(1)

        # Create boolean sliding window mask
        causal_mask = torch.tril(torch.ones(seq_len, seq_len, dtype=torch.bool, device=device), diagonal=shift)
        causal_mask = torch.triu(causal_mask, diagonal=shift - window_size + 1)
        causal_mask = causal_mask.view(batch_size, 1, seq_len, seq_len)

        # TODO: Investigate further - Removing Padding cause accuracy issue
        # seq_lens_t = prefill_metadata.seq_lens_tensor
        # len_mask = (torch.arange(0, seq_len, device=device, dtype=torch.int32).view(1, seq_len).lt(
        #     seq_lens_t.unsqueeze(-1)).view(batch_size, 1, 1, seq_len))
        # causal_mask = causal_mask.logical_and(len_mask)

        mask = torch.concat((past_mask, causal_mask), dim=-1)
        attn_bias = torch.where(mask, torch.tensor(0.0, dtype=dtype, device=device),
                                torch.tensor(float('-inf'), dtype=dtype, device=device))
    else:
        # CAUSAL MASK without removing padding (CAUSAL+sliding window)
        # removing padding cause accuracy issue for images input
        tensor = torch.full((batch_size, 1, seq_len, seq_len), device=device, dtype=dtype, fill_value=1)
        mask = torch.tril(tensor, diagonal=shift)
        mask = torch.triu(mask, diagonal=shift - window_size + 1)
        attn_bias = torch.log(mask)

    attn_metadata = prefill_metadata._replace(window_attn_bias=attn_bias)
    return attn_metadata

_set_block_mapping

_set_block_mapping(
    metadata,
    batch_size,
    device,
    dtype,
    is_window_block=False,
    update_for_chunked_attention=False,
)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _set_block_mapping(self,
                       metadata,
                       batch_size,
                       device,
                       dtype,
                       is_window_block=False,
                       update_for_chunked_attention=False):
    if is_window_block:
        block_usage = metadata.window_block_usage
        block_groups = metadata.window_block_groups
    elif update_for_chunked_attention:
        block_usage = metadata.chunked_block_usage
        block_groups = metadata.chunked_block_groups
    else:
        block_usage = metadata.block_usage
        block_groups = metadata.block_groups

    mask = torch.arange(0, self.block_size, device=device, dtype=torch.int32).unsqueeze(0)
    mask = mask >= block_usage.unsqueeze(-1)
    attn_bias = (torch.zeros_like(mask, dtype=dtype).masked_fill_(mask, -math.inf))

    if not is_fake_hpu():
        block_mapping = torch.nn.functional.one_hot(block_groups, num_classes=batch_size)
    else:
        # Unfortunately one_hot on CPU
        # doesn't handle out of bounds classes so we need to convert
        # all negative values to 0 (block_mapping) or bs (block_groups)
        block_groups = block_groups.to(torch.long)
        block_mapping = torch.nn.functional.relu(block_groups)
        block_mapping = torch.nn.functional.one_hot(block_mapping, num_classes=batch_size)
        oob_values = block_groups.lt(0)
        block_mapping.masked_fill_(oob_values.unsqueeze(-1), 0)
        block_groups.masked_fill_(oob_values, batch_size)
        if is_window_block:
            metadata = custom_tuple_replace(metadata, "TrimmedAttentionMetadata", window_block_groups=block_groups)
        else:
            metadata = custom_tuple_replace(metadata, "TrimmedAttentionMetadata", block_groups=block_groups)
    block_mapping = block_mapping.to(dtype)
    if is_window_block:
        metadata = custom_tuple_replace(metadata,
                                        "TrimmedAttentionMetadata",
                                        window_block_mapping=block_mapping,
                                        window_attn_bias=attn_bias)
    elif update_for_chunked_attention:
        metadata = custom_tuple_replace(metadata,
                                        "TrimmedAttentionMetadata",
                                        chunked_block_mapping=block_mapping,
                                        chunked_attn_bias=attn_bias)
    else:
        metadata = custom_tuple_replace(metadata,
                                        "TrimmedAttentionMetadata",
                                        block_mapping=block_mapping,
                                        attn_bias=attn_bias)
    return metadata

_update_metadata

_update_metadata(
    attn_metadata,
    batch_size,
    seq_len,
    device,
    dtype,
    model_has_chunked_attention=False,
)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _update_metadata(self, attn_metadata, batch_size, seq_len, device, dtype, model_has_chunked_attention=False):
    if attn_metadata.is_prompt:
        attn_metadata = self._set_attn_bias(attn_metadata, batch_size, seq_len, device, dtype)
        if self.interleaved_sliding_window and self.sliding_window is not None:
            attn_metadata = self._set_attn_bias_for_sliding_window(attn_metadata, batch_size, seq_len,
                                                                   self.sliding_window, device, dtype)
        if model_has_chunked_attention:
            attn_metadata = self._set_attn_bias_for_chunked_attention(
                attn_metadata, batch_size, seq_len, self.model.config.text_config.attention_chunk_size, device,
                dtype)
    else:
        attn_metadata = self._set_block_mapping(attn_metadata, batch_size, device, dtype)
        if model_has_chunked_attention:
            attn_metadata = self._set_block_mapping(attn_metadata,
                                                    batch_size,
                                                    device,
                                                    dtype,
                                                    update_for_chunked_attention=True)
        if self.interleaved_sliding_window and self.sliding_window is not None:
            attn_metadata = self._set_block_mapping(attn_metadata, batch_size, device, dtype, True)
    return attn_metadata

compute_logits

compute_logits(*args, **kwargs)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def compute_logits(self, *args, **kwargs):
    return self.model.compute_logits(*args, **kwargs)

embed_input_ids

embed_input_ids(
    input_ids,
    multimodal_embeddings=None,
    is_multimodal=False,
)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def embed_input_ids(self, input_ids, multimodal_embeddings=None, is_multimodal=False):
    return self.model.embed_input_ids(input_ids=input_ids,
                                      multimodal_embeddings=multimodal_embeddings,
                                      is_multimodal=is_multimodal)

embed_multimodal

embed_multimodal(**batched_mm_inputs)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def embed_multimodal(self, **batched_mm_inputs):
    return self.model.embed_multimodal(**batched_mm_inputs)

forward

forward(*args, **kwargs)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def forward(self, *args, **kwargs):
    # TODO(kzawora): something goes VERY WRONG when operating on
    # kwargs['attn_metadata'].slot_mapping, compared to untrimmed metadata
    kwargs = kwargs.copy()
    #        selected_token_indices = kwargs.pop('selected_token_indices')
    if 'lora_mask' in kwargs:
        lora_mask = kwargs['lora_mask']
        LoraMask.setLoraMask(lora_mask)
        kwargs.pop('lora_mask')
    if 'warmup_mode' in kwargs:
        kwargs.pop('warmup_mode')
    input_ids = kwargs['input_ids']
    model_has_chunked_attention = kwargs.pop('model_has_chunked_attention', False)
    if not self.unified_attn:
        kwargs['attn_metadata'] = self._update_metadata(kwargs['attn_metadata'], input_ids.size(0),
                                                        input_ids.size(1), input_ids.device, self.dtype,
                                                        model_has_chunked_attention)
    if self._rotary_prepare_cos_sin is not None:
        self._rotary_prepare_cos_sin(kwargs['positions'], recompute_cos_sin=self.recompute_cos_sin)
    attn_meta = kwargs.pop('attn_metadata')
    if 'kv_caches' in kwargs:
        kwargs.pop('kv_caches')

    # If multimodal inputs, update kwargs
    model_mm_kwargs = kwargs.pop('model_mm_kwargs', None)
    if model_mm_kwargs is not None:
        kwargs.update(model_mm_kwargs)

    num_real_tokens = input_ids.size(0) * input_ids.size(1)

    if self.flatten_input:
        kwargs['input_ids'] = input_ids.view(-1)
    # here num_tokens and num_tokens_across_dp are dummy values which are
    # used to skip sync in forward_context between DP ranks
    with set_forward_context(attn_meta,
                             self.vllm_config,
                             num_tokens=self.dummy_num_input_tokens,
                             num_tokens_across_dp=self.dummy_num_tokens_across_dp_cpu), set_hpu_dp_metadata(
                                 self.vllm_config, num_real_tokens):
        hidden_states = self.model(*args, **kwargs)
        if self._rotary_prepare_cos_sin is not None:
            self._reset_rotary_cos_sin()
    return hidden_states

generate_proposals

generate_proposals(*args, **kwargs)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def generate_proposals(self, *args, **kwargs):
    return self.model.generate_proposals(*args, **kwargs)

PrefillInputData dataclass

Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
@dataclass
class PrefillInputData:
    request_ids: list = empty_list()
    prompt_lens: list = empty_list()
    token_ids: list = empty_list()
    position_ids: list = empty_list()
    attn_metadata: list = empty_list()
    logits_indices: list = empty_list()
    logits_requests: list = empty_list()

attn_metadata class-attribute instance-attribute

attn_metadata: list = empty_list()

logits_indices class-attribute instance-attribute

logits_indices: list = empty_list()

logits_requests class-attribute instance-attribute

logits_requests: list = empty_list()

position_ids class-attribute instance-attribute

position_ids: list = empty_list()

prompt_lens class-attribute instance-attribute

prompt_lens: list = empty_list()

request_ids class-attribute instance-attribute

request_ids: list = empty_list()

token_ids class-attribute instance-attribute

token_ids: list = empty_list()

__init__

__init__(
    request_ids: list = empty_list(),
    prompt_lens: list = empty_list(),
    token_ids: list = empty_list(),
    position_ids: list = empty_list(),
    attn_metadata: list = empty_list(),
    logits_indices: list = empty_list(),
    logits_requests: list = empty_list(),
) -> None

PromptData dataclass

Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
@dataclass
class PromptData:
    input_tokens: torch.Tensor
    input_positions: torch.Tensor
    attn_metadata: HPUAttentionMetadataV1

attn_metadata instance-attribute

attn_metadata: HPUAttentionMetadataV1

input_positions instance-attribute

input_positions: Tensor

input_tokens instance-attribute

input_tokens: Tensor

__init__

__init__(
    input_tokens: Tensor,
    input_positions: Tensor,
    attn_metadata: HPUAttentionMetadataV1,
) -> None

PromptDecodeInfo dataclass

Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
@dataclass
class PromptDecodeInfo:
    prompt_req_ids: list[str]
    decode_req_ids: list[str]
    prompt_scheduled_tokens: list[int]

decode_req_ids instance-attribute

decode_req_ids: list[str]

prompt_req_ids instance-attribute

prompt_req_ids: list[str]

prompt_scheduled_tokens instance-attribute

prompt_scheduled_tokens: list[int]

__init__

__init__(
    prompt_req_ids: list[str],
    decode_req_ids: list[str],
    prompt_scheduled_tokens: list[int],
) -> None

TensorTuple

基类:tuple

一个专门用于保存嵌套torch.Tensors的tuple子类,提供.shape和.device属性。

它确保嵌套结构不是不规则的,并且所有包含的张量都位于同一设备上。

Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
class TensorTuple(tuple):
    """
    A tuple subclass designed to hold nested torch.Tensors, providing
    .shape and .device properties.

    It ensures that the nested structure is not ragged and that all
    contained tensors reside on the same device.
    """

    _shape: tuple[int, ...]
    _device: Optional[torch.device]
    _dtype: Optional[torch.dtype]

    def __new__(cls, iterable):
        # First, we create the actual tuple object instance
        instance = super().__new__(cls, iterable)

        # Now, compute and attach the custom properties.
        # This is done here because tuples are immutable.
        # We store them with a leading underscore.
        instance._shape = get_shape(instance)
        instance._device = _find_tensors_and_validate(instance, 'device')
        instance._dtype = _find_tensors_and_validate(instance, 'dtype')

        return instance

    @property
    def shape(self):
        """Returns the shape of the nested tuple structure."""
        return self._shape

    @property
    def device(self):
        """
        Returns the torch.device of the tensors within the tuple.
        Returns None if no tensors are present.
        """
        return self._device

    @property
    def dtype(self):
        """Returns the torch.dtype of the tensors within the tuple."""
        return self._dtype

_device instance-attribute

_device: Optional[device]

_dtype instance-attribute

_dtype: Optional[dtype]

_shape instance-attribute

_shape: tuple[int, ...]

device property

device

返回元组中张量的torch.device。如果不存在张量,则返回None。

dtype property

dtype

返回元组中张量的torch.dtype。

shape property

shape

返回嵌套元组结构的形状。

__new__

__new__(iterable)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def __new__(cls, iterable):
    # First, we create the actual tuple object instance
    instance = super().__new__(cls, iterable)

    # Now, compute and attach the custom properties.
    # This is done here because tuples are immutable.
    # We store them with a leading underscore.
    instance._shape = get_shape(instance)
    instance._device = _find_tensors_and_validate(instance, 'device')
    instance._dtype = _find_tensors_and_validate(instance, 'dtype')

    return instance

_find_tensors_and_validate

_find_tensors_and_validate(data, attr_name)

一个通用的辅助函数,用于查找所有张量并验证特定属性(如“device”或“dtype”),确保它们都相同。

Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _find_tensors_and_validate(data, attr_name):
    """
    A generic helper to find all tensors and validate a specific attribute
    (like 'device' or 'dtype') ensuring they are all the same.
    """
    found_attr = None

    def find_tensors(nested_data):
        if isinstance(nested_data, torch.Tensor):
            yield nested_data
        elif isinstance(nested_data, (list, tuple)):
            for item in nested_data:
                yield from find_tensors(item)

    tensor_iterator = find_tensors(data)

    try:
        first_tensor = next(tensor_iterator)
        found_attr = getattr(first_tensor, attr_name)
    except StopIteration:
        return None  # No tensors found

    for tensor in tensor_iterator:
        current_attr = getattr(tensor, attr_name)
        if current_attr != found_attr:
            raise ValueError(f"Inconsistent {attr_name}: Found tensors with both '{found_attr}' and '{current_attr}'.")

    return found_attr

_maybe_wrap_in_hpu_graph

_maybe_wrap_in_hpu_graph(*args, **kwargs)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def _maybe_wrap_in_hpu_graph(*args, **kwargs):
    return htorch.hpu.wrap_in_hpu_graph(HpuModelAdapter(
        *args, **kwargs), disable_tensor_cache=True) if htorch.utils.internal.is_lazy() else HpuModelAdapter(
            *args, **kwargs)

bool_helper

bool_helper(value)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def bool_helper(value):
    value = value.lower()
    return value in ("y", "yes", "t", "true", "on", "1")

custom_tuple_replace

custom_tuple_replace(
    obj: object, typename: str, **to_override
)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def custom_tuple_replace(obj: object, typename: str, **to_override):
    # Torch compile dynamo doesn't support calling any named tuple
    # dynamic methods other than len and get_attr. This function is
    # a torch.compile friendly version of tuple._replace

    cached_type = _TYPE_CACHE[typename]['type']
    fields = _TYPE_CACHE[typename]['fields']
    values = {
        field: getattr(obj, field)
        for field in fields  # type: ignore
    }
    values.update(to_override)
    return cached_type(**values)  # type: ignore

ensure_decodes_first

ensure_decodes_first(b: InputBatch)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def ensure_decodes_first(b: InputBatch):
    num_reqs = b.num_reqs
    while True:
        # Find the first prompt index
        first_prompt_index = None
        for i in range(num_reqs):
            if b.num_computed_tokens_cpu[i] < b.num_prompt_tokens[i]:
                first_prompt_index = i
                break
        if first_prompt_index is None:
            break

        # Find the last decode index
        last_decode_index = None
        for i in reversed(range(num_reqs)):
            if b.num_computed_tokens_cpu[i] >= b.num_prompt_tokens[i]:
                last_decode_index = i
                break
        if last_decode_index is None:
            break

        # Sanity
        assert first_prompt_index != last_decode_index

        # Check if done
        if first_prompt_index > last_decode_index:
            break

        # Swap
        b.swap_states(first_prompt_index, last_decode_index)

flatten

flatten(in_list)

返回列表的扁平化表示

Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def flatten(in_list):
    """Return a flattened representation of a list"""
    return list(itertools.chain(*in_list))

gather_list

gather_list(input, indices, v)

使用索引从输入中收集值

Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def gather_list(input, indices, v):
    """Gather values from input using indices"""
    return [input[i] if i is not None else v for i in indices]

get_dp_padding

get_dp_padding(
    num_tokens: int, dp_size: int, dp_rank: int
) -> int
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def get_dp_padding(num_tokens: int, dp_size: int, dp_rank: int) -> int:
    if dp_size == 1:
        return 0

    group = get_dp_group().cpu_group

    num_tokens_across_dp = [0] * dp_size
    num_tokens_across_dp[dp_rank] = num_tokens
    num_tokens_tensor = torch.tensor(num_tokens_across_dp, dtype=torch.int32)
    torch.distributed.all_reduce(num_tokens_tensor, group=group)

    max_tokens_across_dp_cpu = torch.max(num_tokens_tensor).item()
    return max_tokens_across_dp_cpu - num_tokens

get_shape

get_shape(data)

递归查找嵌套元组或列表的形状。

Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def get_shape(data):
    """Recursively finds the shape of a nested tuple or list."""
    if isinstance(data, torch.Tensor):
        return data.shape

    if not isinstance(data, (list, tuple)):
        return ()  # End of a non-tensor branch

    if not data:
        return (0, )

    first_dim = len(data)
    sub_shape = get_shape(data[0])

    for item in data[1:]:
        if get_shape(item) != sub_shape:
            raise ValueError("Inconsistent dimensions: The structure is ragged.")

    return (first_dim, ) + sub_shape

get_target_layer_suffix_list

get_target_layer_suffix_list(model_type) -> list[str]
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def get_target_layer_suffix_list(model_type) -> list[str]:
    # This sets the suffix for the hidden layer name, which is controlled by
    # VLLM_CONFIG_HIDDEN_LAYERS. The default suffix is "DecoderLayer," which is
    # applicable for most language models such as LLaMA, Qwen, and BART. If the
    # model's decoder layer name differs from the default, it will need to
    # be specified here.
    decoder_layer_table = {
        "gpt_bigcode": "BigCodeBlock",
    }

    return [decoder_layer_table.get(model_type, "DecoderLayer"), "EncoderLayer"]

is_mm_optimized

is_mm_optimized(model)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def is_mm_optimized(model):
    return 'Gemma3ForConditionalGeneration' in str(type(model.model)) \
        if hasattr(model, 'model') else \
        'Gemma3ForConditionalGeneration' in str(type(model))

merge_contents

merge_contents(lhs: Mergeable, *rhs: Mergeable)

将数据类的所有内部列表扩展为

Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def merge_contents(lhs: Mergeable, *rhs: Mergeable):
    """Extends all internal lists of a dataclass with """
    """values from given objects"""
    lhs_type = type(lhs)
    lhs_tuple = shallow_tuple(lhs)
    for other in rhs:
        assert lhs_type is type(other), \
            'Only objects of the same type can be merged'
        for dst, src in zip(lhs_tuple, shallow_tuple(other)):
            dst.extend(src)

modify_model_layers

modify_model_layers(
    module: Module,
    suffix_list: list[str],
    n=1,
    counter=None,
)

目前在指定层末尾添加 mark_step。

Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def modify_model_layers(module: torch.nn.Module, suffix_list: list[str], n=1, counter=None):
    """Currently add mark_step at the end of specified layers.
    """

    def forward_hook(module, args, output):
        htorch.core.mark_step()
        return output

    if counter is None:
        counter = [0]

    for child_name, child_module in module.named_children():
        if any(child_module.__class__.__name__.endswith(layer) for layer in suffix_list):
            counter[0] += 1
            if counter[0] % n == 0:
                child_module.register_forward_hook(forward_hook)
        else:
            modify_model_layers(child_module, suffix_list, n, counter)

round_up

round_up(value: int, k: int)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def round_up(value: int, k: int):
    return (value + k - 1) // k * k

shallow_tuple

shallow_tuple(obj: Mergeable) -> tuple

返回一个带有数据类字段值的浅元组

Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def shallow_tuple(obj: Mergeable) -> tuple:
    """Returns a shallow tuple with dataclass field values"""
    # Unfortunately dataclasses.astuple deepcopies the data
    # se we can't use it
    return tuple(getattr(obj, field.name) for field in fields(obj))

subtuple

subtuple(
    obj: object,
    typename: str,
    to_copy: list[str],
    to_override: Optional[dict[str, object]] = None,
)
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def subtuple(obj: object, typename: str, to_copy: list[str], to_override: Optional[dict[str, object]] = None):
    if obj is None:
        return None
    if to_override is None:
        to_override = {}
    fields = set(to_copy) | set(to_override.keys())
    if type(obj) is dict:
        values = {key: obj[key] for key in fields if key in obj}
    else:
        values = {f: to_override.get(f, getattr(obj, f)) for f in fields}
    if typename not in _TYPE_CACHE:
        _TYPE_CACHE[typename] = {'type': collections.namedtuple(typename, ' '.join(fields)), 'fields': fields}
    return _TYPE_CACHE[typename]['type'](**values)  # type: ignore

trim_attn_metadata

trim_attn_metadata(
    metadata: HPUAttentionMetadataV1,
) -> object
Source code in vllm_gaudi/v1/worker/hpu_model_runner.py
def trim_attn_metadata(metadata: HPUAttentionMetadataV1) -> object:
    # NOTE(kzawora): To anyone working on this in the future:
    # Trimming metadata is required when using HPUGraphs.
    # Attention metadata is going to be hashed by PT bridge, and
    # appropriate HPUGraphs will be matched based on all inputs' hash.

    # Before you put more keys in here, make sure you know their
    # value type and make sure you know how it's going to be hashed.
    # You can find that information in input_hash function
    # in habana_frameworks/torch/hpu/graphs.py. You can also hash
    # it manually with torch.hpu.graphs.input_hash(attention_metadata)

    # If you use primitive types here - they will get hashed based
    # on their value. You *will* get lots of excessive graph captures
    # (and an OOM eventually) if you decide to put something like
    # seq_len int here.
    # If you absolutely need a scalar, put it in a tensor. Tensors
    # get hashed using their metadata, not their values:
    # input_hash(torch.tensor(123)) == input_hash(torch.tensor(321))
    # input_hash(123) != input_hash(321)
    # input_hash("abc") != input_hash("cba")
    attention_metadata = subtuple(metadata, 'TrimmedAttentionMetadata', [
        'attn_bias',
        'seq_lens_tensor',
        'context_lens_tensor',
        'block_list',
        'block_mapping',
        'block_usage',
        'slot_mapping',
        'is_prompt',
        'block_size',
        'block_groups',
        'window_block_list',
        'window_block_mapping',
        'window_block_usage',
        'window_block_groups',
        'window_attn_bias',
        'chunked_block_mapping',
        'chunked_attn_bias',
        'chunked_block_list',
        'chunked_block_usage',
        'chunked_block_groups'
    ])  # yapf: disable
    return attention_metadata