You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2020/01/14 14:47:40 UTC

[GitHub] [incubator-mxnet] Yiyan66 opened a new pull request #17302: [numpy]add op random.logistic

Yiyan66 opened a new pull request #17302: [numpy]add op random.logistic
URL: https://github.com/apache/incubator-mxnet/pull/17302
 
 
   ## Description ##
   add op random.logistic
   
   ## Checklist ##
   ### Essentials ###
   Please feel free to remove inapplicable items for your PR.
   - [ ] The PR title starts with [MXNET-$JIRA_ID], where $JIRA_ID refers to the relevant [JIRA issue](https://issues.apache.org/jira/projects/MXNET/issues) created (except PRs with tiny changes)
   - [ ] Changes are complete (i.e. I finished coding on this PR)
   - [ ] All changes have test coverage:
   - Unit tests are added for small changes to verify correctness (e.g. adding a new operator)
   - Nightly tests are added for complicated/long-running ones (e.g. changing distributed kvstore)
   - Build tests will be added for build configuration changes (e.g. adding a new build option with NCCL)
   - [ ] Code is well-documented: 
   - For user-facing API changes, API doc string has been updated. 
   - For new C++ functions in header files, their functionalities and arguments are documented. 
   - For new examples, README.md is added to explain the what the example does, the source of the dataset, expected performance on test set and reference to the original paper if applicable
   - Check the API doc at https://mxnet-ci-doc.s3-accelerate.dualstack.amazonaws.com/PR-$PR_ID/$BUILD_ID/index.html
   - [ ] To the best of my knowledge, examples are either not affected by this change, or have been fixed to be compatible with this change
   
   ### Changes ###
   - [ ] Feature1, tests, (and when applicable, API doc)
   - [ ] Feature2, tests, (and when applicable, API doc)
   
   ## Comments ##
   - If this change is a backward incompatible change, why must this change be made.
   - Interesting edge cases to note 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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] haojin2 merged pull request #17302: [numpy]add op random.logistic, random.gumbel

Posted by GitBox <gi...@apache.org>.
haojin2 merged pull request #17302: [numpy]add op random.logistic, random.gumbel
URL: https://github.com/apache/incubator-mxnet/pull/17302
 
 
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] haojin2 commented on a change in pull request #17302: [numpy]add op random.logistic

Posted by GitBox <gi...@apache.org>.
haojin2 commented on a change in pull request #17302: [numpy]add op random.logistic
URL: https://github.com/apache/incubator-mxnet/pull/17302#discussion_r366765196
 
 

 ##########
 File path: src/operator/numpy/random/np_logistic_op.h
 ##########
 @@ -0,0 +1,331 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * Copyright (c) 2019 by Contributors
+ * \file np_logistic_op.h
+ * \brief Operator for numpy sampling from logistic distributions
+ */
+#ifndef MXNET_OPERATOR_NUMPY_RANDOM_NP_LOGISTIC_OP_H_
+#define MXNET_OPERATOR_NUMPY_RANDOM_NP_LOGISTIC_OP_H_
+
+#include <mxnet/operator_util.h>
+#include <cstdio>
+#include <algorithm>
+#include <string>
+#include <vector>
+#include <cmath>
+#include "../../elemwise_op_common.h"
+#include "../../mshadow_op.h"
+#include "../../mxnet_op.h"
+#include "../../operator_common.h"
+#include "../../tensor/elemwise_binary_broadcast_op.h"
+#include "./dist_common.h"
+
+namespace mxnet {
+namespace op {
+
+struct NumpyLogisticParam : public dmlc::Parameter<NumpyLogisticParam> {
+  dmlc::optional<float> loc;
+  dmlc::optional<float> scale;
+  dmlc::optional<mxnet::Tuple<int>> size;
+  DMLC_DECLARE_PARAMETER(NumpyLogisticParam) {
+    DMLC_DECLARE_FIELD(loc);
+    DMLC_DECLARE_FIELD(scale);
+    DMLC_DECLARE_FIELD(size)
+        .set_default(dmlc::optional<mxnet::Tuple<int>>())
+        .describe(
+            "Output shape. If the given shape is, "
+            "e.g., (m, n, k), then m * n * k samples are drawn. "
+            "Default is None, in which case a single value is returned.");
+  }
+};
+
+inline bool NumpyLogisticOpType(const nnvm::NodeAttrs &attrs,
+                               std::vector<int> *in_attrs,
+                               std::vector<int> *out_attrs) {
+
+  (*out_attrs)[0] = mshadow::kFloat32;
+  (*out_attrs)[1] = mshadow::kFloat32;
+  return true;
+}
+
+namespace mxnet_op {
+
+template <typename DType>
+struct logistic_two_scalar_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, float loc, float scale,
+                                  float *noise, DType *out) {
+    noise[i] = log(noise[i]) - log(1 - noise[i]);
+    out[i] = loc + noise[i] * scale;
+  }
+};
+
+template <typename IType>
+struct check_legal_scale_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, IType *scalar, float* flag) {
+    if (scalar[i] < 0) {
+      *flag = -1.0;
+    }
+  }
+};
+
+template <int ndim, typename IType, typename OType>
+struct logistic_one_scalar_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, int scalar_pos,
+                                  const Shape<ndim> &stride,
+                                  const Shape<ndim> &oshape, IType *array,
+                                  float scalar, float *noise, OType *out) {
+    Shape<ndim> coord = unravel(i, oshape);
+    auto idx = static_cast<index_t>(dot(coord, stride));
+    IType loc_value;
+    IType scale_value;
+    if (scalar_pos == 0) {
+      loc_value = scalar;
+      scale_value = array[idx];
+    } else {
+      loc_value = array[idx];
+      scale_value = scalar;
+    }
+    noise[i] = log(noise[i]) - log(1 - noise[i]);
+    out[i] = loc_value + noise[i] * scale_value;
+  }
+};
+
+template <int ndim, typename IType, typename OType>
+struct logistic_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, const Shape<ndim> &lstride,
+                                  const Shape<ndim> &hstride,
+                                  const Shape<ndim> &oshape, IType *loc,
+                                  IType *scale, float *noise, OType *out) {
+    Shape<ndim> coord = unravel(i, oshape);
+    auto lidx = static_cast<index_t>(dot(coord, lstride));
+    auto hidx = static_cast<index_t>(dot(coord, hstride));
+    IType loc_value = loc[lidx];
+    IType scale_value = scale[hidx];
+    noise[i] = log(noise[i]) - log(1 - noise[i]);
+    out[i] = loc_value + noise[i] * scale_value;
+  }
+};
+
+}  // namespace mxnet_op
+
+template <typename xpu>
+void NumpyLogisticForward(const nnvm::NodeAttrs &attrs,
+                          const OpContext &ctx,
+                          const std::vector<TBlob> &inputs,
+                          const std::vector<OpReqType> &req,
+                          const std::vector<TBlob> &outputs) {
+  using namespace mshadow;
+  using namespace mxnet_op;
+  const NumpyLogisticParam &param = nnvm::get<NumpyLogisticParam>(attrs.parsed);
+  Stream<xpu> *s = ctx.get_stream<xpu>();
+  // Generate base random number.
+  Random<xpu, float> *prnd = ctx.requested[0].get_random<xpu, float>(s);
+  Tensor<xpu, 1, float> workspace =
+      ctx.requested[1].get_space_typed<xpu, 1, float>(Shape1(1), s);
+  Tensor<xpu, 1, float> logistic_tensor = outputs[1].FlatTo1D<xpu, float>(s);
+  Tensor<xpu, 1, float> indicator_device = workspace;
+  float indicator_host = 1.0;
+  float *indicator_device_ptr = indicator_device.dptr_;
+  Kernel<set_zero, xpu>::Launch(s, 1, indicator_device_ptr);
+  prnd->SampleUniform(&logistic_tensor, 0.0, 1.0);
+  mxnet::TShape new_lshape, new_hshape, new_oshape;
+  // [scalar scalar] case
+  if (inputs.size() == 0U) {
+    CHECK_GE(param.scale.value(), 0.0) << "ValueError: scale < 0";
+    MSHADOW_TYPE_SWITCH(outputs[0].type_flag_, DType, {
+      Kernel<logistic_two_scalar_kernel<DType>, xpu>::Launch(
+          s, outputs[0].Size(), param.loc.value(), param.scale.value(),
+          logistic_tensor.dptr_, outputs[0].dptr<DType>());
+    });
+  } else if (inputs.size() == 1U) {
+    // [scalar tensor], [tensor scalar] case
+    int ndim = FillShape(inputs[0].shape_, inputs[0].shape_, outputs[0].shape_,
+                         &new_lshape, &new_lshape, &new_oshape);
+    int scalar_pos;
+    float scalar_value;
+    if (param.loc.has_value()) {
+      scalar_pos = 0;
+      scalar_value = param.loc.value();
+      MSHADOW_TYPE_SWITCH(inputs[0].type_flag_, IType, {
+        Kernel<check_legal_scale_kernel<IType>, xpu>::Launch(
+            s, inputs[0].Size(), inputs[0].dptr<IType>(), indicator_device_ptr);
+      });
+      _copy<xpu>(s, &indicator_host, indicator_device_ptr);
+      CHECK_GE(indicator_host, 0.0) << "ValueError: scale < 0";
+    } else {
+      scalar_pos = 1;
+      scalar_value = param.scale.value();
+      CHECK_GE(scalar_value, 0.0) << "ValueError: scale < 0";
+    }
+    MSHADOW_TYPE_SWITCH(inputs[0].type_flag_, IType, {
+      MSHADOW_TYPE_SWITCH(outputs[0].type_flag_, OType, {
+        BROADCAST_NDIM_SWITCH(ndim, NDim, {
+          Shape<NDim> oshape = new_oshape.get<NDim>();
+          Shape<NDim> stride = calc_stride(new_lshape.get<NDim>());
+          Kernel<logistic_one_scalar_kernel<NDim, IType, OType>, xpu>::Launch(
+              s, outputs[0].Size(), scalar_pos, stride, oshape,
+              inputs[0].dptr<IType>(), scalar_value, logistic_tensor.dptr_,
+              outputs[0].dptr<OType>());
+        });
+      });
+    });
+  } else if (inputs.size() == 2U) {
+    // [tensor tensor] case
+    MSHADOW_TYPE_SWITCH(inputs[0].type_flag_, IType, {
+      Kernel<check_legal_scale_kernel<IType>, xpu>::Launch(
+          s, inputs[1].Size(), inputs[1].dptr<IType>(), indicator_device_ptr);
+    });
+    _copy<xpu>(s, &indicator_host, indicator_device_ptr);
+    CHECK_GE(indicator_host, 0.0) << "ValueError: scale < 0";
+    int ndim = FillShape(inputs[0].shape_, inputs[1].shape_, outputs[0].shape_,
+                         &new_lshape, &new_hshape, &new_oshape);
+    MSHADOW_TYPE_SWITCH(inputs[0].type_flag_, IType, {
+      MSHADOW_TYPE_SWITCH(outputs[0].type_flag_, OType, {
+        BROADCAST_NDIM_SWITCH(ndim, NDim, {
+          Shape<NDim> oshape = new_oshape.get<NDim>();
+          Shape<NDim> lstride = calc_stride(new_lshape.get<NDim>());
+          Shape<NDim> hstride = calc_stride(new_hshape.get<NDim>());
+          Kernel<logistic_kernel<NDim, IType, OType>, xpu>::Launch(
+              s, outputs[0].Size(), lstride, hstride, oshape,
+              inputs[0].dptr<IType>(), inputs[1].dptr<IType>(),
+              logistic_tensor.dptr_, outputs[0].dptr<OType>());
+        });
+      });
+    });
+  }
+}
+
+template<typename xpu, int ndim, typename DType>
+inline void LogisticReparamBackwardImpl(const OpContext& ctx,
+                                        const std::vector<TBlob>& inputs,
+                                        const std::vector<OpReqType>& req,
+                                        const std::vector<TBlob>& outputs,
+                                        const mxnet::TShape& new_lshape,
+                                        const mxnet::TShape& new_rshape,
+                                        const mxnet::TShape& new_oshape) {
+  using namespace mshadow;
+  using namespace mshadow::expr;
+  using namespace broadcast;
+  Stream<xpu> *s = ctx.get_stream<xpu>();
+  const TBlob lgrad = outputs[0].reshape(new_lshape);
+  const TBlob rgrad = outputs[1].reshape(new_rshape);
+  const TBlob ograd = inputs[0].reshape(new_oshape);
+  // Mean
+  const TBlob lhs = inputs[2].reshape(new_lshape);
+  // Variance
+  const TBlob rhs = inputs[3].reshape(new_rshape);
+  const TBlob samples = inputs[4].reshape(new_oshape);
+  const TBlob noise = inputs[5].reshape(new_oshape);
+  size_t workspace_size_l = ReduceWorkspaceSize<ndim, DType>(
+      s, lgrad.shape_, req[0], ograd.shape_, lhs.shape_, rhs.shape_);
+  size_t workspace_size_r = ReduceWorkspaceSize<ndim, DType>(
+      s, rgrad.shape_, req[1], ograd.shape_, lhs.shape_, rhs.shape_);
+  size_t workspace_size = std::max(workspace_size_l, workspace_size_r);
+  Tensor<xpu, 1, char> workspace =
+      ctx.requested[0].get_space_typed<xpu, 1, char>(Shape1(workspace_size), s);
+  Reduce<red::sum, ndim, DType, op::mshadow_op::identity>(s,
+          lgrad, req[0], workspace, ograd);
+  Reduce<red::sum, ndim, DType, op::mshadow_op::mul, op::mshadow_op::left>(
+      s, rgrad, req[1], workspace, ograd, noise, rhs);
+}
+
+template<typename xpu, int ndim, typename DType>
+inline void ScalarLogisticReparamBackwardImpl(const OpContext& ctx,
+                                            const std::vector<TBlob>& inputs,
 
 Review comment:
   alignment

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] haojin2 commented on a change in pull request #17302: [numpy]add op random.logistic, random.gumbel

Posted by GitBox <gi...@apache.org>.
haojin2 commented on a change in pull request #17302: [numpy]add op random.logistic, random.gumbel
URL: https://github.com/apache/incubator-mxnet/pull/17302#discussion_r367799853
 
 

 ##########
 File path: src/operator/numpy/random/np_location_scale_op.h
 ##########
 @@ -0,0 +1,510 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * Copyright (c) 2019 by Contributors
