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 2019/12/27 14:34:13 UTC

[GitHub] [incubator-tvm] TaoLv commented on a change in pull request #4258: [WIP][TVM] Bring Your Own Codegen to TVM

TaoLv commented on a change in pull request #4258: [WIP][TVM] Bring Your Own Codegen to TVM
URL: https://github.com/apache/incubator-tvm/pull/4258#discussion_r361674256
 
 

 ##########
 File path: src/runtime/contrib/dnnl/dnnl.cc
 ##########
 @@ -0,0 +1,247 @@
+/*
+ * 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/runtime/contrib/dnnl/dnnl.cc
+ * \brief TVM compatible wrappers for dnnl kernels.
+ */
+
+#include <tvm/runtime/contrib/dnnl/dnnl_kernel.h>
+
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <algorithm>
+#include <iostream>
+#include <numeric>
+#include <string>
+#include <vector>
+
+namespace tvm {
+namespace runtime {
+namespace contrib {
+
+using namespace dnnl;
+
+typedef struct {
+  void** data;
+} DnnlPackedArgs;
+
+// Read from memory, write to handle
+inline void read_from_dnnl_memory(void* handle, const memory& mem) {
+  size_t bytes = mem.get_desc().get_size();
+
+  uint8_t* src = static_cast<uint8_t*>(mem.get_data_handle());
+  std::copy(src, src + bytes, reinterpret_cast<uint8_t*>(handle));
+}
+
+extern "C" void dnnl_conv2d(float* data, float* weights, float* out, int p_N_,
+                            int p_C_, int p_H_, int p_W_, int p_O_, int p_G_,
+                            int p_Ph_, int p_Pw_, int p_Kh_, int p_Kw_,
+                            int p_Sh_, int p_Sw_) {
+  using tag = memory::format_tag;
+  using dt = memory::data_type;
+  engine eng(engine::kind::cpu, 0);
+  stream s(eng);
+
+  memory::dims conv2d_src_tz = {p_N_, p_C_, p_H_, p_W_};
+  memory::dims conv2d_weights_tz = {p_O_, p_C_, p_Kh_, p_Kw_};
+  if (p_G_ > 1) conv2d_weights_tz = {p_G_, 1, p_C_ / p_G_, p_Kh_, p_Kw_};
+  memory::dims conv2d_bias_tz = {p_O_};
+  memory::dims conv2d_dst_tz = {p_N_, p_O_,
+                                (p_H_ - p_Kh_ + 2 * p_Ph_ + p_Sh_) / p_Sh_,
+                                (p_W_ - p_Kw_ + 2 * p_Pw_ + p_Sw_) / p_Sw_};
+  memory::dims conv2d_strides = {p_Sh_, p_Sw_};
+  memory::dims conv2d_padding = {p_Ph_, p_Pw_};
+
+  std::vector<float> conv2d_bias(p_O_, 0);
+
+  auto user_src_memory =
+      memory({{conv2d_src_tz}, dt::f32, tag::nchw}, eng, data);
+  auto user_weights_memory = memory(
+      {{conv2d_weights_tz}, dt::f32, (p_G_ > 1) ? tag::goihw : tag::oihw}, eng,
+      weights);
+  auto conv2d_user_bias_memory =
+      memory({{conv2d_bias_tz}, dt::f32, tag::x}, eng, conv2d_bias.data());
+
+  auto conv2d_src_md = memory::desc({conv2d_src_tz}, dt::f32, tag::any);
+  auto conv2d_bias_md = memory::desc({conv2d_bias_tz}, dt::f32, tag::any);
+  auto conv2d_weights_md = memory::desc({conv2d_weights_tz}, dt::f32, tag::any);
+  auto conv2d_dst_md = memory::desc({conv2d_dst_tz}, dt::f32, tag::nchw);
 
 Review comment:
   Why hard code the dst format to nchw?

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services