Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
S
Stable Diffusion Webui
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
novelai-storage
Stable Diffusion Webui
Commits
905b1423
Commit
905b1423
authored
Jan 09, 2024
by
AUTOMATIC1111
Committed by
GitHub
Jan 09, 2024
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #14597 from AUTOMATIC1111/improved-manual-cast
Improve the implementation of Manual Cast and IPEX support
parents
6869d958
ca671e5d
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
41 additions
and
16 deletions
+41
-16
modules/devices.py
modules/devices.py
+40
-16
modules/shared_init.py
modules/shared_init.py
+1
-0
No files found.
modules/devices.py
View file @
905b1423
...
@@ -110,6 +110,7 @@ device_codeformer: torch.device = None
...
@@ -110,6 +110,7 @@ device_codeformer: torch.device = None
dtype
:
torch
.
dtype
=
torch
.
float16
dtype
:
torch
.
dtype
=
torch
.
float16
dtype_vae
:
torch
.
dtype
=
torch
.
float16
dtype_vae
:
torch
.
dtype
=
torch
.
float16
dtype_unet
:
torch
.
dtype
=
torch
.
float16
dtype_unet
:
torch
.
dtype
=
torch
.
float16
dtype_inference
:
torch
.
dtype
=
torch
.
float16
unet_needs_upcast
=
False
unet_needs_upcast
=
False
...
@@ -131,21 +132,44 @@ patch_module_list = [
...
@@ -131,21 +132,44 @@ patch_module_list = [
]
]
def
manual_cast_forward
(
self
,
*
args
,
**
kwargs
):
def
manual_cast_forward
(
target_dtype
):
def
forward_wrapper
(
self
,
*
args
,
**
kwargs
):
if
any
(
isinstance
(
arg
,
torch
.
Tensor
)
and
arg
.
dtype
!=
target_dtype
for
arg
in
args
):
args
=
[
arg
.
to
(
target_dtype
)
if
isinstance
(
arg
,
torch
.
Tensor
)
else
arg
for
arg
in
args
]
kwargs
=
{
k
:
v
.
to
(
target_dtype
)
if
isinstance
(
v
,
torch
.
Tensor
)
else
v
for
k
,
v
in
kwargs
.
items
()}
org_dtype
=
torch_utils
.
get_param
(
self
)
.
dtype
org_dtype
=
torch_utils
.
get_param
(
self
)
.
dtype
self
.
to
(
dtype
)
if
org_dtype
!=
target_dtype
:
args
=
[
arg
.
to
(
dtype
)
if
isinstance
(
arg
,
torch
.
Tensor
)
else
arg
for
arg
in
args
]
self
.
to
(
target_dtype
)
kwargs
=
{
k
:
v
.
to
(
dtype
)
if
isinstance
(
v
,
torch
.
Tensor
)
else
v
for
k
,
v
in
kwargs
.
items
()}
result
=
self
.
org_forward
(
*
args
,
**
kwargs
)
result
=
self
.
org_forward
(
*
args
,
**
kwargs
)
if
org_dtype
!=
target_dtype
:
self
.
to
(
org_dtype
)
self
.
to
(
org_dtype
)
if
target_dtype
!=
dtype_inference
:
if
isinstance
(
result
,
tuple
):
result
=
tuple
(
i
.
to
(
dtype_inference
)
if
isinstance
(
i
,
torch
.
Tensor
)
else
i
for
i
in
result
)
elif
isinstance
(
result
,
torch
.
Tensor
):
result
=
result
.
to
(
dtype_inference
)
return
result
return
result
return
forward_wrapper
@
contextlib
.
contextmanager
@
contextlib
.
contextmanager
def
manual_cast
():
def
manual_cast
(
target_dtype
):
for
module_type
in
patch_module_list
:
for
module_type
in
patch_module_list
:
org_forward
=
module_type
.
forward
org_forward
=
module_type
.
forward
module_type
.
forward
=
manual_cast_forward
if
module_type
==
torch
.
nn
.
MultiheadAttention
and
has_xpu
():
module_type
.
forward
=
manual_cast_forward
(
torch
.
float32
)
else
:
module_type
.
forward
=
manual_cast_forward
(
target_dtype
)
module_type
.
org_forward
=
org_forward
module_type
.
org_forward
=
org_forward
try
:
try
:
yield
None
yield
None
...
@@ -161,15 +185,15 @@ def autocast(disable=False):
...
@@ -161,15 +185,15 @@ def autocast(disable=False):
if
fp8
and
device
==
cpu
:
if
fp8
and
device
==
cpu
:
return
torch
.
autocast
(
"cpu"
,
dtype
=
torch
.
bfloat16
,
enabled
=
True
)
return
torch
.
autocast
(
"cpu"
,
dtype
=
torch
.
bfloat16
,
enabled
=
True
)
if
fp8
and
(
dtype
==
torch
.
float32
or
shared
.
cmd_opts
.
precision
==
"full"
or
cuda_no_autocast
()):
if
fp8
and
dtype_inference
==
torch
.
float32
:
return
manual_cast
()
return
manual_cast
(
dtype
)
if
has_mps
()
and
shared
.
cmd_opts
.
precision
!=
"full"
:
return
manual_cast
()
if
dtype
==
torch
.
float32
or
shared
.
cmd_opts
.
precision
==
"full"
:
if
dtype
==
torch
.
float32
or
dtype_inference
==
torch
.
float32
:
return
contextlib
.
nullcontext
()
return
contextlib
.
nullcontext
()
if
has_xpu
()
or
has_mps
()
or
cuda_no_autocast
():
return
manual_cast
(
dtype
)
return
torch
.
autocast
(
"cuda"
)
return
torch
.
autocast
(
"cuda"
)
...
...
modules/shared_init.py
View file @
905b1423
...
@@ -29,6 +29,7 @@ def initialize():
...
@@ -29,6 +29,7 @@ def initialize():
devices
.
dtype
=
torch
.
float32
if
cmd_opts
.
no_half
else
torch
.
float16
devices
.
dtype
=
torch
.
float32
if
cmd_opts
.
no_half
else
torch
.
float16
devices
.
dtype_vae
=
torch
.
float32
if
cmd_opts
.
no_half
or
cmd_opts
.
no_half_vae
else
torch
.
float16
devices
.
dtype_vae
=
torch
.
float32
if
cmd_opts
.
no_half
or
cmd_opts
.
no_half_vae
else
torch
.
float16
devices
.
dtype_inference
=
torch
.
float32
if
cmd_opts
.
precision
==
'full'
else
devices
.
dtype
shared
.
device
=
devices
.
device
shared
.
device
=
devices
.
device
shared
.
weight_load_location
=
None
if
cmd_opts
.
lowram
else
"cpu"
shared
.
weight_load_location
=
None
if
cmd_opts
.
lowram
else
"cpu"
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment