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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
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
import contextlib
import gc
import os
import re

import random
from encodec import EncodecModel
import funcy
import numpy as np
from scipy.special import softmax
import torch

import math


import torch.distributions as torch_distributions

import torch.nn.functional as F
import tqdm
from transformers import BertTokenizer
from huggingface_hub import hf_hub_download

from .model import GPTConfig, GPT
from .model_fine import FineGPT, FineGPTConfig

import traceback
import sys
import time

import math

from rich.pretty import pprint

from .config import logger, load_all_defaults

from huggingface_hub import hf_hub_url
from collections import Counter

from devtools import debug
from collections import defaultdict


def _cast_bool_env_var(s):
    return s.lower() in ("true", "1", "t")


def get_SUNO_USE_DIRECTML():
    if _cast_bool_env_var(os.environ.get("SUNO_USE_DIRECTML", "False")):
        return True

    kwargs = {}
    defaults = load_all_defaults(*kwargs)
    if defaults["SUNO_USE_DIRECTML"] is True:
        return True
    else:
        return False


SUNO_USE_DIRECTML = get_SUNO_USE_DIRECTML()

dml = None
if SUNO_USE_DIRECTML is True:
    print(f"   --->> Experimental AMD DirectML support enabled.")
    import torch_directml

    torch.cuda.is_available = lambda: False

    dml = torch_directml.device()


if (
    torch.cuda.is_available()
    and hasattr(torch.cuda, "amp")
    and hasattr(torch.cuda.amp, "autocast")
    and hasattr(torch.cuda, "is_bf16_supported")
    and torch.cuda.is_bf16_supported()
):
    # print(f"   --->> Experimental NVIDIA BF16 support enabled.")
    autocast = funcy.partial(torch.cuda.amp.autocast, dtype=torch.bfloat16)
else:

    @contextlib.contextmanager
    def autocast():
        yield


# hold models in global scope to lazy load
global models
models = {}

global models_devices
models_devices = {}


CONTEXT_WINDOW_SIZE = 1024

SEMANTIC_RATE_HZ = 49.9
SEMANTIC_VOCAB_SIZE = 10_000

CODEBOOK_SIZE = 1024
N_COARSE_CODEBOOKS = 2
N_FINE_CODEBOOKS = 8
COARSE_RATE_HZ = 75

SAMPLE_RATE = 24_000


SUPPORTED_LANGS = [
    ("English", "en"),
    ("German", "de"),
    ("Spanish", "es"),
    ("French", "fr"),
    ("Hindi", "hi"),
    ("Italian", "it"),
    ("Japanese", "ja"),
    ("Korean", "ko"),
    ("Polish", "pl"),
    ("Portuguese", "pt"),
    ("Russian", "ru"),
    ("Turkish", "tr"),
    ("Chinese", "zh"),
]

ALLOWED_PROMPTS = {"announcer"}
for _, lang in SUPPORTED_LANGS:
    for prefix in ("", f"v2{os.path.sep}"):
        for n in range(10):
            ALLOWED_PROMPTS.add(f"{prefix}{lang}_speaker_{n}")


SUPPORTED_LANGS = [
    ("English", "en"),
    ("German", "de"),
    ("Spanish", "es"),
    ("French", "fr"),
    ("Hindi", "hi"),
    ("Italian", "it"),
    ("Japanese", "ja"),
    ("Korean", "ko"),
    ("Polish", "pl"),
    ("Portuguese", "pt"),
    ("Russian", "ru"),
    ("Turkish", "tr"),
    ("Chinese", "zh"),
]

ALLOWED_PROMPTS = {"announcer"}
for _, lang in SUPPORTED_LANGS:
    for prefix in ("", f"v2{os.path.sep}"):
        for n in range(10):
            ALLOWED_PROMPTS.add(f"{prefix}{lang}_speaker_{n}")


CUR_PATH = os.path.dirname(os.path.abspath(__file__))


default_cache_dir = os.path.join(os.path.expanduser("~"), ".cache")
CACHE_DIR = os.path.join(os.getenv("XDG_CACHE_HOME", default_cache_dir), "suno", "bark_v0")


USE_SMALL_MODELS = _cast_bool_env_var(os.environ.get("SUNO_USE_SMALL_MODELS", "False"))
GLOBAL_ENABLE_MPS = _cast_bool_env_var(os.environ.get("SUNO_ENABLE_MPS", "False"))
OFFLOAD_CPU = _cast_bool_env_var(os.environ.get("SUNO_OFFLOAD_CPU", "False"))

# Slower, possibly lower quality, but more memory efficient
SUNO_HALF_PRECISION = _cast_bool_env_var(os.environ.get("SUNO_HALF_PRECISION", "False"))

# Slower, possibly lower quality, but more memory efficient
SUNO_HALF_BFLOAT16 = _cast_bool_env_var(os.environ.get("SUNO_HALF_BFLOAT16", "False"))

SUNO_DISABLE_COMPILE = _cast_bool_env_var(os.environ.get("SUNO_DISABLE_COMPILE", "False"))

if sys.platform == "win32":
    SUNO_DISABLE_COMPILE = True


if SUNO_USE_DIRECTML is True:
    OFFLOAD_CPU = False

OFFLOAD_CPU = False

REMOTE_MODEL_PATHS = {
    "text_small": {
        "repo_id": "suno/bark",
        "file_name": "text.pt",
    },
    "coarse_small": {
        "repo_id": "suno/bark",
        "file_name": "coarse.pt",
    },
    "fine_small": {
        "repo_id": "suno/bark",
        "file_name": "fine.pt",
    },
    "text": {
        "repo_id": "suno/bark",
        "file_name": "text_2.pt",
    },
    "coarse": {
        "repo_id": "suno/bark",
        "file_name": "coarse_2.pt",
    },
    "fine": {
        "repo_id": "suno/bark",
        "file_name": "fine_2.pt",
    },
}

if not hasattr(torch.nn.functional, "scaled_dot_product_attention") and torch.cuda.is_available():
    logger.warning(
        "torch version does not support flash attention. You will get faster"
        + " inference speed by upgrade torch to newest nightly version."
    )


def _grab_best_device(use_gpu=True):
    if torch.cuda.device_count() > 0 and use_gpu:
        device = "cuda"
    elif torch.backends.mps.is_available() and use_gpu and GLOBAL_ENABLE_MPS:
        device = "mps"
    else:
        device = "cpu"

    return device


def _get_ckpt_path(model_type, use_small=False):
    key = model_type
    if use_small or USE_SMALL_MODELS:
        key += "_small"
    return os.path.join(CACHE_DIR, REMOTE_MODEL_PATHS[key]["file_name"])


def _download(from_hf_path, file_name):
    os.makedirs(CACHE_DIR, exist_ok=True)
    hf_hub_download(repo_id=from_hf_path, filename=file_name, local_dir=CACHE_DIR)


class InferenceContext:
    def __init__(self, benchmark=False):
        # we can't expect inputs to be the same length, so disable benchmarking by default
        self._chosen_cudnn_benchmark = benchmark
        self._cudnn_benchmark = None

    def __enter__(self):
        self._cudnn_benchmark = torch.backends.cudnn.benchmark
        torch.backends.cudnn.benchmark = self._chosen_cudnn_benchmark

    def __exit__(self, exc_type, exc_value, exc_traceback):
        torch.backends.cudnn.benchmark = self._cudnn_benchmark


if torch.cuda.is_available():
    torch.backends.cuda.matmul.allow_tf32 = True
    torch.backends.cudnn.allow_tf32 = True


@contextlib.contextmanager
def _inference_mode():
    if SUNO_USE_DIRECTML is True:
        with InferenceContext(), torch.inference_mode(mode=False), torch.no_grad(), autocast():
            yield
    else:
        with InferenceContext(), torch.inference_mode(), torch.no_grad(), autocast():
            yield


def _clear_cuda_cache():
    if torch.cuda.is_available():
        torch.cuda.empty_cache()
        torch.cuda.synchronize()


def clean_models(model_key=None):
    global models
    model_keys = [model_key] if model_key is not None else list(models.keys())
    for k in model_keys:
        if k in models:
            del models[k]

    _clear_cuda_cache()
    gc.collect()


def _load_codec_model(device):
    model = EncodecModel.encodec_model_24khz()
    model.set_target_bandwidth(6.0)
    model.eval()

    print_loading_info("codec", "EncodecModelPath", device)

    if SUNO_USE_DIRECTML is True:
        model.to(dml)
    else:
        model.to(device)

    if callable(getattr(torch, "compile")) and not SUNO_DISABLE_COMPILE:
        logger.info("torch.compile available, compiling codec model.")
        model = torch.compile(model)
    else:
        logger.info(
            "torch.compile *not* available, you will get better performance if you use pytorch >= 2.0."
        )

    _clear_cuda_cache()
    return model


def load_codec_model(use_gpu=True, force_reload=False):
    global models
    global models_devices
    device = _grab_best_device(use_gpu=use_gpu)
    if device == "mps":
        # encodec doesn't support mps
        device = "cpu"
    model_key = "codec"
    if OFFLOAD_CPU:
        models_devices[model_key] = device
        device = "cpu"
    if model_key not in models or force_reload:
        clean_models(model_key=model_key)

        model = _load_codec_model(device)
        models[model_key] = model

    if SUNO_USE_DIRECTML is True:
        models[model_key].to(dml)
    else:
        models[model_key].to(device)

    return models[model_key]


