haojin2 commented on a change in pull request #16152: [Numpy] Random.gamma() 
implemented
URL: https://github.com/apache/incubator-mxnet/pull/16152#discussion_r364377425
 
 

 ##########
 File path: src/operator/numpy/random/np_gamma_op.h
 ##########
 @@ -0,0 +1,422 @@
+/*
+ * 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_gamma_op.h
+ * \brief Operator for random sampling from gamma distribution
+ */
+
+#ifndef MXNET_OPERATOR_NUMPY_RANDOM_NP_GAMMA_OP_H_
+#define MXNET_OPERATOR_NUMPY_RANDOM_NP_GAMMA_OP_H_
+
+#include <mxnet/operator_util.h>
+#include <mshadow/base.h>
+#include <vector>
+#include <string>
+#include <algorithm>
+#include "./dist_common.h"
+#include "../../elemwise_op_common.h"
+#include "../../tensor/elemwise_binary_broadcast_op.h"
+#include "../../mshadow_op.h"
+#include "../../mxnet_op.h"
+#include "../../operator_common.h"
+
+#define M 2
+
+namespace mxnet {
+namespace op {
+
+struct NumpyGammaParam : public dmlc::Parameter<NumpyGammaParam> {
+  dmlc::optional<float> shape;
+  dmlc::optional<float> scale;
+  std::string ctx;
+  int dtype;
+  dmlc::optional<mxnet::Tuple<int>> size;
+  DMLC_DECLARE_PARAMETER(NumpyGammaParam) {
+    DMLC_DECLARE_FIELD(shape);
+    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("xpu")
+    .describe("Context of output, in format [xpu|xpu|xpu_pinned](n)."
+              " Only used for imperative calls.");
+    DMLC_DECLARE_FIELD(dtype)
+    .add_enum("float32", mshadow::kFloat32)
+    .add_enum("float64", mshadow::kFloat64)
+    .add_enum("float16", mshadow::kFloat16)
+    .set_default(mshadow::kFloat32)
+    .describe("DType of the output in case this can't be inferred. "
+              "Defaults to float32 if not defined (dtype=None).");
+  }
+};
+
+
+namespace mxnet_op {
+
+template <typename IType, typename FType>
+MSHADOW_XINLINE void GammaTransform(IType a, IType b,
+                                    FType* uniforms, FType* normals) {
+  // start
+  FType d = a < 1 ? a + 2.0 / 3.0 : a - 1.0 / 3.0;
+  FType k = sqrt(9.0 * d);
+  FType c = 1.0 / k;
+  for (size_t i = 0; i < M - 1; i++) {
+    FType u = uniforms[i];
+    FType n = normals[i];
+    uniforms[i] = FType(-1);
+    FType ocn = 1+c*n;
+    FType v = ocn*ocn*ocn;
+    if (v > 0) {
+      if (u <= (1 - 0.0331 * (n * n) * (n * n))) {
+        // rejection sample. The second operation should be
+        // performed with low probability. This is the "squeeze"
+        uniforms[i] = FType(d * v * b);
+      }
+      if (logf(u) < 0.5 * (n * n) + d * (1 - v + logf(v))) {
+        // rejection sample. The second operation should be
+        // performed with low probability. This is the "squeeze"
+        uniforms[i] = FType(d * v * b);
+      }
+    }
+  }
+}
+
+
+template <typename IType, typename FType>
+MSHADOW_XINLINE FType GammaReduce(IType a, FType* uniforms) {
+  FType u2 = uniforms[M - 1];
+  for (size_t i = 0; i < M - 1; i++) {
+    FType sample = uniforms[i];
+    if (sample > 0) {
+      return a < 1 ? sample * powf(u2, FType(1.0 / a)) : sample;
+    }
+  }
+  return -1;
+}
+
+template<typename OType, typename FType>
+struct CheckSuccessKernel {
+  MSHADOW_XINLINE static void Map(int i, OType* out, FType* flag) {
+    if (out[i] < 0) {
+      flag[0] = -1.0;
+    }
+  }
+};
+
+template <int ndim, typename IType, typename OType, typename FType>
+struct gamma_kernel {
+  MSHADOW_XINLINE static void Map(index_t i,
+                                  const Shape <ndim> &lstride, const Shape 
<ndim> &hstride,
+                                  const Shape <ndim> &oshape,
+                                  IType *shape, IType *scale,
+                                  FType *uniforms, FType *normals,
+                                  OType *out) {
+  Shape<ndim> coord = unravel(i, oshape);
 
 Review comment:
   2-space indentation for the whole function body.

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to