-
Notifications
You must be signed in to change notification settings - Fork 3
/
invokeai_installer.ps1
3464 lines (2983 loc) · 139 KB
/
invokeai_installer.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
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
# 有关 PowerShell 脚本保存编码的问题: https://learn.microsoft.com/zh-cn/powershell/module/microsoft.powershell.core/about/about_character_encoding?view=powershell-7.4#the-byte-order-mark
# InvokeAI Installer 版本和检查更新间隔
$INVOKEAI_INSTALLER_VERSION = 152
$UPDATE_TIME_SPAN = 3600
# Pip 镜像源
$PIP_INDEX_ADDR = "https://mirrors.cloud.tencent.com/pypi/simple"
$PIP_INDEX_ADDR_ORI = "https://pypi.python.org/simple"
$PIP_EXTRA_INDEX_ADDR = "https://mirrors.cernet.edu.cn/pypi/web/simple"
$PIP_EXTRA_INDEX_ADDR_ORI = "https://download.pytorch.org/whl"
$PIP_FIND_ADDR = "https://mirror.sjtu.edu.cn/pytorch-wheels/torch_stable.html"
$PIP_FIND_ADDR_ORI = "https://download.pytorch.org/whl/torch_stable.html"
$USE_PIP_MIRROR = if (!(Test-Path "$PSScriptRoot/disable_pip_mirror.txt")) { $true } else { $false }
$PIP_INDEX_MIRROR = if ($USE_PIP_MIRROR) { $PIP_INDEX_ADDR } else { $PIP_INDEX_ADDR_ORI }
$PIP_EXTRA_INDEX_MIRROR = if ($USE_PIP_MIRROR) { $PIP_EXTRA_INDEX_ADDR } else { $PIP_EXTRA_INDEX_ADDR_ORI }
$PIP_FIND_MIRROR = if ($USE_PIP_MIRROR) { $PIP_FIND_ADDR } else { $PIP_FIND_ADDR_ORI }
$PIP_EXTRA_INDEX_MIRROR_PYTORCH = "https://download.pytorch.org/whl"
$PIP_EXTRA_INDEX_MIRROR_CU121 = "https://download.pytorch.org/whl/cu121"
$PIP_EXTRA_INDEX_MIRROR_CU124 = "https://download.pytorch.org/whl/cu124"
# uv 最低版本
$UV_MINIMUM_VER = "0.5.2"
# PATH
$PYTHON_PATH = "$PSScriptRoot/InvokeAI/python"
$PYTHON_SCRIPTS_PATH = "$PSScriptRoot/InvokeAI/python/Scripts"
$Env:PATH = "$PYTHON_PATH$([System.IO.Path]::PathSeparator)$PYTHON_SCRIPTS_PATH$([System.IO.Path]::PathSeparator)$Env:PATH"
# 环境变量
$Env:PIP_INDEX_URL = $PIP_INDEX_MIRROR
$Env:PIP_EXTRA_INDEX_URL = $PIP_EXTRA_INDEX_MIRROR
$Env:PIP_FIND_LINKS = $PIP_FIND_MIRROR
$Env:UV_INDEX_URL = $PIP_INDEX_MIRROR
# $Env:UV_EXTRA_INDEX_URL = $PIP_EXTRA_INDEX_MIRROR
$Env:UV_FIND_LINKS = $PIP_FIND_MIRROR
$Env:UV_LINK_MODE = "copy"
$Env:UV_HTTP_TIMEOUT = 30
$Env:UV_CONCURRENT_DOWNLOADS = 50
$Env:PIP_DISABLE_PIP_VERSION_CHECK = 1
$Env:PIP_NO_WARN_SCRIPT_LOCATION = 0
$Env:PIP_TIMEOUT = 30
$Env:PIP_RETRIES = 5
$Env:PYTHONUTF8 = 1
$Env:PYTHONIOENCODING = "utf8"
$Env:CACHE_HOME = "$PSScriptRoot/InvokeAI/cache"
$Env:HF_HOME = "$PSScriptRoot/InvokeAI/cache/huggingface"
$Env:MATPLOTLIBRC = "$PSScriptRoot/InvokeAI/cache"
$Env:MODELSCOPE_CACHE = "$PSScriptRoot/InvokeAI/cache/modelscope/hub"
$Env:MS_CACHE_HOME = "$PSScriptRoot/InvokeAI/cache/modelscope/hub"
$Env:SYCL_CACHE_DIR = "$PSScriptRoot/InvokeAI/cache/libsycl_cache"
$Env:TORCH_HOME = "$PSScriptRoot/InvokeAI/cache/torch"
$Env:U2NET_HOME = "$PSScriptRoot/InvokeAI/cache/u2net"
$Env:XDG_CACHE_HOME = "$PSScriptRoot/InvokeAI/cache"
$Env:PIP_CACHE_DIR = "$PSScriptRoot/InvokeAI/cache/pip"
$Env:PYTHONPYCACHEPREFIX = "$PSScriptRoot/InvokeAI/cache/pycache"
$Env:INVOKEAI_ROOT = "$PSScriptRoot/InvokeAI/invokeai"
$Env:UV_CACHE_DIR = "$PSScriptRoot/InvokeAI/cache/uv"
$Env:UV_PYTHON = "$PSScriptRoot/InvokeAI/python/python.exe"
# 消息输出
function Print-Msg ($msg) {
Write-Host "[$(Get-Date -Format "yyyy-MM-dd HH:mm:ss")]" -ForegroundColor Yellow -NoNewline
Write-Host "[InvokeAI Installer]" -ForegroundColor Cyan -NoNewline
Write-Host ":: " -ForegroundColor Blue -NoNewline
Write-Host "$msg"
}
# 显示 InvokeAI Installer 版本
function Get-InvokeAI-Installer-Version {
$ver = $([string]$INVOKEAI_INSTALLER_VERSION).ToCharArray()
$major = ($ver[0..($ver.Length - 3)])
$minor = $ver[-2]
$micro = $ver[-1]
Print-Msg "InvokeAI Installer 版本: v${major}.${minor}.${micro}"
}
# Pip 镜像源状态
function Pip-Mirror-Status {
if ($USE_PIP_MIRROR) {
Print-Msg "使用 Pip 镜像源"
} else {
Print-Msg "检测到 disable_pip_mirror.txt 配置文件, 已将 Pip 源切换至官方源"
}
}
# 代理配置
function Set-Proxy {
$Env:NO_PROXY = "localhost,127.0.0.1,::1"
if (!(Test-Path "$PSScriptRoot/disable_proxy.txt")) { # 检测是否禁用自动设置镜像源
$INTERNET_SETTING = Get-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
if (Test-Path "$PSScriptRoot/proxy.txt") { # 本地存在代理配置
$proxy_value = Get-Content "$PSScriptRoot/proxy.txt"
$Env:HTTP_PROXY = $proxy_value
$Env:HTTPS_PROXY = $proxy_value
Print-Msg "检测到本地存在 proxy.txt 代理配置文件, 已读取代理配置文件并设置代理"
} elseif ($INTERNET_SETTING.ProxyEnable -eq 1) { # 系统已设置代理
$Env:HTTP_PROXY = "http://$($INTERNET_SETTING.ProxyServer)"
$Env:HTTPS_PROXY = "http://$($INTERNET_SETTING.ProxyServer)"
Print-Msg "检测到系统设置了代理, 已读取系统中的代理配置并设置代理"
}
} else {
Print-Msg "检测到本地存在 disable_proxy.txt 代理配置文件, 禁用自动设置代理"
}
}
# 设置 uv 的使用状态
function Set-uv {
if (Test-Path "$PSScriptRoot/disable_uv.txt") {
Print-Msg "检测到 disable_uv.txt 配置文件, 已禁用 uv, 使用 Pip 作为 Python 包管理器"
$Global:USE_UV = $false
} else {
Print-Msg "默认启用 uv 作为 Python 包管理器, 加快 Python 软件包的安装速度"
Print-Msg "当 uv 安装 Python 软件包失败时, 将自动切换成 Pip 重试 Python 软件包的安装"
$Global:USE_UV = $true
}
}
# 检查 uv 是否需要更新
function Check-uv-Version {
$content = "
import re
from importlib.metadata import version
def compare_versions(version1, version2) -> int:
try:
nums1 = re.sub(r'[a-zA-Z]+', '', version1).replace('-', '.').replace('+', '.').split('.')
nums2 = re.sub(r'[a-zA-Z]+', '', version2).replace('-', '.').replace('+', '.').split('.')
except:
return 0
for i in range(max(len(nums1), len(nums2))):
num1 = int(nums1[i]) if i < len(nums1) else 0
num2 = int(nums2[i]) if i < len(nums2) else 0
if num1 == num2:
continue
elif num1 > num2:
return 1
else:
return -1
return 0
def is_uv_need_update() -> bool:
try:
uv_ver = version('uv')
except:
return True
if compare_versions(uv_ver, uv_minimum_ver) == -1:
return True
else:
return False
uv_minimum_ver = '$UV_MINIMUM_VER'
print(is_uv_need_update())
"
Print-Msg "检测 uv 是否需要更新"
$status = $(python -c "$content")
if ($status -eq "True") {
Print-Msg "更新 uv 中"
python -m pip install -U "uv>=$UV_MINIMUM_VER"
if ($?) {
Print-Msg "uv 更新成功"
} else {
Print-Msg "uv 更新失败, 可能会造成 uv 部分功能异常"
}
} else {
Print-Msg "uv 无需更新"
}
}
# 下载并解压 Python
function Install-Python {
$url = "https://modelscope.cn/models/licyks/invokeai-core-model/resolve/master/pypatchmatch/python-3.10.15-amd64.zip"
# 下载 Python
Print-Msg "正在下载 Python"
Invoke-WebRequest -Uri $url -OutFile "$PSScriptRoot/InvokeAI/cache/python-3.10.15-amd64.zip"
if ($?) { # 检测是否下载成功并解压
# 创建 Python 文件夹
if (!(Test-Path "$PSScriptRoot/InvokeAI/python")) {
New-Item -ItemType Directory -Force -Path "$PSScriptRoot/InvokeAI/python" > $null
}
# 解压 Python
Print-Msg "正在解压 Python"
Expand-Archive -Path "$PSScriptRoot/InvokeAI/cache/python-3.10.15-amd64.zip" -DestinationPath "$PSScriptRoot/InvokeAI/python" -Force
Remove-Item -Path "$PSScriptRoot/InvokeAI/cache/python-3.10.15-amd64.zip"
Print-Msg "Python 安装成功"
} else {
Print-Msg "Python 安装失败, 终止 InvokeAI 安装进程, 可尝试重新运行 InvokeAI Installer 重试失败的安装"
Read-Host | Out-Null
exit 1
}
}
# 下载 uv
function Install-uv {
Print-Msg "正在下载 uv"
python -m pip install uv
if ($?) {
Print-Msg "uv 下载成功"
} else {
Print-Msg "uv 下载失败, 终止 InvokeAI 安装进程, 可尝试重新运行 InvokeAI Installer 重试失败的安装"
Read-Host | Out-Null
exit 1
}
}
# 安装 InvokeAI
function Install-InvokeAI {
# 下载 InvokeAI
$invokeai_ver = "InvokeAI==5.0.2"
Print-Msg "正在下载 InvokeAI"
if ($USE_UV) {
uv pip install $invokeai_ver --no-deps
if (!($?)) {
Print-Msg "检测到 uv 安装 Python 软件包失败, 尝试回滚至 Pip 重试 Python 软件包安装"
python -m pip install $invokeai_ver --no-deps --use-pep517
}
} else {
python -m pip install $invokeai_ver --no-deps --use-pep517
}
if ($?) { # 检测是否下载成功
Print-Msg "InvokeAI 安装成功"
} else {
Print-Msg "InvokeAI 安装失败, 终止 InvokeAI 安装进程, 可尝试重新运行 InvokeAI Installer 重试失败的安装"
Read-Host | Out-Null
exit 1
}
}
# 安装 InvokeAI 依赖
function Install-InvokeAI-Requirements {
$content = "
import re
from importlib.metadata import version
def compare_versions(version1, version2):
try:
nums1 = re.sub(r'[a-zA-Z]+', '', version1).replace('-', '.').replace('+', '.').split('.')
nums2 = re.sub(r'[a-zA-Z]+', '', version2).replace('-', '.').replace('+', '.').split('.')
except:
return 0
for i in range(max(len(nums1), len(nums2))):
num1 = int(nums1[i]) if i < len(nums1) else 0
num2 = int(nums2[i]) if i < len(nums2) else 0
if num1 == num2:
continue
elif num1 > num2:
return 1
else:
return -1
return 0
if compare_versions(version('invokeai'), '5.0.2') == 1:
print(True)
else:
print(False)
"
$status = $(python -c "$content") # 检查 InvokeAI 是否大于 5.0.2
if ($status -eq "True") {
$cuda_ver = "+cu124"
$Env:PIP_FIND_LINKS = " "
$Env:UV_FIND_LINKS = ""
$Env:PIP_EXTRA_INDEX_URL = "$Env:PIP_EXTRA_INDEX_URL $PIP_EXTRA_INDEX_MIRROR_CU124"
$Env:UV_EXTRA_INDEX_URL = $PIP_EXTRA_INDEX_MIRROR_CU124
} else {
$cuda_ver = "+cu118"
}
$content = "
from importlib.metadata import requires
pytorch_ver = []
cuda_ver = '$cuda_ver'
xf_cuda_ver = ''
ver_list = ''
invokeai_requires = requires('invokeai')
if cuda_ver == '+cu118':
xf_cuda_ver = cuda_ver
for i in invokeai_requires:
if i.startswith('torch=='):
pytorch_ver.append(i.split(';')[0].strip() + cuda_ver)
if i.startswith('torchvision=='):
pytorch_ver.append(i.split(';')[0].strip() + cuda_ver)
if i.startswith('torchaudio=='):
pytorch_ver.append(i.split(';')[0].strip() + cuda_ver)
if i.startswith('xformers=='):
pytorch_ver.append(i.split(';')[0].strip() + xf_cuda_ver)
for i in pytorch_ver:
ver_list = f'{ver_list} {i}'
print(ver_list)
"
$pytorch_ver = $(python -c "$content") # 获取 PyTorch 版本
Print-Msg "安装 InvokeAI 依赖中"
if ($USE_UV) {
uv pip install "InvokeAI[xformers]" $pytorch_ver.ToString().Split()
if (!($?)) {
Print-Msg "检测到 uv 安装 Python 软件包失败, 尝试回滚至 Pip 重试 Python 软件包安装"
python -m pip install "InvokeAI[xformers]" $pytorch_ver.ToString().Split() --use-pep517
}
} else {
python -m pip install "InvokeAI[xformers]" $pytorch_ver.ToString().Split() --use-pep517
}
if ($?) {
Print-Msg "InvokeAI 依赖安装成功"
} else {
Print-Msg "InvokeAI 依赖安装失败, 终止 InvokeAI 安装进程, 可尝试重新运行 InvokeAI Installer 重试失败的安装"
Read-Host | Out-Null
exit 1
}
}
# 下载 PyPatchMatch
function Install-PyPatchMatch {
# PyPatchMatch
$url_1 = "https://modelscope.cn/models/licyks/invokeai-core-model/resolve/master/pypatchmatch/libpatchmatch_windows_amd64.dll"
$url_2 = "https://modelscope.cn/models/licyks/invokeai-core-model/resolve/master/pypatchmatch/opencv_world460.dll"
if (!(Test-Path "$PSScriptRoot/InvokeAI/python/Lib/site-packages/patchmatch/libpatchmatch_windows_amd64.dll")) {
Print-Msg "下载 libpatchmatch_windows_amd64.dll 中"
Invoke-WebRequest -Uri $url_1 -OutFile "$PSScriptRoot/InvokeAI/cache/libpatchmatch_windows_amd64.dll"
if ($?) {
Move-Item -Path "$PSScriptRoot/InvokeAI/cache/libpatchmatch_windows_amd64.dll" -Destination "$PSScriptRoot/InvokeAI/python/Lib/site-packages/patchmatch/libpatchmatch_windows_amd64.dll" -Force
Print-Msg "下载 libpatchmatch_windows_amd64.dll 成功"
} else {
Print-Msg "下载 libpatchmatch_windows_amd64.dll 失败"
}
} else {
Print-Msg "无需下载 libpatchmatch_windows_amd64.dll"
}
if (!(Test-Path "$PSScriptRoot/InvokeAI/python/Lib/site-packages/patchmatch/opencv_world460.dll")) {
Print-Msg "下载 opencv_world460.dll 中"
Invoke-WebRequest -Uri $url_2 -OutFile "$PSScriptRoot/InvokeAI/cache/opencv_world460.dll"
if ($?) {
Move-Item -Path "$PSScriptRoot/InvokeAI/cache/opencv_world460.dll" -Destination "$PSScriptRoot/InvokeAI/python/Lib/site-packages/patchmatch/opencv_world460.dll" -Force
Print-Msg "下载 opencv_world460.dll 成功"
} else {
Print-Msg "下载 opencv_world460.dll 失败"
}
} else {
Print-Msg "无需下载 opencv_world460.dll"
}
}
# 下载配置文件
function Download-Config-File($url, $path) {
$length = $url.split("/").length
$name = $url.split("/")[$length - 1]
if (!(Test-Path $path)) {
Print-Msg "下载 $name 中"
Invoke-WebRequest -Uri $url.ToString() -OutFile "$PSScriptRoot/InvokeAI/cache/$name"
if ($?) {
Move-Item -Path "$PSScriptRoot/InvokeAI/cache/$name" -Destination "$path" -Force
Print-Msg "$name 下载成功"
} else {
Print-Msg "$name 下载失败"
}
} else {
Print-Msg "$name 已存在"
}
}
# 预下载模型配置文件
function Get-Model-Config-File {
Print-Msg "预下载模型配置文件中"
New-Item -ItemType Directory -Path "$PSScriptRoot/InvokeAI/invokeai/configs/stable-diffusion" -Force > $null
New-Item -ItemType Directory -Path "$PSScriptRoot/InvokeAI/invokeai/configs/controlnet" -Force > $null
Download-Config-File "https://modelscope.cn/models/licyks/invokeai-core-model/resolve/master/configs/stable-diffusion/sd_xl_base.yaml" "$PSScriptRoot/InvokeAI/invokeai/configs/stable-diffusion/sd_xl_base.yaml"
Download-Config-File "https://modelscope.cn/models/licyks/invokeai-core-model/resolve/master/configs/stable-diffusion/sd_xl_inpaint.yaml" "$PSScriptRoot/InvokeAI/invokeai/configs/stable-diffusion/sd_xl_inpaint.yaml"
Download-Config-File "https://modelscope.cn/models/licyks/invokeai-core-model/resolve/master/configs/stable-diffusion/sd_xl_refiner.yaml" "$PSScriptRoot/InvokeAI/invokeai/configs/stable-diffusion/sd_xl_refiner.yaml"
Download-Config-File "https://modelscope.cn/models/licyks/invokeai-core-model/resolve/master/configs/stable-diffusion/v1-finetune.yaml" "$PSScriptRoot/InvokeAI/invokeai/configs/stable-diffusion/v1-finetune.yaml"
Download-Config-File "https://modelscope.cn/models/licyks/invokeai-core-model/resolve/master/configs/stable-diffusion/v1-finetune_style.yaml" "$PSScriptRoot/InvokeAI/invokeai/configs/stable-diffusion/v1-finetune_style.yaml"
Download-Config-File "https://modelscope.cn/models/licyks/invokeai-core-model/resolve/master/configs/stable-diffusion/v1-inference-v.yaml" "$PSScriptRoot/InvokeAI/invokeai/configs/stable-diffusion/v1-inference-v.yaml"
Download-Config-File "https://modelscope.cn/models/licyks/invokeai-core-model/resolve/master/configs/stable-diffusion/v1-inference.yaml" "$PSScriptRoot/InvokeAI/invokeai/configs/stable-diffusion/v1-inference.yaml"
Download-Config-File "https://modelscope.cn/models/licyks/invokeai-core-model/resolve/master/configs/stable-diffusion/v1-inpainting-inference.yaml" "$PSScriptRoot/InvokeAI/invokeai/configs/stable-diffusion/v1-inpainting-inference.yaml"
Download-Config-File "https://modelscope.cn/models/licyks/invokeai-core-model/resolve/master/configs/stable-diffusion/v1-m1-finetune.yaml" "$PSScriptRoot/InvokeAI/invokeai/configs/stable-diffusion/v1-m1-finetune.yaml"
Download-Config-File "https://modelscope.cn/models/licyks/invokeai-core-model/resolve/master/configs/stable-diffusion/v2-inference-v.yaml" "$PSScriptRoot/InvokeAI/invokeai/configs/stable-diffusion/v2-inference-v.yaml"
Download-Config-File "https://modelscope.cn/models/licyks/invokeai-core-model/resolve/master/configs/stable-diffusion/v2-inference.yaml" "$PSScriptRoot/InvokeAI/invokeai/configs/stable-diffusion/v2-inference.yaml"
Download-Config-File "https://modelscope.cn/models/licyks/invokeai-core-model/resolve/master/configs/stable-diffusion/v2-inpainting-inference-v.yaml" "$PSScriptRoot/InvokeAI/invokeai/configs/stable-diffusion/v2-inpainting-inference-v.yaml"
Download-Config-File "https://modelscope.cn/models/licyks/invokeai-core-model/resolve/master/configs/stable-diffusion/v2-inpainting-inference.yaml" "$PSScriptRoot/InvokeAI/invokeai/configs/stable-diffusion/v2-inpainting-inference.yaml"
Download-Config-File "https://modelscope.cn/models/licyks/invokeai-core-model/resolve/master/configs/stable-diffusion/v2-midas-inference.yaml" "$PSScriptRoot/InvokeAI/invokeai/configs/stable-diffusion/v2-midas-inference.yaml"
Download-Config-File "https://modelscope.cn/models/licyks/invokeai-core-model/resolve/master/configs/controlnet/cldm_v15.yaml" "$PSScriptRoot/InvokeAI/invokeai/configs/controlnet/cldm_v15.yaml"
Download-Config-File "https://modelscope.cn/models/licyks/invokeai-core-model/resolve/master/configs/controlnet/cldm_v21.yaml" "$PSScriptRoot/InvokeAI/invokeai/configs/controlnet/cldm_v21.yaml"
Print-Msg "模型配置文件下载完成"
}
# 安装
function Check-Install {
if (!(Test-Path "$PSScriptRoot/InvokeAI")) {
New-Item -ItemType Directory -Path "$PSScriptRoot/InvokeAI" > $null
}
if (!(Test-Path "$PSScriptRoot/InvokeAI/cache")) {
New-Item -ItemType Directory -Path "$PSScriptRoot/InvokeAI/cache" > $null
}
Print-Msg "检测是否安装 Python"
if (Test-Path "$PSScriptRoot/InvokeAI/python/python.exe") {
Print-Msg "Python 已安装"
} else {
Print-Msg "Python 未安装"
Install-Python
}
Print-Msg "检测是否安装 uv"
python -m pip show uv --quiet 2> $null
if ($?) {
Print-Msg "uv 已安装"
} else {
Print-Msg "uv 未安装"
Install-uv
}
Check-uv-Version
Print-Msg "检查是否安装 InvokeAI"
python -m pip show invokeai --quiet 2> $null
if ($?) {
Print-Msg "InvokeAI 已安装"
} else {
Print-Msg "InvokeAI 未安装"
Install-InvokeAI
}
Install-InvokeAI-Requirements
Print-Msg "检测是否需要安装 PyPatchMatch"
Install-PyPatchMatch
Print-Msg "检测是否需要下载模型配置文件"
Get-Model-Config-File
Set-Content -Encoding UTF8 -Path "$PSScriptRoot/InvokeAI/update_time.txt" -Value $(Get-Date -Format "yyyy-MM-dd HH:mm:ss") # 记录更新时间
}
# 启动脚本
function Write-Launch-Script {
$content = "
# InvokeAI Installer 版本和检查更新间隔
`$INVOKEAI_INSTALLER_VERSION = $INVOKEAI_INSTALLER_VERSION
`$UPDATE_TIME_SPAN = $UPDATE_TIME_SPAN
# Pip 镜像源
`$PIP_INDEX_ADDR = `"$PIP_INDEX_ADDR`"
`$PIP_INDEX_ADDR_ORI = `"$PIP_INDEX_ADDR_ORI`"
`$PIP_EXTRA_INDEX_ADDR = `"$PIP_EXTRA_INDEX_ADDR`"
`$PIP_EXTRA_INDEX_ADDR_ORI = `"$PIP_EXTRA_INDEX_ADDR_ORI`"
`$PIP_FIND_ADDR = `"$PIP_FIND_ADDR`"
`$PIP_FIND_ADDR_ORI = `"$PIP_FIND_ADDR_ORI`"
`$USE_PIP_MIRROR = if (!(Test-Path `"`$PSScriptRoot/disable_pip_mirror.txt`")) { `$true } else { `$false }
`$PIP_INDEX_MIRROR = if (`$USE_PIP_MIRROR) { `$PIP_INDEX_ADDR } else { `$PIP_INDEX_ADDR_ORI }
`$PIP_EXTRA_INDEX_MIRROR = if (`$USE_PIP_MIRROR) { `$PIP_EXTRA_INDEX_ADDR } else { `$PIP_EXTRA_INDEX_ADDR_ORI }
`$PIP_FIND_MIRROR = if (`$USE_PIP_MIRROR) { `$PIP_FIND_ADDR } else { `$PIP_FIND_ADDR_ORI }
`$PIP_EXTRA_INDEX_MIRROR_PYTORCH = `"$PIP_EXTRA_INDEX_MIRROR_PYTORCH`"
`$PIP_EXTRA_INDEX_MIRROR_CU121 = `"$PIP_EXTRA_INDEX_MIRROR_CU121`"
`$PIP_EXTRA_INDEX_MIRROR_CU124 = `"$PIP_EXTRA_INDEX_MIRROR_CU124`"
# uv 最低版本
`$UV_MINIMUM_VER = `"$UV_MINIMUM_VER`"
# PATH
`$PYTHON_PATH = `"`$PSScriptRoot/python`"
`$PYTHON_SCRIPTS_PATH = `"`$PSScriptRoot/python/Scripts`"
`$Env:PATH = `"`$PYTHON_PATH`$([System.IO.Path]::PathSeparator)`$PYTHON_SCRIPTS_PATH`$([System.IO.Path]::PathSeparator)`$Env:PATH`"
# 环境变量
`$Env:PIP_INDEX_URL = `"`$PIP_INDEX_MIRROR`"
`$Env:PIP_EXTRA_INDEX_URL = `"`$PIP_EXTRA_INDEX_MIRROR`"
`$Env:PIP_FIND_LINKS = `"`$PIP_FIND_MIRROR`"
`$Env:UV_INDEX_URL = `"`$PIP_INDEX_MIRROR`"
# `$Env:UV_EXTRA_INDEX_URL = `"`$PIP_EXTRA_INDEX_MIRROR`"
`$Env:UV_FIND_LINKS = `"`$PIP_FIND_MIRROR`"
`$Env:UV_LINK_MODE = `"copy`"
`$Env:UV_HTTP_TIMEOUT = 30
`$Env:UV_CONCURRENT_DOWNLOADS = 50
`$Env:PIP_DISABLE_PIP_VERSION_CHECK = 1
`$Env:PIP_NO_WARN_SCRIPT_LOCATION = 0
`$Env:PIP_TIMEOUT = 30
`$Env:PIP_RETRIES = 5
`$Env:PYTHONUTF8 = 1
`$Env:PYTHONIOENCODING = `"utf8`"
`$Env:CACHE_HOME = `"`$PSScriptRoot/cache`"
`$Env:HF_HOME = `"`$PSScriptRoot/cache/huggingface`"
`$Env:MATPLOTLIBRC = `"`$PSScriptRoot/cache`"
`$Env:MODELSCOPE_CACHE = `"`$PSScriptRoot/cache/modelscope/hub`"
`$Env:MS_CACHE_HOME = `"`$PSScriptRoot/cache/modelscope/hub`"
`$Env:SYCL_CACHE_DIR = `"`$PSScriptRoot/cache/libsycl_cache`"
`$Env:TORCH_HOME = `"`$PSScriptRoot/cache/torch`"
`$Env:U2NET_HOME = `"`$PSScriptRoot/cache/u2net`"
`$Env:XDG_CACHE_HOME = `"`$PSScriptRoot/cache`"
`$Env:PIP_CACHE_DIR = `"`$PSScriptRoot/cache/pip`"
`$Env:PYTHONPYCACHEPREFIX = `"`$PSScriptRoot/cache/pycache`"
`$Env:INVOKEAI_ROOT = `"`$PSScriptRoot/invokeai`"
`$Env:UV_CACHE_DIR = `"`$PSScriptRoot/cache/uv`"
`$Env:UV_PYTHON = `"`$PSScriptRoot/python/python.exe`"
# 消息输出
function Print-Msg (`$msg) {
Write-Host `"[`$(Get-Date -Format `"yyyy-MM-dd HH:mm:ss`")]`" -ForegroundColor Yellow -NoNewline
Write-Host `"[InvokeAI Installer]`" -ForegroundColor Cyan -NoNewline
Write-Host `":: `" -ForegroundColor Blue -NoNewline
Write-Host `"`$msg`"
}
# 显示 InvokeAI Installer 版本
function Get-InvokeAI-Installer-Version {
`$ver = `$([string]`$INVOKEAI_INSTALLER_VERSION).ToCharArray()
`$major = (`$ver[0..(`$ver.Length - 3)])
`$minor = `$ver[-2]
`$micro = `$ver[-1]
Print-Msg `"InvokeAI Installer 版本: v`${major}.`${minor}.`${micro}`"
}
# Pip 镜像源状态
function Pip-Mirror-Status {
if (`$USE_PIP_MIRROR) {
Print-Msg `"使用 Pip 镜像源`"
} else {
Print-Msg `"检测到 disable_pip_mirror.txt 配置文件, 已将 Pip 源切换至官方源`"
}
}
# InvokeAI Installer 更新检测
function Check-InvokeAI-Installer-Update {
# 可用的下载源
`$urls = @(`"https://github.com/licyk/sd-webui-all-in-one/raw/main/invokeai_installer.ps1`", `"https://gitlab.com/licyk/sd-webui-all-in-one/-/raw/main/invokeai_installer.ps1`", `"https://gitee.com/licyk/sd-webui-all-in-one/raw/main/invokeai_installer.ps1`", `"https://github.com/licyk/sd-webui-all-in-one/releases/download/invokeai_installer/invokeai_installer.ps1`", `"https://gitee.com/licyk/sd-webui-all-in-one/releases/download/invokeai_installer/invokeai_installer.ps1`")
`$i = 0
New-Item -ItemType Directory -Path `"`$PSScriptRoot/cache`" -Force > `$null
if (Test-Path `"`$PSScriptRoot/disable_update.txt`") {
Print-Msg `"检测到 disable_update.txt 更新配置文件, 已禁用 InvokeAI Installer 的自动检查更新功能`"
return
}
# 获取更新时间间隔
try {
`$last_update_time = Get-Content `"`$PSScriptRoot/update_time.txt`" 2> `$null
`$last_update_time = Get-Date `$last_update_time -Format `"yyyy-MM-dd HH:mm:ss`"
}
catch {
`$last_update_time = Get-Date 0 -Format `"yyyy-MM-dd HH:mm:ss`"
}
finally {
`$update_time = Get-Date -Format `"yyyy-MM-dd HH:mm:ss`"
`$time_span = New-TimeSpan -Start `$last_update_time -End `$update_time
}
if (`$time_span.TotalSeconds -gt `$UPDATE_TIME_SPAN) {
Set-Content -Encoding UTF8 -Path `"`$PSScriptRoot/update_time.txt`" -Value `$(Get-Date -Format `"yyyy-MM-dd HH:mm:ss`") # 记录更新时间
ForEach (`$url in `$urls) {
Print-Msg `"检查 InvokeAI Installer 更新中`"
Invoke-WebRequest -Uri `$url -OutFile `"`$PSScriptRoot/cache/invokeai_installer.ps1`"
if (`$?) {
`$latest_version = [int]`$(Get-Content `"`$PSScriptRoot/cache/invokeai_installer.ps1`" | Select-String -Pattern `"INVOKEAI_INSTALLER_VERSION`" | ForEach-Object { `$_.ToString() })[0].Split(`"=`")[1].Trim()
if (`$latest_version -gt `$INVOKEAI_INSTALLER_VERSION) {
New-Item -ItemType File -Path `"`$PSScriptRoot/use_update_mode.txt`" -Force > `$null
Print-Msg `"检测到 InvokeAI Installer 有新版本可用, 是否进行更新 (yes/no) ?`"
Print-Msg `"提示: 输入 yes 确认或 no 取消 (默认为 no)`"
`$arg = Read-Host `"=========================================>`"
if (`$arg -eq `"yes`" -or `$arg -eq `"y`" -or `$arg -eq `"YES`" -or `$arg -eq `"Y`") {
Print-Msg `"调用 InvokeAI Installer 进行更新中`"
`$folder_name = Split-Path `$PSScriptRoot -Leaf
if (!(`$folder_name -eq `"InvokeAI`")) { # 检测脚本所在文件夹是否符合要求
Remove-Item -Path `"`$PSScriptRoot/cache/invokeai_installer.ps1`" 2> `$null
Remove-Item -Path `"`$PSScriptRoot/use_update_mode.txt`" 2> `$null
Remove-Item -Path `"`$PSScriptRoot/update_time.txt`" 2> `$null
Print-Msg `"检测到 InvokeAI Installer 管理脚本所在文件夹名称不符合要求, 无法直接进行更新`"
Print-Msg `"当前 InvokeAI Installer 管理脚本所在文件夹名称: `$folder_name`"
Print-Msg `"请前往 `$(Split-Path `"`$PSScriptRoot`") 路径, 将名称为 `$folder_name 的文件夹改名为 InvokeAI, 再重新更新 InvokeAI Installer 管理脚本`"
Print-Msg `"终止 InvokeAI Installer 的更新`"
Read-Host | Out-Null
exit 1
}
Move-Item -Path `"`$PSScriptRoot/cache/invokeai_installer.ps1`" `"`$PSScriptRoot/../invokeai_installer.ps1`" -Force
. `"`$PSScriptRoot/../invokeai_installer.ps1`"
Print-Msg `"更新结束, 需重新启动 InvokeAI Installer 管理脚本以应用更新, 回车退出 InvokeAI Installer 管理脚本`"
Read-Host | Out-Null
exit 0
} else {
Remove-Item -Path `"`$PSScriptRoot/cache/invokeai_installer.ps1`" 2> `$null
Remove-Item -Path `"`$PSScriptRoot/use_update_mode.txt`" 2> `$null
Print-Msg `"跳过 InvokeAI Installer 更新`"
}
} else {
Remove-Item -Path `"`$PSScriptRoot/cache/invokeai_installer.ps1`" 2> `$null
Remove-Item -Path `"`$PSScriptRoot/use_update_mode.txt`" 2> `$null
Print-Msg `"InvokeAI Installer 已是最新版本`"
}
break
} else {
`$i += 1
if (`$i -lt `$urls.Length) {
Print-Msg `"重试检查 InvokeAI Installer 更新中`"
} else {
Print-Msg `"检查 InvokeAI Installer 更新失败`"
}
}
}
}
}
# 代理配置
function Set-Proxy {
`$Env:NO_PROXY = `"localhost,127.0.0.1,::1`"
if (!(Test-Path `"`$PSScriptRoot/disable_proxy.txt`")) { # 检测是否禁用自动设置镜像源
`$INTERNET_SETTING = Get-ItemProperty -Path `"HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings`"
if (Test-Path `"`$PSScriptRoot/proxy.txt`") { # 本地存在代理配置
`$proxy_value = Get-Content `"`$PSScriptRoot/proxy.txt`"
`$Env:HTTP_PROXY = `$proxy_value
`$Env:HTTPS_PROXY = `$proxy_value
Print-Msg `"检测到本地存在 proxy.txt 代理配置文件, 已读取代理配置文件并设置代理`"
} elseif (`$INTERNET_SETTING.ProxyEnable -eq 1) { # 系统已设置代理
`$Env:HTTP_PROXY = `"http://`$(`$INTERNET_SETTING.ProxyServer)`"
`$Env:HTTPS_PROXY = `"http://`$(`$INTERNET_SETTING.ProxyServer)`"
Print-Msg `"检测到系统设置了代理, 已读取系统中的代理配置并设置代理`"
}
} else {
Print-Msg `"检测到本地存在 disable_proxy.txt 代理配置文件, 禁用自动设置代理`"
}
}
# Huggingface 镜像源
function Set-HuggingFace-Mirror {
if (!(Test-Path `"`$PSScriptRoot/disable_mirror.txt`")) { # 检测是否禁用了自动设置huggingface镜像源
if (Test-Path `"`$PSScriptRoot/mirror.txt`") { # 本地存在huggingface镜像源配置
`$hf_mirror_value = Get-Content `"`$PSScriptRoot/mirror.txt`"
`$Env:HF_ENDPOINT = `$hf_mirror_value
Print-Msg `"检测到本地存在 mirror.txt 配置文件, 已读取该配置并设置 HuggingFace 镜像源`"
} else { # 使用默认设置
`$Env:HF_ENDPOINT = `"https://hf-mirror.com`"
Print-Msg `"使用默认 HuggingFace 镜像源`"
}
} else {
Print-Msg `"检测到本地存在 disable_mirror.txt 镜像源配置文件, 禁用自动设置 HuggingFace 镜像源`"
}
}
# 获取 InvokeAI 的网页端口
function Get-InvokeAI-Launch-Port {
`$port = `"9090`"
if (!(Test-Path `"`$PSScriptRoot/invokeai/invokeai.yaml`")) {
return `$port
}
Get-Content -Path `"`$PSScriptRoot/invokeai/invokeai.yaml`" | ForEach-Object {
`$matches = [regex]::Matches(`$_, '^(\w+): (.+)')
foreach (`$match in `$matches) {
`$key = `$match.Groups[1].Value
`$value = `$match.Groups[2].Value
if ((`$key -eq `"port`") -and (!(`$match.Groups[0].Value -eq `"`"))) {
`$port = `$value
break
}
}
}
return `$port
}
# 写入 InvokeAI 启动脚本
function Write-Launch-InvokeAI-Script {
`$content = `"
import re
import sys
from invokeai.app.run_app import run_app
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
sys.exit(run_app())
`"
if (!(Test-Path `"`$PSScriptRoot/cache`")) {
New-Item -ItemType Directory -Path `"`$PSScriptRoot/cache`" > `$null
}
Set-Content -Encoding UTF8 -Path `"`$PSScriptRoot/cache/launch_invokeai.py`" -Value `$content
}
# 设置 InvokeAI 的快捷启动方式
function Create-InvokeAI-Shortcut {
`$filename = `"InvokeAI`"
`$url = `"https://modelscope.cn/models/licyks/invokeai-core-model/resolve/master/pypatchmatch/invokeai_icon.ico`"
`$shortcut_icon = `"`$PSScriptRoot/invokeai_icon.ico`"
if (!(Test-Path `"`$PSScriptRoot/enable_shortcut.txt`")) {
return
}
Print-Msg `"检查 InvokeAI 快捷启动方式中`"
if (!(Test-Path `"`$shortcut_icon`")) {
Print-Msg `"获取 InvokeAI 图标中`"
Invoke-WebRequest -Uri `$url -OutFile `"`$PSScriptRoot/invokeai_icon.ico`"
if (!(`$?)) {
Print-Msg `"获取 InvokeAI 图标失败, 无法创建 InvokeAI 快捷启动方式`"
return
}
}
Print-Msg `"更新 InvokeAI 快捷启动方式`"
`$shell = New-Object -ComObject WScript.Shell
`$desktop = [System.Environment]::GetFolderPath(`"Desktop`")
`$shortcut_path = `"`$desktop\`$filename.lnk`"
`$shortcut = `$shell.CreateShortcut(`$shortcut_path)
`$shortcut.TargetPath = `"`$PSHome\powershell.exe`"
`$launch_script_path = `$(Get-Item `"`$PSScriptRoot/launch.ps1`").FullName
`$shortcut.Arguments = `"-File ```"`$launch_script_path```"`"
`$shortcut.IconLocation = `$shortcut_icon
# 保存到桌面
`$shortcut.Save()
`$start_menu_path = `"`$Env:APPDATA/Microsoft/Windows/Start Menu/Programs`"
`$taskbar_path = `"`$Env:APPDATA\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar`"
# 保存到开始菜单
Copy-Item -Path `"`$shortcut_path`" -Destination `"`$start_menu_path`" -Force
# 固定到任务栏
# Copy-Item -Path `"`$shortcut_path`" -Destination `"`$taskbar_path`" -Force
# `$shell = New-Object -ComObject Shell.Application
# `$shell.Namespace([System.IO.Path]::GetFullPath(`$taskbar_path)).ParseName((Get-Item `$shortcut_path).Name).InvokeVerb('taskbarpin')
}
# 设置 CUDA 内存分配器
function Set-PyTorch-CUDA-Memory-Alloc {
if (!(Test-Path `"`$PSScriptRoot/disable_set_pytorch_cuda_memory_alloc.txt`")) {
Print-Msg `"检测是否可设置 CUDA 内存分配器`"
} else {
Print-Msg `"检测到 disable_set_pytorch_cuda_memory_alloc.txt 配置文件, 已禁用自动设置 CUDA 内存分配器`"
return
}
`$content = `"
import os
import importlib.util
import subprocess
#Can't use pytorch to get the GPU names because the cuda malloc has to be set before the first import.
def get_gpu_names():
if os.name == 'nt':
import ctypes
# Define necessary C structures and types
class DISPLAY_DEVICEA(ctypes.Structure):
_fields_ = [
('cb', ctypes.c_ulong),
('DeviceName', ctypes.c_char * 32),
('DeviceString', ctypes.c_char * 128),
('StateFlags', ctypes.c_ulong),
('DeviceID', ctypes.c_char * 128),
('DeviceKey', ctypes.c_char * 128)
]
# Load user32.dll
user32 = ctypes.windll.user32
# Call EnumDisplayDevicesA
def enum_display_devices():
device_info = DISPLAY_DEVICEA()
device_info.cb = ctypes.sizeof(device_info)
device_index = 0
gpu_names = set()
while user32.EnumDisplayDevicesA(None, device_index, ctypes.byref(device_info), 0):
device_index += 1
gpu_names.add(device_info.DeviceString.decode('utf-8'))
return gpu_names
return enum_display_devices()
else:
gpu_names = set()
out = subprocess.check_output(['nvidia-smi', '-L'])
for l in out.split(b'\n'):
if len(l) > 0:
gpu_names.add(l.decode('utf-8').split(' (UUID')[0])
return gpu_names
blacklist = {'GeForce GTX TITAN X', 'GeForce GTX 980', 'GeForce GTX 970', 'GeForce GTX 960', 'GeForce GTX 950', 'GeForce 945M',
'GeForce 940M', 'GeForce 930M', 'GeForce 920M', 'GeForce 910M', 'GeForce GTX 750', 'GeForce GTX 745', 'Quadro K620',
'Quadro K1200', 'Quadro K2200', 'Quadro M500', 'Quadro M520', 'Quadro M600', 'Quadro M620', 'Quadro M1000',
'Quadro M1200', 'Quadro M2000', 'Quadro M2200', 'Quadro M3000', 'Quadro M4000', 'Quadro M5000', 'Quadro M5500', 'Quadro M6000',
'GeForce MX110', 'GeForce MX130', 'GeForce 830M', 'GeForce 840M', 'GeForce GTX 850M', 'GeForce GTX 860M',
'GeForce GTX 1650', 'GeForce GTX 1630', 'Tesla M4', 'Tesla M6', 'Tesla M10', 'Tesla M40', 'Tesla M60'
}
def cuda_malloc_supported():
try:
names = get_gpu_names()
except:
names = set()
for x in names:
if 'NVIDIA' in x:
for b in blacklist:
if b in x:
return False
return True
def is_nvidia_device():
try:
names = get_gpu_names()
except:
names = set()
for x in names:
if 'NVIDIA' in x:
return True
return False
def get_pytorch_cuda_alloc_conf():
if is_nvidia_device():
if cuda_malloc_supported():
return 'cuda_malloc'
else:
return 'pytorch_malloc'
else:
return None
if __name__ == '__main__':
try:
version = ''
torch_spec = importlib.util.find_spec('torch')
for folder in torch_spec.submodule_search_locations:
ver_file = os.path.join(folder, 'version.py')
if os.path.isfile(ver_file):
spec = importlib.util.spec_from_file_location('torch_version_import', ver_file)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
version = module.__version__
if int(version[0]) >= 2: #enable by default for torch version 2.0 and up
print(get_pytorch_cuda_alloc_conf())
else:
print(None)
except:
print(None)
`"
`$status = `$(python -c `"`$content`")
switch (`$status) {
cuda_malloc {
Print-Msg `"设置 CUDA 内存分配器为 CUDA 内置异步分配器`"
`$Env:PYTORCH_CUDA_ALLOC_CONF = `"backend:cudaMallocAsync`"
}
pytorch_malloc {
Print-Msg `"设置 CUDA 内存分配器为 PyTorch 原生分配器`"
`$Env:PYTORCH_CUDA_ALLOC_CONF = `"garbage_collection_threshold:0.9,max_split_size_mb:512`"
}
Default {
Print-Msg `"显卡非 Nvidia 显卡, 无法设置 CUDA 内存分配器`"
}
}
}
function Main {
Print-Msg `"初始化中`"
Get-InvokeAI-Installer-Version
Set-Proxy
Check-InvokeAI-Installer-Update
Set-HuggingFace-Mirror
Pip-Mirror-Status
Create-InvokeAI-Shortcut
Set-PyTorch-CUDA-Memory-Alloc
`$port = Get-InvokeAI-Launch-Port
Print-Msg `"将使用浏览器打开 http://127.0.0.1:`$port 地址, 进入 InvokeAI 的界面`"
Print-Msg `"提示: 打开浏览器后, 浏览器可能会显示连接失败, 这是因为 InvokeAI 未完成启动, 可以在弹出的 PowerShell 中查看 InvokeAI 的启动过程, 等待 InvokeAI 启动完成后刷新浏览器网页即可`"
Print-Msg `"提示:如果 PowerShell 界面长时间不动, 并且 InvokeAI 未启动, 可以尝试按下几次回车键`"
Start-Sleep -Seconds 2
Print-Msg `"调用浏览器打开地址中`"
Start-Process `"http://127.0.0.1:`$port`"
Print-Msg `"启动 InvokeAI 中`"
Write-Launch-InvokeAI-Script
python `"`$PSScriptRoot/cache/launch_invokeai.py`" --root `"`$PSScriptRoot/invokeai`"
if (`$?) {
Print-Msg `"InvokeAI 正常退出`"
} else {
Print-Msg `"InvokeAI 出现异常, 已退出`"
}
}
###################
Main
Read-Host | Out-Null
"
if (Test-Path "$PSScriptRoot/InvokeAI/launch.ps1") {
Print-Msg "更新 launch.ps1 中"
} else {
Print-Msg "生成 launch.ps1 中"
}
Set-Content -Encoding UTF8 -Path "$PSScriptRoot/InvokeAI/launch.ps1" -Value $content
}
# 更新脚本
function Write-Update-Script {
$content = "
# InvokeAI Installer 版本和检查更新间隔
`$INVOKEAI_INSTALLER_VERSION = $INVOKEAI_INSTALLER_VERSION
`$UPDATE_TIME_SPAN = $UPDATE_TIME_SPAN
# Pip 镜像源
`$PIP_INDEX_ADDR = `"$PIP_INDEX_ADDR`"
`$PIP_INDEX_ADDR_ORI = `"$PIP_INDEX_ADDR_ORI`"
`$PIP_EXTRA_INDEX_ADDR = `"$PIP_EXTRA_INDEX_ADDR`"
`$PIP_EXTRA_INDEX_ADDR_ORI = `"$PIP_EXTRA_INDEX_ADDR_ORI`"
`$PIP_FIND_ADDR = `"$PIP_FIND_ADDR`"
`$PIP_FIND_ADDR_ORI = `"$PIP_FIND_ADDR_ORI`"
`$USE_PIP_MIRROR = if (!(Test-Path `"`$PSScriptRoot/disable_pip_mirror.txt`")) { `$true } else { `$false }
`$PIP_INDEX_MIRROR = if (`$USE_PIP_MIRROR) { `$PIP_INDEX_ADDR } else { `$PIP_INDEX_ADDR_ORI }
`$PIP_EXTRA_INDEX_MIRROR = if (`$USE_PIP_MIRROR) { `$PIP_EXTRA_INDEX_ADDR } else { `$PIP_EXTRA_INDEX_ADDR_ORI }
`$PIP_FIND_MIRROR = if (`$USE_PIP_MIRROR) { `$PIP_FIND_ADDR } else { `$PIP_FIND_ADDR_ORI }
`$PIP_EXTRA_INDEX_MIRROR_PYTORCH = `"$PIP_EXTRA_INDEX_MIRROR_PYTORCH`"
`$PIP_EXTRA_INDEX_MIRROR_CU121 = `"$PIP_EXTRA_INDEX_MIRROR_CU121`"
`$PIP_EXTRA_INDEX_MIRROR_CU124 = `"$PIP_EXTRA_INDEX_MIRROR_CU124`"
# uv 最低版本
`$UV_MINIMUM_VER = `"$UV_MINIMUM_VER`"
# PATH
`$PYTHON_PATH = `"`$PSScriptRoot/python`"
`$PYTHON_SCRIPTS_PATH = `"`$PSScriptRoot/python/Scripts`"
`$Env:PATH = `"`$PYTHON_PATH`$([System.IO.Path]::PathSeparator)`$PYTHON_SCRIPTS_PATH`$([System.IO.Path]::PathSeparator)`$Env:PATH`"
# 环境变量
`$Env:PIP_INDEX_URL = `"`$PIP_INDEX_MIRROR`"
`$Env:PIP_EXTRA_INDEX_URL = `"`$PIP_EXTRA_INDEX_MIRROR`"
`$Env:PIP_FIND_LINKS = `"`$PIP_FIND_MIRROR`"
`$Env:UV_INDEX_URL = `"`$PIP_INDEX_MIRROR`"
# `$Env:UV_EXTRA_INDEX_URL = `"`$PIP_EXTRA_INDEX_MIRROR`"
`$Env:UV_FIND_LINKS = `"`$PIP_FIND_MIRROR`"
`$Env:UV_LINK_MODE = `"copy`"
`$Env:UV_HTTP_TIMEOUT = 30
`$Env:UV_CONCURRENT_DOWNLOADS = 50
`$Env:PIP_DISABLE_PIP_VERSION_CHECK = 1
`$Env:PIP_NO_WARN_SCRIPT_LOCATION = 0
`$Env:PIP_TIMEOUT = 30
`$Env:PIP_RETRIES = 5
`$Env:PYTHONUTF8 = 1
`$Env:PYTHONIOENCODING = `"utf8`"
`$Env:CACHE_HOME = `"`$PSScriptRoot/cache`"
`$Env:HF_HOME = `"`$PSScriptRoot/cache/huggingface`"
`$Env:MATPLOTLIBRC = `"`$PSScriptRoot/cache`"
`$Env:MODELSCOPE_CACHE = `"`$PSScriptRoot/cache/modelscope/hub`"
`$Env:MS_CACHE_HOME = `"`$PSScriptRoot/cache/modelscope/hub`"
`$Env:SYCL_CACHE_DIR = `"`$PSScriptRoot/cache/libsycl_cache`"
`$Env:TORCH_HOME = `"`$PSScriptRoot/cache/torch`"
`$Env:U2NET_HOME = `"`$PSScriptRoot/cache/u2net`"
`$Env:XDG_CACHE_HOME = `"`$PSScriptRoot/cache`"
`$Env:PIP_CACHE_DIR = `"`$PSScriptRoot/cache/pip`"
`$Env:PYTHONPYCACHEPREFIX = `"`$PSScriptRoot/cache/pycache`"
`$Env:INVOKEAI_ROOT = `"`$PSScriptRoot/invokeai`"
`$Env:UV_CACHE_DIR = `"`$PSScriptRoot/cache/uv`"
`$Env:UV_PYTHON = `"`$PSScriptRoot/python/python.exe`"
# 消息输出
function Print-Msg (`$msg) {
Write-Host `"[`$(Get-Date -Format `"yyyy-MM-dd HH:mm:ss`")]`" -ForegroundColor Yellow -NoNewline