####
# Generation Functionality
####


def _tokenize(tokenizer, text):
    return tokenizer.encode(text, add_special_tokens=False)


def _detokenize(tokenizer, enc_text):
    return tokenizer.decode(enc_text)


def _normalize_whitespace(text):
    return re.sub(r"\s+", " ", text).strip()


TEXT_ENCODING_OFFSET = 10_048
SEMANTIC_PAD_TOKEN = 10_000
TEXT_PAD_TOKEN = 129_595
SEMANTIC_INFER_TOKEN = 129_599


def _load_history_prompt(history_prompt_input):
    if isinstance(history_prompt_input, str) and history_prompt_input.endswith(".npz"):
        history_prompt = np.load(history_prompt_input)
    elif isinstance(history_prompt_input, str):
        # make sure this works on non-ubuntu
        history_prompt_input = os.path.join(*history_prompt_input.split("/"))
        if history_prompt_input not in ALLOWED_PROMPTS:
            raise ValueError("history prompt not found")
        history_prompt = np.load(
            os.path.join(CUR_PATH, "assets", "prompts", f"{history_prompt_input}.npz")
        )
    elif isinstance(history_prompt_input, dict):
        assert "semantic_prompt" in history_prompt_input
        assert "coarse_prompt" in history_prompt_input
        assert "fine_prompt" in history_prompt_input
        history_prompt = history_prompt_input
    else:
        raise ValueError("history prompt format unrecognized")
    return history_prompt


def compute_log_probs(token_list, smoothing_factor, scaling_factor):
    # Count the frequency of each token.
    token_freq = Counter(token_list)

    # Add a smoothing factor.
    smoothed_token_freq = {token: freq + smoothing_factor for token, freq in token_freq.items()}

    # Normalize to create a probability distribution.
    total_tokens = len(token_list) + smoothing_factor * len(smoothed_token_freq)
    token_probs = {token: freq / total_tokens for token, freq in smoothed_token_freq.items()}

    # Transform into scaled log-probabilities.
    log_probs = {token: scaling_factor * np.log(prob) for token, prob in token_probs.items()}

    return log_probs


def estimate_s_this_seems_wrong_so_many_math_crashes(prob):
    epsilon = 1e-10
    num = 0
    den = 0
    for i in range(
        min(len(prob), 10000)
    ):  # apparently any number is fine here but they paper was on natural language so maybe not for us?
        # for i in range(768):
        b = prob[i] / (prob[i + 1] + epsilon)
        t = (i + 2) / (i + 1)
        if b > 0 and t > 0:
            num += math.log(b) * math.log(t)
            den += math.log(t) ** 2
    return num / den if den != 0 else 0


def estimate_s(prob):
    epsilon = 1e-10
    num = 0
    den = 0
    # for i in range(3000):
    # in the paper they say 100 is as good as any higher number? But it's not slow so maybe leave it higher?
    # also in the paper they don't have catch divide by 0s though...
    # also the paper was on natural language so maybe not for us. Let's just max it out
    for i in range(min(len(prob), 10000)):
        b = prob[i] / (prob[i + 1] + epsilon)
        t = (i + 2) / (i + 1)
        if b > 0 and t > 0:
            num += math.log(b if b > 0 else 1) * math.log(t if t > 0 else 1)
            # den += math.log(t)**2
            den += math.log(t if t > 0 else 1) ** 2
            # ok NOW this should never be zero and feels more right
            return num / den
    # return num / den if den != 0 else 0 # or should this be float("inf") ? doesn't seem right.


def compute_k_original_paper(n, s, tau):
    print(f"n: {n}, s: {s}, tau: {tau}")
    eps = s - 1
    k = ((eps * (2 ** (tau))) / (1 - n ** (-eps))) ** (1 / s)
    k = round(k)
    return k


def compute_k(n, s, tau, max_k):
    try:
        eps = s - 1
        n_eps = n ** (-eps)
        if s <= 0:
            return 0
        tau_s = tau ** (1 / s)
        k = (eps * 2 * tau_s / (1 - n_eps)) ** (1 / s)
        if isinstance(k, complex):
            return 0
        k = round(k)
        if k > max_k:
            return max_k
        return k
    except OverflowError:
        # Return maximum possible k
        return max_k


def compute_k_orig(n, s, tau):
    print(f"n: {n}, s: {s}, tau: {tau}")
    eps = s - 1
    k = ((eps * (2 ** (tau))) / (1 - n ** (-eps))) ** (1 / s)
    k = round(k)
    return k


def compute_k_not_right(n, s, tau, max_k):
    print(f"n: {n}, s: {s}, tau: {tau}")
    try:
        eps = s - 1
        n_eps = n ** (-eps)
        if s <= 0:
            return max_k
        tau_s = tau ** (1 / s)
        k = (eps * 2 * tau_s / (1 - n_eps)) ** (1 / s)
        k = round(k)
        return k
    except OverflowError:
        # Return maximum possible k
        return max_k


def compute_k_log(n, s, tau):
    print(f"n: {n}, s: {s}, tau: {tau}")
    eps = s - 1
    try:
        log_k = (math.log(eps) + tau * math.log(2) - math.log(1 - n ** (-eps))) / s
        k = round(math.exp(log_k))
    except OverflowError:
        k = float("inf")
    return k


# https://github.com/basusourya/mirostat/blob/master/mirostat.py


# try adjusting target tau dynamically based on just length even? Could you shape the "energy" of the clip?
def mirostat_sampling_v1(
    logits=None,
    tau=5.0,
    learning_rate=1.0,
    max_surprise=None,
    vocab_size=SEMANTIC_VOCAB_SIZE,
    indices_surprise_history=[],
    running_tot_surprise=0,
    generated=[],
):
    sorted_logits, sorted_indices = torch.sort(logits, descending=True)
    prob_original = torch.softmax(sorted_logits, dim=-1).tolist()

    s = estimate_s(prob_original)

    max_k = len(sorted_logits) - 1

    k = compute_k(vocab_size, s, max_surprise, max_k) + 1

    print(f"\n\nK: {k} s: {s} tau: {max_surprise}")

    sorted_logits = sorted_logits[0:k]
    sorted_indices = sorted_indices[0:k]

    prob_topk = torch.softmax(sorted_logits, dim=0)

    prev_i = torch.multinomial(prob_topk, num_samples=1, replacement=True)
    index_surprise = math.log2(1 / prob_original[prev_i])
    print(f"index_surprise: {index_surprise}")
    indices_surprise_history.append(index_surprise)

    running_tot_surprise += index_surprise
    prev = sorted_indices[prev_i]
    generated += prev.tolist()

    error_surprise = index_surprise - tau
    max_surprise -= learning_rate * error_surprise

    # full_probs = torch.zeros_like(logits) # 0? or -inf?
    full_probs = torch.empty_like(logits).fill_(-float("inf"))
    full_probs[sorted_indices] = prob_topk.to(full_probs.dtype)

    return (
        sorted_indices[prev_i],
        max_surprise,
        full_probs,
        indices_surprise_history,
        running_tot_surprise,
        generated,
    )


def mirostat_sampling_meh(
    logits=None,
    tau=5.0,
    learning_rate=1.0,
    max_surprise=None,
    vocab_size=SEMANTIC_VOCAB_SIZE,
    indices_surprise_history=[],
    running_tot_surprise=0,
    generated=[],
):
    sorted_logits, sorted_indices = torch.sort(logits, descending=True)
    prob_original = torch.softmax(sorted_logits, dim=-1).tolist()

    s = estimate_s(prob_original)

    max_k = len(sorted_logits) - 1

    k = compute_k(vocab_size, s, max_surprise, max_k) + 1

    print(f"\n\nK: {k} s: {s} tau: {max_surprise}")

    sorted_logits = sorted_logits[0:k]
    sorted_indices = sorted_indices[0:k]

    prob_topk = torch.softmax(sorted_logits, dim=0)

    prev_i = torch.multinomial(prob_topk, num_samples=1, replacement=True)

    index_surprise = math.log2(1 / prob_original[sorted_indices[prev_i].item()])
    print(f"index_surprise: {index_surprise}")
    indices_surprise_history.append(index_surprise)

    running_tot_surprise += index_surprise
    prev = sorted_indices[prev_i]
    generated += prev.tolist()
    error_surprise = index_surprise - tau
    max_surprise -= learning_rate * error_surprise

    full_probs = torch.empty_like(logits).fill_(-float("inf"))
    full_probs[sorted_indices] = prob_topk.to(full_probs.dtype)

    item_next = sorted_indices[prev_i]

    return (
        item_next,
        max_surprise,
        full_probs,
        indices_surprise_history,
        running_tot_surprise,
        generated,
    )


def mirostat_sampling_least(
    logits=None,
    tau=5.0,
    learning_rate=1.0,
    max_surprise=None,
    vocab_size=SEMANTIC_VOCAB_SIZE,
    indices_surprise_history=[],
    running_tot_surprise=0,
    generated=[],
):
    sorted_logits, sorted_indices = torch.sort(logits, descending=True)
    prob_original = torch.softmax(sorted_logits, dim=-1).tolist()

    s = estimate_s(prob_original)

    max_k = len(sorted_logits) - 1

    k = compute_k(vocab_size, s, max_surprise, max_k) + 1

    print(f"\n\nK: {k} s: {s} tau: {max_surprise}")

    sorted_logits = sorted_logits[0:k]
    sorted_indices = sorted_indices[0:k]

    prob_topk = torch.softmax(sorted_logits, dim=0)

    prev_i = torch.argmin(prob_topk).unsqueeze(0)

    index_surprise = math.log2(1 / prob_original[sorted_indices[prev_i].item()])
    print(f"index_surprise: {index_surprise}")
    indices_surprise_history.append(index_surprise)

    running_tot_surprise += index_surprise
    prev = sorted_indices[prev_i]
    generated += prev.tolist()

    error_surprise = index_surprise - tau
    max_surprise -= learning_rate * error_surprise

    full_probs = torch.empty_like(logits).fill_(-float("inf"))
    full_probs[sorted_indices] = prob_topk.to(full_probs.dtype)

    # Return least likely token and reverse generated logits
    # return sorted_indices[prev_i], max_surprise, torch.flip(full_probs, dims=[0]), indices_surprise_history, running_tot_surprise, generated
    return (
        sorted_indices[prev_i],
        max_surprise,
        full_probs,
        indices_surprise_history,
        running_tot_surprise,
        generated,
    )


def sine_wave_temperature(current_token, max_token):
    return 3.0 + 2.1 * (math.sin(2 * math.pi * (current_token / 150)) / 2.1 + 0.2)


def sine_wave_temperature(current_token, max_token, period=100, phase_shift=0):
    return 0.5 + 2.0 * (math.sin(2 * math.pi * (current_token / period) + phase_shift) / 2 + 0.5)


def sine_wave_temperature(current_token, token_period, start_phase, temp_min, temp_max):
    phase = 2 * math.pi * ((current_token + start_phase) / token_period)
    temp_range = temp_max - temp_min
    return temp_min + temp_range * ((math.sin(phase) / 2) + 0.5)


def mirostat_sampling(
    logits=None,
    tau=5.0,
    learning_rate=1.0,
    max_surprise=None,
    vocab_size=SEMANTIC_VOCAB_SIZE,
    indices_surprise_history=[],
    running_tot_surprise=0,
    generated=[],
    temperature_fn=None,
):
    sorted_logits, sorted_indices = torch.sort(logits, descending=True)
    prob_original = torch.softmax(sorted_logits, dim=-1).tolist()

    s = estimate_s(prob_original)

    max_k = len(sorted_logits) - 1

    k = compute_k(vocab_size, s, max_surprise, max_k) + 1

    sorted_logits = sorted_logits[0:k]
    sorted_indices = sorted_indices[0:k]

    # Current location in the segment
    current_token = len(generated)
    max_token = 768  # Maximum sample length

    if temperature_fn is not None:
        temp = temperature_fn(current_token, max_token)
        sorted_logits = torch.clamp(sorted_logits, -10000, 10000)
        # Apply to logits before softmax
        prob_topk = torch.softmax(sorted_logits / temp, dim=0)
        prob_topk = torch.clamp(prob_topk, 1e-9, 1 - 1e-9)  # Ensures probabilities are valid
    else:
        prob_topk = torch.softmax(sorted_logits, dim=0)

    prev_i = torch.multinomial(prob_topk, num_samples=1, replacement=True)

    epsilon = 1e-10
    index_surprise = math.log2(1 / (prob_original[sorted_indices[prev_i].item()] + epsilon))

    indices_surprise_history.append(index_surprise)

    running_tot_surprise += index_surprise
    prev = sorted_indices[prev_i]
    generated += prev.tolist()

    error_surprise = index_surprise - tau
    max_surprise -= learning_rate * error_surprise

    full_probs = torch.empty_like(logits).fill_(-float("inf"))
    full_probs[sorted_indices] = prob_topk.to(full_probs.dtype)

    if current_token % 25 == 0 and False:
        print(f"Temperature: {temp}")
        print(f"index_surprise: {index_surprise}")
        print(f"\n\nK: {k} s: {s} tau: {max_surprise}")

    return (
        sorted_indices[prev_i],
        max_surprise,
        full_probs,
        indices_surprise_history,
        running_tot_surprise,
        generated,
    )


def compute_negative_influence(negative_logits, n, window_size, negative_scale):
    # Check if negative_logits is empty
    if len(negative_logits) == 0:
        return 0

    # Ensure n is within range
    n = min(max(n, 0), len(negative_logits) - 1)

    # Adjust window_size if it's larger than negative_logits length
    window_size = min(window_size, len(negative_logits))

    # Get the start and end of the window
    start = max(0, n - window_size)
    end = min(len(negative_logits), n + window_size + 1)

    # Generate a Gaussian distribution for the weights and normalize them
    weights = np.exp(-((np.arange(start, end) - n) ** 2) / (2.0 * window_size**2))
    weights /= weights.sum()

    # Compute a weighted average of negative_logits within the window
    negative_influence = np.average(negative_logits[start:end], weights=weights, axis=0)

    # Adjust the influence by the negative_scale
    negative_influence *= min(max(negative_scale, 0), 1)  # Ensure negative_scale is between 0 and 1

    return negative_influence


