[GitHub] [incubator-mxnet] mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd

2019-08-15 Thread GitBox
mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd
URL: https://github.com/apache/incubator-mxnet/pull/15795#discussion_r314212813
 
 

 ##
 File path: src/operator/numpy/linalg/np_gesvd-inl.h
 ##
 @@ -0,0 +1,302 @@
+/*
+ * 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) 2017 by Contributors
+ * \file np_gesvd-inl.h
+ * \brief Function definition of the SVD Operator.
+ */
+#ifndef MXNET_OPERATOR_NUMPY_LINALG_NP_GESVD_INL_H_
+#define MXNET_OPERATOR_NUMPY_LINALG_NP_GESVD_INL_H_
+
+#include 
+#include 
+#include "../../tensor/la_op.h"
+#include "../../tensor/la_op-inl.h"
+
+namespace mxnet {
+namespace op {
+
+struct GesvdVecSign {
+  template
+  MSHADOW_XINLINE static void Map(int i, int m, int n, DType* UT,
+  DType* V, int ldut, int ldv) {
+DType* vrow(V + i * ldv);
+DType maxval(fabs(vrow[0])), vval(0.0);
+int maxind(0);
+for (int i = 1; i < n; ++i) {
+  vval = fabs(vrow[i]);
+  if (vval > maxval) {
+maxval = vval;
+maxind = i;
+  }
+}
+if (vrow[maxind] < 0) {
+  DType* utcol(UT + i % m + (i / m) * ldut * m);
+  for (int i = 0; i < n; ++i) {
+vrow[i] = -vrow[i];
+if (i < m) {
+  utcol[i * ldut] = -utcol[i * ldut];
+}
+  }
+}
+  }
+};
+
+// (UT, L, V) = gesvd(A) [singular value decomposition]
+// - V can overwrite A
+// - Needs workspace (DType), size of which is determined by a workspace query
+struct gesvd {
+  template
+  static void op(const Tensor& A,
+ const Tensor& UT,
+ const Tensor& L,
+ const Tensor& V,
+ const OpContext& ctx,
+ const nnvm::NodeAttrs& attrs) {
+Stream *s = ctx.get_stream();
+if (A.dptr_ != V.dptr_) Copy(V, A, s);
+// From here on, we work on V only
+// Reserve workspace (size determined by query)
+int lwork(linalg_gesvd_workspace_query(UT[0], L[0], V[0], s));
+Tensor work = ctx.requested[0]
+  .get_space_typed(Shape1(lwork), s);
+// Loop over items in batch
+for (index_t i = 0; i < UT.size(0); ++i) {
+  linalg_gesvd(UT[i], L[i], V[i], work, s);
+}
+// Set signs in a deterministic way
+using namespace mxnet_op;
+Kernel::Launch
+  (s, V.size(0) * V.size(1), V.size(1), V.size(2),
+   UT.dptr_, V.dptr_, UT.stride_, V.stride_);
+  }
+};
+
+// (A) => (UT, L, V)
+template
+void NumpyLaGesvdForward(const nnvm::NodeAttrs& attrs,
+ const OpContext& ctx,
+ const std::vector& inputs,
+ const std::vector& req,
+ const std::vector& outputs) {
+  using namespace mshadow;
+  CHECK_EQ(inputs.size(), 1);
+  CHECK_EQ(outputs.size(), 3);
+  if (inputs[0].shape_.Size() == 0) {
+return;
+  }
+  MSHADOW_SGL_DBL_TYPE_SWITCH(outputs[0].type_flag_, OType, {
+mshadow::Stream *s = ctx.get_stream();
+laop::op(inputs[0].FlatToKD(s),
+ outputs[0].FlatToKD(s),
+ outputs[1].FlatToKD(s),
+ outputs[2].FlatToKD(s), ctx, attrs);
+  });
+}
+
+// Helper for gesvd_backward. See technical report
+// `Auto-Differentiating Linear Algebra` for details
+// on https://arxiv.org/pdf/1710.08717.pdf
+template
+DType gesvd_back_helper_eps(DType* X);
+
+template<>
+MSHADOW_XINLINE float gesvd_back_helper_eps(float* X) {
+  return 1e-30;
+}
+
+template<>
+MSHADOW_XINLINE double gesvd_back_helper_eps(double* X) {
+  return 1e-100;
+}
+
+// dA overwritten by L^-1 dA
+struct GesvdBackHelper_dV {
+  template
+  MSHADOW_XINLINE static void Map(int k, int m, int n, DType* L, int ldl,
+  DType* dA, int ldda) {
+const int offl(k * ldl);
+const int offda(k * m * ldda);
+DType denom(0.0);
+const DType eps(gesvd_back_helper_eps(dA));
+for (int i = 0; i < m; ++i) {
+  denom = L[offl + i];
+  if (denom < eps) denom = eps;
+  for (int j = 0; j < n; ++j) {
+dA[offda + i * ldda + j] /= denom;
+  }
+}
+  }
+};
+
+// X (square) overwritten by X L
+// Y overwritten by the diagonal of X
+struct GesvdBackHelper_G1 {
+  template
+  

[GitHub] [incubator-mxnet] mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd

2019-08-14 Thread GitBox
mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd
URL: https://github.com/apache/incubator-mxnet/pull/15795#discussion_r313779126
 
 

 ##
 File path: src/operator/numpy/linalg/np_gesvd-inl.h
 ##
 @@ -0,0 +1,298 @@
+/*
+ * 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) 2017 by Contributors
+ * \file np_gesvd-inl.h
+ * \brief Function definition of the SVD Operator.
+ */
+#ifndef MXNET_OPERATOR_NUMPY_LINALG_NP_GESVD_INL_H_
+#define MXNET_OPERATOR_NUMPY_LINALG_NP_GESVD_INL_H_
+
+#include 
+#include 
+#include "../../tensor/la_op.h"
+#include "../../tensor/la_op-inl.h"
+
+namespace mxnet {
+namespace op {
+
+struct GesvdVecSign {
+  template
+  MSHADOW_XINLINE static void Map(int i, int m, int n, DType* UT,
+  DType* V, int ldut, int ldv) {
+DType* vrow(V + i * ldv);
+DType maxval(fabs(vrow[0])), vval(0.0);
+int maxind(0);
+for (int i = 1; i < n; ++i) {
+  vval = fabs(vrow[i]);
+  if (vval > maxval) {
+maxval = vval;
+maxind = i;
+  }
+}
+if (vrow[maxind] < 0) {
+  DType* utcol(UT + i % m + (i / m) * ldut * m);
+  for (int i = 0; i < n; ++i) {
+vrow[i] = -vrow[i];
+if (i < m) {
+  utcol[i * ldut] = -utcol[i * ldut];
+}
+  }
+}
+  }
+};
+
+// (UT, L, V) = gesvd(A) [singular value decomposition]
+// - V can overwrite A
+// - Needs workspace (both DType and int), size of which is determined by a
+//   workspace query
+struct gesvd {
+  template
+  static void op(const Tensor& A,
+ const Tensor& UT,
+ const Tensor& L,
+ const Tensor& V,
+ const OpContext& ctx,
+ const nnvm::NodeAttrs& attrs) {
+Stream *s = ctx.get_stream();
+if (A.dptr_ != V.dptr_) Copy(V, A, s);
+// From here on, we work on V only
+// Reserve workspace (size determined by query)
+int lwork(linalg_gesvd_workspace_query(UT[0], L[0], V[0], s));
+Tensor work = ctx.requested[0]
+  .get_space_typed(Shape1(lwork), s);
+// Loop over items in batch
+for (index_t i = 0; i < UT.size(0); ++i) {
+  linalg_gesvd(UT[i], L[i], V[i], work, s);
+}
+// Set signs in a deterministic way
+using namespace mxnet_op;
+Kernel::Launch
+  (s, V.size(0) * V.size(1), V.size(1), V.size(2),
+   UT.dptr_, V.dptr_, UT.stride_, V.stride_);
+  }
+};
+
+// (A) => (UT, L, V)
+template
+void NumpyLaGesvdForward(const nnvm::NodeAttrs& attrs,
+ const OpContext& ctx,
+ const std::vector& inputs,
+ const std::vector& req,
+ const std::vector& outputs) {
+  using namespace mshadow;
+  CHECK_EQ(inputs.size(), 1);
+  CHECK_EQ(outputs.size(), 3);
+  if (inputs[0].shape_.Size() == 0) {
+return;
+  }
+  MSHADOW_SGL_DBL_TYPE_SWITCH(outputs[0].type_flag_, OType, {
+mshadow::Stream *s = ctx.get_stream();
+laop::op(inputs[0].FlatToKD(s),
+ outputs[0].FlatToKD(s),
+ outputs[1].FlatToKD(s),
+ outputs[2].FlatToKD(s), ctx, attrs);
+  });
+}
+
+// Helper for gesvd_backward. See technical report for details
+template
+DType gesvd_back_helper_eps(DType* X);
+
+template<>
+MSHADOW_XINLINE float gesvd_back_helper_eps(float* X) {
+  return 1e-30;
+}
+
+template<>
+MSHADOW_XINLINE double gesvd_back_helper_eps(double* X) {
+  return 1e-100;
+}
+
+struct GesvdBackHelper_dV {
+  template
+  MSHADOW_XINLINE static void Map(int k, int m, int n, DType* L, int ldl,
+  DType* dA, int ldda) {
+const int offl(k * ldl);
+const int offda(k * m * ldda);
+DType denom(0.0);
+const DType eps(gesvd_back_helper_eps(dA));
+for (int i = 0; i < m; ++i) {
+  denom = L[offl + i];
+  if (denom < eps) denom = eps;
+  for (int j = 0; j < n; ++j) {
+dA[offda + i * ldda + j] /= denom;
+  }
+}
+  }
+};
+
+struct GesvdBackHelper_G1 {
+  template
+  MSHADOW_XINLINE static void Map(int k, int m, int n, DType* X, int ldx,
+  DType* L, int ldl) {
+const int offl(k * ldl);
+const int 

[GitHub] [incubator-mxnet] mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd

2019-08-14 Thread GitBox
mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd
URL: https://github.com/apache/incubator-mxnet/pull/15795#discussion_r313777983
 
 

 ##
 File path: src/operator/numpy/linalg/np_gesvd-inl.h
 ##
 @@ -0,0 +1,298 @@
+/*
+ * 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) 2017 by Contributors
+ * \file np_gesvd-inl.h
+ * \brief Function definition of the SVD Operator.
+ */
+#ifndef MXNET_OPERATOR_NUMPY_LINALG_NP_GESVD_INL_H_
+#define MXNET_OPERATOR_NUMPY_LINALG_NP_GESVD_INL_H_
+
+#include 
+#include 
+#include "../../tensor/la_op.h"
+#include "../../tensor/la_op-inl.h"
+
+namespace mxnet {
+namespace op {
+
+struct GesvdVecSign {
+  template
+  MSHADOW_XINLINE static void Map(int i, int m, int n, DType* UT,
+  DType* V, int ldut, int ldv) {
+DType* vrow(V + i * ldv);
+DType maxval(fabs(vrow[0])), vval(0.0);
+int maxind(0);
+for (int i = 1; i < n; ++i) {
+  vval = fabs(vrow[i]);
+  if (vval > maxval) {
+maxval = vval;
+maxind = i;
+  }
+}
+if (vrow[maxind] < 0) {
+  DType* utcol(UT + i % m + (i / m) * ldut * m);
+  for (int i = 0; i < n; ++i) {
+vrow[i] = -vrow[i];
+if (i < m) {
+  utcol[i * ldut] = -utcol[i * ldut];
+}
+  }
+}
+  }
+};
+
+// (UT, L, V) = gesvd(A) [singular value decomposition]
+// - V can overwrite A
+// - Needs workspace (both DType and int), size of which is determined by a
+//   workspace query
+struct gesvd {
+  template
+  static void op(const Tensor& A,
+ const Tensor& UT,
+ const Tensor& L,
+ const Tensor& V,
+ const OpContext& ctx,
+ const nnvm::NodeAttrs& attrs) {
+Stream *s = ctx.get_stream();
+if (A.dptr_ != V.dptr_) Copy(V, A, s);
+// From here on, we work on V only
+// Reserve workspace (size determined by query)
+int lwork(linalg_gesvd_workspace_query(UT[0], L[0], V[0], s));
+Tensor work = ctx.requested[0]
+  .get_space_typed(Shape1(lwork), s);
+// Loop over items in batch
+for (index_t i = 0; i < UT.size(0); ++i) {
+  linalg_gesvd(UT[i], L[i], V[i], work, s);
+}
+// Set signs in a deterministic way
+using namespace mxnet_op;
+Kernel::Launch
+  (s, V.size(0) * V.size(1), V.size(1), V.size(2),
+   UT.dptr_, V.dptr_, UT.stride_, V.stride_);
+  }
+};
+
+// (A) => (UT, L, V)
+template
+void NumpyLaGesvdForward(const nnvm::NodeAttrs& attrs,
+ const OpContext& ctx,
+ const std::vector& inputs,
+ const std::vector& req,
+ const std::vector& outputs) {
+  using namespace mshadow;
+  CHECK_EQ(inputs.size(), 1);
+  CHECK_EQ(outputs.size(), 3);
+  if (inputs[0].shape_.Size() == 0) {
+return;
+  }
+  MSHADOW_SGL_DBL_TYPE_SWITCH(outputs[0].type_flag_, OType, {
+mshadow::Stream *s = ctx.get_stream();
+laop::op(inputs[0].FlatToKD(s),
+ outputs[0].FlatToKD(s),
+ outputs[1].FlatToKD(s),
+ outputs[2].FlatToKD(s), ctx, attrs);
+  });
+}
+
+// Helper for gesvd_backward. See technical report for details
 
 Review comment:
   Could you cite the arxiv paper in the code? The new version with SVD will be 
uploaded in the next few days, way before this CR will get merged. Thanks.


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:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-mxnet] mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd

2019-08-14 Thread GitBox
mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd
URL: https://github.com/apache/incubator-mxnet/pull/15795#discussion_r313719755
 
 

 ##
 File path: src/operator/numpy/linalg/np_gesvd-inl.h
 ##
 @@ -0,0 +1,298 @@
+/*
+ * 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) 2017 by Contributors
+ * \file np_gesvd-inl.h
+ * \brief Function definition of the SVD Operator.
+ */
+#ifndef MXNET_OPERATOR_NUMPY_LINALG_NP_GESVD_INL_H_
+#define MXNET_OPERATOR_NUMPY_LINALG_NP_GESVD_INL_H_
+
+#include 
+#include 
+#include "../../tensor/la_op.h"
+#include "../../tensor/la_op-inl.h"
+
+namespace mxnet {
+namespace op {
+
+struct GesvdVecSign {
+  template
+  MSHADOW_XINLINE static void Map(int i, int m, int n, DType* UT,
+  DType* V, int ldut, int ldv) {
+DType* vrow(V + i * ldv);
+DType maxval(fabs(vrow[0])), vval(0.0);
+int maxind(0);
+for (int i = 1; i < n; ++i) {
+  vval = fabs(vrow[i]);
+  if (vval > maxval) {
+maxval = vval;
+maxind = i;
+  }
+}
+if (vrow[maxind] < 0) {
+  DType* utcol(UT + i % m + (i / m) * ldut * m);
+  for (int i = 0; i < n; ++i) {
+vrow[i] = -vrow[i];
+if (i < m) {
+  utcol[i * ldut] = -utcol[i * ldut];
+}
+  }
+}
+  }
+};
+
+// (UT, L, V) = gesvd(A) [singular value decomposition]
+// - V can overwrite A
+// - Needs workspace (both DType and int), size of which is determined by a
+//   workspace query
+struct gesvd {
+  template
+  static void op(const Tensor& A,
+ const Tensor& UT,
+ const Tensor& L,
+ const Tensor& V,
+ const OpContext& ctx,
+ const nnvm::NodeAttrs& attrs) {
+Stream *s = ctx.get_stream();
+if (A.dptr_ != V.dptr_) Copy(V, A, s);
+// From here on, we work on V only
+// Reserve workspace (size determined by query)
+int lwork(linalg_gesvd_workspace_query(UT[0], L[0], V[0], s));
+Tensor work = ctx.requested[0]
+  .get_space_typed(Shape1(lwork), s);
+// Loop over items in batch
+for (index_t i = 0; i < UT.size(0); ++i) {
+  linalg_gesvd(UT[i], L[i], V[i], work, s);
+}
+// Set signs in a deterministic way
+using namespace mxnet_op;
+Kernel::Launch
+  (s, V.size(0) * V.size(1), V.size(1), V.size(2),
+   UT.dptr_, V.dptr_, UT.stride_, V.stride_);
+  }
+};
+
+// (A) => (UT, L, V)
+template
+void NumpyLaGesvdForward(const nnvm::NodeAttrs& attrs,
+ const OpContext& ctx,
+ const std::vector& inputs,
+ const std::vector& req,
+ const std::vector& outputs) {
+  using namespace mshadow;
+  CHECK_EQ(inputs.size(), 1);
+  CHECK_EQ(outputs.size(), 3);
+  if (inputs[0].shape_.Size() == 0) {
+return;
+  }
+  MSHADOW_SGL_DBL_TYPE_SWITCH(outputs[0].type_flag_, OType, {
+mshadow::Stream *s = ctx.get_stream();
+laop::op(inputs[0].FlatToKD(s),
+ outputs[0].FlatToKD(s),
+ outputs[1].FlatToKD(s),
+ outputs[2].FlatToKD(s), ctx, attrs);
+  });
+}
+
+// Helper for gesvd_backward. See technical report for details
+template
+DType gesvd_back_helper_eps(DType* X);
+
+template<>
+MSHADOW_XINLINE float gesvd_back_helper_eps(float* X) {
+  return 1e-30;
+}
+
+template<>
+MSHADOW_XINLINE double gesvd_back_helper_eps(double* X) {
+  return 1e-100;
+}
+
+struct GesvdBackHelper_dV {
+  template
+  MSHADOW_XINLINE static void Map(int k, int m, int n, DType* L, int ldl,
+  DType* dA, int ldda) {
+const int offl(k * ldl);
+const int offda(k * m * ldda);
+DType denom(0.0);
+const DType eps(gesvd_back_helper_eps(dA));
+for (int i = 0; i < m; ++i) {
+  denom = L[offl + i];
+  if (denom < eps) denom = eps;
+  for (int j = 0; j < n; ++j) {
+dA[offda + i * ldda + j] /= denom;
+  }
+}
+  }
+};
+
+struct GesvdBackHelper_G1 {
 
 Review comment:
   Yes, it is all transposed, because of this row/col major issue


This is an automated message from the 

[GitHub] [incubator-mxnet] mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd

2019-08-13 Thread GitBox
mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd
URL: https://github.com/apache/incubator-mxnet/pull/15795#discussion_r313422488
 
 

 ##
 File path: src/operator/numpy/linalg/np_gesvd-inl.h
 ##
 @@ -0,0 +1,298 @@
+/*
+ * 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) 2017 by Contributors
+ * \file np_gesvd-inl.h
+ * \brief Function definition of the SVD Operator.
+ */
+#ifndef MXNET_OPERATOR_NUMPY_LINALG_NP_GESVD_INL_H_
+#define MXNET_OPERATOR_NUMPY_LINALG_NP_GESVD_INL_H_
+
+#include 
+#include 
+#include "../../tensor/la_op.h"
+#include "../../tensor/la_op-inl.h"
+
+namespace mxnet {
+namespace op {
+
+struct GesvdVecSign {
+  template
+  MSHADOW_XINLINE static void Map(int i, int m, int n, DType* UT,
+  DType* V, int ldut, int ldv) {
+DType* vrow(V + i * ldv);
+DType maxval(fabs(vrow[0])), vval(0.0);
+int maxind(0);
+for (int i = 1; i < n; ++i) {
+  vval = fabs(vrow[i]);
+  if (vval > maxval) {
+maxval = vval;
+maxind = i;
+  }
+}
+if (vrow[maxind] < 0) {
+  DType* utcol(UT + i % m + (i / m) * ldut * m);
+  for (int i = 0; i < n; ++i) {
+vrow[i] = -vrow[i];
+if (i < m) {
+  utcol[i * ldut] = -utcol[i * ldut];
+}
+  }
+}
+  }
+};
+
+// (UT, L, V) = gesvd(A) [singular value decomposition]
+// - V can overwrite A
+// - Needs workspace (both DType and int), size of which is determined by a
+//   workspace query
+struct gesvd {
+  template
+  static void op(const Tensor& A,
+ const Tensor& UT,
+ const Tensor& L,
+ const Tensor& V,
+ const OpContext& ctx,
+ const nnvm::NodeAttrs& attrs) {
+Stream *s = ctx.get_stream();
+if (A.dptr_ != V.dptr_) Copy(V, A, s);
+// From here on, we work on V only
+// Reserve workspace (size determined by query)
+int lwork(linalg_gesvd_workspace_query(UT[0], L[0], V[0], s));
+Tensor work = ctx.requested[0]
+  .get_space_typed(Shape1(lwork), s);
+// Loop over items in batch
+for (index_t i = 0; i < UT.size(0); ++i) {
+  linalg_gesvd(UT[i], L[i], V[i], work, s);
+}
+// Set signs in a deterministic way
+using namespace mxnet_op;
+Kernel::Launch
+  (s, V.size(0) * V.size(1), V.size(1), V.size(2),
+   UT.dptr_, V.dptr_, UT.stride_, V.stride_);
+  }
+};
+
+// (A) => (UT, L, V)
+template
+void NumpyLaGesvdForward(const nnvm::NodeAttrs& attrs,
+ const OpContext& ctx,
+ const std::vector& inputs,
+ const std::vector& req,
+ const std::vector& outputs) {
+  using namespace mshadow;
+  CHECK_EQ(inputs.size(), 1);
+  CHECK_EQ(outputs.size(), 3);
+  if (inputs[0].shape_.Size() == 0) {
+return;
+  }
+  MSHADOW_SGL_DBL_TYPE_SWITCH(outputs[0].type_flag_, OType, {
+mshadow::Stream *s = ctx.get_stream();
+laop::op(inputs[0].FlatToKD(s),
+ outputs[0].FlatToKD(s),
+ outputs[1].FlatToKD(s),
+ outputs[2].FlatToKD(s), ctx, attrs);
+  });
+}
+
+// Helper for gesvd_backward. See technical report for details
+template
+DType gesvd_back_helper_eps(DType* X);
+
+template<>
+MSHADOW_XINLINE float gesvd_back_helper_eps(float* X) {
+  return 1e-30;
+}
+
+template<>
+MSHADOW_XINLINE double gesvd_back_helper_eps(double* X) {
+  return 1e-100;
+}
+
+struct GesvdBackHelper_dV {
+  template
+  MSHADOW_XINLINE static void Map(int k, int m, int n, DType* L, int ldl,
+  DType* dA, int ldda) {
+const int offl(k * ldl);
+const int offda(k * m * ldda);
+DType denom(0.0);
+const DType eps(gesvd_back_helper_eps(dA));
+for (int i = 0; i < m; ++i) {
+  denom = L[offl + i];
+  if (denom < eps) denom = eps;
+  for (int j = 0; j < n; ++j) {
+dA[offda + i * ldda + j] /= denom;
+  }
+}
+  }
+};
+
+struct GesvdBackHelper_G1 {
+  template
+  MSHADOW_XINLINE static void Map(int k, int m, int n, DType* X, int ldx,
+  DType* L, int ldl) {
+const int offl(k * ldl);
+const int 

[GitHub] [incubator-mxnet] mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd

2019-08-13 Thread GitBox
mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd
URL: https://github.com/apache/incubator-mxnet/pull/15795#discussion_r313421782
 
 

 ##
 File path: src/operator/numpy/linalg/np_gesvd-inl.h
 ##
 @@ -0,0 +1,298 @@
+/*
+ * 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) 2017 by Contributors
+ * \file np_gesvd-inl.h
+ * \brief Function definition of the SVD Operator.
+ */
+#ifndef MXNET_OPERATOR_NUMPY_LINALG_NP_GESVD_INL_H_
+#define MXNET_OPERATOR_NUMPY_LINALG_NP_GESVD_INL_H_
+
+#include 
+#include 
+#include "../../tensor/la_op.h"
+#include "../../tensor/la_op-inl.h"
+
+namespace mxnet {
+namespace op {
+
+struct GesvdVecSign {
+  template
+  MSHADOW_XINLINE static void Map(int i, int m, int n, DType* UT,
+  DType* V, int ldut, int ldv) {
+DType* vrow(V + i * ldv);
+DType maxval(fabs(vrow[0])), vval(0.0);
+int maxind(0);
+for (int i = 1; i < n; ++i) {
+  vval = fabs(vrow[i]);
+  if (vval > maxval) {
+maxval = vval;
+maxind = i;
+  }
+}
+if (vrow[maxind] < 0) {
+  DType* utcol(UT + i % m + (i / m) * ldut * m);
+  for (int i = 0; i < n; ++i) {
+vrow[i] = -vrow[i];
+if (i < m) {
+  utcol[i * ldut] = -utcol[i * ldut];
+}
+  }
+}
+  }
+};
+
+// (UT, L, V) = gesvd(A) [singular value decomposition]
+// - V can overwrite A
+// - Needs workspace (both DType and int), size of which is determined by a
+//   workspace query
+struct gesvd {
+  template
+  static void op(const Tensor& A,
+ const Tensor& UT,
+ const Tensor& L,
+ const Tensor& V,
+ const OpContext& ctx,
+ const nnvm::NodeAttrs& attrs) {
+Stream *s = ctx.get_stream();
+if (A.dptr_ != V.dptr_) Copy(V, A, s);
+// From here on, we work on V only
+// Reserve workspace (size determined by query)
+int lwork(linalg_gesvd_workspace_query(UT[0], L[0], V[0], s));
+Tensor work = ctx.requested[0]
+  .get_space_typed(Shape1(lwork), s);
+// Loop over items in batch
+for (index_t i = 0; i < UT.size(0); ++i) {
+  linalg_gesvd(UT[i], L[i], V[i], work, s);
+}
+// Set signs in a deterministic way
+using namespace mxnet_op;
+Kernel::Launch
+  (s, V.size(0) * V.size(1), V.size(1), V.size(2),
+   UT.dptr_, V.dptr_, UT.stride_, V.stride_);
+  }
+};
+
+// (A) => (UT, L, V)
+template
+void NumpyLaGesvdForward(const nnvm::NodeAttrs& attrs,
+ const OpContext& ctx,
+ const std::vector& inputs,
+ const std::vector& req,
+ const std::vector& outputs) {
+  using namespace mshadow;
+  CHECK_EQ(inputs.size(), 1);
+  CHECK_EQ(outputs.size(), 3);
+  if (inputs[0].shape_.Size() == 0) {
+return;
+  }
+  MSHADOW_SGL_DBL_TYPE_SWITCH(outputs[0].type_flag_, OType, {
+mshadow::Stream *s = ctx.get_stream();
+laop::op(inputs[0].FlatToKD(s),
+ outputs[0].FlatToKD(s),
+ outputs[1].FlatToKD(s),
+ outputs[2].FlatToKD(s), ctx, attrs);
+  });
+}
+
+// Helper for gesvd_backward. See technical report for details
 
 Review comment:
   Ah, you are right. I will have the new report version being uploaded.


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:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-mxnet] mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd

2019-08-13 Thread GitBox
mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd
URL: https://github.com/apache/incubator-mxnet/pull/15795#discussion_r313421187
 
 

 ##
 File path: src/operator/numpy/linalg/np_gesvd-inl.h
 ##
 @@ -0,0 +1,298 @@
+/*
+ * 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) 2017 by Contributors
+ * \file np_gesvd-inl.h
+ * \brief Function definition of the SVD Operator.
+ */
+#ifndef MXNET_OPERATOR_NUMPY_LINALG_NP_GESVD_INL_H_
+#define MXNET_OPERATOR_NUMPY_LINALG_NP_GESVD_INL_H_
+
+#include 
+#include 
+#include "../../tensor/la_op.h"
+#include "../../tensor/la_op-inl.h"
+
+namespace mxnet {
+namespace op {
+
+struct GesvdVecSign {
+  template
+  MSHADOW_XINLINE static void Map(int i, int m, int n, DType* UT,
+  DType* V, int ldut, int ldv) {
+DType* vrow(V + i * ldv);
+DType maxval(fabs(vrow[0])), vval(0.0);
+int maxind(0);
+for (int i = 1; i < n; ++i) {
+  vval = fabs(vrow[i]);
+  if (vval > maxval) {
+maxval = vval;
+maxind = i;
+  }
+}
+if (vrow[maxind] < 0) {
+  DType* utcol(UT + i % m + (i / m) * ldut * m);
+  for (int i = 0; i < n; ++i) {
+vrow[i] = -vrow[i];
+if (i < m) {
+  utcol[i * ldut] = -utcol[i * ldut];
+}
+  }
+}
+  }
+};
+
+// (UT, L, V) = gesvd(A) [singular value decomposition]
+// - V can overwrite A
+// - Needs workspace (both DType and int), size of which is determined by a
+//   workspace query
+struct gesvd {
+  template
+  static void op(const Tensor& A,
+ const Tensor& UT,
+ const Tensor& L,
+ const Tensor& V,
+ const OpContext& ctx,
+ const nnvm::NodeAttrs& attrs) {
+Stream *s = ctx.get_stream();
+if (A.dptr_ != V.dptr_) Copy(V, A, s);
+// From here on, we work on V only
+// Reserve workspace (size determined by query)
+int lwork(linalg_gesvd_workspace_query(UT[0], L[0], V[0], s));
+Tensor work = ctx.requested[0]
+  .get_space_typed(Shape1(lwork), s);
+// Loop over items in batch
+for (index_t i = 0; i < UT.size(0); ++i) {
+  linalg_gesvd(UT[i], L[i], V[i], work, s);
+}
+// Set signs in a deterministic way
+using namespace mxnet_op;
+Kernel::Launch
+  (s, V.size(0) * V.size(1), V.size(1), V.size(2),
+   UT.dptr_, V.dptr_, UT.stride_, V.stride_);
+  }
+};
+
+// (A) => (UT, L, V)
+template
+void NumpyLaGesvdForward(const nnvm::NodeAttrs& attrs,
+ const OpContext& ctx,
+ const std::vector& inputs,
+ const std::vector& req,
+ const std::vector& outputs) {
+  using namespace mshadow;
+  CHECK_EQ(inputs.size(), 1);
+  CHECK_EQ(outputs.size(), 3);
+  if (inputs[0].shape_.Size() == 0) {
+return;
+  }
+  MSHADOW_SGL_DBL_TYPE_SWITCH(outputs[0].type_flag_, OType, {
+mshadow::Stream *s = ctx.get_stream();
+laop::op(inputs[0].FlatToKD(s),
+ outputs[0].FlatToKD(s),
+ outputs[1].FlatToKD(s),
+ outputs[2].FlatToKD(s), ctx, attrs);
+  });
+}
+
+// Helper for gesvd_backward. See technical report for details
+template
+DType gesvd_back_helper_eps(DType* X);
+
+template<>
+MSHADOW_XINLINE float gesvd_back_helper_eps(float* X) {
+  return 1e-30;
+}
+
+template<>
+MSHADOW_XINLINE double gesvd_back_helper_eps(double* X) {
+  return 1e-100;
+}
+
+struct GesvdBackHelper_dV {
+  template
+  MSHADOW_XINLINE static void Map(int k, int m, int n, DType* L, int ldl,
+  DType* dA, int ldda) {
+const int offl(k * ldl);
+const int offda(k * m * ldda);
+DType denom(0.0);
+const DType eps(gesvd_back_helper_eps(dA));
+for (int i = 0; i < m; ++i) {
+  denom = L[offl + i];
+  if (denom < eps) denom = eps;
+  for (int j = 0; j < n; ++j) {
+dA[offda + i * ldda + j] /= denom;
+  }
+}
+  }
+};
+
+struct GesvdBackHelper_G1 {
+  template
+  MSHADOW_XINLINE static void Map(int k, int m, int n, DType* X, int ldx,
+  DType* L, int ldl) {
+const int offl(k * ldl);
+const int 

[GitHub] [incubator-mxnet] mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd

2019-08-13 Thread GitBox
mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd
URL: https://github.com/apache/incubator-mxnet/pull/15795#discussion_r313420294
 
 

 ##
 File path: src/operator/numpy/linalg/np_gesvd-inl.h
 ##
 @@ -0,0 +1,298 @@
+/*
+ * 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) 2017 by Contributors
+ * \file np_gesvd-inl.h
+ * \brief Function definition of the SVD Operator.
+ */
+#ifndef MXNET_OPERATOR_NUMPY_LINALG_NP_GESVD_INL_H_
+#define MXNET_OPERATOR_NUMPY_LINALG_NP_GESVD_INL_H_
+
+#include 
+#include 
+#include "../../tensor/la_op.h"
+#include "../../tensor/la_op-inl.h"
+
+namespace mxnet {
+namespace op {
+
+struct GesvdVecSign {
+  template
+  MSHADOW_XINLINE static void Map(int i, int m, int n, DType* UT,
+  DType* V, int ldut, int ldv) {
+DType* vrow(V + i * ldv);
+DType maxval(fabs(vrow[0])), vval(0.0);
+int maxind(0);
+for (int i = 1; i < n; ++i) {
+  vval = fabs(vrow[i]);
+  if (vval > maxval) {
+maxval = vval;
+maxind = i;
+  }
+}
+if (vrow[maxind] < 0) {
+  DType* utcol(UT + i % m + (i / m) * ldut * m);
+  for (int i = 0; i < n; ++i) {
+vrow[i] = -vrow[i];
+if (i < m) {
+  utcol[i * ldut] = -utcol[i * ldut];
+}
+  }
+}
+  }
+};
+
+// (UT, L, V) = gesvd(A) [singular value decomposition]
+// - V can overwrite A
+// - Needs workspace (both DType and int), size of which is determined by a
+//   workspace query
+struct gesvd {
+  template
+  static void op(const Tensor& A,
+ const Tensor& UT,
+ const Tensor& L,
+ const Tensor& V,
+ const OpContext& ctx,
+ const nnvm::NodeAttrs& attrs) {
+Stream *s = ctx.get_stream();
+if (A.dptr_ != V.dptr_) Copy(V, A, s);
+// From here on, we work on V only
+// Reserve workspace (size determined by query)
+int lwork(linalg_gesvd_workspace_query(UT[0], L[0], V[0], s));
+Tensor work = ctx.requested[0]
+  .get_space_typed(Shape1(lwork), s);
+// Loop over items in batch
+for (index_t i = 0; i < UT.size(0); ++i) {
+  linalg_gesvd(UT[i], L[i], V[i], work, s);
+}
+// Set signs in a deterministic way
+using namespace mxnet_op;
+Kernel::Launch
+  (s, V.size(0) * V.size(1), V.size(1), V.size(2),
+   UT.dptr_, V.dptr_, UT.stride_, V.stride_);
+  }
+};
+
+// (A) => (UT, L, V)
+template
+void NumpyLaGesvdForward(const nnvm::NodeAttrs& attrs,
+ const OpContext& ctx,
+ const std::vector& inputs,
+ const std::vector& req,
+ const std::vector& outputs) {
+  using namespace mshadow;
+  CHECK_EQ(inputs.size(), 1);
+  CHECK_EQ(outputs.size(), 3);
+  if (inputs[0].shape_.Size() == 0) {
+return;
+  }
+  MSHADOW_SGL_DBL_TYPE_SWITCH(outputs[0].type_flag_, OType, {
+mshadow::Stream *s = ctx.get_stream();
+laop::op(inputs[0].FlatToKD(s),
+ outputs[0].FlatToKD(s),
+ outputs[1].FlatToKD(s),
+ outputs[2].FlatToKD(s), ctx, attrs);
+  });
+}
+
+// Helper for gesvd_backward. See technical report for details
+template
+DType gesvd_back_helper_eps(DType* X);
+
+template<>
+MSHADOW_XINLINE float gesvd_back_helper_eps(float* X) {
+  return 1e-30;
+}
+
+template<>
+MSHADOW_XINLINE double gesvd_back_helper_eps(double* X) {
+  return 1e-100;
+}
+
+struct GesvdBackHelper_dV {
+  template
+  MSHADOW_XINLINE static void Map(int k, int m, int n, DType* L, int ldl,
+  DType* dA, int ldda) {
+const int offl(k * ldl);
+const int offda(k * m * ldda);
+DType denom(0.0);
+const DType eps(gesvd_back_helper_eps(dA));
+for (int i = 0; i < m; ++i) {
+  denom = L[offl + i];
+  if (denom < eps) denom = eps;
+  for (int j = 0; j < n; ++j) {
+dA[offda + i * ldda + j] /= denom;
+  }
+}
+  }
+};
+
+struct GesvdBackHelper_G1 {
+  template
+  MSHADOW_XINLINE static void Map(int k, int m, int n, DType* X, int ldx,
+  DType* L, int ldl) {
+const int offl(k * ldl);
+const int 

[GitHub] [incubator-mxnet] mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd

2019-08-13 Thread GitBox
mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd
URL: https://github.com/apache/incubator-mxnet/pull/15795#discussion_r313308549
 
 

 ##
 File path: tests/python/unittest/test_numpy_op.py
 ##
 @@ -0,0 +1,131 @@
+# Licensed to the Apache Software Foundation (ASF) under one
 
 Review comment:
   Very nice test!


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:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-mxnet] mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd

2019-08-13 Thread GitBox
mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd
URL: https://github.com/apache/incubator-mxnet/pull/15795#discussion_r313306986
 
 

 ##
 File path: src/operator/numpy/linalg/np_gesvd-inl.h
 ##
 @@ -0,0 +1,298 @@
+/*
+ * 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) 2017 by Contributors
+ * \file np_gesvd-inl.h
+ * \brief Function definition of the SVD Operator.
+ */
+#ifndef MXNET_OPERATOR_NUMPY_LINALG_NP_GESVD_INL_H_
+#define MXNET_OPERATOR_NUMPY_LINALG_NP_GESVD_INL_H_
+
+#include 
+#include 
+#include "../../tensor/la_op.h"
+#include "../../tensor/la_op-inl.h"
+
+namespace mxnet {
+namespace op {
+
+struct GesvdVecSign {
+  template
+  MSHADOW_XINLINE static void Map(int i, int m, int n, DType* UT,
+  DType* V, int ldut, int ldv) {
+DType* vrow(V + i * ldv);
+DType maxval(fabs(vrow[0])), vval(0.0);
+int maxind(0);
+for (int i = 1; i < n; ++i) {
+  vval = fabs(vrow[i]);
+  if (vval > maxval) {
+maxval = vval;
+maxind = i;
+  }
+}
+if (vrow[maxind] < 0) {
+  DType* utcol(UT + i % m + (i / m) * ldut * m);
+  for (int i = 0; i < n; ++i) {
+vrow[i] = -vrow[i];
+if (i < m) {
+  utcol[i * ldut] = -utcol[i * ldut];
+}
+  }
+}
+  }
+};
+
+// (UT, L, V) = gesvd(A) [singular value decomposition]
+// - V can overwrite A
+// - Needs workspace (both DType and int), size of which is determined by a
+//   workspace query
+struct gesvd {
+  template
+  static void op(const Tensor& A,
+ const Tensor& UT,
+ const Tensor& L,
+ const Tensor& V,
+ const OpContext& ctx,
+ const nnvm::NodeAttrs& attrs) {
+Stream *s = ctx.get_stream();
+if (A.dptr_ != V.dptr_) Copy(V, A, s);
+// From here on, we work on V only
+// Reserve workspace (size determined by query)
+int lwork(linalg_gesvd_workspace_query(UT[0], L[0], V[0], s));
+Tensor work = ctx.requested[0]
+  .get_space_typed(Shape1(lwork), s);
+// Loop over items in batch
+for (index_t i = 0; i < UT.size(0); ++i) {
+  linalg_gesvd(UT[i], L[i], V[i], work, s);
+}
+// Set signs in a deterministic way
+using namespace mxnet_op;
+Kernel::Launch
+  (s, V.size(0) * V.size(1), V.size(1), V.size(2),
+   UT.dptr_, V.dptr_, UT.stride_, V.stride_);
+  }
+};
+
+// (A) => (UT, L, V)
+template
+void NumpyLaGesvdForward(const nnvm::NodeAttrs& attrs,
+ const OpContext& ctx,
+ const std::vector& inputs,
+ const std::vector& req,
+ const std::vector& outputs) {
+  using namespace mshadow;
+  CHECK_EQ(inputs.size(), 1);
+  CHECK_EQ(outputs.size(), 3);
+  if (inputs[0].shape_.Size() == 0) {
+return;
+  }
+  MSHADOW_SGL_DBL_TYPE_SWITCH(outputs[0].type_flag_, OType, {
+mshadow::Stream *s = ctx.get_stream();
+laop::op(inputs[0].FlatToKD(s),
+ outputs[0].FlatToKD(s),
+ outputs[1].FlatToKD(s),
+ outputs[2].FlatToKD(s), ctx, attrs);
+  });
+}
+
+// Helper for gesvd_backward. See technical report for details
+template
+DType gesvd_back_helper_eps(DType* X);
+
+template<>
+MSHADOW_XINLINE float gesvd_back_helper_eps(float* X) {
+  return 1e-30;
+}
+
+template<>
+MSHADOW_XINLINE double gesvd_back_helper_eps(double* X) {
+  return 1e-100;
+}
+
+struct GesvdBackHelper_dV {
+  template
+  MSHADOW_XINLINE static void Map(int k, int m, int n, DType* L, int ldl,
+  DType* dA, int ldda) {
+const int offl(k * ldl);
+const int offda(k * m * ldda);
+DType denom(0.0);
+const DType eps(gesvd_back_helper_eps(dA));
+for (int i = 0; i < m; ++i) {
+  denom = L[offl + i];
+  if (denom < eps) denom = eps;
+  for (int j = 0; j < n; ++j) {
+dA[offda + i * ldda + j] /= denom;
+  }
+}
+  }
+};
+
+struct GesvdBackHelper_G1 {
+  template
+  MSHADOW_XINLINE static void Map(int k, int m, int n, DType* X, int ldx,
+  DType* L, int ldl) {
+const int offl(k * ldl);
+const int 

[GitHub] [incubator-mxnet] mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd

2019-08-13 Thread GitBox
mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd
URL: https://github.com/apache/incubator-mxnet/pull/15795#discussion_r313306329
 
 

 ##
 File path: src/operator/numpy/linalg/np_gesvd-inl.h
 ##
 @@ -0,0 +1,298 @@
+/*
+ * 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) 2017 by Contributors
+ * \file np_gesvd-inl.h
+ * \brief Function definition of the SVD Operator.
+ */
+#ifndef MXNET_OPERATOR_NUMPY_LINALG_NP_GESVD_INL_H_
+#define MXNET_OPERATOR_NUMPY_LINALG_NP_GESVD_INL_H_
+
+#include 
+#include 
+#include "../../tensor/la_op.h"
+#include "../../tensor/la_op-inl.h"
+
+namespace mxnet {
+namespace op {
+
+struct GesvdVecSign {
+  template
+  MSHADOW_XINLINE static void Map(int i, int m, int n, DType* UT,
+  DType* V, int ldut, int ldv) {
+DType* vrow(V + i * ldv);
+DType maxval(fabs(vrow[0])), vval(0.0);
+int maxind(0);
+for (int i = 1; i < n; ++i) {
+  vval = fabs(vrow[i]);
+  if (vval > maxval) {
+maxval = vval;
+maxind = i;
+  }
+}
+if (vrow[maxind] < 0) {
+  DType* utcol(UT + i % m + (i / m) * ldut * m);
+  for (int i = 0; i < n; ++i) {
+vrow[i] = -vrow[i];
+if (i < m) {
+  utcol[i * ldut] = -utcol[i * ldut];
+}
+  }
+}
+  }
+};
+
+// (UT, L, V) = gesvd(A) [singular value decomposition]
+// - V can overwrite A
+// - Needs workspace (both DType and int), size of which is determined by a
+//   workspace query
+struct gesvd {
+  template
+  static void op(const Tensor& A,
+ const Tensor& UT,
+ const Tensor& L,
+ const Tensor& V,
+ const OpContext& ctx,
+ const nnvm::NodeAttrs& attrs) {
+Stream *s = ctx.get_stream();
+if (A.dptr_ != V.dptr_) Copy(V, A, s);
+// From here on, we work on V only
+// Reserve workspace (size determined by query)
+int lwork(linalg_gesvd_workspace_query(UT[0], L[0], V[0], s));
+Tensor work = ctx.requested[0]
+  .get_space_typed(Shape1(lwork), s);
+// Loop over items in batch
+for (index_t i = 0; i < UT.size(0); ++i) {
+  linalg_gesvd(UT[i], L[i], V[i], work, s);
+}
+// Set signs in a deterministic way
+using namespace mxnet_op;
+Kernel::Launch
+  (s, V.size(0) * V.size(1), V.size(1), V.size(2),
+   UT.dptr_, V.dptr_, UT.stride_, V.stride_);
+  }
+};
+
+// (A) => (UT, L, V)
+template
+void NumpyLaGesvdForward(const nnvm::NodeAttrs& attrs,
+ const OpContext& ctx,
+ const std::vector& inputs,
+ const std::vector& req,
+ const std::vector& outputs) {
+  using namespace mshadow;
+  CHECK_EQ(inputs.size(), 1);
+  CHECK_EQ(outputs.size(), 3);
+  if (inputs[0].shape_.Size() == 0) {
+return;
+  }
+  MSHADOW_SGL_DBL_TYPE_SWITCH(outputs[0].type_flag_, OType, {
+mshadow::Stream *s = ctx.get_stream();
+laop::op(inputs[0].FlatToKD(s),
+ outputs[0].FlatToKD(s),
+ outputs[1].FlatToKD(s),
+ outputs[2].FlatToKD(s), ctx, attrs);
+  });
+}
+
+// Helper for gesvd_backward. See technical report for details
+template
+DType gesvd_back_helper_eps(DType* X);
+
+template<>
+MSHADOW_XINLINE float gesvd_back_helper_eps(float* X) {
+  return 1e-30;
+}
+
+template<>
+MSHADOW_XINLINE double gesvd_back_helper_eps(double* X) {
+  return 1e-100;
+}
+
+struct GesvdBackHelper_dV {
+  template
+  MSHADOW_XINLINE static void Map(int k, int m, int n, DType* L, int ldl,
+  DType* dA, int ldda) {
+const int offl(k * ldl);
+const int offda(k * m * ldda);
+DType denom(0.0);
+const DType eps(gesvd_back_helper_eps(dA));
+for (int i = 0; i < m; ++i) {
+  denom = L[offl + i];
+  if (denom < eps) denom = eps;
+  for (int j = 0; j < n; ++j) {
+dA[offda + i * ldda + j] /= denom;
+  }
+}
+  }
+};
+
+struct GesvdBackHelper_G1 {
+  template
+  MSHADOW_XINLINE static void Map(int k, int m, int n, DType* X, int ldx,
+  DType* L, int ldl) {
+const int offl(k * ldl);
+const int 

[GitHub] [incubator-mxnet] mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd

2019-08-13 Thread GitBox
mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd
URL: https://github.com/apache/incubator-mxnet/pull/15795#discussion_r313296588
 
 

 ##
 File path: src/operator/numpy/linalg/np_gesvd-inl.h
 ##
 @@ -0,0 +1,298 @@
+/*
+ * 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) 2017 by Contributors
+ * \file np_gesvd-inl.h
+ * \brief Function definition of the SVD Operator.
+ */
+#ifndef MXNET_OPERATOR_NUMPY_LINALG_NP_GESVD_INL_H_
+#define MXNET_OPERATOR_NUMPY_LINALG_NP_GESVD_INL_H_
+
+#include 
+#include 
+#include "../../tensor/la_op.h"
+#include "../../tensor/la_op-inl.h"
+
+namespace mxnet {
+namespace op {
+
+struct GesvdVecSign {
+  template
+  MSHADOW_XINLINE static void Map(int i, int m, int n, DType* UT,
+  DType* V, int ldut, int ldv) {
+DType* vrow(V + i * ldv);
+DType maxval(fabs(vrow[0])), vval(0.0);
+int maxind(0);
+for (int i = 1; i < n; ++i) {
+  vval = fabs(vrow[i]);
+  if (vval > maxval) {
+maxval = vval;
+maxind = i;
+  }
+}
+if (vrow[maxind] < 0) {
+  DType* utcol(UT + i % m + (i / m) * ldut * m);
+  for (int i = 0; i < n; ++i) {
+vrow[i] = -vrow[i];
+if (i < m) {
+  utcol[i * ldut] = -utcol[i * ldut];
+}
+  }
+}
+  }
+};
+
+// (UT, L, V) = gesvd(A) [singular value decomposition]
+// - V can overwrite A
+// - Needs workspace (both DType and int), size of which is determined by a
+//   workspace query
+struct gesvd {
+  template
+  static void op(const Tensor& A,
+ const Tensor& UT,
+ const Tensor& L,
+ const Tensor& V,
+ const OpContext& ctx,
+ const nnvm::NodeAttrs& attrs) {
+Stream *s = ctx.get_stream();
+if (A.dptr_ != V.dptr_) Copy(V, A, s);
+// From here on, we work on V only
+// Reserve workspace (size determined by query)
+int lwork(linalg_gesvd_workspace_query(UT[0], L[0], V[0], s));
+Tensor work = ctx.requested[0]
+  .get_space_typed(Shape1(lwork), s);
+// Loop over items in batch
+for (index_t i = 0; i < UT.size(0); ++i) {
+  linalg_gesvd(UT[i], L[i], V[i], work, s);
+}
+// Set signs in a deterministic way
+using namespace mxnet_op;
+Kernel::Launch
+  (s, V.size(0) * V.size(1), V.size(1), V.size(2),
+   UT.dptr_, V.dptr_, UT.stride_, V.stride_);
+  }
+};
+
+// (A) => (UT, L, V)
+template
+void NumpyLaGesvdForward(const nnvm::NodeAttrs& attrs,
+ const OpContext& ctx,
+ const std::vector& inputs,
+ const std::vector& req,
+ const std::vector& outputs) {
+  using namespace mshadow;
+  CHECK_EQ(inputs.size(), 1);
+  CHECK_EQ(outputs.size(), 3);
+  if (inputs[0].shape_.Size() == 0) {
+return;
+  }
+  MSHADOW_SGL_DBL_TYPE_SWITCH(outputs[0].type_flag_, OType, {
+mshadow::Stream *s = ctx.get_stream();
+laop::op(inputs[0].FlatToKD(s),
+ outputs[0].FlatToKD(s),
+ outputs[1].FlatToKD(s),
+ outputs[2].FlatToKD(s), ctx, attrs);
+  });
+}
+
+// Helper for gesvd_backward. See technical report for details
+template
+DType gesvd_back_helper_eps(DType* X);
+
+template<>
+MSHADOW_XINLINE float gesvd_back_helper_eps(float* X) {
+  return 1e-30;
+}
+
+template<>
+MSHADOW_XINLINE double gesvd_back_helper_eps(double* X) {
+  return 1e-100;
+}
+
+struct GesvdBackHelper_dV {
+  template
+  MSHADOW_XINLINE static void Map(int k, int m, int n, DType* L, int ldl,
+  DType* dA, int ldda) {
+const int offl(k * ldl);
+const int offda(k * m * ldda);
+DType denom(0.0);
+const DType eps(gesvd_back_helper_eps(dA));
+for (int i = 0; i < m; ++i) {
+  denom = L[offl + i];
+  if (denom < eps) denom = eps;
+  for (int j = 0; j < n; ++j) {
+dA[offda + i * ldda + j] /= denom;
+  }
+}
+  }
+};
+
+struct GesvdBackHelper_G1 {
+  template
+  MSHADOW_XINLINE static void Map(int k, int m, int n, DType* X, int ldx,
+  DType* L, int ldl) {
+const int offl(k * ldl);
+const int 

[GitHub] [incubator-mxnet] mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd

2019-08-13 Thread GitBox
mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd
URL: https://github.com/apache/incubator-mxnet/pull/15795#discussion_r313302421
 
 

 ##
 File path: src/operator/numpy/linalg/np_gesvd-inl.h
 ##
 @@ -0,0 +1,298 @@
+/*
+ * 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) 2017 by Contributors
+ * \file np_gesvd-inl.h
+ * \brief Function definition of the SVD Operator.
+ */
+#ifndef MXNET_OPERATOR_NUMPY_LINALG_NP_GESVD_INL_H_
+#define MXNET_OPERATOR_NUMPY_LINALG_NP_GESVD_INL_H_
+
+#include 
+#include 
+#include "../../tensor/la_op.h"
+#include "../../tensor/la_op-inl.h"
+
+namespace mxnet {
+namespace op {
+
+struct GesvdVecSign {
+  template
+  MSHADOW_XINLINE static void Map(int i, int m, int n, DType* UT,
+  DType* V, int ldut, int ldv) {
+DType* vrow(V + i * ldv);
+DType maxval(fabs(vrow[0])), vval(0.0);
+int maxind(0);
+for (int i = 1; i < n; ++i) {
+  vval = fabs(vrow[i]);
+  if (vval > maxval) {
+maxval = vval;
+maxind = i;
+  }
+}
+if (vrow[maxind] < 0) {
+  DType* utcol(UT + i % m + (i / m) * ldut * m);
+  for (int i = 0; i < n; ++i) {
+vrow[i] = -vrow[i];
+if (i < m) {
+  utcol[i * ldut] = -utcol[i * ldut];
+}
+  }
+}
+  }
+};
+
+// (UT, L, V) = gesvd(A) [singular value decomposition]
+// - V can overwrite A
+// - Needs workspace (both DType and int), size of which is determined by a
+//   workspace query
+struct gesvd {
+  template
+  static void op(const Tensor& A,
+ const Tensor& UT,
+ const Tensor& L,
+ const Tensor& V,
+ const OpContext& ctx,
+ const nnvm::NodeAttrs& attrs) {
+Stream *s = ctx.get_stream();
+if (A.dptr_ != V.dptr_) Copy(V, A, s);
+// From here on, we work on V only
+// Reserve workspace (size determined by query)
+int lwork(linalg_gesvd_workspace_query(UT[0], L[0], V[0], s));
+Tensor work = ctx.requested[0]
+  .get_space_typed(Shape1(lwork), s);
+// Loop over items in batch
+for (index_t i = 0; i < UT.size(0); ++i) {
+  linalg_gesvd(UT[i], L[i], V[i], work, s);
+}
+// Set signs in a deterministic way
+using namespace mxnet_op;
+Kernel::Launch
+  (s, V.size(0) * V.size(1), V.size(1), V.size(2),
+   UT.dptr_, V.dptr_, UT.stride_, V.stride_);
+  }
+};
+
+// (A) => (UT, L, V)
+template
+void NumpyLaGesvdForward(const nnvm::NodeAttrs& attrs,
+ const OpContext& ctx,
+ const std::vector& inputs,
+ const std::vector& req,
+ const std::vector& outputs) {
+  using namespace mshadow;
+  CHECK_EQ(inputs.size(), 1);
+  CHECK_EQ(outputs.size(), 3);
+  if (inputs[0].shape_.Size() == 0) {
+return;
+  }
+  MSHADOW_SGL_DBL_TYPE_SWITCH(outputs[0].type_flag_, OType, {
+mshadow::Stream *s = ctx.get_stream();
+laop::op(inputs[0].FlatToKD(s),
+ outputs[0].FlatToKD(s),
+ outputs[1].FlatToKD(s),
+ outputs[2].FlatToKD(s), ctx, attrs);
+  });
+}
+
+// Helper for gesvd_backward. See technical report for details
+template
+DType gesvd_back_helper_eps(DType* X);
+
+template<>
+MSHADOW_XINLINE float gesvd_back_helper_eps(float* X) {
+  return 1e-30;
+}
+
+template<>
+MSHADOW_XINLINE double gesvd_back_helper_eps(double* X) {
+  return 1e-100;
+}
+
+struct GesvdBackHelper_dV {
+  template
+  MSHADOW_XINLINE static void Map(int k, int m, int n, DType* L, int ldl,
+  DType* dA, int ldda) {
+const int offl(k * ldl);
+const int offda(k * m * ldda);
+DType denom(0.0);
+const DType eps(gesvd_back_helper_eps(dA));
+for (int i = 0; i < m; ++i) {
+  denom = L[offl + i];
+  if (denom < eps) denom = eps;
+  for (int j = 0; j < n; ++j) {
+dA[offda + i * ldda + j] /= denom;
+  }
+}
+  }
+};
+
+struct GesvdBackHelper_G1 {
+  template
+  MSHADOW_XINLINE static void Map(int k, int m, int n, DType* X, int ldx,
+  DType* L, int ldl) {
+const int offl(k * ldl);
+const int 

[GitHub] [incubator-mxnet] mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd

2019-08-13 Thread GitBox
mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd
URL: https://github.com/apache/incubator-mxnet/pull/15795#discussion_r313301558
 
 

 ##
 File path: src/operator/numpy/linalg/np_gesvd-inl.h
 ##
 @@ -0,0 +1,298 @@
+/*
+ * 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) 2017 by Contributors
+ * \file np_gesvd-inl.h
+ * \brief Function definition of the SVD Operator.
+ */
+#ifndef MXNET_OPERATOR_NUMPY_LINALG_NP_GESVD_INL_H_
+#define MXNET_OPERATOR_NUMPY_LINALG_NP_GESVD_INL_H_
+
+#include 
+#include 
+#include "../../tensor/la_op.h"
+#include "../../tensor/la_op-inl.h"
+
+namespace mxnet {
+namespace op {
+
+struct GesvdVecSign {
+  template
+  MSHADOW_XINLINE static void Map(int i, int m, int n, DType* UT,
+  DType* V, int ldut, int ldv) {
+DType* vrow(V + i * ldv);
+DType maxval(fabs(vrow[0])), vval(0.0);
+int maxind(0);
+for (int i = 1; i < n; ++i) {
+  vval = fabs(vrow[i]);
+  if (vval > maxval) {
+maxval = vval;
+maxind = i;
+  }
+}
+if (vrow[maxind] < 0) {
+  DType* utcol(UT + i % m + (i / m) * ldut * m);
+  for (int i = 0; i < n; ++i) {
+vrow[i] = -vrow[i];
+if (i < m) {
+  utcol[i * ldut] = -utcol[i * ldut];
+}
+  }
+}
+  }
+};
+
+// (UT, L, V) = gesvd(A) [singular value decomposition]
+// - V can overwrite A
+// - Needs workspace (both DType and int), size of which is determined by a
+//   workspace query
+struct gesvd {
+  template
+  static void op(const Tensor& A,
+ const Tensor& UT,
+ const Tensor& L,
+ const Tensor& V,
+ const OpContext& ctx,
+ const nnvm::NodeAttrs& attrs) {
+Stream *s = ctx.get_stream();
+if (A.dptr_ != V.dptr_) Copy(V, A, s);
+// From here on, we work on V only
+// Reserve workspace (size determined by query)
+int lwork(linalg_gesvd_workspace_query(UT[0], L[0], V[0], s));
+Tensor work = ctx.requested[0]
+  .get_space_typed(Shape1(lwork), s);
+// Loop over items in batch
+for (index_t i = 0; i < UT.size(0); ++i) {
+  linalg_gesvd(UT[i], L[i], V[i], work, s);
+}
+// Set signs in a deterministic way
+using namespace mxnet_op;
+Kernel::Launch
+  (s, V.size(0) * V.size(1), V.size(1), V.size(2),
+   UT.dptr_, V.dptr_, UT.stride_, V.stride_);
+  }
+};
+
+// (A) => (UT, L, V)
+template
+void NumpyLaGesvdForward(const nnvm::NodeAttrs& attrs,
+ const OpContext& ctx,
+ const std::vector& inputs,
+ const std::vector& req,
+ const std::vector& outputs) {
+  using namespace mshadow;
+  CHECK_EQ(inputs.size(), 1);
+  CHECK_EQ(outputs.size(), 3);
+  if (inputs[0].shape_.Size() == 0) {
+return;
+  }
+  MSHADOW_SGL_DBL_TYPE_SWITCH(outputs[0].type_flag_, OType, {
+mshadow::Stream *s = ctx.get_stream();
+laop::op(inputs[0].FlatToKD(s),
+ outputs[0].FlatToKD(s),
+ outputs[1].FlatToKD(s),
+ outputs[2].FlatToKD(s), ctx, attrs);
+  });
+}
+
+// Helper for gesvd_backward. See technical report for details
+template
+DType gesvd_back_helper_eps(DType* X);
+
+template<>
+MSHADOW_XINLINE float gesvd_back_helper_eps(float* X) {
+  return 1e-30;
+}
+
+template<>
+MSHADOW_XINLINE double gesvd_back_helper_eps(double* X) {
+  return 1e-100;
+}
+
+struct GesvdBackHelper_dV {
+  template
+  MSHADOW_XINLINE static void Map(int k, int m, int n, DType* L, int ldl,
+  DType* dA, int ldda) {
+const int offl(k * ldl);
+const int offda(k * m * ldda);
+DType denom(0.0);
+const DType eps(gesvd_back_helper_eps(dA));
+for (int i = 0; i < m; ++i) {
+  denom = L[offl + i];
+  if (denom < eps) denom = eps;
+  for (int j = 0; j < n; ++j) {
+dA[offda + i * ldda + j] /= denom;
+  }
+}
+  }
+};
+
+struct GesvdBackHelper_G1 {
+  template
+  MSHADOW_XINLINE static void Map(int k, int m, int n, DType* X, int ldx,
+  DType* L, int ldl) {
+const int offl(k * ldl);
+const int 

[GitHub] [incubator-mxnet] mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd

2019-08-13 Thread GitBox
mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd
URL: https://github.com/apache/incubator-mxnet/pull/15795#discussion_r313300576
 
 

 ##
 File path: src/operator/numpy/linalg/np_gesvd-inl.h
 ##
 @@ -0,0 +1,298 @@
+/*
+ * 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) 2017 by Contributors
+ * \file np_gesvd-inl.h
+ * \brief Function definition of the SVD Operator.
+ */
+#ifndef MXNET_OPERATOR_NUMPY_LINALG_NP_GESVD_INL_H_
+#define MXNET_OPERATOR_NUMPY_LINALG_NP_GESVD_INL_H_
+
+#include 
+#include 
+#include "../../tensor/la_op.h"
+#include "../../tensor/la_op-inl.h"
+
+namespace mxnet {
+namespace op {
+
+struct GesvdVecSign {
+  template
+  MSHADOW_XINLINE static void Map(int i, int m, int n, DType* UT,
+  DType* V, int ldut, int ldv) {
+DType* vrow(V + i * ldv);
+DType maxval(fabs(vrow[0])), vval(0.0);
+int maxind(0);
+for (int i = 1; i < n; ++i) {
+  vval = fabs(vrow[i]);
+  if (vval > maxval) {
+maxval = vval;
+maxind = i;
+  }
+}
+if (vrow[maxind] < 0) {
+  DType* utcol(UT + i % m + (i / m) * ldut * m);
+  for (int i = 0; i < n; ++i) {
+vrow[i] = -vrow[i];
+if (i < m) {
+  utcol[i * ldut] = -utcol[i * ldut];
+}
+  }
+}
+  }
+};
+
+// (UT, L, V) = gesvd(A) [singular value decomposition]
+// - V can overwrite A
+// - Needs workspace (both DType and int), size of which is determined by a
+//   workspace query
+struct gesvd {
+  template
+  static void op(const Tensor& A,
+ const Tensor& UT,
+ const Tensor& L,
+ const Tensor& V,
+ const OpContext& ctx,
+ const nnvm::NodeAttrs& attrs) {
+Stream *s = ctx.get_stream();
+if (A.dptr_ != V.dptr_) Copy(V, A, s);
+// From here on, we work on V only
+// Reserve workspace (size determined by query)
+int lwork(linalg_gesvd_workspace_query(UT[0], L[0], V[0], s));
+Tensor work = ctx.requested[0]
+  .get_space_typed(Shape1(lwork), s);
+// Loop over items in batch
+for (index_t i = 0; i < UT.size(0); ++i) {
+  linalg_gesvd(UT[i], L[i], V[i], work, s);
+}
+// Set signs in a deterministic way
+using namespace mxnet_op;
+Kernel::Launch
+  (s, V.size(0) * V.size(1), V.size(1), V.size(2),
+   UT.dptr_, V.dptr_, UT.stride_, V.stride_);
+  }
+};
+
+// (A) => (UT, L, V)
+template
+void NumpyLaGesvdForward(const nnvm::NodeAttrs& attrs,
+ const OpContext& ctx,
+ const std::vector& inputs,
+ const std::vector& req,
+ const std::vector& outputs) {
+  using namespace mshadow;
+  CHECK_EQ(inputs.size(), 1);
+  CHECK_EQ(outputs.size(), 3);
+  if (inputs[0].shape_.Size() == 0) {
+return;
+  }
+  MSHADOW_SGL_DBL_TYPE_SWITCH(outputs[0].type_flag_, OType, {
+mshadow::Stream *s = ctx.get_stream();
+laop::op(inputs[0].FlatToKD(s),
+ outputs[0].FlatToKD(s),
+ outputs[1].FlatToKD(s),
+ outputs[2].FlatToKD(s), ctx, attrs);
+  });
+}
+
+// Helper for gesvd_backward. See technical report for details
+template
+DType gesvd_back_helper_eps(DType* X);
+
+template<>
+MSHADOW_XINLINE float gesvd_back_helper_eps(float* X) {
+  return 1e-30;
+}
+
+template<>
+MSHADOW_XINLINE double gesvd_back_helper_eps(double* X) {
+  return 1e-100;
+}
+
+struct GesvdBackHelper_dV {
+  template
+  MSHADOW_XINLINE static void Map(int k, int m, int n, DType* L, int ldl,
+  DType* dA, int ldda) {
+const int offl(k * ldl);
+const int offda(k * m * ldda);
+DType denom(0.0);
+const DType eps(gesvd_back_helper_eps(dA));
+for (int i = 0; i < m; ++i) {
+  denom = L[offl + i];
+  if (denom < eps) denom = eps;
+  for (int j = 0; j < n; ++j) {
+dA[offda + i * ldda + j] /= denom;
+  }
+}
+  }
+};
+
+struct GesvdBackHelper_G1 {
+  template
+  MSHADOW_XINLINE static void Map(int k, int m, int n, DType* X, int ldx,
+  DType* L, int ldl) {
+const int offl(k * ldl);
+const int 

[GitHub] [incubator-mxnet] mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd

2019-08-13 Thread GitBox
mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd
URL: https://github.com/apache/incubator-mxnet/pull/15795#discussion_r313300055
 
 

 ##
 File path: src/operator/numpy/linalg/np_gesvd-inl.h
 ##
 @@ -0,0 +1,298 @@
+/*
+ * 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) 2017 by Contributors
+ * \file np_gesvd-inl.h
+ * \brief Function definition of the SVD Operator.
+ */
+#ifndef MXNET_OPERATOR_NUMPY_LINALG_NP_GESVD_INL_H_
+#define MXNET_OPERATOR_NUMPY_LINALG_NP_GESVD_INL_H_
+
+#include 
+#include 
+#include "../../tensor/la_op.h"
+#include "../../tensor/la_op-inl.h"
+
+namespace mxnet {
+namespace op {
+
+struct GesvdVecSign {
+  template
+  MSHADOW_XINLINE static void Map(int i, int m, int n, DType* UT,
+  DType* V, int ldut, int ldv) {
+DType* vrow(V + i * ldv);
+DType maxval(fabs(vrow[0])), vval(0.0);
+int maxind(0);
+for (int i = 1; i < n; ++i) {
+  vval = fabs(vrow[i]);
+  if (vval > maxval) {
+maxval = vval;
+maxind = i;
+  }
+}
+if (vrow[maxind] < 0) {
+  DType* utcol(UT + i % m + (i / m) * ldut * m);
+  for (int i = 0; i < n; ++i) {
+vrow[i] = -vrow[i];
+if (i < m) {
+  utcol[i * ldut] = -utcol[i * ldut];
+}
+  }
+}
+  }
+};
+
+// (UT, L, V) = gesvd(A) [singular value decomposition]
+// - V can overwrite A
+// - Needs workspace (both DType and int), size of which is determined by a
+//   workspace query
+struct gesvd {
+  template
+  static void op(const Tensor& A,
+ const Tensor& UT,
+ const Tensor& L,
+ const Tensor& V,
+ const OpContext& ctx,
+ const nnvm::NodeAttrs& attrs) {
+Stream *s = ctx.get_stream();
+if (A.dptr_ != V.dptr_) Copy(V, A, s);
+// From here on, we work on V only
+// Reserve workspace (size determined by query)
+int lwork(linalg_gesvd_workspace_query(UT[0], L[0], V[0], s));
+Tensor work = ctx.requested[0]
+  .get_space_typed(Shape1(lwork), s);
+// Loop over items in batch
+for (index_t i = 0; i < UT.size(0); ++i) {
+  linalg_gesvd(UT[i], L[i], V[i], work, s);
+}
+// Set signs in a deterministic way
+using namespace mxnet_op;
+Kernel::Launch
+  (s, V.size(0) * V.size(1), V.size(1), V.size(2),
+   UT.dptr_, V.dptr_, UT.stride_, V.stride_);
+  }
+};
+
+// (A) => (UT, L, V)
+template
+void NumpyLaGesvdForward(const nnvm::NodeAttrs& attrs,
+ const OpContext& ctx,
+ const std::vector& inputs,
+ const std::vector& req,
+ const std::vector& outputs) {
+  using namespace mshadow;
+  CHECK_EQ(inputs.size(), 1);
+  CHECK_EQ(outputs.size(), 3);
+  if (inputs[0].shape_.Size() == 0) {
+return;
+  }
+  MSHADOW_SGL_DBL_TYPE_SWITCH(outputs[0].type_flag_, OType, {
+mshadow::Stream *s = ctx.get_stream();
+laop::op(inputs[0].FlatToKD(s),
+ outputs[0].FlatToKD(s),
+ outputs[1].FlatToKD(s),
+ outputs[2].FlatToKD(s), ctx, attrs);
+  });
+}
+
+// Helper for gesvd_backward. See technical report for details
+template
+DType gesvd_back_helper_eps(DType* X);
+
+template<>
+MSHADOW_XINLINE float gesvd_back_helper_eps(float* X) {
+  return 1e-30;
+}
+
+template<>
+MSHADOW_XINLINE double gesvd_back_helper_eps(double* X) {
+  return 1e-100;
+}
+
+struct GesvdBackHelper_dV {
+  template
+  MSHADOW_XINLINE static void Map(int k, int m, int n, DType* L, int ldl,
+  DType* dA, int ldda) {
+const int offl(k * ldl);
+const int offda(k * m * ldda);
+DType denom(0.0);
+const DType eps(gesvd_back_helper_eps(dA));
+for (int i = 0; i < m; ++i) {
+  denom = L[offl + i];
+  if (denom < eps) denom = eps;
+  for (int j = 0; j < n; ++j) {
+dA[offda + i * ldda + j] /= denom;
+  }
+}
+  }
+};
+
+struct GesvdBackHelper_G1 {
+  template
+  MSHADOW_XINLINE static void Map(int k, int m, int n, DType* X, int ldx,
+  DType* L, int ldl) {
+const int offl(k * ldl);
+const int 

[GitHub] [incubator-mxnet] mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd

2019-08-13 Thread GitBox
mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd
URL: https://github.com/apache/incubator-mxnet/pull/15795#discussion_r313296588
 
 

 ##
 File path: src/operator/numpy/linalg/np_gesvd-inl.h
 ##
 @@ -0,0 +1,298 @@
+/*
+ * 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) 2017 by Contributors
+ * \file np_gesvd-inl.h
+ * \brief Function definition of the SVD Operator.
+ */
+#ifndef MXNET_OPERATOR_NUMPY_LINALG_NP_GESVD_INL_H_
+#define MXNET_OPERATOR_NUMPY_LINALG_NP_GESVD_INL_H_
+
+#include 
+#include 
+#include "../../tensor/la_op.h"
+#include "../../tensor/la_op-inl.h"
+
+namespace mxnet {
+namespace op {
+
+struct GesvdVecSign {
+  template
+  MSHADOW_XINLINE static void Map(int i, int m, int n, DType* UT,
+  DType* V, int ldut, int ldv) {
+DType* vrow(V + i * ldv);
+DType maxval(fabs(vrow[0])), vval(0.0);
+int maxind(0);
+for (int i = 1; i < n; ++i) {
+  vval = fabs(vrow[i]);
+  if (vval > maxval) {
+maxval = vval;
+maxind = i;
+  }
+}
+if (vrow[maxind] < 0) {
+  DType* utcol(UT + i % m + (i / m) * ldut * m);
+  for (int i = 0; i < n; ++i) {
+vrow[i] = -vrow[i];
+if (i < m) {
+  utcol[i * ldut] = -utcol[i * ldut];
+}
+  }
+}
+  }
+};
+
+// (UT, L, V) = gesvd(A) [singular value decomposition]
+// - V can overwrite A
+// - Needs workspace (both DType and int), size of which is determined by a
+//   workspace query
+struct gesvd {
+  template
+  static void op(const Tensor& A,
+ const Tensor& UT,
+ const Tensor& L,
+ const Tensor& V,
+ const OpContext& ctx,
+ const nnvm::NodeAttrs& attrs) {
+Stream *s = ctx.get_stream();
+if (A.dptr_ != V.dptr_) Copy(V, A, s);
+// From here on, we work on V only
+// Reserve workspace (size determined by query)
+int lwork(linalg_gesvd_workspace_query(UT[0], L[0], V[0], s));
+Tensor work = ctx.requested[0]
+  .get_space_typed(Shape1(lwork), s);
+// Loop over items in batch
+for (index_t i = 0; i < UT.size(0); ++i) {
+  linalg_gesvd(UT[i], L[i], V[i], work, s);
+}
+// Set signs in a deterministic way
+using namespace mxnet_op;
+Kernel::Launch
+  (s, V.size(0) * V.size(1), V.size(1), V.size(2),
+   UT.dptr_, V.dptr_, UT.stride_, V.stride_);
+  }
+};
+
+// (A) => (UT, L, V)
+template
+void NumpyLaGesvdForward(const nnvm::NodeAttrs& attrs,
+ const OpContext& ctx,
+ const std::vector& inputs,
+ const std::vector& req,
+ const std::vector& outputs) {
+  using namespace mshadow;
+  CHECK_EQ(inputs.size(), 1);
+  CHECK_EQ(outputs.size(), 3);
+  if (inputs[0].shape_.Size() == 0) {
+return;
+  }
+  MSHADOW_SGL_DBL_TYPE_SWITCH(outputs[0].type_flag_, OType, {
+mshadow::Stream *s = ctx.get_stream();
+laop::op(inputs[0].FlatToKD(s),
+ outputs[0].FlatToKD(s),
+ outputs[1].FlatToKD(s),
+ outputs[2].FlatToKD(s), ctx, attrs);
+  });
+}
+
+// Helper for gesvd_backward. See technical report for details
+template
+DType gesvd_back_helper_eps(DType* X);
+
+template<>
+MSHADOW_XINLINE float gesvd_back_helper_eps(float* X) {
+  return 1e-30;
+}
+
+template<>
+MSHADOW_XINLINE double gesvd_back_helper_eps(double* X) {
+  return 1e-100;
+}
+
+struct GesvdBackHelper_dV {
+  template
+  MSHADOW_XINLINE static void Map(int k, int m, int n, DType* L, int ldl,
+  DType* dA, int ldda) {
+const int offl(k * ldl);
+const int offda(k * m * ldda);
+DType denom(0.0);
+const DType eps(gesvd_back_helper_eps(dA));
+for (int i = 0; i < m; ++i) {
+  denom = L[offl + i];
+  if (denom < eps) denom = eps;
+  for (int j = 0; j < n; ++j) {
+dA[offda + i * ldda + j] /= denom;
+  }
+}
+  }
+};
+
+struct GesvdBackHelper_G1 {
+  template
+  MSHADOW_XINLINE static void Map(int k, int m, int n, DType* X, int ldx,
+  DType* L, int ldl) {
+const int offl(k * ldl);
+const int 

[GitHub] [incubator-mxnet] mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd

2019-08-13 Thread GitBox
mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd
URL: https://github.com/apache/incubator-mxnet/pull/15795#discussion_r313295226
 
 

 ##
 File path: src/operator/numpy/linalg/np_gesvd-inl.h
 ##
 @@ -0,0 +1,298 @@
+/*
+ * 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) 2017 by Contributors
+ * \file np_gesvd-inl.h
+ * \brief Function definition of the SVD Operator.
+ */
+#ifndef MXNET_OPERATOR_NUMPY_LINALG_NP_GESVD_INL_H_
+#define MXNET_OPERATOR_NUMPY_LINALG_NP_GESVD_INL_H_
+
+#include 
+#include 
+#include "../../tensor/la_op.h"
+#include "../../tensor/la_op-inl.h"
+
+namespace mxnet {
+namespace op {
+
+struct GesvdVecSign {
+  template
+  MSHADOW_XINLINE static void Map(int i, int m, int n, DType* UT,
+  DType* V, int ldut, int ldv) {
+DType* vrow(V + i * ldv);
+DType maxval(fabs(vrow[0])), vval(0.0);
+int maxind(0);
+for (int i = 1; i < n; ++i) {
+  vval = fabs(vrow[i]);
+  if (vval > maxval) {
+maxval = vval;
+maxind = i;
+  }
+}
+if (vrow[maxind] < 0) {
+  DType* utcol(UT + i % m + (i / m) * ldut * m);
+  for (int i = 0; i < n; ++i) {
+vrow[i] = -vrow[i];
+if (i < m) {
+  utcol[i * ldut] = -utcol[i * ldut];
+}
+  }
+}
+  }
+};
+
+// (UT, L, V) = gesvd(A) [singular value decomposition]
+// - V can overwrite A
+// - Needs workspace (both DType and int), size of which is determined by a
+//   workspace query
+struct gesvd {
+  template
+  static void op(const Tensor& A,
+ const Tensor& UT,
+ const Tensor& L,
+ const Tensor& V,
+ const OpContext& ctx,
+ const nnvm::NodeAttrs& attrs) {
+Stream *s = ctx.get_stream();
+if (A.dptr_ != V.dptr_) Copy(V, A, s);
+// From here on, we work on V only
+// Reserve workspace (size determined by query)
+int lwork(linalg_gesvd_workspace_query(UT[0], L[0], V[0], s));
+Tensor work = ctx.requested[0]
+  .get_space_typed(Shape1(lwork), s);
+// Loop over items in batch
+for (index_t i = 0; i < UT.size(0); ++i) {
+  linalg_gesvd(UT[i], L[i], V[i], work, s);
+}
+// Set signs in a deterministic way
+using namespace mxnet_op;
+Kernel::Launch
+  (s, V.size(0) * V.size(1), V.size(1), V.size(2),
+   UT.dptr_, V.dptr_, UT.stride_, V.stride_);
+  }
+};
+
+// (A) => (UT, L, V)
+template
+void NumpyLaGesvdForward(const nnvm::NodeAttrs& attrs,
+ const OpContext& ctx,
+ const std::vector& inputs,
+ const std::vector& req,
+ const std::vector& outputs) {
+  using namespace mshadow;
+  CHECK_EQ(inputs.size(), 1);
+  CHECK_EQ(outputs.size(), 3);
+  if (inputs[0].shape_.Size() == 0) {
+return;
+  }
+  MSHADOW_SGL_DBL_TYPE_SWITCH(outputs[0].type_flag_, OType, {
+mshadow::Stream *s = ctx.get_stream();
+laop::op(inputs[0].FlatToKD(s),
+ outputs[0].FlatToKD(s),
+ outputs[1].FlatToKD(s),
+ outputs[2].FlatToKD(s), ctx, attrs);
+  });
+}
+
+// Helper for gesvd_backward. See technical report for details
+template
+DType gesvd_back_helper_eps(DType* X);
+
+template<>
+MSHADOW_XINLINE float gesvd_back_helper_eps(float* X) {
+  return 1e-30;
+}
+
+template<>
+MSHADOW_XINLINE double gesvd_back_helper_eps(double* X) {
+  return 1e-100;
+}
+
+struct GesvdBackHelper_dV {
+  template
+  MSHADOW_XINLINE static void Map(int k, int m, int n, DType* L, int ldl,
+  DType* dA, int ldda) {
+const int offl(k * ldl);
+const int offda(k * m * ldda);
+DType denom(0.0);
+const DType eps(gesvd_back_helper_eps(dA));
+for (int i = 0; i < m; ++i) {
+  denom = L[offl + i];
+  if (denom < eps) denom = eps;
+  for (int j = 0; j < n; ++j) {
+dA[offda + i * ldda + j] /= denom;
+  }
+}
+  }
+};
+
+struct GesvdBackHelper_G1 {
+  template
+  MSHADOW_XINLINE static void Map(int k, int m, int n, DType* X, int ldx,
+  DType* L, int ldl) {
+const int offl(k * ldl);
+const int 

[GitHub] [incubator-mxnet] mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd

2019-08-13 Thread GitBox
mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd
URL: https://github.com/apache/incubator-mxnet/pull/15795#discussion_r313294176
 
 

 ##
 File path: src/operator/numpy/linalg/np_gesvd-inl.h
 ##
 @@ -0,0 +1,298 @@
+/*
+ * 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) 2017 by Contributors
+ * \file np_gesvd-inl.h
+ * \brief Function definition of the SVD Operator.
+ */
+#ifndef MXNET_OPERATOR_NUMPY_LINALG_NP_GESVD_INL_H_
+#define MXNET_OPERATOR_NUMPY_LINALG_NP_GESVD_INL_H_
+
+#include 
+#include 
+#include "../../tensor/la_op.h"
+#include "../../tensor/la_op-inl.h"
+
+namespace mxnet {
+namespace op {
+
+struct GesvdVecSign {
+  template
+  MSHADOW_XINLINE static void Map(int i, int m, int n, DType* UT,
+  DType* V, int ldut, int ldv) {
+DType* vrow(V + i * ldv);
+DType maxval(fabs(vrow[0])), vval(0.0);
+int maxind(0);
+for (int i = 1; i < n; ++i) {
+  vval = fabs(vrow[i]);
+  if (vval > maxval) {
+maxval = vval;
+maxind = i;
+  }
+}
+if (vrow[maxind] < 0) {
+  DType* utcol(UT + i % m + (i / m) * ldut * m);
+  for (int i = 0; i < n; ++i) {
+vrow[i] = -vrow[i];
+if (i < m) {
+  utcol[i * ldut] = -utcol[i * ldut];
+}
+  }
+}
+  }
+};
+
+// (UT, L, V) = gesvd(A) [singular value decomposition]
+// - V can overwrite A
+// - Needs workspace (both DType and int), size of which is determined by a
+//   workspace query
+struct gesvd {
+  template
+  static void op(const Tensor& A,
+ const Tensor& UT,
+ const Tensor& L,
+ const Tensor& V,
+ const OpContext& ctx,
+ const nnvm::NodeAttrs& attrs) {
+Stream *s = ctx.get_stream();
+if (A.dptr_ != V.dptr_) Copy(V, A, s);
+// From here on, we work on V only
+// Reserve workspace (size determined by query)
+int lwork(linalg_gesvd_workspace_query(UT[0], L[0], V[0], s));
+Tensor work = ctx.requested[0]
+  .get_space_typed(Shape1(lwork), s);
+// Loop over items in batch
+for (index_t i = 0; i < UT.size(0); ++i) {
+  linalg_gesvd(UT[i], L[i], V[i], work, s);
+}
+// Set signs in a deterministic way
+using namespace mxnet_op;
+Kernel::Launch
+  (s, V.size(0) * V.size(1), V.size(1), V.size(2),
+   UT.dptr_, V.dptr_, UT.stride_, V.stride_);
+  }
+};
+
+// (A) => (UT, L, V)
+template
+void NumpyLaGesvdForward(const nnvm::NodeAttrs& attrs,
+ const OpContext& ctx,
+ const std::vector& inputs,
+ const std::vector& req,
+ const std::vector& outputs) {
+  using namespace mshadow;
+  CHECK_EQ(inputs.size(), 1);
+  CHECK_EQ(outputs.size(), 3);
+  if (inputs[0].shape_.Size() == 0) {
+return;
+  }
+  MSHADOW_SGL_DBL_TYPE_SWITCH(outputs[0].type_flag_, OType, {
+mshadow::Stream *s = ctx.get_stream();
+laop::op(inputs[0].FlatToKD(s),
+ outputs[0].FlatToKD(s),
+ outputs[1].FlatToKD(s),
+ outputs[2].FlatToKD(s), ctx, attrs);
+  });
+}
+
+// Helper for gesvd_backward. See technical report for details
+template
+DType gesvd_back_helper_eps(DType* X);
+
+template<>
+MSHADOW_XINLINE float gesvd_back_helper_eps(float* X) {
+  return 1e-30;
+}
+
+template<>
+MSHADOW_XINLINE double gesvd_back_helper_eps(double* X) {
+  return 1e-100;
+}
+
+struct GesvdBackHelper_dV {
+  template
+  MSHADOW_XINLINE static void Map(int k, int m, int n, DType* L, int ldl,
+  DType* dA, int ldda) {
+const int offl(k * ldl);
+const int offda(k * m * ldda);
+DType denom(0.0);
+const DType eps(gesvd_back_helper_eps(dA));
+for (int i = 0; i < m; ++i) {
+  denom = L[offl + i];
+  if (denom < eps) denom = eps;
+  for (int j = 0; j < n; ++j) {
+dA[offda + i * ldda + j] /= denom;
+  }
+}
+  }
+};
+
+struct GesvdBackHelper_G1 {
 
 Review comment:
   Comment: X (square) overwritten by L X


This is an automated message from the Apache Git Service.
To 

[GitHub] [incubator-mxnet] mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd

2019-08-13 Thread GitBox
mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd
URL: https://github.com/apache/incubator-mxnet/pull/15795#discussion_r313292924
 
 

 ##
 File path: src/operator/numpy/linalg/np_gesvd-inl.h
 ##
 @@ -0,0 +1,298 @@
+/*
+ * 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) 2017 by Contributors
+ * \file np_gesvd-inl.h
+ * \brief Function definition of the SVD Operator.
+ */
+#ifndef MXNET_OPERATOR_NUMPY_LINALG_NP_GESVD_INL_H_
+#define MXNET_OPERATOR_NUMPY_LINALG_NP_GESVD_INL_H_
+
+#include 
+#include 
+#include "../../tensor/la_op.h"
+#include "../../tensor/la_op-inl.h"
+
+namespace mxnet {
+namespace op {
+
+struct GesvdVecSign {
+  template
+  MSHADOW_XINLINE static void Map(int i, int m, int n, DType* UT,
+  DType* V, int ldut, int ldv) {
+DType* vrow(V + i * ldv);
+DType maxval(fabs(vrow[0])), vval(0.0);
+int maxind(0);
+for (int i = 1; i < n; ++i) {
+  vval = fabs(vrow[i]);
+  if (vval > maxval) {
+maxval = vval;
+maxind = i;
+  }
+}
+if (vrow[maxind] < 0) {
+  DType* utcol(UT + i % m + (i / m) * ldut * m);
+  for (int i = 0; i < n; ++i) {
+vrow[i] = -vrow[i];
+if (i < m) {
+  utcol[i * ldut] = -utcol[i * ldut];
+}
+  }
+}
+  }
+};
+
+// (UT, L, V) = gesvd(A) [singular value decomposition]
+// - V can overwrite A
+// - Needs workspace (both DType and int), size of which is determined by a
+//   workspace query
+struct gesvd {
+  template
+  static void op(const Tensor& A,
+ const Tensor& UT,
+ const Tensor& L,
+ const Tensor& V,
+ const OpContext& ctx,
+ const nnvm::NodeAttrs& attrs) {
+Stream *s = ctx.get_stream();
+if (A.dptr_ != V.dptr_) Copy(V, A, s);
+// From here on, we work on V only
+// Reserve workspace (size determined by query)
+int lwork(linalg_gesvd_workspace_query(UT[0], L[0], V[0], s));
+Tensor work = ctx.requested[0]
+  .get_space_typed(Shape1(lwork), s);
+// Loop over items in batch
+for (index_t i = 0; i < UT.size(0); ++i) {
+  linalg_gesvd(UT[i], L[i], V[i], work, s);
+}
+// Set signs in a deterministic way
+using namespace mxnet_op;
+Kernel::Launch
+  (s, V.size(0) * V.size(1), V.size(1), V.size(2),
+   UT.dptr_, V.dptr_, UT.stride_, V.stride_);
+  }
+};
+
+// (A) => (UT, L, V)
+template
+void NumpyLaGesvdForward(const nnvm::NodeAttrs& attrs,
+ const OpContext& ctx,
+ const std::vector& inputs,
+ const std::vector& req,
+ const std::vector& outputs) {
+  using namespace mshadow;
+  CHECK_EQ(inputs.size(), 1);
+  CHECK_EQ(outputs.size(), 3);
+  if (inputs[0].shape_.Size() == 0) {
+return;
+  }
+  MSHADOW_SGL_DBL_TYPE_SWITCH(outputs[0].type_flag_, OType, {
+mshadow::Stream *s = ctx.get_stream();
+laop::op(inputs[0].FlatToKD(s),
+ outputs[0].FlatToKD(s),
+ outputs[1].FlatToKD(s),
+ outputs[2].FlatToKD(s), ctx, attrs);
+  });
+}
+
+// Helper for gesvd_backward. See technical report for details
+template
+DType gesvd_back_helper_eps(DType* X);
+
+template<>
+MSHADOW_XINLINE float gesvd_back_helper_eps(float* X) {
+  return 1e-30;
+}
+
+template<>
+MSHADOW_XINLINE double gesvd_back_helper_eps(double* X) {
+  return 1e-100;
+}
+
+struct GesvdBackHelper_dV {
 
 Review comment:
   Comment: dA overwritten by L^-1 dA


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:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-mxnet] mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd

2019-08-13 Thread GitBox
mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd
URL: https://github.com/apache/incubator-mxnet/pull/15795#discussion_r313288039
 
 

 ##
 File path: src/operator/numpy/linalg/np_gesvd-inl.h
 ##
 @@ -0,0 +1,298 @@
+/*
+ * 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) 2017 by Contributors
+ * \file np_gesvd-inl.h
+ * \brief Function definition of the SVD Operator.
+ */
+#ifndef MXNET_OPERATOR_NUMPY_LINALG_NP_GESVD_INL_H_
+#define MXNET_OPERATOR_NUMPY_LINALG_NP_GESVD_INL_H_
+
+#include 
+#include 
+#include "../../tensor/la_op.h"
+#include "../../tensor/la_op-inl.h"
+
+namespace mxnet {
+namespace op {
+
+struct GesvdVecSign {
+  template
+  MSHADOW_XINLINE static void Map(int i, int m, int n, DType* UT,
+  DType* V, int ldut, int ldv) {
+DType* vrow(V + i * ldv);
+DType maxval(fabs(vrow[0])), vval(0.0);
+int maxind(0);
+for (int i = 1; i < n; ++i) {
+  vval = fabs(vrow[i]);
+  if (vval > maxval) {
+maxval = vval;
+maxind = i;
+  }
+}
+if (vrow[maxind] < 0) {
+  DType* utcol(UT + i % m + (i / m) * ldut * m);
+  for (int i = 0; i < n; ++i) {
+vrow[i] = -vrow[i];
+if (i < m) {
+  utcol[i * ldut] = -utcol[i * ldut];
+}
+  }
+}
+  }
+};
+
+// (UT, L, V) = gesvd(A) [singular value decomposition]
+// - V can overwrite A
+// - Needs workspace (both DType and int), size of which is determined by a
+//   workspace query
+struct gesvd {
+  template
+  static void op(const Tensor& A,
+ const Tensor& UT,
+ const Tensor& L,
+ const Tensor& V,
+ const OpContext& ctx,
+ const nnvm::NodeAttrs& attrs) {
+Stream *s = ctx.get_stream();
+if (A.dptr_ != V.dptr_) Copy(V, A, s);
+// From here on, we work on V only
+// Reserve workspace (size determined by query)
+int lwork(linalg_gesvd_workspace_query(UT[0], L[0], V[0], s));
+Tensor work = ctx.requested[0]
+  .get_space_typed(Shape1(lwork), s);
+// Loop over items in batch
+for (index_t i = 0; i < UT.size(0); ++i) {
+  linalg_gesvd(UT[i], L[i], V[i], work, s);
+}
+// Set signs in a deterministic way
+using namespace mxnet_op;
+Kernel::Launch
+  (s, V.size(0) * V.size(1), V.size(1), V.size(2),
+   UT.dptr_, V.dptr_, UT.stride_, V.stride_);
+  }
+};
+
+// (A) => (UT, L, V)
+template
+void NumpyLaGesvdForward(const nnvm::NodeAttrs& attrs,
+ const OpContext& ctx,
+ const std::vector& inputs,
+ const std::vector& req,
+ const std::vector& outputs) {
+  using namespace mshadow;
+  CHECK_EQ(inputs.size(), 1);
+  CHECK_EQ(outputs.size(), 3);
+  if (inputs[0].shape_.Size() == 0) {
+return;
+  }
+  MSHADOW_SGL_DBL_TYPE_SWITCH(outputs[0].type_flag_, OType, {
+mshadow::Stream *s = ctx.get_stream();
+laop::op(inputs[0].FlatToKD(s),
+ outputs[0].FlatToKD(s),
+ outputs[1].FlatToKD(s),
+ outputs[2].FlatToKD(s), ctx, attrs);
+  });
+}
+
+// Helper for gesvd_backward. See technical report for details
 
 Review comment:
   Is the report cited somewhere?


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:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-mxnet] mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd

2019-08-13 Thread GitBox
mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd
URL: https://github.com/apache/incubator-mxnet/pull/15795#discussion_r313286899
 
 

 ##
 File path: src/operator/numpy/linalg/np_gesvd-inl.h
 ##
 @@ -0,0 +1,298 @@
+/*
+ * 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) 2017 by Contributors
+ * \file np_gesvd-inl.h
+ * \brief Function definition of the SVD Operator.
+ */
+#ifndef MXNET_OPERATOR_NUMPY_LINALG_NP_GESVD_INL_H_
+#define MXNET_OPERATOR_NUMPY_LINALG_NP_GESVD_INL_H_
+
+#include 
+#include 
+#include "../../tensor/la_op.h"
+#include "../../tensor/la_op-inl.h"
+
+namespace mxnet {
+namespace op {
+
+struct GesvdVecSign {
+  template
+  MSHADOW_XINLINE static void Map(int i, int m, int n, DType* UT,
+  DType* V, int ldut, int ldv) {
+DType* vrow(V + i * ldv);
+DType maxval(fabs(vrow[0])), vval(0.0);
+int maxind(0);
+for (int i = 1; i < n; ++i) {
+  vval = fabs(vrow[i]);
+  if (vval > maxval) {
+maxval = vval;
+maxind = i;
+  }
+}
+if (vrow[maxind] < 0) {
+  DType* utcol(UT + i % m + (i / m) * ldut * m);
+  for (int i = 0; i < n; ++i) {
+vrow[i] = -vrow[i];
+if (i < m) {
+  utcol[i * ldut] = -utcol[i * ldut];
+}
+  }
+}
+  }
+};
+
+// (UT, L, V) = gesvd(A) [singular value decomposition]
+// - V can overwrite A
+// - Needs workspace (both DType and int), size of which is determined by a
 
 Review comment:
   Only one workspace (DType) needed here


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:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-mxnet] mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd

2019-08-13 Thread GitBox
mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd
URL: https://github.com/apache/incubator-mxnet/pull/15795#discussion_r313284716
 
 

 ##
 File path: src/operator/linalg_impl.h
 ##
 @@ -1234,6 +1234,137 @@ LINALG_GPU_SYEVD_WORKSPACE_QUERY(DnDsyevd, double)
 
 #endif  // __CUDACC__
 
+ GESVD 

+
+// CPU/GPU-versions of LAPACK function "gesvd"
+
+template inline
+void check_gesvd(const Tensor& UT,
+ const Tensor& L,
+ const Tensor& V) {
+  // Any checking that helps user debug potential problems.
+  CHECK_LE(V.size(0), V.size(1))
+<< "The second to last dimension of A must be less or equal to the "
+<< "last dimension";
+  CHECK_EQ(UT.size(0), UT.size(1))
+<< "UT must be square matrix";
+  CHECK_EQ(V.size(0), L.size(0))
+<< "V, L have incompatible sizes";
+  CHECK_EQ(V.size(0), UT.size(0))
+<< "V, UT must have compatible sizes";
+}
+
+#define LINALG_CPU_GESVD(fname, DType) \
+template<> inline \
+void linalg_gesvd(const Tensor& UT, \
+  const Tensor& L, \
+  const Tensor& V, \
+  const Tensor& work, \
+  Stream *s) { \
+  check_gesvd(UT, L, V); \
+  DType lwork(0); \
+  MXNET_LAPACK_##fname(MXNET_LAPACK_ROW_MAJOR, V.size(0), V.size(1), \
+   UT.dptr_, UT.stride_, L.dptr_, V.dptr_, V.stride_, \
+   , -1); \
+  int ret(MXNET_LAPACK_##fname(MXNET_LAPACK_ROW_MAJOR, V.size(0), V.size(1), \
+   UT.dptr_, UT.stride_, L.dptr_, V.dptr_, 
V.stride_, \
+   work.dptr_, static_cast(lwork))); \
+  CHECK_EQ(ret, 0) << #fname << " failed in lapack on cpu."; \
+}
+
+LINALG_CPU_GESVD(sgesvd, float)
+LINALG_CPU_GESVD(dgesvd, double)
+
+// Mangle temp storage requirements for DType and int into a single
 
 Review comment:
   Remove this comment, it only applies to syevd. See my comment above, you 
have a single workspace here.


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:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-mxnet] mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd

2019-08-13 Thread GitBox
mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd
URL: https://github.com/apache/incubator-mxnet/pull/15795#discussion_r313283809
 
 

 ##
 File path: src/operator/linalg_impl.h
 ##
 @@ -1234,6 +1234,137 @@ LINALG_GPU_SYEVD_WORKSPACE_QUERY(DnDsyevd, double)
 
 #endif  // __CUDACC__
 
+ GESVD 

+
+// CPU/GPU-versions of LAPACK function "gesvd"
+
+template inline
+void check_gesvd(const Tensor& UT,
+ const Tensor& L,
+ const Tensor& V) {
+  // Any checking that helps user debug potential problems.
+  CHECK_LE(V.size(0), V.size(1))
+<< "The second to last dimension of A must be less or equal to the "
+<< "last dimension";
+  CHECK_EQ(UT.size(0), UT.size(1))
+<< "UT must be square matrix";
+  CHECK_EQ(V.size(0), L.size(0))
+<< "V, L have incompatible sizes";
+  CHECK_EQ(V.size(0), UT.size(0))
+<< "V, UT must have compatible sizes";
+}
+
+#define LINALG_CPU_GESVD(fname, DType) \
+template<> inline \
+void linalg_gesvd(const Tensor& UT, \
+  const Tensor& L, \
+  const Tensor& V, \
+  const Tensor& work, \
+  Stream *s) { \
+  check_gesvd(UT, L, V); \
+  DType lwork(0); \
+  MXNET_LAPACK_##fname(MXNET_LAPACK_ROW_MAJOR, V.size(0), V.size(1), \
 
 Review comment:
   The reason why I called the workspace query again in the syevd 
implementation, is that there, work consists of two different workspaces. But 
here, I think you can just use work.size(0) to pass for lwork, and don't have 
to do the query again.


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:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-mxnet] mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd

2019-08-13 Thread GitBox
mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd
URL: https://github.com/apache/incubator-mxnet/pull/15795#discussion_r313282149
 
 

 ##
 File path: src/operator/linalg_impl.h
 ##
 @@ -1234,6 +1234,137 @@ LINALG_GPU_SYEVD_WORKSPACE_QUERY(DnDsyevd, double)
 
 #endif  // __CUDACC__
 
+ GESVD 

+
+// CPU/GPU-versions of LAPACK function "gesvd"
+
+template inline
+void check_gesvd(const Tensor& UT,
+ const Tensor& L,
+ const Tensor& V) {
+  // Any checking that helps user debug potential problems.
+  CHECK_LE(V.size(0), V.size(1))
+<< "The second to last dimension of A must be less or equal to the "
+<< "last dimension";
+  CHECK_EQ(UT.size(0), UT.size(1))
+<< "UT must be square matrix";
+  CHECK_EQ(V.size(0), L.size(0))
+<< "V, L have incompatible sizes";
+  CHECK_EQ(V.size(0), UT.size(0))
+<< "V, UT must have compatible sizes";
+}
+
+#define LINALG_CPU_GESVD(fname, DType) \
+template<> inline \
+void linalg_gesvd(const Tensor& UT, \
+  const Tensor& L, \
+  const Tensor& V, \
+  const Tensor& work, \
+  Stream *s) { \
+  check_gesvd(UT, L, V); \
+  DType lwork(0); \
+  MXNET_LAPACK_##fname(MXNET_LAPACK_ROW_MAJOR, V.size(0), V.size(1), \
 
 Review comment:
   I think this is wrong. You must have done the workspace query before 
(calling the other function), so the size of work will be fine to pass for 
lwork, right? So no need to do the workspace query again here.


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:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-mxnet] mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd

2019-08-13 Thread GitBox
mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd
URL: https://github.com/apache/incubator-mxnet/pull/15795#discussion_r313278037
 
 

 ##
 File path: src/operator/linalg.h
 ##
 @@ -191,6 +191,28 @@ int linalg_syevd_workspace_query(const Tensor& A,
  const Tensor& L,
  Stream *s = 0);
 
+ GESVD 

+
+// CPU/GPU-versions of LAPACK function "gesvd". Please refer to the
+// LAPACK documentation for further details.
+// Note:
+// - V is input and output parameter (overwritten by A)
 
 Review comment:
   V is input and output parameter (it overwrites A)


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:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-mxnet] mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd

2019-08-13 Thread GitBox
mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd
URL: https://github.com/apache/incubator-mxnet/pull/15795#discussion_r313276586
 
 

 ##
 File path: src/operator/c_lapack_api.h
 ##
 @@ -361,6 +382,26 @@ inline void flip(int m, int n, DType *b, int ldb, DType 
*a, int lda) {
   MXNET_LAPACK_CWRAP_SYEVD(ssyevd, float)
   MXNET_LAPACK_CWRAP_SYEVD(dsyevd, double)
 
+  #define MXNET_LAPACK_CWRAP_GESVD(func, dtype) \
 
 Review comment:
   Add comment that due to row-major and internal column-major, the arguments 
are flipped and transposed, and m and n are flipped as well


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:
us...@infra.apache.org


With regards,
Apache Git Services


[GitHub] [incubator-mxnet] mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd

2019-08-13 Thread GitBox
mseeger commented on a change in pull request #15795: [Numpy] Differentiable svd
URL: https://github.com/apache/incubator-mxnet/pull/15795#discussion_r313275134
 
 

 ##
 File path: src/operator/c_lapack_api.h
 ##
 @@ -242,6 +249,20 @@ inline void flip(int m, int n, DType *b, int ldb, DType 
*a, int lda) {
   #define MXNET_LAPACK_sgetrf LAPACKE_sgetrf
   #define MXNET_LAPACK_dgetrf LAPACKE_dgetrf
 
+  #define MXNET_LAPACK_CWRAP_GESVD(prefix, dtype) \
+  inline int MXNET_LAPACK_##prefix##gesvd(int matrix_layout, int m, int n, 
dtype* ut, \
 
 Review comment:
   Not sure I understand this case. Maybe add a comment?


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:
us...@infra.apache.org


With regards,
Apache Git Services