Skip to content

Commit 2d6d4ed

Browse files
patil-surajpcuenca
andauthored
use memory_efficient_attention by default (huggingface#1354)
* use memory_efficient_attention by default * Update src/diffusers/models/attention.py Co-authored-by: Pedro Cuenca <pedro@huggingface.co>
1 parent 8b84f85 commit 2d6d4ed

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

src/diffusers/models/attention.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
import math
15+
import warnings
1516
from dataclasses import dataclass
1617
from typing import Optional
1718

@@ -396,6 +397,16 @@ def __init__(
396397
self.norm2 = nn.LayerNorm(dim)
397398
self.norm3 = nn.LayerNorm(dim)
398399

400+
# if xformers is installed try to use memory_efficient_attention by default
401+
if is_xformers_available():
402+
try:
403+
self._set_use_memory_efficient_attention_xformers(True)
404+
except Exception as e:
405+
warnings.warn(
406+
"Could not enable memory efficient attention. Make sure xformers is installed"
407+
f" correctly and a GPU is available: {e}"
408+
)
409+
399410
def _set_attention_slice(self, slice_size):
400411
self.attn1._slice_size = slice_size
401412
self.attn2._slice_size = slice_size

0 commit comments

Comments
 (0)