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 2020/01/08 22:50:38 UTC

[GitHub] [incubator-mxnet] haojin2 commented on a change in pull request #17014: [NumPy] Add NumPy support for norm

haojin2 commented on a change in pull request #17014: [NumPy] Add NumPy support for norm
URL: https://github.com/apache/incubator-mxnet/pull/17014#discussion_r364482831
 
 

 ##########
 File path: src/operator/numpy/linalg/np_norm.cc
 ##########
 @@ -0,0 +1,182 @@
+/*
+ * 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) 2019 by Contributors
+ * \file np_norm-.cc
+ * \brief CPU registration of np.linalg.norm
+ */
+
+#include "./np_norm-inl.h"
+
+namespace mxnet {
+namespace op {
+
+DMLC_REGISTER_PARAMETER(NumpyNormParam);
+
+inline bool NumpyLpNormShape(const nnvm::NodeAttrs& attrs,
+                             mxnet::ShapeVector *in_attrs,
+                             mxnet::ShapeVector *out_attrs) {
+  if (!shape_is_known((*in_attrs)[0])) return false;
+  const NumpyNormParam& param = nnvm::get<NumpyNormParam>(attrs.parsed);
+  const int ndim = (*in_attrs)[0].ndim();
+  if ((!param.axis.has_value() && param.flag != 0 && ndim > 2) ||
+      (param.axis.has_value() && param.axis.value().ndim() > 2))
+    LOG(FATAL) << "Improper number of dimensions to norm.";
+  if (!param.axis.has_value()) {
+    if ((ndim == 0 && param.flag != 0) ||  // for scalar
+        (ndim == 1 && (param.flag == 2)) ||
+        (ndim >= 2 && (param.ord == 0 || param.ord > 2 || param.ord < -2))) {
+      LOG(FATAL) << "Invalid norm order for inputs.";
+    }
+  } else {
+    if ((param.axis.value().ndim() == 0 && param.flag != 0) ||  // for scalar
+        (param.axis.value().ndim() == 1 && (param.flag == 2)) ||
+        (param.axis.value().ndim() == 2 && (param.ord == 0 || param.ord > 2 || param.ord < -2))) {
+      LOG(FATAL) << "Invalid norm order for inputs.";
+    }
+  }
+  if (!param.keepdims && (*in_attrs)[0].ndim() == 1) {
+    SHAPE_ASSIGN_CHECK(*out_attrs, 0, TShape(0, -1));
+  } else {
+    SHAPE_ASSIGN_CHECK(*out_attrs, 0,
+                       ReduceAxesShapeImpl((*in_attrs)[0], param.axis, param.keepdims, false));
+  }
+  return true;
+}
+
+inline bool NumpyMatrixNormShape(const nnvm::NodeAttrs& attrs,
+                                   mxnet::ShapeVector *in_attrs,
 
 Review comment:
   alignment.

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