Merged
fix(operation): Fix validshape of cat not correct #6289
lidongshengAdd on Sep 15
fix(operation): Fix validshape of cat not correct #6289
Merged
Merged fromtsungl4/pypto-opencat_9.2intocann/pypto9.2.0
Total: 3 files changed+40-7
| @@ -203,6 +203,14 @@ public: | |||
| 203 | */ | 203 | */ |
| 204 | std::vector<SymbolicScalar>& GetValidShape() const; | 204 | std::vector<SymbolicScalar>& GetValidShape() const; |
| 205 | 205 | ||
| 206 | + /** | ||
| 207 | + * \brief Get the valid shape information of the specified axis of Tensor. | ||
| 208 | + * | ||
| 209 | + * \param axis : The axis of the valid shape to be obtained. | ||
| 210 | + * \return SymbolicScalar : The valid shape of the specified axis, if valid shape missing, return shape. | ||
| 211 | + */ | ||
| 212 | + SymbolicScalar GetValidShape(int axis) const; | ||
| 213 | + | ||
| 206 | /** | 214 | /** |
| 207 | * \brief Get the format of the tensor. | 215 | * \brief Get the format of the tensor. |
| 208 | * | 216 | * |
| @@ -66,23 +66,32 @@ Tensor Cat(const std::vector<Tensor>& tensors, int axis) | |||
| 66 | } | 66 | } |
| 67 | 67 | ||
| 68 | CheckCat(tensors, axis); | 68 | CheckCat(tensors, axis); |
| 69 | - | ||
| 70 | - auto resultShape = tensors[0].GetShape(); | ||
| 71 | - auto shapeSize = resultShape.size(); | ||
| 72 | CheckAxisRange(tensors[0], axis); | 69 | CheckAxisRange(tensors[0], axis); |
| 70 | + | ||
| 73 | int axisSize = 0; | 71 | int axisSize = 0; |
| 72 | + SymbolicScalar axisDynSize(0); | ||
| 74 | for (auto tensor : tensors) { | 73 | for (auto tensor : tensors) { |
| 75 | - axisSize += tensor.GetShape()[axis]; | 74 | + axisSize += tensor.GetShape(axis); |
| 75 | + axisDynSize = axisDynSize + tensor.GetValidShape(axis); | ||
| 76 | } | 76 | } |
| 77 | - resultShape[axis] = axisSize; | ||
| 78 | 77 | ||
| 79 | auto format = tensors[0].Format(); | 78 | auto format = tensors[0].Format(); |
| 79 | + auto resultShape = tensors[0].GetShape(); | ||
| 80 | + resultShape[axis] = axisSize; | ||
| 80 | Tensor result(tensors[0].GetDataType(), resultShape, "", format); | 81 | Tensor result(tensors[0].GetDataType(), resultShape, "", format); |
| 81 | - std::vector<SymbolicScalar> offset(shapeSize, 0); | 82 | + |
| 83 | + auto validShape = tensors[0].GetValidShape(); | ||
| 84 | + if (validShape.empty()) { | ||
| 85 | + validShape = SymbolicScalar::FromConcrete(resultShape); | ||
| 86 | + } | ||
| 87 | + validShape[axis] = axisDynSize.Simplify(); | ||
| 88 | + result.GetStorage(false)->UpdateDynValidShape(validShape); | ||
| 89 | + | ||
| 90 | + std::vector<SymbolicScalar> offset(resultShape.size(), 0); | ||
| 82 | for (auto tensor : tensors) { | 91 | for (auto tensor : tensors) { |
| 83 | auto materialized = Assign(tensor); | 92 | auto materialized = Assign(tensor); |
| 84 | Assemble(materialized, offset, result); | 93 | Assemble(materialized, offset, result); |
| 85 | - offset[axis] = offset[axis] + tensor.GetShape()[axis]; | 94 | + offset[axis] = offset[axis] + tensor.GetShape(axis); |
| 86 | } | 95 | } |
| 87 | 96 | ||
| 88 | return result; | 97 | return result; |
| @@ -237,6 +237,22 @@ int32_t Tensor::GetShape(int axis) const | |||
| 237 | 237 | ||
| 238 | std::vector<SymbolicScalar>& Tensor::GetValidShape() const { return storage_->GetDynValidShape(); } | 238 | std::vector<SymbolicScalar>& Tensor::GetValidShape() const { return storage_->GetDynValidShape(); } |
| 239 | 239 | ||
| 240 | +SymbolicScalar Tensor::GetValidShape(int axis) const | ||
| 241 | +{ | ||
| 242 | + auto n = Dim(); | ||
| 243 | + if (axis < 0) { | ||
| 244 | + axis += n; | ||
| 245 | + } | ||
| 246 | + FE_ASSERT(FeError::OUT_OF_RANGE, axis >= 0 && static_cast<size_t>(axis) < n) | ||
| 247 | + << "Axis index " << axis << " is out of range [0, " << (n - 1) << "]."; | ||
| 248 | + | ||
| 249 | + auto& dynShape = storage_->GetDynValidShape(); | ||
| 250 | + if (dynShape.empty()) { | ||
| 251 | + return storage_->shape[axis]; | ||
| 252 | + } | ||
| 253 | + return dynShape[axis]; | ||
| 254 | +} | ||
| 255 | + | ||
| 240 | TileOpFormat Tensor::Format() const { return storage_->Format(); } | 256 | TileOpFormat Tensor::Format() const { return storage_->Format(); } |
| 241 | 257 | ||
| 242 | void Tensor::SetCachePolicy(CachePolicy policy, bool value) | 258 | void Tensor::SetCachePolicy(CachePolicy policy, bool value) |