diff --git a/python/mxnet/ndarray/numpy/_op.py b/python/mxnet/ndarray/numpy/_op.py index 2d73699afff0..3371b419e328 100644 --- a/python/mxnet/ndarray/numpy/_op.py +++ b/python/mxnet/ndarray/numpy/_op.py @@ -304,7 +304,7 @@ def broadcast_to(array, shape): """ if _np.isscalar(array): return full(shape, array) - return _npi.broadcast_to(array, shape) + return _api_internal.broadcast_to(array, shape) @set_module('mxnet.ndarray.numpy') @@ -1834,7 +1834,7 @@ def expand_dims(a, axis): Output array. The number of dimensions is one greater than that of the input array. """ - return _npi.expand_dims(a, axis) + return _api_internal.expand_dims(a, axis) @set_module('mxnet.ndarray.numpy') @@ -1908,7 +1908,7 @@ def tril(m, k=0): [ 7., 8., 0.], [10., 11., 12.]]) """ - return _npi.tril(m, k) + return _api_internal.tril(m, k) def _unary_func_helper(x, fn_array, fn_scalar, out=None, **kwargs): @@ -6837,7 +6837,7 @@ def diff(a, n=1, axis=-1, prepend=None, append=None): # pylint: disable=redefin >>> x = np.array([[1, 3, 6, 10], [0, 5, 6, 8]]) >>> np.diff(x) array([[2, 3, 4], - [5, 1, 2]]) + [5, 1, 2]]) >>> np.diff(x, axis=0) array([[-1, 2, 0, -2]]) @@ -6847,7 +6847,7 @@ def diff(a, n=1, axis=-1, prepend=None, append=None): # pylint: disable=redefin """ if (prepend or append): raise NotImplementedError('prepend and append options are not supported yet') - return _npi.diff(a, n=n, axis=axis) + return _api_internal.diff(a, n, axis) @set_module('mxnet.ndarray.numpy') diff --git a/src/api/operator/numpy/np_broadcast_reduce_op_value.cc b/src/api/operator/numpy/np_broadcast_reduce_op_value.cc new file mode 100644 index 000000000000..2322860aa609 --- /dev/null +++ b/src/api/operator/numpy/np_broadcast_reduce_op_value.cc @@ -0,0 +1,53 @@ +/* + * 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 broadcast_reduce_op_value.cc + * \brief Implementation of the API of functions in + * src/operator/tensor/np_broadcast_reduce_op_value.cc + */ +#include +#include +#include "../utils.h" +#include "../../../operator/tensor/broadcast_reduce_op.h" + +namespace mxnet { + +MXNET_REGISTER_API("_npi.broadcast_to") +.set_body([](runtime::MXNetArgs args, runtime::MXNetRetValue* ret) { + using namespace runtime; + const nnvm::Op* op = Op::Get("_npi_broadcast_to"); + nnvm::NodeAttrs attrs; + op::BroadcastToParam param; + if (args[1].type_code() == kDLInt) { + param.shape = TShape(1, args[1].operator int64_t()); + } else { + param.shape = TShape(args[1].operator ObjectRef()); + } + attrs.parsed = std::move(param); + attrs.op = op; + SetAttrDict(&attrs); + + int num_outputs = 0; + NDArray* inputs[] = {args[0].operator mxnet::NDArray*()}; + auto ndoutputs = Invoke(op, &attrs, 1, inputs, &num_outputs, nullptr); + *ret = ndoutputs[0]; +}); + +} // namespace mxnet diff --git a/src/api/operator/numpy/np_diff_op.cc b/src/api/operator/numpy/np_diff_op.cc new file mode 100644 index 000000000000..dec73b8496a7 --- /dev/null +++ b/src/api/operator/numpy/np_diff_op.cc @@ -0,0 +1,50 @@ +/* + * 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_diff_op.cc + * \brief Implementation of the API of functions in src/operator/numpy/np_diff.cc + */ +#include +#include "../utils.h" +#include "../../../operator/numpy/np_diff-inl.h" + +namespace mxnet { + +MXNET_REGISTER_API("_npi.diff") +.set_body([](runtime::MXNetArgs args, runtime::MXNetRetValue* ret) { + using namespace runtime; + const nnvm::Op* op = Op::Get("_npi_diff"); + nnvm::NodeAttrs attrs; + op::DiffParam param; + param.n = args[1].operator int(); + param.axis = args[2].operator int(); + + // we directly copy DiffParam, which is trivially-copyable + attrs.parsed = param; + attrs.op = op; + SetAttrDict(&attrs); + + int num_outputs = 0; + NDArray* inputs[] = {args[0].operator mxnet::NDArray*()}; + auto ndoutputs = Invoke(op, &attrs, 1, inputs, &num_outputs, nullptr); + *ret = ndoutputs[0]; +}); + +} // namespace mxnet diff --git a/src/api/operator/numpy/np_matrix_op.cc b/src/api/operator/numpy/np_matrix_op.cc new file mode 100644 index 000000000000..080cca867ecb --- /dev/null +++ b/src/api/operator/numpy/np_matrix_op.cc @@ -0,0 +1,49 @@ +/* + * 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_matrix_op.cc + * \brief Implementation of the API of functions in src/operator/tensor/matrix_op.cc + */ +#include +#include "../utils.h" +#include "../../../operator/tensor/matrix_op-inl.h" + +namespace mxnet { + +MXNET_REGISTER_API("_npi.expand_dims") +.set_body([](runtime::MXNetArgs args, runtime::MXNetRetValue* ret) { + using namespace runtime; + const nnvm::Op* op = Op::Get("_npi_expand_dims"); + nnvm::NodeAttrs attrs; + op::ExpandDimParam param; + param.axis = args[1].operator int(); + + // we directly copy ExpandDimParam, which is trivially-copyable + attrs.parsed = param; + attrs.op = op; + SetAttrDict(&attrs); + + int num_outputs = 0; + NDArray* inputs[] = {args[0].operator mxnet::NDArray*()}; + auto ndoutputs = Invoke(op, &attrs, 1, inputs, &num_outputs, nullptr); + *ret = ndoutputs[0]; +}); + +} // namespace mxnet diff --git a/src/api/operator/numpy/np_tril_op.cc b/src/api/operator/numpy/np_tril_op.cc new file mode 100644 index 000000000000..105ff58bf559 --- /dev/null +++ b/src/api/operator/numpy/np_tril_op.cc @@ -0,0 +1,49 @@ +/* + * 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_tril_op.cc + * \brief Implementation of the API of functions in src/operator/numpy/np_diff.cc + */ +#include +#include "../utils.h" +#include "../../../operator/numpy/np_tril_op-inl.h" + +namespace mxnet { + +MXNET_REGISTER_API("_npi.tril") +.set_body([](runtime::MXNetArgs args, runtime::MXNetRetValue* ret) { + using namespace runtime; + const nnvm::Op* op = Op::Get("_npi_tril"); + nnvm::NodeAttrs attrs; + op::TrilParam param; + param.k = args[1].operator int(); + + // we directly copy TrilParam, which is trivially-copyable + attrs.parsed = param; + attrs.op = op; + SetAttrDict(&attrs); + + int num_outputs = 0; + NDArray* inputs[] = {args[0].operator mxnet::NDArray*()}; + auto ndoutputs = Invoke(op, &attrs, 1, inputs, &num_outputs, nullptr); + *ret = ndoutputs[0]; +}); + +} // namespace mxnet diff --git a/src/operator/numpy/np_diff-inl.h b/src/operator/numpy/np_diff-inl.h index 69f175e802dd..8a8bc558962a 100644 --- a/src/operator/numpy/np_diff-inl.h +++ b/src/operator/numpy/np_diff-inl.h @@ -28,6 +28,7 @@ #include #include #include +#include #include "../mxnet_op.h" #include "../operator_common.h" #include "../tensor/broadcast_reduce_op.h" @@ -37,8 +38,6 @@ namespace op { struct DiffParam : public dmlc::Parameter { int n, axis; - dmlc::optional prepend; - dmlc::optional append; DMLC_DECLARE_PARAMETER(DiffParam) { DMLC_DECLARE_FIELD(n).set_default(1).describe( "The number of times values are differenced." @@ -47,6 +46,13 @@ struct DiffParam : public dmlc::Parameter { "Axis along which the cumulative sum is computed." " The default (None) is to compute the diff over the flattened array."); } + void SetAttrDict(std::unordered_map* dict) { + std::ostringstream n_s, axis_s; + n_s << n; + axis_s << axis; + (*dict)["n"] = n_s.str(); + (*dict)["axis"] = axis_s.str(); + } }; inline void YanghuiTri(std::vector* buffer, int n) { diff --git a/src/operator/numpy/np_tril_op-inl.h b/src/operator/numpy/np_tril_op-inl.h index 1ad74e887b6c..50943a09caa2 100644 --- a/src/operator/numpy/np_tril_op-inl.h +++ b/src/operator/numpy/np_tril_op-inl.h @@ -28,6 +28,7 @@ #include #include +#include #include #include "../mxnet_op.h" #include "../operator_common.h" @@ -46,6 +47,11 @@ struct TrilParam : public dmlc::Parameter { "and k<0 for diagonals below the main diagonal. " "If input has shape (S0 S1) k must be between -S0 and S1"); } + void SetAttrDict(std::unordered_map* dict) { + std::ostringstream k_s; + k_s << k; + (*dict)["k"] = k_s.str(); + } }; inline bool TrilOpShape(const nnvm::NodeAttrs& attrs, diff --git a/src/operator/tensor/broadcast_reduce_op.h b/src/operator/tensor/broadcast_reduce_op.h index 5eb0c41aa36c..b06442932a78 100644 --- a/src/operator/tensor/broadcast_reduce_op.h +++ b/src/operator/tensor/broadcast_reduce_op.h @@ -156,6 +156,11 @@ struct BroadcastToParam : public dmlc::Parameter { " E.g `A = broadcast_to(B, shape=(10, 0, 0))` " "has the same meaning as `A = broadcast_axis(B, axis=0, size=10)`."); } + void SetAttrDict(std::unordered_map* dict) { + std::ostringstream shape_s; + shape_s << shape; + (*dict)["shape"] = shape_s.str(); + } }; struct BroadcastLikeParam : public dmlc::Parameter { diff --git a/src/operator/tensor/matrix_op-inl.h b/src/operator/tensor/matrix_op-inl.h index fa7b8a10b212..670104bcfdb0 100644 --- a/src/operator/tensor/matrix_op-inl.h +++ b/src/operator/tensor/matrix_op-inl.h @@ -27,6 +27,7 @@ #include #include +#include #include #include #include @@ -497,6 +498,12 @@ struct ExpandDimParam : public dmlc::Parameter { bool operator==(const ExpandDimParam &other) const { return this->axis == other.axis; } + + void SetAttrDict(std::unordered_map* dict) { + std::ostringstream axis_s; + axis_s << axis; + (*dict)["axis"] = axis_s.str(); + } };