You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2020/01/15 07:53:59 UTC

[GitHub] [incubator-mxnet] AntiZpvoh opened a new pull request #17316: [NumPy] add op random.laplace

AntiZpvoh opened a new pull request #17316: [NumPy] add op random.laplace
URL: https://github.com/apache/incubator-mxnet/pull/17316
 
 
   ## Description ##
   Add op random.laplace, which could generate random samples with Laplace Distribution.
   
   ## Checklist ##
   ### Essentials ###
   Please feel free to remove inapplicable items for your PR.
   - [ ] The PR title starts with [MXNET-$JIRA_ID], where $JIRA_ID refers to the relevant [JIRA issue](https://issues.apache.org/jira/projects/MXNET/issues) created (except PRs with tiny changes)
   - [ ] Changes are complete (i.e. I finished coding on this PR)
   - [ ] All changes have test coverage:
   - Unit tests are added for small changes to verify correctness (e.g. adding a new operator)
   - Nightly tests are added for complicated/long-running ones (e.g. changing distributed kvstore)
   - Build tests will be added for build configuration changes (e.g. adding a new build option with NCCL)
   - [ ] Code is well-documented: 
   - For user-facing API changes, API doc string has been updated. 
   - For new C++ functions in header files, their functionalities and arguments are documented. 
   - For new examples, README.md is added to explain the what the example does, the source of the dataset, expected performance on test set and reference to the original paper if applicable
   - Check the API doc at https://mxnet-ci-doc.s3-accelerate.dualstack.amazonaws.com/PR-$PR_ID/$BUILD_ID/index.html
   - [ ] To the best of my knowledge, examples are either not affected by this change, or have been fixed to be compatible with this change
   
   ### Changes ###
   - [ ] Feature1, tests, (and when applicable, API doc)
   - [ ] Feature2, tests, (and when applicable, API doc)
   
   ## Comments ##
   - If this change is a backward incompatible change, why must this change be made.
   - Interesting edge cases to note here
   

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


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] xidulu commented on a change in pull request #17316: [NumPy] add op random.laplace

Posted by GitBox <gi...@apache.org>.
xidulu commented on a change in pull request #17316: [NumPy] add op random.laplace
URL: https://github.com/apache/incubator-mxnet/pull/17316#discussion_r366736430
 
 

 ##########
 File path: src/operator/numpy/random/np_laplace_op.cu
 ##########
 @@ -0,0 +1,35 @@
+/*
+ * 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_laplace_op.cu
+ * \brief Operator for numpy sampling from Laplace distributions
+ */
+
+#include "./np_laplace_op.h"
+
+namespace mxnet {
+namespace op {
+
+NNVM_REGISTER_OP(_npi_laplace)
+.set_attr<FCompute>("FCompute<gpu>", NumpyLaplaceForward<gpu>);
+
 
 Review comment:
   Don't forget to register the GPU version of `NNVM_REGISTER_OP(_npi_laplace_n)` .

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


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] haojin2 commented on a change in pull request #17316: [NumPy] add op random.laplace

Posted by GitBox <gi...@apache.org>.
haojin2 commented on a change in pull request #17316: [NumPy] add op random.laplace
URL: https://github.com/apache/incubator-mxnet/pull/17316#discussion_r366766965
 
 

 ##########
 File path: python/mxnet/symbol/numpy/random.py
 ##########
 @@ -290,6 +290,54 @@ def choice(a, size=None, replace=True, p=None, ctx=None, out=None):
             return _npi.choice(p, a=a, size=size, replace=replace, ctx=ctx, weighted=True, out=out)
 
 
+def laplace(loc=0.0, scale=1.0, size=None, dtype=None, ctx=None, out=None):
+    r"""Draw random samples from a Laplace distribution.
+
+    Samples are distributed according to a Laplace distribution parametrized
+    by *loc* (mean) and *scale* (the exponential decay).
+
+    Parameters
+    ----------
+    loc : float, The position of the distribution peak.
+
+    scale : float, the exponential decay.
+
+    size : int or tuple of ints, optional. 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.
+
+    dtype : {'float16', 'float32', 'float64'}, optional
+        Data type of output samples. Default is 'float32'
+    ctx : Context, optional
+        Device context of output. Default is current context.
+    out : ``ndarray``, optional
+        Store output to an existing ``ndarray``.
+
+    Returns
+    -------
+    out : _Symbol (symbol representing `mxnet.numpy.ndarray` in computational graphs)
+        Drawn samples from the parameterized Laplace distribution.
+    """
+    from ._symbol import _Symbol as np_symbol
+    input_type = (isinstance(loc, np_symbol), isinstance(scale, np_symbol))
+    if dtype is None:
+        dtype = 'float32'
+    if ctx is None:
+        ctx = current_context()
+    if size == ():
+        size = None
+    if input_type == (True, True):
+        return _npi.laplace(loc, scale, loc=None, scale=None, size=size,
+                           ctx=ctx, dtype=dtype, out=out)
 
 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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] haojin2 commented on a change in pull request #17316: [NumPy] add op random.laplace

