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


With regards,
Apache Git Services

Reply via email to