+ * \file np_location_scale_op.h
+ * \brief Operator for numpy sampling from localtion scale distributions
+ */
+#ifndef MXNET_OPERATOR_NUMPY_RANDOM_NP_LOCATION_SCALE_OP_H_
+#define MXNET_OPERATOR_NUMPY_RANDOM_NP_LOCATION_SCALE_OP_H_
+
+#include <mxnet/operator_util.h>
+#include <cstdio>
+#include <algorithm>
+#include <string>
+#include <vector>
+#include <cmath>
+#include "../../elemwise_op_common.h"
+#include "../../mshadow_op.h"
+#include "../../mxnet_op.h"
+#include "../../operator_common.h"
+#include "../../tensor/elemwise_binary_broadcast_op.h"
+#include "./dist_common.h"
+
+namespace mxnet {
+namespace op {
+
+struct NumpyLocationScaleParam : public dmlc::Parameter<NumpyLocationScaleParam> {
+  dmlc::optional<float> loc;
+  dmlc::optional<float> scale;
+  dmlc::optional<mxnet::Tuple<int>> size;
+  DMLC_DECLARE_PARAMETER(NumpyLocationScaleParam) {
+    DMLC_DECLARE_FIELD(loc);
+    DMLC_DECLARE_FIELD(scale);
+    DMLC_DECLARE_FIELD(size)
+      .set_default(dmlc::optional<mxnet::Tuple<int>>())
+      .describe(
+          "Output shape. If the given shape is, "
+          "e.g., (m, n, k), then m * n * k samples are drawn. "
+          "Default is None, in which case a single value is returned.");
+  }
+};
+
+inline bool NumpyLocationScaleOpType(const nnvm::NodeAttrs &attrs,
+                                     std::vector<int> *in_attrs,
+                                     std::vector<int> *out_attrs) {
+  (*out_attrs)[0] = mshadow::kFloat32;
+  (*out_attrs)[1] = mshadow::kFloat32;
+  return true;
+}
+
+namespace mxnet_op {
+
+template <typename DType>
+struct logistic_two_scalar_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, float loc, float scale,
+                                  float *noise, DType *out) {
+    noise[i] = log(noise[i]) - log(1 - noise[i]);
+    out[i] = loc + noise[i] * scale;
+  }
+};
+
+struct logistic_two_scalar {
+  template <typename xpu, typename DType>
+  static void op(Stream<xpu> *s, const int out_size,
+                 const float loc, const float scale,
+                 const Tensor<xpu, 1, float>& noise,
+                 const Tensor<xpu, 1, DType>& out) {
+    Kernel<logistic_two_scalar_kernel<DType>, xpu>::Launch(
+      s, out_size, loc, scale, noise.dptr_, out.dptr_);
+    }
+};
+
+template <typename DType>
+struct gumbel_two_scalar_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, float loc, float scale,
+                                  float *noise, DType *out) {
+    noise[i] = -log(-log(noise[i]));
+    out[i] = loc + noise[i] * scale;
+  }
+};
+
+struct gumbel_two_scalar {
+  template <typename xpu, typename DType>
+  static void op(Stream<xpu> *s, const int out_size,
+                 const float loc, const float scale,
+                 const Tensor<xpu, 1, float>& noise,
+                 const Tensor<xpu, 1, DType>& out) {
+    Kernel<gumbel_two_scalar_kernel<DType>, xpu>::Launch(
+      s, out_size, loc, scale, noise.dptr_, out.dptr_);
+  }
+};
+
+template <typename IType>
+struct check_legal_scale_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, IType *scalar, float* flag) {
+    if (scalar[i] < 0) {
+      *flag = -1.0;
+    }
+  }
+};
+
+template <int ndim, typename IType, typename OType>
+struct logistic_one_scalar_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, int scalar_pos,
+                                  const Shape<ndim> &stride,
+                                  const Shape<ndim> &oshape, IType *array,
+                                  float scalar, float *noise, OType *out) {
+    Shape<ndim> coord = unravel(i, oshape);
+    auto idx = static_cast<index_t>(dot(coord, stride));
+    IType loc_value;
+    IType scale_value;
+    if (scalar_pos == 0) {
+      loc_value = scalar;
+      scale_value = array[idx];
+    } else {
+      loc_value = array[idx];
+      scale_value = scalar;
+    }
+    noise[i] = log(noise[i]) - log(1 - noise[i]);
+    out[i] = loc_value + noise[i] * scale_value;
+  }
+};
+
+struct logistic_tensor_scalar {
+  template <typename xpu, int ndim, typename IType, typename OType>
+  static void op(Stream<xpu> *s, const int out_size, const int scalar_pos,
+                 const Shape<ndim> &stride, const Shape<ndim> &oshape,
+                 const Tensor<xpu, 1, IType>& array, float scalar,
+                 const Tensor<xpu, 1, float>& noise, const Tensor<xpu, 1, OType>& out) {
+    Kernel<logistic_one_scalar_kernel<ndim, IType, OType>, xpu>::Launch(
+      s, out_size, scalar_pos, stride, oshape, array.dptr_, scalar, noise.dptr_, out.dptr_);
+  }
+};
+
+template <int ndim, typename IType, typename OType>
+struct gumbel_one_scalar_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, int scalar_pos,
+                                  const Shape<ndim> &stride,
+                                  const Shape<ndim> &oshape, IType *array,
+                                  float scalar, float *noise, OType *out) {
+    Shape<ndim> coord = unravel(i, oshape);
+    auto idx = static_cast<index_t>(dot(coord, stride));
+    IType loc_value;
+    IType scale_value;
+    if (scalar_pos == 0) {
+      loc_value = scalar;
+      scale_value = array[idx];
+    } else {
+      loc_value = array[idx];
+      scale_value = scalar;
+    }
+    noise[i] = -log(-log(noise[i]));
+    out[i] = loc_value + noise[i] * scale_value;
+  }
+};
+
+struct gumbel_tensor_scalar {
+  template <typename xpu, int ndim, typename IType, typename OType>
+  static void op(Stream<xpu> *s, const int out_size, const int scalar_pos,
+                 const Shape<ndim> &stride, const Shape<ndim> &oshape,
+                 const Tensor<xpu, 1, IType>& array, float scalar,
+                 const Tensor<xpu, 1, float>& noise, const Tensor<xpu, 1, OType>& out) {
+      Kernel<gumbel_one_scalar_kernel<ndim, IType, OType>, xpu>::Launch(
+        s, out_size, scalar_pos, stride, oshape, array.dptr_, scalar, noise.dptr_, out.dptr_);
+  }
+};
+
+template <int ndim, typename IType, typename OType>
+struct logistic_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, const Shape<ndim> &lstride,
+                                  const Shape<ndim> &hstride,
+                                  const Shape<ndim> &oshape, IType *loc,
+                                  IType *scale, float *noise, OType *out) {
+    Shape<ndim> coord = unravel(i, oshape);
+    auto lidx = static_cast<index_t>(dot(coord, lstride));
+    auto hidx = static_cast<index_t>(dot(coord, hstride));
+    IType loc_value = loc[lidx];
+    IType scale_value = scale[hidx];
+    noise[i] = log(noise[i]) - log(1 - noise[i]);
+    out[i] = loc_value + noise[i] * scale_value;
+  }
+};
+
+struct logistic_two_tensor {
+  template <typename xpu, int ndim, typename IType, typename OType>
+  static void op(Stream<xpu> *s, const int out_size, const Shape<ndim> &lstride,
+                 const Shape<ndim> &hstride, const Shape<ndim> &oshape,
+                 const Tensor<xpu, 1, IType>& loc, const Tensor<xpu, 1, IType>& scale,
+                 const Tensor<xpu, 1, float>& noise, const Tensor<xpu, 1, OType>& out) {
+      Kernel<logistic_kernel<ndim, IType, OType>, xpu>::Launch(
+        s, out_size, lstride, hstride, oshape, loc.dptr_,
+        scale.dptr_, noise.dptr_, out.dptr_);
+  }
+};
+
+template <int ndim, typename IType, typename OType>
+struct gumbel_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, const Shape<ndim> &lstride,
+                                  const Shape<ndim> &hstride,
+                                  const Shape<ndim> &oshape, IType *loc,
+                                  IType *scale, float *noise, OType *out) {
+    Shape<ndim> coord = unravel(i, oshape);
+    auto lidx = static_cast<index_t>(dot(coord, lstride));
+    auto hidx = static_cast<index_t>(dot(coord, hstride));
+    IType loc_value = loc[lidx];
+    IType scale_value = scale[hidx];
+    noise[i] = -log(-log(noise[i]));
+    out[i] = loc_value + noise[i] * scale_value;
+  }
+};
+
+struct gumbel_two_tensor {
+  template <typename xpu, int ndim, typename IType, typename OType>
+  static void op(Stream<xpu> *s, const int out_size, const Shape<ndim> &lstride,
+                 const Shape<ndim> &hstride, const Shape<ndim> &oshape,
+                 const Tensor<xpu, 1, IType>& loc, const Tensor<xpu, 1, IType>& scale,
+                 const Tensor<xpu, 1, float>& noise, const Tensor<xpu, 1, OType>& out) {
+    Kernel<gumbel_kernel<ndim, IType, OType>, xpu>::Launch(
+      s, out_size, lstride, hstride, oshape, loc.dptr_, scale.dptr_, noise.dptr_, out.dptr_);
+  }
+};
+
+}  // namespace mxnet_op
+
+template <typename xpu, typename two_scalar, typename tensor_scalar, typename two_tensor>
+void NumpyLocationScaleForward(const nnvm::NodeAttrs &attrs,
+                          const OpContext &ctx,
 
 Review comment:
   alignment

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] xidulu commented on a change in pull request #17302: [numpy]add op random.logistic, random.gumbel

Posted by GitBox <gi...@apache.org>.
xidulu commented on a change in pull request #17302: [numpy]add op random.logistic, random.gumbel
URL: https://github.com/apache/incubator-mxnet/pull/17302#discussion_r367798667
 
 

 ##########
 File path: src/operator/numpy/random/np_location_scale_op.h
 ##########
 @@ -0,0 +1,510 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * Copyright (c) 2019 by Contributors
+ * \file np_location_scale_op.h
+ * \brief Operator for numpy sampling from localtion scale distributions
+ */
+#ifndef MXNET_OPERATOR_NUMPY_RANDOM_NP_LOCATION_SCALE_OP_H_
+#define MXNET_OPERATOR_NUMPY_RANDOM_NP_LOCATION_SCALE_OP_H_
+
+#include <mxnet/operator_util.h>
+#include <cstdio>
+#include <algorithm>
+#include <string>
+#include <vector>
+#include <cmath>
+#include "../../elemwise_op_common.h"
+#include "../../mshadow_op.h"
+#include "../../mxnet_op.h"
+#include "../../operator_common.h"
+#include "../../tensor/elemwise_binary_broadcast_op.h"
+#include "./dist_common.h"
+
+namespace mxnet {
+namespace op {
+
+struct NumpyLocationScaleParam : public dmlc::Parameter<NumpyLocationScaleParam> {
+  dmlc::optional<float> loc;
+  dmlc::optional<float> scale;
+  dmlc::optional<mxnet::Tuple<int>> size;
+  DMLC_DECLARE_PARAMETER(NumpyLocationScaleParam) {
+    DMLC_DECLARE_FIELD(loc);
+    DMLC_DECLARE_FIELD(scale);
+    DMLC_DECLARE_FIELD(size)
+      .set_default(dmlc::optional<mxnet::Tuple<int>>())
+      .describe(
+          "Output shape. If the given shape is, "
+          "e.g., (m, n, k), then m * n * k samples are drawn. "
+          "Default is None, in which case a single value is returned.");
+  }
+};
+
+inline bool NumpyLocationScaleOpType(const nnvm::NodeAttrs &attrs,
+                                     std::vector<int> *in_attrs,
+                                     std::vector<int> *out_attrs) {
+  (*out_attrs)[0] = mshadow::kFloat32;
+  (*out_attrs)[1] = mshadow::kFloat32;
+  return true;
+}
+
+namespace mxnet_op {
+
+template <typename DType>
+struct logistic_two_scalar_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, float loc, float scale,
+                                  float *noise, DType *out) {
+    noise[i] = log(noise[i]) - log(1 - noise[i]);
+    out[i] = loc + noise[i] * scale;
+  }
+};
+
+struct logistic_two_scalar {
+  template <typename xpu, typename DType>
+  static void op(Stream<xpu> *s, const int out_size,
+                 const float loc, const float scale,
+                 const Tensor<xpu, 1, float>& noise,
+                 const Tensor<xpu, 1, DType>& out) {
+    Kernel<logistic_two_scalar_kernel<DType>, xpu>::Launch(
+      s, out_size, loc, scale, noise.dptr_, out.dptr_);
+    }
+};
+
+template <typename DType>
+struct gumbel_two_scalar_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, float loc, float scale,
+                                  float *noise, DType *out) {
+    noise[i] = -log(-log(noise[i]));
+    out[i] = loc + noise[i] * scale;
+  }
+};
+
+struct gumbel_two_scalar {
+  template <typename xpu, typename DType>
+  static void op(Stream<xpu> *s, const int out_size,
+                 const float loc, const float scale,
+                 const Tensor<xpu, 1, float>& noise,
+                 const Tensor<xpu, 1, DType>& out) {
+    Kernel<gumbel_two_scalar_kernel<DType>, xpu>::Launch(
+      s, out_size, loc, scale, noise.dptr_, out.dptr_);
+  }
+};
+
+template <typename IType>
+struct check_legal_scale_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, IType *scalar, float* flag) {
+    if (scalar[i] < 0) {
+      *flag = -1.0;
+    }
+  }
+};
+
+template <int ndim, typename IType, typename OType>
+struct logistic_one_scalar_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, int scalar_pos,
+                                  const Shape<ndim> &stride,
+                                  const Shape<ndim> &oshape, IType *array,
+                                  float scalar, float *noise, OType *out) {
+    Shape<ndim> coord = unravel(i, oshape);
+    auto idx = static_cast<index_t>(dot(coord, stride));
+    IType loc_value;
+    IType scale_value;
+    if (scalar_pos == 0) {
+      loc_value = scalar;
+      scale_value = array[idx];
+    } else {
+      loc_value = array[idx];
+      scale_value = scalar;
+    }
+    noise[i] = log(noise[i]) - log(1 - noise[i]);
+    out[i] = loc_value + noise[i] * scale_value;
+  }
+};
+
+struct logistic_tensor_scalar {
+  template <typename xpu, int ndim, typename IType, typename OType>
+  static void op(Stream<xpu> *s, const int out_size, const int scalar_pos,
+                 const Shape<ndim> &stride, const Shape<ndim> &oshape,
+                 const Tensor<xpu, 1, IType>& array, float scalar,
+                 const Tensor<xpu, 1, float>& noise, const Tensor<xpu, 1, OType>& out) {
+    Kernel<logistic_one_scalar_kernel<ndim, IType, OType>, xpu>::Launch(
+      s, out_size, scalar_pos, stride, oshape, array.dptr_, scalar, noise.dptr_, out.dptr_);
+  }
+};
+
+template <int ndim, typename IType, typename OType>
+struct gumbel_one_scalar_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, int scalar_pos,
+                                  const Shape<ndim> &stride,
+                                  const Shape<ndim> &oshape, IType *array,
+                                  float scalar, float *noise, OType *out) {
+    Shape<ndim> coord = unravel(i, oshape);
+    auto idx = static_cast<index_t>(dot(coord, stride));
+    IType loc_value;
+    IType scale_value;
+    if (scalar_pos == 0) {
+      loc_value = scalar;
+      scale_value = array[idx];
+    } else {
+      loc_value = array[idx];
+      scale_value = scalar;
+    }
+    noise[i] = -log(-log(noise[i]));
+    out[i] = loc_value + noise[i] * scale_value;
+  }
+};
+
+struct gumbel_tensor_scalar {
+  template <typename xpu, int ndim, typename IType, typename OType>
+  static void op(Stream<xpu> *s, const int out_size, const int scalar_pos,
+                 const Shape<ndim> &stride, const Shape<ndim> &oshape,
+                 const Tensor<xpu, 1, IType>& array, float scalar,
+                 const Tensor<xpu, 1, float>& noise, const Tensor<xpu, 1, OType>& out) {
+      Kernel<gumbel_one_scalar_kernel<ndim, IType, OType>, xpu>::Launch(
+        s, out_size, scalar_pos, stride, oshape, array.dptr_, scalar, noise.dptr_, out.dptr_);
+  }
+};
+
+template <int ndim, typename IType, typename OType>
+struct logistic_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, const Shape<ndim> &lstride,
+                                  const Shape<ndim> &hstride,
+                                  const Shape<ndim> &oshape, IType *loc,
+                                  IType *scale, float *noise, OType *out) {
+    Shape<ndim> coord = unravel(i, oshape);
+    auto lidx = static_cast<index_t>(dot(coord, lstride));
+    auto hidx = static_cast<index_t>(dot(coord, hstride));
+    IType loc_value = loc[lidx];
+    IType scale_value = scale[hidx];
+    noise[i] = log(noise[i]) - log(1 - noise[i]);
+    out[i] = loc_value + noise[i] * scale_value;
+  }
+};
+
+struct logistic_two_tensor {
+  template <typename xpu, int ndim, typename IType, typename OType>
+  static void op(Stream<xpu> *s, const int out_size, const Shape<ndim> &lstride,
+                 const Shape<ndim> &hstride, const Shape<ndim> &oshape,
+                 const Tensor<xpu, 1, IType>& loc, const Tensor<xpu, 1, IType>& scale,
+                 const Tensor<xpu, 1, float>& noise, const Tensor<xpu, 1, OType>& out) {
+      Kernel<logistic_kernel<ndim, IType, OType>, xpu>::Launch(
+        s, out_size, lstride, hstride, oshape, loc.dptr_,
+        scale.dptr_, noise.dptr_, out.dptr_);
+  }
+};
+
+template <int ndim, typename IType, typename OType>
+struct gumbel_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, const Shape<ndim> &lstride,
+                                  const Shape<ndim> &hstride,
+                                  const Shape<ndim> &oshape, IType *loc,
+                                  IType *scale, float *noise, OType *out) {
+    Shape<ndim> coord = unravel(i, oshape);
+    auto lidx = static_cast<index_t>(dot(coord, lstride));
+    auto hidx = static_cast<index_t>(dot(coord, hstride));
+    IType loc_value = loc[lidx];
+    IType scale_value = scale[hidx];
+    noise[i] = -log(-log(noise[i]));
+    out[i] = loc_value + noise[i] * scale_value;
+  }
+};
+
+struct gumbel_two_tensor {
+  template <typename xpu, int ndim, typename IType, typename OType>
+  static void op(Stream<xpu> *s, const int out_size, const Shape<ndim> &lstride,
+                 const Shape<ndim> &hstride, const Shape<ndim> &oshape,
+                 const Tensor<xpu, 1, IType>& loc, const Tensor<xpu, 1, IType>& scale,
+                 const Tensor<xpu, 1, float>& noise, const Tensor<xpu, 1, OType>& out) {
+    Kernel<gumbel_kernel<ndim, IType, OType>, xpu>::Launch(
 
 Review comment:
   Why wrapping a a kernel inside another kernel?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] reminisce commented on a change in pull request #17302: [numpy]add op random.logistic, random.gumbel

Posted by GitBox <gi...@apache.org>.
reminisce commented on a change in pull request #17302: [numpy]add op random.logistic, random.gumbel
URL: https://github.com/apache/incubator-mxnet/pull/17302#discussion_r368362494
 
 

 ##########
 File path: python/mxnet/ndarray/numpy/random.py
 ##########
 @@ -193,6 +194,83 @@ def normal(loc=0.0, scale=1.0, size=None, dtype=None, ctx=None, out=None):
                            ctx=ctx, dtype=dtype, out=out)
 
 
+def logistic(loc=0.0, scale=1.0, size=None):
+    r"""Draw samples from a logistic distribution.
+
+    Samples are drawn from a logistic distribution with specified
+    parameters, loc (location or mean, also median), and scale (>0).
+
+    Parameters
+    ----------
+    loc : float or array_like of floats, optional
+        Parameter of the distribution. Default is 0.
+    scale : float or array_like of floats, optional
+        Parameter of the distribution. Must be non-negative.
+        Default is 1.
+    size : int or tuple of ints, optional
+        Output shape.  If the given shape is, e.g., ``(m, n, k)``, then
+        ``m * n * k`` samples are drawn.  If size is ``None`` (default),
+        a single value is returned if ``loc`` and ``scale`` are both scalars.
+        Otherwise, ``np.broadcast(loc, scale).size`` samples are drawn.
+
+    Returns
+    -------
+    out : ndarray or scalar
+        Drawn samples from the parameterized logistic distribution.
+    """
+    from ...numpy import ndarray as np_ndarray
+    input_type = (isinstance(loc, np_ndarray), isinstance(scale, np_ndarray))
+    if size == ():
+        size = None
+    if input_type == (True, True):
+        return _npi.logistic(loc, scale, loc=None, scale=None, size=size)
+    elif input_type == (False, True):
+        return _npi.logistic(scale, loc=loc, scale=None, size=size)
+    elif input_type == (True, False):
+        return _npi.logistic(loc, loc=None, scale=scale, size=size)
+    else:
+        return _npi.logistic(loc=loc, scale=scale, size=size)
+
+
+def gumbel(loc=0.0, scale=1.0, size=None):
 
 Review comment:
   Same here, `ctx`.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] haojin2 commented on issue #17302: [numpy]add op random.logistic

