You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2018/05/23 17:27:18 UTC

[GitHub] anirudh2290 commented on a change in pull request #11025: added ravel/unravel operators

anirudh2290 commented on a change in pull request #11025: added ravel/unravel operators
URL: https://github.com/apache/incubator-mxnet/pull/11025#discussion_r190333867
 
 

 ##########
 File path: src/operator/tensor/ravel.h
 ##########
 @@ -0,0 +1,166 @@
+/*
+ * 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.
+ */
+
+/*!
+ * Copyright (c) 2018 by Contributors
+ * \file ravel.h
+ * \brief Operators for ravel/unravel indices.
+ */
+#ifndef MXNET_OPERATOR_TENSOR_RAVEL_H_
+#define MXNET_OPERATOR_TENSOR_RAVEL_H_
+
+#include <mxnet/operator_util.h>
+#include <vector>
+#include <algorithm>
+#include "../mshadow_op.h"
+#include "../mxnet_op.h"
+#include "../operator_common.h"
+#include "../elemwise_op_common.h"
+
+namespace mxnet {
+namespace op {
+
+struct RavelParam : public dmlc::Parameter<RavelParam> {
+  TShape shape;
+  DMLC_DECLARE_PARAMETER(RavelParam) {
+    DMLC_DECLARE_FIELD(shape)
+      .set_default(TShape())
+      .describe("Shape of the array into which the multi-indices apply.");
+  }
+};
+
+inline bool RavelOpShape(const nnvm::NodeAttrs& attrs,
+                         std::vector<TShape>* in_attrs,
+                         std::vector<TShape>* out_attrs) {
+  using namespace mshadow;
+  const TShape& shape = nnvm::get<RavelParam>(attrs.parsed).shape;
+  CHECK_EQ(in_attrs->size(), 1);
+  CHECK_EQ(out_attrs->size(), 1);
+  CHECK_GT(shape.ndim(), 0) << "Invalid shape parameter for ravel operator.";
+  if ((*in_attrs)[0].ndim() > 0) {
+    CHECK_EQ((*in_attrs)[0].ndim(), 2)
+      << "Input to ravel operator must be two-dimensional.";
+    CHECK_EQ((*in_attrs)[0][0], shape.ndim())
+      << "First dimension of input of ravel operator does not match shape parameter dimension.";
+    SHAPE_ASSIGN_CHECK(*out_attrs, 0, Shape1((*in_attrs)[0][1]));
+    return true;
+  }
+  if ((*out_attrs)[0].ndim() > 0) {
+    SHAPE_ASSIGN_CHECK(*in_attrs, 0, Shape2(shape.ndim(), (*out_attrs)[0][0]));
+    return true;
+  }
+  return false;
+}
+
+inline bool UnravelOpShape(const nnvm::NodeAttrs& attrs,
+                           std::vector<TShape>* in_attrs,
+                           std::vector<TShape>* out_attrs) {
+  using namespace mshadow;
+  const TShape& shape = nnvm::get<RavelParam>(attrs.parsed).shape;
+  CHECK_EQ(in_attrs->size(), 1);
+  CHECK_EQ(out_attrs->size(), 1);
+  CHECK_GT(shape.ndim(), 0) << "Invalid shape parameter for unravel operator.";
+  if ((*in_attrs)[0].ndim() > 0) {
+    SHAPE_ASSIGN_CHECK(*out_attrs, 0, Shape2(shape.ndim(), (*in_attrs)[0][0]));
+    return true;
+  }
+  if ((*out_attrs)[0].ndim() > 0) {
+    CHECK_EQ((*out_attrs)[0].ndim(), 2)
+      << "Output of unravel operator must be two-dimensional.";
+    CHECK_EQ((*out_attrs)[0][0], shape.ndim())
+      << "First dimension of output of ravel operator does not match shape parameter dimension.";
+    SHAPE_ASSIGN_CHECK(*in_attrs, 0, Shape1((*out_attrs)[0][1]));
+    return true;
+  }
+  return false;
+}
+
+struct ravel_index {
+  template<typename DType>
+  MSHADOW_XINLINE static void Map(int i, index_t N, index_t ndim, index_t *shape,
+                                  DType *unravelled, DType *ravelled) {
+    index_t ret = 0;
+    #pragma unroll
+    for (index_t j = 0; j < ndim; ++j) {
+      ret = ret * shape[j] + unravelled[i+j*N];
+    }
+    ravelled[i] = ret;
+  }
+};
+
+struct unravel_index {
+  template<typename DType>
+  MSHADOW_XINLINE static void Map(int i, index_t N, index_t ndim, index_t *shape,
+                                  DType *unravelled, DType *ravelled) {
+    index_t idx(ravelled[i]);
+    #pragma unroll
+    for (int j = ndim; j--; ) {
 
 Review comment:
   I find this style a little confusing and would prefer `for(int j = ndim; j >= 0; j--)` . What is the motivation ?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on 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