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 | * \brief Mapping op constructions |
22 | * \file nn/mapping.h |
23 | */ |
24 | #ifndef TVM_TOPI_NN_MAPPING_H_ |
25 | #define TVM_TOPI_NN_MAPPING_H_ |
26 | |
27 | #include <tvm/te/operation.h> |
28 | #include <tvm/topi/tags.h> |
29 | |
30 | #include <string> |
31 | |
32 | namespace tvm { |
33 | namespace topi { |
34 | namespace nn { |
35 | |
36 | using namespace tvm::te; |
37 | |
38 | /*! |
39 | * \brief Scale and shift with NCHW order |
40 | * |
41 | * \param x The input tensor. |
42 | * \param scale Scale tensor, 1-D of size channel |
43 | * \param shift Shift tensor, 1-D of size channel |
44 | * \param name The name of the operation |
45 | * \param tag The tag to mark the operation |
46 | * |
47 | * \return A Tensor whose op member is the scale shift operation |
48 | */ |
49 | inline Tensor scale_shift_nchw(const Tensor& x, const Tensor& scale, const Tensor& shift, |
50 | std::string name = "ScaleShift" , std::string tag = kBroadcast) { |
51 | return tvm::te::compute( |
52 | x->shape, [&](Var b, Var c, Var h, Var w) { return x(b, c, h, w) * scale(c) + shift(c); }, |
53 | name, tag); |
54 | } |
55 | |
56 | /*! |
57 | * \brief Scale and shift with NHWC order |
58 | * |
59 | * \param x The input tensor. |
60 | * \param scale Scale tensor, 1-D of size channel |
61 | * \param shift Shift tensor, 1-D of size channel |
62 | * \param name The name of the operation |
63 | * \param tag The tag to mark the operation |
64 | * |
65 | * \return A Tensor whose op member is the scale shift operation |
66 | */ |
67 | inline Tensor scale_shift_nhwc(const Tensor& x, const Tensor& scale, const Tensor& shift, |
68 | std::string name = "ScaleShift" , std::string tag = kBroadcast) { |
69 | return tvm::te::compute( |
70 | x->shape, [&](Var b, Var h, Var w, Var c) { return x(b, h, w, c) * scale(c) + shift(c); }, |
71 | name, tag); |
72 | } |
73 | |
74 | } // namespace nn |
75 | } // namespace topi |
76 | } // namespace tvm |
77 | #endif // TVM_TOPI_NN_MAPPING_H_ |
78 | |