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