mxnet
tensor.h
Go to the documentation of this file.
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 
25 #ifndef NNVM_TOP_TENSOR_H_
26 #define NNVM_TOP_TENSOR_H_
27 
28 #include <dmlc/base.h>
29 #include <dmlc/parameter.h>
30 #include <nnvm/tuple.h>
31 
32 namespace nnvm {
33 namespace top {
34 
35 struct ConcatenateParam : public dmlc::Parameter<ConcatenateParam> {
36  int axis;
38  DMLC_DECLARE_FIELD(axis).set_default(1)
39  .describe("the axis to be concated.");
40  }
41 };
42 
43 struct ExpandDimsParam : public dmlc::Parameter<ExpandDimsParam> {
44  int axis;
47  DMLC_DECLARE_FIELD(axis)
48  .describe("the axis to be expanded.");
49  DMLC_DECLARE_FIELD(num_newaxis).set_lower_bound(1).set_default(1)
50  .describe("Number of new axis to be inserted.");
51  }
52 };
53 
54 struct SplitParam : public dmlc::Parameter<SplitParam> {
55  // numpy convention, only support indices, not support list.
57  int axis;
58  // additional hint whether it is equal_split mode
59  // deduced from indices_or_sections
61 
63  DMLC_DECLARE_FIELD(indices_or_sections)
64  .describe("Number of outputs to be splitted");
65  DMLC_DECLARE_FIELD(axis).set_default(1)
66  .describe("the axis to be splitted.");
67  }
68 };
69 
70 
71 struct TakeParam : public dmlc::Parameter<TakeParam> {
73 
75  DMLC_DECLARE_FIELD(axis).set_default(dmlc::optional<int>())
76  .describe("the axis over which to select values.");
77  }
78 };
79 
80 struct StridedSliceParam : public dmlc::Parameter<StridedSliceParam> {
81  // numpy convention, only support indices, not support list.
85 
87  DMLC_DECLARE_FIELD(begin)
88  .describe("Indices for begin of slice");
89  DMLC_DECLARE_FIELD(end)
90  .describe("Indices for end of the slice");
91  DMLC_DECLARE_FIELD(stride).set_default(Tuple<int64_t>())
92  .describe("Stride values of the slice");
93  }
94 };
95 
96 enum TypeFlag {
97  kFloat32 = 0,
98  kFloat64 = 1,
99  kFloat16 = 2,
100  kUint8 = 3,
101  kInt32 = 4,
102  kInt8 = 5,
103  kInt64 = 6,
104  kInt16 = 7,
105  kUint16 = 8,
106  kUint32 = 9,
107  kUint64 = 10,
108 };
109 
111  kGT0 = 0,
112  kLT0 = 1,
113  kMax = 2,
114  kMin = 3,
115 };
116 
117 #define DMLC_DECLARE_DTYPE_FIELD(name) \
118  DMLC_DECLARE_FIELD(name) \
119  .add_enum("float16", kFloat16) \
120  .add_enum("float32", kFloat32) \
121  .add_enum("float64", kFloat64) \
122  .add_enum("uint8", kUint8) \
123  .add_enum("uint16", kUint16) \
124  .add_enum("uint32", kUint32) \
125  .add_enum("uint64", kUint64) \
126  .add_enum("int8", kInt8) \
127  .add_enum("int16", kInt16) \
128  .add_enum("int32", kInt32) \
129  .add_enum("int64", kInt64)
130 
131 struct CastParam : public dmlc::Parameter<CastParam> {
132  int dtype;
135  .describe("Output data type.");
136  }
137 };
138 
139 struct IndicatorParam : public dmlc::Parameter<IndicatorParam> {
141  bool exclude;
143  DMLC_DECLARE_FIELD(axis).set_default(TShape())
144  .describe(R"code(The axis or axes along which to perform the indicator rule.
145 
146  The default, `axis=()`, will compute over all elements into a
147  scalar array with shape `(1,)`.
148 
149  If `axis` is int, rule is applied on a particular axis.
150 
151  If `axis` is a tuple of ints, rule is applied on all the axes
152  specified in the tuple.
153 
154  If `exclude` is true, rule will be applied on the axes that are
155  NOT in axis instead.)code");
156  DMLC_DECLARE_FIELD(exclude).set_default(false)
157  .describe("Whether to apply rule on axis that are NOT in axis instead.");
158  }
159 };
160 
161 struct ReshapeParam : public dmlc::Parameter<ReshapeParam> {
163 
165  DMLC_DECLARE_FIELD(shape);
166  }
167 };
168 
169 struct SqueezeParam : public dmlc::Parameter<SqueezeParam> {
171 
173  DMLC_DECLARE_FIELD(axis).set_default(TShape())
174  .describe("The axis to squeeze in the input tensor.");
175  }
176 };
177 
178 struct ScalarParam : public dmlc::Parameter<ScalarParam> {
179  double scalar;
180 
182  DMLC_DECLARE_FIELD(scalar);
183  }
184 };
185 
186 struct FillValueParam : public dmlc::Parameter<FillValueParam> {
187  double fill_value;
188 
190  DMLC_DECLARE_FIELD(fill_value)
191  .describe("Scalar value to be filled");
192  }
193 };
194 
195 struct TransposeParam : public dmlc::Parameter<TransposeParam> {
197 
199  DMLC_DECLARE_FIELD(axes).set_default(TShape())
200  .describe("Target axis order. By default the axes will be inverted.");
201  }
202 };
203 
204 struct FlipParam : public dmlc::Parameter<FlipParam> {
205  int axis;
207  DMLC_DECLARE_FIELD(axis).set_default(0)
208  .describe("the axis to be reveresed.");
209  }
210 };
211 
212 struct BroadcastToParam : public dmlc::Parameter<BroadcastToParam> {
214 
216  DMLC_DECLARE_FIELD(shape).set_default(TShape())
217  .describe("The shape of the desired array."
218  " We can set the dim to zero if it's same as the original."
219  " E.g `A = broadcast_to(B, shape=(10, 0, 0))` ");
220  }
221 };
222 
223 struct ReduceParam : public dmlc::Parameter<ReduceParam> {
225  bool keepdims;
226  bool exclude;
227  int dtype;
228 
230  DMLC_DECLARE_FIELD(axis).set_default(TShape())
231  .describe(R"code(The axis or axes along which to perform the reduction.
232 
233  The default, `axis=()`, will compute over all elements into a
234  scalar array with shape `(1,)`.
235 
236  If `axis` is int, a reduction is performed on a particular axis.
237 
238  If `axis` is a tuple of ints, a reduction is performed on all the axes
239  specified in the tuple.
240 
241  If `exclude` is true, reduction will be performed on the axes that are
242  NOT in axis instead.)code");
243 
244  DMLC_DECLARE_FIELD(keepdims).set_default(false)
245  .describe("If this is set to `True`, the reduced axes are left "
246  "in the result as dimension with size one.");
247  DMLC_DECLARE_FIELD(exclude).set_default(false)
248  .describe("Whether to perform reduction on axis that are NOT in axis instead.");
249  DMLC_DECLARE_DTYPE_FIELD(dtype).set_default(kInt32)
250  .describe("Target data type.");
251  }
252 };
253 
254 struct InitOpWithScalarParam : public dmlc::Parameter<InitOpWithScalarParam> {
256  int dtype;
257  double fill_value;
258 
260  DMLC_DECLARE_FIELD(shape).set_default(TShape());
261  DMLC_DECLARE_DTYPE_FIELD(dtype).set_default(kFloat32)
262  .describe("Target data type.");
263  DMLC_DECLARE_FIELD(fill_value).describe("Scalar value to fill");
264  }
265 };
266 
267 struct InitOpParam : public dmlc::Parameter<InitOpParam> {
269  int dtype;
270 
272  DMLC_DECLARE_FIELD(shape).set_default(TShape());
273  DMLC_DECLARE_DTYPE_FIELD(dtype).set_default(kFloat32)
274  .describe("Target data type.");
275  }
276 };
277 
278 struct ElementWiseReduceParam : public dmlc::Parameter<ElementWiseReduceParam> {
279  int num_args;
281  DMLC_DECLARE_FIELD(num_args).set_lower_bound(1)
282  .describe("Number of inputs to be reduced.");
283  }
284 };
285 
286 struct MatMulParam : public dmlc::Parameter<MatMulParam> {
289 
291  DMLC_DECLARE_FIELD(transpose_a)
292  .describe("If true then transpose the first input before dot.")
293  .set_default(false);
294  DMLC_DECLARE_FIELD(transpose_b)
295  .describe("If true then transpose the second input before dot.")
296  .set_default(false);
297  }
298 };
299 
300 struct ClipParam : public dmlc::Parameter<ClipParam> {
301  double a_min, a_max;
303  DMLC_DECLARE_FIELD(a_min)
304  .describe("Minimum value such that value smaller then this will be clipped.");
305  DMLC_DECLARE_FIELD(a_max)
306  .describe("Maximum value such that value larger then this will be clipped.");
307  }
308 };
309 
310 struct SliceLikeParam : public dmlc::Parameter<SliceLikeParam> {
313  DMLC_DECLARE_FIELD(axis).set_default(Tuple<int>())
314  .describe("List of axes on which input data will be sliced according to the "
315  "corresponding size of the second input. By default will slice "
316  "on all axes. Negative axes are supported.");
317  }
318 };
319 
320 } // namespace top
321 } // namespace nnvm
322 
323 #endif // NNVM_TOP_TENSOR_H_
int axis
Definition: tensor.h:57
int axis
Definition: tensor.h:44
Definition: base.h:36
Definition: tensor.h:35
int dtype
Definition: tensor.h:227
dmlc::optional< int > axis
Definition: tensor.h:72
Definition: tensor.h:107
DMLC_DECLARE_PARAMETER(SqueezeParam)
Definition: tensor.h:172
int dtype
Definition: tensor.h:132
Tuple< int64_t > begin
Definition: tensor.h:82
DMLC_DECLARE_PARAMETER(MatMulParam)
Definition: tensor.h:290
double scalar
Definition: tensor.h:179
Definition: tensor.h:111
Definition: tensor.h:204
DMLC_DECLARE_PARAMETER(CastParam)
Definition: tensor.h:133
Definition: tensor.h:300
bool exclude
Definition: tensor.h:141
DMLC_DECLARE_PARAMETER(ElementWiseReduceParam)
Definition: tensor.h:280
int num_args
Definition: tensor.h:279
bool keepdims
Definition: tensor.h:225
DMLC_DECLARE_PARAMETER(SplitParam)
Definition: tensor.h:62
Definition: tensor.h:254
DMLC_DECLARE_PARAMETER(IndicatorParam)
Definition: tensor.h:142
DMLC_DECLARE_PARAMETER(InitOpWithScalarParam)
Definition: tensor.h:259
TShape axis
Definition: tensor.h:170
Definition: tensor.h:105
Definition: tensor.h:113
TShape shape
Definition: tensor.h:268
Tuple< int64_t > end
Definition: tensor.h:83
Definition: tensor.h:169
DMLC_DECLARE_PARAMETER(FillValueParam)
Definition: tensor.h:189
Definition: tensor.h:195
Definition: tensor.h:178
DMLC_DECLARE_PARAMETER(TransposeParam)
Definition: tensor.h:198
Definition: tensor.h:212
int dtype
Definition: tensor.h:256
A Shape class that is used to represent shape of each tensor.
Definition: tuple.h:344
int axis
Definition: tensor.h:205
DMLC_DECLARE_PARAMETER(ExpandDimsParam)
Definition: tensor.h:46
Definition: tensor.h:286
DMLC_DECLARE_PARAMETER(ScalarParam)
Definition: tensor.h:181
double fill_value
Definition: tensor.h:187
DMLC_DECLARE_PARAMETER(FlipParam)
Definition: tensor.h:206
DMLC_DECLARE_PARAMETER(ClipParam)
Definition: tensor.h:302
bool transpose_a
Definition: tensor.h:287
Definition: tensor.h:112
Definition: tensor.h:267
Definition: tensor.h:80
DMLC_DECLARE_PARAMETER(InitOpParam)
Definition: tensor.h:271
Definition: tensor.h:114
TShape axis
Definition: tensor.h:224
TypeFlag
Definition: tensor.h:96
IndicatorRuleFlag
Definition: tensor.h:110
Definition: tensor.h:278
#define DMLC_DECLARE_DTYPE_FIELD(name)
Definition: tensor.h:117
Definition: tensor.h:98
double a_min
Definition: tensor.h:301
bool equal_split
Definition: tensor.h:60
Tuple< int64_t > stride
Definition: tensor.h:84
Definition: tensor.h:310
TShape shape
Definition: tensor.h:213
Data structure Tuple and TShape to store dynamic sized shapes.
Definition: tensor.h:97
TShape axis
Definition: tensor.h:140
Definition: tensor.h:103
DMLC_DECLARE_PARAMETER(ReduceParam)
Definition: tensor.h:229
Tuple< int64_t > shape
Definition: tensor.h:162
bool exclude
Definition: tensor.h:226
Tuple< int > indices_or_sections
Definition: tensor.h:56
int dtype
Definition: tensor.h:269
Definition: tensor.h:106
Definition: tensor.h:43
Definition: tensor.h:100
int num_newaxis
Definition: tensor.h:45
TShape shape
Definition: tensor.h:255
DMLC_DECLARE_PARAMETER(StridedSliceParam)
Definition: tensor.h:86
Definition: tensor.h:161
DMLC_DECLARE_PARAMETER(ConcatenateParam)
Definition: tensor.h:37
DMLC_DECLARE_PARAMETER(ReshapeParam)
Definition: tensor.h:164
Definition: tensor.h:54
bool transpose_b
Definition: tensor.h:288
Definition: tensor.h:223
Definition: tensor.h:131
Definition: tensor.h:186
Definition: tensor.h:139
double fill_value
Definition: tensor.h:257
DMLC_DECLARE_PARAMETER(BroadcastToParam)
Definition: tensor.h:215
TShape axes
Definition: tensor.h:196
DMLC_DECLARE_PARAMETER(SliceLikeParam)
Definition: tensor.h:312
Definition: tensor.h:104
Definition: tensor.h:101
Tuple< int > axis
Definition: tensor.h:311
Definition: tensor.h:102
Provide lightweight util to do parameter setup and checking.
Definition: tensor.h:99
int axis
Definition: tensor.h:36
Definition: tensor.h:71
DMLC_DECLARE_PARAMETER(TakeParam)
Definition: tensor.h:74