You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by GitBox <gi...@apache.org> on 2022/01/19 18:18:04 UTC

[GitHub] [tvm] sfvaroglu opened a new pull request #9982: Add qnn.rsqrt op

sfvaroglu opened a new pull request #9982:
URL: https://github.com/apache/tvm/pull/9982


   Add QNN op for reciprocal sqrt, the default legalization goes through dequantize->rsqrt->quantize 
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] AndrewZhaoLuo commented on a change in pull request #9982: [QNN] Add qnn.rsqrt op

Posted by GitBox <gi...@apache.org>.
AndrewZhaoLuo commented on a change in pull request #9982:
URL: https://github.com/apache/tvm/pull/9982#discussion_r788171438



##########
File path: tests/python/relay/test_pass_fake_quantization_to_integer.py
##########
@@ -303,6 +303,19 @@ def test_fake_quantize_global_avg_pool():
     compare_fq_to_int(op, [x_np], True)
 
 
+def test_fake_quantize_rsqrt():
+    x = relay.var("x", shape=[1, 3, 224, 224], dtype="int8")
+    zero = relay.const(0)
+
+    x = relay.qnn.op.dequantize(x, relay.const(2.0), zero)
+    op = relay.rsqrt(x)
+    op = relay.qnn.op.quantize(op, relay.const(2.0), zero)
+
+    x_np = np.random.randint(-128, 127, size=[1, 3, 224, 224], dtype="int8")
+
+    compare_fq_to_int(op, [x_np], True)

Review comment:
       Oh yeah, this is canonicalized later makes sense




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] AndrewZhaoLuo commented on pull request #9982: [QNN] Add qnn.rsqrt op

Posted by GitBox <gi...@apache.org>.
AndrewZhaoLuo commented on pull request #9982:
URL: https://github.com/apache/tvm/pull/9982#issuecomment-1017951562


   Probably just need to rebase on main after https://github.com/apache/tvm/commit/589fc01026dce43cc096f458972b4c0ba8bf2577 
   
   The CI failure does not seem to be the fault of the PR


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] AndrewZhaoLuo commented on a change in pull request #9982: [QNN] Add qnn.rsqrt op

Posted by GitBox <gi...@apache.org>.
AndrewZhaoLuo commented on a change in pull request #9982:
URL: https://github.com/apache/tvm/pull/9982#discussion_r788130953



##########
File path: tests/python/relay/test_pass_fake_quantization_to_integer.py
##########
@@ -303,6 +303,19 @@ def test_fake_quantize_global_avg_pool():
     compare_fq_to_int(op, [x_np], True)
 
 
+def test_fake_quantize_rsqrt():
+    x = relay.var("x", shape=[1, 3, 224, 224], dtype="int8")
+    zero = relay.const(0)
+
+    x = relay.qnn.op.dequantize(x, relay.const(2.0), zero)
+    op = relay.rsqrt(x)
+    op = relay.qnn.op.quantize(op, relay.const(2.0), zero)
+
+    x_np = np.random.randint(-128, 127, size=[1, 3, 224, 224], dtype="int8")
+
+    compare_fq_to_int(op, [x_np], True)

Review comment:
       Huh, shouldn't this fail? since the default lowering matches the fake-quantized version and the comparison function makes sure they aren't the same structure




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] sfvaroglu commented on a change in pull request #9982: [QNN] Add qnn.rsqrt op

Posted by GitBox <gi...@apache.org>.
sfvaroglu commented on a change in pull request #9982:
URL: https://github.com/apache/tvm/pull/9982#discussion_r788154887



##########
File path: tests/python/relay/test_pass_fake_quantization_to_integer.py
##########
@@ -303,6 +303,19 @@ def test_fake_quantize_global_avg_pool():
     compare_fq_to_int(op, [x_np], True)
 
 
+def test_fake_quantize_rsqrt():
+    x = relay.var("x", shape=[1, 3, 224, 224], dtype="int8")
+    zero = relay.const(0)
+
+    x = relay.qnn.op.dequantize(x, relay.const(2.0), zero)
+    op = relay.rsqrt(x)
+    op = relay.qnn.op.quantize(op, relay.const(2.0), zero)
+
+    x_np = np.random.randint(-128, 127, size=[1, 3, 224, 224], dtype="int8")
+
+    compare_fq_to_int(op, [x_np], True)

