Skip to content

Commit

Permalink
fix c++03
Browse files Browse the repository at this point in the history
  • Loading branch information
Baiyuetribe committed Jan 6, 2025
1 parent f3342d5 commit 2440a7f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 36 deletions.
43 changes: 9 additions & 34 deletions src/layer/topk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,31 +59,6 @@ int TopK::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_bl
Mat& top_blob_values = top_blobs[0]; // values
Mat& top_blob_indices = top_blobs[1]; // indices

// // 根据largest参数定义比较函数
// auto comp = [this](const std::pair<float, int> &a, const std::pair<float, int> &b)
// {
// if (a.first == b.first)
// return a.second < b.second; // 值相等时按索引升序排序
// return this->largest ? (a.first > b.first) : (a.first < b.first);
// };

// simplestl兼容写法
struct CompareFunc
{
bool largest;
CompareFunc(bool l)
: largest(l)
{
}
bool operator()(const std::pair<float, int>& a, const std::pair<float, int>& b) const
{
if (a.first == b.first)
return a.second < b.second; // 值相等时按索引升序排序
return largest ? (a.first > b.first) : (a.first < b.first);
}
};
CompareFunc comp(largest);

// 根据dims创建不同维度的输出
if (dims == 1)
{
Expand All @@ -102,7 +77,7 @@ int TopK::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_bl
}

// 根据sorted参数选择排序方式
do_sort(vec, k, sorted, comp);
do_sort(vec, k, sorted);

// 保存结果
for (int i = 0; i < k; i++)
Expand All @@ -129,7 +104,7 @@ int TopK::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_bl
vec[i] = std::make_pair(bottom_blob.row(i)[j], i);
}

do_sort(vec, k, sorted, comp);
do_sort(vec, k, sorted);

// 保存结果到对应列
for (int i = 0; i < k; i++)
Expand Down Expand Up @@ -157,7 +132,7 @@ int TopK::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_bl
vec[j] = std::make_pair(ptr[j], j);
}

do_sort(vec, k, sorted, comp);
do_sort(vec, k, sorted);

for (int j = 0; j < k; j++)
{
Expand Down Expand Up @@ -188,7 +163,7 @@ int TopK::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_bl
}

// 排序
do_sort(channel_values, k, sorted, comp);
do_sort(channel_values, k, sorted);

// 写回结果
for (int c = 0; c < k; c++)
Expand Down Expand Up @@ -219,7 +194,7 @@ int TopK::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_bl
}

// 找到最大行的索引
do_sort(row_scores, k, sorted, comp);
do_sort(row_scores, k, sorted);

// 保存该列的结果
for (int i = 0; i < k; i++)
Expand Down Expand Up @@ -251,7 +226,7 @@ int TopK::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_bl
vec[i] = std::make_pair(ptr[i], i);
}

do_sort(vec, k, sorted, comp);
do_sort(vec, k, sorted);

for (int i = 0; i < k; i++)
{
Expand Down Expand Up @@ -307,7 +282,7 @@ int TopK::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_bl
row_scores[j] = std::make_pair(ptr[offset], j);
}

do_sort(row_scores, k, sorted, comp);
do_sort(row_scores, k, sorted);

// 循环写入前 k 个值
for (int kk = 0; kk < k; kk++)
Expand Down Expand Up @@ -342,7 +317,7 @@ int TopK::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_bl
row_scores[j] = std::make_pair(ptr[offset], j);
}

do_sort(row_scores, k, sorted, comp);
do_sort(row_scores, k, sorted);

// 写回结果
for (int kk = 0; kk < k; kk++)
Expand Down Expand Up @@ -374,7 +349,7 @@ int TopK::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_bl
row_values[j] = std::make_pair(ptr[j], j);
}

do_sort(row_values, k, sorted, comp);
do_sort(row_values, k, sorted);

// 写回结果
for (int j = 0; j < k; j++)
Expand Down
26 changes: 24 additions & 2 deletions src/layer/topk.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,31 @@ class TopK : public Layer
int sorted;

private:
template<typename T>
void do_sort(std::vector<std::pair<float, int> >& vec, int k, bool sorted, const T& comp) const
// auto comp = [this](const std::pair<float, int> &a, const std::pair<float, int> &b)
// {
// if (a.first == b.first)
// return a.second < b.second; // 值相等时按索引升序排序
// return this->largest ? (a.first > b.first) : (a.first < b.first);
// };

// simplestl兼容写法
struct CompareFunc
{
bool largest;
CompareFunc(bool l)
: largest(l)
{
}
bool operator()(const std::pair<float, int>& a, const std::pair<float, int>& b) const
{
if (a.first == b.first)
return a.second < b.second; // 值相等时按索引升序排序
return largest ? (a.first > b.first) : (a.first < b.first);
}
};
void do_sort(std::vector<std::pair<float, int> >& vec, int k, bool sorted) const
{
CompareFunc comp(largest); // 兼容c++03
if (sorted)
{
std::partial_sort(vec.begin(), vec.begin() + k, vec.end(), comp);
Expand Down

0 comments on commit 2440a7f

Please sign in to comment.