diff --git a/lib/Conversion/TorchToLinalg/DataMovement.cpp b/lib/Conversion/TorchToLinalg/DataMovement.cpp index 297a0f4c2be6..e96d65970b82 100644 --- a/lib/Conversion/TorchToLinalg/DataMovement.cpp +++ b/lib/Conversion/TorchToLinalg/DataMovement.cpp @@ -978,6 +978,7 @@ class ConvertAtenViewOp : public OpConversionPattern { return success(); } + // TODO: audit possibility of sparsity on these tensors Type adjustedResultType = RankedTensorType::get( makeShapeLLVMCompatible(outputShape), resultType.getElementType()); Type adjustedInputType = RankedTensorType::get( @@ -1005,6 +1006,7 @@ class ConvertAtenViewOp : public OpConversionPattern { intermediateShape.push_back(sum); } + // TODO: audit possibility of sparsity on these tensor Type intermediateResultType = RankedTensorType::get(makeShapeLLVMCompatible(intermediateShape), resultType.getElementType()); @@ -1657,6 +1659,7 @@ class ConvertAtenSliceScatterOp auto srcType = src.getType().cast(); int64_t srcRank = srcType.getRank(); SmallVector srcAbstractSizes(srcRank, kUnknownSize); + // TODO: audit possibility of sparsity on these tensor auto abstractSrcType = RankedTensorType::get( makeShapeLLVMCompatible(srcAbstractSizes), srcType.getElementType()); Value abstractSrc = diff --git a/lib/Conversion/TorchToLinalg/IndirectDataMovement.cpp b/lib/Conversion/TorchToLinalg/IndirectDataMovement.cpp index 277341bea874..f9ee56070d61 100644 --- a/lib/Conversion/TorchToLinalg/IndirectDataMovement.cpp +++ b/lib/Conversion/TorchToLinalg/IndirectDataMovement.cpp @@ -206,8 +206,8 @@ namespace { // // TODO: Find an optimal lowering. // current lowering is not optimal for bags of large embeddings. -// Since it traverses the output tensor multiple times. -// +// Since it traverses the output tensor multiple times. +// // class ConvertAtenEmbeddingBagPaddingIdxOp diff --git a/lib/Conversion/TorchToLinalg/Linear.cpp b/lib/Conversion/TorchToLinalg/Linear.cpp index d818b99c0c4a..6d0d72075d76 100644 --- a/lib/Conversion/TorchToLinalg/Linear.cpp +++ b/lib/Conversion/TorchToLinalg/Linear.cpp @@ -377,8 +377,8 @@ class ConvertAtenMatmulOp : public OpConversionPattern { // TODO: Improve usage of static shape information. SmallVector lhsTargetShape(lhsBroadcastToShape.size(), ShapedType::kDynamic); - auto lhsBroadcastType = - RankedTensorType::get(lhsTargetShape, lhsType.getElementType()); + auto lhsBroadcastType = RankedTensorType::get( + lhsTargetShape, lhsType.getElementType(), lhsType.getEncoding()); if (failed(torch_to_linalg::broadcastToGivenShape( op, rewriter, lhs, lhsBroadcastToShape, lhsBroadcastType, broadcastedLhs))) { @@ -387,8 +387,8 @@ class ConvertAtenMatmulOp : public OpConversionPattern { } SmallVector rhsTargetShape(rhsBroadcastToShape.size(), ShapedType::kDynamic); - auto rhsBroadcastType = - RankedTensorType::get(rhsTargetShape, rhsType.getElementType()); + auto rhsBroadcastType = RankedTensorType::get( + rhsTargetShape, rhsType.getElementType(), rhsType.getEncoding()); if (failed(torch_to_linalg::broadcastToGivenShape( op, rewriter, rhs, rhsBroadcastToShape, rhsBroadcastType, broadcastedRhs))) { @@ -880,7 +880,7 @@ class ConvertAtenConvolutionOp : public OpConversionPattern { if(numSpacialDims != 2) return rewriter.notifyMatchFailure( op, "unimplemented: only 2D grouped convolution supported"); - + // Special depthwise case auto inShape = makeShapeTorchCompatible( input.getType().cast().getShape()); @@ -894,6 +894,7 @@ class ConvertAtenConvolutionOp : public OpConversionPattern { (weightShape[0] == kUnknownSize ? kUnknownSize : weightShape[0] * weightShape[1]), weightShape[2], weightShape[3]}; + // TODO: audit possibility of sparsity on this tensor Type collapsedType = RankedTensorType::get( makeShapeLLVMCompatible(collapsedShape), elementType); Value collapsedWeight = rewriter.create( diff --git a/lib/Conversion/TorchToLinalg/Utils.cpp b/lib/Conversion/TorchToLinalg/Utils.cpp index 77459aca3a60..8bff5034c6b4 100644 --- a/lib/Conversion/TorchToLinalg/Utils.cpp +++ b/lib/Conversion/TorchToLinalg/Utils.cpp @@ -87,6 +87,7 @@ Value torch_to_linalg::getDynamicZeroPaddedTensor( *pad = castIntToIndex(b, loc, *pad); Type elementType = input.getType().cast().getElementType(); + // TODO: audit possibility of sparsity on this tensor Type inputType = RankedTensorType::get(makeShapeLLVMCompatible(llvm::ArrayRef( SmallVector(inRank, kUnknownSize))), diff --git a/lib/Dialect/Torch/IR/TorchTypes.cpp b/lib/Dialect/Torch/IR/TorchTypes.cpp index b5b63954fe42..a154fb4653c4 100644 --- a/lib/Dialect/Torch/IR/TorchTypes.cpp +++ b/lib/Dialect/Torch/IR/TorchTypes.cpp @@ -467,7 +467,8 @@ TensorType ValueTensorType::toBuiltinTensor() const { Type elementType = convertDtypeToBuiltinElementType(getContext(), getDtype()); if (!elementType) return nullptr; - return RankedTensorType::get(makeShapeLLVMCompatible(getSizes()), elementType); + return RankedTensorType::get(makeShapeLLVMCompatible(getSizes()), elementType, + getOptionalSparsity()); } LogicalResult diff --git a/test/Conversion/TorchToLinalg/sparse.mlir b/test/Conversion/TorchToLinalg/sparse.mlir new file mode 100644 index 000000000000..5d952fde3509 --- /dev/null +++ b/test/Conversion/TorchToLinalg/sparse.mlir @@ -0,0 +1,36 @@ +// RUN: torch-mlir-opt <%s -convert-torch-to-linalg -split-input-file -verify-diagnostics | FileCheck %s + +// ----- + +#CSR = #sparse_tensor.encoding<{ map = (d0, d1) -> (d0 : dense, d1 : compressed) }> + +// CHECK: #[[$CSR:.*]] = #sparse_tensor.encoding<{ map = (d0, d1) -> (d0 : dense, d1 : compressed) }> +// CHECK-LABEL: func.func @sum( +// CHECK-SAME: %[[A:.*]]: !torch.vtensor<[64,64],f32,#[[$CSR]]>) -> !torch.vtensor<[],f32> +// CHECK: %[[S:.*]] = torch_c.to_builtin_tensor %[[A]] : !torch.vtensor<[64,64],f32,#[[$CSR]]> -> tensor<64x64xf32, #[[$CSR]]> +// CHECK: linalg.generic {{{.*}}} ins(%[[S]] : tensor<64x64xf32, #[[$CSR]]>) +func.func @sum(%arg0: !torch.vtensor<[64,64],f32,#CSR>) -> !torch.vtensor<[],f32> { + %none = torch.constant.none + %0 = torch.aten.sum %arg0, %none + : !torch.vtensor<[64,64],f32,#CSR>, !torch.none -> !torch.vtensor<[],f32> + return %0 : !torch.vtensor<[],f32> +} + +// ----- + +#CSR = #sparse_tensor.encoding<{ map = (d0, d1) -> (d0 : dense, d1 : compressed) }> + +// CHECK: #[[$CSR:.*]] = #sparse_tensor.encoding<{ map = (d0, d1) -> (d0 : dense, d1 : compressed) }> +// CHECK-LABEL: func.func @SpMM( +// CHECK-SAME: %[[A:.*]]: !torch.vtensor<[8,16],f32,#[[$CSR]]>, +// CHECK-SAME: %[[B:.*]]: !torch.vtensor<[16,8],f32>) -> !torch.vtensor<[8,8],f32> +// CHECK: %[[S:.*]] = torch_c.to_builtin_tensor %[[A]] : !torch.vtensor<[8,16],f32,#[[$CSR]]> -> tensor<8x16xf32, #[[$CSR]]> +// CHECK: %[[T:.*]] = torch_c.to_builtin_tensor %[[B]] : !torch.vtensor<[16,8],f32> -> tensor<16x8xf32> +// CHECK: linalg.matmul ins(%[[S]], %[[T]] : tensor<8x16xf32, #[[$CSR]]>, tensor<16x8xf32>) +func.func @SpMM(%arg0: !torch.vtensor<[8,16],f32,#CSR>, + %arg1: !torch.vtensor<[16,8],f32>) -> !torch.vtensor<[8,8],f32> { + %0 = torch.aten.matmul %arg0, %arg1 + : !torch.vtensor<[8,16],f32,#CSR>, + !torch.vtensor<[16,8],f32> -> !torch.vtensor<[8,8],f32> + return %0 : !torch.vtensor<[8,8],f32> +}