|
| 1 | +// Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +// All rights reserved. |
| 3 | +// |
| 4 | +// This source code is licensed under the BSD-style license found in the |
| 5 | +// LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +#include <executorch/backends/vulkan/runtime/graph/ops/impl/Common.h> |
| 8 | +#include <executorch/backends/vulkan/runtime/graph/ops/utils/ShaderNameUtils.h> |
| 9 | +#include <iostream> |
| 10 | +#include <vector> |
| 11 | +#include "utils.h" |
| 12 | + |
| 13 | +using namespace executorch::vulkan::prototyping; |
| 14 | + |
| 15 | +// Generate test cases for add operation |
| 16 | +std::vector<TestCase> generate_add_test_cases() { |
| 17 | + std::vector<TestCase> test_cases; |
| 18 | + |
| 19 | + // Set the data generation type as a local variable |
| 20 | + DataGenType data_gen_type = DataGenType::ONES; |
| 21 | + |
| 22 | + // Define different input size configurations |
| 23 | + std::vector<std::vector<int64_t>> size_configs = { |
| 24 | + {1, 64, 64}, // Small square |
| 25 | + {1, 128, 128}, // Medium square |
| 26 | + {1, 256, 256}, // Large square |
| 27 | + {1, 512, 512}, // Very large square |
| 28 | + {1, 1, 1024}, // Wide tensor |
| 29 | + {1, 1024, 1}, // Tall tensor |
| 30 | + {32, 32, 32}, // 3D cube |
| 31 | + {16, 128, 64}, // 3D rectangular |
| 32 | + }; |
| 33 | + |
| 34 | + // Storage types to test |
| 35 | + std::vector<utils::StorageType> storage_types = { |
| 36 | + utils::kTexture3D, utils::kBuffer}; |
| 37 | + |
| 38 | + // Data types to test |
| 39 | + std::vector<vkapi::ScalarType> data_types = {vkapi::kFloat, vkapi::kHalf}; |
| 40 | + |
| 41 | + // Generate test cases for each combination |
| 42 | + for (const auto& sizes : size_configs) { |
| 43 | + for (const auto& storage_type : storage_types) { |
| 44 | + for (const auto& data_type : data_types) { |
| 45 | + TestCase test_case; |
| 46 | + |
| 47 | + // Create a descriptive name for the test case |
| 48 | + std::string size_str = ""; |
| 49 | + for (size_t i = 0; i < sizes.size(); ++i) { |
| 50 | + size_str += std::to_string(sizes[i]); |
| 51 | + if (i < sizes.size() - 1) |
| 52 | + size_str += "x"; |
| 53 | + } |
| 54 | + |
| 55 | + std::string storage_str = |
| 56 | + (storage_type == utils::kTexture3D) ? "Texture3D" : "Buffer"; |
| 57 | + std::string dtype_str = (data_type == vkapi::kFloat) ? "Float" : "Half"; |
| 58 | + |
| 59 | + // Add data generation type to the name for clarity |
| 60 | + std::string test_name = |
| 61 | + "Add_" + size_str + "_" + storage_str + "_" + dtype_str; |
| 62 | + test_case.set_name(test_name); |
| 63 | + |
| 64 | + // Set the operator name for the test case |
| 65 | + test_case.set_operator_name("etvk.add_prototype"); |
| 66 | + |
| 67 | + // Add two input tensors with the same size, type, storage, and data |
| 68 | + // generation method |
| 69 | + ValueSpec input_a( |
| 70 | + sizes, data_type, storage_type, utils::kWidthPacked, data_gen_type); |
| 71 | + ValueSpec input_b( |
| 72 | + sizes, data_type, storage_type, utils::kWidthPacked, data_gen_type); |
| 73 | + |
| 74 | + // Add output tensor with the same size, type, and storage as inputs |
| 75 | + // (output uses ZEROS by default) |
| 76 | + ValueSpec output( |
| 77 | + sizes, |
| 78 | + data_type, |
| 79 | + storage_type, |
| 80 | + utils::kWidthPacked, |
| 81 | + DataGenType::ZEROS); |
| 82 | + |
| 83 | + test_case.add_input_spec(input_a); |
| 84 | + test_case.add_input_spec(input_b); |
| 85 | + test_case.add_output_spec(output); |
| 86 | + |
| 87 | + test_cases.push_back(test_case); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return test_cases; |
| 93 | +} |
| 94 | + |
| 95 | +// Custom FLOP calculator for add operation |
| 96 | +// Add operation performs 1 FLOP (addition) per element |
| 97 | +int64_t add_flop_calculator(const TestCase& test_case) { |
| 98 | + // Calculate total elements from the first input tensor |
| 99 | + int64_t total_elements = 1; |
| 100 | + if (!test_case.empty() && test_case.num_inputs() > 0 && |
| 101 | + test_case.inputs()[0].is_tensor()) { |
| 102 | + const auto& sizes = test_case.inputs()[0].get_tensor_sizes(); |
| 103 | + for (int64_t size : sizes) { |
| 104 | + total_elements *= size; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + // Add operation: 1 FLOP per element (one addition) |
| 109 | + return total_elements; |
| 110 | +} |
| 111 | + |
| 112 | +// Reference implementation for add operator |
| 113 | +void add_reference_compute(TestCase& test_case) { |
| 114 | + const ValueSpec& input_a = test_case.inputs().at(0); |
| 115 | + const ValueSpec& input_b = test_case.inputs().at(1); |
| 116 | + |
| 117 | + ValueSpec& output = test_case.outputs().at(0); |
| 118 | + |
| 119 | + if (input_a.dtype != vkapi::kFloat) { |
| 120 | + throw std::invalid_argument("Unsupported dtype"); |
| 121 | + } |
| 122 | + |
| 123 | + // Calculate number of elements |
| 124 | + int64_t num_elements = input_a.numel(); |
| 125 | + |
| 126 | + auto& input_a_data = input_a.get_float_data(); |
| 127 | + auto& input_b_data = input_b.get_float_data(); |
| 128 | + |
| 129 | + auto& ref_data = output.get_ref_float_data(); |
| 130 | + ref_data.resize(num_elements); |
| 131 | + for (int64_t i = 0; i < num_elements; ++i) { |
| 132 | + ref_data[i] = input_a_data[i] + input_b_data[i]; |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +int main(int argc, char* argv[]) { |
| 137 | + set_print_output(false); // Disable output tensor printing |
| 138 | + set_print_latencies(false); // Enable latency timing printing |
| 139 | + set_use_gpu_timestamps(true); // Enable GPU timestamps |
| 140 | + |
| 141 | + print_performance_header(); |
| 142 | + std::cout << "Add Operation Prototyping Framework" << std::endl; |
| 143 | + print_separator(); |
| 144 | + |
| 145 | + // Initialize Vulkan context |
| 146 | + try { |
| 147 | + api::context()->initialize_querypool(); |
| 148 | + } catch (const std::exception& e) { |
| 149 | + std::cerr << "Failed to initialize Vulkan context: " << e.what() |
| 150 | + << std::endl; |
| 151 | + return 1; |
| 152 | + } |
| 153 | + |
| 154 | + // Execute test cases using the new framework with custom FLOP calculator and |
| 155 | + // reference compute |
| 156 | + auto results = execute_test_cases( |
| 157 | + generate_add_test_cases, |
| 158 | + add_flop_calculator, |
| 159 | + "Add", |
| 160 | + 3, |
| 161 | + 10, |
| 162 | + add_reference_compute); |
| 163 | + |
| 164 | + return 0; |
| 165 | +} |
0 commit comments