Skip to content

Commit 2f14677

Browse files
essayedJaccovG
authored andcommitted
Fix some issues with TensorIterator and padding
1 parent aeb46a9 commit 2f14677

File tree

3 files changed

+122
-36
lines changed

3 files changed

+122
-36
lines changed

include/mli_compiler_api.hpp

Lines changed: 104 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,31 @@ class Conv2d_CS : public CompilerGenericInterface {
288288
const OffsetBuffer& wtszeropts,
289289
const OffsetBuffer& ctrl_buffer) = 0;
290290

291-
// mli_status GetKernelPrivateData(void* kernel_private_data_buffer) override ;
292-
// unsigned GetKernelPrivateDataSize() override ;
293-
// unsigned GetRuntimeObjectSize() override ;
291+
/**
292+
* @brief Method to set buffer memory offsets and memory IDs for the kernel
293+
*
294+
* Compiler computes a memory map and buffer offsets are set using this method.
295+
* Compiler also needs to indicate in which memory the buffers reside.
296+
* These ID's need to match the array of memory bases that the xop-interpreter passes to
297+
* the init function.
298+
*
299+
* In this method you specify offsets for tensors passed to the constructor
300+
*
301+
* @param input [I] OffsetBuffer containing Memory Identifier and Offset in that memory
302+
* @param output [I] OffsetBuffer containing Memory Identifier and Offset in that memory
303+
* @param weights_and_zeropts [I] OffsetBuffer containing Memory Identifier and Offset in that memory
304+
* @param inpzeropts [I] OffsetBuffer containing Memory Identifier and Offset in that memory
305+
* @param ctrl_buffer [I] OffsetBuffer containing Memory Identifier and Offset in that memory
306+
*
307+
* @return MLI status code
308+
*/
309+
virtual mli_status AttachBufferOffsets(const OffsetBuffer &input,
310+
const OffsetBuffer &output,
311+
const OffsetBuffer &weights_and_zeropts,
312+
const OffsetBuffer &inpzeropts,
313+
const OffsetBuffer &ctrl_buffer) {
314+
NOT_IMPLEMENTED_METHOD;
315+
return MLI_STATUS_OK; };
294316

295317
/**
296318
* @brief Method to set iteration information used in the .Update()
@@ -565,6 +587,33 @@ class DepthwiseConv2d_CS : public CompilerGenericInterface {
565587
const OffsetBuffer& inpzeropts,
566588
const OffsetBuffer& wtszeropts,
567589
const OffsetBuffer& ctrl_buffer) = 0;
590+
591+
/**
592+
* @brief Method to set buffer memory offsets and memory IDs for the kernel
593+
*
594+
* Compiler computes a memory map and buffer offsets are set using this method.
595+
* Compiler also needs to indicate in which memory the buffers reside.
596+
* These ID's need to match the array of memory bases that the xop-interpreter passes to
597+
* the init function.
598+
*
599+
* In this method you specify offsets for tensors passed to the constructor
600+
*
601+
* @param input [I] OffsetBuffer containing Memory Identifier and Offset in that memory
602+
* @param output [I] OffsetBuffer containing Memory Identifier and Offset in that memory
603+
* @param weights_and_zeropts [I] OffsetBuffer containing Memory Identifier and Offset in that memory
604+
* @param inpzeropts [I] OffsetBuffer containing Memory Identifier and Offset in that memory
605+
* @param ctrl_buffer [I] OffsetBuffer containing Memory Identifier and Offset in that memory
606+
*
607+
* @return MLI status code
608+
*/
609+
virtual mli_status AttachBufferOffsets(const OffsetBuffer &input,
610+
const OffsetBuffer &output,
611+
const OffsetBuffer &weights_and_zeropts,
612+
const OffsetBuffer &inpzeropts,
613+
const OffsetBuffer &ctrl_buffer) {
614+
NOT_IMPLEMENTED_METHOD;
615+
return MLI_STATUS_OK; };
616+
568617
};
569618