Posted by GitBox <gi...@apache.org>.
haojin2 commented on a change in pull request #17316: [NumPy] add op random.laplace
URL: https://github.com/apache/incubator-mxnet/pull/17316#discussion_r366767024
 
 

 ##########
 File path: python/mxnet/symbol/numpy/random.py
 ##########
 @@ -290,6 +290,54 @@ def choice(a, size=None, replace=True, p=None, ctx=None, out=None):
             return _npi.choice(p, a=a, size=size, replace=replace, ctx=ctx, weighted=True, out=out)
 
 
+def laplace(loc=0.0, scale=1.0, size=None, dtype=None, ctx=None, out=None):
+    r"""Draw random samples from a Laplace distribution.
+
+    Samples are distributed according to a Laplace distribution parametrized
+    by *loc* (mean) and *scale* (the exponential decay).
+
+    Parameters
+    ----------
+    loc : float, The position of the distribution peak.
+
+    scale : float, the exponential decay.
+
+    size : int or tuple of ints, optional. 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.
+
+    dtype : {'float16', 'float32', 'float64'}, optional
+        Data type of output samples. Default is 'float32'
+    ctx : Context, optional
+        Device context of output. Default is current context.
+    out : ``ndarray``, optional
+        Store output to an existing ``ndarray``.
+
+    Returns
+    -------
+    out : _Symbol (symbol representing `mxnet.numpy.ndarray` in computational graphs)
+        Drawn samples from the parameterized Laplace distribution.
+    """
+    from ._symbol import _Symbol as np_symbol
+    input_type = (isinstance(loc, np_symbol), isinstance(scale, np_symbol))
+    if dtype is None:
+        dtype = 'float32'
+    if ctx is None:
+        ctx = current_context()
+    if size == ():
+        size = None
+    if input_type == (True, True):
+        return _npi.laplace(loc, scale, loc=None, scale=None, size=size,
+                           ctx=ctx, dtype=dtype, out=out)
+    elif input_type == (False, True):
+        return _npi.laplace(scale, loc=loc, scale=None, size=size,
+                           ctx=ctx, dtype=dtype, out=out)
 
 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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] xidulu commented on a change in pull request #17316: [NumPy] add op random.laplace

Posted by GitBox <gi...@apache.org>.
xidulu commented on a change in pull request #17316: [NumPy] add op random.laplace
URL: https://github.com/apache/incubator-mxnet/pull/17316#discussion_r366735999
 
 

 ##########
 File path: src/operator/numpy/random/np_laplace_op.cc
 ##########
 @@ -0,0 +1,105 @@
