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/20 08:37:07 UTC

[GitHub] [incubator-mxnet] AntiZpvoh opened a new pull request #17385: [NumPy] add random.geometric op

AntiZpvoh opened a new pull request #17385: [NumPy] add random.geometric op
URL: https://github.com/apache/incubator-mxnet/pull/17385
 
 
   ## Description ##
   add numpy.random.geometric op and write the unit test
   
   ## 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] hzfan commented on issue #17385: [NumPy] add random.geometric op

Posted by GitBox <gi...@apache.org>.
hzfan commented on issue #17385: [NumPy] add random.geometric op
URL: https://github.com/apache/incubator-mxnet/pull/17385#issuecomment-605766245
 
 
   FFI LGTM. Please resolve the comments by @haojin2 and @xidulu 

----------------------------------------------------------------
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] hzfan commented on a change in pull request #17385: [NumPy] add random.geometric op

Posted by GitBox <gi...@apache.org>.
hzfan commented on a change in pull request #17385: [NumPy] add random.geometric op
URL: https://github.com/apache/incubator-mxnet/pull/17385#discussion_r393584811
 
 

 ##########
 File path: src/api/operator/numpy/random/np_geometric_op.cc
 ##########
 @@ -0,0 +1,89 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ * 
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file np_geometric_op.cc
+ * \brief Implementation of the API of functions in src/operator/numpy/np_geometric_op.cc
+ */
+#include <mxnet/api_registry.h>
+#include <mxnet/runtime/packed_func.h>
+#include "../../utils.h"
+#include "../../../../operator/numpy/random/np_geometric_op.h"
+
+namespace mxnet {
+
+MXNET_REGISTER_API("_npi.geometric")
+.set_body([](runtime::MXNetArgs args, runtime::MXNetRetValue* ret) {
+  using namespace runtime;
+  const nnvm::Op* op = Op::Get("_npi_geometric");
+  nnvm::NodeAttrs attrs;
+  op::NumpyGeometricParam param;
+
+  NDArray* in = nullptr;
+  int num_inputs = 0;
+
+  if (args[0].type_code() == kNull) {
+    param.prob = dmlc::nullopt;
+  } else if (args[0].type_code() == kNDArrayHandle) {
+    param.prob = dmlc::nullopt;
+    in = args[0].operator mxnet::NDArray *();
+    num_inputs++;
+  } else {
+    param.prob = args[0].operator double();  // convert arg to T
+  }
+
+  if (args[1].type_code() == kNull) {
+    param.size = dmlc::nullopt;
+  } else {
+    if (args[1].type_code() == kDLInt) {
+      param.size = mxnet::Tuple<int>(1, args[1].operator int64_t());
+    } else {
+      param.size = mxnet::Tuple<int>(args[1].operator ObjectRef());
+    }
+  }
+
+  if (args[2].type_code() == kNull) {
+    param.dtype = mshadow::kFloat32;
+  } else {
+    param.dtype = String2MXNetTypeWithBool(args[2].operator std::string());
+  }
+  attrs.parsed = std::move(param);
+  attrs.op = op;
+  SetAttrDict<op::InitOpParam>(&attrs);
+  if (args[3].type_code() != kNull) {
+    attrs.dict["ctx"] = args[3].operator std::string();
+  }
+
+  NDArray** inputs = in == nullptr ? nullptr : &in;
+
+  NDArray* out = args[4].operator mxnet::NDArray*();
+  NDArray** outputs = out == nullptr ? nullptr : &out;
+  // set the number of outputs provided by the `out` arugment
+  int num_outputs = out != nullptr;
+  auto ndoutputs = Invoke(op, &attrs, num_inputs, inputs, &num_outputs, outputs);
+  if (out) {
+    // PythonArg(n) designates the nth python argument is to be returned.
+    // So suppose `out` is the 3rd positional argument, we use PythonArg(2) (0-based index)
 
 Review comment:
   Comments are inconsistent with code, please remove.

----------------------------------------------------------------
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] AntiZpvoh commented on a change in pull request #17385: [NumPy] add random.geometric op

Posted by GitBox <gi...@apache.org>.
AntiZpvoh commented on a change in pull request #17385: [NumPy] add random.geometric op
URL: https://github.com/apache/incubator-mxnet/pull/17385#discussion_r369942090
 
 

 ##########
 File path: src/operator/numpy/random/np_geometric_op.h
 ##########
 @@ -0,0 +1,177 @@
+/*
+ * 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_geometric_op.h
+ * \brief Operator for numpy sampling from geometric distribution.
+ */
+#ifndef MXNET_OPERATOR_NUMPY_RANDOM_NP_GEOMETRIC_OP_H_
+#define MXNET_OPERATOR_NUMPY_RANDOM_NP_GEOMETRIC_OP_H_
+
+#include <mxnet/operator_util.h>
+#include <algorithm>
+#include <string>
+#include <vector>
+#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 NumpyGeometricParam : public dmlc::Parameter<NumpyGeometricParam> {
+  dmlc::optional<float> prob;
+  std::string ctx;
+  int dtype;
+  dmlc::optional<mxnet::Tuple<int>> size;
+  DMLC_DECLARE_PARAMETER(NumpyGeometricParam) {
+    DMLC_DECLARE_FIELD(prob);
+    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.");
+    DMLC_DECLARE_FIELD(dtype)
+        .add_enum("uint8", mshadow::kUint8)
+        .add_enum("int32", mshadow::kInt32)
+        .add_enum("float32", mshadow::kFloat32)
+        .add_enum("float64", mshadow::kFloat64)
+        .add_enum("float16", mshadow::kFloat16)
+        .add_enum("bool", mshadow::kBool)
+        .set_default(mshadow::kFloat32)
+        .describe(
+            "DType of the output in case this can't be inferred. "
+            "Defaults to float32 if not defined (dtype=None).");
+  }
+};
+
+inline bool NumpyGeometricOpType(const nnvm::NodeAttrs &attrs,
+                               std::vector<int> *in_attrs,
+                               std::vector<int> *out_attrs) {
+  const NumpyGeometricParam &param = nnvm::get<NumpyGeometricParam>(attrs.parsed);
+  int otype = param.dtype;
+  (*out_attrs)[0] = otype;
+  return true;
+}
+
+namespace mxnet_op {
+
+template <int ndim, typename IType, typename OType>
+struct geometric_kernel {
+  MSHADOW_XINLINE static void Map(index_t i,
+                                  const Shape<ndim> &stride,
+                                  const Shape<ndim> &oshape,
+                                  IType *probs, float* uniforms, OType *out) {
+    Shape<ndim> coord = unravel(i, oshape);
+    auto idx = static_cast<index_t>(dot(coord, stride));
+    IType prob = probs[idx];
+    out[i] = floor(log(uniforms[i]) / log(1 - prob)) + 1;
+  }
+};
+
+template <typename OType>
+struct scalar_geometric_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, float prob, float *uniforms,
+                                  OType *out) {
+    out[i] = floor(log(uniforms[i]) / log(1 - prob)) + 1;
+  }
+};
+
+template <typename IType>
+struct check_legal_prob_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, IType *scalar, float* flag) {
+    if (scalar[i] < 0.0 || scalar[i] > 1.0) {
 
 Review comment:
   You mean 0 prob is unacceptable?

----------------------------------------------------------------
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 #17385: [NumPy] add random.geometric op

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

 ##########
 File path: src/operator/numpy/random/np_geometric_op.h
 ##########
 @@ -0,0 +1,177 @@
+/*
+ * 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_geometric_op.h
+ * \brief Operator for numpy sampling from geometric distribution.
+ */
+#ifndef MXNET_OPERATOR_NUMPY_RANDOM_NP_GEOMETRIC_OP_H_
+#define MXNET_OPERATOR_NUMPY_RANDOM_NP_GEOMETRIC_OP_H_
+
+#include <mxnet/operator_util.h>
+#include <algorithm>
+#include <string>
+#include <vector>
+#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 NumpyGeometricParam : public dmlc::Parameter<NumpyGeometricParam> {
+  dmlc::optional<float> prob;
+  std::string ctx;
+  int dtype;
+  dmlc::optional<mxnet::Tuple<int>> size;
+  DMLC_DECLARE_PARAMETER(NumpyGeometricParam) {
+    DMLC_DECLARE_FIELD(prob);
+    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.");
+    DMLC_DECLARE_FIELD(dtype)
+        .add_enum("uint8", mshadow::kUint8)
+        .add_enum("int32", mshadow::kInt32)
+        .add_enum("float32", mshadow::kFloat32)
+        .add_enum("float64", mshadow::kFloat64)
+        .add_enum("float16", mshadow::kFloat16)
+        .add_enum("bool", mshadow::kBool)
+        .set_default(mshadow::kFloat32)
+        .describe(
+            "DType of the output in case this can't be inferred. "
+            "Defaults to float32 if not defined (dtype=None).");
+  }
+};
+
+inline bool NumpyGeometricOpType(const nnvm::NodeAttrs &attrs,
+                               std::vector<int> *in_attrs,
+                               std::vector<int> *out_attrs) {
+  const NumpyGeometricParam &param = nnvm::get<NumpyGeometricParam>(attrs.parsed);
+  int otype = param.dtype;
+  (*out_attrs)[0] = otype;
+  return true;
+}
+
+namespace mxnet_op {
+
+template <int ndim, typename IType, typename OType>
+struct geometric_kernel {
+  MSHADOW_XINLINE static void Map(index_t i,
+                                  const Shape<ndim> &stride,
+                                  const Shape<ndim> &oshape,
+                                  IType *probs, float* uniforms, OType *out) {
+    Shape<ndim> coord = unravel(i, oshape);
+    auto idx = static_cast<index_t>(dot(coord, stride));
+    IType prob = probs[idx];
+    out[i] = floor(log(uniforms[i]) / log(1 - prob)) + 1;
+  }
+};
+
+template <typename OType>
+struct scalar_geometric_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, float prob, float *uniforms,
+                                  OType *out) {
+    out[i] = floor(log(uniforms[i]) / log(1 - prob)) + 1;
+  }
+};
+
+template <typename IType>
+struct check_legal_prob_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, IType *scalar, float* flag) {
+    if (scalar[i] < 0.0 || scalar[i] > 1.0) {
+      flag[0] = -1.0;
+    }
+  }
+};
+
+}  // namespace mxnet_op
+
+template <typename xpu>
+void NumpyGeometricForward(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 NumpyGeometricParam &param = nnvm::get<NumpyGeometricParam>(attrs.parsed);
+  Stream<xpu> *s = ctx.get_stream<xpu>();
+  index_t output_len = outputs[0].Size();
+  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(output_len + 1), s);
+  Tensor<xpu, 1, float> uniform_tensor = workspace.Slice(0, output_len);
+  Tensor<xpu, 1, float> indicator_device = workspace.Slice(output_len, output_len + 1);
+  float indicator_host = 1.0;
+  float *indicator_device_ptr = indicator_device.dptr_;
+  Kernel<set_zero, xpu>::Launch(s, 1, indicator_device_ptr);
+  prnd->SampleUniform(&uniform_tensor, 0.0, 1.0);
+  if (inputs.size() == 0U) {
+    // scalar prob input
+    CHECK_LE(param.prob.value(), 1.0) << "ValueError: expect probs >= 0 && probs <= 1";
+    CHECK_GE(param.prob.value(), 0.0) << "ValueError: expect probs >= 0 && probs <= 1";
+    MSHADOW_TYPE_SWITCH_WITH_BOOL(outputs[0].type_flag_, OType, {
+      Kernel<scalar_geometric_kernel<OType>, xpu>::Launch(
+        s, outputs[0].Size(), param.prob.value(),
+        uniform_tensor.dptr_, outputs[0].dptr<OType>());
+    });
+  } else if (inputs.size() == 1U) {
+    mxnet::TShape new_lshape, new_oshape;
+    int ndim = FillShape(inputs[0].shape_, inputs[0].shape_, outputs[0].shape_,
+                         &new_lshape, &new_lshape, &new_oshape);
+    MSHADOW_TYPE_SWITCH(inputs[0].type_flag_, IType, {
+      MSHADOW_TYPE_SWITCH_WITH_BOOL(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>());
+          // tensor prob input
+          MSHADOW_TYPE_SWITCH(inputs[0].type_flag_, IType, {
+            Kernel<check_legal_prob_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: expect probs >= 0 && probs <= 1";
+          Kernel<geometric_kernel<NDim, IType, OType>, xpu>::Launch(
+            s, outputs[0].Size(), stride, oshape, inputs[0].dptr<IType>(),
+            uniform_tensor.dptr_, outputs[0].dptr<OType>());
+
+//          Kernel<geometric_kernel<NDim, IType, OType>, xpu>::Launch(
 
 Review comment:
   Remove unused code.

----------------------------------------------------------------
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] hzfan commented on a change in pull request #17385: [NumPy] add random.geometric op

Posted by GitBox <gi...@apache.org>.
hzfan commented on a change in pull request #17385: [NumPy] add random.geometric op
URL: https://github.com/apache/incubator-mxnet/pull/17385#discussion_r393585864
 
 

 ##########
 File path: src/operator/numpy/random/np_geometric_op.h
 ##########
 @@ -0,0 +1,184 @@
+/*
+ * 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_geometric_op.h
+ * \brief Operator for numpy sampling from geometric distribution.
+ */
+#ifndef MXNET_OPERATOR_NUMPY_RANDOM_NP_GEOMETRIC_OP_H_
+#define MXNET_OPERATOR_NUMPY_RANDOM_NP_GEOMETRIC_OP_H_
+
+#include <mxnet/operator_util.h>
+#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 NumpyGeometricParam : public dmlc::Parameter<NumpyGeometricParam> {
+  dmlc::optional<float> prob;
+  std::string ctx;
+  int dtype;
+  dmlc::optional<mxnet::Tuple<int>> size;
+  DMLC_DECLARE_PARAMETER(NumpyGeometricParam) {
+    DMLC_DECLARE_FIELD(prob);
+    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.");
+    DMLC_DECLARE_FIELD(dtype)
+        .add_enum("uint8", mshadow::kUint8)
+        .add_enum("int32", mshadow::kInt32)
+        .add_enum("float32", mshadow::kFloat32)
+        .add_enum("float64", mshadow::kFloat64)
+        .add_enum("float16", mshadow::kFloat16)
+        .add_enum("bool", mshadow::kBool)
+        .set_default(mshadow::kInt32)
+        .describe(
+            "DType of the output in case this can't be inferred. "
+            "Defaults to int32 if not defined (dtype=None).");
+  }
+
+  void SetAttrDict(std::unordered_map<std::string, std::string>* dict) {
+    std::ostringstream prob_s, dtype_s, size_s;
+    prob_s << prob;
+    dtype_s << dtype;
+    size_s << size;
+    (*dict)["prob"] = prob_s.str();
+    (*dict)["dtype"] = dtype_s.str();
 
 Review comment:
   ```
   (*dict)["dtype"] = String2MXNetTypeWithBool(dtype);
   ```
   `String2MXNetTypeWithBool` lies in `incubator-mxnet/src/api/operator/op_utils.h`

----------------------------------------------------------------
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 #17385: [NumPy] add random.geometric op

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

 ##########
 File path: src/operator/numpy/random/np_geometric_op.h
 ##########
 @@ -0,0 +1,177 @@
+/*
+ * 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_geometric_op.h
+ * \brief Operator for numpy sampling from geometric distribution.
+ */
+#ifndef MXNET_OPERATOR_NUMPY_RANDOM_NP_GEOMETRIC_OP_H_
+#define MXNET_OPERATOR_NUMPY_RANDOM_NP_GEOMETRIC_OP_H_
+
+#include <mxnet/operator_util.h>
+#include <algorithm>
+#include <string>
+#include <vector>
+#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 NumpyGeometricParam : public dmlc::Parameter<NumpyGeometricParam> {
+  dmlc::optional<float> prob;
+  std::string ctx;
+  int dtype;
+  dmlc::optional<mxnet::Tuple<int>> size;
+  DMLC_DECLARE_PARAMETER(NumpyGeometricParam) {
+    DMLC_DECLARE_FIELD(prob);
+    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.");
+    DMLC_DECLARE_FIELD(dtype)
+        .add_enum("uint8", mshadow::kUint8)
+        .add_enum("int32", mshadow::kInt32)
+        .add_enum("float32", mshadow::kFloat32)
+        .add_enum("float64", mshadow::kFloat64)
+        .add_enum("float16", mshadow::kFloat16)
+        .add_enum("bool", mshadow::kBool)
+        .set_default(mshadow::kFloat32)
+        .describe(
+            "DType of the output in case this can't be inferred. "
+            "Defaults to float32 if not defined (dtype=None).");
+  }
+};
+
+inline bool NumpyGeometricOpType(const nnvm::NodeAttrs &attrs,
+                               std::vector<int> *in_attrs,
+                               std::vector<int> *out_attrs) {
+  const NumpyGeometricParam &param = nnvm::get<NumpyGeometricParam>(attrs.parsed);
+  int otype = param.dtype;
+  (*out_attrs)[0] = otype;
+  return true;
+}
+
+namespace mxnet_op {
+
+template <int ndim, typename IType, typename OType>
+struct geometric_kernel {
+  MSHADOW_XINLINE static void Map(index_t i,
+                                  const Shape<ndim> &stride,
+                                  const Shape<ndim> &oshape,
+                                  IType *probs, float* uniforms, OType *out) {
+    Shape<ndim> coord = unravel(i, oshape);
+    auto idx = static_cast<index_t>(dot(coord, stride));
+    IType prob = probs[idx];
+    out[i] = floor(log(uniforms[i]) / log(1 - prob)) + 1;
+  }
+};
+
+template <typename OType>
+struct scalar_geometric_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, float prob, float *uniforms,
+                                  OType *out) {
+    out[i] = floor(log(uniforms[i]) / log(1 - prob)) + 1;
+  }
+};
+
+template <typename IType>
+struct check_legal_prob_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, IType *scalar, float* flag) {
+    if (scalar[i] < 0.0 || scalar[i] > 1.0) {
+      flag[0] = -1.0;
+    }
+  }
+};
+
+}  // namespace mxnet_op
+
+template <typename xpu>
+void NumpyGeometricForward(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 NumpyGeometricParam &param = nnvm::get<NumpyGeometricParam>(attrs.parsed);
+  Stream<xpu> *s = ctx.get_stream<xpu>();
+  index_t output_len = outputs[0].Size();
+  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(output_len + 1), s);
+  Tensor<xpu, 1, float> uniform_tensor = workspace.Slice(0, output_len);
+  Tensor<xpu, 1, float> indicator_device = workspace.Slice(output_len, output_len + 1);
+  float indicator_host = 1.0;
+  float *indicator_device_ptr = indicator_device.dptr_;
+  Kernel<set_zero, xpu>::Launch(s, 1, indicator_device_ptr);
+  prnd->SampleUniform(&uniform_tensor, 0.0, 1.0);
+  if (inputs.size() == 0U) {
+    // scalar prob input
+    CHECK_LE(param.prob.value(), 1.0) << "ValueError: expect probs >= 0 && probs <= 1";
 
 Review comment:
   Expect `prob` **>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] haojin2 commented on a change in pull request #17385: [NumPy] add random.geometric op

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

 ##########
 File path: src/operator/numpy/random/np_geometric_op.h
 ##########
 @@ -0,0 +1,177 @@