def generate_text_semantic(
    text,
    history_prompt=None,
    temp=0.7,
    top_k=None,
    top_p=None,
    silent=False,
    min_eos_p=0.2,
    max_gen_duration_s=None,
    allow_early_stop=True,
    use_kv_caching=True,
    use_mirostat_sampling=False,
    # tau = 31100.0,
    tau=5.0,
    miro_learning_rate=1.0,
    token_repeat_penalty=0.0,
    inverted_p=None,
    bottom_k=None,
    return_logits=False,
    negative_tokens=None,
    negative_logits=None,
    negative_text_prompt_logits_scale=None,
    negative_text_prompt_logits_scale_window_size=64,
    negative_text_prompt_divergence_scale=None,
):
    """Generate semantic tokens from text."""

    if return_logits:
        all_logits = []

    if temp == 0:
        temp = 0.001
    # debug(locals())
    logger.debug(locals())
    assert isinstance(text, str)
    text = _normalize_whitespace(text)
    # assert len(text.strip()) > 0

    if history_prompt is not None:
        history_prompt = _load_history_prompt(history_prompt)
        semantic_history = history_prompt["semantic_prompt"]
        assert (
            isinstance(semantic_history, np.ndarray)
            and len(semantic_history.shape) == 1
            and len(semantic_history) > 0
            and semantic_history.min() >= 0
            and semantic_history.max() <= SEMANTIC_VOCAB_SIZE - 1
        )
    else:
        semantic_history = None
    # load models if not yet exist
    global models
    global models_devices
    if "text" not in models:
        if SUNO_USE_DIRECTML is True:
            preload_models(load_one_model_type="text")
        else:
            preload_models()
    model_container = models["text"]
    model = model_container["model"]
    tokenizer = model_container["tokenizer"]
    encoded_text = np.array(_tokenize(tokenizer, text)) + TEXT_ENCODING_OFFSET
    if OFFLOAD_CPU:
        if GLOBAL_ENABLE_MPS:
            device = _grab_best_device(use_gpu=False)
            models_devices["text"] = device
        model.to(models_devices["text"])
    device = next(model.parameters()).device
    if len(encoded_text) > 256:
        p = round((len(encoded_text) - 256) / len(encoded_text) * 100, 1)
        logger.warning(f"warning, text too long, lopping of last {p}%")
        encoded_text = encoded_text[:256]
    encoded_text = np.pad(
        encoded_text,
        (0, 256 - len(encoded_text)),
        constant_values=TEXT_PAD_TOKEN,
        mode="constant",
    )
    if semantic_history is not None:
        semantic_history = semantic_history.astype(np.int64)
        # print(f"Actual length of semantic input: {len(semantic_history)}")
        # lop off if history is too long, pad if needed
        semantic_history = semantic_history[-256:]
        semantic_history = np.pad(
            semantic_history,
            (0, 256 - len(semantic_history)),
            constant_values=SEMANTIC_PAD_TOKEN,
            mode="constant",
        )
    else:
        semantic_history = np.array([SEMANTIC_PAD_TOKEN] * 256)
    x = torch.from_numpy(
        np.hstack([encoded_text, semantic_history, np.array([SEMANTIC_INFER_TOKEN])]).astype(
            np.int64
        )
    )[None]
    assert x.shape[1] == 256 + 256 + 1
    with _inference_mode():
        if SUNO_USE_DIRECTML is True:
            device = dml
        x = x.to(device)
        n_tot_steps = 768

        # preallocate tensor
        x_initial = x.shape[1]
        x = torch.hstack([x, torch.empty([1, n_tot_steps], dtype=torch.int32, device=device)])

        # custom tqdm updates since we don't know when eos will occur
        pbar = tqdm.tqdm(disable=silent, total=n_tot_steps)
        pbar_state = 0
        tot_generated_duration_s = 0
        kv_cache = None

        # mirostat
        prev = None
        max_surprise = 2 * tau
        indices_surprise_history = []
        running_tot_surprise = 0
        miro_generated = []  # debug

        token_counts = defaultdict(int)
        for n in range(n_tot_steps):
            # if use_kv_caching and kv_cache is not None:
            #    x_input = x[:, [-1]]
            # else:
            #    x_input = x

            x_input = (
                x[:, [x_initial + n - 1]]
                if use_kv_caching and kv_cache is not None
                else x[:, : x_initial + n]
            )
            logits, kv_cache = model(
                x_input, merge_context=True, use_cache=use_kv_caching, past_kv=kv_cache
            )
            relevant_logits = logits[0, 0, :SEMANTIC_VOCAB_SIZE]
            if allow_early_stop:
                relevant_logits = torch.hstack(
                    (relevant_logits, logits[0, 0, [SEMANTIC_PAD_TOKEN]])  # eos
                )

            # Detach and convert to numpy for faster calculations
            original_device = relevant_logits.device
            relevant_logits = relevant_logits.detach().cpu().type(torch.float32).numpy()

            # Jon doing some silly ideas here, but inverted_p seems genuinely useful
            if top_p is not None or inverted_p is not None:
                if inverted_p is not None:
                    sorted_indices = np.argsort(relevant_logits)
                    cumulative_limit = inverted_p
                elif top_p is not None:
                    sorted_indices = np.argsort(relevant_logits)[::-1]
                    cumulative_limit = top_p
                sorted_logits = relevant_logits[sorted_indices]
                cumulative_probs = np.cumsum(softmax(sorted_logits))
                sorted_indices_to_remove = cumulative_probs > cumulative_limit
                sorted_indices_to_remove[1:] = sorted_indices_to_remove[:-1].copy()
                sorted_indices_to_remove[0] = False
                relevant_logits[sorted_indices[sorted_indices_to_remove]] = -np.inf

            relevant_logits = torch.from_numpy(relevant_logits)
            relevant_logits = relevant_logits.to(original_device)

            if top_k is not None or bottom_k is not None:
                if bottom_k is not None:
                    v, _ = torch.topk(
                        relevant_logits, max(bottom_k, relevant_logits.size(-1)), largest=False
                    )
                    relevant_logits[relevant_logits > v[-1]] = -float("Inf")
                elif top_k is not None:
                    v, _ = torch.topk(relevant_logits, min(top_k, relevant_logits.size(-1)))
                    relevant_logits[relevant_logits < v[-1]] = -float("Inf")

            if use_mirostat_sampling:
                logits_for_miro = relevant_logits / temp
                (
                    item_next,
                    max_surprise,
                    probs,
                    indices_surprise_history,
                    running_tot_surprise,
                    miro_generated,
                ) = mirostat_sampling(
                    logits=logits_for_miro,
                    max_surprise=max_surprise,
                    tau=tau,
                    learning_rate=miro_learning_rate,
                    vocab_size=SEMANTIC_VOCAB_SIZE,
                    indices_surprise_history=indices_surprise_history,
                    running_tot_surprise=running_tot_surprise,
                    generated=miro_generated,
                    temperature_fn=None,
                )
                # item_next = item_next.to(torch.int32)

            else:
                if token_repeat_penalty != 0.0 and token_repeat_penalty != 1.0:
                    for token, count in token_counts.items():
                        relevant_logits[token] += math.log(token_repeat_penalty) * count

                if return_logits:
                    all_logits.append(relevant_logits)

                probs = F.softmax(relevant_logits / temp, dim=-1)
                item_next = torch.multinomial(probs, num_samples=1).to(torch.int32)

            if allow_early_stop and (
                item_next == SEMANTIC_VOCAB_SIZE
                or (min_eos_p is not None and probs[-1] >= min_eos_p)
            ):
                n -= 1  # backtrack 1
                # eos found, so break
                pbar.total = n
                pbar.update(n - pbar_state)

                break
            # x = torch.cat((x, item_next[None]), dim=1)
            if token_repeat_penalty != 0.0 and token_repeat_penalty != 1.0:
                token_counts[int(item_next)] += 1

            x[0][x_initial + n] = item_next
            tot_generated_duration_s += 1 / SEMANTIC_RATE_HZ
            if max_gen_duration_s is not None and tot_generated_duration_s > max_gen_duration_s:
                pbar.total = n
                pbar.update(n - pbar_state)
                break
            if n == n_tot_steps - 1:
                pbar.total = n
                pbar.update(n - pbar_state)
                break
            del logits, relevant_logits, probs, item_next
            if n > pbar_state:
                if n > pbar.total:
                    pbar.total = n
                pbar.update(n - pbar_state)
            pbar_state = n
        pbar.total = n
        pbar.refresh()

        pbar.close()
        # out = x.detach().cpu().numpy().squeeze()[256 + 256 + 1 :]
        out = x.detach().cpu().numpy().squeeze()[x_initial : x_initial + n + 1]
        if use_mirostat_sampling and False:
            print(f"Target tau: {tau}")
            print("Total surprise value:", sum(indices_surprise_history))
            print("Average surprise value:", sum(indices_surprise_history) / len(out))
            print(f"Generated Miro: {miro_generated}")
            print(f"out: {out}")
    if OFFLOAD_CPU:
        model.to("cpu")
    assert all(0 <= out) and all(out < SEMANTIC_VOCAB_SIZE)
    _clear_cuda_cache()

    if SUNO_USE_DIRECTML is True:
        clean_models()

    if return_logits:
        return out, all_logits
    else:
        return out


def generate_text_semantic_branching_not_batching(
    text,
    history_prompt=None,
    temp=0.7,
    top_k=None,
    top_p=None,
    silent=False,
    min_eos_p=0.2,
    max_gen_duration_s=None,
    allow_early_stop=True,
    use_kv_caching=True,
    num_sample_per_step=2,
):
    """Generate semantic tokens from text."""
    assert isinstance(text, str)
    text = _normalize_whitespace(text)
    assert len(text.strip()) > 0
    if history_prompt is not None:
        history_prompt = _load_history_prompt(history_prompt)
        semantic_history = history_prompt["semantic_prompt"]
        assert (
            isinstance(semantic_history, np.ndarray)
            and len(semantic_history.shape) == 1
            and len(semantic_history) > 0
            and semantic_history.min() >= 0
            and semantic_history.max() <= SEMANTIC_VOCAB_SIZE - 1
        )
    else:
        semantic_history = None
    # load models if not yet exist
    global models
    global models_devices
    if "text" not in models:
        if SUNO_USE_DIRECTML is True:
            preload_models(load_one_model_type="text")
        else:
            preload_models()
    model_container = models["text"]
    model = model_container["model"]
    tokenizer = model_container["tokenizer"]
    encoded_text = np.array(_tokenize(tokenizer, text)) + TEXT_ENCODING_OFFSET
    if OFFLOAD_CPU:
        model.to(models_devices["text"])
    device = next(model.parameters()).device
    if len(encoded_text) > 256:
        p = round((len(encoded_text) - 256) / len(encoded_text) * 100, 1)
        logger.warning(f"warning, text too long, lopping of last {p}%")
        encoded_text = encoded_text[:256]
    encoded_text = np.pad(
        encoded_text,
        (0, 256 - len(encoded_text)),
        constant_values=TEXT_PAD_TOKEN,
        mode="constant",
    )
    if semantic_history is not None:
        semantic_history = semantic_history.astype(np.int64)
        # lop off if history is too long, pad if needed
        semantic_history = semantic_history[-256:]
        semantic_history = np.pad(
            semantic_history,
            (0, 256 - len(semantic_history)),
            constant_values=SEMANTIC_PAD_TOKEN,
            mode="constant",
        )
    else:
        semantic_history = np.array([SEMANTIC_PAD_TOKEN] * 256)
    # x = torch.from_numpy(
    #     np.hstack([
    #         encoded_text, semantic_history, np.array([SEMANTIC_INFER_TOKEN])
    #     ]).astype(np.int64)
    # )[None]

    x = torch.from_numpy(
        np.hstack([encoded_text, semantic_history, np.array([SEMANTIC_INFER_TOKEN])]).astype(
            np.int64
        )
    ).repeat(num_sample_per_step, 1)

    assert x.shape[1] == 256 + 256 + 1
    with _inference_mode():
        x = x.to(device)
        n_tot_steps = 768
        # custom tqdm updates since we don't know when eos will occur
        pbar = tqdm.tqdm(disable=silent, total=n_tot_steps)
        pbar_state = 0
        tot_generated_duration_s = 0
        kv_cache = None
        for n in range(n_tot_steps):
            if use_kv_caching and kv_cache is not None:
                x_input = x[:, [-1]]
            else:
                x_input = x
            logits, kv_cache = model(
                x_input, merge_context=True, use_cache=use_kv_caching, past_kv=kv_cache
            )
            relevant_logits = logits[0, 0, :SEMANTIC_VOCAB_SIZE]
            if allow_early_stop:
                relevant_logits = torch.hstack(
                    (relevant_logits, logits[0, 0, [SEMANTIC_PAD_TOKEN]])  # eos
                )
            if top_p is not None:
                # faster to convert to numpy
                original_device = relevant_logits.device
                relevant_logits = relevant_logits.detach().cpu().type(torch.float32).numpy()
                sorted_indices = np.argsort(relevant_logits)[::-1]
                sorted_logits = relevant_logits[sorted_indices]
                cumulative_probs = np.cumsum(softmax(sorted_logits))
                sorted_indices_to_remove = cumulative_probs > top_p
                sorted_indices_to_remove[1:] = sorted_indices_to_remove[:-1].copy()
                sorted_indices_to_remove[0] = False
                relevant_logits[sorted_indices[sorted_indices_to_remove]] = -np.inf
                relevant_logits = torch.from_numpy(relevant_logits)
                relevant_logits = relevant_logits.to(original_device)
            if top_k is not None:
                v, _ = torch.topk(relevant_logits, min(top_k, relevant_logits.size(-1)))
                relevant_logits[relevant_logits < v[-1]] = -float("Inf")
            # probs = F.softmax(relevant_logits / temp, dim=-1)
            # item_next = torch.multinomial(probs, num_samples=1).to(torch.int32)

            probs = F.softmax(relevant_logits / temp, dim=-1)
            item_next = torch.multinomial(probs, num_samples=num_sample_per_step).to(torch.int32)
            if allow_early_stop and (
                item_next == SEMANTIC_VOCAB_SIZE
                or (min_eos_p is not None and probs[-1] >= min_eos_p)
            ):
                # eos found, so break
                pbar.update(n - pbar_state)
                break
            # x = torch.cat((x, item_next[None]), dim=1)
            for i in range(num_sample_per_step):
                x[i] = torch.cat((x[i], item_next[i][None]), dim=0)
            tot_generated_duration_s += 1 / SEMANTIC_RATE_HZ
            if max_gen_duration_s is not None and tot_generated_duration_s > max_gen_duration_s:
                pbar.update(n - pbar_state)
                break
            if n == n_tot_steps - 1:
                pbar.update(n - pbar_state)
                break
            del logits, relevant_logits, probs, item_next
            if n > pbar_state:
                if n > pbar.total:
                    pbar.total = n
                pbar.update(n - pbar_state)
            pbar_state = n
        pbar.total = n
        pbar.refresh()
        pbar.close()
        out = x.detach().cpu().numpy().squeeze()[256 + 256 + 1 :]
    if OFFLOAD_CPU:
        model.to("cpu")
    assert all(0 <= out) and all(out < SEMANTIC_VOCAB_SIZE)
    _clear_cuda_cache()
    return out


