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