-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: YdrMaster <[email protected]>
- Loading branch information
Showing
8 changed files
with
149 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#ifndef KERNEL_EXPAND_INFO_H | ||
#define KERNEL_EXPAND_INFO_H | ||
|
||
#include "../tensor.h" | ||
#include <vector> | ||
|
||
namespace refactor::kernel { | ||
|
||
/// @brief 优化用于计算的单向广播描述。 | ||
struct ExpandInfo { | ||
struct Dim { | ||
dim_t i, o; | ||
|
||
bool operator==(Dim const &) const noexcept; | ||
bool operator!=(Dim const &) const noexcept; | ||
}; | ||
|
||
/// @brief 所有输入输出的各维度步长。 | ||
std::vector<Dim> strides; | ||
dim_t blockCount, blockSize; | ||
|
||
ExpandInfo(std::vector<Dim>, dim_t, dim_t) noexcept; | ||
ExpandInfo(Tensor const &input, Tensor const &output) noexcept; | ||
ExpandInfo reform(dim_t maxblockSize) const noexcept; | ||
void reformAssign(dim_t maxblockSize) noexcept; | ||
}; | ||
|
||
}// namespace refactor::kernel | ||
|
||
#endif// KERNEL_EXPAND_INFO_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
#include "kernel/attributes/expand_info.h" | ||
#include <numeric> | ||
|
||
namespace refactor::kernel { | ||
|
||
bool ExpandInfo::Dim::operator==(Dim const &rhs) const noexcept { | ||
return i == rhs.i && o == rhs.o; | ||
} | ||
bool ExpandInfo::Dim::operator!=(Dim const &rhs) const noexcept { | ||
return !operator==(rhs); | ||
} | ||
|
||
ExpandInfo::ExpandInfo( | ||
std::vector<Dim> strides_, | ||
dim_t blockCount_, | ||
dim_t blockSize_) noexcept | ||
: strides(std::move(strides_)), | ||
blockCount(blockCount_), | ||
blockSize(blockSize_) {} | ||
|
||
ExpandInfo::ExpandInfo( | ||
Tensor const &input, | ||
Tensor const &output) noexcept | ||
: strides{{1, 1}}, | ||
blockCount(1), | ||
blockSize(input.dataType.size()) { | ||
ASSERT(input.rank() <= output.rank(), "Unreachable"); | ||
auto i = input.shape.rbegin(), | ||
end = input.shape.rend(), | ||
o = output.shape.rbegin(); | ||
dim_t stride = 1; | ||
while (i != end) { | ||
auto i_ = *i++, | ||
o_ = *o++; | ||
if (o_ == 1) { continue; } | ||
if (auto &it = strides.back(); i_ == 1) { | ||
if (it.i == 0) { | ||
it.o *= o_; | ||
} else { | ||
strides.push_back({0, it.o * o_}); | ||
} | ||
} else { | ||
stride *= i_; | ||
if (it.i == 0) { | ||
strides.push_back({stride, it.o * o_}); | ||
} else { | ||
it.i = stride; | ||
it.o *= o_; | ||
} | ||
} | ||
} | ||
ASSERT(strides[0].i == strides[0].o, "Unreachable"); | ||
auto elementCount = strides[0].i; | ||
|
||
std::reverse(strides.begin(), strides.end()); | ||
strides.pop_back(); | ||
strides.shrink_to_fit(); | ||
|
||
blockSize *= elementCount; | ||
for (auto &s : strides) { | ||
s.i /= elementCount; | ||
s.o /= elementCount; | ||
} | ||
blockCount = std::accumulate(o, output.shape.rend(), strides[0].o, std::multiplies()); | ||
} | ||
|
||
ExpandInfo ExpandInfo::reform(dim_t maxblockSize) const noexcept { | ||
auto blockSize_ = std::gcd(blockSize, maxblockSize); | ||
if (blockSize_ == blockSize) { return *this; } | ||
auto times = blockSize / blockSize_; | ||
ExpandInfo ans( | ||
std::vector<Dim>(strides.size() + 1), | ||
blockCount * times, | ||
blockSize_); | ||
for (auto i : range0_(strides.size())) { | ||
ans.strides[i].i *= times; | ||
ans.strides[i].o *= times; | ||
} | ||
ans.strides.back() = {1, 1}; | ||
return ans; | ||
} | ||
void ExpandInfo::reformAssign(dim_t maxblockSize) noexcept { | ||
auto blockSize_ = std::gcd(blockSize, maxblockSize); | ||
if (blockSize_ == blockSize) { return; } | ||
auto times = blockSize / blockSize_; | ||
blockCount *= times; | ||
blockSize = blockSize_; | ||
for (auto &s : strides) { | ||
s.i *= times; | ||
s.o *= times; | ||
} | ||
strides.resize(strides.size() + 1); | ||
strides.back() = {1, 1}; | ||
} | ||
|
||
}// namespace refactor::kernel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#include "kernel/attributes/expand_info.h" | ||
#include <gtest/gtest.h> | ||
|
||
using namespace refactor; | ||
using namespace kernel; | ||
|
||
TEST(kernel, ExpandInfo) { | ||
auto input = Tensor::share(DataType::F32, {3, 4, 1, 6}), | ||
output = Tensor::share(DataType::F32, {2, 3, 4, 5, 6}); | ||
ExpandInfo info(*input, *output); | ||
EXPECT_EQ(info.blockSize, 24); | ||
EXPECT_EQ(info.blockCount, 120); | ||
EXPECT_EQ(info.strides, (std::vector<ExpandInfo::Dim>{{12, 60}, {0, 5}})); | ||
} |