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 2021/09/01 13:38:53 UTC

[GitHub] [tvm] areusch commented on a change in pull request #8833: [2/10] CMSIS-NN code generator for softmax

areusch commented on a change in pull request #8833:
URL: https://github.com/apache/tvm/pull/8833#discussion_r700222024



##########
File path: src/relay/backend/contrib/cmsisnn/codegen_cmsisnn.cc
##########
@@ -0,0 +1,205 @@
+/*
+ * 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 <cmath>
+#include <fstream>
+#include <map>
+#include <sstream>
+#include <string>
+#include <vector>
+
+#include "../../../../runtime/file_utils.h"
+#include "../../../../target/source/codegen_c.h"
+#include "../../../qnn/utils.h"
+
+namespace tvm {
+namespace runtime {
+
+using namespace tir;
+
+class CodeGenCMSISNN : public tvm::codegen::CodeGenC {
+ public:
+  void Init(bool output_ssa) {
+    decl_stream << "#include <stdio.h>\n";
+    decl_stream << "#include <stdlib.h>\n";
+    decl_stream << "#include <dlpack/dlpack.h>\n";
+    decl_stream << "#include <tvm/runtime/crt/module.h>\n";
+    decl_stream << "#include <arm_nnfunctions.h>\n";
+    CodeGenC::Init(output_ssa);
+  }
+
+  /*!
+   * \brief Emit code that offloads a subgraph to the Cortex-M
+   *
+   * \return string of code that offloads a subgraph to the Cortex-M
+   */
+  void AddFunction(const PrimFunc& prim_func) {
+    PrintExternCPrefix(stream);
+    CodeGenC::AddFunction(prim_func);
+    PrintExternCPostfix(stream);
+  }
+
+ private:
+  void VisitExpr_(const CallNode* op, std::ostream& os) {  // NOLINT(*)
+    if (!op->op.same_as(builtin::call_extern())) {
+      return;
+    }
+    std::string cmsis_func_name = op->args[0].as<StringImmNode>()->value;
+    if (cmsis_func_name == "arm_softmax_s8") {
+      EmitSoftmax(op);
+    }
+    return;
+  }
+
+  /*!  * \brief Creates a cplusplus guard prefix for extern "C" printing */
+  void PrintExternCPrefix(std::ostringstream& ss) {
+    PrintIndent();
+    ss << "#ifdef __cplusplus\n";
+    ss << "extern \"C\" {\n";
+    ss << "#endif\n";
+  }
+
+  /*!  * \brief Creates a cplusplus guard postfix for extern "C" printing */
+  void PrintExternCPostfix(std::ostringstream& ss) {
+    PrintIndent();
+    ss << "#ifdef __cplusplus\n";
+    ss << "}\n";
+    ss << "#endif\n";
+  }
+
+  /*!  * \brief Emits CMSIS-NN code block for softmax */
+  void EmitSoftmax(const CallNode* op) {
+    // @tir.call_extern("arm_softmax_s8", buffer_0, num_rows, row_size, scale, buffer_1, dtype=int8)
+    std::string cmsis_func_name = op->args[0].as<StringImmNode>()->value;
+    int32_t num_rows = op->args[2].as<IntImmNode>()->value;
+    int32_t row_size = op->args[3].as<IntImmNode>()->value;
+    float quant_scale = op->args[4].as<FloatImmNode>()->value;
+
+    // calculate multiplier and shift for CMSIS-NN softmax API
+    // Note: tfl micro assumptions
+    // TF_LITE_ENSURE_TYPES_EQ(context, output->type, kTfLiteInt8);
+    // TF_LITE_ENSURE_EQ(context, output->params.zero_point, -128);
+    // TF_LITE_ENSURE(context, output->params.scale == 1.f / 256);
+    double beta = 1.0;
+    int32_t input_bits = 5;
+    double beta_multiplier = (beta * quant_scale * (1 << (31 - input_bits)));
+    beta_multiplier = std::min<double>(beta_multiplier, (1ll << 31) - 1.0);
+    auto mult_shift_pair = tvm::relay::qnn::GetFixedPointMultiplierShift(beta_multiplier);
+    int32_t mult = std::get<0>(mult_shift_pair);
+    int32_t shift = std::get<1>(mult_shift_pair);
+    int32_t diff_min = (1 << 5) - 1;
+    diff_min <<= (31 - 5);
+    diff_min >>= shift;
+    diff_min *= -1;
+
+    PrintIndent();
+    stream << "int32_t num_rows = " << num_rows << ";\n";

Review comment:
       is there expected to be only one CMSIS-NN call per primfunc?

##########
File path: tests/python/driver/tvmc/test_compiler.py
##########
@@ -308,6 +310,37 @@ def test_compile_tflite_module_with_external_codegen(tflite_mobilenet_v1_1_quant
     assert os.path.exists(dumps_path)
 
 
+def test_compile_tflite_module_with_external_codegen_cmsisnn(
+    tmpdir_factory, tflite_cnn_s_quantized
+):
+    pytest.importorskip("tflite")
+
+    output_dir = tmpdir_factory.mktemp("mlf")
+    tvmc_model = tvmc.load(tflite_cnn_s_quantized)
+
+    output_file_name = f"{output_dir}/file.tar"
+
+    tvmc_package = tvmc.compiler.compile_model(
+        tvmc_model,
+        target=f"cmsis-nn, c -runtime=c --system-lib --link-params -mcpu=cortex-m55 --executor=aot",
+        output_format="mlf",

Review comment:
       one question: i'd like to include a list of libraries required in the MLF (separate PR). is that something you guys are okay with? cc @Mousius @leandron @mehrdadh @guberti 

##########
File path: src/relay/backend/contrib/cmsisnn/codegen_cmsisnn.cc
##########
@@ -0,0 +1,205 @@
+/*
+ * 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 <cmath>
+#include <fstream>
+#include <map>
+#include <sstream>
+#include <string>
+#include <vector>
+
+#include "../../../../runtime/file_utils.h"
+#include "../../../../target/source/codegen_c.h"
+#include "../../../qnn/utils.h"
+
+namespace tvm {
+namespace runtime {
+
+using namespace tir;
+
+class CodeGenCMSISNN : public tvm::codegen::CodeGenC {
+ public:
+  void Init(bool output_ssa) {
+    decl_stream << "#include <stdio.h>\n";
+    decl_stream << "#include <stdlib.h>\n";
+    decl_stream << "#include <dlpack/dlpack.h>\n";
+    decl_stream << "#include <tvm/runtime/crt/module.h>\n";
+    decl_stream << "#include <arm_nnfunctions.h>\n";
+    CodeGenC::Init(output_ssa);
+  }
+
+  /*!
+   * \brief Emit code that offloads a subgraph to the Cortex-M
+   *
+   * \return string of code that offloads a subgraph to the Cortex-M
+   */
+  void AddFunction(const PrimFunc& prim_func) {
+    PrintExternCPrefix(stream);
+    CodeGenC::AddFunction(prim_func);
+    PrintExternCPostfix(stream);
+  }
+
+ private:
+  void VisitExpr_(const CallNode* op, std::ostream& os) {  // NOLINT(*)
+    if (!op->op.same_as(builtin::call_extern())) {
+      return;
+    }
+    std::string cmsis_func_name = op->args[0].as<StringImmNode>()->value;
+    if (cmsis_func_name == "arm_softmax_s8") {
+      EmitSoftmax(op);
+    }
+    return;
+  }
+
+  /*!  * \brief Creates a cplusplus guard prefix for extern "C" printing */
+  void PrintExternCPrefix(std::ostringstream& ss) {
+    PrintIndent();
+    ss << "#ifdef __cplusplus\n";
+    ss << "extern \"C\" {\n";
+    ss << "#endif\n";
+  }
+
+  /*!  * \brief Creates a cplusplus guard postfix for extern "C" printing */
+  void PrintExternCPostfix(std::ostringstream& ss) {
+    PrintIndent();
+    ss << "#ifdef __cplusplus\n";
+    ss << "}\n";
+    ss << "#endif\n";
+  }
+
+  /*!  * \brief Emits CMSIS-NN code block for softmax */
+  void EmitSoftmax(const CallNode* op) {
+    // @tir.call_extern("arm_softmax_s8", buffer_0, num_rows, row_size, scale, buffer_1, dtype=int8)
+    std::string cmsis_func_name = op->args[0].as<StringImmNode>()->value;
+    int32_t num_rows = op->args[2].as<IntImmNode>()->value;
+    int32_t row_size = op->args[3].as<IntImmNode>()->value;
+    float quant_scale = op->args[4].as<FloatImmNode>()->value;
+
+    // calculate multiplier and shift for CMSIS-NN softmax API
+    // Note: tfl micro assumptions
+    // TF_LITE_ENSURE_TYPES_EQ(context, output->type, kTfLiteInt8);
+    // TF_LITE_ENSURE_EQ(context, output->params.zero_point, -128);
+    // TF_LITE_ENSURE(context, output->params.scale == 1.f / 256);
+    double beta = 1.0;
+    int32_t input_bits = 5;
+    double beta_multiplier = (beta * quant_scale * (1 << (31 - input_bits)));
+    beta_multiplier = std::min<double>(beta_multiplier, (1ll << 31) - 1.0);
+    auto mult_shift_pair = tvm::relay::qnn::GetFixedPointMultiplierShift(beta_multiplier);

Review comment:
       @jroesch @electriclilies @tqchen what's our stance on adding this much complexity to the codegen? can you comment whether it's okay to invoke `tvm::relay::qnn::GetFixedPointMultiplierShift` from here?
   
   in general, we should favor putting as much validation logic in the compiler as possible. it kind of seems like this should move upwards though, so that the codegen is just emitting tir.call_extern with the same arguments as are in the CallNode? Doing that would make it possible to emit code using llvm codegen.




-- 
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