Skip to content
This repository has been archived by the owner on Jun 19, 2024. It is now read-only.

Commit

Permalink
Decompose torch.ones/zeros (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanyo Kwok authored Jan 10, 2023
1 parent 0f88ea9 commit 9537390
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/Dialect/Torch/Transforms/DecomposeComplexOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2363,6 +2363,24 @@ class DecomposeConstantTensorNewLikeOp : public OpRewritePattern<OpTy> {
};
} // namespace

namespace {
// Decompose constant tensor like ops.
template <typename OpTy, typename NewOpTy, int val>
class DecomposeConstantTensorOp : public OpRewritePattern<OpTy> {
using OpRewritePattern<OpTy>::OpRewritePattern;
LogicalResult matchAndRewrite(OpTy op,
PatternRewriter &rewriter) const override {
Type resultDtype = op.getType().template cast<BaseTensorType>().getDtype();
Value fillVal = getConstantWithGivenDtypeAndValue(rewriter, op.getLoc(),
val, resultDtype);
rewriter.replaceOpWithNewOp<NewOpTy>(op, op.getType(), op.size(), fillVal,
op.dtype(), op.layout(), op.device(),
op.pin_memory());
return success();
}
};
} // namespace

namespace {
// Decompose `aten.full` op into `aten.broadcast_to`
class DecomposeAtenFullOp : public OpRewritePattern<AtenFullOp> {
Expand Down Expand Up @@ -3546,6 +3564,11 @@ class DecomposeComplexOpsPass
patterns.add<DecomposeConstantTensorNewLikeOp<AtenNewOnesOp, AtenOnesOp>>(
context);
target.addIllegalOp<AtenNewOnesOp>();
patterns.add<DecomposeConstantTensorOp<AtenZerosOp, AtenFullOp, 0>>(
context);
target.addIllegalOp<AtenZerosOp>();
patterns.add<DecomposeConstantTensorOp<AtenOnesOp, AtenFullOp, 1>>(context);
target.addIllegalOp<AtenOnesOp>();
patterns.add<DecomposeAtenHardtanhOp>(context);
target.addIllegalOp<AtenHardtanhOp>();
patterns.add<DecomposeAtenFullOp>(context);
Expand Down

0 comments on commit 9537390

Please sign in to comment.