|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// This source code is licensed under the BSD-style license found in the |
| 4 | +// LICENSE file in the root directory of this source tree. |
| 5 | + |
| 6 | +#include <algorithm> |
| 7 | +#include <array> |
| 8 | +#include <cstddef> |
| 9 | +#include <cstdint> |
| 10 | +#include <functional> |
| 11 | +#include <iostream> |
| 12 | +#include <limits> |
| 13 | +#include <random> |
| 14 | +#include <vector> |
| 15 | + |
| 16 | +#include "bench/subgraph/models.h" |
| 17 | +#include "include/xnnpack.h" |
| 18 | + |
| 19 | +// align a size up to XNN_EXTRA_BYTES |
| 20 | +#define XNN_PAD_EXTRA_BYTES(s, t) \ |
| 21 | + (((s) + XNN_EXTRA_BYTES / sizeof(t) - 1) & ~(XNN_EXTRA_BYTES / sizeof(t) - 1)) |
| 22 | + |
| 23 | +namespace models { |
| 24 | + |
| 25 | +xnn_subgraph_t FP32FullyConnected(size_t batch_size, size_t input_channels, |
| 26 | + size_t output_channels) { |
| 27 | + xnn_status status; |
| 28 | + xnn_subgraph_t subgraph = nullptr; |
| 29 | + status = xnn_create_subgraph(/*num_external_values=*/2, 0, &subgraph); |
| 30 | + if (status != xnn_status_success) { |
| 31 | + std::cerr << "failed to create subgrpah" << std::endl; |
| 32 | + return nullptr; |
| 33 | + } |
| 34 | + |
| 35 | + std::random_device random_device; // NOLINT(runtime/random_device) |
| 36 | + auto rng = std::mt19937(random_device()); |
| 37 | + |
| 38 | + uint32_t v0 = XNN_INVALID_VALUE_ID; |
| 39 | + std::array<size_t, 2> v0_dims = {{batch_size, input_channels}}; |
| 40 | + status = xnn_define_tensor_value( |
| 41 | + subgraph, xnn_datatype_fp32, v0_dims.size(), v0_dims.data(), |
| 42 | + /*data=*/nullptr, 0, XNN_VALUE_FLAG_EXTERNAL_INPUT, &v0); |
| 43 | + if (status != xnn_status_success) { |
| 44 | + std::cerr << "failed to create tensor v0" << std::endl; |
| 45 | + return nullptr; |
| 46 | + } |
| 47 | + |
| 48 | + uint32_t v38 = XNN_INVALID_VALUE_ID; |
| 49 | + std::array<size_t, 2> v38_dims = {{batch_size, output_channels}}; |
| 50 | + status = xnn_define_tensor_value( |
| 51 | + subgraph, xnn_datatype_fp32, v38_dims.size(), v38_dims.data(), |
| 52 | + /*data=*/nullptr, 1, XNN_VALUE_FLAG_EXTERNAL_OUTPUT, &v38); |
| 53 | + if (status != xnn_status_success) { |
| 54 | + std::cerr << "failed to create tensor v38" << std::endl; |
| 55 | + return nullptr; |
| 56 | + } |
| 57 | + |
| 58 | + static std::vector<float> w42_data; |
| 59 | + w42_data.resize(XNN_PAD_EXTRA_BYTES(input_channels * output_channels, float)); |
| 60 | + uint32_t w42 = XNN_INVALID_VALUE_ID; |
| 61 | + std::array<size_t, 2> w42_dims = {{output_channels, input_channels}}; |
| 62 | + status = xnn_define_tensor_value( |
| 63 | + subgraph, xnn_datatype_fp32, w42_dims.size(), w42_dims.data(), |
| 64 | + /*data=*/w42_data.data(), XNN_INVALID_VALUE_ID, /*flags=*/0, &w42); |
| 65 | + if (status != xnn_status_success) { |
| 66 | + std::cerr << "failed to create tensor w42" << std::endl; |
| 67 | + return nullptr; |
| 68 | + } |
| 69 | + |
| 70 | + auto f32rng = std::bind(std::uniform_real_distribution<float>(-1.0f, +1.0f), |
| 71 | + std::ref(rng)); |
| 72 | + std::generate(w42_data.begin(), w42_data.end(), std::ref(f32rng)); |
| 73 | + |
| 74 | + status = xnn_define_fully_connected( |
| 75 | + subgraph, |
| 76 | + /*output_min=*/-std::numeric_limits<float>::infinity(), |
| 77 | + /*output_max=*/std::numeric_limits<float>::infinity(), |
| 78 | + /*input_id=*/v0, |
| 79 | + /*filter_id=*/w42, |
| 80 | + /*bias_id=*/XNN_INVALID_VALUE_ID, |
| 81 | + /*output_id=*/v38, |
| 82 | + /*flags=*/0); |
| 83 | + if (status != xnn_status_success) { |
| 84 | + std::cerr << "failed to create node #6" << std::endl; |
| 85 | + return nullptr; |
| 86 | + } |
| 87 | + |
| 88 | + return subgraph; |
| 89 | +} // NOLINT(readability/fn_size) |
| 90 | + |
| 91 | +xnn_subgraph_t QD8FullyConnected(size_t batch_size, size_t input_channels, |
| 92 | + size_t output_channels) { |
| 93 | + xnn_status status; |
| 94 | + xnn_subgraph_t subgraph = nullptr; |
| 95 | + status = xnn_create_subgraph(/*num_external_values=*/2, 0, &subgraph); |
| 96 | + if (status != xnn_status_success) { |
| 97 | + std::cerr << "failed to create subgrpah" << std::endl; |
| 98 | + return nullptr; |
| 99 | + } |
| 100 | + |
| 101 | + std::random_device random_device; // NOLINT(runtime/random_device) |
| 102 | + auto rng = std::mt19937(random_device()); |
| 103 | + |
| 104 | + uint32_t v0 = XNN_INVALID_VALUE_ID; |
| 105 | + std::array<size_t, 2> v0_dims = {{batch_size, input_channels}}; |
| 106 | + status = xnn_define_tensor_value( |
| 107 | + subgraph, xnn_datatype_fp32, v0_dims.size(), v0_dims.data(), |
| 108 | + /*data=*/nullptr, 0, XNN_VALUE_FLAG_EXTERNAL_INPUT, &v0); |
| 109 | + if (status != xnn_status_success) { |
| 110 | + std::cerr << "failed to create tensor v0" << std::endl; |
| 111 | + return nullptr; |
| 112 | + } |
| 113 | + |
| 114 | + uint32_t v1 = XNN_INVALID_VALUE_ID; |
| 115 | + std::array<size_t, 2> v1_dims = {{batch_size, input_channels}}; |
| 116 | + status = xnn_define_dynamically_quantized_tensor_value( |
| 117 | + subgraph, xnn_datatype_qdint8, /*num_dims=*/v1_dims.size(), |
| 118 | + /*num_non_batch_dims=*/1, /*dims=*/v1_dims.data(), |
| 119 | + /*external_id=*/XNN_INVALID_VALUE_ID, |
| 120 | + /*flags=*/0, &v1); |
| 121 | + if (status != xnn_status_success) { |
| 122 | + std::cerr << "failed to create tensor v1" << std::endl; |
| 123 | + return nullptr; |
| 124 | + } |
| 125 | + |
| 126 | + uint32_t v38 = XNN_INVALID_VALUE_ID; |
| 127 | + std::array<size_t, 2> v38_dims = {{batch_size, output_channels}}; |
| 128 | + status = xnn_define_tensor_value( |
| 129 | + subgraph, xnn_datatype_fp32, v38_dims.size(), v38_dims.data(), |
| 130 | + /*data=*/nullptr, 1, XNN_VALUE_FLAG_EXTERNAL_OUTPUT, &v38); |
| 131 | + if (status != xnn_status_success) { |
| 132 | + std::cerr << "failed to create tensor v38" << std::endl; |
| 133 | + return nullptr; |
| 134 | + } |
| 135 | + |
| 136 | + static std::vector<int8_t> w42_data; |
| 137 | + w42_data.resize( |
| 138 | + XNN_PAD_EXTRA_BYTES(input_channels * output_channels, int8_t)); |
| 139 | + uint32_t w42 = XNN_INVALID_VALUE_ID; |
| 140 | + std::array<size_t, 2> w42_dims = {{output_channels, input_channels}}; |
| 141 | + static std::vector<float> w42_scale; |
| 142 | + w42_scale.resize(output_channels); |
| 143 | + { |
| 144 | + auto scalerng = std::bind( |
| 145 | + std::uniform_real_distribution<float>(0.01f, 1.0f), std::ref(rng)); |
| 146 | + std::generate(w42_scale.begin(), w42_scale.end(), std::ref(scalerng)); |
| 147 | + } |
| 148 | + status = xnn_define_channelwise_quantized_tensor_value( |
| 149 | + subgraph, xnn_datatype_qcint8, |
| 150 | + /*scale=*/w42_scale.data(), w42_dims.size(), 0, w42_dims.data(), |
| 151 | + /*data=*/w42_data.data(), XNN_INVALID_VALUE_ID, /*flags=*/0, &w42); |
| 152 | + if (status != xnn_status_success) { |
| 153 | + std::cerr << "failed to create tensor w42" << std::endl; |
| 154 | + return nullptr; |
| 155 | + } |
| 156 | + |
| 157 | + auto qc8rng = std::bind( |
| 158 | + std::uniform_int_distribution<int>(std::numeric_limits<int8_t>::min(), |
| 159 | + std::numeric_limits<int8_t>::max()), |
| 160 | + std::ref(rng)); |
| 161 | + std::generate(w42_data.begin(), w42_data.end(), std::ref(qc8rng)); |
| 162 | + |
| 163 | + status = xnn_define_unary(subgraph, xnn_unary_convert, /*params=*/nullptr, |
| 164 | + /*input_id=*/v0, /*output_id=*/v1, |
| 165 | + /*flags=*/0); |
| 166 | + if (status != xnn_status_success) { |
| 167 | + std::cerr << "failed to create create convert " << std::endl; |
| 168 | + return nullptr; |
| 169 | + } |
| 170 | + |
| 171 | + status = xnn_define_fully_connected( |
| 172 | + subgraph, |
| 173 | + /*output_min=*/-std::numeric_limits<float>::infinity(), |
| 174 | + /*output_max=*/std::numeric_limits<float>::infinity(), |
| 175 | + /*input_id=*/v1, |
| 176 | + /*filter_id=*/w42, |
| 177 | + /*bias_id=*/XNN_INVALID_VALUE_ID, |
| 178 | + /*output_id=*/v38, |
| 179 | + /*flags=*/0); |
| 180 | + if (status != xnn_status_success) { |
| 181 | + std::cerr << "failed to create node #6" << std::endl; |
| 182 | + return nullptr; |
| 183 | + } |
| 184 | + |
| 185 | + return subgraph; |
| 186 | +} // NOLINT(readability/fn_size) |
| 187 | + |
| 188 | +} // namespace models |
0 commit comments