Tenncor
funcarg.hpp
Go to the documentation of this file.
1 //
8 
9 #include "teq/funcarg.hpp"
10 
11 #include "eteq/coord.hpp"
12 #include "eteq/inode.hpp"
13 
14 #ifndef ETEQ_FUNCARG_HPP
15 #define ETEQ_FUNCARG_HPP
16 
17 namespace eteq
18 {
19 
21 template <typename T>
22 struct FuncArg final
23 {
25  FuncArg (NodeptrT<T> node, teq::CoordptrT shaper, CoordptrT coorder) :
26  node_(node), shaper_(shaper), coorder_(coorder)
27  {
28  if (node_ == nullptr)
29  {
30  logs::fatal("cannot map a null node");
31  }
32  }
33 
35  teq::Shape shape (void) const
36  {
37  return teq::apply_shaper(shaper_, node_->shape());
38  }
39 
42  {
43  return node_->get_tensor();
44  }
45 
46  NodeptrT<T> get_node (void) const
47  {
48  return node_;
49  }
50 
53  {
54  return shaper_;
55  }
56 
59  bool map_io (void) const
60  {
61  // map in to out only if bijective
62  return nullptr == coorder_ || coorder_->is_bijective();
63  }
64 
66  CoordptrT get_coorder (void) const
67  {
68  return coorder_;
69  }
70 
71 private:
74 
77 
80 };
81 
83 template <typename T>
84 using ArgsT = std::vector<FuncArg<T>>;
85 
87 template <typename T>
89 {
90  return FuncArg<T>(node, teq::identity, nullptr);
91 }
92 
97 template <typename T>
99 {
100  if (offset >= teq::rank_cap)
101  {
102  logs::fatalf("cannot reduce dimensions [%d:]. Must be less than %d",
103  offset, teq::rank_cap);
104  }
105 
106  teq::RankT n = std::min<teq::RankT>(offset + ndims, teq::rank_cap);
107  teq::Shape shape = node->shape();
108  std::vector<teq::RankT> dims; // dims are allowed to be non-contiguous
109  std::vector<teq::DimT> slist;
110  dims.reserve(n);
111  slist.reserve(n);
112 
113  for (teq::RankT i = offset; i < n; ++i)
114  {
115  if (shape.at(i) > 1)
116  {
117  dims.push_back(i);
118  }
119  slist.push_back(shape.at(i));
120  }
121 
122  return FuncArg<T>(node, teq::reduce(offset, slist), reduce(dims));
123 }
124 
129 template <typename T>
131  teq::RankT rank, std::vector<teq::DimT> ext)
132 {
133  return FuncArg<T>(node, teq::extend(rank, ext), extend(rank, ext));
134 }
135 
139 template <typename T>
140 FuncArg<T> permute_map (NodeptrT<T> node, std::vector<teq::RankT> order)
141 {
142  return FuncArg<T>(node, teq::permute(order), permute(order));
143 }
144 
149 template <typename T>
151  teq::RankT extent, teq::RankT dimension)
152 {
153  if (dimension >= teq::rank_cap)
154  {
155  logs::fatalf("cannot slice dimension %d beyond rank_cap %d",
156  dimension, teq::rank_cap);
157  }
158  teq::CoordT slicings;
159  std::fill(slicings.begin(), slicings.end(), teq::rank_cap);
160  slicings[0] = offset;
161  slicings[1] = extent;
162  slicings[2] = dimension;
163  return FuncArg<T>(node,
164  std::make_shared<teq::CoordMap>(
165  [=](teq::MatrixT fwd)
166  {
167  for (teq::RankT i = 0; i < teq::rank_cap; ++i)
168  {
169  fwd[i][i] = 1;
170  }
171  fwd[teq::rank_cap][dimension] =
172  extent - node->shape().at(dimension);
173  }),
174  std::make_shared<CoordMap>(slicings, false));
175 }
176 
181 template <typename T>
183  const std::pair<teq::DimT,teq::DimT>& padding,
184  teq::RankT dimension)
185 {
186  if (dimension >= teq::rank_cap)
187  {
188  logs::fatalf("cannot pad dimension %d beyond rank_cap %d",
189  dimension, teq::rank_cap);
190  }
191  teq::CoordT paddings;
192  std::fill(paddings.begin(), paddings.end(), teq::rank_cap);
193  paddings[0] = padding.first;
194  paddings[1] = padding.second;
195  paddings[2] = dimension;
196  return FuncArg<T>(node,
197  std::make_shared<teq::CoordMap>(
198  [=](teq::MatrixT fwd)
199  {
200  for (teq::RankT i = 0; i < teq::rank_cap; ++i)
201  {
202  fwd[i][i] = 1;
203  }
204  fwd[teq::rank_cap][dimension] =
205  padding.first + padding.second;
206  }),
207  std::make_shared<CoordMap>(paddings, false));
208 }
209 
218 template <typename T>
220  const std::vector<teq::DimT>& incrs)
221 {
222  teq::CoordT strides;
223  std::fill(strides.begin(), strides.end(), 1);
224  std::copy(incrs.begin(), incrs.end(), strides.begin());
225  return FuncArg<T>(node,
226  std::make_shared<teq::CoordMap>(
227  [=](teq::MatrixT fwd)
228  {
229  for (teq::RankT i = 0; i < teq::rank_cap; ++i)
230  {
231  // ceil(in_dim / stride) =
232  // round(in_dim / stride + 0.5 - <smol num>)
233  fwd[i][i] = 1. / strides[i];
234  fwd[teq::rank_cap][i] = 0.5 -
236  }
237  }),
238  std::make_shared<CoordMap>(strides, false));
239 }
240 
241 }
242 
243 #endif // ETEQ_FUNCARG_HPP
std::array< CDimT, rank_cap > CoordT
Definition: shape.hpp:56
FuncArg(NodeptrT< T > node, teq::CoordptrT shaper, CoordptrT coorder)
Construct FuncArg with specific node, shaper, and coorder.
Definition: funcarg.hpp:25
const RankT rank_cap
Number of dimsensions in a shape/coordinate.
Definition: shape.hpp:47
FuncArg< T > slice_map(NodeptrT< T > node, teq::RankT offset, teq::RankT extent, teq::RankT dimension)
Definition: funcarg.hpp:150
CoordptrT extend(teq::RankT rank, std::vector< teq::DimT > ext)
Return CoordMap wrapper of extension parameters.
teq::CoordptrT get_shaper(void) const
Return shaper coord map.
Definition: funcarg.hpp:52
Definition: constant.hpp:17
CoordptrT reduce(std::vector< teq::RankT > red_dims)
Return CoordMap wrapper of reduction dimensions.
uint8_t RankT
Type used for shape rank.
Definition: shape.hpp:23
Definition: shape.hpp:62
CoordptrT extend(RankT rank, std::vector< DimT > ext)
bool map_io(void) const
Definition: funcarg.hpp:59
FuncArg< T > reduce_map(NodeptrT< T > node, teq::RankT offset, teq::RankT ndims)
Definition: funcarg.hpp:98
Eigen node version of teq::FuncArg.
Definition: funcarg.hpp:22
std::shared_ptr< iCoordMap > CoordptrT
Type of iCoordMap smartpointer.
Definition: coord.hpp:106
CoordptrT reduce(RankT rank, std::vector< DimT > red)
EigenptrT< T > max(teq::Shape &outshape, const OpArg< T > &a, const OpArg< T > &b)
Definition: operator.hpp:970
NodeptrT< T > node_
Tensor reference.
Definition: funcarg.hpp:73
std::shared_ptr< CoordMap > CoordptrT
Type of iCoordMap smartpointer.
Definition: coord.hpp:64
FuncArg< T > extend_map(NodeptrT< T > node, teq::RankT rank, std::vector< teq::DimT > ext)
Definition: funcarg.hpp:130
CoordptrT identity
Identity matrix instance.
teq::CoordptrT shaper_
Shape mapper.
Definition: funcarg.hpp:76
teq::TensptrT get_tensor(void) const
Return tensor being mapped.
Definition: funcarg.hpp:41
NodeptrT< T > get_node(void) const
Definition: funcarg.hpp:46
FuncArg< T > pad_map(NodeptrT< T > node, const std::pair< teq::DimT, teq::DimT > &padding, teq::RankT dimension)
Definition: funcarg.hpp:182
FuncArg< T > permute_map(NodeptrT< T > node, std::vector< teq::RankT > order)
Definition: funcarg.hpp:140
FuncArg< T > stride_map(NodeptrT< T > node, const std::vector< teq::DimT > &incrs)
Definition: funcarg.hpp:219
CoordptrT permute(std::vector< teq::RankT > dims)
Return CoordMap wrapper of permute indices.
std::shared_ptr< iTensor > TensptrT
Tensor smart pointer.
Definition: itensor.hpp:51
CoordptrT get_coorder(void) const
Return coord map for coordinates.
Definition: funcarg.hpp:66
std::vector< FuncArg< T > > ArgsT
Type of typed functor arguments.
Definition: funcarg.hpp:84
CoordptrT coorder_
Coordinate mapper.
Definition: funcarg.hpp:79
CoordptrT permute(std::vector< RankT > order)
std::shared_ptr< iNode< T > > NodeptrT
Smart pointer of node.
Definition: inode.hpp:63
FuncArg< T > identity_map(NodeptrT< T > node)
Return FuncArg<T> that identity maps input tensor.
Definition: funcarg.hpp:88
Shape apply_shaper(const CoordptrT &shaper, Shape inshape)
DimT at(RankT idx) const
Return DimT element at idx for any index in range [0:rank_cap)
Definition: shape.hpp:108
double[mat_dim][mat_dim] MatrixT
Coordinate transformation matrix (using homogeneous)
Definition: matops.hpp:28
teq::Shape shape(void) const
Return shape of tensor filtered through coordinate mapper.
Definition: funcarg.hpp:35