sunshinemyson commented on a change in pull request #9046:
URL: https://github.com/apache/tvm/pull/9046#discussion_r713973055



##########
File path: src/relay/backend/contrib/vsi_npu/codegen.cc
##########
@@ -0,0 +1,425 @@
+/*
+ * 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.
+ */
+#include "codegen_vsi_npu.h"
+
+#include "../../../../runtime/contrib/vsi_npu/vsi_npu_runtime.h"
+#include "../../utils.h"
+#include "../codegen_c/codegen_c.h"
+#include "op_map/op_setup.h"
+
+#include <tvm/relay/attrs/image.h>
+#include <tvm/relay/attrs/nn.h>
+#include <tvm/relay/attrs/reduce.h>
+#include <tvm/relay/expr_functor.h>
+#include <tvm/relay/transform.h>
+#include <tvm/relay/type.h>
+#include <tvm/runtime/module.h>
+#include <tvm/runtime/registry.h>
+
+#include <fstream>
+#include <iostream>
+#include <numeric>
+#include <cassert>
+#include <sstream>
+
+#include "tim/transform/layout_inference.h"
+
+namespace tvx = tim::vx;
+
+namespace tvm {
+namespace relay {
+namespace contrib {
+namespace vsi_npu {
+
+using TensorInfoTable = std::map<Expr, std::vector<tim::vx::TensorSpec>>;
+
+void quant_info_infer(VxOpTable& op_tb, Expr now_expr, bool is_input) {
+  auto now_opsetup = op_tb[now_expr];
+  Expr pre_expr;
+  if ((now_opsetup->pCallbackexpr_ == nullptr ||
+      now_opsetup->pCallbackexpr_->ptr_pre_callback_ == nullptr) && is_input
+      ) {
+    return;
+  } else if((now_opsetup->pCallbackexpr_ == nullptr ||
+      now_opsetup->pCallbackexpr_->ptr_pre_callback_ == nullptr
+      || op_tb[now_expr]->specs_[0].quantization_.ZeroPoints().size() == 0)&& 
!is_input ){
+      return;
+  } else {
+    pre_expr = now_opsetup->pCallbackexpr_->ptr_pre_callback_->expr_;
+  }
+
+  auto pre_opsetup = op_tb[pre_expr];
+  auto ptr_callback = pre_opsetup->pCallbackexpr_;
+
+  if (now_opsetup->specs_[0].datatype_ == tvx::DataType::FLOAT32 ||
+      pre_opsetup->specs_[0].datatype_ == tvx::DataType::FLOAT32 ||
+      now_opsetup->specs_[0].datatype_ == tvx::DataType::BOOL8 ||
+      pre_opsetup->specs_[0].datatype_ == tvx::DataType::BOOL8) {
+    return;
+  }
+
+  tvx::Quantization& now_quant_info = now_opsetup->specs_[0].quantization_;
+
+  std::vector<int32_t> zps;
+  std::vector<float> scales;
+  if (now_quant_info.Type() == tvx::QuantType::NONE) {
+    zps = {0};
+    scales = {1.0};
+    
now_quant_info.SetType(tvx::QuantType::ASYMMETRIC).SetScales({1.0}).SetZeroPoints({0});
+  } else {
+    zps = now_quant_info.ZeroPoints();
+    scales = now_quant_info.Scales();
+  }
+
+  while (ptr_callback &&
+         
op_tb[ptr_callback->expr_]->specs_[0].quantization_.ZeroPoints().size() == 0) {
+    Expr expr = ptr_callback->expr_;
+    auto datatype = 
GetTvxType(expr->checked_type().as<TensorTypeNode>()->dtype);
+    if (datatype != tim::vx::DataType::INT32) {
+      op_tb[expr]
+          ->specs_[0]
+          .quantization_.SetType(tvx::QuantType::ASYMMETRIC)
+          .SetScales(scales)
+          .SetZeroPoints(zps);
+    }
+    ptr_callback = ptr_callback->ptr_pre_callback_;
+  }
+}
+
+template <typename T, typename T2>
+void attribute_transform(const T &attrs, T2 &attrs_num) {
+
+  std::transform(attrs.begin(), attrs.end(), attrs_num.begin(),
+                 [](const PrimExpr &attrs_num) {
+                   return static_cast<uint32_t>(
+                       attrs_num.as<IntImmNode>()->value);
+                 });
+};
+
+std::shared_ptr<tvx::Tensor> createVxOPerand(TensorInfoTable tensor_info,

Review comment:
       Will fix.

##########
File path: src/relay/backend/contrib/vsi_npu/codegen.cc
##########
@@ -0,0 +1,425 @@
+/*
+ * 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.
+ */
+#include "codegen_vsi_npu.h"
+
+#include "../../../../runtime/contrib/vsi_npu/vsi_npu_runtime.h"
+#include "../../utils.h"
+#include "../codegen_c/codegen_c.h"
+#include "op_map/op_setup.h"
+
+#include <tvm/relay/attrs/image.h>
+#include <tvm/relay/attrs/nn.h>
+#include <tvm/relay/attrs/reduce.h>
+#include <tvm/relay/expr_functor.h>
+#include <tvm/relay/transform.h>
+#include <tvm/relay/type.h>
+#include <tvm/runtime/module.h>
+#include <tvm/runtime/registry.h>
+
+#include <fstream>
+#include <iostream>
+#include <numeric>
+#include <cassert>
+#include <sstream>
+
+#include "tim/transform/layout_inference.h"
+
+namespace tvx = tim::vx;
+
+namespace tvm {
+namespace relay {
+namespace contrib {
+namespace vsi_npu {
+
+using TensorInfoTable = std::map<Expr, std::vector<tim::vx::TensorSpec>>;
+
+void quant_info_infer(VxOpTable& op_tb, Expr now_expr, bool is_input) {
+  auto now_opsetup = op_tb[now_expr];
+  Expr pre_expr;
+  if ((now_opsetup->pCallbackexpr_ == nullptr ||
+      now_opsetup->pCallbackexpr_->ptr_pre_callback_ == nullptr) && is_input
+      ) {
+    return;
+  } else if((now_opsetup->pCallbackexpr_ == nullptr ||
+      now_opsetup->pCallbackexpr_->ptr_pre_callback_ == nullptr
+      || op_tb[now_expr]->specs_[0].quantization_.ZeroPoints().size() == 0)&& 
!is_input ){
+      return;
+  } else {
+    pre_expr = now_opsetup->pCallbackexpr_->ptr_pre_callback_->expr_;
+  }
+
+  auto pre_opsetup = op_tb[pre_expr];
+  auto ptr_callback = pre_opsetup->pCallbackexpr_;
+
+  if (now_opsetup->specs_[0].datatype_ == tvx::DataType::FLOAT32 ||
+      pre_opsetup->specs_[0].datatype_ == tvx::DataType::FLOAT32 ||
+      now_opsetup->specs_[0].datatype_ == tvx::DataType::BOOL8 ||
+      pre_opsetup->specs_[0].datatype_ == tvx::DataType::BOOL8) {
+    return;
+  }
+
+  tvx::Quantization& now_quant_info = now_opsetup->specs_[0].quantization_;
+
+  std::vector<int32_t> zps;
+  std::vector<float> scales;
+  if (now_quant_info.Type() == tvx::QuantType::NONE) {
+    zps = {0};
+    scales = {1.0};
+    
now_quant_info.SetType(tvx::QuantType::ASYMMETRIC).SetScales({1.0}).SetZeroPoints({0});
+  } else {
+    zps = now_quant_info.ZeroPoints();
+    scales = now_quant_info.Scales();
+  }
+
+  while (ptr_callback &&
+         
op_tb[ptr_callback->expr_]->specs_[0].quantization_.ZeroPoints().size() == 0) {
+    Expr expr = ptr_callback->expr_;
+    auto datatype = 
GetTvxType(expr->checked_type().as<TensorTypeNode>()->dtype);
+    if (datatype != tim::vx::DataType::INT32) {
+      op_tb[expr]
+          ->specs_[0]
+          .quantization_.SetType(tvx::QuantType::ASYMMETRIC)
+          .SetScales(scales)
+          .SetZeroPoints(zps);
+    }
+    ptr_callback = ptr_callback->ptr_pre_callback_;
+  }
+}
+
+template <typename T, typename T2>
+void attribute_transform(const T &attrs, T2 &attrs_num) {
+
+  std::transform(attrs.begin(), attrs.end(), attrs_num.begin(),
+                 [](const PrimExpr &attrs_num) {
+                   return static_cast<uint32_t>(
+                       attrs_num.as<IntImmNode>()->value);
+                 });
+};
+
+std::shared_ptr<tvx::Tensor> createVxOPerand(TensorInfoTable tensor_info,
+                                             Expr expr, tvx::Graph *graph,
+                                             uint32_t idx = 0) {
+  auto tensor_spec = tensor_info[expr][idx];
+  void *data = expr->IsInstance<ConstantNode>()
+                   ? expr.as<ConstantNode>()->data->data
+                   : nullptr;
+  return data == nullptr ? graph->CreateTensor(tensor_spec)
+                         : graph->CreateTensor(tensor_spec, data);
+};
+
+static std::vector<tim::vx::TensorSpec>
+GetTimVxTensorSpec(const TupleTypeNode *tuple) {
+  auto input_node_tensors = tuple->fields;
+
+  std::vector<tim::vx::TensorSpec> specs;
+  uint32_t input_node_num = input_node_tensors.size();
+  for (uint32_t i = 0; i < input_node_num; i++) {
+    std::cout << "GetTimVxTensorSpec: " << 
input_node_tensors[i].as<TensorTypeNode>() << std::endl;
+    tim::vx::ShapeType shape;
+    std::transform(input_node_tensors[i].as<TensorTypeNode>()->shape.rbegin(),
+                   input_node_tensors[i].as<TensorTypeNode>()->shape.rend(),
+                   std::back_inserter(shape), [](const PrimExpr &dim) {
+                     return static_cast<int>(dim.as<IntImmNode>()->value);
+                   });
+
+    auto dtype = input_node_tensors[i].as<TensorTypeNode>()->dtype;
+    auto dataType = GetTvxType(dtype);
+
+    tim::vx::TensorSpec spec(dataType, shape,
+                             tim::vx::TensorAttribute::OUTPUT);
+    specs.push_back(spec);
+  }
+  return specs;
+}
+
+using namespace backend;

Review comment:
       Will fix.




-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to