Posted by GitBox <gi...@apache.org>.
haojin2 commented on issue #17302: [numpy]add op random.logistic
URL: https://github.com/apache/incubator-mxnet/pull/17302#issuecomment-574875122
 
 
   also, fix the sanity issues:
   ```
   src/operator/numpy/random/np_logistic_op.h:331:  #endif line should be "#endif  // MXNET_OPERATOR_NUMPY_RANDOM_NP_LOGISTIC_OP_H_"  [build/header_guard] [5]
   
   src/operator/numpy/random/np_logistic_op.h:63:  Redundant blank line at the start of a code block should be deleted.  [whitespace/blank_line] [2]
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] xidulu commented on a change in pull request #17302: [numpy]add op random.logistic, random.gumbel

Posted by GitBox <gi...@apache.org>.
xidulu commented on a change in pull request #17302: [numpy]add op random.logistic, random.gumbel
URL: https://github.com/apache/incubator-mxnet/pull/17302#discussion_r377428122
 
 

 ##########
 File path: tests/python/unittest/test_numpy_op.py
 ##########
 @@ -7483,4 +7485,4 @@ def hybrid_forward(self, F, x, *args, **kwargs):
 
 if __name__ == '__main__':
     import nose
-    nose.runmodule()
+    nose.runmodule()
 
 Review comment:
   Put back the newline to the end of the file.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] Yiyan66 commented on a change in pull request #17302: [numpy]add op random.logistic, random.gumbel

Posted by GitBox <gi...@apache.org>.
Yiyan66 commented on a change in pull request #17302: [numpy]add op random.logistic, random.gumbel
URL: https://github.com/apache/incubator-mxnet/pull/17302#discussion_r367800505
 
 

 ##########
 File path: src/operator/numpy/random/np_location_scale_op.h
 ##########
 @@ -0,0 +1,510 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * Copyright (c) 2019 by Contributors
+ * \file np_location_scale_op.h
+ * \brief Operator for numpy sampling from localtion scale distributions
+ */
+#ifndef MXNET_OPERATOR_NUMPY_RANDOM_NP_LOCATION_SCALE_OP_H_
+#define MXNET_OPERATOR_NUMPY_RANDOM_NP_LOCATION_SCALE_OP_H_
+
+#include <mxnet/operator_util.h>
+#include <cstdio>
+#include <algorithm>
+#include <string>
+#include <vector>
+#include <cmath>
+#include "../../elemwise_op_common.h"
+#include "../../mshadow_op.h"
+#include "../../mxnet_op.h"
+#include "../../operator_common.h"
+#include "../../tensor/elemwise_binary_broadcast_op.h"
+#include "./dist_common.h"
+
+namespace mxnet {
+namespace op {
+
+struct NumpyLocationScaleParam : public dmlc::Parameter<NumpyLocationScaleParam> {
+  dmlc::optional<float> loc;
+  dmlc::optional<float> scale;
+  dmlc::optional<mxnet::Tuple<int>> size;
+  DMLC_DECLARE_PARAMETER(NumpyLocationScaleParam) {
+    DMLC_DECLARE_FIELD(loc);
+    DMLC_DECLARE_FIELD(scale);
+    DMLC_DECLARE_FIELD(size)
+      .set_default(dmlc::optional<mxnet::Tuple<int>>())
+      .describe(
+          "Output shape. If the given shape is, "
+          "e.g., (m, n, k), then m * n * k samples are drawn. "
+          "Default is None, in which case a single value is returned.");
+  }
+};
+
+inline bool NumpyLocationScaleOpType(const nnvm::NodeAttrs &attrs,
+                                     std::vector<int> *in_attrs,
+                                     std::vector<int> *out_attrs) {
+  (*out_attrs)[0] = mshadow::kFloat32;
+  (*out_attrs)[1] = mshadow::kFloat32;
+  return true;
+}
+
+namespace mxnet_op {
+
+template <typename DType>
+struct logistic_two_scalar_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, float loc, float scale,
+                                  float *noise, DType *out) {
+    noise[i] = log(noise[i]) - log(1 - noise[i]);
+    out[i] = loc + noise[i] * scale;
+  }
+};
+
+struct logistic_two_scalar {
+  template <typename xpu, typename DType>
+  static void op(Stream<xpu> *s, const int out_size,
+                 const float loc, const float scale,
+                 const Tensor<xpu, 1, float>& noise,
+                 const Tensor<xpu, 1, DType>& out) {
+    Kernel<logistic_two_scalar_kernel<DType>, xpu>::Launch(
+      s, out_size, loc, scale, noise.dptr_, out.dptr_);
+    }
+};
+
+template <typename DType>
+struct gumbel_two_scalar_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, float loc, float scale,
+                                  float *noise, DType *out) {
+    noise[i] = -log(-log(noise[i]));
+    out[i] = loc + noise[i] * scale;
+  }
+};
+
+struct gumbel_two_scalar {
+  template <typename xpu, typename DType>
+  static void op(Stream<xpu> *s, const int out_size,
+                 const float loc, const float scale,
+                 const Tensor<xpu, 1, float>& noise,
+                 const Tensor<xpu, 1, DType>& out) {
+    Kernel<gumbel_two_scalar_kernel<DType>, xpu>::Launch(
+      s, out_size, loc, scale, noise.dptr_, out.dptr_);
+  }
+};
+
+template <typename IType>
+struct check_legal_scale_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, IType *scalar, float* flag) {
+    if (scalar[i] < 0) {
+      *flag = -1.0;
+    }
+  }
+};
+
+template <int ndim, typename IType, typename OType>
+struct logistic_one_scalar_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, int scalar_pos,
+                                  const Shape<ndim> &stride,
+                                  const Shape<ndim> &oshape, IType *array,
+                                  float scalar, float *noise, OType *out) {
+    Shape<ndim> coord = unravel(i, oshape);
+    auto idx = static_cast<index_t>(dot(coord, stride));
+    IType loc_value;
+    IType scale_value;
+    if (scalar_pos == 0) {
+      loc_value = scalar;
+      scale_value = array[idx];
+    } else {
+      loc_value = array[idx];
+      scale_value = scalar;
+    }
+    noise[i] = log(noise[i]) - log(1 - noise[i]);
+    out[i] = loc_value + noise[i] * scale_value;
+  }
+};
+
+struct logistic_tensor_scalar {
+  template <typename xpu, int ndim, typename IType, typename OType>
+  static void op(Stream<xpu> *s, const int out_size, const int scalar_pos,
+                 const Shape<ndim> &stride, const Shape<ndim> &oshape,
+                 const Tensor<xpu, 1, IType>& array, float scalar,
+                 const Tensor<xpu, 1, float>& noise, const Tensor<xpu, 1, OType>& out) {
+    Kernel<logistic_one_scalar_kernel<ndim, IType, OType>, xpu>::Launch(
+      s, out_size, scalar_pos, stride, oshape, array.dptr_, scalar, noise.dptr_, out.dptr_);
+  }
+};
+
+template <int ndim, typename IType, typename OType>
+struct gumbel_one_scalar_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, int scalar_pos,
+                                  const Shape<ndim> &stride,
+                                  const Shape<ndim> &oshape, IType *array,
+                                  float scalar, float *noise, OType *out) {
+    Shape<ndim> coord = unravel(i, oshape);
+    auto idx = static_cast<index_t>(dot(coord, stride));
+    IType loc_value;
+    IType scale_value;
+    if (scalar_pos == 0) {
+      loc_value = scalar;
+      scale_value = array[idx];
+    } else {
+      loc_value = array[idx];
+      scale_value = scalar;
+    }
+    noise[i] = -log(-log(noise[i]));
+    out[i] = loc_value + noise[i] * scale_value;
+  }
+};
+
+struct gumbel_tensor_scalar {
+  template <typename xpu, int ndim, typename IType, typename OType>
+  static void op(Stream<xpu> *s, const int out_size, const int scalar_pos,
+                 const Shape<ndim> &stride, const Shape<ndim> &oshape,
+                 const Tensor<xpu, 1, IType>& array, float scalar,
+                 const Tensor<xpu, 1, float>& noise, const Tensor<xpu, 1, OType>& out) {
+      Kernel<gumbel_one_scalar_kernel<ndim, IType, OType>, xpu>::Launch(
+        s, out_size, scalar_pos, stride, oshape, array.dptr_, scalar, noise.dptr_, out.dptr_);
+  }
+};
+
+template <int ndim, typename IType, typename OType>
+struct logistic_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, const Shape<ndim> &lstride,
+                                  const Shape<ndim> &hstride,
+                                  const Shape<ndim> &oshape, IType *loc,
+                                  IType *scale, float *noise, OType *out) {
+    Shape<ndim> coord = unravel(i, oshape);
+    auto lidx = static_cast<index_t>(dot(coord, lstride));
+    auto hidx = static_cast<index_t>(dot(coord, hstride));
+    IType loc_value = loc[lidx];
+    IType scale_value = scale[hidx];
+    noise[i] = log(noise[i]) - log(1 - noise[i]);
+    out[i] = loc_value + noise[i] * scale_value;
+  }
+};
+
+struct logistic_two_tensor {
+  template <typename xpu, int ndim, typename IType, typename OType>
+  static void op(Stream<xpu> *s, const int out_size, const Shape<ndim> &lstride,
+                 const Shape<ndim> &hstride, const Shape<ndim> &oshape,
+                 const Tensor<xpu, 1, IType>& loc, const Tensor<xpu, 1, IType>& scale,
+                 const Tensor<xpu, 1, float>& noise, const Tensor<xpu, 1, OType>& out) {
+      Kernel<logistic_kernel<ndim, IType, OType>, xpu>::Launch(
+        s, out_size, lstride, hstride, oshape, loc.dptr_,
+        scale.dptr_, noise.dptr_, out.dptr_);
+  }
+};
+
+template <int ndim, typename IType, typename OType>
+struct gumbel_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, const Shape<ndim> &lstride,
+                                  const Shape<ndim> &hstride,
+                                  const Shape<ndim> &oshape, IType *loc,
+                                  IType *scale, float *noise, OType *out) {
+    Shape<ndim> coord = unravel(i, oshape);
+    auto lidx = static_cast<index_t>(dot(coord, lstride));
+    auto hidx = static_cast<index_t>(dot(coord, hstride));
+    IType loc_value = loc[lidx];
+    IType scale_value = scale[hidx];
+    noise[i] = -log(-log(noise[i]));
+    out[i] = loc_value + noise[i] * scale_value;
+  }
+};
+
+struct gumbel_two_tensor {
+  template <typename xpu, int ndim, typename IType, typename OType>
+  static void op(Stream<xpu> *s, const int out_size, const Shape<ndim> &lstride,
+                 const Shape<ndim> &hstride, const Shape<ndim> &oshape,
+                 const Tensor<xpu, 1, IType>& loc, const Tensor<xpu, 1, IType>& scale,
+                 const Tensor<xpu, 1, float>& noise, const Tensor<xpu, 1, OType>& out) {
+    Kernel<gumbel_kernel<ndim, IType, OType>, xpu>::Launch(
 
 Review comment:
   For <ndim, IType, OType> could not be got during the registration.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] xidulu commented on a change in pull request #17302: [numpy]add op random.logistic, random.gumbel

Posted by GitBox <gi...@apache.org>.
xidulu commented on a change in pull request #17302: [numpy]add op random.logistic, random.gumbel
URL: https://github.com/apache/incubator-mxnet/pull/17302#discussion_r367796729
 
 

 ##########
 File path: python/mxnet/numpy/random.py
 ##########
 @@ -200,6 +200,192 @@ def normal(loc=0.0, scale=1.0, size=None, dtype=None, ctx=None, out=None):
     return _mx_nd_np.random.normal(loc, scale, size, dtype, ctx, out)
 
 
+def logistic(loc=0.0, scale=1.0, size=None):
+    r"""Draw samples from a logistic distribution.
+
+    Samples are drawn from a logistic distribution with specified
+    parameters, loc (location or mean, also median), and scale (>0).
+
+    Parameters
+    ----------
+    loc : float or array_like of floats, optional
+        Parameter of the distribution. Default is 0.
+    scale : float or array_like of floats, optional
+        Parameter of the distribution. Must be non-negative.
+        Default is 1.
+    size : int or tuple of ints, optional
+        Output shape.  If the given shape is, e.g., ``(m, n, k)``, then
+        ``m * n * k`` samples are drawn.  If size is ``None`` (default),
+        a single value is returned if ``loc`` and ``scale`` are both scalars.
+        Otherwise, ``np.broadcast(loc, scale).size`` samples are drawn.
+
+    Returns
+    -------
+    out : ndarray or scalar
+        Drawn samples from the parameterized logistic distribution.
+
+    See Also
+    --------
+    scipy.stats.logistic : probability density function, distribution or
+        cumulative density function, etc.
+
+    Notes
+    -----
+    The probability density for the Logistic distribution is
+
+    .. math:: P(x) = P(x) = \frac{e^{-(x-\mu)/s}}{s(1+e^{-(x-\mu)/s})^2},
+
+    where :math:`\mu` = location and :math:`s` = scale.
+
+    The Logistic distribution is used in Extreme Value problems where it
+    can act as a mixture of Gumbel distributions, in Epidemiology, and by
+    the World Chess Federation (FIDE) where it is used in the Elo ranking
+    system, assuming the performance of each player is a logistically
+    distributed random variable.
+
+    References
+    ----------
+    .. [1] Reiss, R.-D. and Thomas M. (2001), "Statistical Analysis of
+        Extreme Values, from Insurance, Finance, Hydrology and Other
+        Fields," Birkhauser Verlag, Basel, pp 132-133.
+    .. [2] Weisstein, Eric W. "Logistic Distribution." From
+        MathWorld--A Wolfram Web Resource.
+        http://mathworld.wolfram.com/LogisticDistribution.html
+    .. [3] Wikipedia, "Logistic-distribution",
+        https://en.wikipedia.org/wiki/Logistic_distribution
+
+    Examples
+    --------
+    Draw samples from the distribution:
+
+    >>> loc, scale = 10, 1
+    >>> s = np.random.logistic(loc, scale, 10000)
+    >>> import matplotlib.pyplot as plt
+    >>> count, bins, ignored = plt.hist(s, bins=50)
+
+    #   plot against distribution
+
+    >>> def logist(x, loc, scale):
+    ...     return np.exp((loc-x)/scale)/(scale*(1+np.exp((loc-x)/scale))**2)
+    >>> lgst_val = logist(bins, loc, scale)
+    >>> plt.plot(bins, lgst_val * count.max() / lgst_val.max())
+    >>> plt.show()
+    """
+    return _mx_nd_np.random.logistic(loc, scale, size)
+
+
+def gumbel(loc=0.0, scale=1.0, size=None):
+    r"""Draw samples from a Gumbel distribution.
+
+    Draw samples from a Gumbel distribution with specified location and
+    scale.  For more information on the Gumbel distribution, see
+    Notes and References below.
+
+    Parameters
+    ----------
+    loc : float or array_like of floats, optional
+        The location of the mode of the distribution. Default is 0.
+    scale : float or array_like of floats, optional
+        The scale parameter of the distribution. Default is 1. Must be non-
+        negative.
+    size : int or tuple of ints, optional
+        Output shape.  If the given shape is, e.g., ``(m, n, k)``, then
+        ``m * n * k`` samples are drawn.  If size is ``None`` (default),
+        a single value is returned if ``loc`` and ``scale`` are both scalars.
+        Otherwise, ``np.broadcast(loc, scale).size`` samples are drawn.
+
+    Returns
+    -------
+    out : ndarray or scalar
+        Drawn samples from the parameterized Gumbel distribution.
+
+    See Also
 
 Review comment:
   Remove this section.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] xidulu commented on a change in pull request #17302: [numpy]add op random.logistic, random.gumbel

Posted by GitBox <gi...@apache.org>.
xidulu commented on a change in pull request #17302: [numpy]add op random.logistic, random.gumbel
URL: https://github.com/apache/incubator-mxnet/pull/17302#discussion_r367796564
 
 

 ##########
 File path: python/mxnet/numpy/random.py
 ##########
 @@ -200,6 +200,192 @@ def normal(loc=0.0, scale=1.0, size=None, dtype=None, ctx=None, out=None):
     return _mx_nd_np.random.normal(loc, scale, size, dtype, ctx, out)
 
 
+def logistic(loc=0.0, scale=1.0, size=None):
+    r"""Draw samples from a logistic distribution.
+
+    Samples are drawn from a logistic distribution with specified
+    parameters, loc (location or mean, also median), and scale (>0).
+
+    Parameters
+    ----------
+    loc : float or array_like of floats, optional
+        Parameter of the distribution. Default is 0.
+    scale : float or array_like of floats, optional
+        Parameter of the distribution. Must be non-negative.
+        Default is 1.
+    size : int or tuple of ints, optional
+        Output shape.  If the given shape is, e.g., ``(m, n, k)``, then
+        ``m * n * k`` samples are drawn.  If size is ``None`` (default),
+        a single value is returned if ``loc`` and ``scale`` are both scalars.
+        Otherwise, ``np.broadcast(loc, scale).size`` samples are drawn.
+
+    Returns
+    -------
+    out : ndarray or scalar
+        Drawn samples from the parameterized logistic distribution.
+
+    See Also
+    --------
 
 Review comment:
   Remove this section.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] xidulu commented on a change in pull request #17302: [numpy]add op random.logistic, random.gumbel

Posted by GitBox <gi...@apache.org>.
xidulu commented on a change in pull request #17302: [numpy]add op random.logistic, random.gumbel
URL: https://github.com/apache/incubator-mxnet/pull/17302#discussion_r377427243
 
 

 ##########
 File path: python/mxnet/ndarray/numpy/random.py
 ##########
 @@ -227,6 +228,95 @@ def lognormal(mean=0.0, sigma=1.0, size=None, dtype=None, ctx=None, out=None):
     return _mx_np_op.exp(normal(loc=mean, scale=sigma, size=size, dtype=dtype, ctx=ctx, out=out))
 
 
+def logistic(loc=0.0, scale=1.0, size=None, ctx=None, out=None):
+    r"""Draw samples from a logistic distribution.
+
+    Samples are drawn from a logistic distribution with specified
+    parameters, loc (location or mean, also median), and scale (>0).
+
+    Parameters
+    ----------
+    loc : float or array_like of floats, optional
+        Parameter of the distribution. Default is 0.
+    scale : float or array_like of floats, optional
+        Parameter of the distribution. Must be non-negative.
+        Default is 1.
+    size : int or tuple of ints, optional
+        Output shape.  If the given shape is, e.g., ``(m, n, k)``, then
+        ``m * n * k`` samples are drawn.  If size is ``None`` (default),
+        a single value is returned if ``loc`` and ``scale`` are both scalars.
+        Otherwise, ``np.broadcast(loc, scale).size`` samples are drawn.
+
+    Returns
+    -------
+    out : ndarray or scalar
+        Drawn samples from the parameterized logistic distribution.
+    """
+    from ...numpy import ndarray as np_ndarray
+    input_type = (isinstance(loc, np_ndarray), isinstance(scale, np_ndarray))
+    if ctx is None:
+        ctx = current_context()
+    if size == ():
+        size = None
+    if input_type == (True, True):
+        return _npi.logistic(loc, scale, loc=None, scale=None, size=size,
+                             ctx=ctx, out=out)
+    elif input_type == (False, True):
+        return _npi.logistic(scale, loc=loc, scale=None, size=size,
+                             ctx=ctx, out=out)
+    elif input_type == (True, False):
+        return _npi.logistic(loc, loc=None, scale=scale, size=size,
+                             ctx=ctx, out=out)
+    else:
+        return _npi.logistic(loc=loc, scale=scale, size=size,
+                             ctx=ctx, out=out)
+
+
+def gumbel(loc=0.0, scale=1.0, size=None, ctx=None, out=None):
+    r"""Draw samples from a Gumbel distribution.
+
+    Draw samples from a Gumbel distribution with specified location and
+    scale.  For more information on the Gumbel distribution, see
+    Notes and References below.
 
 Review comment:
   Remove "For more information on the Gumbel distribution, see
       Notes and References below."

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] xidulu commented on a change in pull request #17302: [numpy]add op random.logistic, random.gumbel

Posted by GitBox <gi...@apache.org>.
xidulu commented on a change in pull request #17302: [numpy]add op random.logistic, random.gumbel
URL: https://github.com/apache/incubator-mxnet/pull/17302#discussion_r377426937
 
 

 ##########
 File path: python/mxnet/numpy/random.py
 ##########
 @@ -260,6 +261,180 @@ def lognormal(mean=0.0, sigma=1.0, size=None, dtype=None, ctx=None, out=None):
     return _mx_nd_np.random.lognormal(mean, sigma, size, dtype, ctx, out)
 
 
+def logistic(loc=0.0, scale=1.0, size=None, ctx=None, out=None):
+    r"""Draw samples from a logistic distribution.
+
+    Samples are drawn from a logistic distribution with specified
+    parameters, loc (location or mean, also median), and scale (>0).
+
+    Parameters
+    ----------
+    loc : float or array_like of floats, optional
+        Parameter of the distribution. Default is 0.
+    scale : float or array_like of floats, optional
+        Parameter of the distribution. Must be non-negative.
+        Default is 1.
+    size : int or tuple of ints, optional
+        Output shape.  If the given shape is, e.g., ``(m, n, k)``, then
+        ``m * n * k`` samples are drawn.  If size is ``None`` (default),
+        a single value is returned if ``loc`` and ``scale`` are both scalars.
+        Otherwise, ``np.broadcast(loc, scale).size`` samples are drawn.
+
+    Returns
+    -------
+    out : ndarray or scalar
+        Drawn samples from the parameterized logistic distribution.
+
+    Notes
+    -----
+    The probability density for the Logistic distribution is
+
+    .. math:: P(x) = P(x) = \frac{e^{-(x-\mu)/s}}{s(1+e^{-(x-\mu)/s})^2},
+
+    where :math:`\mu` = location and :math:`s` = scale.
+
+    The Logistic distribution is used in Extreme Value problems where it
+    can act as a mixture of Gumbel distributions, in Epidemiology, and by
+    the World Chess Federation (FIDE) where it is used in the Elo ranking
+    system, assuming the performance of each player is a logistically
+    distributed random variable.
+
+    References
+    ----------
+    .. [1] Reiss, R.-D. and Thomas M. (2001), "Statistical Analysis of
+        Extreme Values, from Insurance, Finance, Hydrology and Other
+        Fields," Birkhauser Verlag, Basel, pp 132-133.
+    .. [2] Weisstein, Eric W. "Logistic Distribution." From
+        MathWorld--A Wolfram Web Resource.
+        http://mathworld.wolfram.com/LogisticDistribution.html
+    .. [3] Wikipedia, "Logistic-distribution",
+        https://en.wikipedia.org/wiki/Logistic_distribution
+
+    Examples
+    --------
+    Draw samples from the distribution:
+
+    >>> loc, scale = 10, 1
+    >>> s = np.random.logistic(loc, scale, 10000)
+    >>> import matplotlib.pyplot as plt
+    >>> count, bins, ignored = plt.hist(s, bins=50)
+
+    #   plot against distribution
+
+    >>> def logist(x, loc, scale):
+    ...     return np.exp((loc-x)/scale)/(scale*(1+np.exp((loc-x)/scale))**2)
+    >>> lgst_val = logist(bins, loc, scale)
+    >>> plt.plot(bins, lgst_val * count.max() / lgst_val.max())
+    >>> plt.show()
+    """
+    return _mx_nd_np.random.logistic(loc, scale, size, ctx, out)
+
+
+def gumbel(loc=0.0, scale=1.0, size=None, ctx=None, out=None):
+    r"""Draw samples from a Gumbel distribution.
+
+    Draw samples from a Gumbel distribution with specified location and
+    scale.  For more information on the Gumbel distribution, see
+    Notes and References below.
+
+    Parameters
+    ----------
+    loc : float or array_like of floats, optional
+        The location of the mode of the distribution. Default is 0.
+    scale : float or array_like of floats, optional
+        The scale parameter of the distribution. Default is 1. Must be non-
+        negative.
+    size : int or tuple of ints, optional
+        Output shape.  If the given shape is, e.g., ``(m, n, k)``, then
+        ``m * n * k`` samples are drawn.  If size is ``None`` (default),
+        a single value is returned if ``loc`` and ``scale`` are both scalars.
+        Otherwise, ``np.broadcast(loc, scale).size`` samples are drawn.
+
+    Returns
+    -------
+    out : ndarray or scalar
+        Drawn samples from the parameterized Gumbel distribution.
+
+    Notes
 
 Review comment:
   Not necessary I guess?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] haojin2 commented on a change in pull request #17302: [numpy]add op random.logistic

Posted by GitBox <gi...@apache.org>.
haojin2 commented on a change in pull request #17302: [numpy]add op random.logistic
URL: https://github.com/apache/incubator-mxnet/pull/17302#discussion_r366765107
 
 

 ##########
 File path: src/operator/numpy/random/np_logistic_op.h
 ##########
 @@ -0,0 +1,331 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * Copyright (c) 2019 by Contributors
+ * \file np_logistic_op.h
+ * \brief Operator for numpy sampling from logistic distributions
+ */
+#ifndef MXNET_OPERATOR_NUMPY_RANDOM_NP_LOGISTIC_OP_H_
+#define MXNET_OPERATOR_NUMPY_RANDOM_NP_LOGISTIC_OP_H_
+
+#include <mxnet/operator_util.h>
+#include <cstdio>
+#include <algorithm>
+#include <string>
+#include <vector>
+#include <cmath>
+#include "../../elemwise_op_common.h"
+#include "../../mshadow_op.h"
+#include "../../mxnet_op.h"
+#include "../../operator_common.h"
+#include "../../tensor/elemwise_binary_broadcast_op.h"
+#include "./dist_common.h"
+
+namespace mxnet {
+namespace op {
+
+struct NumpyLogisticParam : public dmlc::Parameter<NumpyLogisticParam> {
+  dmlc::optional<float> loc;
+  dmlc::optional<float> scale;
+  dmlc::optional<mxnet::Tuple<int>> size;
+  DMLC_DECLARE_PARAMETER(NumpyLogisticParam) {
+    DMLC_DECLARE_FIELD(loc);
+    DMLC_DECLARE_FIELD(scale);
+    DMLC_DECLARE_FIELD(size)
+        .set_default(dmlc::optional<mxnet::Tuple<int>>())
+        .describe(
+            "Output shape. If the given shape is, "
+            "e.g., (m, n, k), then m * n * k samples are drawn. "
+            "Default is None, in which case a single value is returned.");
+  }
+};
+
+inline bool NumpyLogisticOpType(const nnvm::NodeAttrs &attrs,
+                               std::vector<int> *in_attrs,
+                               std::vector<int> *out_attrs) {
+
+  (*out_attrs)[0] = mshadow::kFloat32;
+  (*out_attrs)[1] = mshadow::kFloat32;
+  return true;
+}
+
+namespace mxnet_op {
+
+template <typename DType>
+struct logistic_two_scalar_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, float loc, float scale,
+                                  float *noise, DType *out) {
+    noise[i] = log(noise[i]) - log(1 - noise[i]);
+    out[i] = loc + noise[i] * scale;
+  }
+};
+
+template <typename IType>
+struct check_legal_scale_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, IType *scalar, float* flag) {
+    if (scalar[i] < 0) {
+      *flag = -1.0;
+    }
+  }
+};
+
+template <int ndim, typename IType, typename OType>
+struct logistic_one_scalar_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, int scalar_pos,
+                                  const Shape<ndim> &stride,
+                                  const Shape<ndim> &oshape, IType *array,
+                                  float scalar, float *noise, OType *out) {
+    Shape<ndim> coord = unravel(i, oshape);
+    auto idx = static_cast<index_t>(dot(coord, stride));
+    IType loc_value;
+    IType scale_value;
+    if (scalar_pos == 0) {
+      loc_value = scalar;
+      scale_value = array[idx];
+    } else {
+      loc_value = array[idx];
+      scale_value = scalar;
+    }
+    noise[i] = log(noise[i]) - log(1 - noise[i]);
+    out[i] = loc_value + noise[i] * scale_value;
+  }
+};
+
+template <int ndim, typename IType, typename OType>
+struct logistic_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, const Shape<ndim> &lstride,
+                                  const Shape<ndim> &hstride,
+                                  const Shape<ndim> &oshape, IType *loc,
+                                  IType *scale, float *noise, OType *out) {
+    Shape<ndim> coord = unravel(i, oshape);
+    auto lidx = static_cast<index_t>(dot(coord, lstride));
+    auto hidx = static_cast<index_t>(dot(coord, hstride));
+    IType loc_value = loc[lidx];
+    IType scale_value = scale[hidx];
+    noise[i] = log(noise[i]) - log(1 - noise[i]);
+    out[i] = loc_value + noise[i] * scale_value;
+  }
+};
+
+}  // namespace mxnet_op
+
+template <typename xpu>
+void NumpyLogisticForward(const nnvm::NodeAttrs &attrs,
+                          const OpContext &ctx,
+                          const std::vector<TBlob> &inputs,
+                          const std::vector<OpReqType> &req,
+                          const std::vector<TBlob> &outputs) {
+  using namespace mshadow;
+  using namespace mxnet_op;
+  const NumpyLogisticParam &param = nnvm::get<NumpyLogisticParam>(attrs.parsed);
+  Stream<xpu> *s = ctx.get_stream<xpu>();
+  // Generate base random number.
+  Random<xpu, float> *prnd = ctx.requested[0].get_random<xpu, float>(s);
+  Tensor<xpu, 1, float> workspace =
+      ctx.requested[1].get_space_typed<xpu, 1, float>(Shape1(1), s);
+  Tensor<xpu, 1, float> logistic_tensor = outputs[1].FlatTo1D<xpu, float>(s);
+  Tensor<xpu, 1, float> indicator_device = workspace;
+  float indicator_host = 1.0;
+  float *indicator_device_ptr = indicator_device.dptr_;
+  Kernel<set_zero, xpu>::Launch(s, 1, indicator_device_ptr);
+  prnd->SampleUniform(&logistic_tensor, 0.0, 1.0);
+  mxnet::TShape new_lshape, new_hshape, new_oshape;
+  // [scalar scalar] case
+  if (inputs.size() == 0U) {
+    CHECK_GE(param.scale.value(), 0.0) << "ValueError: scale < 0";
+    MSHADOW_TYPE_SWITCH(outputs[0].type_flag_, DType, {
+      Kernel<logistic_two_scalar_kernel<DType>, xpu>::Launch(
+          s, outputs[0].Size(), param.loc.value(), param.scale.value(),
+          logistic_tensor.dptr_, outputs[0].dptr<DType>());
+    });
+  } else if (inputs.size() == 1U) {
+    // [scalar tensor], [tensor scalar] case
+    int ndim = FillShape(inputs[0].shape_, inputs[0].shape_, outputs[0].shape_,
+                         &new_lshape, &new_lshape, &new_oshape);
+    int scalar_pos;
+    float scalar_value;
+    if (param.loc.has_value()) {
+      scalar_pos = 0;
+      scalar_value = param.loc.value();
+      MSHADOW_TYPE_SWITCH(inputs[0].type_flag_, IType, {
+        Kernel<check_legal_scale_kernel<IType>, xpu>::Launch(
+            s, inputs[0].Size(), inputs[0].dptr<IType>(), indicator_device_ptr);
+      });
+      _copy<xpu>(s, &indicator_host, indicator_device_ptr);
+      CHECK_GE(indicator_host, 0.0) << "ValueError: scale < 0";
+    } else {
+      scalar_pos = 1;
+      scalar_value = param.scale.value();
+      CHECK_GE(scalar_value, 0.0) << "ValueError: scale < 0";
+    }
+    MSHADOW_TYPE_SWITCH(inputs[0].type_flag_, IType, {
+      MSHADOW_TYPE_SWITCH(outputs[0].type_flag_, OType, {
+        BROADCAST_NDIM_SWITCH(ndim, NDim, {
+          Shape<NDim> oshape = new_oshape.get<NDim>();
+          Shape<NDim> stride = calc_stride(new_lshape.get<NDim>());
+          Kernel<logistic_one_scalar_kernel<NDim, IType, OType>, xpu>::Launch(
+              s, outputs[0].Size(), scalar_pos, stride, oshape,
+              inputs[0].dptr<IType>(), scalar_value, logistic_tensor.dptr_,
+              outputs[0].dptr<OType>());
+        });
+      });
+    });
+  } else if (inputs.size() == 2U) {
+    // [tensor tensor] case
+    MSHADOW_TYPE_SWITCH(inputs[0].type_flag_, IType, {
+      Kernel<check_legal_scale_kernel<IType>, xpu>::Launch(
+          s, inputs[1].Size(), inputs[1].dptr<IType>(), indicator_device_ptr);
+    });
+    _copy<xpu>(s, &indicator_host, indicator_device_ptr);
+    CHECK_GE(indicator_host, 0.0) << "ValueError: scale < 0";
+    int ndim = FillShape(inputs[0].shape_, inputs[1].shape_, outputs[0].shape_,
+                         &new_lshape, &new_hshape, &new_oshape);
+    MSHADOW_TYPE_SWITCH(inputs[0].type_flag_, IType, {
+      MSHADOW_TYPE_SWITCH(outputs[0].type_flag_, OType, {
+        BROADCAST_NDIM_SWITCH(ndim, NDim, {
+          Shape<NDim> oshape = new_oshape.get<NDim>();
+          Shape<NDim> lstride = calc_stride(new_lshape.get<NDim>());
+          Shape<NDim> hstride = calc_stride(new_hshape.get<NDim>());
+          Kernel<logistic_kernel<NDim, IType, OType>, xpu>::Launch(
+              s, outputs[0].Size(), lstride, hstride, oshape,
+              inputs[0].dptr<IType>(), inputs[1].dptr<IType>(),
+              logistic_tensor.dptr_, outputs[0].dptr<OType>());
+        });
+      });
+    });
+  }
+}
+
+template<typename xpu, int ndim, typename DType>
+inline void LogisticReparamBackwardImpl(const OpContext& ctx,
+                                        const std::vector<TBlob>& inputs,
+                                        const std::vector<OpReqType>& req,
+                                        const std::vector<TBlob>& outputs,
+                                        const mxnet::TShape& new_lshape,
+                                        const mxnet::TShape& new_rshape,
+                                        const mxnet::TShape& new_oshape) {
+  using namespace mshadow;
+  using namespace mshadow::expr;
+  using namespace broadcast;
+  Stream<xpu> *s = ctx.get_stream<xpu>();
+  const TBlob lgrad = outputs[0].reshape(new_lshape);
+  const TBlob rgrad = outputs[1].reshape(new_rshape);
+  const TBlob ograd = inputs[0].reshape(new_oshape);
+  // Mean
+  const TBlob lhs = inputs[2].reshape(new_lshape);
+  // Variance
+  const TBlob rhs = inputs[3].reshape(new_rshape);
+  const TBlob samples = inputs[4].reshape(new_oshape);
+  const TBlob noise = inputs[5].reshape(new_oshape);
+  size_t workspace_size_l = ReduceWorkspaceSize<ndim, DType>(
+      s, lgrad.shape_, req[0], ograd.shape_, lhs.shape_, rhs.shape_);
+  size_t workspace_size_r = ReduceWorkspaceSize<ndim, DType>(
+      s, rgrad.shape_, req[1], ograd.shape_, lhs.shape_, rhs.shape_);
+  size_t workspace_size = std::max(workspace_size_l, workspace_size_r);
+  Tensor<xpu, 1, char> workspace =
+      ctx.requested[0].get_space_typed<xpu, 1, char>(Shape1(workspace_size), s);
+  Reduce<red::sum, ndim, DType, op::mshadow_op::identity>(s,
+          lgrad, req[0], workspace, ograd);
 
 Review comment:
   ```c++
     Reduce<red::sum, ndim, DType, op::mshadow_op::identity>(
       s, lgrad, req[0], workspace, ograd);
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] reminisce commented on a change in pull request #17302: [numpy]add op random.logistic, random.gumbel

