From 91e84e4c2123b16fedc950838528354da5d4e715 Mon Sep 17 00:00:00 2001 From: JiangZhaoh Date: Mon, 16 Dec 2019 07:57:18 +0000 Subject: [PATCH] reuse existing function / add licenses / modify alignment --- src/operator/numpy/np_delete_op-inl.h | 120 +++++++++++--------------- src/operator/numpy/np_delete_op.cc | 23 ++++- src/operator/numpy/np_delete_op.cu | 19 ++++ 3 files changed, 89 insertions(+), 73 deletions(-) diff --git a/src/operator/numpy/np_delete_op-inl.h b/src/operator/numpy/np_delete_op-inl.h index 68ae09b5a42f..bcdb126f233f 100644 --- a/src/operator/numpy/np_delete_op-inl.h +++ b/src/operator/numpy/np_delete_op-inl.h @@ -1,3 +1,22 @@ +/* + * 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_delete_op-inl.h @@ -11,6 +30,7 @@ #include #include "../../common/utils.h" #include "../tensor/sort_op.h" +#include "../tensor/init_op.h" #include "../operator_common.h" #include "../mxnet_op.h" #include "../tensor/broadcast_reduce_op.h" @@ -64,19 +84,11 @@ struct SliceToIndices { } }; -template -struct AssignNum { - MSHADOW_XINLINE static void Map(int i, IType* output, const IType data) { - output[i] = data; - } -}; - struct IsDeleteCal { template MSHADOW_XINLINE static void Map(int i, int N, bool* is_delete, const IType* indices) { - if ((static_cast(indices[i]) >= 0) && - (static_cast(indices[i]) < N)) { - is_delete[static_cast(indices[i])] = true; + if ((indices[i] >= 0) && (indices[i] < N)) { + is_delete[static_cast(indices[i])] = true; } } }; @@ -98,39 +110,16 @@ struct OutPosCal { } }; -template -inline mshadow::Shape GetStride(const mxnet::TShape& shape) { - mshadow::Shapestride; - size_t tmp = 1; - for (int i = shape.ndim() - 1; i >= 0; --i) { - stride[i] = tmp; - tmp *= shape[i]; - } - return stride; -} - -template -inline mshadow::Shape GetKernelShape(const mxnet::TShape& shape) { - mshadow::Shapek_shape; - for (int i = 0 ; i < shape.ndim() ; ++i) { - k_shape[i] = shape[i]; - } - return k_shape; -} - -template +template struct DeleteImpl { /*! * \brief delete a sub-array from input along an axis according to 'is_delete'. - * \tparam xpu - cpu or gpu. * \param out_data - output: a new array with sub-arrays along an axis deleted. * \param in_arr - input: 'arr', original array. * \param is_delete - mark where will be deleted or be reminded in 'arr' * \param out_pos - if is_delete[i] is 'false', out_pos[i] indicates its. * \param arrshape - the shape of 'arr'. - * \param arr_stride - the stride of 'arr'. * \param out_stride - the stride of 'out_data'. - * \param out_ndim - the ndim of 'out_data'. * \param axis - delete sub-array along this axis */ template @@ -138,24 +127,14 @@ struct DeleteImpl { const DType* in_arr, const bool* is_delete, const int64_t* out_pos, - const mshadow::Shape<10> arrshape, - const mshadow::Shape<10> arr_stride, - const mshadow::Shape<10> out_stride, - const int out_ndim, const int axis) { - const int64_t arr_head = i / arr_stride[axis]; - const int64_t arr_mid = arr_head % arrshape[axis]; - mshadow::Shape<10> arr_idx; // i -> position in in_arr's shape - for (int j = 0; j < out_ndim; ++j) { - const int64_t head = i / arr_stride[j]; - const int64_t mid = head % arrshape[j]; - arr_idx[j] = mid; - } - if (!is_delete[arr_mid]) { - arr_idx[axis] = out_pos[arr_mid]; - int64_t dest_idx = 0; - for (int j =0; j < out_ndim; ++j) { - dest_idx += out_stride[j] * arr_idx[j]; - } + const mshadow::Shape arrshape, + const mshadow::Shape out_stride, + const int axis) { + // i -> position in in_arr's shape + mshadow::Shape arr_idx = mxnet_op::unravel(i, arrshape); + if (!is_delete[arr_idx[axis]]) { + arr_idx[axis] = out_pos[arr_idx[axis]]; + int64_t dest_idx = mxnet_op::dot(arr_idx, out_stride); KERNEL_ASSIGN(out_data[dest_idx], req, in_arr[i]); } } @@ -248,24 +227,23 @@ void NumpyDeleteCompute(const nnvm::NodeAttrs& attrs, } MSHADOW_TYPE_SWITCH(((inputs.size() == 2U) ? - inputs[delete_::kObj].dtype() : - mshadow::DataType::kFlag), IType, { + inputs[delete_::kObj].dtype() : + mshadow::DataType::kFlag), IType, { size_t temp_mem_size = sizeof(int64_t) * arr.shape()[axis] + sizeof(IType) * numtodel + sizeof(bool) * arr.shape()[axis]; Tensor temp_mem = - ctx.requested[0].get_space_typed(Shape1(temp_mem_size), s); + ctx.requested[0].get_space_typed(Shape1(temp_mem_size), s); int64_t* out_pos_ptr = reinterpret_cast(temp_mem.dptr_); IType* indices_ptr = reinterpret_cast (temp_mem.dptr_ + sizeof(int64_t) * arr.shape()[axis]); bool* is_delete_ptr = reinterpret_cast (temp_mem.dptr_ + sizeof(int64_t) * arr.shape()[axis] + - sizeof(IType) * numtodel); + sizeof(IType) * numtodel); if (param.step.has_value()) { - Kernel::Launch(s, numtodel, - indices_ptr, start, step); + Kernel::Launch(s, numtodel, indices_ptr, start, step); } else if (param.int_ind.has_value()) { - Kernel, xpu>::Launch(s, numtodel, indices_ptr, index); + Fill(s, TBlob(indices_ptr, Shape1(numtodel), xpu::kDevMask), kWriteTo, index); } else { mxnet_op::copy(s, TBlob(indices_ptr, inputs[delete_::kObj].shape(), inputs[delete_::kObj].data().dev_mask()), @@ -290,18 +268,18 @@ void NumpyDeleteCompute(const nnvm::NodeAttrs& attrs, newshape[axis] -= numtodel; const_cast(outputs[delete_::kOut]).Init(newshape); } - mshadow::Shape<10> arr_strides = GetStride<10>(arr.shape()); - mshadow::Shape<10> out_strides = GetStride<10>(newshape); - mshadow::Shape<10> k_arrshape = GetKernelShape<10>(arr.shape()); - MSHADOW_TYPE_SWITCH(outputs[delete_::kOut].dtype(), DType, { - MXNET_ASSIGN_REQ_SWITCH(req[delete_::kOut], req_type, { - Kernel, xpu>::Launch( - s, arr.shape().Size(), - outputs[delete_::kOut].data().dptr(), - arr.data().dptr(), - is_delete_ptr, out_pos_ptr, - k_arrshape, arr_strides, out_strides, - newshape.ndim(), axis); + MXNET_NDIM_SWITCH(newshape.ndim(), ndim, { + mshadow::Shape out_strides = mxnet_op::calc_stride(newshape.get()); + MSHADOW_TYPE_SWITCH(outputs[delete_::kOut].dtype(), DType, { + MXNET_ASSIGN_REQ_SWITCH(req[delete_::kOut], req_type, { + Kernel, xpu>::Launch( + s, arr.shape().Size(), + outputs[delete_::kOut].data().dptr(), + arr.data().dptr(), + is_delete_ptr, out_pos_ptr, + arr.shape().get(), + out_strides, axis); + }); }); }); }); diff --git a/src/operator/numpy/np_delete_op.cc b/src/operator/numpy/np_delete_op.cc index 11d0ea62a03f..48840bf9d230 100644 --- a/src/operator/numpy/np_delete_op.cc +++ b/src/operator/numpy/np_delete_op.cc @@ -1,3 +1,22 @@ +/* + * 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_delete_op.cc @@ -21,8 +40,8 @@ bool NumpyDeleteType(const nnvm::NodeAttrs& attrs, CHECK_EQ(out_type->size(), 1U); if (insize == 3) { CHECK_NE((*in_type)[1], -1) << "Index type must be set for insert operator\n"; - CHECK(((*in_type)[1] == mshadow::DataType::kFlag) - || ((*in_type)[1] == mshadow::DataType::kFlag)) + CHECK(((*in_type)[1] == mshadow::DataType::kFlag) || + ((*in_type)[1] == mshadow::DataType::kFlag)) << "Index type only support int32 or int64.\n"; } TYPE_ASSIGN_CHECK(*out_type, 0, (*in_type)[0]); diff --git a/src/operator/numpy/np_delete_op.cu b/src/operator/numpy/np_delete_op.cu index 7abb824fec3e..599d01788138 100644 --- a/src/operator/numpy/np_delete_op.cu +++ b/src/operator/numpy/np_delete_op.cu @@ -1,3 +1,22 @@ +/* + * 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_delete_op.cu