|
| 1 | +#include "ops.hpp" |
| 2 | +#include "utils.hpp" |
| 3 | +#include <infinirt.h> |
| 4 | +#include <iomanip> |
| 5 | +#include <iostream> |
| 6 | + |
| 7 | +namespace infiniop_test::flash_attention { |
| 8 | +struct Test::Attributes { |
| 9 | + int mask_type; |
| 10 | + std::shared_ptr<Tensor> q; |
| 11 | + std::shared_ptr<Tensor> k; |
| 12 | + std::shared_ptr<Tensor> v; |
| 13 | + std::shared_ptr<Tensor> mask; |
| 14 | + std::shared_ptr<Tensor> out; |
| 15 | + std::shared_ptr<Tensor> l; |
| 16 | + std::shared_ptr<Tensor> ans; |
| 17 | +}; |
| 18 | + |
| 19 | +std::shared_ptr<Test> Test::build( |
| 20 | + std::unordered_map<std::string, std::vector<uint8_t>> attributes, |
| 21 | + std::unordered_map<std::string, std::shared_ptr<Tensor>> tensors, |
| 22 | + double rtol, double atol) { |
| 23 | + |
| 24 | + auto test = std::shared_ptr<Test>(new Test(rtol, atol)); |
| 25 | + test->_attributes = new Attributes(); |
| 26 | + |
| 27 | + if (attributes.find("mask_type") == attributes.end() |
| 28 | + || tensors.find("q") == tensors.end() |
| 29 | + || tensors.find("k") == tensors.end() |
| 30 | + || tensors.find("v") == tensors.end() |
| 31 | + || tensors.find("out") == tensors.end() |
| 32 | + || tensors.find("l") == tensors.end() |
| 33 | + || tensors.find("ans") == tensors.end()) { |
| 34 | + throw std::runtime_error("Invalid Test: Missing attributes or tensors"); |
| 35 | + } |
| 36 | + |
| 37 | + if (tensors.find("mask") == tensors.end()) { |
| 38 | + test->_attributes->mask = nullptr; |
| 39 | + } else { |
| 40 | + test->_attributes->mask = tensors["mask"]; |
| 41 | + } |
| 42 | + |
| 43 | + test->_attributes->mask_type = *reinterpret_cast<int *>(attributes["mask_type"].data()); |
| 44 | + |
| 45 | + test->_attributes->q = tensors["q"]; |
| 46 | + test->_attributes->k = tensors["k"]; |
| 47 | + test->_attributes->v = tensors["v"]; |
| 48 | + test->_attributes->out = tensors["out"]; |
| 49 | + test->_attributes->l = tensors["l"]; |
| 50 | + test->_attributes->ans = tensors["ans"]; |
| 51 | + |
| 52 | + return test; |
| 53 | +} |
| 54 | + |
| 55 | +std::shared_ptr<infiniop_test::Result> Test::run( |
| 56 | + infiniopHandle_t handle, infiniDevice_t device, int device_id, |
| 57 | + size_t warm_ups, size_t iterations) { |
| 58 | + |
| 59 | + infiniopFlashAttentionDescriptor_t op_desc; |
| 60 | + infiniopAttentionMaskType_t mask_type = static_cast<infiniopAttentionMaskType_t>(_attributes->mask_type); |
| 61 | + CHECK_OR(infiniopCreateFlashAttentionDescriptor( |
| 62 | + handle, &op_desc, |
| 63 | + _attributes->out->desc(), |
| 64 | + _attributes->l->desc(), |
| 65 | + _attributes->q->desc(), |
| 66 | + _attributes->k->desc(), |
| 67 | + _attributes->v->desc(), |
| 68 | + _attributes->mask->desc(), |
| 69 | + mask_type), |
| 70 | + return TEST_FAILED(OP_CREATION_FAILED, "Failed to create FlashAttention descriptor")); |
| 71 | + |
| 72 | + auto out = _attributes->out->to(device, device_id); |
| 73 | + auto l = _attributes->l->to(device, device_id); |
| 74 | + auto q = _attributes->q->to(device, device_id); |
| 75 | + auto k = _attributes->k->to(device, device_id); |
| 76 | + auto v = _attributes->v->to(device, device_id); |
| 77 | + auto mask = _attributes->mask ? _attributes->mask->to(device, device_id) : nullptr; |
| 78 | + |
| 79 | + size_t workspace_size; |
| 80 | + CHECK_OR(infiniopGetFlashAttentionWorkspaceSize(op_desc, &workspace_size), |
| 81 | + return TEST_FAILED(OP_CREATION_FAILED, "Failed to get workspace size")); |
| 82 | + void *workspace = nullptr; |
| 83 | + if (workspace_size > 0) { |
| 84 | + CHECK_OR(infinirtMalloc(&workspace, workspace_size), |
| 85 | + return TEST_FAILED(OP_CREATION_FAILED, "Failed to allocate workspace")); |
| 86 | + } |
| 87 | + |
| 88 | + CHECK_OR(infiniopFlashAttention(op_desc, |
| 89 | + workspace, workspace_size, |
| 90 | + out->data(), |
| 91 | + l->data(), |
| 92 | + q->data(), |
| 93 | + k->data(), |
| 94 | + v->data(), |
| 95 | + mask ? mask->data() : nullptr, |
| 96 | + nullptr), |
| 97 | + return TEST_FAILED(OP_EXECUTION_FAILED, "FlashAttention execution failed")); |
| 98 | + |
| 99 | + try { |
| 100 | + allClose(out, _attributes->ans, _rtol, _atol); |
| 101 | + } catch (const std::exception &e) { |
| 102 | + return TEST_FAILED(RESULT_INCORRECT, e.what()); |
| 103 | + } |
| 104 | + |
| 105 | + double elapsed_time = 0; |
| 106 | + |
| 107 | + elapsed_time = benchmark( |
| 108 | + [=]() { |
| 109 | + infiniopFlashAttention(op_desc, |
| 110 | + workspace, workspace_size, |
| 111 | + out->data(), |
| 112 | + l->data(), |
| 113 | + q->data(), |
| 114 | + k->data(), |
| 115 | + v->data(), |
| 116 | + mask ? mask->data() : nullptr, |
| 117 | + nullptr); |
| 118 | + }, |
| 119 | + warm_ups, iterations); |
| 120 | + |
| 121 | + if (workspace != nullptr) { |
| 122 | + infinirtFree(workspace); |
| 123 | + } |
| 124 | + |
| 125 | + return TEST_PASSED(elapsed_time); |
| 126 | +} |
| 127 | + |
| 128 | +std::vector<std::string> Test::attribute_names() { |
| 129 | + return {"mask_type"}; |
| 130 | +} |
| 131 | + |
| 132 | +std::vector<std::string> Test::tensor_names() { |
| 133 | + return {"q", "k", "v", "mask", "out", "l", "ans"}; |
| 134 | +} |
| 135 | + |
| 136 | +std::vector<std::string> Test::output_names() { |
| 137 | + return {"out", "l"}; |
| 138 | +} |
| 139 | + |
| 140 | +std::string Test::toString() const { |
| 141 | + std::ostringstream oss; |
| 142 | + oss << op_name() << std::endl; |
| 143 | + oss << "- masktype=" << static_cast<infiniopAttentionMaskType_t>(_attributes->mask_type) << std::endl; |
| 144 | + oss << "- q: " << _attributes->q->info() << std::endl; |
| 145 | + oss << "- k: " << _attributes->k->info() << std::endl; |
| 146 | + oss << "- v: " << _attributes->v->info() << std::endl; |
| 147 | + oss << "- mask: " << (_attributes->mask ? _attributes->mask->info() : "none") << std::endl; |
| 148 | + oss << "- out: " << _attributes->out->info() << std::endl; |
| 149 | + oss << std::scientific << std::setprecision(2); |
| 150 | + oss << "- rtol=" << _rtol << ", atol=" << _atol << std::endl; |
| 151 | + return oss.str(); |
| 152 | +} |
| 153 | + |
| 154 | +Test::~Test() { |
| 155 | + delete _attributes; |
| 156 | +} |
| 157 | + |
| 158 | +} // namespace infiniop_test::flash_attention |
0 commit comments