-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
[Relay/TOPI][TFLite] Implemented MATRIX_SET_DIAG Operator for Relay/TOPI and TFLite Frontend. #6303
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
# pylint: disable=invalid-name | ||
"""MatrixSetDiag in Python""" | ||
import numpy as np | ||
|
||
def matrix_set_diag(input_np, diagonal): | ||
"""matrix_set_diag operator implemented in numpy. | ||
|
||
Returns a numpy array with the diagonal of input array | ||
replaced with the provided diagonal values. | ||
|
||
Parameters | ||
---------- | ||
input : numpy.ndarray | ||
Input Array. | ||
Shape = [D1, D2, D3, ... , Dn-1 , Dn] | ||
diagonal : numpy.ndarray | ||
Values to be filled in the diagonal. | ||
Shape = [D1, D2, D3, ... , Dn-1] | ||
|
||
Returns | ||
------- | ||
result : numpy.ndarray | ||
New Array with given diagonal values. | ||
Shape = [D1, D2, D3, ... , Dn-1 , Dn] | ||
""" | ||
out = np.array(input_np, copy=True) | ||
n = min(input_np.shape[-1], input_np.shape[-2]) | ||
for i in range(n): | ||
out[..., i, i] = diagonal[..., i] | ||
|
||
return out |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3093,5 +3093,55 @@ RELAY_REGISTER_OP("sparse_to_dense") | |
.set_attr<FInferCorrectLayout>("FInferCorrectLayout", ElemwiseArbitraryLayout) | ||
.set_attr<FTVMCompute>("FTVMCompute", SparseToDenseCompute); | ||
|
||
// relay.matrix_set_diag | ||
bool MatrixSetDiagRel(const Array<Type>& types, int num_inputs, const Attrs& attrs, | ||
const TypeReporter& reporter) { | ||
// `types` contains: [input, diagonal, result] | ||
CHECK_EQ(types.size(), 3); | ||
|
||
const auto* input = types[0].as<TensorTypeNode>(); | ||
CHECK(input); | ||
|
||
const auto* diagonal = types[1].as<TensorTypeNode>(); | ||
CHECK(diagonal); | ||
|
||
int d_ndims = diagonal->shape.size(); | ||
for (int i = 0; i < d_ndims - 1; i++) { | ||
reporter->AssertEQ(input->shape[i], diagonal->shape[i]); | ||
} | ||
auto min_dim = if_then_else(input->shape[d_ndims - 1] >= input->shape[d_ndims], | ||
input->shape[d_ndims], input->shape[d_ndims - 1]); | ||
Comment on lines
+3112
to
+3113
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for reviewing. |
||
reporter->Assert(diagonal->shape[d_ndims - 1] >= min_dim); | ||
|
||
reporter->Assign(types[2], TensorType(input->shape, input->dtype)); | ||
return true; | ||
} | ||
|
||
Array<te::Tensor> MatrixSetDiagCompute(const Attrs& attrs, const Array<te::Tensor>& inputs, | ||
const Type& out_type) { | ||
return Array<te::Tensor>{topi::matrix_set_diag(inputs[0], inputs[1])}; | ||
} | ||
|
||
Expr MakeMatrixSetDiag(Expr input, Expr diagonal) { | ||
static const Op& op = Op::Get("matrix_set_diag"); | ||
return Call(op, {input, diagonal}, Attrs(), {}); | ||
} | ||
|
||
TVM_REGISTER_GLOBAL("relay.op._make.matrix_set_diag").set_body_typed(MakeMatrixSetDiag); | ||
|
||
RELAY_REGISTER_OP("matrix_set_diag") | ||
.describe( | ||
R"code(Returns a tensor with the diagonal of input tensor replaced with the provided diagonal values. | ||
**input** Input tensor. | ||
**diagonal** Values to be filled in the diagonal. | ||
)code" TVM_ADD_FILELINE) | ||
.set_num_inputs(2) | ||
.add_argument("input", "Tensor", "Input Tensor.") | ||
.add_argument("diagonal", "Tensor", "Values to be filled in the diagonal.") | ||
.set_support_level(10) | ||
.add_type_rel("MatrixSetDiag", MatrixSetDiagRel) | ||
.set_attr<FTVMCompute>("FTVMCompute", MatrixSetDiagCompute) | ||
.set_attr<TOpPattern>("TOpPattern", kInjective); | ||
|
||
} // namespace relay | ||
} // namespace tvm |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2652,6 +2652,77 @@ def test_forward_reverse_v2(): | |
_test_reverse_v2((5, 6, 4, 2), np.array([2], dtype='int32'), dtype) | ||
|
||
|
||
####################################################################### | ||
# MATRIX_SET_DIAG | ||
# --------------- | ||
|
||
def _test_matrix_set_diag(input_shape, input_type, quantized=False): | ||
""" One iteration of MATRIX_SET_DIAG """ | ||
with tf.Graph().as_default(): | ||
diagonal_shape = list(input_shape[:-2]) | ||
diagonal_shape.append(min(input_shape[-2], input_shape[-1])) | ||
Comment on lines
+2662
to
+2663
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should the broadcasting case be tested here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TFLite MATRIX_SET_DIAG doesn't seem to be a broadcast operator. So, I'll change the registration to be injective. |
||
|
||
if quantized: | ||
# ignoring input_type as quantized requires uint8 | ||
input = np.random.uniform(0, 256, input_shape).astype('uint8') | ||
in_input = tf.placeholder(dtype='float32', shape=input.shape, name="input") | ||
inq_input = tf.quantization.fake_quant_with_min_max_args( | ||
in_input, | ||
min=-100, | ||
max=100, | ||
name="q_input") | ||
|
||
diagonal = np.random.uniform(0, 256, diagonal_shape).astype('uint8') | ||
in_diagonal = tf.placeholder(dtype='float32', shape=diagonal.shape, name="diagonal") | ||
inq_diagonal = tf.quantization.fake_quant_with_min_max_args( | ||
in_diagonal, | ||
min=-100, | ||
max=100, | ||
name="q_diagonal") | ||
|
||
input_range = {'q_input': (-100, 100), 'q_diagonal': (-100, 100)} | ||
|
||
out = array_ops.matrix_set_diag(inq_input, inq_diagonal) | ||
out = tf.quantization.fake_quant_with_min_max_args( | ||
out, | ||
min=-100, | ||
max=100, | ||
name="out") | ||
|
||
compare_tflite_with_tvm( | ||
[input, diagonal], | ||
["q_input", "q_diagonal"], | ||
[inq_input, inq_diagonal], | ||
[out], | ||
quantized=True, | ||
input_range=input_range) | ||
else: | ||
input = np.random.uniform(0, 100, input_shape).astype(input_type) | ||
diagonal = np.random.uniform(0, 100, diagonal_shape).astype(input_type) | ||
|
||
in_input = tf.placeholder(dtype=input.dtype, shape=input.shape, name="input") | ||
in_diagonal = tf.placeholder(dtype=diagonal.dtype, shape=diagonal.shape, name="diagonal") | ||
|
||
out = array_ops.matrix_set_diag(in_input, in_diagonal) | ||
|
||
compare_tflite_with_tvm( | ||
[input, diagonal], | ||
["input", "diagonal"], | ||
[in_input, in_diagonal], | ||
[out]) | ||
|
||
def test_forward_matrix_set_diag(): | ||
""" MATRIX_SET_DIAG """ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add a pkg version check > '1.14.0' There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The API docs seem to suggest that matrix_set_diag is present even in version '1.0'. |
||
for dtype in [np.float32, np.int32]: | ||
_test_matrix_set_diag((4, 4), dtype) | ||
_test_matrix_set_diag((5, 4, 3, 4), dtype) | ||
_test_matrix_set_diag((4, 4, 2), dtype) | ||
|
||
_test_matrix_set_diag((4, 4), np.uint8, quantized=True) | ||
_test_matrix_set_diag((5, 4, 3, 4), np.uint8, quantized=True) | ||
_test_matrix_set_diag((4, 4, 2), np.uint8, quantized=True) | ||
|
||
|
||
####################################################################### | ||
# Custom Operators | ||
# ---------------- | ||
|
@@ -3131,6 +3202,7 @@ def test_forward_mediapipe_hand_landmark(): | |
test_forward_arg_min_max() | ||
test_forward_expand_dims() | ||
test_forward_reverse_v2() | ||
test_forward_matrix_set_diag() | ||
|
||
# NN | ||
test_forward_convolution() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A suggestion:- may be if we can support
alignment
andk
(offset) similar toMatrixSetDiagV3
in tf, it will be good. we can support directly for tensorflow ops as well.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That might take some time.
Would it be fine to have that in a follow-up PR?