Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[luci/pass] Introduce ArrayIndex helper #14565

Merged
merged 8 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions compiler/luci/pass/src/helpers/ArrayIndex.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (c) 2025 Samsung Electronics Co., Ltd. All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "ArrayIndex.h"

#include <cassert>

namespace luci
{

Array4DIndex::Array4DIndex(uint32_t D0, uint32_t D1, uint32_t D2, uint32_t D3)
: _dim{D0, D1, D2, D3}
{
assert(D0 > 0 && D1 > 0 && D2 > 0 && D3 > 0);

_strides[3] = 1;
_strides[2] = D3;
_strides[1] = D3 * D2;
_strides[0] = D3 * D2 * D1;

for (int i = 0; i < 4; ++i)
{
assert(_strides[i] > 0);
}
}
uint32_t Array4DIndex::operator()(uint32_t i0, uint32_t i1, uint32_t i2, uint32_t i3) const
{
seanshpark marked this conversation as resolved.
Show resolved Hide resolved
assert(i0 < _dim[0] && i1 < _dim[1] && i2 < _dim[2] && i3 < _dim[3]);
return i0 * _strides[0] + i1 * _strides[1] + i2 * _strides[2] + i3 * _strides[3];
}

uint32_t Array4DIndex::size(void) const { return _strides[3]; }
uint32_t Array4DIndex::stride(uint32_t axis) const { return _strides[axis]; }

} // namespace luci
52 changes: 52 additions & 0 deletions compiler/luci/pass/src/helpers/ArrayIndex.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2025 Samsung Electronics Co., Ltd. All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef __LUCI_ARRAY_INDEX_H__
seanshpark marked this conversation as resolved.
Show resolved Hide resolved
#define __LUCI_ARRAY_INDEX_H__

#include <cstdint>

namespace luci
{

/**
* @brief Index class for 4D tensor (NHWC) to calculate linear index from multi-dimensional indices.
seanshpark marked this conversation as resolved.
Show resolved Hide resolved
*/
class Array4DIndex
dayo09 marked this conversation as resolved.
Show resolved Hide resolved
{
public:
Array4DIndex(uint32_t N, uint32_t H, uint32_t W, uint32_t D);
/**
* @brief Calculate linear index from multi-dimensional indices.
*/
seanshpark marked this conversation as resolved.
Show resolved Hide resolved
uint32_t operator()(uint32_t n, uint32_t h, uint32_t w, uint32_t d) const;
/**
* @brief Get total number of elements in the tensor.
*/
uint32_t size(void) const;
/**
* @brief Get stride of the given axis.
*/
uint32_t stride(uint32_t axis) const;

protected:
dayo09 marked this conversation as resolved.
Show resolved Hide resolved
uint32_t _dim[4];
uint32_t _strides[4];
};

} // namespace luci

#endif // __LUCI_ARRAY_INDEX_H__
40 changes: 40 additions & 0 deletions compiler/luci/pass/src/helpers/ArrayIndex.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2025 Samsung Electronics Co., Ltd. All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "ArrayIndex.h"

#include <gtest/gtest.h>

TEST(LuciPassHelpersArrayIndex, array_index_4d)
{
luci::Array4DIndex idx(5, 4, 3, 2);

EXPECT_EQ(idx.rank(), 4);

EXPECT_EQ(idx(0, 0, 0, 0), 0);

EXPECT_EQ(idx(1, 0, 0, 0), idx.stride(0));
EXPECT_EQ(idx(0, 1, 0, 0), idx.stride(1));
EXPECT_EQ(idx(0, 0, 1, 0), idx.stride(2));
EXPECT_EQ(idx(0, 0, 0, 1), idx.stride(3));

EXPECT_EQ(idx(4, 3, 2, 1), 4 * 4 * 3 * 2 + 3 * 3 * 2 + 2 * 2 + 1);
}

TEST(LuciPassHelpersArrayIndex, array_index_4d_NEG)
dayo09 marked this conversation as resolved.
Show resolved Hide resolved
{
ASSERT_THROW(luci::Array4DIndex idx(5, 4, 3, -1));
}
Loading