+/*
+ * 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_geometric_op.h
+ * \brief Operator for numpy sampling from geometric distribution.
+ */
+#ifndef MXNET_OPERATOR_NUMPY_RANDOM_NP_GEOMETRIC_OP_H_
+#define MXNET_OPERATOR_NUMPY_RANDOM_NP_GEOMETRIC_OP_H_
+
+#include <mxnet/operator_util.h>
+#include <algorithm>
+#include <string>
+#include <vector>
+#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 NumpyGeometricParam : public dmlc::Parameter<NumpyGeometricParam> {
+  dmlc::optional<float> prob;
+  std::string ctx;
+  int dtype;
+  dmlc::optional<mxnet::Tuple<int>> size;
+  DMLC_DECLARE_PARAMETER(NumpyGeometricParam) {
+    DMLC_DECLARE_FIELD(prob);
+    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.");
+    DMLC_DECLARE_FIELD(dtype)
+        .add_enum("uint8", mshadow::kUint8)
+        .add_enum("int32", mshadow::kInt32)
+        .add_enum("float32", mshadow::kFloat32)
+        .add_enum("float64", mshadow::kFloat64)
+        .add_enum("float16", mshadow::kFloat16)
+        .add_enum("bool", mshadow::kBool)
+        .set_default(mshadow::kFloat32)
 
 Review comment:
   @AntiZpvoh 

----------------------------------------------------------------
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 #17385: [NumPy] add random.geometric op

Posted by GitBox <gi...@apache.org>.
haojin2 commented on issue #17385: [NumPy] add random.geometric op
URL: https://github.com/apache/incubator-mxnet/pull/17385#issuecomment-598480020
 
 
   @hzfan Can you also take a look at the FFI parts?

----------------------------------------------------------------
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 #17385: [NumPy] add random.geometric op

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

 ##########
 File path: src/operator/numpy/random/np_geometric_op.h
 ##########
 @@ -0,0 +1,177 @@
+/*
+ * 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_geometric_op.h
+ * \brief Operator for numpy sampling from geometric distribution.
+ */
+#ifndef MXNET_OPERATOR_NUMPY_RANDOM_NP_GEOMETRIC_OP_H_
+#define MXNET_OPERATOR_NUMPY_RANDOM_NP_GEOMETRIC_OP_H_
+
+#include <mxnet/operator_util.h>
+#include <algorithm>
+#include <string>
+#include <vector>
+#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 NumpyGeometricParam : public dmlc::Parameter<NumpyGeometricParam> {
+  dmlc::optional<float> prob;
+  std::string ctx;
+  int dtype;
+  dmlc::optional<mxnet::Tuple<int>> size;
+  DMLC_DECLARE_PARAMETER(NumpyGeometricParam) {
+    DMLC_DECLARE_FIELD(prob);
+    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.");
+    DMLC_DECLARE_FIELD(dtype)
+        .add_enum("uint8", mshadow::kUint8)
+        .add_enum("int32", mshadow::kInt32)
+        .add_enum("float32", mshadow::kFloat32)
+        .add_enum("float64", mshadow::kFloat64)
+        .add_enum("float16", mshadow::kFloat16)
+        .add_enum("bool", mshadow::kBool)
+        .set_default(mshadow::kFloat32)
+        .describe(
+            "DType of the output in case this can't be inferred. "
+            "Defaults to float32 if not defined (dtype=None).");
+  }
+};
+
+inline bool NumpyGeometricOpType(const nnvm::NodeAttrs &attrs,
+                               std::vector<int> *in_attrs,
+                               std::vector<int> *out_attrs) {
+  const NumpyGeometricParam &param = nnvm::get<NumpyGeometricParam>(attrs.parsed);
+  int otype = param.dtype;
+  (*out_attrs)[0] = otype;
+  return true;
+}
+
+namespace mxnet_op {
+
+template <int ndim, typename IType, typename OType>
+struct geometric_kernel {
+  MSHADOW_XINLINE static void Map(index_t i,
+                                  const Shape<ndim> &stride,
+                                  const Shape<ndim> &oshape,
+                                  IType *probs, float* uniforms, OType *out) {
+    Shape<ndim> coord = unravel(i, oshape);
+    auto idx = static_cast<index_t>(dot(coord, stride));
+    IType prob = probs[idx];
+    out[i] = floor(log(uniforms[i]) / log(1 - prob)) + 1;
+  }
+};
+
+template <typename OType>
+struct scalar_geometric_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, float prob, float *uniforms,
+                                  OType *out) {
+    out[i] = floor(log(uniforms[i]) / log(1 - prob)) + 1;
+  }
+};
+
+template <typename IType>
+struct check_legal_prob_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, IType *scalar, float* flag) {
+    if (scalar[i] < 0.0 || scalar[i] > 1.0) {
+      flag[0] = -1.0;
+    }
+  }
+};
+
+}  // namespace mxnet_op
+
+template <typename xpu>
+void NumpyGeometricForward(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 NumpyGeometricParam &param = nnvm::get<NumpyGeometricParam>(attrs.parsed);
+  Stream<xpu> *s = ctx.get_stream<xpu>();
+  index_t output_len = outputs[0].Size();
+  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(output_len + 1), s);
+  Tensor<xpu, 1, float> uniform_tensor = workspace.Slice(0, output_len);
+  Tensor<xpu, 1, float> indicator_device = workspace.Slice(output_len, output_len + 1);
+  float indicator_host = 1.0;
+  float *indicator_device_ptr = indicator_device.dptr_;
+  Kernel<set_zero, xpu>::Launch(s, 1, indicator_device_ptr);
+  prnd->SampleUniform(&uniform_tensor, 0.0, 1.0);
+  if (inputs.size() == 0U) {
+    // scalar prob input
+    CHECK_LE(param.prob.value(), 1.0) << "ValueError: expect probs >= 0 && probs <= 1";
 
 Review comment:
   `p` should be strictly **larger** than zero.

----------------------------------------------------------------
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 #17385: [NumPy] add random.geometric op

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

 ##########
 File path: src/operator/numpy/random/np_geometric_op.h
 ##########
 @@ -0,0 +1,177 @@
+/*
+ * 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_geometric_op.h
+ * \brief Operator for numpy sampling from geometric distribution.
+ */
+#ifndef MXNET_OPERATOR_NUMPY_RANDOM_NP_GEOMETRIC_OP_H_
+#define MXNET_OPERATOR_NUMPY_RANDOM_NP_GEOMETRIC_OP_H_
+
+#include <mxnet/operator_util.h>
+#include <algorithm>
+#include <string>
+#include <vector>
+#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 NumpyGeometricParam : public dmlc::Parameter<NumpyGeometricParam> {
+  dmlc::optional<float> prob;
+  std::string ctx;
+  int dtype;
+  dmlc::optional<mxnet::Tuple<int>> size;
+  DMLC_DECLARE_PARAMETER(NumpyGeometricParam) {
+    DMLC_DECLARE_FIELD(prob);
+    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.");
+    DMLC_DECLARE_FIELD(dtype)
+        .add_enum("uint8", mshadow::kUint8)
+        .add_enum("int32", mshadow::kInt32)
+        .add_enum("float32", mshadow::kFloat32)
+        .add_enum("float64", mshadow::kFloat64)
+        .add_enum("float16", mshadow::kFloat16)
+        .add_enum("bool", mshadow::kBool)
+        .set_default(mshadow::kFloat32)
+        .describe(
+            "DType of the output in case this can't be inferred. "
+            "Defaults to float32 if not defined (dtype=None).");
+  }
+};
+
+inline bool NumpyGeometricOpType(const nnvm::NodeAttrs &attrs,
+                               std::vector<int> *in_attrs,
+                               std::vector<int> *out_attrs) {
+  const NumpyGeometricParam &param = nnvm::get<NumpyGeometricParam>(attrs.parsed);
+  int otype = param.dtype;
+  (*out_attrs)[0] = otype;
+  return true;
+}
+
+namespace mxnet_op {
+
+template <int ndim, typename IType, typename OType>
+struct geometric_kernel {
+  MSHADOW_XINLINE static void Map(index_t i,
+                                  const Shape<ndim> &stride,
+                                  const Shape<ndim> &oshape,
+                                  IType *probs, float* uniforms, OType *out) {
+    Shape<ndim> coord = unravel(i, oshape);
+    auto idx = static_cast<index_t>(dot(coord, stride));
+    IType prob = probs[idx];
+    out[i] = floor(log(uniforms[i]) / log(1 - prob)) + 1;
+  }
+};
+
+template <typename OType>
+struct scalar_geometric_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, float prob, float *uniforms,
+                                  OType *out) {
+    out[i] = floor(log(uniforms[i]) / log(1 - prob)) + 1;
+  }
+};
+
+template <typename IType>
+struct check_legal_prob_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, IType *scalar, float* flag) {
+    if (scalar[i] < 0.0 || scalar[i] > 1.0) {
 
 Review comment:
   Should it be `scalar[i] <= 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] AntiZpvoh commented on a change in pull request #17385: [NumPy] add random.geometric op

Posted by GitBox <gi...@apache.org>.
AntiZpvoh commented on a change in pull request #17385: [NumPy] add random.geometric op
URL: https://github.com/apache/incubator-mxnet/pull/17385#discussion_r369942090
 
 

 ##########
 File path: src/operator/numpy/random/np_geometric_op.h
 ##########
 @@ -0,0 +1,177 @@
+/*
+ * 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_geometric_op.h
+ * \brief Operator for numpy sampling from geometric distribution.
+ */
+#ifndef MXNET_OPERATOR_NUMPY_RANDOM_NP_GEOMETRIC_OP_H_
+#define MXNET_OPERATOR_NUMPY_RANDOM_NP_GEOMETRIC_OP_H_
+
+#include <mxnet/operator_util.h>
+#include <algorithm>
+#include <string>
+#include <vector>
+#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 NumpyGeometricParam : public dmlc::Parameter<NumpyGeometricParam> {
+  dmlc::optional<float> prob;
+  std::string ctx;
+  int dtype;
+  dmlc::optional<mxnet::Tuple<int>> size;
+  DMLC_DECLARE_PARAMETER(NumpyGeometricParam) {
+    DMLC_DECLARE_FIELD(prob);
+    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.");
+    DMLC_DECLARE_FIELD(dtype)
+        .add_enum("uint8", mshadow::kUint8)
+        .add_enum("int32", mshadow::kInt32)
+        .add_enum("float32", mshadow::kFloat32)
+        .add_enum("float64", mshadow::kFloat64)
+        .add_enum("float16", mshadow::kFloat16)
+        .add_enum("bool", mshadow::kBool)
+        .set_default(mshadow::kFloat32)
+        .describe(
+            "DType of the output in case this can't be inferred. "
+            "Defaults to float32 if not defined (dtype=None).");
+  }
+};
+
+inline bool NumpyGeometricOpType(const nnvm::NodeAttrs &attrs,
+                               std::vector<int> *in_attrs,
+                               std::vector<int> *out_attrs) {
+  const NumpyGeometricParam &param = nnvm::get<NumpyGeometricParam>(attrs.parsed);
+  int otype = param.dtype;
+  (*out_attrs)[0] = otype;
+  return true;
+}
+
+namespace mxnet_op {
+
+template <int ndim, typename IType, typename OType>
+struct geometric_kernel {
+  MSHADOW_XINLINE static void Map(index_t i,
+                                  const Shape<ndim> &stride,
+                                  const Shape<ndim> &oshape,
+                                  IType *probs, float* uniforms, OType *out) {
+    Shape<ndim> coord = unravel(i, oshape);
+    auto idx = static_cast<index_t>(dot(coord, stride));
+    IType prob = probs[idx];
+    out[i] = floor(log(uniforms[i]) / log(1 - prob)) + 1;
+  }
+};
+
+template <typename OType>
+struct scalar_geometric_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, float prob, float *uniforms,
+                                  OType *out) {
+    out[i] = floor(log(uniforms[i]) / log(1 - prob)) + 1;
+  }
+};
+
+template <typename IType>
+struct check_legal_prob_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, IType *scalar, float* flag) {
+    if (scalar[i] < 0.0 || scalar[i] > 1.0) {
 
 Review comment:
   You mean 0 prob is unacceptable?

----------------------------------------------------------------
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 #17385: [NumPy] add random.geometric op

Posted by GitBox <gi...@apache.org>.
haojin2 commented on issue #17385: [NumPy] add random.geometric op
URL: https://github.com/apache/incubator-mxnet/pull/17385#issuecomment-598479958
 
 
   @AntiZpvoh Please address all comments and also resolve the conflicts.

----------------------------------------------------------------
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 #17385: [NumPy] add random.geometric op

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

 ##########
 File path: src/operator/numpy/random/np_geometric_op.h
 ##########
 @@ -0,0 +1,185 @@
+/*
+ * 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_geometric_op.h
+ * \brief Operator for numpy sampling from geometric distribution.
+ */
+#ifndef MXNET_OPERATOR_NUMPY_RANDOM_NP_GEOMETRIC_OP_H_
+#define MXNET_OPERATOR_NUMPY_RANDOM_NP_GEOMETRIC_OP_H_
+
+#include <mxnet/operator_util.h>
+#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 NumpyGeometricParam : public dmlc::Parameter<NumpyGeometricParam> {
+  dmlc::optional<float> prob;
+  std::string ctx;
+  int dtype;
+  dmlc::optional<mxnet::Tuple<int>> size;
+  DMLC_DECLARE_PARAMETER(NumpyGeometricParam) {
+    DMLC_DECLARE_FIELD(prob);
+    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.");
+    DMLC_DECLARE_FIELD(dtype)
+        .add_enum("uint8", mshadow::kUint8)
+        .add_enum("int32", mshadow::kInt32)
+        .add_enum("float32", mshadow::kFloat32)
+        .add_enum("float64", mshadow::kFloat64)
+        .add_enum("float16", mshadow::kFloat16)
+        .add_enum("bool", mshadow::kBool)
+        .set_default(mshadow::kFloat32)
+        .describe(
+            "DType of the output in case this can't be inferred. "
+            "Defaults to float32 if not defined (dtype=None).");
+  }
+  void SetAttrDict(std::unordered_map<std::string, std::string>* dict) {
+    std::ostringstream prob_s, dtype_s, size_s;
+    prob_s << prob;
+    dtype_s << dtype;
+    size_s << size;
+    (*dict)["prob"] = prob_s.str();
+    (*dict)["dtype"] = dtype_s.str();
+    (*dict)["size"] = size_s.str();
+    // We do not set ctx, because ctx has been set in dict instead of InitOpParam.
 
 Review comment:
   fix this line of comment or get rid of it.

----------------------------------------------------------------
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 #17385: [NumPy] add random.geometric op

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

 ##########
 File path: src/operator/numpy/random/np_geometric_op.h
 ##########
 @@ -0,0 +1,177 @@
+/*
+ * 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_geometric_op.h
+ * \brief Operator for numpy sampling from geometric distribution.
+ */
+#ifndef MXNET_OPERATOR_NUMPY_RANDOM_NP_GEOMETRIC_OP_H_
+#define MXNET_OPERATOR_NUMPY_RANDOM_NP_GEOMETRIC_OP_H_
+
+#include <mxnet/operator_util.h>
+#include <algorithm>
+#include <string>
+#include <vector>
+#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 NumpyGeometricParam : public dmlc::Parameter<NumpyGeometricParam> {
+  dmlc::optional<float> prob;
+  std::string ctx;
+  int dtype;
+  dmlc::optional<mxnet::Tuple<int>> size;
+  DMLC_DECLARE_PARAMETER(NumpyGeometricParam) {
+    DMLC_DECLARE_FIELD(prob);
+    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.");
+    DMLC_DECLARE_FIELD(dtype)
+        .add_enum("uint8", mshadow::kUint8)
+        .add_enum("int32", mshadow::kInt32)
+        .add_enum("float32", mshadow::kFloat32)
+        .add_enum("float64", mshadow::kFloat64)
+        .add_enum("float16", mshadow::kFloat16)
+        .add_enum("bool", mshadow::kBool)
+        .set_default(mshadow::kFloat32)
 
 Review comment:
   I suggest changing the default dtype to `int` in order to be consistent with numpy.

----------------------------------------------------------------
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 #17385: [NumPy] add random.geometric op

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

 ##########
 File path: src/operator/numpy/random/np_geometric_op.h
 ##########
 @@ -0,0 +1,185 @@
+/*
+ * 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_geometric_op.h
+ * \brief Operator for numpy sampling from geometric distribution.
+ */
+#ifndef MXNET_OPERATOR_NUMPY_RANDOM_NP_GEOMETRIC_OP_H_
+#define MXNET_OPERATOR_NUMPY_RANDOM_NP_GEOMETRIC_OP_H_
+
+#include <mxnet/operator_util.h>
+#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 NumpyGeometricParam : public dmlc::Parameter<NumpyGeometricParam> {
+  dmlc::optional<float> prob;
+  std::string ctx;
+  int dtype;
+  dmlc::optional<mxnet::Tuple<int>> size;
+  DMLC_DECLARE_PARAMETER(NumpyGeometricParam) {
+    DMLC_DECLARE_FIELD(prob);
+    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.");
+    DMLC_DECLARE_FIELD(dtype)
+        .add_enum("uint8", mshadow::kUint8)
+        .add_enum("int32", mshadow::kInt32)
+        .add_enum("float32", mshadow::kFloat32)
+        .add_enum("float64", mshadow::kFloat64)
+        .add_enum("float16", mshadow::kFloat16)
+        .add_enum("bool", mshadow::kBool)
+        .set_default(mshadow::kFloat32)
+        .describe(
+            "DType of the output in case this can't be inferred. "
+            "Defaults to float32 if not defined (dtype=None).");
+  }
+  void SetAttrDict(std::unordered_map<std::string, std::string>* dict) {
 
 Review comment:
   one more blank line above

----------------------------------------------------------------
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 #17385: [NumPy] add random.geometric op

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

 ##########
 File path: src/operator/numpy/random/np_geometric_op.h
 ##########
 @@ -0,0 +1,173 @@
+/*
+ * 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_geometric_op.h
+ * \brief Operator for numpy sampling from geometric distribution.
+ */
+#ifndef MXNET_OPERATOR_NUMPY_RANDOM_NP_GEOMETRIC_OP_H_
+#define MXNET_OPERATOR_NUMPY_RANDOM_NP_GEOMETRIC_OP_H_
+
+#include <mxnet/operator_util.h>
+#include <algorithm>
+#include <string>
+#include <vector>
+#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 NumpyGeometricParam : public dmlc::Parameter<NumpyGeometricParam> {
+  dmlc::optional<float> prob;
+  std::string ctx;
+  int dtype;
+  dmlc::optional<mxnet::Tuple<int>> size;
+  DMLC_DECLARE_PARAMETER(NumpyGeometricParam) {
+    DMLC_DECLARE_FIELD(prob);
+    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.");
+    DMLC_DECLARE_FIELD(dtype)
+        .add_enum("uint8", mshadow::kUint8)
+        .add_enum("int32", mshadow::kInt32)
+        .add_enum("float32", mshadow::kFloat32)
+        .add_enum("float64", mshadow::kFloat64)
+        .add_enum("float16", mshadow::kFloat16)
+        .add_enum("bool", mshadow::kBool)
+        .set_default(mshadow::kInt32)
+        .describe(
+            "DType of the output in case this can't be inferred. "
+            "Defaults to float32 if not defined (dtype=None).");
+  }
+};
+
+inline bool NumpyGeometricOpType(const nnvm::NodeAttrs &attrs,
+                               std::vector<int> *in_attrs,
+                               std::vector<int> *out_attrs) {
+  const NumpyGeometricParam &param = nnvm::get<NumpyGeometricParam>(attrs.parsed);
+  int otype = param.dtype;
+  (*out_attrs)[0] = otype;
+  return true;
+}
+
+namespace mxnet_op {
+
+template <int ndim, typename IType, typename OType>
+struct geometric_kernel {
+  MSHADOW_XINLINE static void Map(index_t i,
+                                  const Shape<ndim> &stride,
+                                  const Shape<ndim> &oshape,
+                                  IType *probs, float* uniforms, OType *out) {
+    Shape<ndim> coord = unravel(i, oshape);
+    auto idx = static_cast<index_t>(dot(coord, stride));
+    IType prob = probs[idx];
+    out[i] = floor(log(uniforms[i]) / log(1 - prob)) + 1;
+  }
+};
+
+template <typename OType>
+struct scalar_geometric_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, float prob, float *uniforms,
+                                  OType *out) {
+    out[i] = floor(log(uniforms[i]) / log(1 - prob)) + 1;
+  }
+};
+
+template <typename IType>
+struct check_legal_prob_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, IType *scalar, float* flag) {
+    if (scalar[i] <= 0.0 || scalar[i] > 1.0) {
+      flag[0] = -1.0;
+    }
+  }
+};
+
+}  // namespace mxnet_op
+
+template <typename xpu>
+void NumpyGeometricForward(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