Posted by GitBox <gi...@apache.org>.
reminisce commented on a change in pull request #17302: [numpy]add op random.logistic, random.gumbel
URL: https://github.com/apache/incubator-mxnet/pull/17302#discussion_r368362440
 
 

 ##########
 File path: python/mxnet/ndarray/numpy/random.py
 ##########
 @@ -193,6 +194,83 @@ def normal(loc=0.0, scale=1.0, size=None, dtype=None, ctx=None, out=None):
                            ctx=ctx, dtype=dtype, out=out)
 
 
+def logistic(loc=0.0, scale=1.0, size=None):
 
 Review comment:
   Can we support `ctx` as well? Otherwise, GPU implementation is not fully tested.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] xidulu commented on a change in pull request #17302: [numpy]add op random.logistic, random.gumbel

Posted by GitBox <gi...@apache.org>.
xidulu commented on a change in pull request #17302: [numpy]add op random.logistic, random.gumbel
URL: https://github.com/apache/incubator-mxnet/pull/17302#discussion_r377426714
 
 

 ##########
 File path: python/mxnet/symbol/numpy/random.py
 ##########
 @@ -251,6 +252,95 @@ def lognormal(mean=0.0, sigma=1.0, size=None, dtype=None, ctx=None, out=None):
     return _mx_np_symbol.exp(normal(loc=mean, scale=sigma, size=size, dtype=dtype, ctx=ctx, out=out))
 
 
