haojin2 commented on a change in pull request #15825: Numpy . add numpy op 
logspace
URL: https://github.com/apache/incubator-mxnet/pull/15825#discussion_r314460110
 
 

 ##########
 File path: src/operator/tensor/init_op.h
 ##########
 @@ -644,6 +644,75 @@ inline bool RangeLikeShape(const nnvm::NodeAttrs& attrs,
   return true;
 }
 
+struct LogspaceParam : public dmlc::Parameter<LogspaceParam> {
+  double start;
+  double stop;
+  int num;
+  bool endpoint;
+  double base;
+  std::string ctx;
+  int dtype;
+  DMLC_DECLARE_PARAMETER(LogspaceParam) {
+    DMLC_DECLARE_FIELD(start)
+        .describe("The starting value of the sequence.");
+    DMLC_DECLARE_FIELD(stop)
+        .describe("The ending value of the sequence");
+    DMLC_DECLARE_FIELD(num)
+        .describe("Number of samples to generate. Must be non-negative.");
+    DMLC_DECLARE_FIELD(endpoint)
+        .set_default(true)
+        .describe("If True, stop is the last sample. Otherwise, it is not 
included.");
+    DMLC_DECLARE_FIELD(base)
+        .set_default(10.0)
+        .describe("The base of the log space. The step size between the 
elements in "
+                  "ln(samples) / ln(base) (or log_base(samples)) is uniform.");
+    DMLC_DECLARE_FIELD(ctx)
+        .set_default("")
+        .describe("Context of output, in format [cpu|gpu|cpu_pinned](n)."
+                  "Only used for imperative calls.");
+    DMLC_DECLARE_FIELD(dtype).set_default(mshadow::kFloat32)
+            MXNET_ADD_ALL_TYPES
+        .describe("Target data type.");
+  }
+};
+
+struct logspace_fwd {
+  template<typename DType>
+  MSHADOW_XINLINE static void Map(index_t i, double start, double stop, double 
base,
+                                  double step, int req, DType* out) {
+    KERNEL_ASSIGN(out[i], req, static_cast<DType>(math::pow(base, 
static_cast<double>(start + step * i))));  // NOLINT(*)
+  }
+};
+
+template<typename xpu>
+void LogspaceCompute(const nnvm::NodeAttrs& attrs,
+                     const OpContext& ctx,
+                     const std::vector<TBlob>& inputs,
+                     const std::vector<OpReqType>& req,
+                     const std::vector<TBlob>& outputs) {
+  using namespace mxnet_op;
+  Stream<xpu> *s = ctx.get_stream<xpu>();
+  const LogspaceParam& param = nnvm::get<LogspaceParam>(attrs.parsed);
+  MSHADOW_TYPE_SWITCH(outputs[0].type_flag_, DType, {
+    int step_num = param.endpoint ? param.num - 1 : param.num;
+    double step = step_num > 0 ? (param.stop - param.start) / step_num : 0.0f;
+    Kernel<logspace_fwd, xpu>::Launch(s, outputs[0].Size(), param.start, 
param.stop, param.base,
+                                      step, req[0], outputs[0].dptr<DType>());
+  });
+}
+
+inline bool LogspaceShape(const nnvm::NodeAttrs& attrs,
 
 Review comment:
   put this function in `.cc` file.

----------------------------------------------------------------
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