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


With regards,
Apache Git Services

Reply via email to