|
11 | 11 | //===----------------------------------------------------------------------===//
|
12 | 12 |
|
13 | 13 | #include <functional>
|
| 14 | +#include <numeric> |
14 | 15 |
|
15 | 16 | #include "mlir/Dialect/Tosa/IR/TosaOps.h"
|
16 | 17 | #include "mlir/Dialect/Tosa/Transforms/Passes.h"
|
@@ -289,8 +290,130 @@ struct TosaFoldConstantReciprocal : public OpRewritePattern<ReciprocalOp> {
|
289 | 290 | }
|
290 | 291 | };
|
291 | 292 |
|
| 293 | +/// Getting the axes position of the element which is located |
| 294 | +/// in the tensor at the counter index |
| 295 | + |
| 296 | +llvm::SmallVector<int64_t> |
| 297 | +getPositionFromIndex(int64_t index, llvm::ArrayRef<int64_t> tensorShape) { |
| 298 | + int64_t remaining = index; |
| 299 | + llvm::SmallVector<int64_t> position(tensorShape.size(), 0); |
| 300 | + for (int64_t i = tensorShape.size() - 1; i >= 0; --i) { |
| 301 | + position[i] = remaining % tensorShape[i]; |
| 302 | + remaining /= tensorShape[i]; |
| 303 | + } |
| 304 | + return position; |
| 305 | +} |
| 306 | + |
| 307 | +/// Getting the index of the element which is located at the |
| 308 | +/// axes position in the tensor |
| 309 | + |
| 310 | +int64_t getIndexFromPosition(llvm::ArrayRef<int64_t> position, |
| 311 | + llvm::ArrayRef<int64_t> tensorShape) { |
| 312 | + int64_t index = 0; |
| 313 | + int64_t multiplierTmp = 1; |
| 314 | + for (int64_t i = position.size() - 1; i >= 0; --i) { |
| 315 | + index += position[i] * multiplierTmp; |
| 316 | + multiplierTmp *= tensorShape[i]; |
| 317 | + } |
| 318 | + return index; |
| 319 | +} |
| 320 | + |
| 321 | +template <typename OperationType> |
| 322 | +llvm::APInt calculateReducedValue(const mlir::ElementsAttr &oldTensorAttr, |
| 323 | + llvm::ArrayRef<int64_t> oldShape, |
| 324 | + int64_t reductionAxis, |
| 325 | + int64_t reductionIndex) { |
| 326 | + |
| 327 | + llvm::SmallVector<int64_t> newShape(oldShape); |
| 328 | + newShape[reductionAxis] = 1; |
| 329 | + /// Let's calculate the position of the index |
| 330 | + llvm::SmallVector<int64_t> position = |
| 331 | + getPositionFromIndex(reductionIndex, newShape); |
| 332 | + auto oldTensor = oldTensorAttr.getValues<llvm::APInt>(); |
| 333 | + /// Starting from the first positon along the reduction axis |
| 334 | + position[reductionAxis] = 0; |
| 335 | + int64_t indexAtOldTensor = getIndexFromPosition(position, oldShape); |
| 336 | + llvm::APInt reducedValue = oldTensor[indexAtOldTensor]; |
| 337 | + |
| 338 | + for (int64_t reductionAxisVal = 1; reductionAxisVal < oldShape[reductionAxis]; |
| 339 | + ++reductionAxisVal) { |
| 340 | + |
| 341 | + int64_t stride = std::accumulate(oldShape.begin() + reductionAxis + 1, |
| 342 | + oldShape.end(), 1, std::multiplies<int>()); |
| 343 | + int64_t index = indexAtOldTensor + stride * reductionAxisVal; |
| 344 | + reducedValue = |
| 345 | + OperationType::calcOneElement(reducedValue, oldTensor[index]); |
| 346 | + } |
| 347 | + return reducedValue; |
| 348 | +} |
| 349 | + |
| 350 | +template <typename OperationType> |
| 351 | +struct ReduceConstantOptimization : public OpRewritePattern<OperationType> { |
| 352 | + |
| 353 | + using OpRewritePattern<OperationType>::OpRewritePattern; |
| 354 | + |
| 355 | + LogicalResult matchAndRewrite(OperationType op, |
| 356 | + PatternRewriter &rewriter) const override { |
| 357 | + Value inputOp = op.getInput(); |
| 358 | + auto constOp = inputOp.getDefiningOp<tosa::ConstOp>(); |
| 359 | + |
| 360 | + if (!constOp) |
| 361 | + return rewriter.notifyMatchFailure( |
| 362 | + op, "reduce input must be const operation"); |
| 363 | + |
| 364 | + if (!inputOp.hasOneUse()) |
| 365 | + return rewriter.notifyMatchFailure( |
| 366 | + op, "input operation has more than one user"); |
| 367 | + |
| 368 | + auto resultType = cast<ShapedType>(op.getOutput().getType()); |
| 369 | + |
| 370 | + if (!resultType.hasStaticShape()) |
| 371 | + return rewriter.notifyMatchFailure(op, "result type shape is not static"); |
| 372 | + |
| 373 | + auto reductionAxis = op.getAxis(); |
| 374 | + const auto denseElementsAttr = constOp.getValue(); |
| 375 | + const auto shapedOldElementsValues = |
| 376 | + denseElementsAttr.getType().cast<ShapedType>(); |
| 377 | + |
| 378 | + if (!llvm::isa<IntegerType>(shapedOldElementsValues.getElementType())) |
| 379 | + return rewriter.notifyMatchFailure( |
| 380 | + op, "reduce input currently supported with integer type"); |
| 381 | + |
| 382 | + auto oldShape = shapedOldElementsValues.getShape(); |
| 383 | + auto newShape = resultType.getShape(); |
| 384 | + |
| 385 | + auto newNumOfElements = std::accumulate(newShape.begin(), newShape.end(), 1, |
| 386 | + std::multiplies<int>()); |
| 387 | + llvm::SmallVector<APInt> newReducedTensor(newNumOfElements); |
| 388 | + |
| 389 | + for (int64_t reductionIndex = 0; reductionIndex < newNumOfElements; |
| 390 | + ++reductionIndex) { |
| 391 | + |
| 392 | + /// Let's reduce all the elements along this reduction axis |
| 393 | + newReducedTensor[reductionIndex] = calculateReducedValue<OperationType>( |
| 394 | + denseElementsAttr, oldShape, reductionAxis, reductionIndex); |
| 395 | + } |
| 396 | + |
| 397 | + auto rankedTensorType = cast<RankedTensorType>(resultType); |
| 398 | + auto denseAttr = |
| 399 | + mlir::DenseElementsAttr::get(rankedTensorType, newReducedTensor); |
| 400 | + rewriter.replaceOpWithNewOp<tosa::ConstOp>(op, rankedTensorType, denseAttr); |
| 401 | + return success(); |
| 402 | + } |
| 403 | +}; |
| 404 | + |
292 | 405 | } // namespace
|
293 | 406 |
|
| 407 | +void mlir::tosa::populateTosaConstantReduction(MLIRContext *ctx, |
| 408 | + RewritePatternSet &patterns) { |
| 409 | + patterns.add<ReduceConstantOptimization<ReduceAllOp>>(ctx); |
| 410 | + patterns.add<ReduceConstantOptimization<ReduceAnyOp>>(ctx); |
| 411 | + patterns.add<ReduceConstantOptimization<ReduceMaxOp>>(ctx); |
| 412 | + patterns.add<ReduceConstantOptimization<ReduceMinOp>>(ctx); |
| 413 | + patterns.add<ReduceConstantOptimization<ReduceProdOp>>(ctx); |
| 414 | + patterns.add<ReduceConstantOptimization<ReduceSumOp>>(ctx); |
| 415 | +} |
| 416 | + |
294 | 417 | void mlir::tosa::populateTosaFoldConstantTransposePatterns(
|
295 | 418 | MLIRContext *ctx, RewritePatternSet &patterns) {
|
296 | 419 | patterns.add<TosaFoldConstantTranspose>(ctx);
|
|
0 commit comments