Skip to content

Commit

Permalink
Merge pull request #48050 from sjarus:delete_resize
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 364912256
Change-Id: Id34b6ef0341403366671e691a9ff2c3d6f444b79
  • Loading branch information
tensorflower-gardener committed Mar 24, 2021
2 parents 03ace1f + 910a9ce commit 94dafb8
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 149 deletions.
65 changes: 0 additions & 65 deletions tensorflow/compiler/mlir/tosa/transforms/legalize_common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2577,71 +2577,6 @@ llvm::Optional<Value> convertReduceMeanOp(
return val;
}

// Lowers ResizeBilinear and ResizeNearestNeighbor to TOSA resize.
llvm::Optional<Value> convertResizeOp(PatternRewriter& rewriter, Operation* op,
RankedTensorType output_type,
Value input_value, StringRef mode) {
RankedTensorType input_type =
input_value.getType().dyn_cast<RankedTensorType>();
if (!input_type) return llvm::None;

auto input_shape = input_type.getShape();
auto output_shape = output_type.getShape();

size_t input_height = input_shape[1];
size_t input_width = input_shape[2];
size_t output_height = output_shape[1];
size_t output_width = output_shape[2];

bool input_is_qtype =
input_type.getElementType().isa<mlir::quant::UniformQuantizedType>();
bool output_is_qtype =
output_type.getElementType().isa<mlir::quant::UniformQuantizedType>();

if (input_is_qtype != output_is_qtype) {
op->emitOpError(
"ConvertResizeOp: input/output tensor should "
"be all quantized or all floating-point.");
return llvm::None;
}

if (!input_is_qtype) {
// TODO: support float type
op->emitOpError("ConvertResizeOp: floating-point type not supported yet ");
return llvm::None;
}

int32_t shift = 11; // Set default shift to maximum allowed

double frac_y =
static_cast<double>(output_height) / static_cast<double>(input_height);
double frac_x =
static_cast<double>(output_width) / static_cast<double>(input_width);
int32_t stride_y = std::lround(frac_y * static_cast<double>(1 << shift));
int32_t stride_x = std::lround(frac_x * static_cast<double>(1 << shift));

// Stride is int16
while (stride_y >= 32768 || stride_x >= 32768) {
shift--;
stride_y = std::lround(frac_y * static_cast<double>(1 << shift));
stride_x = std::lround(frac_x * static_cast<double>(1 << shift));
}

ArrayAttr output_size =
rewriter.getI64ArrayAttr({static_cast<int64_t>(output_height),
static_cast<int64_t>(output_width)});
ArrayAttr stride = rewriter.getI64ArrayAttr({stride_y, stride_x});
ArrayAttr offset = rewriter.getI64ArrayAttr({0, 0});
IntegerAttr shift_attr = rewriter.getI32IntegerAttr(shift);
StringAttr resize_mode = rewriter.getStringAttr(mode.str());

return rewriter
.create<tosa::ResizeOp>(op->getLoc(), output_type, input_value,
output_size, stride, offset, shift_attr,
resize_mode)
.getResult();
}

