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
a65dd315
Commit
a65dd315
authored
Jun 24, 2024
by
AUTOMATIC1111
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix T5
parent
34b4443c
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
27 additions
and
11 deletions
+27
-11
modules/models/sd3/other_impls.py
modules/models/sd3/other_impls.py
+27
-11
No files found.
modules/models/sd3/other_impls.py
View file @
a65dd315
...
@@ -11,6 +11,18 @@ from transformers import CLIPTokenizer, T5TokenizerFast
...
@@ -11,6 +11,18 @@ from transformers import CLIPTokenizer, T5TokenizerFast
#################################################################################################
#################################################################################################
class
AutocastLinear
(
nn
.
Linear
):
"""Same as usual linear layer, but casts its weights to whatever the parameter type is.
This is different from torch.autocast in a way that float16 layer processing float32 input
will return float16 with autocast on, and float32 with this. T5 seems to be fucked
if you do it in full float16 (returning almost all zeros in the final output).
"""
def
forward
(
self
,
x
):
return
torch
.
nn
.
functional
.
linear
(
x
,
self
.
weight
.
to
(
x
.
dtype
),
self
.
bias
.
to
(
x
.
dtype
)
if
self
.
bias
is
not
None
else
None
)
def
attention
(
q
,
k
,
v
,
heads
,
mask
=
None
):
def
attention
(
q
,
k
,
v
,
heads
,
mask
=
None
):
"""Convenience wrapper around a basic attention operation"""
"""Convenience wrapper around a basic attention operation"""
b
,
_
,
dim_head
=
q
.
shape
b
,
_
,
dim_head
=
q
.
shape
...
@@ -27,9 +39,9 @@ class Mlp(nn.Module):
...
@@ -27,9 +39,9 @@ class Mlp(nn.Module):
out_features
=
out_features
or
in_features
out_features
=
out_features
or
in_features
hidden_features
=
hidden_features
or
in_features
hidden_features
=
hidden_features
or
in_features
self
.
fc1
=
nn
.
Linear
(
in_features
,
hidden_features
,
bias
=
bias
,
dtype
=
dtype
,
device
=
device
)
self
.
fc1
=
Autocast
Linear
(
in_features
,
hidden_features
,
bias
=
bias
,
dtype
=
dtype
,
device
=
device
)
self
.
act
=
act_layer
self
.
act
=
act_layer
self
.
fc2
=
nn
.
Linear
(
hidden_features
,
out_features
,
bias
=
bias
,
dtype
=
dtype
,
device
=
device
)
self
.
fc2
=
Autocast
Linear
(
hidden_features
,
out_features
,
bias
=
bias
,
dtype
=
dtype
,
device
=
device
)
def
forward
(
self
,
x
):
def
forward
(
self
,
x
):
x
=
self
.
fc1
(
x
)
x
=
self
.
fc1
(
x
)
...
@@ -297,7 +309,6 @@ class T5XXLModel(SDClipModel):
...
@@ -297,7 +309,6 @@ class T5XXLModel(SDClipModel):
### T5 implementation, for the T5-XXL text encoder portion, largely pulled from upstream impl
### T5 implementation, for the T5-XXL text encoder portion, largely pulled from upstream impl
#################################################################################################
#################################################################################################
class
T5XXLTokenizer
(
SDTokenizer
):
class
T5XXLTokenizer
(
SDTokenizer
):
"""Wraps the T5 Tokenizer from HF into the SDTokenizer interface"""
"""Wraps the T5 Tokenizer from HF into the SDTokenizer interface"""
def
__init__
(
self
):
def
__init__
(
self
):
...
@@ -319,9 +330,9 @@ class T5LayerNorm(torch.nn.Module):
...
@@ -319,9 +330,9 @@ class T5LayerNorm(torch.nn.Module):
class
T5DenseGatedActDense
(
torch
.
nn
.
Module
):
class
T5DenseGatedActDense
(
torch
.
nn
.
Module
):
def
__init__
(
self
,
model_dim
,
ff_dim
,
dtype
,
device
):
def
__init__
(
self
,
model_dim
,
ff_dim
,
dtype
,
device
):
super
()
.
__init__
()
super
()
.
__init__
()
self
.
wi_0
=
nn
.
Linear
(
model_dim
,
ff_dim
,
bias
=
False
,
dtype
=
dtype
,
device
=
device
)
self
.
wi_0
=
Autocast
Linear
(
model_dim
,
ff_dim
,
bias
=
False
,
dtype
=
dtype
,
device
=
device
)
self
.
wi_1
=
nn
.
Linear
(
model_dim
,
ff_dim
,
bias
=
False
,
dtype
=
dtype
,
device
=
device
)
self
.
wi_1
=
Autocast
Linear
(
model_dim
,
ff_dim
,
bias
=
False
,
dtype
=
dtype
,
device
=
device
)
self
.
wo
=
nn
.
Linear
(
ff_dim
,
model_dim
,
bias
=
False
,
dtype
=
dtype
,
device
=
device
)
self
.
wo
=
Autocast
Linear
(
ff_dim
,
model_dim
,
bias
=
False
,
dtype
=
dtype
,
device
=
device
)
def
forward
(
self
,
x
):
def
forward
(
self
,
x
):
hidden_gelu
=
torch
.
nn
.
functional
.
gelu
(
self
.
wi_0
(
x
),
approximate
=
"tanh"
)
hidden_gelu
=
torch
.
nn
.
functional
.
gelu
(
self
.
wi_0
(
x
),
approximate
=
"tanh"
)
...
@@ -348,10 +359,10 @@ class T5Attention(torch.nn.Module):
...
@@ -348,10 +359,10 @@ class T5Attention(torch.nn.Module):
def
__init__
(
self
,
model_dim
,
inner_dim
,
num_heads
,
relative_attention_bias
,
dtype
,
device
):
def
__init__
(
self
,
model_dim
,
inner_dim
,
num_heads
,
relative_attention_bias
,
dtype
,
device
):
super
()
.
__init__
()
super
()
.
__init__
()
# Mesh TensorFlow initialization to avoid scaling before softmax
# Mesh TensorFlow initialization to avoid scaling before softmax
self
.
q
=
nn
.
Linear
(
model_dim
,
inner_dim
,
bias
=
False
,
dtype
=
dtype
,
device
=
device
)
self
.
q
=
Autocast
Linear
(
model_dim
,
inner_dim
,
bias
=
False
,
dtype
=
dtype
,
device
=
device
)
self
.
k
=
nn
.
Linear
(
model_dim
,
inner_dim
,
bias
=
False
,
dtype
=
dtype
,
device
=
device
)
self
.
k
=
Autocast
Linear
(
model_dim
,
inner_dim
,
bias
=
False
,
dtype
=
dtype
,
device
=
device
)
self
.
v
=
nn
.
Linear
(
model_dim
,
inner_dim
,
bias
=
False
,
dtype
=
dtype
,
device
=
device
)
self
.
v
=
Autocast
Linear
(
model_dim
,
inner_dim
,
bias
=
False
,
dtype
=
dtype
,
device
=
device
)
self
.
o
=
nn
.
Linear
(
inner_dim
,
model_dim
,
bias
=
False
,
dtype
=
dtype
,
device
=
device
)
self
.
o
=
Autocast
Linear
(
inner_dim
,
model_dim
,
bias
=
False
,
dtype
=
dtype
,
device
=
device
)
self
.
num_heads
=
num_heads
self
.
num_heads
=
num_heads
self
.
relative_attention_bias
=
None
self
.
relative_attention_bias
=
None
if
relative_attention_bias
:
if
relative_attention_bias
:
...
@@ -421,11 +432,16 @@ class T5Attention(torch.nn.Module):
...
@@ -421,11 +432,16 @@ class T5Attention(torch.nn.Module):
q
=
self
.
q
(
x
)
q
=
self
.
q
(
x
)
k
=
self
.
k
(
x
)
k
=
self
.
k
(
x
)
v
=
self
.
v
(
x
)
v
=
self
.
v
(
x
)
if
self
.
relative_attention_bias
is
not
None
:
if
self
.
relative_attention_bias
is
not
None
:
past_bias
=
self
.
compute_bias
(
x
.
shape
[
1
],
x
.
shape
[
1
],
x
.
device
)
past_bias
=
self
.
compute_bias
(
x
.
shape
[
1
],
x
.
shape
[
1
],
x
.
device
)
if
past_bias
is
not
None
:
if
past_bias
is
not
None
:
mask
=
past_bias
mask
=
past_bias
out
=
attention
(
q
,
k
*
((
k
.
shape
[
-
1
]
/
self
.
num_heads
)
**
0.5
),
v
,
self
.
num_heads
,
mask
)
else
:
mask
=
None
out
=
attention
(
q
,
k
*
((
k
.
shape
[
-
1
]
/
self
.
num_heads
)
**
0.5
),
v
,
self
.
num_heads
,
mask
.
to
(
x
.
dtype
)
if
mask
is
not
None
else
None
)
return
self
.
o
(
out
),
past_bias
return
self
.
o
(
out
),
past_bias
...
...
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