+/*
+ * 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_laplace_op.cc
+ * \brief Operator for numpy sampling from Laplace distributions
+ */
+#include "./np_laplace_op.h"
+
+namespace mxnet {
+namespace op {
+
+DMLC_REGISTER_PARAMETER(NumpyLaplaceParam);
+
+NNVM_REGISTER_OP(_npi_laplace)
+.describe("numpy behavior Laplace")
+.set_num_inputs(
+  [](const nnvm::NodeAttrs& attrs) {
+    const NumpyLaplaceParam& param = nnvm::get<NumpyLaplaceParam>(attrs.parsed);
+    int num_inputs = 2;
+    if (param.loc.has_value()) num_inputs -= 1;
+    if (param.scale.has_value()) num_inputs -= 1;
+    return num_inputs;
+  }
+)
+.set_num_outputs(1)
+.set_attr<nnvm::FListInputNames>("FListInputNames",
+  [](const NodeAttrs& attrs) {
+    const NumpyLaplaceParam& param = nnvm::get<NumpyLaplaceParam>(attrs.parsed);
+    int num_inputs = 2;
+    if (param.loc.has_value()) num_inputs -= 1;
+    if (param.scale.has_value()) num_inputs -= 1;
+    if (num_inputs == 0) return std::vector<std::string>();
+    if (num_inputs == 1) return std::vector<std::string>{"input1"};
+    return std::vector<std::string>{"input1", "input2"};
+  })
+.set_attr_parser(ParamParser<NumpyLaplaceParam>)
+.set_attr<mxnet::FInferShape>("FInferShape", TwoparamsDistOpShape<NumpyLaplaceParam>)
+.set_attr<nnvm::FInferType>("FInferType", NumpyLaplaceOpType)
+.set_attr<FResourceRequest>("FResourceRequest",
+  [](const nnvm::NodeAttrs& attrs) {
+      return std::vector<ResourceRequest>{
+        ResourceRequest::kRandom, ResourceRequest::kTempSpace};
+  })
+.set_attr<FCompute>("FCompute<cpu>", NumpyLaplaceForward<cpu>)
+.set_attr<nnvm::FGradient>("FGradient", MakeZeroGradNodes)
+.add_argument("input1", "NDArray-or-Symbol", "Source input")
+.add_argument("input2", "NDArray-or-Symbol", "Source input")
+.add_arguments(NumpyLaplaceParam::__FIELDS__());
+
+NNVM_REGISTER_OP(_npi_laplace_n)
 
 Review comment:
   If you register the `sample_n` version of `laplace` in the backend, you should implement the corresponding frontend under the numpy extension namespace, you could refer to `normal_n` and `uniform_n` in https://github.com/apache/incubator-mxnet/blob/master/python/mxnet/numpy_extension/random.py

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


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] haojin2 commented on a change in pull request #17316: [NumPy] add op random.laplace

Posted by GitBox <gi...@apache.org>.
haojin2 commented on a change in pull request #17316: [NumPy] add op random.laplace
URL: https://github.com/apache/incubator-mxnet/pull/17316#discussion_r366767278
 
 

 ##########
 File path: src/operator/numpy/random/np_laplace_op.h
 ##########
 @@ -0,0 +1,218 @@
+/*
+ * 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_laplace_op.h
+ * \brief Operator for numpy sampling from Laplace distributions
+ */
+#ifndef MXNET_OPERATOR_NUMPY_RANDOM_NP_LAPLACE_OP_H_
+#define MXNET_OPERATOR_NUMPY_RANDOM_NP_LAPLACE_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 NumpyLaplaceParam : public dmlc::Parameter<NumpyLaplaceParam> {
+  dmlc::optional<float> loc;
+  dmlc::optional<float> scale;
+  std::string ctx;
+  int dtype;
+  dmlc::optional<mxnet::Tuple<int>> size;
+  DMLC_DECLARE_PARAMETER(NumpyLaplaceParam) {
+    DMLC_DECLARE_FIELD(loc);
+    DMLC_DECLARE_FIELD(scale);
+    DMLC_DECLARE_FIELD(size)
+        .set_default(dmlc::optional<mxnet::Tuple<int>>())
 
 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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] haojin2 commented on a change in pull request #17316: [NumPy] add op random.laplace

Posted by GitBox <gi...@apache.org>.
haojin2 commented on a change in pull request #17316: [NumPy] add op random.laplace
URL: https://github.com/apache/incubator-mxnet/pull/17316#discussion_r366767094
 
 

 ##########
 File path: python/mxnet/symbol/numpy/random.py
 ##########
 @@ -290,6 +290,54 @@ def choice(a, size=None, replace=True, p=None, ctx=None, out=None):
             return _npi.choice(p, a=a, size=size, replace=replace, ctx=ctx, weighted=True, out=out)
 
 
+def laplace(loc=0.0, scale=1.0, size=None, dtype=None, ctx=None, out=None):
+    r"""Draw random samples from a Laplace distribution.
+
+    Samples are distributed according to a Laplace distribution parametrized
+    by *loc* (mean) and *scale* (the exponential decay).
+
+    Parameters
+    ----------
+    loc : float, The position of the distribution peak.
+
+    scale : float, the exponential decay.
+
+    size : int or tuple of ints, optional. 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.
+
+    dtype : {'float16', 'float32', 'float64'}, optional
+        Data type of output samples. Default is 'float32'
+    ctx : Context, optional
+        Device context of output. Default is current context.
+    out : ``ndarray``, optional
+        Store output to an existing ``ndarray``.
+
+    Returns
+    -------
+    out : _Symbol (symbol representing `mxnet.numpy.ndarray` in computational graphs)
+        Drawn samples from the parameterized Laplace distribution.
+    """
+    from ._symbol import _Symbol as np_symbol
+    input_type = (isinstance(loc, np_symbol), isinstance(scale, np_symbol))
+    if dtype is None:
+        dtype = 'float32'
+    if ctx is None:
+        ctx = current_context()
+    if size == ():
+        size = None
+    if input_type == (True, True):
+        return _npi.laplace(loc, scale, loc=None, scale=None, size=size,
+                           ctx=ctx, dtype=dtype, out=out)
+    elif input_type == (False, True):
+        return _npi.laplace(scale, loc=loc, scale=None, size=size,
+                           ctx=ctx, dtype=dtype, out=out)
+    elif input_type == (True, False):
+        return _npi.laplace(loc, loc=None, scale=scale, size=size,
+                           ctx=ctx, dtype=dtype, out=out)
+    else:
+        return _npi.laplace(loc=loc, scale=scale, size=size,
+                           ctx=ctx, dtype=dtype, out=out)
 
 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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] haojin2 commented on a change in pull request #17316: [NumPy] add op random.laplace

