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