Skip to content
This repository was archived by the owner on Nov 17, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions python/mxnet/ndarray/numpy/_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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]])

Expand All @@ -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')
Expand Down
53 changes: 53 additions & 0 deletions src/api/operator/numpy/np_broadcast_reduce_op_value.cc
Original file line number Diff line number Diff line change
@@ -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 <mxnet/api_registry.h>
#include <mxnet/runtime/packed_func.h>
#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<op::BroadcastToParam>(&attrs);

int num_outputs = 0;
NDArray* inputs[] = {args[0].operator mxnet::NDArray*()};
auto ndoutputs = Invoke(op, &attrs, 1, inputs, &num_outputs, nullptr);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 1 seems like a magic number. Better to use int num_inputs = 1 and call Invoke with num_inputs. Same for 3 other operators.

*ret = ndoutputs[0];
});

} // namespace mxnet
50 changes: 50 additions & 0 deletions src/api/operator/numpy/np_diff_op.cc
Original file line number Diff line number Diff line change
@@ -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 <mxnet/api_registry.h>
#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<op::DiffParam>(&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
49 changes: 49 additions & 0 deletions src/api/operator/numpy/np_matrix_op.cc
Original file line number Diff line number Diff line change
@@ -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 <mxnet/api_registry.h>
#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<op::ExpandDimParam>(&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
49 changes: 49 additions & 0 deletions src/api/operator/numpy/np_tril_op.cc
Original file line number Diff line number Diff line change
@@ -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 <mxnet/api_registry.h>
#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<op::TrilParam>(&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
10 changes: 8 additions & 2 deletions src/operator/numpy/np_diff-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <mxnet/base.h>
#include <mxnet/operator_util.h>
#include <vector>
#include <string>
#include "../mxnet_op.h"
#include "../operator_common.h"
#include "../tensor/broadcast_reduce_op.h"
Expand All @@ -37,8 +38,6 @@ namespace op {

struct DiffParam : public dmlc::Parameter<DiffParam> {
int n, axis;
dmlc::optional<mxnet::TShape> prepend;
dmlc::optional<mxnet::TShape> append;
DMLC_DECLARE_PARAMETER(DiffParam) {
DMLC_DECLARE_FIELD(n).set_default(1).describe(
"The number of times values are differenced."
Expand All @@ -47,6 +46,13 @@ struct DiffParam : public dmlc::Parameter<DiffParam> {
"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<std::string, std::string>* 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<int>* buffer, int n) {
Expand Down
6 changes: 6 additions & 0 deletions src/operator/numpy/np_tril_op-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#include <dmlc/parameter.h>
#include <vector>
#include <string>
#include <algorithm>
#include "../mxnet_op.h"
#include "../operator_common.h"
Expand All @@ -46,6 +47,11 @@ struct TrilParam : public dmlc::Parameter<TrilParam> {
"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<std::string, std::string>* dict) {
std::ostringstream k_s;
k_s << k;
(*dict)["k"] = k_s.str();
}
};

inline bool TrilOpShape(const nnvm::NodeAttrs& attrs,
Expand Down
5 changes: 5 additions & 0 deletions src/operator/tensor/broadcast_reduce_op.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ struct BroadcastToParam : public dmlc::Parameter<BroadcastToParam> {
" 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<std::string, std::string>* dict) {
std::ostringstream shape_s;
shape_s << shape;
(*dict)["shape"] = shape_s.str();
}
};

struct BroadcastLikeParam : public dmlc::Parameter<BroadcastLikeParam> {
Expand Down
7 changes: 7 additions & 0 deletions src/operator/tensor/matrix_op-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include <mxnet/operator_util.h>
#include <vector>
#include <string>
#include <algorithm>
#include <utility>
#include <type_traits>
Expand Down Expand Up @@ -497,6 +498,12 @@ struct ExpandDimParam : public dmlc::Parameter<ExpandDimParam> {
bool operator==(const ExpandDimParam &other) const {
return this->axis == other.axis;
}

void SetAttrDict(std::unordered_map<std::string, std::string>* dict) {
std::ostringstream axis_s;
axis_s << axis;
(*dict)["axis"] = axis_s.str();
}
};


Expand Down