+def logistic(loc=0.0, scale=1.0, size=None, ctx=None, out=None):
+    r"""Draw samples from a logistic distribution.
+
+    Samples are drawn from a logistic distribution with specified
+    parameters, loc (location or mean, also median), and scale (>0).
+
+    Parameters
+    ----------
+    loc : float or array_like of floats, optional
+        Parameter of the distribution. Default is 0.
+    scale : float or array_like of floats, optional
+        Parameter of the distribution. Must be non-negative.
+        Default is 1.
+    size : int or tuple of ints, optional
+        Output shape.  If the given shape is, e.g., ``(m, n, k)``, then
+        ``m * n * k`` samples are drawn.  If size is ``None`` (default),
+        a single value is returned if ``loc`` and ``scale`` are both scalars.
+        Otherwise, ``np.broadcast(loc, scale).size`` samples are drawn.
+
+    Returns
+    -------
+    out : ndarray or scalar
+        Drawn samples from the parameterized logistic distribution.
+    """
+    from ._symbol import _Symbol as np_symbol
+    input_type = (isinstance(loc, np_symbol), isinstance(scale, np_symbol))
+    if ctx is None:
+        ctx = current_context()
+    if size == ():
+        size = None
+    if input_type == (True, True):
+        return _npi.logistic(loc, scale, loc=None, scale=None, size=size,
+                             ctx=ctx, out=out)
+    elif input_type == (False, True):
+        return _npi.logistic(scale, loc=loc, scale=None, size=size,
+                             ctx=ctx, out=out)
+    elif input_type == (True, False):
+        return _npi.logistic(loc, loc=None, scale=scale, size=size,
+                             ctx=ctx, out=out)
+    else:
+        return _npi.logistic(loc=loc, scale=scale, size=size,
+                             ctx=ctx, out=out)
+
+
+def gumbel(loc=0.0, scale=1.0, size=None, ctx=None, out=None):
+    r"""Draw samples from a Gumbel distribution.
+
+    Draw samples from a Gumbel distribution with specified location and
+    scale.  For more information on the Gumbel distribution, see
+    Notes and References below.
 
 Review comment:
   Remove this sentence.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] xidulu commented on a change in pull request #17302: [numpy]add op random.logistic, random.gumbel

Posted by GitBox <gi...@apache.org>.
xidulu commented on a change in pull request #17302: [numpy]add op random.logistic, random.gumbel
URL: https://github.com/apache/incubator-mxnet/pull/17302#discussion_r370157230
 
 

 ##########
 File path: python/mxnet/ndarray/numpy/random.py
 ##########
 @@ -246,87 +335,6 @@ def multinomial(n, pvals, size=None):
         return _npi.multinomial(n=n, pvals=pvals, size=size)
 
 
-def multivariate_normal(mean, cov, size=None, check_valid=None, tol=None):
 
 Review comment:
   ?? Please put this back.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] xidulu commented on a change in pull request #17302: [numpy]add op random.logistic

Posted by GitBox <gi...@apache.org>.
xidulu commented on a change in pull request #17302: [numpy]add op random.logistic
URL: https://github.com/apache/incubator-mxnet/pull/17302#discussion_r366410192
 
 

 ##########
 File path: tests/python/unittest/test_numpy_op.py
 ##########
 @@ -3325,15 +3363,15 @@ def hybrid_forward(self, F, loc, scale):
     for hybridize in [False, True]:
         for dtype in dtypes:
             for ((shape1, shape2), out_shape) in zip(param_shape, output_shapes):
-                test_normal_grad = TestNormalGrad(out_shape)
+                test_logistic_grad = TestLogisticGrad(out_shape)
                 if hybridize:
-                    test_normal_grad.hybridize()
+                    test_logistic_grad.hybridize()
                 loc = np.zeros(shape1)
                 loc.attach_grad()
                 scale = np.ones(shape2)
                 scale.attach_grad()
                 with mx.autograd.record():
-                    samples = test_normal_grad(loc, scale)
+                    samples = test_logistic_grad(loc, scale)
                 samples.backward()
                 assert loc.grad.shape == shape1
                 assert scale.grad.shape == shape2
 
 Review comment:
   Should include cases testing correctness of the gradients.
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] xidulu commented on a change in pull request #17302: [numpy]add op random.logistic, random.gumbel

Posted by GitBox <gi...@apache.org>.
xidulu commented on a change in pull request #17302: [numpy]add op random.logistic, random.gumbel
URL: https://github.com/apache/incubator-mxnet/pull/17302#discussion_r377426937
 
 

 ##########
 File path: python/mxnet/numpy/random.py
 ##########
 @@ -260,6 +261,180 @@ def lognormal(mean=0.0, sigma=1.0, size=None, dtype=None, ctx=None, out=None):
     return _mx_nd_np.random.lognormal(mean, sigma, size, dtype, ctx, out)
 
 
+def logistic(loc=0.0, scale=1.0, size=None, ctx=None, out=None):
+    r"""Draw samples from a logistic distribution.
+
+    Samples are drawn from a logistic distribution with specified
+    parameters, loc (location or mean, also median), and scale (>0).
+
+    Parameters
+    ----------
+    loc : float or array_like of floats, optional
+        Parameter of the distribution. Default is 0.
+    scale : float or array_like of floats, optional
+        Parameter of the distribution. Must be non-negative.
+        Default is 1.
+    size : int or tuple of ints, optional
+        Output shape.  If the given shape is, e.g., ``(m, n, k)``, then
+        ``m * n * k`` samples are drawn.  If size is ``None`` (default),
+        a single value is returned if ``loc`` and ``scale`` are both scalars.
+        Otherwise, ``np.broadcast(loc, scale).size`` samples are drawn.
+
+    Returns
+    -------
+    out : ndarray or scalar
+        Drawn samples from the parameterized logistic distribution.
+
+    Notes
+    -----
+    The probability density for the Logistic distribution is
+
+    .. math:: P(x) = P(x) = \frac{e^{-(x-\mu)/s}}{s(1+e^{-(x-\mu)/s})^2},
+
+    where :math:`\mu` = location and :math:`s` = scale.
+
+    The Logistic distribution is used in Extreme Value problems where it
+    can act as a mixture of Gumbel distributions, in Epidemiology, and by
+    the World Chess Federation (FIDE) where it is used in the Elo ranking
+    system, assuming the performance of each player is a logistically
+    distributed random variable.
+
+    References
+    ----------
+    .. [1] Reiss, R.-D. and Thomas M. (2001), "Statistical Analysis of
+        Extreme Values, from Insurance, Finance, Hydrology and Other
+        Fields," Birkhauser Verlag, Basel, pp 132-133.
+    .. [2] Weisstein, Eric W. "Logistic Distribution." From
+        MathWorld--A Wolfram Web Resource.
+        http://mathworld.wolfram.com/LogisticDistribution.html
+    .. [3] Wikipedia, "Logistic-distribution",
+        https://en.wikipedia.org/wiki/Logistic_distribution
+
+    Examples
+    --------
+    Draw samples from the distribution:
+
+    >>> loc, scale = 10, 1
+    >>> s = np.random.logistic(loc, scale, 10000)
+    >>> import matplotlib.pyplot as plt
+    >>> count, bins, ignored = plt.hist(s, bins=50)
+
+    #   plot against distribution
+
+    >>> def logist(x, loc, scale):
+    ...     return np.exp((loc-x)/scale)/(scale*(1+np.exp((loc-x)/scale))**2)
+    >>> lgst_val = logist(bins, loc, scale)
+    >>> plt.plot(bins, lgst_val * count.max() / lgst_val.max())
+    >>> plt.show()
+    """
+    return _mx_nd_np.random.logistic(loc, scale, size, ctx, out)
+
+
+def gumbel(loc=0.0, scale=1.0, size=None, ctx=None, out=None):
+    r"""Draw samples from a Gumbel distribution.
+
+    Draw samples from a Gumbel distribution with specified location and
+    scale.  For more information on the Gumbel distribution, see
+    Notes and References below.
+
+    Parameters
+    ----------
+    loc : float or array_like of floats, optional
+        The location of the mode of the distribution. Default is 0.
+    scale : float or array_like of floats, optional
+        The scale parameter of the distribution. Default is 1. Must be non-
+        negative.
+    size : int or tuple of ints, optional
+        Output shape.  If the given shape is, e.g., ``(m, n, k)``, then
+        ``m * n * k`` samples are drawn.  If size is ``None`` (default),
+        a single value is returned if ``loc`` and ``scale`` are both scalars.
+        Otherwise, ``np.broadcast(loc, scale).size`` samples are drawn.
+
+    Returns
+    -------
+    out : ndarray or scalar
+        Drawn samples from the parameterized Gumbel distribution.
+
+    Notes
 
 Review comment:
   Remove the notes and reference section.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] xidulu commented on a change in pull request #17302: [numpy]add op random.logistic, random.gumbel

