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

2020-01-16 Thread GitBox
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 
+#include 
+#include 
+#include 
+#include 
+#include 
+#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 {
+  dmlc::optional loc;
+  dmlc::optional scale;
+  dmlc::optional> size;
+  DMLC_DECLARE_PARAMETER(NumpyLocationScaleParam) {
+DMLC_DECLARE_FIELD(loc);
+DMLC_DECLARE_FIELD(scale);
+DMLC_DECLARE_FIELD(size)
+  .set_default(dmlc::optional>())
+  .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 ,
+ std::vector *in_attrs,
+ std::vector *out_attrs) {
+  (*out_attrs)[0] = mshadow::kFloat32;
+  (*out_attrs)[1] = mshadow::kFloat32;
+  return true;
+}
+
+namespace mxnet_op {
+
+template 
+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 
+  static void op(Stream *s, const int out_size,
+ const float loc, const float scale,
+ const Tensor& noise,
+ const Tensor& out) {
+Kernel, xpu>::Launch(
+  s, out_size, loc, scale, noise.dptr_, out.dptr_);
+}
+};
+
+template 
+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 
+  static void op(Stream *s, const int out_size,
+ const float loc, const float scale,
+ const Tensor& noise,
+ const Tensor& out) {
+Kernel, xpu>::Launch(
+  s, out_size, loc, scale, noise.dptr_, out.dptr_);
+  }
+};
+
+template 
+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 
+struct logistic_one_scalar_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, int scalar_pos,
+  const Shape ,
+  const Shape , IType *array,
+  float scalar, float *noise, OType *out) {
+Shape coord = unravel(i, oshape);
+auto idx = static_cast(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 
+  static void op(Stream *s, const int out_size, const int scalar_pos,
+ const Shape , const Shape ,
+ const Tensor& array, float scalar,
+ const Tensor& noise, const Tensor& out) {
+Kernel, xpu>::Launch(
+  s, out_size, scalar_pos, stride, 

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

2020-01-15 Thread GitBox
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 
+#include 
+#include 
+#include 
+#include 
+#include 
+#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 {
+  dmlc::optional loc;
+  dmlc::optional scale;
+  dmlc::optional> size;
+  DMLC_DECLARE_PARAMETER(NumpyLogisticParam) {
+DMLC_DECLARE_FIELD(loc);
+DMLC_DECLARE_FIELD(scale);
+DMLC_DECLARE_FIELD(size)
+.set_default(dmlc::optional>())
+.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 ,
+   std::vector *in_attrs,
+   std::vector *out_attrs) {
+
+  (*out_attrs)[0] = mshadow::kFloat32;
+  (*out_attrs)[1] = mshadow::kFloat32;
+  return true;
+}
+
+namespace mxnet_op {
+
+template 
+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 
+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 
+struct logistic_one_scalar_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, int scalar_pos,
+  const Shape ,
+  const Shape , IType *array,
+  float scalar, float *noise, OType *out) {
+Shape coord = unravel(i, oshape);
+auto idx = static_cast(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 
+struct logistic_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, const Shape ,
+  const Shape ,
+  const Shape , IType *loc,
+  IType *scale, float *noise, OType *out) {
+Shape coord = unravel(i, oshape);
+auto lidx = static_cast(dot(coord, lstride));
+auto hidx = static_cast(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 
+void NumpyLogisticForward(const nnvm::NodeAttrs ,
+  const OpContext ,
+  const std::vector ,
+  const std::vector ,
+  const std::vector ) {
+  using namespace mshadow;
+  using namespace mxnet_op;
+  const NumpyLogisticParam  = 
nnvm::get(attrs.parsed);
+  Stream *s = ctx.get_stream();
+  // Generate base random number.
+  Random *prnd = ctx.requested[0].get_random(s);
+  Tensor workspace =
+  ctx.requested[1].get_space_typed(Shape1(1), s);
+  Tensor logistic_tensor = outputs[1].FlatTo1D(s);
+  Tensor indicator_device = workspace;
+  float 

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

2020-01-15 Thread GitBox
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 
+#include 
+#include 
+#include 
+#include 
+#include 
+#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 {
+  dmlc::optional loc;
+  dmlc::optional scale;
+  dmlc::optional> size;
+  DMLC_DECLARE_PARAMETER(NumpyLogisticParam) {
+DMLC_DECLARE_FIELD(loc);
+DMLC_DECLARE_FIELD(scale);
+DMLC_DECLARE_FIELD(size)
+.set_default(dmlc::optional>())
+.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 ,
+   std::vector *in_attrs,
+   std::vector *out_attrs) {
+
+  (*out_attrs)[0] = mshadow::kFloat32;
+  (*out_attrs)[1] = mshadow::kFloat32;
+  return true;
+}
+
+namespace mxnet_op {
+
+template 
+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 
+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 
+struct logistic_one_scalar_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, int scalar_pos,
+  const Shape ,
+  const Shape , IType *array,
+  float scalar, float *noise, OType *out) {
+Shape coord = unravel(i, oshape);
+auto idx = static_cast(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 
+struct logistic_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, const Shape ,
+  const Shape ,
+  const Shape , IType *loc,
+  IType *scale, float *noise, OType *out) {
+Shape coord = unravel(i, oshape);
+auto lidx = static_cast(dot(coord, lstride));
+auto hidx = static_cast(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 
+void NumpyLogisticForward(const nnvm::NodeAttrs ,
+  const OpContext ,
+  const std::vector ,
+  const std::vector ,
+  const std::vector ) {
+  using namespace mshadow;
+  using namespace mxnet_op;
+  const NumpyLogisticParam  = 
nnvm::get(attrs.parsed);
+  Stream *s = ctx.get_stream();
+  // Generate base random number.
+  Random *prnd = ctx.requested[0].get_random(s);
+  Tensor workspace =
+  ctx.requested[1].get_space_typed(Shape1(1), s);
+  Tensor logistic_tensor = outputs[1].FlatTo1D(s);
+  Tensor indicator_device = workspace;
+  float 

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

2020-01-15 Thread GitBox
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 
+#include 
+#include 
+#include 
+#include 
+#include 
+#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 {
+  dmlc::optional loc;
+  dmlc::optional scale;
+  dmlc::optional> size;
+  DMLC_DECLARE_PARAMETER(NumpyLogisticParam) {
+DMLC_DECLARE_FIELD(loc);
+DMLC_DECLARE_FIELD(scale);
+DMLC_DECLARE_FIELD(size)
+.set_default(dmlc::optional>())
 
 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:
us...@infra.apache.org


With regards,
Apache Git Services