Review comment:
       It lowers to qnn.rsqrt
   
   ```
   def @main(%x: Tensor[(1, 3, 224, 224), int8]) -> Tensor[(1, 3, 224, 224), int8] {
     %0 = qnn.dequantize(%x, 2f /* ty=float32 */, 0 /* ty=int32 */, Tensor[(1, 3, 224, 224), int8], float32, int32) /* ty=Tensor[(1, 3, 224, 224), float32] */;
     %1 = rsqrt(%0, Tensor[(1, 3, 224, 224), float32]) /* ty=Tensor[(1, 3, 224, 224), float32] */;
     qnn.quantize(%1, 2f /* ty=float32 */, 0 /* ty=int32 */, Tensor[(1, 3, 224, 224), float32], float32, int32, out_dtype="int8") /* ty=Tensor[(1, 3, 224, 224), int8] */
   }
   ```
   vs.
   ```
    def @main(%x: Tensor[(1, 3, 224, 224), int8]) -> Tensor[(1, 3, 224, 224), int8] {
     qnn.rsqrt(%x, 2f /* ty=float32 */, 0 /* ty=int32 */, 2f /* ty=float32 */, 0 /* ty=int32 */, Tensor[(1, 3, 224, 224), int8], float32, int32, float32, int32) /* ty=Tensor[(1, 3, 224, 224), int8] */
   }
   ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] AndrewZhaoLuo commented on a change in pull request #9982: [QNN] Add qnn.rsqrt op

Posted by GitBox <gi...@apache.org>.
AndrewZhaoLuo commented on a change in pull request #9982:
URL: https://github.com/apache/tvm/pull/9982#discussion_r788130953



##########
File path: tests/python/relay/test_pass_fake_quantization_to_integer.py
##########
@@ -303,6 +303,19 @@ def test_fake_quantize_global_avg_pool():
     compare_fq_to_int(op, [x_np], True)
 
 
+def test_fake_quantize_rsqrt():
+    x = relay.var("x", shape=[1, 3, 224, 224], dtype="int8")
+    zero = relay.const(0)
+
+    x = relay.qnn.op.dequantize(x, relay.const(2.0), zero)
+    op = relay.rsqrt(x)
+    op = relay.qnn.op.quantize(op, relay.const(2.0), zero)
+
+    x_np = np.random.randint(-128, 127, size=[1, 3, 224, 224], dtype="int8")
+
+    compare_fq_to_int(op, [x_np], True)

Review comment:
       Huh, shouldn't this fail? since the default lowering matches the fake-quantized version?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] AndrewZhaoLuo merged pull request #9982: [QNN] Add qnn.rsqrt op

Posted by GitBox <gi...@apache.org>.
AndrewZhaoLuo merged pull request #9982:
URL: https://github.com/apache/tvm/pull/9982


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] mbrookhart commented on a change in pull request #9982: Add qnn.rsqrt op

Posted by GitBox <gi...@apache.org>.
mbrookhart commented on a change in pull request #9982:
URL: https://github.com/apache/tvm/pull/9982#discussion_r788050306



##########
File path: src/relay/qnn/op/rsqrt.cc
##########
@@ -0,0 +1,121 @@
+/*
+ * 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.
+ */
+
+/*!
+ * \file src/relay/qnn/op/rsqrt.cc
+ * \brief QNN rsqrt operator.
+ */
+#include <tvm/relay/analysis.h>
+#include <tvm/relay/op_attr_types.h>
+
+#include "op_common.h"
+
+namespace tvm {
+namespace relay {
+namespace qnn {
+
+bool QnnRsqrtRel(const Array<Type>& types, int num_inputs, const Attrs& attrs,
+                 const TypeReporter& reporter) {
+  // Expected Types: data, scale, zero_point, output_scale, output_zero_point
+  ICHECK_EQ(types.size(), 6);
+  const auto* x = types[0].as<TensorTypeNode>();
+  if (x == nullptr) return false;
+  ICHECK(x->dtype == DataType::Int(8) || x->dtype == DataType::UInt(8))
+      << "Expected quantized rsqrt type(int8, uint8) for input but was " << x->dtype;
+
+  // Check the types of scale and zero points.
+  for (size_t i = 1; i < 5; ++i) {
+    if (types[i].as<IncompleteTypeNode>()) {
+      return false;
+    }
+  }
+  ICHECK(IsScalarType(types[1], DataType::Float(32)));  // scale
+  ICHECK(IsScalarType(types[2], DataType::Int(32)));    // zero_point
+  ICHECK(IsScalarType(types[3], DataType::Float(32)));  // output_scale
+  ICHECK(IsScalarType(types[4], DataType::Int(32)));    // output_zero_point
+
+  // Assign types for scale and zero points.
+  reporter->Assign(types[1], TensorType({}, DataType::Float(32)));  // scale
+  reporter->Assign(types[2], TensorType({}, DataType::Int(32)));    // zero_point
+  reporter->Assign(types[3], TensorType({}, DataType::Float(32)));  // output_scale
+  reporter->Assign(types[4], TensorType({}, DataType::Int(32)));    // output_zero_point
+
+  // Collect the input tensor and output tensor devoid of scale and zero points to reuse Relay
+  // IdentityRel infer type function.
+  Array<Type> tensor_types = {types[0], types[5]};
+  return IdentityRel(tensor_types, 2, attrs, reporter);
+}
+
+// Positional relay function to create quantized rsqrt operator used by frontend FFI.
+Expr MakeQuantizedRsqrt(Expr x, Expr scale, Expr zero_point, Expr output_scale,
+                        Expr output_zero_point) {
+  static const Op& op = Op::Get("qnn.rsqrt");
+  return Call(op, {x, scale, zero_point, output_scale, output_zero_point}, Attrs(), {});
+}
+
+/*
+ * \brief Canonicalizes the QNN rsqrt op.
+ * \param attrs The empty attribute.
+ * \param new_args The new mutated args to the call node.
+ * \param arg_types The types of input and output.
+ * \return The sequence of Relay ops for add op.
+ */
+Expr QnnRsqrtCanonicalize(const Attrs& attrs, const Array<Expr>& new_args,
+                          const Array<tvm::relay::Type>& arg_types) {

Review comment:
       Would you mind leaving a comment on the approach here and possible future methods to keep this in Integer?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] sfvaroglu commented on a change in pull request #9982: Add qnn.rsqrt op

Posted by GitBox <gi...@apache.org>.
sfvaroglu commented on a change in pull request #9982:
URL: https://github.com/apache/tvm/pull/9982#discussion_r788105128



##########
File path: src/relay/qnn/op/rsqrt.cc
##########
@@ -0,0 +1,121 @@
+/*
+ * 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.
+ */
+
+/*!
+ * \file src/relay/qnn/op/rsqrt.cc
+ * \brief QNN rsqrt operator.
+ */
+#include <tvm/relay/analysis.h>
+#include <tvm/relay/op_attr_types.h>
+
+#include "op_common.h"
+
+namespace tvm {
+namespace relay {
+namespace qnn {
+
+bool QnnRsqrtRel(const Array<Type>& types, int num_inputs, const Attrs& attrs,
+                 const TypeReporter& reporter) {
+  // Expected Types: data, scale, zero_point, output_scale, output_zero_point
+  ICHECK_EQ(types.size(), 6);
+  const auto* x = types[0].as<TensorTypeNode>();
+  if (x == nullptr) return false;
+  ICHECK(x->dtype == DataType::Int(8) || x->dtype == DataType::UInt(8))
+      << "Expected quantized rsqrt type(int8, uint8) for input but was " << x->dtype;
+
+  // Check the types of scale and zero points.
+  for (size_t i = 1; i < 5; ++i) {
+    if (types[i].as<IncompleteTypeNode>()) {
+      return false;
+    }
+  }
+  ICHECK(IsScalarType(types[1], DataType::Float(32)));  // scale
+  ICHECK(IsScalarType(types[2], DataType::Int(32)));    // zero_point
+  ICHECK(IsScalarType(types[3], DataType::Float(32)));  // output_scale
+  ICHECK(IsScalarType(types[4], DataType::Int(32)));    // output_zero_point
+
+  // Assign types for scale and zero points.
+  reporter->Assign(types[1], TensorType({}, DataType::Float(32)));  // scale
+  reporter->Assign(types[2], TensorType({}, DataType::Int(32)));    // zero_point
+  reporter->Assign(types[3], TensorType({}, DataType::Float(32)));  // output_scale
+  reporter->Assign(types[4], TensorType({}, DataType::Int(32)));    // output_zero_point
+
+  // Collect the input tensor and output tensor devoid of scale and zero points to reuse Relay
+  // IdentityRel infer type function.
+  Array<Type> tensor_types = {types[0], types[5]};
+  return IdentityRel(tensor_types, 2, attrs, reporter);
+}
+
+// Positional relay function to create quantized rsqrt operator used by frontend FFI.
+Expr MakeQuantizedRsqrt(Expr x, Expr scale, Expr zero_point, Expr output_scale,
+                        Expr output_zero_point) {
+  static const Op& op = Op::Get("qnn.rsqrt");
+  return Call(op, {x, scale, zero_point, output_scale, output_zero_point}, Attrs(), {});
+}
+
+/*
+ * \brief Canonicalizes the QNN rsqrt op.
+ * \param attrs The empty attribute.
+ * \param new_args The new mutated args to the call node.
+ * \param arg_types The types of input and output.
+ * \return The sequence of Relay ops for add op.
+ */
+Expr QnnRsqrtCanonicalize(const Attrs& attrs, const Array<Expr>& new_args,
+                          const Array<tvm::relay::Type>& arg_types) {

Review comment:
       Done




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [tvm] AndrewZhaoLuo merged pull request #9982: [QNN] Add qnn.rsqrt op

Posted by GitBox <gi...@apache.org>.
AndrewZhaoLuo merged pull request #9982:
URL: https://github.com/apache/tvm/pull/9982


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org