Posted by GitBox <gi...@apache.org>.
haojin2 commented on a change in pull request #17316: [NumPy] add op random.laplace
URL: https://github.com/apache/incubator-mxnet/pull/17316#discussion_r366767071
 
 

 ##########
 File path: python/mxnet/symbol/numpy/random.py
 ##########
 @@ -290,6 +290,54 @@ def choice(a, size=None, replace=True, p=None, ctx=None, out=None):
             return _npi.choice(p, a=a, size=size, replace=replace, ctx=ctx, weighted=True, out=out)
 
 
+def laplace(loc=0.0, scale=1.0, size=None, dtype=None, ctx=None, out=None):
+    r"""Draw random samples from a Laplace distribution.
+
+    Samples are distributed according to a Laplace distribution parametrized
+    by *loc* (mean) and *scale* (the exponential decay).
+
+    Parameters
+    ----------
+    loc : float, The position of the distribution peak.
+
+    scale : float, the exponential decay.
+
+    size : int or tuple of ints, optional. 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.
+
+    dtype : {'float16', 'float32', 'float64'}, optional
+        Data type of output samples. Default is 'float32'
+    ctx : Context, optional
+        Device context of output. Default is current context.
+    out : ``ndarray``, optional
+        Store output to an existing ``ndarray``.
+
+    Returns
+    -------
+    out : _Symbol (symbol representing `mxnet.numpy.ndarray` in computational graphs)
+        Drawn samples from the parameterized Laplace distribution.
+    """
+    from ._symbol import _Symbol as np_symbol
+    input_type = (isinstance(loc, np_symbol), isinstance(scale, np_symbol))
+    if dtype is None:
+        dtype = 'float32'
+    if ctx is None:
+        ctx = current_context()
+    if size == ():
+        size = None
+    if input_type == (True, True):
+        return _npi.laplace(loc, scale, loc=None, scale=None, size=size,
+                           ctx=ctx, dtype=dtype, out=out)
+    elif input_type == (False, True):
+        return _npi.laplace(scale, loc=loc, scale=None, size=size,
+                           ctx=ctx, dtype=dtype, out=out)
+    elif input_type == (True, False):
+        return _npi.laplace(loc, loc=None, scale=scale, size=size,
+                           ctx=ctx, dtype=dtype, out=out)
 
 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:
