* Copyright (c) 2025 Huawei Technologies Co., Ltd.
* This program is free software, you can redistribute it and/or modify it under the terms and conditions of
* CANN Open Software License Agreement Version 2.0 (the "License").
* Please refer to the License for details. You may not use this file except in compliance with the License.
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
* See LICENSE in the root of the software repository for the full text of the License.
*/
* \file tile_shape_resolver.cpp
* \brief Resolve the per-input / per-output tile shape of an op from the op (opcode + op-level tile shape + shapes).
*/
#include "tile_shape_resolver.h"
#include <algorithm>
#include "interface/utils/error.h"
#include "interface/operation/opcode.h"
#include "interface/operation/operation_impl.h"
#include "interface/tensor/logical_tensor.h"
namespace npu::tile_fwk {
TileShapeResolver& TileShapeResolver::Instance()
{
static TileShapeResolver inst;
return inst;
}
namespace {
TileShape MakeTileShapeFromVec(const std::vector<int64_t>& vec)
{
TileShape tile;
tile.SetVecTile(vec);
return tile;
}
std::vector<int64_t> ElementwiseInputVecTile(const std::vector<int64_t>& inShape, const VecTile& opVecTile)
{
std::vector<int64_t> tile(inShape.size());
for (size_t axis = 0; axis < inShape.size(); ++axis) {
if (axis < opVecTile.tile.size()) {
tile[axis] = std::min(inShape[axis], opVecTile.tile[axis]);
} else {
tile[axis] = inShape[axis];
}
}
return tile;
}
std::vector<int64_t> TransDataInputVecTile(const std::vector<int64_t>& inShape, const VecTile& opVecTile, int64_t group,
int64_t groupAxis)
{
if (group <= 0) {
group = 1;
}
std::vector<int64_t> tile(inShape.size());
for (size_t axis = 0; axis < inShape.size(); ++axis) {
int64_t curShapeLen = (static_cast<int64_t>(axis) == groupAxis) ? inShape[axis] / group : inShape[axis];
if (axis < opVecTile.tile.size()) {
tile[axis] = std::min(curShapeLen, opVecTile.tile[axis]);
} else {
tile[axis] = curShapeLen;
}
}
return tile;
}
std::vector<int64_t> VecTileAxisFull(const std::vector<int64_t>& inShape, const std::vector<int64_t>& clampShape,
const VecTile& opVecTile, int64_t fullAxis)
{
std::vector<int64_t> tile(inShape.size());
for (size_t axis = 0; axis < inShape.size(); ++axis) {
if (static_cast<int64_t>(axis) == fullAxis) {
tile[axis] = inShape[axis];
} else if (axis < opVecTile.tile.size()) {
tile[axis] = std::min(clampShape[axis], opVecTile.tile[axis]);
} else {
tile[axis] = clampShape[axis];
}
}
return tile;
}
}
TileShape TileShapeResolver::GetInputTileShape(const Operation& op, int index) const
{
const auto& iOperands = op.GetIOperands();
FE_ASSERT(FeError::OUT_OF_RANGE, index >= 0 && static_cast<size_t>(index) < iOperands.size())
<< "input tile shape index out of range: " << index << ", input operand size=" << iOperands.size();
const auto& opTileShape = op.GetTileShape();
const auto& opVecTile = opTileShape.GetVecTile();
const auto& inShape = iOperands[index]->GetShape();
switch (op.GetOpcode()) {
case Opcode::OP_INDEX_ADD_UB:
case Opcode::OP_INDEX_ADD: {
int axis = op.HasAttr(OP_ATTR_PREFIX + "axis") ?
static_cast<int>(op.GetIntAttribute(OP_ATTR_PREFIX + "axis")) :
0;
if (index == 0) {
return MakeTileShapeFromVec(VecTileAxisFull(inShape, inShape, opVecTile, axis));
}
if (index == 1) {
return MakeTileShapeFromVec(ElementwiseInputVecTile(inShape, opVecTile));
}
return MakeTileShapeFromVec(inShape);
}
case Opcode::OP_GATHER: {
int axis = op.HasAttr(OP_ATTR_PREFIX + "axis") ?
static_cast<int>(op.GetIntAttribute(OP_ATTR_PREFIX + "axis")) :
0;
if (axis < 0) {
axis += static_cast<int>(iOperands[0]->GetShape().size());
}
const auto& paramsShape = iOperands[0]->GetShape();
const auto& indicesShape = iOperands[1]->GetShape();
auto indicesDim = static_cast<int64_t>(indicesShape.size());
if (index == 0) {
std::vector<int64_t> tile(paramsShape.size());
for (size_t a = 0; a < paramsShape.size(); ++a) {
if (static_cast<int64_t>(a) == axis) {
tile[a] = paramsShape[a];
} else {
int64_t cur = (static_cast<int64_t>(a) < axis) ? static_cast<int64_t>(a) :
static_cast<int64_t>(a) + indicesDim - 1;
tile[a] = (cur < static_cast<int64_t>(opVecTile.tile.size())) ?
std::min(paramsShape[a], opVecTile.tile[cur]) :
paramsShape[a];
}
}
return MakeTileShapeFromVec(tile);
}
if (index == 1) {
std::vector<int64_t> tile(indicesShape.size());
for (size_t j = 0; j < indicesShape.size(); ++j) {
int64_t cur = axis + static_cast<int64_t>(j);
tile[j] = (cur < static_cast<int64_t>(opVecTile.tile.size())) ?
std::min(indicesShape[j], opVecTile.tile[cur]) :
indicesShape[j];
}
return MakeTileShapeFromVec(tile);
}
return opTileShape;
}
case Opcode::OP_GATHER_ELEMENT: {
int axis = op.HasAttr(OP_ATTR_PREFIX + "axis") ?
static_cast<int>(op.GetIntAttribute(OP_ATTR_PREFIX + "axis")) :
0;
if (index == 0) {
return MakeTileShapeFromVec(VecTileAxisFull(inShape, inShape, opVecTile, axis));
}
return MakeTileShapeFromVec(ElementwiseInputVecTile(inShape, opVecTile));
}
case Opcode::OP_SCATTER_ELEMENT: {
int axis = op.HasAttr(OP_ATTR_PREFIX + "axis") ?
static_cast<int>(op.GetIntAttribute(OP_ATTR_PREFIX + "axis")) :
0;
const auto& idxShape = iOperands[1]->GetShape();
if (index == 0) {
return MakeTileShapeFromVec(VecTileAxisFull(inShape, idxShape, opVecTile, axis));
}
return MakeTileShapeFromVec(VecTileAxisFull(idxShape, idxShape, opVecTile, axis));
}
case Opcode::OP_SCATTER: {
int axis = op.HasAttr(OP_ATTR_PREFIX + "axis") ?
static_cast<int>(op.GetIntAttribute(OP_ATTR_PREFIX + "axis")) :
0;
const auto& idxShape = iOperands[1]->GetShape();
if (index == 0) {
return MakeTileShapeFromVec(VecTileAxisFull(inShape, idxShape, opVecTile, axis));
}
return MakeTileShapeFromVec(VecTileAxisFull(idxShape, idxShape, opVecTile, axis));
}
case Opcode::OP_INDEX_OUTCAST: {
int axis = op.HasAttr("axis") ? static_cast<int>(op.GetIntAttribute("axis")) : 0;
if (index == 0 || index == 2) {
return MakeTileShapeFromVec(VecTileAxisFull(inShape, inShape, opVecTile, axis));
}
std::vector<int64_t> tile = inShape;
for (size_t a = 0; a < inShape.size(); ++a) {
if (static_cast<int64_t>(a) == axis) {
tile[a] = inShape[a];
} else if (a == 0 && axis != 0 && a < opVecTile.tile.size()) {
tile[a] = std::min(inShape[a], opVecTile.tile[a]);
} else {
tile[a] = inShape[a];
}
}
return MakeTileShapeFromVec(tile);
}
case Opcode::OP_RANGE:
return opTileShape;
case Opcode::OP_ROWMAX_SINGLE:
case Opcode::OP_ROWMIN_SINGLE:
case Opcode::OP_ROWSUM_SINGLE:
case Opcode::OP_ROWPROD_SINGLE:
case Opcode::OP_ROWARGMAX_SINGLE:
case Opcode::OP_ROWARGMIN_SINGLE: {
int axis = op.HasAttr(OP_ATTR_PREFIX + "AXIS") ?
static_cast<int>(op.GetIntAttribute(OP_ATTR_PREFIX + "AXIS")) :
0;
if (axis < 0) {
axis += static_cast<int>(inShape.size());
}
return MakeTileShapeFromVec(VecTileAxisFull(inShape, inShape, opVecTile, axis));
}
case Opcode::OP_ROWEXPMAX:
case Opcode::OP_ROWEXPSUM: {
return MakeTileShapeFromVec(VecTileAxisFull(inShape, inShape, opVecTile, 1));
}
case Opcode::OP_WHERE_TT:
case Opcode::OP_WHERE_TS:
case Opcode::OP_WHERE_ST:
case Opcode::OP_WHERE_SS: {
if (index == 0) {
auto tile = ElementwiseInputVecTile(inShape, opVecTile);
if (iOperands[0]->Datatype() == DataType::DT_UINT8 && !tile.empty() &&
opVecTile.tile.size() >= tile.size()) {
tile.back() = std::min(inShape.back(), opVecTile.tile[tile.size() - 1] / 8);
}
return MakeTileShapeFromVec(tile);
}
return MakeTileShapeFromVec(ElementwiseInputVecTile(inShape, opVecTile));
}
case Opcode::OP_BITSORT:
case Opcode::OP_MRGSORT: {
int axis = op.HasAttr(OP_ATTR_PREFIX + "axis") ?
static_cast<int>(op.GetIntAttribute(OP_ATTR_PREFIX + "axis")) :
0;
if (axis < 0) {
axis += static_cast<int>(inShape.size());
}
return MakeTileShapeFromVec(VecTileAxisFull(inShape, inShape, opVecTile, axis));
}
case Opcode::OP_ARGSORT: {
int axis = op.HasAttr("axis") ? static_cast<int>(op.GetIntAttribute("axis")) : 0;
if (axis < 0) {
axis += static_cast<int>(inShape.size());
}
return MakeTileShapeFromVec(VecTileAxisFull(inShape, inShape, opVecTile, axis));
}
case Opcode::OP_EXTRACT: {
int64_t last = static_cast<int64_t>(inShape.size()) - 1;
return MakeTileShapeFromVec(VecTileAxisFull(inShape, inShape, opVecTile, last));
}
case Opcode::OP_TOPK: {
int axis = op.HasAttr(OP_ATTR_PREFIX + "axis") ?
static_cast<int>(op.GetIntAttribute(OP_ATTR_PREFIX + "axis")) :
0;
if (axis < 0) {
axis += static_cast<int>(inShape.size());
}
std::vector<int64_t> tile = ElementwiseInputVecTile(inShape, opVecTile);
if (axis >= static_cast<int>(opVecTile.tile.size())) {
return MakeTileShapeFromVec(tile);
}
int64_t shapeAxis = inShape[axis];
int64_t align = opVecTile.tile[axis];
int64_t nonAxisProd = 1;
for (size_t a = 0; a < inShape.size(); ++a) {
if (static_cast<int>(a) == axis) {
continue;
}
nonAxisProd *= tile[a];
}
int64_t sourceShapeSize = nonAxisProd * shapeAxis;
constexpr int64_t kBlockSize = 32;
constexpr int64_t maxNumValue = 8192;
if (shapeAxis > align * 2) {
int64_t tileShapeSize = nonAxisProd * align;
if (sourceShapeSize < maxNumValue) {
align = shapeAxis;
} else if (tileShapeSize < maxNumValue) {
align = std::max<int64_t>(kBlockSize, (maxNumValue / nonAxisProd / kBlockSize) * kBlockSize);
}
}
align = (align + kBlockSize - 1) / kBlockSize * kBlockSize;
tile[axis] = std::min(align, shapeAxis);
return MakeTileShapeFromVec(tile);
}
case Opcode::OP_SORT_UB: {
int axis = op.HasAttr(OP_ATTR_PREFIX + "axis") ?
static_cast<int>(op.GetIntAttribute(OP_ATTR_PREFIX + "axis")) :
0;
if (axis < 0) {
axis += static_cast<int>(inShape.size());
}
return MakeTileShapeFromVec(VecTileAxisFull(inShape, inShape, opVecTile, axis));
}
case Opcode::OP_VEC_DUP:
return opTileShape;
case Opcode::OP_NCHW2NC1HWC0: {
int64_t group = op.HasAttr(OP_ATTR_PREFIX + "group") ? op.GetIntAttribute(OP_ATTR_PREFIX + "group") : 1;
return MakeTileShapeFromVec(TransDataInputVecTile(inShape, opVecTile, group, 1));
}
case Opcode::OP_NCHW2Fractal_Z: {
int64_t group = op.HasAttr(OP_ATTR_PREFIX + "group") ? op.GetIntAttribute(OP_ATTR_PREFIX + "group") : 1;
return MakeTileShapeFromVec(TransDataInputVecTile(inShape, opVecTile, group, 0));
}
case Opcode::OP_NC1HWC02NCHW: {
int64_t group = op.HasAttr(OP_ATTR_PREFIX + "group") ? op.GetIntAttribute(OP_ATTR_PREFIX + "group") : 1;
return MakeTileShapeFromVec(TransDataInputVecTile(inShape, opVecTile, group, 1));
}
case Opcode::OP_NCDHW2NDC1HWC0: {
int64_t group = op.HasAttr(OP_ATTR_PREFIX + "group") ? op.GetIntAttribute(OP_ATTR_PREFIX + "group") : 1;
return MakeTileShapeFromVec(TransDataInputVecTile(inShape, opVecTile, group, 2));
}
case Opcode::OP_NCDHW2FRACTAL_Z_3D: {
int64_t group = op.HasAttr(OP_ATTR_PREFIX + "group") ? op.GetIntAttribute(OP_ATTR_PREFIX + "group") : 1;
return MakeTileShapeFromVec(TransDataInputVecTile(inShape, opVecTile, group, 0));
}
case Opcode::OP_NDC1HWC02NCDHW: {
int64_t group = op.HasAttr(OP_ATTR_PREFIX + "group") ? op.GetIntAttribute(OP_ATTR_PREFIX + "group") : 1;
return MakeTileShapeFromVec(TransDataInputVecTile(inShape, opVecTile, group, 2));
}
case Opcode::OP_UNIFORM:
return opTileShape;
case Opcode::OP_PACK: {
int64_t tile = 1;
for (size_t i = 0; i < opVecTile.tile.size(); ++i) {
tile *= opVecTile.tile[i];
}
int64_t axis0 = inShape.empty() ? tile : std::min(inShape[0], tile);
return MakeTileShapeFromVec({axis0});
}
case Opcode::OP_PRELU: {
if (index == 0) {
return MakeTileShapeFromVec(ElementwiseInputVecTile(inShape, opVecTile));
}
const auto& weightShape = iOperands[1]->GetShape();
if (inShape.size() == 1) {
return MakeTileShapeFromVec({1});
}
int64_t wTile = (opVecTile.tile.size() >= 2) ? std::min(weightShape[0], opVecTile.tile[1]) : weightShape[0];
return MakeTileShapeFromVec({wTile});
}
case Opcode::OP_CUBE_CONV_D2S:
case Opcode::OP_CUBE_CONCAT_C:
return opTileShape;
case Opcode::OP_INDEX_PUT: {
const auto& selfShape = iOperands[0]->GetShape();
const auto& valuesShape = iOperands[1]->GetShape();
auto selfDim = static_cast<int64_t>(selfShape.size());
auto valuesDim = static_cast<int64_t>(valuesShape.size());
if (index == 1) {
return MakeTileShapeFromVec(ElementwiseInputVecTile(valuesShape, opVecTile));
}
if (index >= 2) {
int64_t axis0Tile = opVecTile.tile.empty() ? inShape[0] : std::min(inShape[0], opVecTile.tile[0]);
return MakeTileShapeFromVec({axis0Tile});
}
std::vector<int64_t> tile(selfDim);
int64_t leading = selfDim - valuesDim;
for (int64_t axis = 0; axis < selfDim; ++axis) {
if (axis <= leading) {
tile[axis] = selfShape[axis];
} else {
int64_t cur = axis - leading;
if (cur < static_cast<int64_t>(opVecTile.tile.size())) {
tile[axis] = std::min(selfShape[axis], opVecTile.tile[cur]);
} else {
tile[axis] = selfShape[axis];
}
}
}
return MakeTileShapeFromVec(tile);
}
case Opcode::OP_A_MUL_B:
case Opcode::OP_A_MUL_BT:
case Opcode::OP_A_MULACC_B:
case Opcode::OP_A_MULACC_BT: {
const auto& cubeTile = opTileShape.GetCubeTile();
auto transA = op.HasAttr(Matrix::A_MUL_B_TRANS_A) && op.GetBoolAttribute(Matrix::A_MUL_B_TRANS_A);
auto transB = op.HasAttr(Matrix::A_MUL_B_TRANS_B) && op.GetBoolAttribute(Matrix::A_MUL_B_TRANS_B);
if (index == 0) {
std::vector<int64_t> tileA = {cubeTile.m[0], cubeTile.k[0]};
if (transA) {
std::reverse(tileA.begin(), tileA.end());
}
return MakeTileShapeFromVec(tileA);
}
if (index == 1) {
std::vector<int64_t> tileB = {cubeTile.k[0], cubeTile.n[0]};
if (transB) {
std::reverse(tileB.begin(), tileB.end());
}
return MakeTileShapeFromVec(tileB);
}
auto hasMX = op.HasAttr(Matrix::A_MUL_B_MX_ATTR) && op.GetBoolAttribute(Matrix::A_MUL_B_MX_ATTR);
auto hasBias = op.HasAttr(Matrix::A_MUL_B_BIAS_ATTR) && op.GetBoolAttribute(Matrix::A_MUL_B_BIAS_ATTR);
auto hasScale = op.HasAttr(Matrix::A_MUL_B_VECTOR_QUANT_FLAG) &&
op.GetBoolAttribute(Matrix::A_MUL_B_VECTOR_QUANT_FLAG);
int base = 2;
int aScaleIdx = -1, bScaleIdx = -1, biasIdx = -1, scaleIdx = -1;
if (hasMX) {
aScaleIdx = base;
bScaleIdx = base + 1;
base += 2;
}
if (hasBias) {
biasIdx = base++;
}
if (hasScale) {
scaleIdx = base;
}
const auto& aShape = iOperands[0]->GetShape();
const auto& bShape = iOperands[1]->GetShape();
int64_t m = transA ? aShape[1] : aShape[0];
int64_t k = transA ? aShape[0] : aShape[1];
int64_t n = transB ? bShape[0] : bShape[1];
auto ceilDiv = [](int64_t x, int64_t a) { return (x + a - 1) / a; };
if (index == biasIdx || index == scaleIdx) {
return MakeTileShapeFromVec({1, std::min(n, cubeTile.n[0])});
}
if (hasMX && index == aScaleIdx) {
auto transAScale = op.HasAttr(Matrix::A_MUL_B_SCALE_A_COPY_IN_MODE) &&
op.GetIntAttribute(Matrix::A_MUL_B_SCALE_A_COPY_IN_MODE) ==
static_cast<int64_t>(Matrix::CopyInMode::DN2NZ);
int64_t mL0 = std::min(m, cubeTile.m[0]);
int64_t kScale = ceilDiv(std::min(k, cubeTile.k[1]), 64);
std::vector<int64_t> tile = transAScale ? std::vector<int64_t>{kScale, mL0, 2} :
std::vector<int64_t>{mL0, kScale, 2};
return MakeTileShapeFromVec(tile);
}
if (hasMX && index == bScaleIdx) {
auto transBScale = op.HasAttr(Matrix::A_MUL_B_SCALE_B_COPY_IN_MODE) &&
op.GetIntAttribute(Matrix::A_MUL_B_SCALE_B_COPY_IN_MODE) ==
static_cast<int64_t>(Matrix::CopyInMode::DN2NZ);
int64_t nL0 = std::min(n, cubeTile.n[0]);
int64_t kScale = ceilDiv(std::min(k, cubeTile.k[1]), 64);
std::vector<int64_t> tile = transBScale ? std::vector<int64_t>{nL0, kScale, 2} :
std::vector<int64_t>{kScale, nL0, 2};
return MakeTileShapeFromVec(tile);
}
return opTileShape;
}
case Opcode::OP_RESHAPE:
return MakeTileShapeFromVec(inShape);
default:
return MakeTileShapeFromVec(ElementwiseInputVecTile(inShape, opVecTile));
}
}
TileShape TileShapeResolver::GetOutputTileShape(const Operation& op, int index) const
{
const auto& oOperands = op.GetOOperands();
FE_ASSERT(FeError::OUT_OF_RANGE, index >= 0 && static_cast<size_t>(index) < oOperands.size())
<< "output tile shape index out of range: " << index << ", output operand size=" << oOperands.size();
const auto& opTileShape = op.GetTileShape();
const auto& opVecTile = opTileShape.GetVecTile();
const auto& outShape = oOperands[index]->GetShape();
switch (op.GetOpcode()) {
case Opcode::OP_A_MUL_B:
case Opcode::OP_A_MUL_BT:
case Opcode::OP_A_MULACC_B:
case Opcode::OP_A_MULACC_BT: {
const auto& cubeTile = opTileShape.GetCubeTile();
const auto& iOperands = op.GetIOperands();
auto transA = op.HasAttr(Matrix::A_MUL_B_TRANS_A) && op.GetBoolAttribute(Matrix::A_MUL_B_TRANS_A);
auto transB = op.HasAttr(Matrix::A_MUL_B_TRANS_B) && op.GetBoolAttribute(Matrix::A_MUL_B_TRANS_B);
const auto& aShape = iOperands[0]->GetShape();
const auto& bShape = iOperands[1]->GetShape();
int64_t m = transA ? aShape[1] : aShape[0];
int64_t n = transB ? bShape[0] : bShape[1];
return MakeTileShapeFromVec({std::min(m, cubeTile.m[0]), std::min(n, cubeTile.n[0])});
}
case Opcode::OP_NCHW2NC1HWC0:
case Opcode::OP_NCHW2Fractal_Z:
case Opcode::OP_NC1HWC02NCHW:
case Opcode::OP_NCDHW2NDC1HWC0:
case Opcode::OP_NCDHW2FRACTAL_Z_3D:
case Opcode::OP_NDC1HWC02NCDHW: {
return MakeTileShapeFromVec(outShape);
}
case Opcode::OP_PACK: {
const auto& iOperands = op.GetIOperands();
int64_t byte = iOperands.empty() ? 1 : static_cast<int64_t>(BytesOf(iOperands[0]->Datatype()));
int64_t tile = 1;
for (size_t i = 0; i < opVecTile.tile.size(); ++i) {
tile *= opVecTile.tile[i];
}
int64_t out0 = outShape.empty() ? tile * byte : std::min(outShape[0], tile * byte);
return MakeTileShapeFromVec({out0});
}
case Opcode::OP_CUBE_CONV_D2S:
case Opcode::OP_CUBE_CONCAT_C:
return opTileShape;
case Opcode::OP_PERMUTE:
case Opcode::OP_PERMUTE_ELEMENT: {
auto perm = op.GetVectorIntAttribute<int>(OpAttributeKey::perm);
std::vector<int64_t> tile(outShape.size());
for (size_t i = 0; i < outShape.size(); ++i) {
int64_t srcAxis = (i < perm.size()) ? static_cast<int64_t>(perm[i]) : static_cast<int64_t>(i);
if (srcAxis < 0) {
srcAxis += static_cast<int64_t>(outShape.size());
}
int64_t v = (srcAxis < static_cast<int64_t>(opVecTile.tile.size())) ? opVecTile.tile[srcAxis] :
outShape[i];
tile[i] = std::min(outShape[i], v);
}
return MakeTileShapeFromVec(tile);
}
case Opcode::OP_TRANSPOSE_MOVEOUT:
case Opcode::OP_TRANSPOSE_MOVEIN:
case Opcode::OP_TRANSPOSE_VNCHWCONV: {
auto swp = op.GetVectorIntAttribute<int64_t>(OP_ATTR_PREFIX + "shape");
std::vector<int64_t> tile(outShape.size());
for (size_t a = 0; a < outShape.size(); ++a) {
int64_t srcAxis = static_cast<int64_t>(a);
if (swp.size() == 2) {
if (a == static_cast<size_t>(swp[0])) {
srcAxis = swp[1];
} else if (a == static_cast<size_t>(swp[1])) {
srcAxis = swp[0];
}
}
int64_t v = (srcAxis < static_cast<int64_t>(opVecTile.tile.size())) ? opVecTile.tile[srcAxis] :
outShape[a];
tile[a] = std::min(outShape[a], v);
}
return MakeTileShapeFromVec(tile);
}
case Opcode::OP_UNPACK: {
const auto& iOperands = op.GetIOperands();
int64_t dstByte = static_cast<int64_t>(BytesOf(oOperands[index]->Datatype()));
if (iOperands.empty() || opVecTile.tile.empty() || dstByte <= 0) {
return opTileShape;
}
int64_t in0 = iOperands[0]->GetShape().empty() ? 0 : iOperands[0]->GetShape()[0];
return MakeTileShapeFromVec({std::min(in0, opVecTile.tile[0]) / dstByte});
}
case Opcode::OP_DEINTERLEAVE_SINGLE: {
std::vector<int64_t> tile(outShape.size());
for (size_t a = 0; a < outShape.size(); ++a) {
if (a == outShape.size() - 1 && a < opVecTile.tile.size()) {
tile[a] = std::min(outShape[a], opVecTile.tile[a] / 2);
} else if (a < opVecTile.tile.size()) {
tile[a] = std::min(outShape[a], opVecTile.tile[a]);
} else {
tile[a] = outShape[a];
}
}
return MakeTileShapeFromVec(tile);
}
case Opcode::OP_EXTRACT: {
int64_t last = static_cast<int64_t>(outShape.size()) - 1;
return MakeTileShapeFromVec(VecTileAxisFull(outShape, outShape, opVecTile, last));
}
case Opcode::OP_BITSORT:
case Opcode::OP_MRGSORT: {
int axis = op.HasAttr(OP_ATTR_PREFIX + "axis") ?
static_cast<int>(op.GetIntAttribute(OP_ATTR_PREFIX + "axis")) :
0;
if (axis < 0) {
axis += static_cast<int>(outShape.size());
}
return MakeTileShapeFromVec(VecTileAxisFull(outShape, outShape, opVecTile, axis));
}
case Opcode::OP_GATHER_MASK_BUILDIN: {
uint8_t patternMode = static_cast<uint8_t>(op.GetIntAttribute(OP_ATTR_PREFIX + "patternMode"));
int64_t divisor = 1;
if (patternMode == 1 || patternMode == 2) {
divisor = 2;
} else if (patternMode >= 3 && patternMode <= 6) {
divisor = 4;
}
std::vector<int64_t> tile(outShape.size());
for (size_t a = 0; a < outShape.size(); ++a) {
if (a == outShape.size() - 1 && a < opVecTile.tile.size()) {
tile[a] = std::min(outShape[a], opVecTile.tile[a] / divisor);
} else if (a < opVecTile.tile.size()) {
tile[a] = std::min(outShape[a], opVecTile.tile[a]);
} else {
tile[a] = outShape[a];
}
}
return MakeTileShapeFromVec(tile);
}
case Opcode::OP_RESHAPE:
return MakeTileShapeFromVec(outShape);
case Opcode::OP_INDEX_PUT:
return MakeTileShapeFromVec(outShape);
case Opcode::OP_ASSEMBLE_SSA:
return MakeTileShapeFromVec(outShape);
case Opcode::OP_INDEX_OUTCAST: {
const auto& iOperands = op.GetIOperands();
int axis = op.HasAttr("axis") ? static_cast<int>(op.GetIntAttribute("axis")) : 0;
if (op.HasAttr(OpAttributeKey::cacheMode) &&
op.GetStringAttribute(OpAttributeKey::cacheMode) == "PA_BSND") {
return MakeTileShapeFromVec(outShape);
}
if (!iOperands.empty()) {
const auto& srcShape = iOperands[0]->GetShape();
if (axis == 1 && srcShape.size() == 2 && opVecTile.tile.size() >= 2 &&
opVecTile.tile[1] == srcShape[1]) {
return MakeTileShapeFromVec(outShape);
}
}
if (iOperands.size() >= 3) {
const auto& dstShape = iOperands[2]->GetShape();
return MakeTileShapeFromVec(VecTileAxisFull(dstShape, dstShape, opVecTile, axis));
}
return MakeTileShapeFromVec(ElementwiseInputVecTile(outShape, opVecTile));
}
default:
return MakeTileShapeFromVec(ElementwiseInputVecTile(outShape, opVecTile));
}
}
}