Merged
feat(Operation): Add input tile shape resolver. #5126
高前程Add on Jul 27
feat(Operation): Add input tile shape resolver. #5126
Merged
Merged fromImmutable01/pypto0727_viewtileintocann/pyptomaster
Total: 3 files changed+1114-0
| @@ -0,0 +1,645 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2025 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +/*! | ||
| 12 | + * \file tile_shape_resolver.cpp | ||
| 13 | + * \brief Resolve the per-input / per-output tile shape of an op from the op (opcode + op-level tile shape + shapes). | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +#include "tile_shape_resolver.h" | ||
| 17 | + | ||
| 18 | +#include <algorithm> | ||
| 19 | + | ||
| 20 | +#include "interface/utils/error.h" | ||
| 21 | +#include "interface/operation/opcode.h" | ||
| 22 | +#include "interface/operation/operation_impl.h" | ||
| 23 | +#include "interface/tensor/logical_tensor.h" | ||
| 24 | + | ||
| 25 | +namespace npu::tile_fwk { | ||
| 26 | + | ||
| 27 | +TileShapeResolver& TileShapeResolver::Instance() | ||
| 28 | +{ | ||
| 29 | + static TileShapeResolver inst; | ||
| 30 | + return inst; | ||
| 31 | +} | ||
| 32 | + | ||
| 33 | +namespace { | ||
| 34 | +// Build a TileShape whose VecTile is `vec`, leaving other sub-tiles default. | ||
| 35 | +TileShape MakeTileShapeFromVec(const std::vector<int64_t>& vec) | ||
| 36 | +{ | ||
| 37 | + TileShape tile; | ||
| 38 | + tile.SetVecTile(vec); | ||
| 39 | + return tile; | ||
| 40 | +} | ||
| 41 | + | ||
| 42 | +// Per-axis tile size for an elementwise operand: min(operand shape, op vecTile). | ||
| 43 | +// Axes beyond the op vecTile keep the operand's full shape (not tiled). | ||
| 44 | +std::vector<int64_t> ElementwiseInputVecTile(const std::vector<int64_t>& inShape, const VecTile& opVecTile) | ||
| 45 | +{ | ||
| 46 | + std::vector<int64_t> tile(inShape.size()); | ||
| 47 | + for (size_t axis = 0; axis < inShape.size(); ++axis) { | ||
| 48 | + if (axis < opVecTile.tile.size()) { | ||
| 49 | + tile[axis] = std::min(inShape[axis], opVecTile.tile[axis]); | ||
| 50 | + } else { | ||
| 51 | + tile[axis] = inShape[axis]; | ||
| 52 | + } | ||
| 53 | + } | ||
| 54 | + return tile; | ||
| 55 | +} | ||
| 56 | + | ||
| 57 | +// Like ElementwiseInputVecTile but the `groupAxis` is tiled within its per-group slice | ||
| 58 | +// (curShapeLen = inShape[groupAxis] / group), mirroring InnerTransData/InnerTransDataND. | ||
| 59 | +std::vector<int64_t> TransDataInputVecTile(const std::vector<int64_t>& inShape, const VecTile& opVecTile, int64_t group, | ||
| 60 | + int64_t groupAxis) | ||
| 61 | +{ | ||
| 62 | + if (group <= 0) { | ||
| 63 | + group = 1; | ||
| 64 | + } | ||
| 65 | + std::vector<int64_t> tile(inShape.size()); | ||
| 66 | + for (size_t axis = 0; axis < inShape.size(); ++axis) { | ||
| 67 | + int64_t curShapeLen = (static_cast<int64_t>(axis) == groupAxis) ? inShape[axis] / group : inShape[axis]; | ||
| 68 | + if (axis < opVecTile.tile.size()) { | ||
| 69 | + tile[axis] = std::min(curShapeLen, opVecTile.tile[axis]); | ||
| 70 | + } else { | ||
| 71 | + tile[axis] = curShapeLen; | ||
| 72 | + } | ||
| 73 | + } | ||
| 74 | + return tile; | ||
| 75 | +} | ||
| 76 | + | ||
| 77 | +// Like ElementwiseInputVecTile but `fullAxis` is kept at its full shape (not tiled), | ||
| 78 | +// and per-axis clamping uses `clampShape` instead of `inShape` (some ops clamp an | ||
| 79 | +// input by another operand's shape, e.g. SCATTER clamps self/src by idx.shape). | ||
| 80 | +std::vector<int64_t> VecTileAxisFull(const std::vector<int64_t>& inShape, const std::vector<int64_t>& clampShape, | ||
| 81 | + const VecTile& opVecTile, int64_t fullAxis) | ||
| 82 | +{ | ||
| 83 | + std::vector<int64_t> tile(inShape.size()); | ||
| 84 | + for (size_t axis = 0; axis < inShape.size(); ++axis) { | ||
| 85 | + if (static_cast<int64_t>(axis) == fullAxis) { | ||
| 86 | + tile[axis] = inShape[axis]; | ||
| 87 | + } else if (axis < opVecTile.tile.size()) { | ||
| 88 | + tile[axis] = std::min(clampShape[axis], opVecTile.tile[axis]); | ||
| 89 | + } else { | ||
| 90 | + tile[axis] = clampShape[axis]; | ||
| 91 | + } | ||
| 92 | + } | ||
| 93 | + return tile; | ||
| 94 | +} | ||
| 95 | +} // namespace | ||
| 96 | + | ||
| 97 | +TileShape TileShapeResolver::GetInputTileShape(const Operation& op, int index) const | ||
| 98 | +{ | ||
| 99 | + const auto& iOperands = op.GetIOperands(); | ||
| 100 | + FE_ASSERT(FeError::OUT_OF_RANGE, index >= 0 && static_cast<size_t>(index) < iOperands.size()) | ||
| @@ -0,0 +1,50 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2025 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +/*! | ||
| 12 | + * \file tile_shape_resolver.h | ||
| 13 | + * \brief Resolve the per-input / per-output tile shape of an op from its opcode and op-level tile shape. | ||
| 14 | + */ | ||
| 15 | + | ||
| 16 | +#pragma once | ||
| 17 | + | ||
| 18 | +#include "operation.h" | ||
| 19 | +#include "tilefwk/tile_shape.h" | ||
| 20 | + | ||
| 21 | +namespace npu::tile_fwk { | ||
| 22 | + | ||
| 23 | +// Singleton method class that derives the tile shape of a given input or output tensor | ||
| 24 | +// from the opcode and the op-level tile shape. Per-opcode derivation rules are filled in | ||
| 25 | +// GetInputTileShape / GetOutputTileShape; the default returns the op-level tile shape | ||
| 26 | +// clamped to the operand's own shape (elementwise). | ||
| 27 | +class TileShapeResolver { | ||
| 28 | +public: | ||
| 29 | + static TileShapeResolver& Instance(); | ||
| 30 | + | ||
| 31 | + // Given the op and the input operand index, return the tile shape describing | ||
| 32 | + // how that input tensor is actually tiled. The op contributes its opcode, | ||
| 33 | + // op-level tile shape and input tensor shapes, so the result reflects the | ||
| 34 | + // real per-axis tile size (e.g. clamped to each input's own shape). | ||
| 35 | + TileShape GetInputTileShape(const Operation& op, int index) const; | ||
| 36 | + | ||
| 37 | + // Given the op and the output operand index, return the tile shape describing | ||
| 38 | + // how that output tensor is actually tiled (i.e. the first-cut output tile shape | ||
| 39 | + // produced by the op's TileFunc during expansion). The default rule clamps the | ||
| 40 | + // op-level VecTile to the output's own shape per axis; cube matmul and a few | ||
| 41 | + // layout-transform ops override this. | ||
| 42 | + TileShape GetOutputTileShape(const Operation& op, int index) const; | ||
| 43 | + | ||
| 44 | +private: | ||
| 45 | + TileShapeResolver() = default; | ||
| 46 | + TileShapeResolver(const TileShapeResolver&) = delete; | ||
| 47 | + TileShapeResolver& operator=(const TileShapeResolver&) = delete; | ||
| 48 | +}; | ||
| 49 | + | ||
| 50 | +} // namespace npu::tile_fwk | ||
| @@ -0,0 +1,419 @@ | |||
| 1 | +/** | ||
| 2 | + * Copyright (c) 2025-2026 Huawei Technologies Co., Ltd. | ||
| 3 | + * This program is free software, you can redistribute it and/or modify it under the terms and conditions of | ||
| 4 | + * CANN Open Software License Agreement Version 2.0 (the "License"). | ||
| 5 | + * Please refer to the License for details. You may not use this file except in compliance with the License. | ||
| 6 | + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, | ||
| 7 | + * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. | ||
| 8 | + * See LICENSE in the root of the software repository for the full text of the License. | ||
| 9 | + */ | ||
| 10 | + | ||
| 11 | +/*! | ||
| 12 | + * \file test_tile_shape_resolver.cpp | ||
| 13 | + * \brief Verify TileShapeResolver (both GetInputTileShape and GetOutputTileShape) against the | ||
| 14 | + * actual first-cut tile shape produced by the registered TileFunc (via ExpandFunction) | ||
| 15 | + * for representative tensor ops. | ||
| 16 | + * | ||
| 17 | + * Strategy: | ||
| 18 | + * 1. Build a single-op tensor graph with the DSL inside FUNCTION. The op picks up | ||
| 19 | + * TileShape::Current() as its op-level tile shape. The fixture holds the output Tensor(s) | ||
| 20 | + * outside FUNCTION so their underlying LogicalTensor survives. | ||
| 21 | + * 2. Before expansion: record each input/output operand's underlying RawTensor identity and | ||
| 22 | + * compute the resolver's expected per-input and per-output tile shape. | ||
| 23 | + * Note: in this harness the tensor-graph op's oOperand is still empty at this point (outputs | ||
| 24 | + * are attached lazily during the expansion pipeline), so we attach the fixture-provided | ||
| 25 | + * output LogicalTensors to oOperand first; this is exactly what ExpandFunction consumes. | ||
| 26 | + * 3. Run ExpandFunction on the function; the tensor-graph op is replaced by tile ops whose | ||
| 27 | + * input/output operands are Views sharing the original operands' RawTensor. | ||
| 28 | + * 4. Scan the emitted tile ops in emission order; the first input (output) operand whose | ||
| 29 | + * RawTensor matches operand i gives the actual first-cut tile shape for input (output) i. | ||
| 30 | + * 5. Compare expected vs actual per input and per output. | ||
| 31 | + */ | ||
| 32 | + | ||
| 33 | +#include <gtest/gtest.h> | ||
| 34 | +#include <vector> | ||
| 35 | +#include <string> | ||
| 36 | +#include <tuple> | ||
| 37 | +#include "tilefwk/tilefwk.h" | ||
| 38 | +#include "interface/inner/tilefwk.h" | ||
| 39 | +#include "interface/function/function.h" | ||
| 40 | +#include "interface/operation/operation.h" | ||
| 41 | +#include "interface/operation/tile_shape_resolver.h" | ||
| 42 | +#include "interface/program/program.h" | ||
| 43 | +#include "passes/tensor_graph_pass/expand_function.h" | ||
| 44 | +#include "interface/configs/config_manager.h" | ||
| 45 | + | ||
| 46 | +using namespace npu::tile_fwk; | ||
| 47 | + | ||
| 48 | +namespace { | ||
| 49 | +// Core harness: given a function containing exactly one tensor-graph op and the output Tensors | ||
| 50 | +// produced by that op (held outside FUNCTION), verify that the resolver's per-input AND per-output | ||
| 51 | +// tile shape matches the actual first-cut tile shape emitted by the op's TileFunc during expansion. | ||
| 52 | +static void ExpectResolverMatchesExpansion(Function* fn, std::vector<Tensor> outputs) | ||
| 53 | +{ | ||
| 54 | + ASSERT_NE(fn, nullptr); | ||
| 55 | + auto preOps = fn->Operations().DuplicatedOpList(); | ||
| 56 | + ASSERT_EQ(preOps.size(), 1u) << "fixture must build exactly one tensor-graph op"; | ||
| 57 | + Operation* op = preOps[0]; | ||
| 58 | + | ||
| 59 | + const auto& iOperands = op->GetIOperands(); | ||
| 60 | + const int nIn = static_cast<int>(iOperands.size()); | ||
| 61 | + ASSERT_GE(nIn, 1) << "op has no tensor inputs to verify"; | ||
| 62 | + | ||
| 63 | + // The tensor-graph op's oOperand is still empty here (outputs are attached lazily during the | ||
| 64 | + // expansion pipeline). Attach the fixture-provided output LogicalTensors so both the resolver | ||
| 65 | + // and ExpandFunction see the real outputs. If oOperand is already populated (e.g. inplace ops | ||
| 66 | + // whose output is an input), leave it untouched. | ||
| 67 | + auto& oOperandsMut = op->GetOOperands(); | ||
| 68 | + if (oOperandsMut.empty()) { | ||
| 69 | + ASSERT_FALSE(outputs.empty()) << "fixture must provide at least one output tensor"; | ||
| 70 | + for (auto& out : outputs) { | ||
| 71 | + oOperandsMut.push_back(out.GetStorage()); | ||
| 72 | + } | ||
| 73 | + } | ||
| 74 | + const auto& oOperands = oOperandsMut; | ||
| 75 | + const int nOut = static_cast<int>(oOperands.size()); | ||
| 76 | + ASSERT_GE(nOut, 1) << "op has no tensor outputs to verify"; | ||
| 77 | + | ||
| 78 | + // Record each input's RawTensor identity and the resolver's expected input tile. | ||
| 79 | + std::vector<RawTensor*> inRawIds(nIn, nullptr); | ||
| 80 | + std::vector<std::vector<int64_t>> inExpected(nIn); | ||
| 81 | + for (int i = 0; i < nIn; ++i) { | ||
| 82 | + inRawIds[i] = iOperands[i]->GetRawTensor().get(); | ||
| 83 | + inExpected[i] = TileShapeResolver::Instance().GetInputTileShape(*op, i).GetVecTile().tile; | ||
| 84 | + } | ||
| 85 | + | ||
| 86 | + // Record each output's RawTensor identity and the resolver's expected output tile. | ||
| 87 | + std::vector<RawTensor*> outRawIds(nOut, nullptr); | ||
| 88 | + std::vector<std::vector<int64_t>> outExpected(nOut); | ||
| 89 | + for (int o = 0; o < nOut; ++o) { | ||
| 90 | + outRawIds[o] = oOperands[o]->GetRawTensor().get(); | ||
| 91 | + outExpected[o] = TileShapeResolver::Instance().GetOutputTileShape(*op, o).GetVecTile().tile; | ||
| 92 | + } | ||
| 93 | + | ||
| 94 | + // Expand: the tensor-graph op is replaced by tile ops whose input/output operands are Views | ||
| 95 | + // sharing the original operands' RawTensor. Abort the test if expansion itself fails, | ||
| 96 | + // otherwise the subsequent scan would capture partial/intermediate shapes and mask | ||
| 97 | + // the real failure as a spurious tile-shape mismatch. | ||
| 98 | + ExpandFunction expandFunction; | ||
| 99 | + ASSERT_NE(expandFunction.RunOnFunction(*fn), Status::FAILED) | ||
| 100 | + << "ExpandFunction failed; cannot verify tile shapes against a non-expanded graph"; | ||
| 101 | + | ||
| 102 | + // Capture the first-cut tile shape per input: the first emitted tile op (in emission | ||
| 103 | + // order) that consumes a View of input i. TiledXxx emits first-cut tiles first | ||
| 104 | + // (depth-first recursion starting at offset 0), so the first match is the first cut. | ||
| 105 | + std::vector<std::vector<int64_t>> inActual(nIn); | ||
| 106 | + std::vector<bool> inCaptured(nIn, false); | ||
| 107 | + // Capture the first-cut tile shape per output: the first emitted tile op that produces | ||
| 108 | + // a View of output o (i.e. o appears among the tile op's output operands). | ||
| 109 | + std::vector<std::vector<int64_t>> outActual(nOut); | ||
| 110 | + std::vector<bool> outCaptured(nOut, false); | ||
| 111 | + for (auto& top : fn->Operations().DuplicatedOpList()) { | ||
| 112 | + for (auto& operand : top->GetIOperands()) { | ||
| 113 | + RawTensor* raw = operand->GetRawTensor().get(); | ||
| 114 | + for (int i = 0; i < nIn; ++i) { | ||
| 115 | + if (!inCaptured[i] && raw == inRawIds[i]) { | ||
| 116 | + inActual[i] = operand->GetShape(); | ||
| 117 | + inCaptured[i] = true; | ||
| 118 | + } | ||
| 119 | + } | ||
| 120 | + } | ||
| 121 | + for (auto& operand : top->GetOOperands()) { | ||
| 122 | + RawTensor* raw = operand->GetRawTensor().get(); | ||
| 123 | + for (int o = 0; o < nOut; ++o) { | ||
| 124 | + if (!outCaptured[o] && raw == outRawIds[o]) { | ||
| 125 | + outActual[o] = operand->GetShape(); | ||
| 126 | + outCaptured[o] = true; | ||
| 127 | + } | ||
| 128 | + } | ||
| 129 | + } | ||
| 130 | + } | ||
| 131 | + | ||
| 132 | + for (int i = 0; i < nIn; ++i) { | ||
| 133 | + EXPECT_TRUE(inCaptured[i]) << "input[" << i << "] was never consumed by an emitted tile op"; | ||
| 134 | + if (!inCaptured[i]) { | ||
| 135 | + continue; | ||
| 136 | + } | ||
| 137 | + EXPECT_EQ(inExpected[i], inActual[i]) | ||
| 138 | + << "input[" << i << "] first-cut tile shape mismatch (expected from resolver)"; | ||
| 139 | + } | ||
| 140 | + for (int o = 0; o < nOut; ++o) { | ||
| 141 | + EXPECT_TRUE(outCaptured[o]) << "output[" << o << "] was never produced by an emitted tile op"; | ||
| 142 | + if (!outCaptured[o]) { | ||
| 143 | + continue; | ||
| 144 | + } | ||
| 145 | + EXPECT_EQ(outExpected[o], outActual[o]) | ||
| 146 | + << "output[" << o << "] first-cut tile shape mismatch (expected from resolver)"; | ||
| 147 | + } | ||
| 148 | +} | ||
| 149 | +} // namespace | ||
| 150 | + | ||
| 151 | +class TileShapeResolverTest : public ::testing::Test { | ||
| 152 | +public: | ||
| 153 | + static void SetUpTestCase() {} | ||
| 154 | + static void TearDownTestCase() {} | ||
| 155 | + | ||
| 156 | + void SetUp() override | ||
| 157 | + { | ||
| 158 | + Program::GetInstance().Reset(); | ||
| 159 | + config::Reset(); | ||
| 160 | + config::SetHostOption(COMPILE_STAGE, CS_EXECUTE_GRAPH); | ||
| 161 | + config::SetPlatformConfig(KEY_ENABLE_COST_MODEL, false); | ||
| 162 | + } | ||
| 163 | + void TearDown() override {} | ||
| 164 | +}; | ||
| 165 | + | ||
| 166 | +// ---- Elementwise (default branch) ---- | ||
| 167 | +TEST_F(TileShapeResolverTest, Div_Elementwise) | ||
| 168 | +{ | ||
| 169 | + TileShape::Current().SetVecTile(32, 32); | ||
| 170 | + Tensor a(DT_FP32, {64, 64}, "a"); | ||
| 171 | + Tensor b(DT_FP32, {64, 64}, "b"); | ||
| 172 | + Tensor out; | ||
| 173 | + FUNCTION("DivCase") { out = Div(a, b); } | ||
| 174 | + ExpectResolverMatchesExpansion(Program::GetInstance().GetCurrentFunction(), {out}); | ||
| 175 | +} | ||
| 176 | + | ||
| 177 | +// ---- OP_GATHER: params (axis full) + indices ---- | ||
| 178 | +TEST_F(TileShapeResolverTest, Gather_Axis0) | ||
| 179 | +{ | ||
| 180 | + TileShape::Current().SetVecTile(4, 16, 16); | ||
| 181 | + Tensor params(DT_FP32, {8, 16}, "params"); | ||
| 182 | + Tensor indices(DT_INT32, {4, 16}, "indices"); | ||
| 183 | + Tensor out; | ||
| 184 | + FUNCTION("GatherCase") { out = Gather(params, indices, 0); } | ||
| 185 | + ExpectResolverMatchesExpansion(Program::GetInstance().GetCurrentFunction(), {out}); | ||
| 186 | +} | ||
| 187 | + | ||
| 188 | +// ---- OP_SCATTER: self (axis full) + idx (axis full) + src (axis full) ---- | ||
| 189 | +TEST_F(TileShapeResolverTest, Scatter_Axis0) | ||
| 190 | +{ | ||
| 191 | + TileShape::Current().SetVecTile(8, 8); | ||
| 192 | + Tensor self(DT_FP32, {8, 16}, "self"); | ||
| 193 | + Tensor indices(DT_INT32, {4, 16}, "indices"); | ||
| 194 | + Tensor src(DT_FP32, {4, 16}, "src"); | ||
| 195 | + Tensor out; | ||
| 196 | + FUNCTION("ScatterCase") { out = Scatter(self, indices, src, 0, ScatterMode::NONE); } | ||
| 197 | + ExpectResolverMatchesExpansion(Program::GetInstance().GetCurrentFunction(), {out}); | ||
| 198 | +} | ||
| 199 | + | ||
| 200 | +// ---- OP_WHERE_TT: cond + input + other, all elementwise ---- | ||
| 201 | +TEST_F(TileShapeResolverTest, Where_TT) | ||
| 202 | +{ | ||
| 203 | + TileShape::Current().SetVecTile(32, 32); | ||
| 204 | + Tensor cond(DT_BOOL, {64, 64}, "cond"); | ||
| 205 | + Tensor input(DT_FP32, {64, 64}, "input"); | ||
| 206 | + Tensor other(DT_FP32, {64, 64}, "other"); | ||
| 207 | + Tensor out; | ||
| 208 | + FUNCTION("WhereCase") { out = Where(cond, input, other); } | ||
| 209 | + ExpectResolverMatchesExpansion(Program::GetInstance().GetCurrentFunction(), {out}); | ||
| 210 | +} | ||
| 211 | + | ||
| 212 | +// ---- OP_ONEHOT: single 1-D input, default elementwise branch ---- | ||
| 213 | +TEST_F(TileShapeResolverTest, OneHot_1D) | ||
| 214 | +{ | ||
| 215 | + TileShape::Current().SetVecTile(8, 16); | ||
| 216 | + Tensor self(DT_INT32, {8}, "self"); | ||
| 217 | + Tensor out; | ||
| 218 | + FUNCTION("OneHotCase") { out = OneHot(self, 16); } | ||
| 219 | + ExpectResolverMatchesExpansion(Program::GetInstance().GetCurrentFunction(), {out}); | ||
| 220 | +} | ||
| 221 | + | ||
| 222 | +// ---- OP_TOPK: single input, non-axis elementwise + axis chunked (aligned to 32) ---- | ||
| 223 | +TEST_F(TileShapeResolverTest, TopK_Axis1_MergeSort) | ||
| 224 | +{ | ||
| 225 | + TileShape::Current().SetVecTile(32, 4); | ||
| 226 | + Tensor self(DT_FP32, {64, 128}, "self"); | ||
| 227 | + Tensor values, indices; | ||
| 228 | + FUNCTION("TopKCase") { std::tie(values, indices) = TopK(self, 4, 1, true, TopKAlgo::MERGE_SORT); } | ||
| 229 | + ExpectResolverMatchesExpansion(Program::GetInstance().GetCurrentFunction(), {values, indices}); | ||
| 230 | +} | ||
| 231 | + | ||
| 232 | +// ---- OP_GATHER_ELEMENT: params (axis full) + indices (elementwise) ---- | ||
| 233 | +TEST_F(TileShapeResolverTest, GatherElements_Axis0) | ||
| 234 | +{ | ||
| 235 | + TileShape::Current().SetVecTile(4, 16); | ||
| 236 | + Tensor params(DT_FP32, {8, 16}, "params"); | ||
| 237 | + Tensor indices(DT_INT32, {8, 16}, "indices"); | ||
| 238 | + Tensor out; | ||
| 239 | + FUNCTION("GatherElementsCase") { out = GatherElements(params, indices, 0); } | ||
| 240 | + ExpectResolverMatchesExpansion(Program::GetInstance().GetCurrentFunction(), {out}); | ||
| 241 | +} | ||
| 242 | + | ||
| 243 | +// ---- OP_INDEX_ADD: [self, src, indices]; self axis full, src elementwise, indices full ---- | ||
| 244 | +TEST_F(TileShapeResolverTest, IndexAdd_Axis0) | ||
| 245 | +{ | ||
| 246 | + TileShape::Current().SetVecTile(8, 8); | ||
| 247 | + Tensor self(DT_FP32, {8, 16}, "self"); | ||
| 248 | + Tensor src(DT_FP32, {4, 16}, "src"); | ||
| 249 | + Tensor indices(DT_INT32, {4}, "indices"); | ||
| 250 | + Element alpha(DT_FP32, 1.0f); | ||
| 251 | + FUNCTION("IndexAddCase") { IndexAdd_(self, src, indices, 0, alpha); } | ||
| 252 | + ExpectResolverMatchesExpansion(Program::GetInstance().GetCurrentFunction(), {self}); | ||
| 253 | +} | ||
| 254 | + | ||
| 255 | +// ---- OP_PRELU: [input, weight]; input elementwise, weight 1-D tiled on channel axis ---- | ||
| 256 | +TEST_F(TileShapeResolverTest, PReLU_2D) | ||
| 257 | +{ | ||
| 258 | + TileShape::Current().SetVecTile(8, 8); | ||
| 259 | + Tensor input(DT_FP32, {8, 16}, "input"); | ||
| 260 | + Tensor weight(DT_FP32, {16}, "weight"); | ||
| 261 | + Tensor out; | ||
| 262 | + FUNCTION("PReLUCase") { out = PReLU(input, weight); } | ||
| 263 | + ExpectResolverMatchesExpansion(Program::GetInstance().GetCurrentFunction(), {out}); | ||
| 264 | +} | ||
| 265 | + | ||
| 266 | +// ---- OP_CAST: single input, default elementwise branch ---- | ||
| 267 | +TEST_F(TileShapeResolverTest, Cast_Fp32ToFp16) | ||
| 268 | +{ | ||
| 269 | + TileShape::Current().SetVecTile(4, 8); | ||
| 270 | + Tensor self(DT_FP32, {8, 16}, "self"); | ||
| 271 | + Tensor out; | ||
| 272 | + FUNCTION("CastCase") { out = Cast(self, DataType::DT_FP16, CastMode::CAST_NONE); } | ||
| 273 | + ExpectResolverMatchesExpansion(Program::GetInstance().GetCurrentFunction(), {out}); | ||
| 274 | +} | ||
| 275 | + | ||
| 276 | +// ---- OP_EXPAND: single input broadcast; default elementwise branch clamps by input's own shape ---- | ||
| 277 | +TEST_F(TileShapeResolverTest, Expand_Broadcast) | ||
| 278 | +{ | ||
| 279 | + TileShape::Current().SetVecTile(4, 16); | ||
| 280 | + Tensor self(DT_FP32, {8, 1}, "self"); | ||
| 281 | + Tensor out; | ||
| 282 | + FUNCTION("ExpandCase") { out = Expand(self, {8, 16}); } | ||
| 283 | + ExpectResolverMatchesExpansion(Program::GetInstance().GetCurrentFunction(), {out}); | ||
| 284 | +} | ||
| 285 | + | ||
| 286 | +// ---- OP_AXPY: [self, other] both elementwise (y += alpha*x) ---- | ||
| 287 | +TEST_F(TileShapeResolverTest, Axpy_Elementwise) | ||
| 288 | +{ | ||
| 289 | + TileShape::Current().SetVecTile(4, 8); | ||
| 290 | + Tensor self(DT_FP32, {8, 16}, "self"); | ||
| 291 | + Tensor other(DT_FP32, {8, 16}, "other"); | ||
| 292 | + Tensor out; | ||
| 293 | + FUNCTION("AxpyCase") { out = Axpy(self, other, 1.0f); } | ||
| 294 | + ExpectResolverMatchesExpansion(Program::GetInstance().GetCurrentFunction(), {out}); | ||
| 295 | +} | ||
| 296 | + | ||
| 297 | +// ---- OP_PERMUTE: output axis i <- input axis perm[i]; output tile = VecTile permuted by perm. | ||
| 298 | +// Uses vecTile smaller than input on every axis so the default (min per output axis) would | ||
| 299 | +// differ from the correct permuted VecTile, making this a discriminating fixture. ---- | ||
| 300 | +TEST_F(TileShapeResolverTest, Permute_AxisSwap) | ||
| 301 | +{ | ||
| 302 | + TileShape::Current().SetVecTile(2, 4, 8); | ||
| 303 | + Tensor self(DT_FP32, {4, 8, 16}, "self"); | ||
| 304 | + Tensor out; | ||
| 305 | + FUNCTION("PermuteCase") { out = Permute(self, {1, 0, 2}); } | ||
| 306 | + ExpectResolverMatchesExpansion(Program::GetInstance().GetCurrentFunction(), {out}); | ||
| 307 | +} | ||
| 308 | + | ||
| 309 | +// ---- OP_MRGSORT: single input; sort axis (last) kept full, others min by vecTile ---- | ||
| 310 | +TEST_F(TileShapeResolverTest, MrgSort_Axis1) | ||
| 311 | +{ | ||
| 312 | + TileShape::Current().SetVecTile(4, 16); | ||
| 313 | + Tensor self(DT_FP32, {8, 16}, "self"); | ||
| 314 | + Tensor out; | ||
| 315 | + FUNCTION("MrgSortCase") { out = MrgSort(self, 2); } | ||
| 316 | + ExpectResolverMatchesExpansion(Program::GetInstance().GetCurrentFunction(), {out}); | ||
| 317 | +} | ||
| 318 | + | ||
| 319 | +// ---- Reduction single family (OP_ROW*_SINGLE): single input; reduce axis kept full, | ||
| 320 | +// other axes min by vecTile. FP32 + keepDim=true yields a single tensor-graph op. ---- | ||
| 321 | + | ||
| 322 | +TEST_F(TileShapeResolverTest, RowSumSingle_Axis1) | ||
| 323 | +{ | ||
| 324 | + TileShape::Current().SetVecTile(4, 16); | ||
| 325 | + Tensor self(DT_FP32, {8, 16}, "self"); | ||
| 326 | + Tensor out; | ||
| 327 | + FUNCTION("RowSumSingleCase") { out = Sum(self, 1, true); } | ||
| 328 | + ExpectResolverMatchesExpansion(Program::GetInstance().GetCurrentFunction(), {out}); | ||
| 329 | +} | ||
| 330 | + | ||
| 331 | +TEST_F(TileShapeResolverTest, RowMaxSingle_Axis0) | ||
| 332 | +{ | ||
| 333 | + TileShape::Current().SetVecTile(8, 4); | ||
| 334 | + Tensor self(DT_FP32, {8, 16}, "self"); | ||
| 335 | + Tensor out; | ||
| 336 | + FUNCTION("RowMaxSingleCase") { out = Amax(self, 0, true); } | ||
| 337 | + ExpectResolverMatchesExpansion(Program::GetInstance().GetCurrentFunction(), {out}); | ||
| 338 | +} | ||
| 339 | + | ||
| 340 | +TEST_F(TileShapeResolverTest, RowMinSingle_Axis1) | ||
| 341 | +{ | ||
| 342 | + TileShape::Current().SetVecTile(4, 16); | ||
| 343 | + Tensor self(DT_FP32, {8, 16}, "self"); | ||
| 344 | + Tensor out; | ||
| 345 | + FUNCTION("RowMinSingleCase") { out = Amin(self, 1, true); } | ||
| 346 | + ExpectResolverMatchesExpansion(Program::GetInstance().GetCurrentFunction(), {out}); | ||
| 347 | +} | ||
| 348 | + | ||
| 349 | +TEST_F(TileShapeResolverTest, RowProdSingle_Axis0) | ||
| 350 | +{ | ||
| 351 | + TileShape::Current().SetVecTile(8, 4); | ||
| 352 | + Tensor self(DT_FP32, {8, 16}, "self"); | ||
| 353 | + Tensor out; | ||
| 354 | + FUNCTION("RowProdSingleCase") { out = Prod(self, 0, true); } | ||
| 355 | + ExpectResolverMatchesExpansion(Program::GetInstance().GetCurrentFunction(), {out}); | ||
| 356 | +} | ||
| 357 | + | ||
| 358 | +TEST_F(TileShapeResolverTest, RowArgMaxSingle_Axis1) | ||
| 359 | +{ | ||
| 360 | + TileShape::Current().SetVecTile(4, 16); | ||
| 361 | + Tensor self(DT_FP32, {8, 16}, "self"); | ||
| 362 | + Tensor out; | ||
| 363 | + FUNCTION("RowArgMaxSingleCase") { out = ArgMax(self, 1, true); } | ||
| 364 | + ExpectResolverMatchesExpansion(Program::GetInstance().GetCurrentFunction(), {out}); | ||
| 365 | +} | ||
| 366 | + | ||
| 367 | +TEST_F(TileShapeResolverTest, RowArgMinSingle_Axis0) | ||
| 368 | +{ | ||
| 369 | + TileShape::Current().SetVecTile(8, 4); | ||
| 370 | + Tensor self(DT_FP32, {8, 16}, "self"); | ||
| 371 | + Tensor out; | ||
| 372 | + FUNCTION("RowArgMinSingleCase") { out = ArgMin(self, 0, true); } | ||
| 373 | + ExpectResolverMatchesExpansion(Program::GetInstance().GetCurrentFunction(), {out}); | ||
| 374 | +} | ||
| 375 | + | ||
| 376 | +// ---- OP_BITSORT (via Sort32): single input; sort axis (last) kept full, others min by vecTile. ---- | ||
| 377 | +TEST_F(TileShapeResolverTest, BitSort_Axis1) | ||
| 378 | +{ | ||
| 379 | + TileShape::Current().SetVecTile(4, 16); | ||
| 380 | + Tensor self(DT_FP32, {8, 16}, "self"); | ||
| 381 | + Tensor out; | ||
| 382 | + FUNCTION("BitSortCase") { out = Sort32(self, 0); } | ||
| 383 | + ExpectResolverMatchesExpansion(Program::GetInstance().GetCurrentFunction(), {out}); | ||
| 384 | +} | ||
| 385 | + | ||
| 386 | +// ---- OP_EXTRACT (via TopKExtract): single input; last axis kept full, others min by vecTile. ---- | ||
| 387 | +TEST_F(TileShapeResolverTest, Extract_Axis1) | ||
| 388 | +{ | ||
| 389 | + TileShape::Current().SetVecTile(4, 16); | ||
| 390 | + Tensor self(DT_FP32, {8, 16}, "self"); | ||
| 391 | + Tensor out; | ||
| 392 | + FUNCTION("ExtractCase") { out = TopKExtract(self, 4, false); } | ||
| 393 | + ExpectResolverMatchesExpansion(Program::GetInstance().GetCurrentFunction(), {out}); | ||
| 394 | +} | ||
| 395 | + | ||
| 396 | +// ---- OP_SCATTER_ELEMENT (via Scatter with Element src): [self, indices]; axis full on both. ---- | ||
| 397 | +TEST_F(TileShapeResolverTest, ScatterElement_Axis0) | ||
| 398 | +{ | ||
| 399 | + TileShape::Current().SetVecTile(8, 8); | ||
| 400 | + Tensor self(DT_FP32, {8, 16}, "self"); | ||
| 401 | + Tensor indices(DT_INT32, {8, 16}, "indices"); | ||
| 402 | + Element src(DT_FP32, 1.0f); | ||
| 403 | + Tensor out; | ||
| 404 | + FUNCTION("ScatterElementCase") { out = Scatter(self, indices, src, 0, ScatterMode::NONE); } | ||
| 405 | + ExpectResolverMatchesExpansion(Program::GetInstance().GetCurrentFunction(), {out}); | ||
| 406 | +} | ||
| 407 | + | ||
| 408 | +// ---- OP_INDEX_ADD_UB: [self, src, indices]; FP32 + INT32 -> single op. ---- | ||
| 409 | +TEST_F(TileShapeResolverTest, IndexAddUb_Axis0) | ||
| 410 | +{ | ||
| 411 | + TileShape::Current().SetVecTile(8, 8); | ||
| 412 | + Tensor self(DT_FP32, {8, 16}, "self"); | ||
| 413 | + Tensor src(DT_FP32, {4, 16}, "src"); | ||
| 414 | + Tensor indices(DT_INT32, {4}, "indices"); | ||
| 415 | + Element alpha(DT_FP32, 1.0f); | ||
| 416 | + Tensor out; | ||
| 417 | + FUNCTION("IndexAddUbCase") { out = IndexAddUB(self, src, indices, 0, alpha); } | ||
| 418 | + ExpectResolverMatchesExpansion(Program::GetInstance().GetCurrentFunction(), {out}); | ||
| 419 | +} | ||