Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom builder to reduce op allowing type inference #1965

Merged
merged 3 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions stablehlo/dialect/StablehloOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1783,6 +1783,48 @@ LogicalResult ReduceOp::inferReturnTypeComponents(
inferredReturnShapes);
}

void ReduceOp::build(OpBuilder&, OperationState& odsState, ValueRange inputs,
ValueRange initValues, DenseI64ArrayAttr dimensions,
TypeRange elementTypes) {
odsState.addOperands(inputs);
odsState.addOperands(initValues);
odsState.addAttribute(getDimensionsAttrName(odsState.name), dimensions);
sdasgup3 marked this conversation as resolved.
Show resolved Hide resolved
(void)odsState.addRegion();

SmallVector<int64_t> newDimensions;
Attribute encoding;
ReduceOp::Adaptor adaptor(
odsState.operands,
odsState.attributes.getDictionary(odsState.getContext()), {},
odsState.regions);

SmallVector<ShapedType> inputArgTensorTypes{
llvm::map_range(adaptor.getInputs().getTypes(),
[](Type t) { return t.cast<ShapedType>(); })};
SmallVector<ShapedType> initValueTensorTypes{
llvm::map_range(adaptor.getInitValues().getTypes(),
[](Type t) { return t.cast<ShapedType>(); })};

if (failed(hlo::verifyReduceOpInputsAndInferShape(
odsState.location, inputArgTensorTypes, dimensions, newDimensions,
encoding)))
llvm::report_fatal_error("Failed to infer result type(s).");

SmallVector<Type> inferredReturnTypes;
for (auto [inputTy, elementTy] :
llvm::zip(inputArgTensorTypes, elementTypes)) {
if (inputTy.hasRank()) {
inferredReturnTypes.push_back(
RankedTensorType::get(newDimensions, elementTy, encoding));
} else {
if (encoding != nullptr)
llvm::report_fatal_error("attribute not supported.");
inferredReturnTypes.push_back(UnrankedTensorType::get(elementTy));
}
}
odsState.addTypes(inferredReturnTypes);
}

LogicalResult ReduceOp::verify() {
return hlo::verifyReduceOp(getLoc(), getInputs(), getInitValues(),
getDimensions(), getBody());
Expand Down
8 changes: 8 additions & 0 deletions stablehlo/dialect/StablehloOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -1476,6 +1476,14 @@ def StableHLO_ReduceOp: StableHLO_ShapedInterfaceOp<"reduce", [
);
let regions = (region SizedRegion<1>:$body /*reduce_i4*/);

// Builder
// The following custom builder allows inferring the operation type using the
// 'element_types' of the arguments of the 'body'.
let builders = [
OpBuilder<(ins "ValueRange":$inputs, "ValueRange":$init_values,
"DenseI64ArrayAttr":$dimensions, "TypeRange":$element_types)>,
];

let results = (outs Variadic<HLO_Tensor>);

let hasCustomAssemblyFormat = 1;
Expand Down
Loading