1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *   http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied.  See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 /*!
21  * \file np_delete_op.cc
22  * \brief CPU Implementation of numpy insert operations
23  */
24 
25 #include <vector>
26 #include "./np_delete_op-inl.h"
27 
28 namespace mxnet {
29 namespace op {
30 
31 DMLC_REGISTER_PARAMETER(NumpyDeleteParam);
32 
NumpyDeleteType(const nnvm::NodeAttrs & attrs,std::vector<int> * in_type,std::vector<int> * out_type)33 bool NumpyDeleteType(const nnvm::NodeAttrs& attrs,
34                      std::vector<int> *in_type,
35                      std::vector<int> *out_type) {
36   const NumpyDeleteParam& param = nnvm::get<NumpyDeleteParam>(attrs.parsed);
37   int insize = (param.step.has_value() || param.int_ind.has_value()) ? 1 : 2;
38   CHECK_EQ(in_type->size(), insize);
39   CHECK_EQ(out_type->size(), 1U);
40   if (insize == 3) {
41     CHECK_NE((*in_type)[1], -1) << "Index type must be set for insert operator\n";
42     CHECK(((*in_type)[1] == mshadow::DataType<int64_t>::kFlag) ||
43           ((*in_type)[1] == mshadow::DataType<int32_t>::kFlag))
44       << "Index type only support int32 or int64.\n";
45   }
46   TYPE_ASSIGN_CHECK(*out_type, 0, (*in_type)[0]);
47   TYPE_ASSIGN_CHECK(*in_type, 0, (*out_type)[0]);
48   return (*in_type)[0] != -1;
49 }
50 
NumpyDeleteStorageType(const nnvm::NodeAttrs & attrs,const int dev_mask,DispatchMode * dispatch_mode,std::vector<int> * in_attrs,std::vector<int> * out_attrs)51 inline bool NumpyDeleteStorageType(const nnvm::NodeAttrs& attrs,
52                                    const int dev_mask,
53                                    DispatchMode* dispatch_mode,
54                                    std::vector<int> *in_attrs,
55                                    std::vector<int> *out_attrs) {
56   const NumpyDeleteParam& param = nnvm::get<NumpyDeleteParam>(attrs.parsed);
57   unsigned int insize = (param.step.has_value() || param.int_ind.has_value()) ? 1U : 2U;
58   CHECK_EQ(in_attrs->size(), insize);
59   CHECK_EQ(out_attrs->size(), 1U);
60   for (int &attr : *in_attrs) {
61     CHECK_EQ(attr, kDefaultStorage) << "Only default storage is supported";
62   }
63   for (int &attr : *out_attrs) {
64     attr = kDefaultStorage;
65   }
66   *dispatch_mode = DispatchMode::kFComputeEx;
67   return true;
68 }
69 
70 NNVM_REGISTER_OP(_npi_delete)
71 .describe(R"code(Delete values along the given axis before the given indices.)code" ADD_FILELINE)
72 .set_attr_parser(ParamParser<NumpyDeleteParam>)
__anon39f25dc40102(const NodeAttrs& attrs) 73 .set_num_inputs([](const NodeAttrs& attrs) {
74   const NumpyDeleteParam& params = nnvm::get<NumpyDeleteParam>(attrs.parsed);
75   return (params.step.has_value() || params.int_ind.has_value()) ? 1U : 2U;
76 })
77 .set_num_outputs(1)
78 .set_attr<nnvm::FListInputNames>("FListInputNames",
__anon39f25dc40202(const NodeAttrs& attrs) 79   [](const NodeAttrs& attrs) {
80     const NumpyDeleteParam& params = nnvm::get<NumpyDeleteParam>(attrs.parsed);
81     return (params.step.has_value() || params.int_ind.has_value()) ?
82             std::vector<std::string>{"arr"} :
83             std::vector<std::string>{"arr", "obj"};
84 })
85 .set_attr<nnvm::FInferType>("FInferType", NumpyDeleteType)
86 .set_attr<mxnet::FComputeEx>("FComputeEx<cpu>", NumpyDeleteCompute<cpu>)
87 .set_attr<FInferStorageType>("FInferStorageType", NumpyDeleteStorageType)
88 .set_attr<FResourceRequest>("FResourceRequest",
__anon39f25dc40302(const NodeAttrs& attrs) 89   [](const NodeAttrs& attrs) {
90     return std::vector<ResourceRequest>{ResourceRequest::kTempSpace};
91   })
92 .add_argument("arr", "NDArray-or-Symbol", "Input ndarray")
93 .add_argument("obj", "NDArray-or-Symbol", "Input ndarray")
94 .add_arguments(NumpyDeleteParam::__FIELDS__());
95 
96 }  // namespace op
97 }  // namespace mxnet
98