test/nextgen/format/plain_format.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // | ||
| 2 | // SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates <open-source-office@arm.com> | ||
| 3 | // | ||
| 4 | // SPDX-License-Identifier: Apache-2.0 | ||
| 5 | // | ||
| 6 | |||
| 7 | #include "test/nextgen/format/plain_format.hpp" | ||
| 8 | |||
| 9 | #include <algorithm> | ||
| 10 | #include <cstddef> | ||
| 11 | #include <cstdint> | ||
| 12 | #include <functional> | ||
| 13 | #include <numeric> | ||
| 14 | #include <ostream> | ||
| 15 | #include <random> | ||
| 16 | |||
| 17 | #include "test/common/assert.hpp" | ||
| 18 | #include "test/common/buffer.hpp" | ||
| 19 | #include "test/common/compare.hpp" | ||
| 20 | #include "test/common/data_type.hpp" | ||
| 21 | #include "test/common/round.hpp" | ||
| 22 | #include "test/common/span.hpp" | ||
| 23 | #include "test/nextgen/common/random.hpp" | ||
| 24 | #include "test/nextgen/format/format.hpp" | ||
| 25 | #include "test/nextgen/reference/compare.hpp" | ||
| 26 | #include "test/nextgen/reference/print.hpp" | ||
| 27 | #include "test/reference/fill.hpp" | ||
| 28 | |||
| 29 | namespace kai::test { | ||
| 30 | |||
| 31 | 3000 | size_t PlainFormat::compute_offset(Span<const size_t> shape, Span<const size_t> indices) const { | |
| 32 |
1/4✓ Branch 0 taken 3000 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
3000 | KAI_TEST_ASSERT(shape.size() > 0); |
| 33 |
1/4✓ Branch 0 taken 3000 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
3000 | KAI_TEST_ASSERT(shape.size() == indices.size()); |
| 34 | |||
| 35 | 3000 | const size_t num_dims = shape.size(); | |
| 36 | 3000 | size_t stride = round_up_division(shape.at(num_dims - 1) * data_type_size_in_bits(m_dtype), 8); | |
| 37 | 3000 | size_t offset = indices.at(num_dims - 1) * data_type_size_in_bits(m_dtype) / 8; | |
| 38 |
1/4✓ Branch 0 taken 3000 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
3000 | KAI_TEST_ASSERT(indices.at(num_dims - 1) * data_type_size_in_bits(m_dtype) % 8 == 0); |
| 39 | |||
| 40 |
2/2✓ Branch 0 taken 1200 times.
✓ Branch 1 taken 1800 times.
|
3000 | if (num_dims > 1) { |
| 41 | 1800 | size_t dim = num_dims - 2; | |
| 42 | |||
| 43 | 1800 | while (true) { | |
| 44 | 1800 | offset += indices.at(dim) * stride; | |
| 45 | 1800 | stride *= shape.at(dim); | |
| 46 | |||
| 47 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1800 times.
|
1800 | if (dim == 0) { |
| 48 | 1800 | break; | |
| 49 | } | ||
| 50 | |||
| 51 | ✗ | --dim; | |
| 52 | } | ||
| 53 | 1800 | } | |
| 54 | |||
| 55 | 6000 | return offset; | |
| 56 | 3000 | } | |
| 57 | |||
| 58 | 6255 | size_t PlainFormat::compute_size(Span<const size_t> shape) const { | |
| 59 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6255 times.
|
6255 | if (shape.empty()) { |
| 60 | ✗ | return 0; | |
| 61 | } | ||
| 62 | |||
| 63 | 6255 | const size_t row_size = round_up_division(shape.at(shape.size() - 1) * data_type_size_in_bits(m_dtype), 8); | |
| 64 | 6255 | const size_t num_rows = std::accumulate(shape.begin(), shape.end() - 1, 1, std::multiplies<>()); | |
| 65 | 6255 | const size_t size = row_size * num_rows; | |
| 66 | |||
| 67 | 6255 | return size; | |
| 68 | 6255 | } | |
| 69 | |||
| 70 | 600 | Buffer PlainFormat::generate_random(Span<const size_t> shape, Rng& rng) const { | |
| 71 | 600 | const size_t len = compute_size(shape) * 8 / data_type_size_in_bits(m_dtype); | |
| 72 | 1200 | const uint32_t seed = | |
| 73 | 600 | std::uniform_int_distribution<uint32_t>()(rng); // REVISIT: Use the random number generator directly. | |
| 74 | |||
| 75 |
1/2✓ Branch 0 taken 600 times.
✗ Branch 1 not taken.
|
600 | switch (m_dtype) { |
| 76 | case DataType::FP32: | ||
| 77 | |||
| 78 | 600 | return fill_random<float>(len, seed); | |
| 79 | |||
| 80 | default: | ||
| 81 | ✗ | KAI_TEST_ERROR("Not supported!"); | |
| 82 | } | ||
| 83 | 600 | } | |
| 84 | |||
| 85 | ✗ | Buffer PlainFormat::pack(Span<const size_t> shape, Span<const Span<const std::byte>> buffers) const { | |
| 86 | ✗ | KAI_TEST_ASSERT_MSG(buffers.size() == 1, "Plain format only has 1 data component."); | |
| 87 | |||
| 88 | ✗ | const Span<const std::byte> data = buffers.at(0); | |
| 89 | ✗ | const size_t size = compute_size(shape); | |
| 90 | ✗ | KAI_TEST_ASSERT_MSG(data.size() == size, "The data buffer must have the right size."); | |
| 91 | |||
| 92 | ✗ | Buffer packed_buffer(size); | |
| 93 | ✗ | std::copy(data.begin(), data.end(), packed_buffer.data()); | |
| 94 | |||
| 95 | ✗ | return packed_buffer; | |
| 96 | ✗ | } | |
| 97 | |||
| 98 | 600 | bool PlainFormat::compare( | |
| 99 | Span<const size_t> shape, Span<const size_t> tile_coords, Span<const size_t> tile_shape, | ||
| 100 | Span<const std::byte> imp_buffer, Span<const std::byte> ref_buffer, MismatchHandler& handler) const { | ||
| 101 |
1/4✓ Branch 0 taken 600 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
600 | KAI_TEST_ASSERT_MSG(shape.size() == 2, "Only 2D array is supported."); |
| 102 | |||
| 103 | 600 | const CompareFn compare_fn = make_compare_plain_2d(m_dtype); | |
| 104 |
6/12✓ Branch 0 taken 600 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 600 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 600 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 600 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 600 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 600 times.
✗ Branch 11 not taken.
|
3600 | const size_t num_checks = compare_fn( |
| 105 | 3000 | shape, tile_coords, tile_shape, imp_buffer, ref_buffer, | |
| 106 | 600 | [](std::ostream& os, Span<const size_t> indices) { | |
| 107 | ✗ | os << "Mismatch at row " << indices.at(0) << ", col " << indices.at(1); | |
| 108 | ✗ | }, | |
| 109 | 600 | handler); | |
| 110 | |||
| 111 | 1200 | return handler.success(num_checks); | |
| 112 | 600 | } | |
| 113 | |||
| 114 | ✗ | void PlainFormat::print(std::ostream& os, Span<const size_t> shape, Span<const std::byte> data) const { | |
| 115 | ✗ | if (shape.empty()) { | |
| 116 | ✗ | os << "None"; | |
| 117 | ✗ | } else { | |
| 118 | ✗ | const PrintFn print_fn = make_print_array(m_dtype); | |
| 119 | ✗ | print_fn(os, shape, data, 0); | |
| 120 | ✗ | } | |
| 121 | ✗ | } | |
| 122 | |||
| 123 | 1945 | bool PlainFormat::operator==(const Format& other) const { | |
| 124 |
1/4✓ Branch 0 taken 1945 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
1945 | const auto* rhs = dynamic_cast<const PlainFormat*>(&other); |
| 125 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1945 times.
|
1945 | return rhs != nullptr && m_dtype == rhs->m_dtype; |
| 126 | 1945 | } | |
| 127 | |||
| 128 | } // namespace kai::test | ||
| 129 |