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