// Lowers Quantize to a sequence of TOSA quantization ops.
llvm::Optional<Value> convertQuantizeOp(PatternRewriter& rewriter,
Operation* op,
Expand Down
42 changes: 0 additions & 42 deletions tensorflow/compiler/mlir/tosa/transforms/legalize_tf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,6 @@ DECL_CONVERT_OP(StridedSlice);
DECL_CONVERT_OP(Less);
DECL_CONVERT_OP(LessEqual);
DECL_CONVERT_OP(Pad);
DECL_CONVERT_OP(ResizeBilinear);
DECL_CONVERT_OP(ResizeNearestNeighbor);
DECL_CONVERT_OP(Gather);
DECL_CONVERT_OP(GatherV2);
DECL_CONVERT_OP(SelectV2);
Expand Down Expand Up @@ -1657,44 +1655,6 @@ LogicalResult ConvertTFPadOp::matchAndRewrite(Operation* op,
return success();
}

LogicalResult ConvertTFResizeBilinearOp::matchAndRewrite(
Operation* op, PatternRewriter& rewriter) const {
auto tf_resize_op = cast<TF::ResizeBilinearOp>(op);

RankedTensorType output_type =
tf_resize_op.getResult().getType().dyn_cast<RankedTensorType>();
// Not a ranked tensor output
if (!output_type) return failure();

llvm::Optional<Value> result = convertResizeOp(
rewriter, op, output_type, tf_resize_op.images(), StringRef("BILINEAR"));

if (!result) return failure();

rewriter.replaceOp(op, {result.getValue()});

return success();
}

LogicalResult ConvertTFResizeNearestNeighborOp::matchAndRewrite(
Operation* op, PatternRewriter& rewriter) const {
auto tf_resize_op = cast<TF::ResizeNearestNeighborOp>(op);

RankedTensorType output_type =
tf_resize_op.getResult().getType().dyn_cast<RankedTensorType>();
// Not a ranked tensor output
if (!output_type) return failure();

llvm::Optional<Value> result = convertResizeOp(
rewriter, op, output_type, tf_resize_op.images(), StringRef("NEAREST"));

if (!result) return failure();

rewriter.replaceOp(op, {result.getValue()});

return success();
}

LogicalResult ConvertTFMatMulOp::matchAndRewrite(
Operation* op, PatternRewriter& rewriter) const {
auto tf_matmul_op = cast<TF::MatMulOp>(op);
Expand Down Expand Up @@ -2095,8 +2055,6 @@ void LegalizeTF::runOnFunction() {
patterns.insert<ConvertTFLessOp>(ctx);
patterns.insert<ConvertTFLessEqualOp>(ctx);
patterns.insert<ConvertTFPadOp>(ctx);
patterns.insert<ConvertTFResizeBilinearOp>(ctx);
patterns.insert<ConvertTFResizeNearestNeighborOp>(ctx);
patterns.insert<ConvertTFGatherOp>(ctx);
patterns.insert<ConvertTFGatherV2Op>(ctx);
patterns.insert<ConvertTFSelectV2Op>(ctx);
Expand Down
42 changes: 0 additions & 42 deletions tensorflow/compiler/mlir/tosa/transforms/legalize_tfl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,6 @@ DECL_CONVERT_OP(ZerosLike);
DECL_CONVERT_OP(Less);
DECL_CONVERT_OP(LessEqual);
DECL_CONVERT_OP(Pad);
DECL_CONVERT_OP(ResizeBilinear);
DECL_CONVERT_OP(ResizeNearestNeighbor);
DECL_CONVERT_OP(Select);
DECL_CONVERT_OP(SelectV2);
DECL_CONVERT_OP(SpaceToBatchNd);
Expand Down Expand Up @@ -2269,44 +2267,6 @@ LogicalResult ConvertTFLPadOp::matchAndRewrite(
return success();
}

LogicalResult ConvertTFLResizeBilinearOp::matchAndRewrite(
Operation* op, PatternRewriter& rewriter) const {
auto tfl_resize_op = cast<TFL::ResizeBilinearOp>(op);

RankedTensorType output_type =
tfl_resize_op.getResult().getType().dyn_cast<RankedTensorType>();
// Not a ranked tensor output
if (!output_type) return failure();

llvm::Optional<Value> result = convertResizeOp(
rewriter, op, output_type, tfl_resize_op.input(), StringRef("BILINEAR"));

if (!result) return failure();

rewriter.replaceOp(op, {result.getValue()});

return success();
}

LogicalResult ConvertTFLResizeNearestNeighborOp::matchAndRewrite(
Operation* op, PatternRewriter& rewriter) const {
auto tfl_resize_op = cast<TFL::ResizeNearestNeighborOp>(op);

RankedTensorType output_type =
tfl_resize_op.getResult().getType().dyn_cast<RankedTensorType>();
// Not a ranked tensor output
if (!output_type) return failure();

llvm::Optional<Value> result = convertResizeOp(
rewriter, op, output_type, tfl_resize_op.input(), StringRef("NEAREST"));

if (!result) return failure();

rewriter.replaceOp(op, {result.getValue()});

return success();
}

LogicalResult ConvertTFLSelectOp::matchAndRewrite(
Operation* op, PatternRewriter& rewriter) const {
auto tfl_sel_op = cast<TFL::SelectOp>(op);
Expand Down Expand Up @@ -2944,8 +2904,6 @@ void LegalizeTFL::runOnFunction() {
DEF_PATTERN_INSERT(TFLLess);
DEF_PATTERN_INSERT(TFLLessEqual);
DEF_PATTERN_INSERT(TFLPad);
DEF_PATTERN_INSERT(TFLResizeBilinear);
DEF_PATTERN_INSERT(TFLResizeNearestNeighbor);
DEF_PATTERN_INSERT(TFLSelect);
DEF_PATTERN_INSERT(TFLSelectV2);
DEF_PATTERN_INSERT(TFLSpaceToBatchNd);
Expand Down

0 comments on commit 94dafb8

Please sign in to comment.