-
Notifications
You must be signed in to change notification settings - Fork 295
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
base: master
Are you sure you want to change the base?
Conversation
Optional<Expression> outputPresentValue; | ||
}; | ||
|
||
inline MultiHeadAttentionOutputs MultiHeadAttention( |
There was a problem hiding this comment.
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) | ||
{ |
There was a problem hiding this comment.
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]; |
There was a problem hiding this comment.
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.
There was a problem hiding this 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.
No description provided.