def generate_coarse(
    x_semantic,
    history_prompt=None,
    temp=0.7,
    top_k=None,
    top_p=None,
    silent=False,
    max_coarse_history=630,  # min 60 (faster), max 630 (more context)
    sliding_window_len=60,
    use_kv_caching=True,
    x_coarse_history_alignment_hack=-2,
):
    """Generate coarse audio codes from semantic tokens."""

    logger.debug(locals())
    assert (
        isinstance(x_semantic, np.ndarray)
        and len(x_semantic.shape) == 1
        and len(x_semantic) > 0
        and x_semantic.min() >= 0
        and x_semantic.max() <= SEMANTIC_VOCAB_SIZE - 1
    )
    assert 60 <= max_coarse_history <= 630
    assert max_coarse_history + sliding_window_len <= 1024 - 256
    semantic_to_coarse_ratio = COARSE_RATE_HZ / SEMANTIC_RATE_HZ * N_COARSE_CODEBOOKS

    max_semantic_history = int(np.floor(max_coarse_history / semantic_to_coarse_ratio))
    if history_prompt is not None:
        history_prompt = _load_history_prompt(history_prompt)
        x_semantic_history = history_prompt["semantic_prompt"]
        x_coarse_history = history_prompt["coarse_prompt"]

        # print(f"Pre Trim sem coars: {x_semantic_history.shape} {x_coarse_history.shape}")
        assert (
            isinstance(x_semantic_history, np.ndarray)
            and len(x_semantic_history.shape) == 1
            and len(x_semantic_history) > 0
            and x_semantic_history.min() >= 0
            and x_semantic_history.max() <= SEMANTIC_VOCAB_SIZE - 1
            and isinstance(x_coarse_history, np.ndarray)
            and len(x_coarse_history.shape) == 2
            and x_coarse_history.shape[0] == N_COARSE_CODEBOOKS
            and x_coarse_history.shape[-1] >= 0
            and x_coarse_history.min() >= 0
            and x_coarse_history.max() <= CODEBOOK_SIZE - 1
            and (
                round(x_coarse_history.shape[-1] / len(x_semantic_history), 1)
                == round(semantic_to_coarse_ratio / N_COARSE_CODEBOOKS, 1)
            )
        )

        x_coarse_history = _flatten_codebooks(x_coarse_history) + SEMANTIC_VOCAB_SIZE
        # trim histories correctly
        n_semantic_hist_provided = np.min(
            [
                max_semantic_history,
                len(x_semantic_history) - len(x_semantic_history) % 2,
                int(np.floor(len(x_coarse_history) / semantic_to_coarse_ratio)),
            ]
        )
        n_coarse_hist_provided = int(round(n_semantic_hist_provided * semantic_to_coarse_ratio))
        x_semantic_history = x_semantic_history[-n_semantic_hist_provided:].astype(np.int32)
        x_coarse_history = x_coarse_history[-n_coarse_hist_provided:].astype(np.int32)
        # TODO: bit of a hack for time alignment (sounds better)
        # x_coarse_history = x_coarse_history[:-2]
        x_coarse_history = x_coarse_history[:x_coarse_history_alignment_hack]

    else:
        x_semantic_history = np.array([], dtype=np.int32)
        x_coarse_history = np.array([], dtype=np.int32)

    # print(f"actual lengths we're using, x_semantic_history: {len(x_semantic_history)} x_coarse_history: {len(x_coarse_history)}")

    # load models if not yet exist
    global models
    global models_devices
    if "coarse" not in models:
        if SUNO_USE_DIRECTML is True:
            preload_models(load_one_model_type="coarse")
        else:
            preload_models()
    model = models["coarse"]
    if OFFLOAD_CPU:
        if GLOBAL_ENABLE_MPS:
            device = _grab_best_device(use_gpu=False)
            models_devices["coarse"] = device
        model.to(models_devices["coarse"])

    device = next(model.parameters()).device
    # start loop
    n_steps = int(
        round(
            np.floor(len(x_semantic) * semantic_to_coarse_ratio / N_COARSE_CODEBOOKS)
            * N_COARSE_CODEBOOKS
        )
    )
    assert n_steps > 0 and n_steps % N_COARSE_CODEBOOKS == 0

    # reminder to try filling up some of the COARSE_INFER_TOKEN with history to get better short clips
    x_semantic = np.hstack([x_semantic_history, x_semantic]).astype(np.int32)
    x_coarse = x_coarse_history.astype(np.int32)
    base_semantic_idx = len(x_semantic_history)
    with _inference_mode():
        if SUNO_USE_DIRECTML is True:
            device = dml
        x_semantic_in = torch.from_numpy(x_semantic)[None].to(device)
        x_coarse_in = torch.from_numpy(x_coarse)[None].to(device)
        n_window_steps = int(np.ceil(n_steps / sliding_window_len))
        n_step = 0
        for _ in tqdm.tqdm(range(n_window_steps), total=n_window_steps, disable=silent):
            semantic_idx = base_semantic_idx + int(round(n_step / semantic_to_coarse_ratio))
            # pad from right side
            x_in = x_semantic_in[:, np.max([0, semantic_idx - max_semantic_history]) :]
            x_in = x_in[:, :256]
            x_in = F.pad(
                x_in,
                (0, 256 - x_in.shape[-1]),
                "constant",
                COARSE_SEMANTIC_PAD_TOKEN,
            )

            x_in = torch.hstack(
                [
                    x_in,
                    torch.tensor([COARSE_INFER_TOKEN])[None].to(device),
                    x_coarse_in[:, -max_coarse_history:],
                ]
            )
            kv_cache = None
            for _ in range(sliding_window_len):
                if n_step >= n_steps:
                    continue
                is_major_step = n_step % N_COARSE_CODEBOOKS == 0

                if use_kv_caching and kv_cache is not None:
                    x_input = x_in[:, [-1]]
                else:
                    x_input = x_in

                logits, kv_cache = model(x_input, use_cache=use_kv_caching, past_kv=kv_cache)
                logit_start_idx = SEMANTIC_VOCAB_SIZE + (1 - int(is_major_step)) * CODEBOOK_SIZE
                logit_end_idx = SEMANTIC_VOCAB_SIZE + (2 - int(is_major_step)) * CODEBOOK_SIZE
                relevant_logits = logits[0, 0, logit_start_idx:logit_end_idx]
                if top_p is not None:
                    # faster to convert to numpy
                    logits_device = relevant_logits.device
                    logits_dtype = relevant_logits.type()
                    relevant_logits = relevant_logits.detach().cpu().type(torch.float32).numpy()
                    sorted_indices = np.argsort(relevant_logits)[::-1]
                    sorted_logits = relevant_logits[sorted_indices]
                    cumulative_probs = np.cumsum(softmax(sorted_logits))
                    sorted_indices_to_remove = cumulative_probs > top_p
                    sorted_indices_to_remove[1:] = sorted_indices_to_remove[:-1].copy()
                    sorted_indices_to_remove[0] = False
                    relevant_logits[sorted_indices[sorted_indices_to_remove]] = -np.inf
                    relevant_logits = torch.from_numpy(relevant_logits)
                    relevant_logits = relevant_logits.to(logits_device).type(logits_dtype)
                if top_k is not None:
                    v, _ = torch.topk(relevant_logits, min(top_k, relevant_logits.size(-1)))
                    relevant_logits[relevant_logits < v[-1]] = -float("Inf")
                probs = F.softmax(relevant_logits / temp, dim=-1)
                # multinomial bugged on mps: shuttle to cpu if necessary
                inf_device = probs.device
                if probs.device.type == "mps":
                    probs = probs.to("cpu")
                item_next = torch.multinomial(probs, num_samples=1)
                probs = probs.to(inf_device)
                item_next = item_next.to(inf_device)
                item_next += logit_start_idx
                x_coarse_in = torch.cat((x_coarse_in, item_next[None]), dim=1)
                x_in = torch.cat((x_in, item_next[None]), dim=1)
                del logits, relevant_logits, probs, item_next
                n_step += 1
            del x_in
        del x_semantic_in
    if OFFLOAD_CPU:
        model.to("cpu")
    gen_coarse_arr = x_coarse_in.detach().cpu().numpy().squeeze()[len(x_coarse_history) :]
    del x_coarse_in
    assert len(gen_coarse_arr) == n_steps
    gen_coarse_audio_arr = gen_coarse_arr.reshape(-1, N_COARSE_CODEBOOKS).T - SEMANTIC_VOCAB_SIZE
    for n in range(1, N_COARSE_CODEBOOKS):
        gen_coarse_audio_arr[n, :] -= n * CODEBOOK_SIZE
    _clear_cuda_cache()
    if SUNO_USE_DIRECTML is True:
        clean_models()
    return gen_coarse_audio_arr


def generate_coarse_amd_directml(
    x_semantic,
    history_prompt=None,
    temp=0.7,
    top_k=None,
    top_p=None,
    silent=False,
    max_coarse_history=630,  # min 60 (faster), max 630 (more context)
    sliding_window_len=60,
    use_kv_caching=True,
    x_coarse_history_alignment_hack=-2,
):
    """Generate coarse audio codes from semantic tokens."""

    logger.debug(locals())

    assert (
        isinstance(x_semantic, np.ndarray)
        and len(x_semantic.shape) == 1
        and len(x_semantic) > 0
        and x_semantic.min() >= 0
        and x_semantic.max() <= SEMANTIC_VOCAB_SIZE - 1
    )
    assert 60 <= max_coarse_history <= 630
    assert max_coarse_history + sliding_window_len <= 1024 - 256
    semantic_to_coarse_ratio = COARSE_RATE_HZ / SEMANTIC_RATE_HZ * N_COARSE_CODEBOOKS
    max_semantic_history = int(np.floor(max_coarse_history / semantic_to_coarse_ratio))
    if history_prompt is not None:
        history_prompt = _load_history_prompt(history_prompt)
        x_semantic_history = history_prompt["semantic_prompt"]
        x_coarse_history = history_prompt["coarse_prompt"]
        assert (
            isinstance(x_semantic_history, np.ndarray)
            and len(x_semantic_history.shape) == 1
            and len(x_semantic_history) > 0
            and x_semantic_history.min() >= 0
            and x_semantic_history.max() <= SEMANTIC_VOCAB_SIZE - 1
            and isinstance(x_coarse_history, np.ndarray)
            and len(x_coarse_history.shape) == 2
            and x_coarse_history.shape[0] == N_COARSE_CODEBOOKS
            and x_coarse_history.shape[-1] >= 0
            and x_coarse_history.min() >= 0
            and x_coarse_history.max() <= CODEBOOK_SIZE - 1
            and (
                round(x_coarse_history.shape[-1] / len(x_semantic_history), 1)
                == round(semantic_to_coarse_ratio / N_COARSE_CODEBOOKS, 1)
            )
        )
        x_coarse_history = _flatten_codebooks(x_coarse_history) + SEMANTIC_VOCAB_SIZE
        # trim histories correctly
        n_semantic_hist_provided = np.min(
            [
                max_semantic_history,
                len(x_semantic_history) - len(x_semantic_history) % 2,
                int(np.floor(len(x_coarse_history) / semantic_to_coarse_ratio)),
            ]
        )
        n_coarse_hist_provided = int(round(n_semantic_hist_provided * semantic_to_coarse_ratio))
        x_semantic_history = x_semantic_history[-n_semantic_hist_provided:].astype(np.int32)
        x_coarse_history = x_coarse_history[-n_coarse_hist_provided:].astype(np.int32)
        # TODO: bit of a hack for time alignment (sounds better)
        x_coarse_history = x_coarse_history[:-2]
    else:
        x_semantic_history = np.array([], dtype=np.int32)
        x_coarse_history = np.array([], dtype=np.int32)
    # load models if not yet exist
    global models
    global models_devices
    if "coarse" not in models:
        if SUNO_USE_DIRECTML is True:
            preload_models(load_one_model_type="coarse")
        else:
            preload_models()
    model = models["coarse"]
    if OFFLOAD_CPU:
        if GLOBAL_ENABLE_MPS:
            device = _grab_best_device(use_gpu=False)
            models_devices["coarse"] = device
        model.to(models_devices["coarse"])
    # device = next(model.parameters()).device

    # start loop
    n_steps = int(
        round(
            np.floor(len(x_semantic) * semantic_to_coarse_ratio / N_COARSE_CODEBOOKS)
            * N_COARSE_CODEBOOKS
        )
    )
    assert n_steps > 0 and n_steps % N_COARSE_CODEBOOKS == 0
    x_semantic = np.hstack([x_semantic_history, x_semantic]).astype(np.int32)
    x_coarse = x_coarse_history.astype(np.int32)
    base_semantic_idx = len(x_semantic_history)
    cumulative_time = 0
    with _inference_mode():
        try:
            # x_semantic_in = torch.from_numpy(x_semantic)[None].to(dml)
            x_semantic_in_np = x_semantic[None]
            # x_coarse_in = torch.from_numpy(x_coarse)[None].to(dml)
            x_coarse_in_np = x_coarse[None]
            n_window_steps = int(np.ceil(n_steps / sliding_window_len))
            n_step = 0
            for _ in tqdm.tqdm(range(n_window_steps), total=n_window_steps, disable=silent):
                semantic_idx = base_semantic_idx + int(round(n_step / semantic_to_coarse_ratio))
                # pad from right side
                x_in_np = x_semantic_in_np[:, np.max([0, semantic_idx - max_semantic_history]) :]
                x_in_np = x_in_np[:, :256]
                """
                x_in_np = F.pad(
                    x_in_np,
                    (0, 256 - x_in_np.shape[-1]),
                    "constant",
                    COARSE_SEMANTIC_PAD_TOKEN,
                )
                """
                np_pad_size = ((0, 0), (0, 256 - x_in_np.shape[-1]))
                x_in_np = np.pad(
                    x_in_np,
                    np_pad_size,
                    constant_values=COARSE_SEMANTIC_PAD_TOKEN,
                    mode="constant",
                )

                """
                x_in = torch.hstack(
                    [
                        x_in,
                        torch.tensor([COARSE_INFER_TOKEN])[None].to(dml),
                        x_coarse_in[:, -max_coarse_history:],
                    ]
                )
                """

                coarse_infer_token_np = np.array([COARSE_INFER_TOKEN])[None]

                x_in_np = np.hstack(
                    [
                        x_in_np,
                        coarse_infer_token_np,
                        x_coarse_in_np[:, -max_coarse_history:],
                    ]
                )

                kv_cache = None
                for _ in range(sliding_window_len):
                    if n_step >= n_steps:
                        continue
                    is_major_step = n_step % N_COARSE_CODEBOOKS == 0

                    if use_kv_caching and kv_cache is not None:
                        x_input = x_in_np[:, [-1]]
                    else:
                        x_input = x_in_np

                    x_input_tensor = torch.from_numpy(x_input).to(dml)

                    logits, kv_cache = model(
                        x_input_tensor, use_cache=use_kv_caching, past_kv=kv_cache
                    )

                    logit_start_idx = SEMANTIC_VOCAB_SIZE + (1 - int(is_major_step)) * CODEBOOK_SIZE
                    logit_end_idx = SEMANTIC_VOCAB_SIZE + (2 - int(is_major_step)) * CODEBOOK_SIZE
                    relevant_logits = logits[0, 0, logit_start_idx:logit_end_idx]

                    if top_p is not None:
                        # faster to convert to numpy
                        # original_device = relevant_logits.device
                        relevant_logits = relevant_logits.detach().cpu().type(torch.float32).numpy()
                        sorted_indices = np.argsort(relevant_logits)[::-1]
                        sorted_logits = relevant_logits[sorted_indices]
                        cumulative_probs = np.cumsum(softmax(sorted_logits))
                        sorted_indices_to_remove = cumulative_probs > top_p
                        sorted_indices_to_remove[1:] = sorted_indices_to_remove[:-1].copy()
                        sorted_indices_to_remove[0] = False
                        relevant_logits[sorted_indices[sorted_indices_to_remove]] = -np.inf
                        relevant_logits = torch.from_numpy(relevant_logits)
                        # relevant_logits = relevant_logits.to(original_device)
                        # stay as numpy, since we converted for directml anyway...
                    if top_k is not None:
                        v, _ = torch.topk(
                            relevant_logits.to(dml),
                            min(top_k, relevant_logits.to(dml).size(-1)),
                        )
                        relevant_logits[relevant_logits < v[-1]] = -float("Inf")

                    # probs = F.softmax(relevant_logits.to(dml) / temp, dim=-1)

                    start_time = time.time()

                    # item_next = torch.multinomial(probs, num_samples=1).to(torch.int32)

                    probs_np = (
                        F.softmax(relevant_logits.to(dml) / temp, dim=-1)
                        .cpu()
                        .type(torch.float32)
                        .numpy()
                    )

                    item_next_np = np.random.choice(
                        np.arange(probs_np.shape[-1]), size=1, p=probs_np.flatten()
                    )

                    # item_next = torch.from_numpy(item_next_np).to(torch.int32).to(dml)

                    # doing in raw numpy same speed with AMD directML, but maybe faster if you setup MKL correctly?
                    # actually tha wasn't quite righ anyway...
                    end_time = time.time()
                    cumulative_time = cumulative_time + (end_time - start_time)

                    # amd_multinomial = torch_distributions.Categorical(probs)
                    # action = amd_multinomial.sample((1,))
                    # item_next = amd_multinomial.log_prob(action).to(torch.int32)

                    # multinomial bugged on mps: shuttle to cpu if necessary
                    # inf_device = probs.device
                    # if probs.device.type == "mps" or True:
                    #    probs = probs.to("cpu")
                    #    # print(f"Here in coarse: {probs.device}")
                    # item_next = torch.multinomial(probs, num_samples=1)
                    # probs = probs.to(inf_device)
                    # item_next = item_next.to(inf_device)

                    item_next_np += logit_start_idx

                    x_coarse_in_np = np.hstack((x_coarse_in_np, item_next_np[None]))

                    # x_coarse_in = torch.from_numpy(x_coarse_in_np).to(dml)
                    # x_in = torch.cat((x_in_np.to(dml), item_next_np[None]), dim=1)

                    x_in_np = np.hstack((x_in_np, item_next_np[None]))
                    del logits, relevant_logits, probs_np, item_next_np
                    n_step += 1
                del x_in_np
            del x_semantic_in_np
        except RuntimeError as e:
            print(f"RuntimeError: {e}")
            # show all possble details and traceback, print to output
            print(f"Traceback: {traceback.format_exc()}")  # and print(sys.exc_info()[2])
            print(f"Exception: {sys.exc_info()[2]}")

    if OFFLOAD_CPU:
        model.to("cpu")
    gen_coarse_arr = x_coarse_in_np.squeeze()[len(x_coarse_history) :]
    del x_coarse_in_np
    assert len(gen_coarse_arr) == n_steps
    gen_coarse_audio_arr = gen_coarse_arr.reshape(-1, N_COARSE_CODEBOOKS).T - SEMANTIC_VOCAB_SIZE
    for n in range(1, N_COARSE_CODEBOOKS):
        gen_coarse_audio_arr[n, :] -= n * CODEBOOK_SIZE
    _clear_cuda_cache()
    if SUNO_USE_DIRECTML is True:
        clean_models()
    return gen_coarse_audio_arr


