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

Add MultiheadAttention to DirectMLX #600

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

PatriceVignola
Copy link
Contributor

No description provided.

@PatriceVignola PatriceVignola requested a review from fdwr June 24, 2024 19:21
Optional<Expression> outputPresentValue;
};

inline MultiHeadAttentionOutputs MultiHeadAttention(
Copy link
Contributor

@fdwr fdwr Jun 25, 2024

Choose a reason for hiding this comment

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

    inline MultiheadAttentionOutputs MultiheadAttention(

Multihead is a single word (https://en.wiktionary.org/wiki/multihead / https://www.merriam-webster.com/dictionary/multiheaded), consistent with our enum DML_MULTIHEAD_ATTENTION_OPERATOR_DESC and with PyTorch (https://pytorch.org/docs/stable/generated/torch.nn.MultiheadAttention.html). The people using hyphens don't know better that "multi" is a prefix :b.

detail::GraphBuilder* builder = nullptr;

if (query)
{
Copy link
Contributor

Choose a reason for hiding this comment

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

(minor 🤷) assert(stackedKeyValue || (key && value)); for assertion consistency with the other branches?

{
assert(stackedQueryKeyValue);
assert(stackedQueryKeyValueTensor.sizes.size() >= 5);
batchSize = stackedQueryKeyValueTensor.sizes[stackedQueryKeyValueTensor.sizes.size() - 5];
Copy link
Contributor

@fdwr fdwr Jun 25, 2024

Choose a reason for hiding this comment

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

stackedQueryKeyValueTensor.sizes.size() - 5

I worry about callers using DMLX and directly populating tensors from some model description, and then DMLX accessing invalid negative indices here because the tensor size is too small, especially if that model description comes from external data that is not completely under the program's control. We could say that it's the responsibility of the caller to validate all these sizes up-front before calling DMLX, but even DML validates tensor sizes before accessing any potentially invalid indices. Can we strengthen these mere asserts which only happen in debug builds to an std::invalid_argument instead?

e.g.

            DMLX_THROW_IF_NOT(stackedQueryKeyValueTensor.sizes.size() >= 5, std::invalid_argument);
            batchSize = stackedQueryKeyValueTensor.sizes[stackedQueryKeyValueTensor.sizes.size() - 5];
#if __cpp_exceptions
    #if DMLX_USE_WIL
        #define DMLX_THROW_IF_FAILED(_hr) THROW_IF_FAILED(_hr)
        #define DMLX_THROW(_hr) THROW_HR(_hr)
        #define DMLX_THROW_IF_NOT(condition, exceptionType) if (!(condition)) { throw exceptionType; }
    #else
        #define DMLX_THROW_IF_FAILED(_hr) if (FAILED(_hr)) { throw std::runtime_error(#_hr); }
        #define DMLX_THROW(_hr) throw std::runtime_error(#_hr); 
        #define DMLX_THROW_IF_NOT(condition, exceptionType) if (!(condition)) { throw exceptionType; }
    #endif
#else
    #define DMLX_THROW_IF_FAILED(_hr) if (FAILED(_hr)) { std::abort(); }
    #define DMLX_THROW(_hr) { std::abort(); } 
    #define DMLX_THROW_IF_NOT(condition, exceptionType) { std::abort(); } 
#endif

I'm not proposing we turn every assert into an exception, as DML API validation will validate things too, and we don't need to doubly validate in DMLX, but at least to validate the cases where DMLX itself would access invalid memory.

Copy link
Contributor

@fdwr fdwr left a comment

Choose a reason for hiding this comment

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

3 comments. LGTM otherwise.

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