onnx2versal
Loading...
Searching...
No Matches
graph_quantize_linear.h
1#ifndef __QUANTIZE_LINEAR_GRAPH_H__
2#define __QUANTIZE_LINEAR_GRAPH_H__
3
4#include <adf.h>
5#include "quantize_linear.h"
6#include "graph_concat.h"
7#include "graph_split.h"
8
9
35template <template<typename, int, int, int> class QUANTIZE_LINEAR, typename TT, int INP_H, int INP_W, int OUT_W>
36class QuantizeLinearGraph : public adf::graph {
37
38 private:
39 adf::kernel k[1];
40 std::string id;
41
42 public:
43 adf::port<input> pin[1];
44 adf::port<output> pout[1];
45
47 float y_scale,
48 TT y_zero,
49 int repeat_cnt = 1
50 ) {
51 k[0] = adf::kernel::create_object<QUANTIZE_LINEAR<TT, INP_H, INP_W, OUT_W>>(y_scale, y_zero);
52 adf::source(k[0]) = "quantize_linear.cc";
53 adf::headers(k[0]) = {"quantize_linear.h"};
54 adf::runtime<ratio>(k[0]) = 0.6;
55 adf::repetition_count(k[0]) = repeat_cnt;
56
57 adf::connect<adf::window<INP_H*INP_W*4>> (pin[0], k[0].in[0]);
58 adf::connect<adf::window<INP_H*OUT_W>> (k[0].out[0], pout[0]);
59 }
60
61};
62
63
72template <template<typename, int, int, int> class QUANTIZE_LINEAR, typename TT, int INP_H, int INP_W, int OUT_W>
73class QuantizeLinearStreamGraph : public adf::graph {
74
75 private:
76 adf::kernel k[1];
77 std::string id;
78
79 public:
80 adf::port<input> pin[1];
81 adf::port<output> pout[1];
82
84 float y_scale,
85 TT y_zero
86 ) {
87 k[0] = adf::kernel::create_object<QUANTIZE_LINEAR<TT, INP_H, INP_W, OUT_W>>(y_scale, y_zero);
88 adf::source(k[0]) = "quantize_linear.cc";
89 adf::headers(k[0]) = {"quantize_linear.h"};
90 adf::runtime<ratio>(k[0]) = 0.6;
91
92 adf::connect<adf::stream> (pin[0], k[0].in[0]);
93 adf::connect<adf::stream> (k[0].out[0], pout[0]);
94
95 adf::samples_per_iteration(k[0].in[0]) = INP_H*INP_W;
96 adf::samples_per_iteration(k[0].out[0]) = INP_H*OUT_W;
97 }
98
99};
100
101
110template <template<typename, int, int, int> class QUANTIZE_LINEAR, int HCHUNK,
111 typename TT, int INP_H, int INP_W, int OUT_W>
112class QuantizeLinearChunkHPktStreamGraph : public adf::graph {
113
114 private:
116 mSplitGraph split_graph;
117
118 static constexpr int LCNT = INP_H/HCHUNK;
119 adf::kernel k[LCNT];
120 std::string id;
121
122 ConcatStreamGraph<ConcatInt8Stream, TT, LCNT, 1, HCHUNK*OUT_W, INP_H*OUT_W> concat_graph;
123
124 public:
125 adf::port<input> pin[1];
126 adf::port<output> pout[1];
127
129 float y_scale,
130 TT y_zero
131 ) {
132 static_assert(INP_H % HCHUNK == 0);
133
134 adf::connect<adf::stream> (pin[0], split_graph.pin[0]);
135
136 for (int i = 0; i < LCNT; i++) {
137 k[i] = adf::kernel::create_object<QUANTIZE_LINEAR<TT, HCHUNK, INP_W, OUT_W>>(y_scale, y_zero);
138 adf::source(k[i]) = "quantize_linear.cc";
139 adf::headers(k[i]) = {"quantize_linear.h"};
140 adf::runtime<ratio>(k[i]) = 0.6;
141
142 adf::connect<adf::stream> (split_graph.pout[i], k[i].in[0]);
143 adf::connect<adf::stream> (k[i].out[0], concat_graph.pin[i]);
144
145 adf::samples_per_iteration(k[i].in[0]) = HCHUNK*INP_W;
146 adf::samples_per_iteration(k[i].out[0]) = HCHUNK*OUT_W;
147 }
148
149 for (int i = 0; i < LCNT; i++) {
150 if ((i&0x1) == 1)
151 adf::location<adf::kernel>(k[i]) = adf::location<adf::kernel>(k[i-1]) + adf::relative_offset({.col_offset=0, .row_offset=1});
152 if (i == 4)
153 adf::location<adf::kernel>(k[i]) = adf::location<adf::kernel>(k[i-1]) + adf::relative_offset({.col_offset=-1, .row_offset=2});
154 if (i == 2 || i == 6)
155 adf::location<adf::kernel>(k[i]) = adf::location<adf::kernel>(k[i-2]) + adf::relative_offset({.col_offset=1, .row_offset=0});
156 adf::location<adf::stack>(k[i]) = adf::location<adf::kernel>(k[i]);
157 }
158
159 for (int i = 0; i < concat_graph.k1.size(); i++) {
160 adf::location_constraint cTilePos = adf::location<adf::kernel>(concat_graph.k1[i]);
161 adf::location<adf::stack>(concat_graph.k1[i]) = cTilePos;
162
163 if (i < 2) {
164 adf::location<adf::kernel>(concat_graph.k1[i]) =
165 adf::location<adf::kernel>(k[i*2]) + adf::relative_offset({.col_offset=0, .row_offset=-1});
166 } else {
167 adf::location<adf::kernel>(concat_graph.k1[i]) =
168 adf::location<adf::kernel>(k[i*2+1]) + adf::relative_offset({.col_offset=0, .row_offset=1});
169 }
170 }
171 adf::location<adf::kernel>(split_graph.k[0]) = adf::location<adf::kernel>(k[1]) + adf::relative_offset({.col_offset=0, .row_offset=1});
172 adf::connect<adf::stream> (concat_graph.pout[0], pout[0]);
173 }
174
175};
179#endif // __QUANTIZE_LINEAR_GRAPH_H__
Multi instance pktstream graph.
Definition graph_quantize_linear.h:112
Single instance graph.
Definition graph_quantize_linear.h:36
Single instance stream graph.
Definition graph_quantize_linear.h:73
Graph wrapper for two stream split.
Definition graph_split.h:185