def generate_fine(
    x_coarse_gen,
    history_prompt=None,
    temp=0.5,
    silent=True,
):
    if temp == 0:
        temp = 0.001

    """Generate full audio codes from coarse audio codes."""
    assert (
        isinstance(x_coarse_gen, np.ndarray)
        and len(x_coarse_gen.shape) == 2
        and 1 <= x_coarse_gen.shape[0] <= N_FINE_CODEBOOKS - 1
        and x_coarse_gen.shape[1] > 0
        and x_coarse_gen.min() >= 0
        and x_coarse_gen.max() <= CODEBOOK_SIZE - 1
    )
    if history_prompt is not None:
        history_prompt = _load_history_prompt(history_prompt)
        x_fine_history = history_prompt["fine_prompt"]
        assert (
            isinstance(x_fine_history, np.ndarray)
            and len(x_fine_history.shape) == 2
            and x_fine_history.shape[0] == N_FINE_CODEBOOKS
            and x_fine_history.shape[1] >= 0
            and x_fine_history.min() >= 0
            and x_fine_history.max() <= CODEBOOK_SIZE - 1
        )
    else:
        x_fine_history = None
    n_coarse = x_coarse_gen.shape[0]
    # load models if not yet exist
    global models
    global models_devices
    if "fine" not in models:
        if SUNO_USE_DIRECTML is True:
            preload_models(load_one_model_type="fine")
        else:
            preload_models()
    model = models["fine"]
    if OFFLOAD_CPU:
        if GLOBAL_ENABLE_MPS:
            device = _grab_best_device(use_gpu=False)
            models_devices["fine"] = device
        model.to(models_devices["fine"])
    device = next(model.parameters()).device
    # make input arr
    in_arr = np.vstack(
        [
            x_coarse_gen,
            np.zeros((N_FINE_CODEBOOKS - n_coarse, x_coarse_gen.shape[1]))
            + CODEBOOK_SIZE,  # padding
        ]
    ).astype(np.int32)
    # prepend history if available (max 512)
    if x_fine_history is not None:
        x_fine_history = x_fine_history.astype(np.int32)
        in_arr = np.hstack(
            [
                x_fine_history[:, -512:].astype(np.int32),
                in_arr,
            ]
        )
        n_history = x_fine_history[:, -512:].shape[1]
    else:
        n_history = 0
    n_remove_from_end = 0
    # need to pad if too short (since non-causal model)
    if in_arr.shape[1] < 1024:
        n_remove_from_end = 1024 - in_arr.shape[1]
        in_arr = np.hstack(
            [
                in_arr,
                np.zeros((N_FINE_CODEBOOKS, n_remove_from_end), dtype=np.int32) + CODEBOOK_SIZE,
            ]
        )
    # we can be lazy about fractional loop and just keep overwriting codebooks
    n_loops = np.max([0, int(np.ceil((x_coarse_gen.shape[1] - (1024 - n_history)) / 512))]) + 1
    with _inference_mode():
        if SUNO_USE_DIRECTML is True:
            device = dml
        in_arr = torch.tensor(in_arr.T).to(device)
        for n in tqdm.tqdm(range(n_loops), disable=silent):
            start_idx = np.min([n * 512, in_arr.shape[0] - 1024])
            start_fill_idx = np.min([n_history + n * 512, in_arr.shape[0] - 512])
            rel_start_fill_idx = start_fill_idx - start_idx
            in_buffer = in_arr[start_idx : start_idx + 1024, :][None]
            for nn in range(n_coarse, N_FINE_CODEBOOKS):
                logits = model(nn, in_buffer)
                if temp is None:
                    relevant_logits = logits[0, rel_start_fill_idx:, :CODEBOOK_SIZE]
                    codebook_preds = torch.argmax(relevant_logits, -1)
                else:
                    relevant_logits = logits[0, :, :CODEBOOK_SIZE] / temp
                    probs = F.softmax(relevant_logits, dim=-1)
                    codebook_preds = torch.multinomial(
                        probs[rel_start_fill_idx:1024], num_samples=1
                    ).reshape(-1)
                codebook_preds = codebook_preds.to(torch.int32)
                in_buffer[0, rel_start_fill_idx:, nn] = codebook_preds
                del logits, codebook_preds
            # transfer over info into model_in and convert to numpy
            for nn in range(n_coarse, N_FINE_CODEBOOKS):
                in_arr[
                    start_fill_idx : start_fill_idx + (1024 - rel_start_fill_idx), nn
                ] = in_buffer[0, rel_start_fill_idx:, nn]
            del in_buffer
        gen_fine_arr = in_arr.detach().cpu().numpy().squeeze().T
        del in_arr
    if OFFLOAD_CPU:
        model.to("cpu")
    gen_fine_arr = gen_fine_arr[:, n_history:]
    if n_remove_from_end > 0:
        gen_fine_arr = gen_fine_arr[:, :-n_remove_from_end]
    assert gen_fine_arr.shape[-1] == x_coarse_gen.shape[-1]
    _clear_cuda_cache()
    if SUNO_USE_DIRECTML is True:
        clean_models()
    return gen_fine_arr


def _flatten_codebooks(arr, offset_size=CODEBOOK_SIZE):
    assert len(arr.shape) == 2
    arr = arr.copy()
    if offset_size is not None:
        for n in range(1, arr.shape[0]):
            arr[n, :] += offset_size * n
    flat_arr = arr.ravel("F")
    return flat_arr