570619
/**
@@ -637,6 +686,31 @@ class FullyConnected_CS : public CompilerGenericInterface {
637686
const OffsetBuffer &weights,
638687
const OffsetBuffer &wtszeropts,
639688
const OffsetBuffer &ctrl_buffer) = 0;
689+
690+
/**
691+
* @brief Method to set buffer memory offsets and memory IDs for the kernel
692+
*
693+
* Compiler computes a memory map and buffer offsets are set using this method.
694+
* Compiler also needs to indicate in which memory the buffers reside.
695+
* These ID's need to match the array of memory bases that the xop-interpreter passes to
696+
* the init function.
697+
*
698+
* In this method you specify offsets for tensors passed to the constructor
699+
*
700+
* @param input [I] OffsetBuffer containing Memory Identifier and Offset in that memory
701+
* @param output [I] OffsetBuffer containing Memory Identifier and Offset in that memory
702+
* @param weights_and_zeropts [I] OffsetBuffer containing Memory Identifier and Offset in that memory
703+
* @param ctrl_buffer [I] OffsetBuffer containing Memory Identifier and Offset in that memory
704+
*
705+
* @return MLI status code
706+
*/
707+
virtual mli_status AttachBufferOffsets(const OffsetBuffer &input,
708+
const OffsetBuffer &output,
709+
const OffsetBuffer &weights_and_zeropts,
710+
const OffsetBuffer &ctrl_buffer) {
711+
NOT_IMPLEMENTED_METHOD;
712+
return MLI_STATUS_OK; };
713+
640714
};
641715

642716
/**
@@ -1397,6 +1471,33 @@ class TransposeConv2D_CS : public CompilerGenericInterface {
13971471
const OffsetBuffer &inpzeropts,
13981472
const OffsetBuffer &wtszeropts,
13991473
const OffsetBuffer &ctrl_buffer) = 0;
1474+
1475+
/**
1476+
* @brief Method to set buffer memory offsets and memory IDs for the kernel
1477+
*
1478+
* Compiler computes a memory map and buffer offsets are set using this method.
1479+
* Compiler also needs to indicate in which memory the buffers reside.
1480+
* These ID's need to match the array of memory bases that the xop-interpreter passes to
1481+
* the init function.
1482+
*
1483+
* In this method you specify offsets for tensors passed to the constructor
1484+
*
1485+
* @param input [I] OffsetBuffer containing Memory Identifier and Offset in that memory
1486+
* @param output [I] OffsetBuffer containing Memory Identifier and Offset in that memory
1487+
* @param weights_and_zeropts [I] OffsetBuffer containing Memory Identifier and Offset in that memory
1488+
* @param inpzeropts [I] OffsetBuffer containing Memory Identifier and Offset in that memory
1489+
* @param ctrl_buffer [I] OffsetBuffer containing Memory Identifier and Offset in that memory
1490+
*
1491+
* @return MLI status code
1492+
*/
1493+
virtual mli_status AttachBufferOffsets(const OffsetBuffer &input,
1494+
const OffsetBuffer &output,
1495+
const OffsetBuffer &weights_and_zeropts,
1496+
const OffsetBuffer &inpzeropts,
1497+
const OffsetBuffer &ctrl_buffer) {
1498+
NOT_IMPLEMENTED_METHOD;
1499+
return MLI_STATUS_OK; };
1500+
14001501
};
14011502

