Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support CPU inference - use pytorch-implementation when xformers isn't installed #48

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

saart
Copy link

@saart saart commented Feb 1, 2025

No description provided.

@saart saart mentioned this pull request Feb 1, 2025
@saart saart changed the title support CPU inference - use a pytorch-implementation instead of xformers support CPU inference - use pytorch-implementation when xformers isn't installed Feb 1, 2025
)

def memory_efficient_attention(query, key, value, p):
""" This code is taken from https://facebookresearch.github.io/xformers/components/ops.html """

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"""
A simplified PyTorch implementation of memory-efficient attention.
Assumes inputs are already reshaped to 4D [batch, heads, seq_len, head_dim].
This implementation is SLOWER and LESS MEMORY-EFFICIENT than xformers,
and does NOT SUPPORT attn_bias.

    Args:
        query (Tensor): shape (batch, heads, seq_len, head_dim)
        key (Tensor): shape (batch, heads, seq_len, head_dim)
        value (Tensor): shape (batch, heads, seq_len, head_dim)
        p (float): dropout probability. Default: 0.0 (no dropout)
        attn_bias:  NOT SUPPORTED IN THIS FALLBACK.
        scale (float, optional): scaling factor for the dot product. If None,
            defaults to 1 / sqrt(head_dim).
    Returns:
        Tensor: shape (batch, heads, seq_len, head_dim)
    """
    
    Its batter to replace the comment with this, it will improve the developer experience.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants