-
Notifications
You must be signed in to change notification settings - Fork 60
[T1-3-1] GushanFall #387
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
Open
GushanFall
wants to merge
7
commits into
InfiniTensor:main
Choose a base branch
from
GushanFall:T1-3-1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[T1-3-1] GushanFall #387
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a0591f5
format
GushanFall beec5dc
fix some bugs
GushanFall 34a89e4
delete enable_gqa
GushanFall 6caf7cc
update
GushanFall be82b46
Merge branch 'main' into T1-3-1
GushanFall 9dc5330
Merge branch 'main' into T1-3-1
PanZezhong1725 1f3f915
T1-3-1:Modified part of the code according to the suggestions.
GushanFall File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,5 +20,5 @@ cache/ | |
| # JSON | ||
| *.json | ||
|
|
||
| #GGUF | ||
| # GGUF | ||
| *.gguf | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| #ifndef __INFINIOP_FLASH_ATTENTION_API_H__ | ||
| #define __INFINIOP_FLASH_ATTENTION_API_H__ | ||
|
|
||
| #include "../operator_descriptor.h" | ||
|
|
||
| typedef struct InfiniopDescriptor *infiniopFlashAttentionDescriptor_t; | ||
|
|
||
| __C __export infiniStatus_t infiniopCreateFlashAttentionDescriptor( | ||
| infiniopHandle_t handle, | ||
| infiniopFlashAttentionDescriptor_t *desc_ptr, | ||
| infiniopTensorDescriptor_t out_desc, | ||
| infiniopTensorDescriptor_t l_desc, | ||
| infiniopTensorDescriptor_t q_desc, | ||
| infiniopTensorDescriptor_t k_desc, | ||
| infiniopTensorDescriptor_t v_desc, | ||
| infiniopTensorDescriptor_t mask_desc, | ||
| infiniopAttentionMaskType_t mask_type); | ||
|
|
||
| __C __export infiniStatus_t infiniopGetFlashAttentionWorkspaceSize( | ||
| infiniopFlashAttentionDescriptor_t desc, | ||
| size_t *size); | ||
|
|
||
| __C __export infiniStatus_t infiniopFlashAttention( | ||
| infiniopFlashAttentionDescriptor_t desc, | ||
| void *workspace, | ||
| size_t workspace_size, | ||
| void *out, | ||
| void *l, | ||
| const void *q, | ||
| const void *k, | ||
| const void *v, | ||
| const void *mask, | ||
| void *stream); | ||
|
|
||
| __C __export infiniStatus_t infiniopDestroyFlashAttentionDescriptor(infiniopFlashAttentionDescriptor_t desc); | ||
|
|
||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| #ifndef __INFINIOP_FLASH_ATTENTION_BACKWARD_H__ | ||
| #define __INFINIOP_FLASH_ATTENTION_BACKWARD_H__ | ||
|
|
||
| #include "../operator_descriptor.h" | ||
|
|
||
| typedef struct InfiniopDescriptor *infiniopFlashAttentionBackwardDescriptor_t; | ||
|
|
||
| __C __export infiniStatus_t infiniopCreateFlashAttentionBackwardDescriptor( | ||
| infiniopHandle_t handle, | ||
| infiniopFlashAttentionBackwardDescriptor_t *desc_ptr, | ||
| infiniopTensorDescriptor_t grad_q_desc, | ||
| infiniopTensorDescriptor_t grad_k_desc, | ||
| infiniopTensorDescriptor_t grad_v_desc, | ||
| infiniopTensorDescriptor_t q_desc, | ||
| infiniopTensorDescriptor_t k_desc, | ||
| infiniopTensorDescriptor_t v_desc, | ||
| infiniopTensorDescriptor_t grad_out_desc, | ||
| infiniopTensorDescriptor_t mask_desc, | ||
| infiniopAttentionMaskType_t mask_type); | ||
|
|
||
| __C __export infiniStatus_t infiniopGetFlashAttentionBackwardWorkspaceSize( | ||
| infiniopFlashAttentionBackwardDescriptor_t desc, | ||
| size_t *size); | ||
|
|
||
| __C __export infiniStatus_t infiniopFlashAttentionBackward( | ||
| infiniopFlashAttentionBackwardDescriptor_t desc, | ||
| void *workspace, | ||
| size_t workspace_size, | ||
| void *grad_q, | ||
| void *grad_k, | ||
| void *grad_v, | ||
| const void *q, | ||
| const void *k, | ||
| const void *v, | ||
| const void *grad_out, | ||
| const void *mask, | ||
| void *stream); | ||
|
|
||
| __C __export infiniStatus_t infiniopDestroyFlashAttentionBackwardDescriptor( | ||
| infiniopFlashAttentionBackwardDescriptor_t desc); | ||
|
|
||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| #include "ops.hpp" | ||
| #include "utils.hpp" | ||
| #include <infinirt.h> | ||
| #include <iomanip> | ||
| #include <iostream> | ||
|
|
||
| namespace infiniop_test::flash_attention { | ||
| struct Test::Attributes { | ||
| int mask_type; | ||
| std::shared_ptr<Tensor> q; | ||
| std::shared_ptr<Tensor> k; | ||
| std::shared_ptr<Tensor> v; | ||
| std::shared_ptr<Tensor> mask; | ||
| std::shared_ptr<Tensor> out; | ||
| std::shared_ptr<Tensor> l; | ||
| std::shared_ptr<Tensor> ans; | ||
| }; | ||
|
|
||
| std::shared_ptr<Test> Test::build( | ||
| std::unordered_map<std::string, std::vector<uint8_t>> attributes, | ||
| std::unordered_map<std::string, std::shared_ptr<Tensor>> tensors, | ||
| double rtol, double atol) { | ||
|
|
||
| auto test = std::shared_ptr<Test>(new Test(rtol, atol)); | ||
| test->_attributes = new Attributes(); | ||
|
|
||
| if (attributes.find("mask_type") == attributes.end() | ||
| || tensors.find("q") == tensors.end() | ||
| || tensors.find("k") == tensors.end() | ||
| || tensors.find("v") == tensors.end() | ||
| || tensors.find("out") == tensors.end() | ||
| || tensors.find("l") == tensors.end() | ||
| || tensors.find("ans") == tensors.end()) { | ||
| throw std::runtime_error("Invalid Test: Missing attributes or tensors"); | ||
| } | ||
|
|
||
| if (tensors.find("mask") == tensors.end()) { | ||
| test->_attributes->mask = nullptr; | ||
| } else { | ||
| test->_attributes->mask = tensors["mask"]; | ||
| } | ||
|
|
||
| test->_attributes->mask_type = *reinterpret_cast<int *>(attributes["mask_type"].data()); | ||
|
|
||
| test->_attributes->q = tensors["q"]; | ||
| test->_attributes->k = tensors["k"]; | ||
| test->_attributes->v = tensors["v"]; | ||
| test->_attributes->out = tensors["out"]; | ||
| test->_attributes->l = tensors["l"]; | ||
| test->_attributes->ans = tensors["ans"]; | ||
|
|
||
| return test; | ||
| } | ||
|
|
||
| std::shared_ptr<infiniop_test::Result> Test::run( | ||
| infiniopHandle_t handle, infiniDevice_t device, int device_id, | ||
| size_t warm_ups, size_t iterations) { | ||
|
|
||
| infiniopFlashAttentionDescriptor_t op_desc; | ||
| infiniopAttentionMaskType_t mask_type = static_cast<infiniopAttentionMaskType_t>(_attributes->mask_type); | ||
| CHECK_OR(infiniopCreateFlashAttentionDescriptor( | ||
| handle, &op_desc, | ||
| _attributes->out->desc(), | ||
| _attributes->l->desc(), | ||
| _attributes->q->desc(), | ||
| _attributes->k->desc(), | ||
| _attributes->v->desc(), | ||
| _attributes->mask->desc(), | ||
| mask_type), | ||
| return TEST_FAILED(OP_CREATION_FAILED, "Failed to create FlashAttention descriptor")); | ||
|
|
||
| auto out = _attributes->out->to(device, device_id); | ||
| auto l = _attributes->l->to(device, device_id); | ||
| auto q = _attributes->q->to(device, device_id); | ||
| auto k = _attributes->k->to(device, device_id); | ||
| auto v = _attributes->v->to(device, device_id); | ||
| auto mask = _attributes->mask ? _attributes->mask->to(device, device_id) : nullptr; | ||
|
|
||
| size_t workspace_size; | ||
| CHECK_OR(infiniopGetFlashAttentionWorkspaceSize(op_desc, &workspace_size), | ||
| return TEST_FAILED(OP_CREATION_FAILED, "Failed to get workspace size")); | ||
| void *workspace = nullptr; | ||
| if (workspace_size > 0) { | ||
| CHECK_OR(infinirtMalloc(&workspace, workspace_size), | ||
| return TEST_FAILED(OP_CREATION_FAILED, "Failed to allocate workspace")); | ||
| } | ||
|
|
||
| CHECK_OR(infiniopFlashAttention(op_desc, | ||
| workspace, workspace_size, | ||
| out->data(), | ||
| l->data(), | ||
| q->data(), | ||
| k->data(), | ||
| v->data(), | ||
| mask ? mask->data() : nullptr, | ||
| nullptr), | ||
| return TEST_FAILED(OP_EXECUTION_FAILED, "FlashAttention execution failed")); | ||
|
|
||
| try { | ||
| allClose(out, _attributes->ans, _rtol, _atol); | ||
| } catch (const std::exception &e) { | ||
| return TEST_FAILED(RESULT_INCORRECT, e.what()); | ||
| } | ||
|
|
||
| double elapsed_time = 0; | ||
|
|
||
| elapsed_time = benchmark( | ||
| [=]() { | ||
| infiniopFlashAttention(op_desc, | ||
| workspace, workspace_size, | ||
| out->data(), | ||
| l->data(), | ||
| q->data(), | ||
| k->data(), | ||
| v->data(), | ||
| mask ? mask->data() : nullptr, | ||
| nullptr); | ||
| }, | ||
| warm_ups, iterations); | ||
|
|
||
| if (workspace != nullptr) { | ||
| infinirtFree(workspace); | ||
| } | ||
|
|
||
| return TEST_PASSED(elapsed_time); | ||
| } | ||
|
|
||
| std::vector<std::string> Test::attribute_names() { | ||
| return {"mask_type"}; | ||
| } | ||
|
|
||
| std::vector<std::string> Test::tensor_names() { | ||
| return {"q", "k", "v", "mask", "out", "l", "ans"}; | ||
| } | ||
|
|
||
| std::vector<std::string> Test::output_names() { | ||
| return {"out", "l"}; | ||
| } | ||
|
|
||
| std::string Test::toString() const { | ||
| std::ostringstream oss; | ||
| oss << op_name() << std::endl; | ||
| oss << "- masktype=" << static_cast<infiniopAttentionMaskType_t>(_attributes->mask_type) << std::endl; | ||
| oss << "- q: " << _attributes->q->info() << std::endl; | ||
| oss << "- k: " << _attributes->k->info() << std::endl; | ||
| oss << "- v: " << _attributes->v->info() << std::endl; | ||
| oss << "- mask: " << (_attributes->mask ? _attributes->mask->info() : "none") << std::endl; | ||
| oss << "- out: " << _attributes->out->info() << std::endl; | ||
| oss << std::scientific << std::setprecision(2); | ||
| oss << "- rtol=" << _rtol << ", atol=" << _atol << std::endl; | ||
| return oss.str(); | ||
| } | ||
|
|
||
| Test::~Test() { | ||
| delete _attributes; | ||
| } | ||
|
|
||
| } // namespace infiniop_test::flash_attention |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.