14021503
/**

include/mli_iterator.hpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,10 +1012,18 @@ class TensorIterator {
10121012
uint32_t current_size[tensorRank];
10131013

10141014
for (uint32_t i = 0; i < tensorRank; i++) {
1015+
uint32_t tile_size = 0;
1016+
if (m_tile_idx[i] == m_config.get_count(i) - 1) { // Last iteration
1017+
tile_size = m_config.get_last_size(i);
1018+
} else if (m_tile_idx[i] == 0) { // First iteration
1019+
tile_size = m_config.get_first_size(i);
1020+
} else { // Middle iteration
1021+
tile_size = m_config.get_size(i);
1022+
}
10151023
current_size[i] =
1016-
m_full_tensor.get_dim(i) - m_pos[i] < m_config.get_size(i)
1024+
m_full_tensor.get_dim(i) - m_pos[i] < tile_size
10171025
? m_full_tensor.get_dim(i) - m_pos[i]
1018-
: m_config.get_size(i);
1026+
: tile_size;
10191027
}
10201028
int32_t order[iterRank] = {0};
10211029
for (uint32_t i = 0; i < iterRank; i++) {
@@ -1130,6 +1138,10 @@ class TensorIterator {
11301138
return !m_tile_idx[dim_idx];
11311139
}
11321140

1141+
bool is_last_tile(uint32_t dim_idx) const {
1142+
return m_tile_idx[dim_idx] == (m_config.get_count(dim_idx) - 1);
1143+
}
1144+
11331145
void SetCount(int32_t val, uint32_t dim) {
11341146
m_config.SetCount(val, dim);
11351147
}

lib/src/kernels/convolution/mli_krn_conv2d_runtime.cc

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -120,55 +120,28 @@ mli_status Conv2d::Update() {
120120
void Conv2d::UpdateTilePaddings() {
121121
memcpy(&m_tile_cfg, &m_metadata.cfg, sizeof(m_metadata.cfg));
122122

123-
int32_t tile_input_offsets[kConvIORank];
124123
const auto& input = m_metadata.input;
125-
input.get_pos(tile_input_offsets);
126-
const auto& input_it_cfg = input.get_config();
127124

128125
int32_t tile_idx[kConvIORank];
129126
input.get_tile_idx(tile_idx);
130127

131128
// top padding
132129
if (!input.is_first_tile(kGroupTensorHeightDim)) {
133-
//TODO: if first_inc != 0 get_first_inc + get_inc * (idx - 1)
134-
uint32_t pad_used = input_it_cfg.get_inc(kGroupTensorHeightDim) * tile_idx[kGroupTensorHeightDim];
135-
if (pad_used < m_tile_cfg.padding_begin[0]) {
136-
m_tile_cfg.padding_begin[0] -= pad_used;
137-
}
138-
else {
139-
m_tile_cfg.padding_begin[0] = 0;
140-
}
130+
m_tile_cfg.padding_begin[0] = 0;
141131
}
142132

143133
// left padding
144134
if (!input.is_first_tile(kGroupTensorWidthDim)) {
145-
// TODO: if first_inc != 0 get_first_inc + get_inc * (idx - 1)
146-
uint32_t pad_used = input_it_cfg.get_inc(kGroupTensorWidthDim) * tile_idx[kGroupTensorWidthDim];
147-
if (pad_used < m_tile_cfg.padding_begin[1]) {
148-
m_tile_cfg.padding_begin[1] -= pad_used;
149-
}
150-
else {
151-
m_tile_cfg.padding_begin[1] = 0;
152-
}
135+
m_tile_cfg.padding_begin[1] = 0;
153136
}
154137

155138
// bottom padding
156-
uint32_t used_size_y = (int32_t)(input.get_dim(kGroupTensorHeightDim) + m_tile_cfg.padding_begin[0]);
157-
int32_t pad_bot = tile_input_offsets[kGroupTensorHeightDim] + (int32_t)input_it_cfg.get_size(kGroupTensorHeightDim) - used_size_y;
158-
if (pad_bot > 0) {
159-
m_tile_cfg.padding_end[0] = MIN((uint32_t)pad_bot, m_metadata.cfg.padding_end[0]);
160-
}
161-
else {
139+
if (!input.is_last_tile(kGroupTensorHeightDim)) {
162140
m_tile_cfg.padding_end[0] = 0;
163141
}
164142

165143
// right padding
166-
uint32_t used_size_x = (int32_t)(input.get_dim(kGroupTensorWidthDim) + m_tile_cfg.padding_begin[1]);
167-
int32_t pad_right = tile_input_offsets[kGroupTensorWidthDim] + (int32_t)input_it_cfg.get_size(kGroupTensorWidthDim) - used_size_x;
168-
if (pad_right > 0) {
169-
m_tile_cfg.padding_end[1] = MIN((uint32_t)pad_right, m_metadata.cfg.padding_end[1]);
170-
}
171-
else {
144+
if (!input.is_last_tile(kGroupTensorWidthDim)) {
172145
m_tile_cfg.padding_end[1] = 0;
173146
}
174147
}

0 commit comments

Comments
 (0)