COARSE_SEMANTIC_PAD_TOKEN = 12_048
COARSE_INFER_TOKEN = 12_050


def codec_decode(fine_tokens):
    """Turn quantized audio codes into audio array using encodec."""
    # load models if not yet exist
    global models
    global models_devices
    if "codec" not in models:
        if SUNO_USE_DIRECTML is True:
            preload_models(load_one_model_type="codec")
        else:
            preload_models()
    model = models["codec"]
    if OFFLOAD_CPU:
        if GLOBAL_ENABLE_MPS:
            device = _grab_best_device(use_gpu=False)
            models_devices["codec"] = device
        model.to(models_devices["codec"])
    device = next(model.parameters()).device
    arr = torch.from_numpy(fine_tokens)[None]
    if SUNO_USE_DIRECTML is True:
        arr = arr.to(dml)
    else:
        arr = arr.to(device)
    arr = arr.transpose(0, 1)
    emb = model.quantizer.decode(arr)
    out = model.decoder(emb)
    audio_arr = out.detach().cpu().numpy().squeeze()
    del arr, emb, out
    if OFFLOAD_CPU:
        model.to("cpu")
    if SUNO_USE_DIRECTML is True:
        clean_models()
    return audio_arr


## Added:


# Just overriding this because somehow I keep loading the wrong models?
def load_model(use_gpu=True, use_small=False, force_reload=False, model_type="text"):
    logger.debug(locals())

    _load_model_f = funcy.partial(_load_model, model_type=model_type, use_small=use_small)
    if model_type not in ("text", "coarse", "fine"):
        raise NotImplementedError()
    global models
    global models_devices
    device = _grab_best_device(use_gpu=use_gpu)
    model_key = f"{model_type}"
    if OFFLOAD_CPU:
        models_devices[model_key] = device
        device = "cpu"
    if model_key not in models or force_reload:
        ckpt_path = _get_ckpt_path(model_type, use_small=use_small)
        clean_models(model_key=model_key)
        model = _load_model_f(ckpt_path, device)
        models[model_key] = model
    if model_type == "text":
        if SUNO_USE_DIRECTML is True:
            models[model_key]["model"].to(dml)
        else:
            models[model_key]["model"].to(device)
    else:
        if SUNO_USE_DIRECTML is True:
            models[model_key].to(dml)
        else:
            models[model_key].to(device)
    logger.debug(f"Loaded {model_key} onto {device}.")
    return models[model_key]


def print_loading_info(model_key, ckpt_path, device):
    device_str = str(device)
    if SUNO_USE_DIRECTML is True:
        device_str = "directml (partial AMD GPU support)"
    if GLOBAL_ENABLE_MPS:
        device_str = "cpu/mps: Partial Apple Support"
    if OFFLOAD_CPU:
        device_str = "cpu/gpu: Offloading, cpu until needed, then gpu"

    print(f"--Loading {model_key} model from {ckpt_path} to {device_str}")


def _load_model(ckpt_path, device, use_small=False, model_type="text"):
    if model_type == "text":
        ConfigClass = GPTConfig
        ModelClass = GPT
    elif model_type == "coarse":
        ConfigClass = GPTConfig
        ModelClass = GPT
    elif model_type == "fine":
        ConfigClass = FineGPTConfig
        ModelClass = FineGPT
    else:
        raise NotImplementedError()
    model_key = f"{model_type}_small" if use_small or USE_SMALL_MODELS else model_type
    model_info = REMOTE_MODEL_PATHS[model_key]
    if not os.path.exists(ckpt_path):
        logger.info(f"{model_type} model not found, downloading into `{CACHE_DIR}`.")

        remote_filename = hf_hub_url(model_info["repo_id"], model_info["file_name"])
        print(
            f"Downloading {model_key} {model_info['repo_id']} remote model file {remote_filename} {model_info['file_name']} to {CACHE_DIR}"
        )  # added
        _download(model_info["repo_id"], model_info["file_name"])

    print_loading_info(model_key, ckpt_path, device)

    # If I try to load straight to DML, I get a strange error. So doing in two steps.
    checkpoint = torch.load(ckpt_path, map_location=device)

    # this is a hack
    model_args = checkpoint["model_args"]
    if "input_vocab_size" not in model_args:
        model_args["input_vocab_size"] = model_args["vocab_size"]
        model_args["output_vocab_size"] = model_args["vocab_size"]
        del model_args["vocab_size"]
    gptconf = ConfigClass(**checkpoint["model_args"])
    model = ModelClass(gptconf)

    if SUNO_HALF_PRECISION:
        model = model.half()
    elif SUNO_HALF_BFLOAT16:
        model.bfloat16()

    state_dict = checkpoint["model"]
    # fixup checkpoint
    unwanted_prefix = "_orig_mod."
    for k, v in list(state_dict.items()):
        if k.startswith(unwanted_prefix):
            state_dict[k[len(unwanted_prefix) :]] = state_dict.pop(k)
    extra_keys = set(state_dict.keys()) - set(model.state_dict().keys())
    extra_keys = set([k for k in extra_keys if not k.endswith(".attn.bias")])
    missing_keys = set(model.state_dict().keys()) - set(state_dict.keys())
    missing_keys = set([k for k in missing_keys if not k.endswith(".attn.bias")])
    if len(extra_keys) != 0:
        raise ValueError(f"extra keys found: {extra_keys}")
    if len(missing_keys) != 0:
        raise ValueError(f"missing keys: {missing_keys}")
    model.load_state_dict(state_dict, strict=False)
    n_params = model.get_num_params()
    val_loss = checkpoint["best_val_loss"].item()
    logger.info(f"model loaded: {round(n_params/1e6,1)}M params, {round(val_loss,3)} loss")
    model.eval()
    if SUNO_USE_DIRECTML is True:
        model.to(dml)
    else:
        model.to(device)
    # del checkpoint, state_dict
    del checkpoint, state_dict, model_args, val_loss
    _clear_cuda_cache()
    if model_type == "text":
        tokenizer = BertTokenizer.from_pretrained("bert-base-multilingual-cased")

        return {
            "model": model,
            "tokenizer": tokenizer,
        }
    return model


def preload_models(
    text_use_gpu=True,
    text_use_small=False,
    coarse_use_gpu=True,
    coarse_use_small=False,
    fine_use_gpu=True,
    fine_use_small=False,
    codec_use_gpu=True,
    force_reload=False,
    load_one_model_type=None,
):
    """Load all the necessary models for the pipeline."""

    if SUNO_USE_DIRECTML is True:
        text_use_gpu = False
        coarse_use_gpu = False
        fine_use_gpu = False

    # What is going on here
    logger.debug(
        f"USE_SMALL_MODELS = {USE_SMALL_MODELS} GLOBAL_ENABLE_MPS = {GLOBAL_ENABLE_MPS}, OFFLOAD_CPU = {OFFLOAD_CPU}"
    )
    logger.debug(
        f"text_use_gpu = {text_use_gpu}, text_use_small = {text_use_small}, coarse_use_gpu = {coarse_use_gpu}, coarse_use_small = {coarse_use_small}, fine_use_gpu = {fine_use_gpu}, fine_use_small = {fine_use_small}, codec_use_gpu = {codec_use_gpu}, force_reload = {force_reload}"
    )

    if USE_SMALL_MODELS:
        text_use_small = True
        coarse_use_small = True
        fine_use_small = True

    if _grab_best_device() == "cpu" and (
        text_use_gpu or coarse_use_gpu or fine_use_gpu or codec_use_gpu
    ):
        warning_string = " -->No GPU being used. Careful, inference might be very slow!"

        if SUNO_USE_DIRECTML is True:
            warning_string = "-->GPU using DirectML (partial AMD GPU support)"
        if GLOBAL_ENABLE_MPS:
            warning_string = "-->cpu/mps: Partial Apple Support"

        # logger.warning(warning_string)
        print(f"{warning_string}")

    if load_one_model_type is not None:
        if load_one_model_type == "text":
            _ = load_model(
                model_type="text",
                use_gpu=text_use_gpu,
                use_small=text_use_small,
                force_reload=force_reload,
            )
        elif load_one_model_type == "coarse":
            _ = load_model(
                model_type="coarse",
                use_gpu=coarse_use_gpu,
                use_small=coarse_use_small,
                force_reload=force_reload,
            )
        elif load_one_model_type == "fine":
            _ = load_model(
                model_type="fine",
                use_gpu=fine_use_gpu,
                use_small=fine_use_small,
                force_reload=force_reload,
            )
        elif load_one_model_type == "codec":
            _ = load_codec_model(use_gpu=codec_use_gpu, force_reload=force_reload)
    else:
        _ = load_model(
            model_type="text",
            use_gpu=text_use_gpu,
            use_small=text_use_small,
            force_reload=force_reload,
        )
        _ = load_model(
            model_type="coarse",
            use_gpu=coarse_use_gpu,
            use_small=coarse_use_small,
            force_reload=force_reload,
        )
        _ = load_model(
            model_type="fine",
            use_gpu=fine_use_gpu,
            use_small=fine_use_small,
            force_reload=force_reload,
        )
        _ = load_codec_model(use_gpu=codec_use_gpu, force_reload=force_reload)