mxnet
implicit_gemm.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 MSHADOW_EXTENSION_IMPLICIT_GEMM_H_
26 #define MSHADOW_EXTENSION_IMPLICIT_GEMM_H_
27 
28 #include "../extension.h"
29 #include "../packet-inl.h"
30 
31 namespace mshadow {
32 namespace expr {
39 template<typename LhsExp, typename RhsExp, typename DType>
41  public Exp<ImplicitGEMMExp<LhsExp, RhsExp, DType>,
42  DType, type::kChainer> {
44  const LhsExp &lhs_;
46  const RhsExp &rhs_;
52  ImplicitGEMMExp(const LhsExp &lhs, const RhsExp &rhs)
53  : lhs_(lhs), rhs_(rhs) {
56  this->shape_ = mshadow::Shape2(slhs[0], srhs[1]);
57  prod_size_ = slhs[1];
58  }
59 };
60 
61 
62 template<typename LhsExp, typename RhsExp, typename DType, int e1, int e2>
63 inline ImplicitGEMMExp<LhsExp, RhsExp, DType>
65  const Exp<RhsExp, DType, e2> &rhs) {
67  ::Error_Expression_Does_Not_Meet_Dimension_Req();
69 }
70 
71 //----------------------
72 // Execution plan
73 //----------------------
74 template<typename LhsExp, typename RhsExp, typename DType>
75 struct Plan<ImplicitGEMMExp<LhsExp, RhsExp, DType>, DType> {
76  public:
78  : lhs_(MakePlan(e.lhs_)),
79  rhs_(MakePlan(e.rhs_)),
80  prod_size_(e.prod_size_),
81  prod_size_lower_align_(packet::LowerAlign<DType, MSHADOW_DEFAULT_PACKET>(e.prod_size_)) {
82  }
83 
84  MSHADOW_XINLINE DType Eval(index_t y, index_t x) const {
85  typedef packet::Packet<DType> Packet;
86  Packet sum = Packet::Fill(0);
87 
88  const size_t packetSize = Packet::size;
89  DType lhs_temp[packetSize], rhs_temp[packetSize];
90 
91  for (index_t i = 0; i < prod_size_lower_align_; i += packetSize) {
92  // unroll
93  for (index_t j = 0; j < packetSize; ++j) {
94  lhs_temp[j] = lhs_.Eval(y, i + j);
95  }
96  for (index_t j = 0; j < packetSize; ++j) {
97  rhs_temp[j] = rhs_.Eval(i + j, x);
98  }
99  sum = sum + Packet::LoadUnAligned(lhs_temp) * Packet::LoadUnAligned(rhs_temp);
100  }
101  DType ret_result = sum.Sum();
102 
103  for (index_t i = prod_size_lower_align_; i < prod_size_; ++i) {
104  ret_result += lhs_.Eval(y, i) * rhs_.Eval(i, x);
105  }
106  return ret_result;
107  }
108 
109  private:
112  const index_t prod_size_;
113  const index_t prod_size_lower_align_;
114 };
115 
116 template<typename LhsExp, typename RhsExp, typename DType>
117 inline Plan<ImplicitGEMMExp<LhsExp, RhsExp, DType>, DType>
120 }
121 
122 
123 template<int dim, typename LhsExp, typename RhsExp, typename DType>
124 struct ShapeCheck<dim, ImplicitGEMMExp<LhsExp, RhsExp, DType> > {
125  inline static Shape<dim>
127  CHECK(dim == 2)
128  << "ImplicitGEMMExp only support 2 dimension";
131  CHECK_EQ(shape1[1], shape2[0])
132  << "implicit_dot The matrix shape do not match";
133  return t.shape_;
134  }
135 };
136 
137 template<typename LhsExp, typename RhsExp, typename DType>
138 struct ExpInfo<ImplicitGEMMExp<LhsExp, RhsExp, DType> > {
139  static const int kDim = 2;
141 };
142 
143 } // namespace expr
144 } // namespace mshadow
145 #endif // MSHADOW_EXTENSION_IMPLICIT_GEMM_H_
146 
mshadow::expr::ExpInfo::kDevMask
static const int kDevMask
Definition: expr_engine-inl.h:264
mshadow::expr::Exp::self
const SubType & self(void) const
Definition: expression.h:82
mshadow::expr::ImplicitGEMMExp::prod_size_
index_t prod_size_
internal production size
Definition: implicit_gemm.h:48
mshadow::expr::TypeCheckPass
used to help static type check
Definition: expr_engine-inl.h:330
mshadow::expr::ImplicitGEMMExp::ImplicitGEMMExp
ImplicitGEMMExp(const LhsExp &lhs, const RhsExp &rhs)
constructor
Definition: implicit_gemm.h:52
mshadow::packet::Packet
Generic packet type.
Definition: packet-inl.h:59
mshadow::expr::ImplicitGEMMExp::rhs_
const RhsExp & rhs_
rhs operand
Definition: implicit_gemm.h:46
MSHADOW_XINLINE
#define MSHADOW_XINLINE
Definition: base.h:228
mshadow::packet::LowerAlign
index_t LowerAlign(index_t size)
get lower bound of aligned index of size
Definition: packet-inl.h:146
mshadow::expr::ShapeCheck
runtime shape checking template get the shape of an expression, report error if shape mismatch
Definition: expr_engine-inl.h:364
mshadow::expr::ShapeCheck::Check
static Shape< dim > Check(const E &t)
mshadow::expr::ExpInfo
static type inference template, used to get the dimension of each expression, if ExpInfo<E>::kDim == ...
Definition: expr_engine-inl.h:262
mshadow::expr::MakePlan
Plan< BinaryMapExp< OP, TA, TB, DType, etype >, DType > MakePlan(const BinaryMapExp< OP, TA, TB, DType, etype > &e)
Definition: expr_engine-inl.h:239
mshadow::expr::ShapeCheck< dim, ImplicitGEMMExp< LhsExp, RhsExp, DType > >::Check
static Shape< dim > Check(const ImplicitGEMMExp< LhsExp, RhsExp, DType > &t)
Definition: implicit_gemm.h:126
mshadow::expr::ExpInfo::kDim
static const int kDim
Definition: expr_engine-inl.h:263
mshadow::index_t
int32_t index_t
type that will be used for index
Definition: base.h:328
mshadow::expr::Plan
Definition: expr_engine-inl.h:58
mshadow::expr::ImplicitGEMMExp::shape_
Shape< 2 > shape_
the shape of this expression
Definition: implicit_gemm.h:50
mshadow::expr::Exp
defines how expression exp can be evaluated and stored into dst
Definition: expression.h:79
mshadow::expr::implicit_dot
ImplicitGEMMExp< LhsExp, RhsExp, DType > implicit_dot(const Exp< LhsExp, DType, e1 > &lhs, const Exp< RhsExp, DType, e2 > &rhs)
Definition: implicit_gemm.h:64
mshadow
overloaded + operator between half_t and bf16_t
Definition: base.h:319
mshadow::expr::ImplicitGEMMExp::lhs_
const LhsExp & lhs_
lhs operand
Definition: implicit_gemm.h:44
mshadow::Shape2
MSHADOW_XINLINE Shape< 2 > Shape2(index_t s0, index_t s1)
construct a two dimension shape, stride will equal s0
Definition: tensor.h:230
mshadow::Shape< 2 >
mshadow::expr::Plan< ImplicitGEMMExp< LhsExp, RhsExp, DType >, DType >::Plan
Plan(const ImplicitGEMMExp< LhsExp, RhsExp, DType > &e)
Definition: implicit_gemm.h:77
mshadow::expr::ImplicitGEMMExp
Matrix multiplication.
Definition: implicit_gemm.h:40
MSHADOW_DEFAULT_PACKET
#define MSHADOW_DEFAULT_PACKET
Definition: packet-inl.h:47
mshadow::expr::Plan< ImplicitGEMMExp< LhsExp, RhsExp, DType >, DType >::Eval
MSHADOW_XINLINE DType Eval(index_t y, index_t x) const
Definition: implicit_gemm.h:84