users@infra.apache.org


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] xidulu commented on a change in pull request #17316: [NumPy] add op random.laplace

Posted by GitBox <gi...@apache.org>.
xidulu commented on a change in pull request #17316: [NumPy] add op random.laplace
URL: https://github.com/apache/incubator-mxnet/pull/17316#discussion_r392263662
 
 

 ##########
 File path: src/api/operator/numpy/random/np_laplace_op.cc
 ##########
 @@ -0,0 +1,99 @@
+/*
+ * 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.
+ */
+
+/*!
+ * \file np_init_op.cc
+ * \brief Implementation of the API of functions in src/operator/numpy/np_init_op.cc
 
 Review comment:
   np_laplace_op.cc

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


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] haojin2 merged pull request #17316: [NumPy] add op random.laplace

Posted by GitBox <gi...@apache.org>.
haojin2 merged pull request #17316: [NumPy] add op random.laplace
URL: https://github.com/apache/incubator-mxnet/pull/17316
 
 
   

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


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] xidulu commented on issue #17316: [NumPy] add op random.laplace

Posted by GitBox <gi...@apache.org>.
xidulu commented on issue #17316: [NumPy] add op random.laplace
URL: https://github.com/apache/incubator-mxnet/pull/17316#issuecomment-598751830
 
 
   LGTM regarding the logic.

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


With regards,
Apache Git Services

[GitHub] [incubator-mxnet] xidulu commented on a change in pull request #17316: [NumPy] add op random.laplace

Posted by GitBox <gi...@apache.org>.
xidulu commented on a change in pull request #17316: [NumPy] add op random.laplace
URL: https://github.com/apache/incubator-mxnet/pull/17316#discussion_r392265525
 
 

 ##########
 File path: src/operator/numpy/random/np_laplace_op.h
 ##########
 @@ -0,0 +1,232 @@
+/*
+ * 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_laplace_op.h
+ * \brief Operator for numpy sampling from Laplace distributions
+ */
+#ifndef MXNET_OPERATOR_NUMPY_RANDOM_NP_LAPLACE_OP_H_
+#define MXNET_OPERATOR_NUMPY_RANDOM_NP_LAPLACE_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 NumpyLaplaceParam : public dmlc::Parameter<NumpyLaplaceParam> {
+  dmlc::optional<float> loc;
+  dmlc::optional<float> scale;
+  std::string ctx;
+  int dtype;
+  dmlc::optional<mxnet::Tuple<int>> size;
+  DMLC_DECLARE_PARAMETER(NumpyLaplaceParam) {
+    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.");
+    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("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).");
+  }
+
+  void SetAttrDict(std::unordered_map<std::string, std::string>* dict) {
+    std::ostringstream loc_s, scale_s, size_s, dtype_s;
+    loc_s << loc;
+    scale_s << scale;
+    size_s << size;
+    dtype_s << dtype;
+    (*dict)["loc"] = loc_s.str();
+    (*dict)["scale"] = scale_s.str();
+    (*dict)["size"] = size_s.str();
+    (*dict)["dtype"] = dtype_s.str();
+    // We do not set ctx, because ctx has been set in dict instead of InitOpParam.
+    // Setting ctx here results in an error.
+  }
+};
+
+inline bool NumpyLaplaceOpType(const nnvm::NodeAttrs &attrs,
+                               std::vector<int> *in_attrs,
+                               std::vector<int> *out_attrs) {
+  const NumpyLaplaceParam &param = nnvm::get<NumpyLaplaceParam>(attrs.parsed);
+  int otype = param.dtype;
+  if (otype != -1) {
+    (*out_attrs)[0] = otype;
+  } else {
+    (*out_attrs)[0] = mshadow::kFloat32;
+  }
+  return true;
+}
+
+namespace mxnet_op {
+template <int ndim, typename IType, typename OType>
+struct laplace_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 *uniforms, 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];
+    if (uniforms[i] < 0.5) {
+      out[i] = loc_value + scale_value * log(2 * uniforms[i]);
+    } else {
+      out[i] = loc_value - scale_value * log(2 * (1 - uniforms[i]));
+    }
+  }
+};
+
+template <int ndim, typename IType, typename OType>
+struct laplace_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 *uniforms, 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;
+    }
+    if (uniforms[i] < 0.5) {
+      out[i] = loc_value + scale_value * log(2 * uniforms[i]);
+    } else {
+      out[i] = loc_value - scale_value * log(2 * (1 - uniforms[i]));
+    }
+  }
+};
+
+template <typename OType>
+struct laplace_two_scalar_kernel {
+  MSHADOW_XINLINE static void Map(index_t i, float loc, float scale,
+                                  float *uniforms, OType *out) {
+    if (uniforms[i] < 0.5) {
+      out[i] = loc + scale * log(2 * uniforms[i]);
+    } else {
+      out[i] = loc - scale * log(2 * (1 - uniforms[i]));
+    }
+  }
+};
+}  // namespace mxnet_op
+
+template <typename xpu>
+void NumpyLaplaceForward(const nnvm::NodeAttrs &attrs,
+                         const OpContext &ctx,
+                         const std::vector<TBlob> &inputs,
+                         const std::vector<OpReqType> &req,
+                         const std::vector<TBlob> &outputs) {
+  using namespace mshadow;
+  using namespace mxnet_op;
+  const NumpyLaplaceParam &param = nnvm::get<NumpyLaplaceParam>(attrs.parsed);
+  CHECK_EQ(outputs.size(), 1);
+  Stream<xpu> *s = ctx.get_stream<xpu>();
+
+  // Generate base random number.
+  Random<xpu, float> *prnd = ctx.requested[0].get_random<xpu, float>(s);
+  Tensor<xpu, 1, float> laplace_tensor =
+      ctx.requested[1].get_space_typed<xpu, 1, float>(Shape1(outputs[0].Size()),
+                                                      s);
+  prnd->SampleUniform(&laplace_tensor, 0, 1);
+  mxnet::TShape new_lshape, new_hshape, new_oshape;
+
+  // [scalar scalar] case
+  if (inputs.size() == 0U) {
+    MSHADOW_TYPE_SWITCH(outputs[0].type_flag_, OType, {
+      Kernel<laplace_two_scalar_kernel<OType>, xpu>::Launch(
+          s, outputs[0].Size(), param.loc.value(), param.scale.value(),
+          laplace_tensor.dptr_, outputs[0].dptr<OType>());
+    });
+  } else if (inputs.size() == 1U) {
+    // [scalar tensor], [tensor scalar] case
+    int ndim = FillShape(inputs[0].shape_, inputs[0].shape_, outputs[0].shape_,
+                         &new_lshape, &new_lshape, &new_oshape);
+    int scalar_pos;
+    float scalar_value;
+    // int type_flag = param.t;
 
 Review comment:
   remove unused code

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


With regards,
Apache Git Services