Posted by GitBox <gi...@apache.org>.
xidulu commented on a change in pull request #17302: [numpy]add op random.logistic, random.gumbel
URL: https://github.com/apache/incubator-mxnet/pull/17302#discussion_r369386680
 
 

 ##########
 File path: tests/python/unittest/test_numpy_op.py
 ##########
 @@ -3323,21 +3322,96 @@ def hybrid_forward(self, F, loc, scale):
         (3, 4, 5)
     ]
     for hybridize in [False, True]:
-        for dtype in dtypes:
-            for ((shape1, shape2), out_shape) in zip(param_shape, output_shapes):
-                test_normal_grad = TestNormalGrad(out_shape)
-                if hybridize:
-                    test_normal_grad.hybridize()
-                loc = np.zeros(shape1)
-                loc.attach_grad()
-                scale = np.ones(shape2)
-                scale.attach_grad()
-                with mx.autograd.record():
-                    samples = test_normal_grad(loc, scale)
-                samples.backward()
-                assert loc.grad.shape == shape1
-                assert scale.grad.shape == shape2
-                assert_almost_equal(loc.grad.asnumpy().sum(), _np.ones(out_shape).sum(), rtol=1e-3, atol=1e-5)
+        for ((shape1, shape2), out_shape) in zip(param_shape, output_shapes):
+            test_normal_grad = TestNormalGrad(out_shape)
+            if hybridize:
+                test_normal_grad.hybridize()
+            loc = np.zeros(shape1)
+            loc.attach_grad()
+            scale = np.ones(shape2)
+            scale.attach_grad()
+            with mx.autograd.record():
+                samples = test_normal_grad(loc, scale)
+            samples.backward()
+            assert loc.grad.shape == shape1
+            assert scale.grad.shape == shape2
+            assert_almost_equal(loc.grad.asnumpy().sum(), _np.ones(out_shape).sum(), rtol=1e-3, atol=1e-5)
+
+
+@with_seed()
+@use_np
+def test_np_logistic_grad():
+    class TestLogisticGrad(HybridBlock):
+        def __init__(self, shape):
+            super(TestLogisticGrad, self).__init__()
+            self._shape = shape
+
+        def hybrid_forward(self, F, loc, scale):
+            return F.np.random.logistic(loc, scale, self._shape)
+
+    param_shape = [
+        [(3, 2), (3, 2)],
+        [(3, 2, 2), (3, 2, 2)],
+        [(3, 4, 5), (4, 1)],
+    ]
+    output_shapes = [
+        (3, 2),
+        (4, 3, 2, 2),
+        (3, 4, 5)
+    ]
+    for hybridize in [False, True]:
+        for ((shape1, shape2), out_shape) in zip(param_shape, output_shapes):
+            test_logistic_grad = TestLogisticGrad(out_shape)
+            if hybridize:
+                test_logistic_grad.hybridize()
+            loc = np.zeros(shape1)
+            loc.attach_grad()
+            scale = np.ones(shape2)
+            scale.attach_grad()
+            with mx.autograd.record():
+                samples = test_logistic_grad(loc, scale)
+            samples.backward()
+            assert loc.grad.shape == shape1
+            assert scale.grad.shape == shape2
+            assert_almost_equal(loc.grad.asnumpy().sum(), _np.ones(out_shape).sum(), rtol=1e-3, atol=1e-5)
+
+
+@with_seed()
+@use_np
+def test_np_gumbel_grad():
 
 Review comment:
   You could merge `test_np_normal_grad`,`test_np_gumbel_grad` and `test_np_logistic_grad` into one test case, and iterate the corresponding methods through a list and `getattr` like https://github.com/apache/incubator-mxnet/blob/master/tests/python/unittest/test_numpy_op.py#L3411

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] xidulu commented on a change in pull request #17302: [numpy]add op random.logistic, random.gumbel

Posted by GitBox <gi...@apache.org>.
xidulu commented on a change in pull request #17302: [numpy]add op random.logistic, random.gumbel
URL: https://github.com/apache/incubator-mxnet/pull/17302#discussion_r377427161
 
 

 ##########
 File path: python/mxnet/numpy/random.py
 ##########
 @@ -260,6 +261,180 @@ def lognormal(mean=0.0, sigma=1.0, size=None, dtype=None, ctx=None, out=None):
     return _mx_nd_np.random.lognormal(mean, sigma, size, dtype, ctx, out)
 
 
