mirror of https://github.com/llvm/torch-mlir
[Torch] add support for aten.scatter_add (#3534)
parent
0fb8b017d8
commit
5e4f00acb1
|
@ -373,16 +373,19 @@ static FailureOr<SmallVector<Value>> createTMTensorTopkOp(
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
class ConvertAtenScatterSrcOp : public OpConversionPattern<AtenScatterSrcOp> {
|
template <typename AtenOpT>
|
||||||
|
class ConvertAtenScatterOp : public OpConversionPattern<AtenOpT> {
|
||||||
public:
|
public:
|
||||||
using OpConversionPattern::OpConversionPattern;
|
using OpConversionPattern<AtenOpT>::OpConversionPattern;
|
||||||
|
using OpAdaptor = typename AtenOpT::Adaptor;
|
||||||
LogicalResult
|
LogicalResult
|
||||||
matchAndRewrite(AtenScatterSrcOp op, OpAdaptor adaptor,
|
matchAndRewrite(AtenOpT op, OpAdaptor adaptor,
|
||||||
ConversionPatternRewriter &rewriter) const override {
|
ConversionPatternRewriter &rewriter) const override {
|
||||||
if (failed(verifyLinalgCompatibleTypes(op, rewriter)))
|
if (failed(verifyLinalgCompatibleTypes(op, rewriter)))
|
||||||
return failure();
|
return failure();
|
||||||
Location loc = op.getLoc();
|
Location loc = op.getLoc();
|
||||||
const TypeConverter *typeConverter = getTypeConverter();
|
const TypeConverter *typeConverter =
|
||||||
|
OpConversionPattern<AtenOpT>::getTypeConverter();
|
||||||
Value self = adaptor.getSelf();
|
Value self = adaptor.getSelf();
|
||||||
Value index = adaptor.getIndex();
|
Value index = adaptor.getIndex();
|
||||||
Value src = adaptor.getSrc();
|
Value src = adaptor.getSrc();
|
||||||
|
@ -410,7 +413,19 @@ public:
|
||||||
/*dimensionsMap=*/createDefaultDimMap(indices), /*uniqueIndices=*/false,
|
/*dimensionsMap=*/createDefaultDimMap(indices), /*uniqueIndices=*/false,
|
||||||
[&](OpBuilder &b, Location loc, Value updatesElement,
|
[&](OpBuilder &b, Location loc, Value updatesElement,
|
||||||
Value inputElement) {
|
Value inputElement) {
|
||||||
b.create<TMTensor::YieldOp>(loc, updatesElement);
|
if (isa<AtenScatterSrcOp>(op)) {
|
||||||
|
b.create<TMTensor::YieldOp>(loc, updatesElement);
|
||||||
|
} else if (isa<AtenScatterAddOp>(op)) {
|
||||||
|
if (isa<mlir::IntegerType>(selfType.getElementType())) {
|
||||||
|
Value add =
|
||||||
|
b.create<arith::AddIOp>(loc, inputElement, updatesElement);
|
||||||
|
b.create<TMTensor::YieldOp>(loc, add);
|
||||||
|
} else if (isa<mlir::FloatType>(selfType.getElementType())) {
|
||||||
|
Value add =
|
||||||
|
b.create<arith::AddFOp>(loc, inputElement, updatesElement);
|
||||||
|
b.create<TMTensor::YieldOp>(loc, add);
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
auto resultType = cast<RankedTensorType>(
|
auto resultType = cast<RankedTensorType>(
|
||||||
|
@ -2169,7 +2184,11 @@ public:
|
||||||
context);
|
context);
|
||||||
|
|
||||||
target.addIllegalOp<AtenScatterSrcOp>();
|
target.addIllegalOp<AtenScatterSrcOp>();
|
||||||
patterns.add<ConvertAtenScatterSrcOp>(typeConverter, context);
|
patterns.add<ConvertAtenScatterOp<AtenScatterSrcOp>>(typeConverter,
|
||||||
|
context);
|
||||||
|
target.addIllegalOp<AtenScatterAddOp>();
|
||||||
|
patterns.add<ConvertAtenScatterOp<AtenScatterAddOp>>(typeConverter,
|
||||||
|
context);
|
||||||
target.addIllegalOp<AtenKthvalueOp>();
|
target.addIllegalOp<AtenKthvalueOp>();
|
||||||
patterns.add<ConvertAtenKthvalueOp>(typeConverter, context);
|
patterns.add<ConvertAtenKthvalueOp>(typeConverter, context);
|
||||||
|
|
||||||
|
|
|
@ -9787,6 +9787,9 @@ StringRef mlir::torch::Torch::getAbstractInterpLibrary() {
|
||||||
" func.func @\"__torch_mlir_shape_fn.aten.scatter.value\"(%arg0: !torch.list<int>, %arg1: !torch.int, %arg2: !torch.list<int>, %arg3: !torch.float) -> !torch.list<int> {\n"
|
" func.func @\"__torch_mlir_shape_fn.aten.scatter.value\"(%arg0: !torch.list<int>, %arg1: !torch.int, %arg2: !torch.list<int>, %arg3: !torch.float) -> !torch.list<int> {\n"
|
||||||
" return %arg0 : !torch.list<int>\n"
|
" return %arg0 : !torch.list<int>\n"
|
||||||
" }\n"
|
" }\n"
|
||||||
|
" func.func @\"__torch_mlir_shape_fn.aten.scatter_add\"(%arg0: !torch.list<int>, %arg1: !torch.int, %arg2: !torch.list<int>, %arg3: !torch.list<int>) -> !torch.list<int> {\n"
|
||||||
|
" return %arg0 : !torch.list<int>\n"
|
||||||
|
" }\n"
|
||||||
" func.func @\"__torch_mlir_shape_fn.aten.index_select\"(%arg0: !torch.list<int>, %arg1: !torch.int, %arg2: !torch.list<int>) -> !torch.list<int> {\n"
|
" func.func @\"__torch_mlir_shape_fn.aten.index_select\"(%arg0: !torch.list<int>, %arg1: !torch.int, %arg2: !torch.list<int>) -> !torch.list<int> {\n"
|
||||||
" %0 = call @__torch__.torch.jit._shape_functions.index_select(%arg0, %arg1, %arg2) : (!torch.list<int>, !torch.int, !torch.list<int>) -> !torch.list<int>\n"
|
" %0 = call @__torch__.torch.jit._shape_functions.index_select(%arg0, %arg1, %arg2) : (!torch.list<int>, !torch.int, !torch.list<int>) -> !torch.list<int>\n"
|
||||||
" return %0 : !torch.list<int>\n"
|
" return %0 : !torch.list<int>\n"
|
||||||
|
@ -11567,6 +11570,10 @@ StringRef mlir::torch::Torch::getAbstractInterpLibrary() {
|
||||||
" %0:2 = torch.prim.TupleUnpack %arg0 : !torch.tuple<int, int> -> !torch.int, !torch.int\n"
|
" %0:2 = torch.prim.TupleUnpack %arg0 : !torch.tuple<int, int> -> !torch.int, !torch.int\n"
|
||||||
" return %0#1 : !torch.int\n"
|
" return %0#1 : !torch.int\n"
|
||||||
" }\n"
|
" }\n"
|
||||||
|
" func.func @\"__torch_mlir_dtype_fn.aten.scatter_add\"(%arg0: !torch.tuple<int, int>, %arg1: !torch.int, %arg2: !torch.tuple<int, int>, %arg3: !torch.tuple<int, int>) -> !torch.int {\n"
|
||||||
|
" %0:2 = torch.prim.TupleUnpack %arg0 : !torch.tuple<int, int> -> !torch.int, !torch.int\n"
|
||||||
|
" return %0#1 : !torch.int\n"
|
||||||
|
" }\n"
|
||||||
" func.func @\"__torch_mlir_dtype_fn.aten.masked_scatter\"(%arg0: !torch.tuple<int, int>, %arg1: !torch.tuple<int, int>, %arg2: !torch.tuple<int, int>) -> !torch.int {\n"
|
" func.func @\"__torch_mlir_dtype_fn.aten.masked_scatter\"(%arg0: !torch.tuple<int, int>, %arg1: !torch.tuple<int, int>, %arg2: !torch.tuple<int, int>) -> !torch.int {\n"
|
||||||
" %0:2 = torch.prim.TupleUnpack %arg0 : !torch.tuple<int, int> -> !torch.int, !torch.int\n"
|
" %0:2 = torch.prim.TupleUnpack %arg0 : !torch.tuple<int, int> -> !torch.int, !torch.int\n"
|
||||||
" return %0#1 : !torch.int\n"
|
" return %0#1 : !torch.int\n"
|
||||||
|
|
|
@ -2682,6 +2682,7 @@ ONNX_XFAIL_SET = {
|
||||||
"ScatterReduceIntMaxModuleIncludeSelf",
|
"ScatterReduceIntMaxModuleIncludeSelf",
|
||||||
"ScatterReduceIntMinModuleIncludeSelf",
|
"ScatterReduceIntMinModuleIncludeSelf",
|
||||||
"ScatterValueFloatModule_basic",
|
"ScatterValueFloatModule_basic",
|
||||||
|
"ScatterAddStaticModule_basic",
|
||||||
# Failure - onnx_lowering: onnx.ScatterND
|
# Failure - onnx_lowering: onnx.ScatterND
|
||||||
"IndexPut1DFloatAccumulateModule_basic",
|
"IndexPut1DFloatAccumulateModule_basic",
|
||||||
"IndexPut1DIntAccumulateModule_basic",
|
"IndexPut1DIntAccumulateModule_basic",
|
||||||
|
|
|
@ -1810,6 +1810,9 @@ def aten〇scatter〇src〡shape(self: List[int], dim: int, index: List[int], sr
|
||||||
def aten〇scatter〇value〡shape(self: List[int], dim: int, index: List[int], value: float) -> List[int]:
|
def aten〇scatter〇value〡shape(self: List[int], dim: int, index: List[int], value: float) -> List[int]:
|
||||||
return self
|
return self
|
||||||
|
|
||||||
|
def aten〇scatter_add〡shape(self: List[int], dim: int, index: List[int], src: List[int]) -> List[int]:
|
||||||
|
return self
|
||||||
|
|
||||||
def aten〇index_select〡shape(self: List[int], dim: int, index: List[int]) -> List[int]:
|
def aten〇index_select〡shape(self: List[int], dim: int, index: List[int]) -> List[int]:
|
||||||
return upstream_shape_functions.index_select(self, dim, index)
|
return upstream_shape_functions.index_select(self, dim, index)
|
||||||
|
|
||||||
|
@ -3115,6 +3118,12 @@ def aten〇scatter〇value〡dtype(self_rank_dtype: Tuple[int, int], dim: int, i
|
||||||
self_rank, self_dtype = self_rank_dtype
|
self_rank, self_dtype = self_rank_dtype
|
||||||
return self_dtype
|
return self_dtype
|
||||||
|
|
||||||
|
@check_dtype_function(
|
||||||
|
[Invocation(TensorOfShape(3, dtype=dtype), 0, TensorOfShape(3, dtype=torch.int64), TensorOfShape(3, dtype=dtype)) for dtype in _SORTED_TORCH_TYPES])
|
||||||
|
def aten〇scatter_add〡dtype(self_rank_dtype: Tuple[int, int], dim: int, index_rank_dtype: Tuple[int, int], src_rank_dtype: Tuple[int, int]) -> int:
|
||||||
|
self_rank, self_dtype = self_rank_dtype
|
||||||
|
return self_dtype
|
||||||
|
|
||||||
@check_dtype_function(
|
@check_dtype_function(
|
||||||
[Invocation(TensorOfShape(3, dtype=dtype), TensorOfShape(3, dtype=torch.bool), TensorOfShape(3, dtype=dtype)) for dtype in _SORTED_TORCH_TYPES])
|
[Invocation(TensorOfShape(3, dtype=dtype), TensorOfShape(3, dtype=torch.bool), TensorOfShape(3, dtype=dtype)) for dtype in _SORTED_TORCH_TYPES])
|
||||||
def aten〇masked_scatter〡dtype(self_rank_dtype: Tuple[int, int], mask_rank_dtype: Tuple[int, int], source_rank_dtype: Tuple[int, int]) -> int:
|
def aten〇masked_scatter〡dtype(self_rank_dtype: Tuple[int, int], mask_rank_dtype: Tuple[int, int], source_rank_dtype: Tuple[int, int]) -> int:
|
||||||
|
|
|
@ -1020,6 +1020,31 @@ def ScatterValueIntModule_basic(module, tu: TestUtils):
|
||||||
# ==============================================================================
|
# ==============================================================================
|
||||||
|
|
||||||
|
|
||||||
|
class ScatterAddStaticModule(torch.nn.Module):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
|
||||||
|
@export
|
||||||
|
@annotate_args(
|
||||||
|
[
|
||||||
|
None,
|
||||||
|
([10, 8, 6], torch.float32, True),
|
||||||
|
([2, 4, 3], torch.int64, True),
|
||||||
|
([5, 8, 6], torch.float32, True),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
def forward(self, input, index, src):
|
||||||
|
return torch.ops.aten.scatter_add(input, 0, index, src)
|
||||||
|
|
||||||
|
|
||||||
|
@register_test_case(module_factory=lambda: ScatterAddStaticModule())
|
||||||
|
def ScatterAddStaticModule_basic(module, tu: TestUtils):
|
||||||
|
module.forward(tu.rand(10, 8, 6), tu.randint(2, 4, 3, high=4), tu.rand(5, 8, 6))
|
||||||
|
|
||||||
|
|
||||||
|
# ==============================================================================
|
||||||
|
|
||||||
|
|
||||||
class ScatterReduceFloatModule(torch.nn.Module):
|
class ScatterReduceFloatModule(torch.nn.Module):
|
||||||
include_self: bool
|
include_self: bool
|
||||||
reduce_type: str
|
reduce_type: str
|
||||||
|
|
Loading…
Reference in New Issue