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 init_op.cc 22 * \brief CPU Implementation of init op 23 */ 24 #include "./init_op.h" 25 #include "./elemwise_unary_op.h" 26 27 namespace mxnet { 28 namespace op { 29 30 DMLC_REGISTER_PARAMETER(InitOpParam); 31 DMLC_REGISTER_PARAMETER(InitOpWithScalarParam); 32 DMLC_REGISTER_PARAMETER(InitOpWithoutDTypeParam); 33 DMLC_REGISTER_PARAMETER(RangeParam); 34 DMLC_REGISTER_PARAMETER(RangeLikeParam); 35 DMLC_REGISTER_PARAMETER(EyeParam); 36 DMLC_REGISTER_PARAMETER(LinspaceParam); 37 38 NNVM_REGISTER_OP(_zeros_without_dtype) 39 .describe("fill target with zeros without default dtype") 40 .set_num_inputs(0) 41 .set_num_outputs(1) 42 .set_attr_parser(ParamParser<InitOpWithoutDTypeParam>) 43 .set_attr<mxnet::FInferShape>("FInferShape", InitShape<InitOpWithoutDTypeParam>) 44 .set_attr<nnvm::FInferType>("FInferType", InitType<InitOpWithoutDTypeParam>) 45 .set_attr<FInferStorageType>("FInferStorageType", 46 InitStorageType<InitOpWithoutDTypeParam, true, true>) 47 .set_attr<FCompute>("FCompute<cpu>", FillCompute<cpu, 0>) 48 .set_attr<FComputeEx>("FComputeEx<cpu>", FillComputeZerosEx<cpu>) 49 .add_arguments(InitOpWithoutDTypeParam::__FIELDS__()); 50 51 NNVM_REGISTER_OP(_zeros) 52 .describe("fill target with zeros") 53 .set_num_inputs(0) 54 .set_num_outputs(1) 55 .set_attr_parser(ParamParser<InitOpParam>) 56 .set_attr<mxnet::FInferShape>("FInferShape", InitShape<InitOpParam>) 57 .set_attr<nnvm::FInferType>("FInferType", InitType<InitOpParam>) 58 .set_attr<FInferStorageType>("FInferStorageType", InitStorageType<InitOpParam, true, true>) 59 .set_attr<FCompute>("FCompute<cpu>", FillCompute<cpu, 0>) 60 .set_attr<FComputeEx>("FComputeEx<cpu>", FillComputeZerosEx<cpu>) 61 .add_arguments(InitOpParam::__FIELDS__()); 62 63 NNVM_REGISTER_OP(_eye) 64 .describe("Return a 2-D array with ones on the diagonal and zeros elsewhere.") 65 .set_num_inputs(0) 66 .set_num_outputs(1) 67 .set_attr_parser(ParamParser<EyeParam>) 68 .set_attr<mxnet::FInferShape>("FInferShape", InitEyeShape<EyeParam>) 69 .set_attr<nnvm::FInferType>("FInferType", InitType<EyeParam>) 70 .set_attr<FCompute>("FCompute<cpu>", EyeFill<cpu>) 71 .add_arguments(EyeParam::__FIELDS__()); 72 73 NNVM_REGISTER_OP(_ones) 74 .describe("fill target with ones") 75 .set_num_inputs(0) 76 .set_num_outputs(1) 77 .set_attr_parser(ParamParser<InitOpParam>) 78 .set_attr<mxnet::FInferShape>("FInferShape", InitShape<InitOpParam>) 79 .set_attr<nnvm::FInferType>("FInferType", InitType<InitOpParam>) 80 .set_attr<FCompute>("FCompute<cpu>", FillCompute<cpu, 1>) 81 .add_arguments(InitOpParam::__FIELDS__()); 82 83 NNVM_REGISTER_OP(_full) 84 .add_alias("_npi_full") 85 .describe("fill target with a scalar value") 86 .set_num_inputs(0) 87 .set_num_outputs(1) 88 .set_attr_parser(ParamParser<InitOpWithScalarParam>) 89 .set_attr<mxnet::FInferShape>("FInferShape", InitShape<InitOpWithScalarParam>) 90 .set_attr<nnvm::FInferType>("FInferType", InitType<InitOpWithScalarParam>) 91 .set_attr<FCompute>("FCompute<cpu>", InitFillWithScalarCompute<cpu>) 92 .add_arguments(InitOpWithScalarParam::__FIELDS__()); 93 94 NNVM_REGISTER_OP(_arange) 95 .describe("Return evenly spaced values within a given interval. Similar to Numpy") 96 .set_num_inputs(0) 97 .set_num_outputs(1) 98 .set_attr_parser(RangeParamParser) 99 .set_attr<mxnet::FInferShape>("FInferShape", RangeShape) 100 .set_attr<nnvm::FInferType>("FInferType", InitType<RangeParam>) 101 .set_attr<FCompute>("FCompute<cpu>", RangeCompute<cpu, RangeParam>) 102 .add_arguments(RangeParam::__FIELDS__()); 103 104 NNVM_REGISTER_OP(_contrib_arange_like) 105 .add_alias("_npx_arange_like") 106 .describe(R"code(Return an array with evenly spaced values. If axis is not given, the output will 107 have the same shape as the input array. Otherwise, the output will be a 1-D array with size of 108 the specified axis in input shape. 109 110 Examples:: 111 112 x = [[0.14883883 0.7772398 0.94865847 0.7225052 ] 113 [0.23729339 0.6112595 0.66538996 0.5132841 ] 114 [0.30822644 0.9912457 0.15502319 0.7043658 ]] 115 <NDArray 3x4 @cpu(0)> 116 117 out = mx.nd.contrib.arange_like(x, start=0) 118 119 [[ 0. 1. 2. 3.] 120 [ 4. 5. 6. 7.] 121 [ 8. 9. 10. 11.]] 122 <NDArray 3x4 @cpu(0)> 123 124 out = mx.nd.contrib.arange_like(x, start=0, axis=-1) 125 126 [0. 1. 2. 3.] 127 <NDArray 4 @cpu(0)> 128 )code") 129 .set_num_inputs(1) 130 .set_num_outputs(1) 131 .set_attr_parser(ParamParser<RangeLikeParam>) 132 .set_attr<mxnet::FInferShape>("FInferShape", RangeLikeShape) 133 .set_attr<nnvm::FInferType>("FInferType", ElemwiseType<1, 1>) 134 .set_attr<nnvm::FIgnoreInputs>("FIgnoreInputs", __anon44548a2a0102(const NodeAttrs& attrs) 135 [](const NodeAttrs& attrs) { return std::vector<uint32_t>(1, 0); }) 136 .set_attr<FCompute>("FCompute<cpu>", RangeCompute<cpu, RangeLikeParam>) 137 .set_attr<nnvm::FGradient>("FGradient", MakeZeroGradNodes) 138 .add_argument("data", "NDArray-or-Symbol", "The input") 139 .add_arguments(RangeLikeParam::__FIELDS__()); 140 141 NNVM_REGISTER_OP(_linspace) 142 .add_alias("_npi_linspace") 143 .describe("Return evenly spaced numbers over a specified interval. Similar to Numpy") 144 .set_num_inputs(0) 145 .set_num_outputs(1) 146 .set_attr_parser(ParamParser<LinspaceParam>) 147 .set_attr<mxnet::FInferShape>("FInferShape", LinspaceShape) 148 .set_attr<nnvm::FInferType>("FInferType", InitType<LinspaceParam>) 149 .set_attr<FCompute>("FCompute<cpu>", LinspaceCompute<cpu>) 150 .add_arguments(RangeParam::__FIELDS__()); 151 152 NNVM_REGISTER_OP(zeros_like) 153 MXNET_ADD_SPARSE_OP_ALIAS(zeros_like) 154 .describe(R"code(Return an array of zeros with the same shape, type and storage type 155 as the input array. 156 157 The storage type of ``zeros_like`` output depends on the storage type of the input 158 159 - zeros_like(row_sparse) = row_sparse 160 - zeros_like(csr) = csr 161 - zeros_like(default) = default 162 163 Examples:: 164 165 x = [[ 1., 1., 1.], 166 [ 1., 1., 1.]] 167 168 zeros_like(x) = [[ 0., 0., 0.], 169 [ 0., 0., 0.]] 170 171 )code") 172 .set_num_inputs(1) 173 .set_num_outputs(1) 174 .set_attr<mxnet::FInferShape>("FInferShape", ElemwiseShape<1, 1>) 175 .set_attr<nnvm::FInferType>("FInferType", ElemwiseType<1, 1>) 176 .set_attr<FInferStorageType>("FInferStorageType", ElemwiseStorageType<1, 1, false, true, true>) 177 .set_attr<nnvm::FIgnoreInputs>("FIgnoreInputs", __anon44548a2a0202(const NodeAttrs& attrs) 178 [](const NodeAttrs& attrs) { return std::vector<uint32_t>(1, 0); }) 179 .set_attr<FCompute>("FCompute<cpu>", FillCompute<cpu, 0>) 180 .set_attr<FComputeEx>("FComputeEx<cpu>", FillComputeZerosEx<cpu>) 181 .set_attr<nnvm::FGradient>("FGradient", MakeZeroGradNodes) 182 .add_argument("data", "NDArray-or-Symbol", "The input"); 183 184 NNVM_REGISTER_OP(ones_like) 185 .describe(R"code(Return an array of ones with the same shape and type 186 as the input array. 187 188 Examples:: 189 190 x = [[ 0., 0., 0.], 191 [ 0., 0., 0.]] 192 193 ones_like(x) = [[ 1., 1., 1.], 194 [ 1., 1., 1.]] 195 196 )code") 197 .set_num_inputs(1) 198 .set_num_outputs(1) 199 .set_attr<mxnet::FInferShape>("FInferShape", ElemwiseShape<1, 1>) 200 .set_attr<nnvm::FInferType>("FInferType", ElemwiseType<1, 1>) 201 .set_attr<nnvm::FIgnoreInputs>("FIgnoreInputs", __anon44548a2a0302(const NodeAttrs& attrs) 202 [](const NodeAttrs& attrs) { return std::vector<uint32_t>(1, 0); }) 203 .set_attr<FCompute>("FCompute<cpu>", FillCompute<cpu, 1>) 204 .set_attr<nnvm::FGradient>("FGradient", MakeZeroGradNodes) 205 .add_argument("data", "NDArray-or-Symbol", "The input"); 206 207 } // namespace op 208 } // namespace mxnet 209