+def logistic(loc=0.0, scale=1.0, size=None, ctx=None, out=None):
+    r"""Draw samples from a logistic distribution.
+
+    Samples are drawn from a logistic distribution with specified
+    parameters, loc (location or mean, also median), and scale (>0).
+
+    Parameters
+    ----------
+    loc : float or array_like of floats, optional
+        Parameter of the distribution. Default is 0.
+    scale : float or array_like of floats, optional
+        Parameter of the distribution. Must be non-negative.
+        Default is 1.
+    size : int or tuple of ints, optional
+        Output shape.  If the given shape is, e.g., ``(m, n, k)``, then
+        ``m * n * k`` samples are drawn.  If size is ``None`` (default),
+        a single value is returned if ``loc`` and ``scale`` are both scalars.
+        Otherwise, ``np.broadcast(loc, scale).size`` samples are drawn.
+
+    Returns
+    -------
+    out : ndarray or scalar
+        Drawn samples from the parameterized logistic distribution.
+
+    Notes
 
 Review comment:
   Remove the notes section and reference section.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] haojin2 commented on a change in pull request #17302: [numpy]add op random.logistic

Posted by GitBox <gi...@apache.org>.
haojin2 commented on a change in pull request #17302: [numpy]add op random.logistic
URL: https://github.com/apache/incubator-mxnet/pull/17302#discussion_r366763583
 
 

 ##########
 File path: src/operator/numpy/random/np_logistic_op.h
 ##########
 @@ -0,0 +1,331 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * Copyright (c) 2019 by Contributors
+ * \file np_logistic_op.h
+ * \brief Operator for numpy sampling from logistic distributions
+ */
+#ifndef MXNET_OPERATOR_NUMPY_RANDOM_NP_LOGISTIC_OP_H_
+#define MXNET_OPERATOR_NUMPY_RANDOM_NP_LOGISTIC_OP_H_
+
+#include <mxnet/operator_util.h>
+#include <cstdio>
+#include <algorithm>
+#include <string>
+#include <vector>
+#include <cmath>
+#include "../../elemwise_op_common.h"
+#include "../../mshadow_op.h"
+#include "../../mxnet_op.h"
+#include "../../operator_common.h"
+#include "../../tensor/elemwise_binary_broadcast_op.h"
+#include "./dist_common.h"
+
+namespace mxnet {
+namespace op {
+
+struct NumpyLogisticParam : public dmlc::Parameter<NumpyLogisticParam> {
+  dmlc::optional<float> loc;
+  dmlc::optional<float> scale;
+  dmlc::optional<mxnet::Tuple<int>> size;
+  DMLC_DECLARE_PARAMETER(NumpyLogisticParam) {
+    DMLC_DECLARE_FIELD(loc);
+    DMLC_DECLARE_FIELD(scale);
+    DMLC_DECLARE_FIELD(size)
+        .set_default(dmlc::optional<mxnet::Tuple<int>>())
 
 Review comment:
   2-space indentation.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] xidulu commented on a change in pull request #17302: [numpy]add op random.logistic, random.gumbel

Posted by GitBox <gi...@apache.org>.
xidulu commented on a change in pull request #17302: [numpy]add op random.logistic, random.gumbel
URL: https://github.com/apache/incubator-mxnet/pull/17302#discussion_r369385862
 
 

 ##########
 File path: src/operator/numpy/random/np_location_scale_op.h
 ##########
 @@ -0,0 +1,449 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * Copyright (c) 2019 by Contributors
+ * \file np_location_scale_op.h
+ * \brief Operator for numpy sampling from localtion scale distributions
+ */
+#ifndef MXNET_OPERATOR_NUMPY_RANDOM_NP_LOCATION_SCALE_OP_H_
+#define MXNET_OPERATOR_NUMPY_RANDOM_NP_LOCATION_SCALE_OP_H_
+
+#include <mxnet/operator_util.h>
+#include <cstdio>
+#include <algorithm>
+#include <string>
+#include <vector>
+#include <cmath>
+#include "../../elemwise_op_common.h"
+#include "../../mshadow_op.h"
+#include "../../mxnet_op.h"
+#include "../../operator_common.h"
+#include "../../tensor/elemwise_binary_broadcast_op.h"
+#include "./dist_common.h"
+
+namespace mxnet {
+namespace op {
+
+struct NumpyLocationScaleParam : public dmlc::Parameter<NumpyLocationScaleParam> {
+  dmlc::optional<float> loc;
+  dmlc::optional<float> scale;
+  dmlc::optional<mxnet::Tuple<int>> size;
+  std::string ctx;
+  DMLC_DECLARE_PARAMETER(NumpyLocationScaleParam) {
+    DMLC_DECLARE_FIELD(loc);
+    DMLC_DECLARE_FIELD(scale);
+    DMLC_DECLARE_FIELD(size)
+      .set_default(dmlc::optional<mxnet::Tuple<int>>())
+      .describe(
+          "Output shape. If the given shape is, "
+          "e.g., (m, n, k), then m * n * k samples are drawn. "
+          "Default is None, in which case a single value is returned.");
+    DMLC_DECLARE_FIELD(ctx).set_default("cpu").describe(
+        "Context of output, in format [cpu|gpu|cpu_pinned](n)."
+        " Only used for imperative calls.");
+  }
+};
+
+inline bool NumpyLocationScaleOpType(const nnvm::NodeAttrs &attrs,
+                                     std::vector<int> *in_attrs,
+                                     std::vector<int> *out_attrs) {
+  (*out_attrs)[0] = mshadow::kFloat32;
+  (*out_attrs)[1] = mshadow::kFloat32;
+  return true;
+}
+
+namespace mxnet_op {
+
+struct logistic_two_scalar_kernel {
+  template <typename DType>
+  MSHADOW_XINLINE static void Map(index_t i, float loc, float scale,
+                                  float *noise, DType *out) {
+    noise[i] = log(noise[i]) - log(1 - noise[i]);
+    out[i] = loc + noise[i] * scale;
+  }
+};
+
+struct gumbel_two_scalar_kernel {
+  template <typename DType>
+  MSHADOW_XINLINE static void Map(index_t i, float loc, float scale,
+                                  float *noise, DType *out) {
+    noise[i] = -log(-log(noise[i]));
+    out[i] = loc + noise[i] * scale;
+  }
+};
+
+
+template <typename IType>
+struct check_legal_scale_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, IType *scalar, float* flag) {
+    if (scalar[i] < 0) {
+      *flag = -1.0;
+    }
+  }
+};
+
+struct logistic_one_scalar_kernel {
+  template <int ndim, typename IType, typename OType>
+  MSHADOW_XINLINE static void Map(index_t i, int scalar_pos,
+                                  const Shape<ndim> &stride,
+                                  const Shape<ndim> &oshape, IType *array,
+                                  float scalar, float *noise, OType *out) {
+    Shape<ndim> coord = unravel(i, oshape);
+    auto idx = static_cast<index_t>(dot(coord, stride));
+    IType loc_value;
+    IType scale_value;
+    if (scalar_pos == 0) {
+      loc_value = scalar;
+      scale_value = array[idx];
+    } else {
+      loc_value = array[idx];
+      scale_value = scalar;
+    }
+    noise[i] = log(noise[i]) - log(1 - noise[i]);
+    out[i] = loc_value + noise[i] * scale_value;
+  }
+};
+
+struct gumbel_one_scalar_kernel {
+  template <int ndim, typename IType, typename OType>
+  MSHADOW_XINLINE static void Map(index_t i, int scalar_pos,
+                                  const Shape<ndim> &stride,
+                                  const Shape<ndim> &oshape, IType *array,
+                                  float scalar, float *noise, OType *out) {
+    Shape<ndim> coord = unravel(i, oshape);
+    auto idx = static_cast<index_t>(dot(coord, stride));
+    IType loc_value;
+    IType scale_value;
+    if (scalar_pos == 0) {
+      loc_value = scalar;
+      scale_value = array[idx];
+    } else {
+      loc_value = array[idx];
+      scale_value = scalar;
+    }
+    noise[i] = -log(-log(noise[i]));
+    out[i] = loc_value + noise[i] * scale_value;
+  }
+};
+
+struct logistic_kernel {
+  template <int ndim, typename IType, typename OType>
+  MSHADOW_XINLINE static void Map(index_t i, const Shape<ndim> &lstride,
+                                  const Shape<ndim> &hstride,
+                                  const Shape<ndim> &oshape, IType *loc,
+                                  IType *scale, float *noise, OType *out) {
+    Shape<ndim> coord = unravel(i, oshape);
+    auto lidx = static_cast<index_t>(dot(coord, lstride));
+    auto hidx = static_cast<index_t>(dot(coord, hstride));
+    IType loc_value = loc[lidx];
+    IType scale_value = scale[hidx];
+    noise[i] = log(noise[i]) - log(1 - noise[i]);
+    out[i] = loc_value + noise[i] * scale_value;
+  }
+};
+
+struct gumbel_kernel {
+  template <int ndim, typename IType, typename OType>
+  MSHADOW_XINLINE static void Map(index_t i, const Shape<ndim> &lstride,
+                                  const Shape<ndim> &hstride,
+                                  const Shape<ndim> &oshape, IType *loc,
+                                  IType *scale, float *noise, OType *out) {
+    Shape<ndim> coord = unravel(i, oshape);
+    auto lidx = static_cast<index_t>(dot(coord, lstride));
+    auto hidx = static_cast<index_t>(dot(coord, hstride));
+    IType loc_value = loc[lidx];
+    IType scale_value = scale[hidx];
+    noise[i] = -log(-log(noise[i]));
+    out[i] = loc_value + noise[i] * scale_value;
+  }
+};
+
+}  // namespace mxnet_op
+
+template <typename xpu, typename two_scalar_kernel, typename scalar_tensor_kernel,
+          typename two_tensor_kernel>
+void NumpyLocationScaleForward(const nnvm::NodeAttrs &attrs,
+                               const OpContext &ctx,
+                               const std::vector<TBlob> &inputs,
+                               const std::vector<OpReqType> &req,
+                               const std::vector<TBlob> &outputs) {
+  using namespace mshadow;
+  using namespace mxnet_op;
+  const NumpyLocationScaleParam &param = nnvm::get<NumpyLocationScaleParam>(attrs.parsed);
+  Stream<xpu> *s = ctx.get_stream<xpu>();
+  // Generate base random number.
+  Random<xpu, float> *prnd = ctx.requested[0].get_random<xpu, float>(s);
+  Tensor<xpu, 1, float> workspace =
+    ctx.requested[1].get_space_typed<xpu, 1, float>(Shape1(1), s);
+  Tensor<xpu, 1, float> location_tensor = outputs[1].FlatTo1D<xpu, float>(s);
+  Tensor<xpu, 1, float> indicator_device = workspace;
+  float indicator_host = 1.0;
+  float *indicator_device_ptr = indicator_device.dptr_;
+  Kernel<set_zero, xpu>::Launch(s, 1, indicator_device_ptr);
+  prnd->SampleUniform(&location_tensor, 0.0, 1.0);
+  mxnet::TShape new_lshape, new_hshape, new_oshape;
+  // [scalar scalar] case
+  if (inputs.size() == 0U) {
+    CHECK_GE(param.scale.value(), 0.0) << "ValueError: scale < 0";
+    MSHADOW_TYPE_SWITCH(outputs[0].type_flag_, DType, {
+      Kernel<two_scalar_kernel, xpu>::Launch(
+        s, outputs[0].Size(), param.loc.value(), param.scale.value(),
+        location_tensor.dptr_, outputs[0].dptr<DType>());
+    });
+  } else if (inputs.size() == 1U) {
+    // [scalar tensor], [tensor scalar] case
+    int ndim = FillShape(inputs[0].shape_, inputs[0].shape_, outputs[0].shape_,
+                         &new_lshape, &new_lshape, &new_oshape);
+    int scalar_pos;
+    float scalar_value;
+    if (param.loc.has_value()) {
+      scalar_pos = 0;
+      scalar_value = param.loc.value();
+      MSHADOW_TYPE_SWITCH(inputs[0].type_flag_, IType, {
+        Kernel<check_legal_scale_kernel<IType>, xpu>::Launch(
+          s, inputs[0].Size(), inputs[0].dptr<IType>(), indicator_device_ptr);
+      });
+      _copy<xpu>(s, &indicator_host, indicator_device_ptr);
+      CHECK_GE(indicator_host, 0.0) << "ValueError: scale < 0";
+    } else {
+      scalar_pos = 1;
+      scalar_value = param.scale.value();
+      CHECK_GE(scalar_value, 0.0) << "ValueError: scale < 0";
+    }
+    MSHADOW_TYPE_SWITCH(inputs[0].type_flag_, IType, {
+      MSHADOW_TYPE_SWITCH(outputs[0].type_flag_, OType, {
+        BROADCAST_NDIM_SWITCH(ndim, NDim, {
+          Shape<NDim> oshape = new_oshape.get<NDim>();
+          Shape<NDim> stride = calc_stride(new_lshape.get<NDim>());
+          Kernel<scalar_tensor_kernel, xpu>::Launch(
+            s, outputs[0].Size(), scalar_pos, stride, oshape,
+            inputs[0].dptr<IType>(), scalar_value, location_tensor.dptr_,
+            outputs[0].dptr<OType>());
+        });
+      });
+    });
+  } else if (inputs.size() == 2U) {
+    // [tensor tensor] case
+    MSHADOW_TYPE_SWITCH(inputs[0].type_flag_, IType, {
+      Kernel<check_legal_scale_kernel<IType>, xpu>::Launch(
+        s, inputs[1].Size(), inputs[1].dptr<IType>(), indicator_device_ptr);
+    });
+    _copy<xpu>(s, &indicator_host, indicator_device_ptr);
+    CHECK_GE(indicator_host, 0.0) << "ValueError: scale < 0";
+    int ndim = FillShape(inputs[0].shape_, inputs[1].shape_, outputs[0].shape_,
+                         &new_lshape, &new_hshape, &new_oshape);
+    MSHADOW_TYPE_SWITCH(inputs[0].type_flag_, IType, {
+      MSHADOW_TYPE_SWITCH(outputs[0].type_flag_, OType, {
+        BROADCAST_NDIM_SWITCH(ndim, NDim, {
+          Shape<NDim> oshape = new_oshape.get<NDim>();
+          Shape<NDim> lstride = calc_stride(new_lshape.get<NDim>());
+          Shape<NDim> hstride = calc_stride(new_hshape.get<NDim>());
+          Kernel<two_tensor_kernel, xpu>::Launch(
+            s, outputs[0].Size(), lstride, hstride, oshape,
+            inputs[0].dptr<IType>(), inputs[1].dptr<IType>(),
+            location_tensor.dptr_, outputs[0].dptr<OType>());
+        });
+      });
+    });
+  }
+}
+
+template<typename xpu, int ndim, typename DType>
+inline void LocationScaleReparamBackwardImpl(const OpContext& ctx,
+                                             const std::vector<TBlob>& inputs,
+                                             const std::vector<OpReqType>& req,
+                                             const std::vector<TBlob>& outputs,
+                                             const mxnet::TShape& new_lshape,
+                                             const mxnet::TShape& new_rshape,
+                                             const mxnet::TShape& new_oshape) {
+  using namespace mshadow;
+  using namespace mshadow::expr;
+  using namespace broadcast;
+  Stream<xpu> *s = ctx.get_stream<xpu>();
+  const TBlob lgrad = outputs[0].reshape(new_lshape);
+  const TBlob rgrad = outputs[1].reshape(new_rshape);
+  const TBlob ograd = inputs[0].reshape(new_oshape);
+  // Mean
+  const TBlob lhs = inputs[2].reshape(new_lshape);
+  // Variance
+  const TBlob rhs = inputs[3].reshape(new_rshape);
+  const TBlob samples = inputs[4].reshape(new_oshape);
+  const TBlob noise = inputs[5].reshape(new_oshape);
+  size_t workspace_size_l = ReduceWorkspaceSize<ndim, DType>(
+    s, lgrad.shape_, req[0], ograd.shape_, lhs.shape_, rhs.shape_);
+  size_t workspace_size_r = ReduceWorkspaceSize<ndim, DType>(
+    s, rgrad.shape_, req[1], ograd.shape_, lhs.shape_, rhs.shape_);
+  size_t workspace_size = std::max(workspace_size_l, workspace_size_r);
+  Tensor<xpu, 1, char> workspace =
+    ctx.requested[0].get_space_typed<xpu, 1, char>(Shape1(workspace_size), s);
+  Reduce<red::sum, ndim, DType, op::mshadow_op::identity>(
+    s, lgrad, req[0], workspace, ograd);
+  Reduce<red::sum, ndim, DType, op::mshadow_op::mul, op::mshadow_op::left>(
+    s, rgrad, req[1], workspace, ograd, noise, rhs);
+}
+
+template<typename xpu, int ndim, typename DType>
+inline void ScalarLocationScaleReparamBackwardImpl(const OpContext& ctx,
+                                                   const std::vector<TBlob>& inputs,
+                                                   const std::vector<OpReqType>& req,
+                                                   const std::vector<TBlob>& outputs,
+                                                   const mxnet::TShape& new_ishape,
+                                                   const mxnet::TShape& new_oshape,
+                                                   const bool loc_is_tensor) {
+  using namespace mshadow;
+  using namespace mshadow::expr;
+  using namespace broadcast;
+  Stream<xpu> *s = ctx.get_stream<xpu>();
+  const TBlob igrad = outputs[0].reshape(new_ishape);
+  // inputs: [grad_from_samples, grad_from_noise(invisible), input_tensor,
+  //          samples, noise]
+  const TBlob ograd = inputs[0].reshape(new_oshape);
+  const TBlob itensor = inputs[2].reshape(new_ishape);
+  const TBlob samples = inputs[3].reshape(new_oshape);
+  const TBlob noise = inputs[4].reshape(new_oshape);
+  size_t workspace_size =
+    ReduceWorkspaceSize<ndim, DType>(s, igrad.shape_, req[0], ograd.shape_);
+  Tensor<xpu, 1, char> workspace =
+    ctx.requested[0].get_space_typed<xpu, 1, char>(Shape1(workspace_size), s);
+  if (loc_is_tensor) {
+    Reduce<red::sum, ndim, DType, op::mshadow_op::identity>(s, igrad, req[0],
+                                                            workspace, ograd);
+  } else {
+    Reduce<red::sum, ndim, DType, op::mshadow_op::mul, op::mshadow_op::left>(
+      s, igrad, req[0], workspace, ograd, noise, noise);
+  }
+}
+
+// Allow logistic sampling to be differentiable,
 
 Review comment:
   // Allow logistic and gumbel

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] xidulu commented on a change in pull request #17302: [numpy]add op random.logistic, random.gumbel

Posted by GitBox <gi...@apache.org>.
xidulu commented on a change in pull request #17302: [numpy]add op random.logistic, random.gumbel
URL: https://github.com/apache/incubator-mxnet/pull/17302#discussion_r369385391
 
 

 ##########
 File path: src/operator/numpy/random/np_location_scale_op.h
 ##########
 @@ -0,0 +1,449 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * Copyright (c) 2019 by Contributors
+ * \file np_location_scale_op.h
+ * \brief Operator for numpy sampling from localtion scale distributions
+ */
+#ifndef MXNET_OPERATOR_NUMPY_RANDOM_NP_LOCATION_SCALE_OP_H_
+#define MXNET_OPERATOR_NUMPY_RANDOM_NP_LOCATION_SCALE_OP_H_
+
+#include <mxnet/operator_util.h>
+#include <cstdio>
+#include <algorithm>
+#include <string>
+#include <vector>
+#include <cmath>
+#include "../../elemwise_op_common.h"
+#include "../../mshadow_op.h"
+#include "../../mxnet_op.h"
+#include "../../operator_common.h"
+#include "../../tensor/elemwise_binary_broadcast_op.h"
+#include "./dist_common.h"
+
+namespace mxnet {
+namespace op {
+
+struct NumpyLocationScaleParam : public dmlc::Parameter<NumpyLocationScaleParam> {
+  dmlc::optional<float> loc;
+  dmlc::optional<float> scale;
+  dmlc::optional<mxnet::Tuple<int>> size;
+  std::string ctx;
+  DMLC_DECLARE_PARAMETER(NumpyLocationScaleParam) {
+    DMLC_DECLARE_FIELD(loc);
+    DMLC_DECLARE_FIELD(scale);
+    DMLC_DECLARE_FIELD(size)
+      .set_default(dmlc::optional<mxnet::Tuple<int>>())
+      .describe(
+          "Output shape. If the given shape is, "
+          "e.g., (m, n, k), then m * n * k samples are drawn. "
+          "Default is None, in which case a single value is returned.");
+    DMLC_DECLARE_FIELD(ctx).set_default("cpu").describe(
+        "Context of output, in format [cpu|gpu|cpu_pinned](n)."
+        " Only used for imperative calls.");
+  }
+};
+
+inline bool NumpyLocationScaleOpType(const nnvm::NodeAttrs &attrs,
+                                     std::vector<int> *in_attrs,
+                                     std::vector<int> *out_attrs) {
+  (*out_attrs)[0] = mshadow::kFloat32;
+  (*out_attrs)[1] = mshadow::kFloat32;
+  return true;
+}
+
+namespace mxnet_op {
+
+struct logistic_two_scalar_kernel {
+  template <typename DType>
+  MSHADOW_XINLINE static void Map(index_t i, float loc, float scale,
+                                  float *noise, DType *out) {
+    noise[i] = log(noise[i]) - log(1 - noise[i]);
+    out[i] = loc + noise[i] * scale;
+  }
+};
+
+struct gumbel_two_scalar_kernel {
+  template <typename DType>
+  MSHADOW_XINLINE static void Map(index_t i, float loc, float scale,
+                                  float *noise, DType *out) {
+    noise[i] = -log(-log(noise[i]));
+    out[i] = loc + noise[i] * scale;
+  }
+};
+
+
+template <typename IType>
+struct check_legal_scale_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, IType *scalar, float* flag) {
+    if (scalar[i] < 0) {
+      *flag = -1.0;
+    }
+  }
+};
+
+struct logistic_one_scalar_kernel {
+  template <int ndim, typename IType, typename OType>
+  MSHADOW_XINLINE static void Map(index_t i, int scalar_pos,
+                                  const Shape<ndim> &stride,
+                                  const Shape<ndim> &oshape, IType *array,
+                                  float scalar, float *noise, OType *out) {
+    Shape<ndim> coord = unravel(i, oshape);
+    auto idx = static_cast<index_t>(dot(coord, stride));
+    IType loc_value;
+    IType scale_value;
+    if (scalar_pos == 0) {
+      loc_value = scalar;
+      scale_value = array[idx];
+    } else {
+      loc_value = array[idx];
+      scale_value = scalar;
+    }
+    noise[i] = log(noise[i]) - log(1 - noise[i]);
+    out[i] = loc_value + noise[i] * scale_value;
+  }
+};
+
+struct gumbel_one_scalar_kernel {
+  template <int ndim, typename IType, typename OType>
+  MSHADOW_XINLINE static void Map(index_t i, int scalar_pos,
+                                  const Shape<ndim> &stride,
+                                  const Shape<ndim> &oshape, IType *array,
+                                  float scalar, float *noise, OType *out) {
+    Shape<ndim> coord = unravel(i, oshape);
+    auto idx = static_cast<index_t>(dot(coord, stride));
+    IType loc_value;
+    IType scale_value;
+    if (scalar_pos == 0) {
+      loc_value = scalar;
+      scale_value = array[idx];
+    } else {
+      loc_value = array[idx];
+      scale_value = scalar;
+    }
+    noise[i] = -log(-log(noise[i]));
+    out[i] = loc_value + noise[i] * scale_value;
+  }
+};
+
+struct logistic_kernel {
+  template <int ndim, typename IType, typename OType>
+  MSHADOW_XINLINE static void Map(index_t i, const Shape<ndim> &lstride,
+                                  const Shape<ndim> &hstride,
+                                  const Shape<ndim> &oshape, IType *loc,
+                                  IType *scale, float *noise, OType *out) {
+    Shape<ndim> coord = unravel(i, oshape);
+    auto lidx = static_cast<index_t>(dot(coord, lstride));
+    auto hidx = static_cast<index_t>(dot(coord, hstride));
+    IType loc_value = loc[lidx];
+    IType scale_value = scale[hidx];
+    noise[i] = log(noise[i]) - log(1 - noise[i]);
+    out[i] = loc_value + noise[i] * scale_value;
+  }
+};
+
+struct gumbel_kernel {
+  template <int ndim, typename IType, typename OType>
+  MSHADOW_XINLINE static void Map(index_t i, const Shape<ndim> &lstride,
+                                  const Shape<ndim> &hstride,
+                                  const Shape<ndim> &oshape, IType *loc,
+                                  IType *scale, float *noise, OType *out) {
+    Shape<ndim> coord = unravel(i, oshape);
+    auto lidx = static_cast<index_t>(dot(coord, lstride));
+    auto hidx = static_cast<index_t>(dot(coord, hstride));
+    IType loc_value = loc[lidx];
+    IType scale_value = scale[hidx];
+    noise[i] = -log(-log(noise[i]));
+    out[i] = loc_value + noise[i] * scale_value;
+  }
+};
+
+}  // namespace mxnet_op
+
+template <typename xpu, typename two_scalar_kernel, typename scalar_tensor_kernel,
+          typename two_tensor_kernel>
+void NumpyLocationScaleForward(const nnvm::NodeAttrs &attrs,
+                               const OpContext &ctx,
+                               const std::vector<TBlob> &inputs,
+                               const std::vector<OpReqType> &req,
+                               const std::vector<TBlob> &outputs) {
+  using namespace mshadow;
+  using namespace mxnet_op;
+  const NumpyLocationScaleParam &param = nnvm::get<NumpyLocationScaleParam>(attrs.parsed);
+  Stream<xpu> *s = ctx.get_stream<xpu>();
+  // Generate base random number.
+  Random<xpu, float> *prnd = ctx.requested[0].get_random<xpu, float>(s);
+  Tensor<xpu, 1, float> workspace =
+    ctx.requested[1].get_space_typed<xpu, 1, float>(Shape1(1), s);
+  Tensor<xpu, 1, float> location_tensor = outputs[1].FlatTo1D<xpu, float>(s);
 
 Review comment:
   Could you change the name to `uniform_tensor` or `noise_tensor`? The name `location_tensor` looks confusing to me, cuz I believe the location is stored in inputs[0].

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] xidulu commented on a change in pull request #17302: [numpy]add op random.logistic

Posted by GitBox <gi...@apache.org>.
xidulu commented on a change in pull request #17302: [numpy]add op random.logistic
URL: https://github.com/apache/incubator-mxnet/pull/17302#discussion_r366404838
 
 

 ##########
 File path: src/operator/numpy/random/np_logistic_op.h
 ##########
 @@ -0,0 +1,331 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * Copyright (c) 2019 by Contributors
+ * \file np_logistic_op.h
+ * \brief Operator for numpy sampling from logistic distributions
+ */
+#ifndef MXNET_OPERATOR_NUMPY_RANDOM_NP_LOGISTIC_OP_H_
+#define MXNET_OPERATOR_NUMPY_RANDOM_NP_LOGISTIC_OP_H_
+
+#include <mxnet/operator_util.h>
+#include <cstdio>
+#include <algorithm>
+#include <string>
+#include <vector>
+#include <cmath>
+#include "../../elemwise_op_common.h"
+#include "../../mshadow_op.h"
+#include "../../mxnet_op.h"
+#include "../../operator_common.h"
+#include "../../tensor/elemwise_binary_broadcast_op.h"
+#include "./dist_common.h"
+
+namespace mxnet {
+namespace op {
+
+struct NumpyLogisticParam : public dmlc::Parameter<NumpyLogisticParam> {
+  dmlc::optional<float> loc;
+  dmlc::optional<float> scale;
+  dmlc::optional<mxnet::Tuple<int>> size;
+  DMLC_DECLARE_PARAMETER(NumpyLogisticParam) {
+    DMLC_DECLARE_FIELD(loc);
+    DMLC_DECLARE_FIELD(scale);
+    DMLC_DECLARE_FIELD(size)
+        .set_default(dmlc::optional<mxnet::Tuple<int>>())
+        .describe(
+            "Output shape. If the given shape is, "
+            "e.g., (m, n, k), then m * n * k samples are drawn. "
+            "Default is None, in which case a single value is returned.");
+  }
+};
+
+inline bool NumpyLogisticOpType(const nnvm::NodeAttrs &attrs,
+                               std::vector<int> *in_attrs,
+                               std::vector<int> *out_attrs) {
+  const NumpyLogisticParam &param = nnvm::get<NumpyLogisticParam>(attrs.parsed);
+  (*out_attrs)[0] = mshadow::kFloat32;
+  (*out_attrs)[1] = mshadow::kFloat32;
+  return true;
+}
+
+namespace mxnet_op {
+
+template <typename DType>
+struct logistic_two_scalar_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, float loc, float scale,
+                                  float *noise, DType *out) {
+    noise[i] = log(noise[i]) - log(1 - noise[i]);
+    out[i] = loc + noise[i] * scale;
+  }
+};
+
+template <typename IType>
+struct check_legal_scale_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, IType *scalar, float* flag) {
+    if (scalar[i] < 0) {
+      *flag = -1.0;
+    }
+  }
+};
+
+template <int ndim, typename IType, typename OType>
+struct logistic_one_scalar_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, int scalar_pos,
+                                  const Shape<ndim> &stride,
+                                  const Shape<ndim> &oshape, IType *array,
+                                  float scalar, float *noise, OType *out) {
+    Shape<ndim> coord = unravel(i, oshape);
+    auto idx = static_cast<index_t>(dot(coord, stride));
+    IType loc_value;
+    IType scale_value;
+    if (scalar_pos == 0) {
+      loc_value = scalar;
+      scale_value = array[idx];
+    } else {
+      loc_value = array[idx];
+      scale_value = scalar;
+    }
+    noise[i] = log(noise[i]) - log(1 - noise[i]);
+    out[i] = loc_value + noise[i] * scale_value;
+  }
+};
+
+template <int ndim, typename IType, typename OType>
+struct logical_kernel {
 
 Review comment:
   Better be `logistic_kernel {...`

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] xidulu commented on a change in pull request #17302: [numpy]add op random.logistic, random.gumbel

Posted by GitBox <gi...@apache.org>.
xidulu commented on a change in pull request #17302: [numpy]add op random.logistic, random.gumbel
URL: https://github.com/apache/incubator-mxnet/pull/17302#discussion_r369385793
 
 

 ##########
 File path: src/operator/numpy/random/np_location_scale_op.h
 ##########
 @@ -0,0 +1,449 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * Copyright (c) 2019 by Contributors
+ * \file np_location_scale_op.h
+ * \brief Operator for numpy sampling from localtion scale distributions
+ */
+#ifndef MXNET_OPERATOR_NUMPY_RANDOM_NP_LOCATION_SCALE_OP_H_
+#define MXNET_OPERATOR_NUMPY_RANDOM_NP_LOCATION_SCALE_OP_H_
+
+#include <mxnet/operator_util.h>
+#include <cstdio>
+#include <algorithm>
+#include <string>
+#include <vector>
+#include <cmath>
+#include "../../elemwise_op_common.h"
+#include "../../mshadow_op.h"
+#include "../../mxnet_op.h"
+#include "../../operator_common.h"
+#include "../../tensor/elemwise_binary_broadcast_op.h"
+#include "./dist_common.h"
+
+namespace mxnet {
+namespace op {
+
+struct NumpyLocationScaleParam : public dmlc::Parameter<NumpyLocationScaleParam> {
+  dmlc::optional<float> loc;
+  dmlc::optional<float> scale;
+  dmlc::optional<mxnet::Tuple<int>> size;
+  std::string ctx;
+  DMLC_DECLARE_PARAMETER(NumpyLocationScaleParam) {
+    DMLC_DECLARE_FIELD(loc);
+    DMLC_DECLARE_FIELD(scale);
+    DMLC_DECLARE_FIELD(size)
+      .set_default(dmlc::optional<mxnet::Tuple<int>>())
+      .describe(
+          "Output shape. If the given shape is, "
+          "e.g., (m, n, k), then m * n * k samples are drawn. "
+          "Default is None, in which case a single value is returned.");
+    DMLC_DECLARE_FIELD(ctx).set_default("cpu").describe(
+        "Context of output, in format [cpu|gpu|cpu_pinned](n)."
+        " Only used for imperative calls.");
+  }
+};
+
+inline bool NumpyLocationScaleOpType(const nnvm::NodeAttrs &attrs,
+                                     std::vector<int> *in_attrs,
+                                     std::vector<int> *out_attrs) {
+  (*out_attrs)[0] = mshadow::kFloat32;
+  (*out_attrs)[1] = mshadow::kFloat32;
+  return true;
+}
+
+namespace mxnet_op {
+
+struct logistic_two_scalar_kernel {
+  template <typename DType>
+  MSHADOW_XINLINE static void Map(index_t i, float loc, float scale,
+                                  float *noise, DType *out) {
+    noise[i] = log(noise[i]) - log(1 - noise[i]);
+    out[i] = loc + noise[i] * scale;
+  }
+};
+
+struct gumbel_two_scalar_kernel {
+  template <typename DType>
+  MSHADOW_XINLINE static void Map(index_t i, float loc, float scale,
+                                  float *noise, DType *out) {
+    noise[i] = -log(-log(noise[i]));
+    out[i] = loc + noise[i] * scale;
+  }
+};
+
+
+template <typename IType>
+struct check_legal_scale_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, IType *scalar, float* flag) {
+    if (scalar[i] < 0) {
+      *flag = -1.0;
+    }
+  }
+};
+
+struct logistic_one_scalar_kernel {
+  template <int ndim, typename IType, typename OType>
+  MSHADOW_XINLINE static void Map(index_t i, int scalar_pos,
+                                  const Shape<ndim> &stride,
+                                  const Shape<ndim> &oshape, IType *array,
+                                  float scalar, float *noise, OType *out) {
+    Shape<ndim> coord = unravel(i, oshape);
+    auto idx = static_cast<index_t>(dot(coord, stride));
+    IType loc_value;
+    IType scale_value;
+    if (scalar_pos == 0) {
+      loc_value = scalar;
+      scale_value = array[idx];
+    } else {
+      loc_value = array[idx];
+      scale_value = scalar;
+    }
+    noise[i] = log(noise[i]) - log(1 - noise[i]);
+    out[i] = loc_value + noise[i] * scale_value;
+  }
+};
+
+struct gumbel_one_scalar_kernel {
+  template <int ndim, typename IType, typename OType>
+  MSHADOW_XINLINE static void Map(index_t i, int scalar_pos,
+                                  const Shape<ndim> &stride,
+                                  const Shape<ndim> &oshape, IType *array,
+                                  float scalar, float *noise, OType *out) {
+    Shape<ndim> coord = unravel(i, oshape);
+    auto idx = static_cast<index_t>(dot(coord, stride));
+    IType loc_value;
+    IType scale_value;
+    if (scalar_pos == 0) {
+      loc_value = scalar;
+      scale_value = array[idx];
+    } else {
+      loc_value = array[idx];
+      scale_value = scalar;
+    }
+    noise[i] = -log(-log(noise[i]));
+    out[i] = loc_value + noise[i] * scale_value;
+  }
+};
+
+struct logistic_kernel {
+  template <int ndim, typename IType, typename OType>
+  MSHADOW_XINLINE static void Map(index_t i, const Shape<ndim> &lstride,
+                                  const Shape<ndim> &hstride,
+                                  const Shape<ndim> &oshape, IType *loc,
+                                  IType *scale, float *noise, OType *out) {
+    Shape<ndim> coord = unravel(i, oshape);
+    auto lidx = static_cast<index_t>(dot(coord, lstride));
+    auto hidx = static_cast<index_t>(dot(coord, hstride));
+    IType loc_value = loc[lidx];
+    IType scale_value = scale[hidx];
+    noise[i] = log(noise[i]) - log(1 - noise[i]);
+    out[i] = loc_value + noise[i] * scale_value;
+  }
+};
+
+struct gumbel_kernel {
+  template <int ndim, typename IType, typename OType>
+  MSHADOW_XINLINE static void Map(index_t i, const Shape<ndim> &lstride,
+                                  const Shape<ndim> &hstride,
+                                  const Shape<ndim> &oshape, IType *loc,
+                                  IType *scale, float *noise, OType *out) {
+    Shape<ndim> coord = unravel(i, oshape);
+    auto lidx = static_cast<index_t>(dot(coord, lstride));
+    auto hidx = static_cast<index_t>(dot(coord, hstride));
+    IType loc_value = loc[lidx];
+    IType scale_value = scale[hidx];
+    noise[i] = -log(-log(noise[i]));
+    out[i] = loc_value + noise[i] * scale_value;
+  }
+};
+
+}  // namespace mxnet_op
+
+template <typename xpu, typename two_scalar_kernel, typename scalar_tensor_kernel,
+          typename two_tensor_kernel>
+void NumpyLocationScaleForward(const nnvm::NodeAttrs &attrs,
+                               const OpContext &ctx,
+                               const std::vector<TBlob> &inputs,
+                               const std::vector<OpReqType> &req,
+                               const std::vector<TBlob> &outputs) {
+  using namespace mshadow;
+  using namespace mxnet_op;
+  const NumpyLocationScaleParam &param = nnvm::get<NumpyLocationScaleParam>(attrs.parsed);
+  Stream<xpu> *s = ctx.get_stream<xpu>();
+  // Generate base random number.
+  Random<xpu, float> *prnd = ctx.requested[0].get_random<xpu, float>(s);
+  Tensor<xpu, 1, float> workspace =
+    ctx.requested[1].get_space_typed<xpu, 1, float>(Shape1(1), s);
+  Tensor<xpu, 1, float> location_tensor = outputs[1].FlatTo1D<xpu, float>(s);
+  Tensor<xpu, 1, float> indicator_device = workspace;
+  float indicator_host = 1.0;
+  float *indicator_device_ptr = indicator_device.dptr_;
+  Kernel<set_zero, xpu>::Launch(s, 1, indicator_device_ptr);
+  prnd->SampleUniform(&location_tensor, 0.0, 1.0);
+  mxnet::TShape new_lshape, new_hshape, new_oshape;
+  // [scalar scalar] case
+  if (inputs.size() == 0U) {
+    CHECK_GE(param.scale.value(), 0.0) << "ValueError: scale < 0";
+    MSHADOW_TYPE_SWITCH(outputs[0].type_flag_, DType, {
+      Kernel<two_scalar_kernel, xpu>::Launch(
+        s, outputs[0].Size(), param.loc.value(), param.scale.value(),
+        location_tensor.dptr_, outputs[0].dptr<DType>());
+    });
+  } else if (inputs.size() == 1U) {
+    // [scalar tensor], [tensor scalar] case
+    int ndim = FillShape(inputs[0].shape_, inputs[0].shape_, outputs[0].shape_,
+                         &new_lshape, &new_lshape, &new_oshape);
+    int scalar_pos;
+    float scalar_value;
+    if (param.loc.has_value()) {
+      scalar_pos = 0;
+      scalar_value = param.loc.value();
+      MSHADOW_TYPE_SWITCH(inputs[0].type_flag_, IType, {
+        Kernel<check_legal_scale_kernel<IType>, xpu>::Launch(
+          s, inputs[0].Size(), inputs[0].dptr<IType>(), indicator_device_ptr);
+      });
+      _copy<xpu>(s, &indicator_host, indicator_device_ptr);
+      CHECK_GE(indicator_host, 0.0) << "ValueError: scale < 0";
+    } else {
+      scalar_pos = 1;
+      scalar_value = param.scale.value();
+      CHECK_GE(scalar_value, 0.0) << "ValueError: scale < 0";
+    }
+    MSHADOW_TYPE_SWITCH(inputs[0].type_flag_, IType, {
+      MSHADOW_TYPE_SWITCH(outputs[0].type_flag_, OType, {
+        BROADCAST_NDIM_SWITCH(ndim, NDim, {
+          Shape<NDim> oshape = new_oshape.get<NDim>();
+          Shape<NDim> stride = calc_stride(new_lshape.get<NDim>());
+          Kernel<scalar_tensor_kernel, xpu>::Launch(
+            s, outputs[0].Size(), scalar_pos, stride, oshape,
+            inputs[0].dptr<IType>(), scalar_value, location_tensor.dptr_,
+            outputs[0].dptr<OType>());
+        });
+      });
+    });
+  } else if (inputs.size() == 2U) {
+    // [tensor tensor] case
+    MSHADOW_TYPE_SWITCH(inputs[0].type_flag_, IType, {
+      Kernel<check_legal_scale_kernel<IType>, xpu>::Launch(
+        s, inputs[1].Size(), inputs[1].dptr<IType>(), indicator_device_ptr);
+    });
+    _copy<xpu>(s, &indicator_host, indicator_device_ptr);
+    CHECK_GE(indicator_host, 0.0) << "ValueError: scale < 0";
+    int ndim = FillShape(inputs[0].shape_, inputs[1].shape_, outputs[0].shape_,
+                         &new_lshape, &new_hshape, &new_oshape);
+    MSHADOW_TYPE_SWITCH(inputs[0].type_flag_, IType, {
+      MSHADOW_TYPE_SWITCH(outputs[0].type_flag_, OType, {
+        BROADCAST_NDIM_SWITCH(ndim, NDim, {
+          Shape<NDim> oshape = new_oshape.get<NDim>();
+          Shape<NDim> lstride = calc_stride(new_lshape.get<NDim>());
+          Shape<NDim> hstride = calc_stride(new_hshape.get<NDim>());
+          Kernel<two_tensor_kernel, xpu>::Launch(
+            s, outputs[0].Size(), lstride, hstride, oshape,
+            inputs[0].dptr<IType>(), inputs[1].dptr<IType>(),
+            location_tensor.dptr_, outputs[0].dptr<OType>());
+        });
+      });
+    });
+  }
+}
+
+template<typename xpu, int ndim, typename DType>
+inline void LocationScaleReparamBackwardImpl(const OpContext& ctx,
+                                             const std::vector<TBlob>& inputs,
+                                             const std::vector<OpReqType>& req,
+                                             const std::vector<TBlob>& outputs,
+                                             const mxnet::TShape& new_lshape,
+                                             const mxnet::TShape& new_rshape,
+                                             const mxnet::TShape& new_oshape) {
+  using namespace mshadow;
+  using namespace mshadow::expr;
+  using namespace broadcast;
+  Stream<xpu> *s = ctx.get_stream<xpu>();
+  const TBlob lgrad = outputs[0].reshape(new_lshape);
+  const TBlob rgrad = outputs[1].reshape(new_rshape);
+  const TBlob ograd = inputs[0].reshape(new_oshape);
+  // Mean
+  const TBlob lhs = inputs[2].reshape(new_lshape);
+  // Variance
 
 Review comment:
   Better change `// Variance` to `// Scale`, as the `scale` of these two distributions are not equal to their var.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services