/src/libjxl/lib/jxl/enc_cluster.cc
Line | Count | Source |
1 | | // Copyright (c) the JPEG XL Project Authors. All rights reserved. |
2 | | // |
3 | | // Use of this source code is governed by a BSD-style |
4 | | // license that can be found in the LICENSE file. |
5 | | |
6 | | #include "lib/jxl/enc_cluster.h" |
7 | | |
8 | | #include <algorithm> |
9 | | #include <cstddef> |
10 | | #include <cstdint> |
11 | | #include <limits> |
12 | | #include <map> |
13 | | #include <numeric> |
14 | | #include <queue> |
15 | | #include <tuple> |
16 | | #include <vector> |
17 | | |
18 | | #include "lib/jxl/base/status.h" |
19 | | #include "lib/jxl/enc_ans_params.h" |
20 | | |
21 | | #undef HWY_TARGET_INCLUDE |
22 | | #define HWY_TARGET_INCLUDE "lib/jxl/enc_cluster.cc" |
23 | | #include <hwy/foreach_target.h> |
24 | | #include <hwy/highway.h> |
25 | | |
26 | | #include "lib/jxl/base/fast_math-inl.h" |
27 | | HWY_BEFORE_NAMESPACE(); |
28 | | namespace jxl { |
29 | | namespace HWY_NAMESPACE { |
30 | | |
31 | | // These templates are not found via ADL. |
32 | | using hwy::HWY_NAMESPACE::AllTrue; |
33 | | using hwy::HWY_NAMESPACE::Eq; |
34 | | using hwy::HWY_NAMESPACE::GetLane; |
35 | | using hwy::HWY_NAMESPACE::IfThenZeroElse; |
36 | | using hwy::HWY_NAMESPACE::SumOfLanes; |
37 | | using hwy::HWY_NAMESPACE::Zero; |
38 | | |
39 | | template <class V> |
40 | 948M | V Entropy(V count, V inv_total, V total) { |
41 | 948M | const HWY_CAPPED(float, Histogram::kRounding) d; |
42 | 948M | const auto zero = Set(d, 0.0f); |
43 | | // TODO(eustas): why (0 - x) instead of Neg(x)? |
44 | 948M | return IfThenZeroElse( |
45 | 948M | Eq(count, total), |
46 | 948M | Sub(zero, Mul(count, FastLog2f(d, Mul(inv_total, count))))); |
47 | 948M | } Unexecuted instantiation: hwy::N_SSE4::Vec128<float, 4ul> jxl::N_SSE4::Entropy<hwy::N_SSE4::Vec128<float, 4ul> >(hwy::N_SSE4::Vec128<float, 4ul>, hwy::N_SSE4::Vec128<float, 4ul>, hwy::N_SSE4::Vec128<float, 4ul>) hwy::N_AVX2::Vec256<float> jxl::N_AVX2::Entropy<hwy::N_AVX2::Vec256<float> >(hwy::N_AVX2::Vec256<float>, hwy::N_AVX2::Vec256<float>, hwy::N_AVX2::Vec256<float>) Line | Count | Source | 40 | 948M | V Entropy(V count, V inv_total, V total) { | 41 | 948M | const HWY_CAPPED(float, Histogram::kRounding) d; | 42 | 948M | const auto zero = Set(d, 0.0f); | 43 | | // TODO(eustas): why (0 - x) instead of Neg(x)? | 44 | 948M | return IfThenZeroElse( | 45 | 948M | Eq(count, total), | 46 | 948M | Sub(zero, Mul(count, FastLog2f(d, Mul(inv_total, count))))); | 47 | 948M | } |
Unexecuted instantiation: hwy::N_AVX3::Vec256<float> jxl::N_AVX3::Entropy<hwy::N_AVX3::Vec256<float> >(hwy::N_AVX3::Vec256<float>, hwy::N_AVX3::Vec256<float>, hwy::N_AVX3::Vec256<float>) Unexecuted instantiation: hwy::N_AVX3_ZEN4::Vec256<float> jxl::N_AVX3_ZEN4::Entropy<hwy::N_AVX3_ZEN4::Vec256<float> >(hwy::N_AVX3_ZEN4::Vec256<float>, hwy::N_AVX3_ZEN4::Vec256<float>, hwy::N_AVX3_ZEN4::Vec256<float>) Unexecuted instantiation: hwy::N_AVX3_SPR::Vec256<float> jxl::N_AVX3_SPR::Entropy<hwy::N_AVX3_SPR::Vec256<float> >(hwy::N_AVX3_SPR::Vec256<float>, hwy::N_AVX3_SPR::Vec256<float>, hwy::N_AVX3_SPR::Vec256<float>) Unexecuted instantiation: hwy::N_SSE2::Vec128<float, 4ul> jxl::N_SSE2::Entropy<hwy::N_SSE2::Vec128<float, 4ul> >(hwy::N_SSE2::Vec128<float, 4ul>, hwy::N_SSE2::Vec128<float, 4ul>, hwy::N_SSE2::Vec128<float, 4ul>) |
48 | | |
49 | 1.01M | void HistogramCondition(Histogram& a) { |
50 | 1.01M | const HWY_CAPPED(int32_t, Histogram::kRounding) di; |
51 | 1.01M | const auto kZero = Zero(di); |
52 | 1.01M | auto total = kZero; |
53 | 1.01M | int nz_pos = -static_cast<int>(Lanes(di)); |
54 | 5.65M | for (size_t i = 0; i < a.counts.size(); i += Lanes(di)) { |
55 | 4.64M | const auto counts = LoadU(di, &a.counts[i]); |
56 | 4.64M | const bool nz = !AllTrue(di, Eq(counts, kZero)); |
57 | 4.64M | total = Add(total, counts); |
58 | 4.64M | if (nz) nz_pos = i; |
59 | 4.64M | } |
60 | 1.01M | a.counts.resize(nz_pos + Lanes(di)); |
61 | 1.01M | a.total_count = GetLane(SumOfLanes(di, total)); |
62 | 1.01M | } Unexecuted instantiation: jxl::N_SSE4::HistogramCondition(jxl::Histogram&) jxl::N_AVX2::HistogramCondition(jxl::Histogram&) Line | Count | Source | 49 | 1.01M | void HistogramCondition(Histogram& a) { | 50 | 1.01M | const HWY_CAPPED(int32_t, Histogram::kRounding) di; | 51 | 1.01M | const auto kZero = Zero(di); | 52 | 1.01M | auto total = kZero; | 53 | 1.01M | int nz_pos = -static_cast<int>(Lanes(di)); | 54 | 5.65M | for (size_t i = 0; i < a.counts.size(); i += Lanes(di)) { | 55 | 4.64M | const auto counts = LoadU(di, &a.counts[i]); | 56 | 4.64M | const bool nz = !AllTrue(di, Eq(counts, kZero)); | 57 | 4.64M | total = Add(total, counts); | 58 | 4.64M | if (nz) nz_pos = i; | 59 | 4.64M | } | 60 | 1.01M | a.counts.resize(nz_pos + Lanes(di)); | 61 | 1.01M | a.total_count = GetLane(SumOfLanes(di, total)); | 62 | 1.01M | } |
Unexecuted instantiation: jxl::N_AVX3::HistogramCondition(jxl::Histogram&) Unexecuted instantiation: jxl::N_AVX3_ZEN4::HistogramCondition(jxl::Histogram&) Unexecuted instantiation: jxl::N_AVX3_SPR::HistogramCondition(jxl::Histogram&) Unexecuted instantiation: jxl::N_SSE2::HistogramCondition(jxl::Histogram&) |
63 | | |
64 | 41.0M | void HistogramEntropy(const Histogram& a) { |
65 | 41.0M | a.entropy = 0.0f; |
66 | 41.0M | if (a.total_count == 0) return; |
67 | | |
68 | 16.9M | const HWY_CAPPED(float, Histogram::kRounding) df; |
69 | 16.9M | const HWY_CAPPED(int32_t, Histogram::kRounding) di; |
70 | | |
71 | 16.9M | const auto inv_tot = Set(df, 1.0f / a.total_count); |
72 | 16.9M | auto entropy_lanes = Zero(df); |
73 | 16.9M | auto total = Set(df, a.total_count); |
74 | | |
75 | 48.8M | for (size_t i = 0; i < a.counts.size(); i += Lanes(di)) { |
76 | 31.8M | const auto counts = LoadU(di, &a.counts[i]); |
77 | 31.8M | entropy_lanes = |
78 | 31.8M | Add(entropy_lanes, Entropy(ConvertTo(df, counts), inv_tot, total)); |
79 | 31.8M | } |
80 | 16.9M | a.entropy += GetLane(SumOfLanes(df, entropy_lanes)); |
81 | 16.9M | } Unexecuted instantiation: jxl::N_SSE4::HistogramEntropy(jxl::Histogram const&) jxl::N_AVX2::HistogramEntropy(jxl::Histogram const&) Line | Count | Source | 64 | 41.0M | void HistogramEntropy(const Histogram& a) { | 65 | 41.0M | a.entropy = 0.0f; | 66 | 41.0M | if (a.total_count == 0) return; | 67 | | | 68 | 16.9M | const HWY_CAPPED(float, Histogram::kRounding) df; | 69 | 16.9M | const HWY_CAPPED(int32_t, Histogram::kRounding) di; | 70 | | | 71 | 16.9M | const auto inv_tot = Set(df, 1.0f / a.total_count); | 72 | 16.9M | auto entropy_lanes = Zero(df); | 73 | 16.9M | auto total = Set(df, a.total_count); | 74 | | | 75 | 48.8M | for (size_t i = 0; i < a.counts.size(); i += Lanes(di)) { | 76 | 31.8M | const auto counts = LoadU(di, &a.counts[i]); | 77 | 31.8M | entropy_lanes = | 78 | 31.8M | Add(entropy_lanes, Entropy(ConvertTo(df, counts), inv_tot, total)); | 79 | 31.8M | } | 80 | 16.9M | a.entropy += GetLane(SumOfLanes(df, entropy_lanes)); | 81 | 16.9M | } |
Unexecuted instantiation: jxl::N_AVX3::HistogramEntropy(jxl::Histogram const&) Unexecuted instantiation: jxl::N_AVX3_ZEN4::HistogramEntropy(jxl::Histogram const&) Unexecuted instantiation: jxl::N_AVX3_SPR::HistogramEntropy(jxl::Histogram const&) Unexecuted instantiation: jxl::N_SSE2::HistogramEntropy(jxl::Histogram const&) |
82 | | |
83 | 321M | float HistogramDistance(const Histogram& a, const Histogram& b) { |
84 | 321M | if (a.total_count == 0 || b.total_count == 0) return 0; |
85 | | |
86 | 321M | const HWY_CAPPED(float, Histogram::kRounding) df; |
87 | 321M | const HWY_CAPPED(int32_t, Histogram::kRounding) di; |
88 | | |
89 | 321M | const auto inv_tot = Set(df, 1.0f / (a.total_count + b.total_count)); |
90 | 321M | auto distance_lanes = Zero(df); |
91 | 321M | auto total = Set(df, a.total_count + b.total_count); |
92 | | |
93 | 1.23G | for (size_t i = 0; i < std::max(a.counts.size(), b.counts.size()); |
94 | 916M | i += Lanes(di)) { |
95 | 916M | const auto a_counts = |
96 | 916M | a.counts.size() > i ? LoadU(di, &a.counts[i]) : Zero(di); |
97 | 916M | const auto b_counts = |
98 | 916M | b.counts.size() > i ? LoadU(di, &b.counts[i]) : Zero(di); |
99 | 916M | const auto counts = ConvertTo(df, Add(a_counts, b_counts)); |
100 | 916M | distance_lanes = Add(distance_lanes, Entropy(counts, inv_tot, total)); |
101 | 916M | } |
102 | 321M | const float total_distance = GetLane(SumOfLanes(df, distance_lanes)); |
103 | 321M | return total_distance - a.entropy - b.entropy; |
104 | 321M | } Unexecuted instantiation: jxl::N_SSE4::HistogramDistance(jxl::Histogram const&, jxl::Histogram const&) jxl::N_AVX2::HistogramDistance(jxl::Histogram const&, jxl::Histogram const&) Line | Count | Source | 83 | 321M | float HistogramDistance(const Histogram& a, const Histogram& b) { | 84 | 321M | if (a.total_count == 0 || b.total_count == 0) return 0; | 85 | | | 86 | 321M | const HWY_CAPPED(float, Histogram::kRounding) df; | 87 | 321M | const HWY_CAPPED(int32_t, Histogram::kRounding) di; | 88 | | | 89 | 321M | const auto inv_tot = Set(df, 1.0f / (a.total_count + b.total_count)); | 90 | 321M | auto distance_lanes = Zero(df); | 91 | 321M | auto total = Set(df, a.total_count + b.total_count); | 92 | | | 93 | 1.23G | for (size_t i = 0; i < std::max(a.counts.size(), b.counts.size()); | 94 | 916M | i += Lanes(di)) { | 95 | 916M | const auto a_counts = | 96 | 916M | a.counts.size() > i ? LoadU(di, &a.counts[i]) : Zero(di); | 97 | 916M | const auto b_counts = | 98 | 916M | b.counts.size() > i ? LoadU(di, &b.counts[i]) : Zero(di); | 99 | 916M | const auto counts = ConvertTo(df, Add(a_counts, b_counts)); | 100 | 916M | distance_lanes = Add(distance_lanes, Entropy(counts, inv_tot, total)); | 101 | 916M | } | 102 | 321M | const float total_distance = GetLane(SumOfLanes(df, distance_lanes)); | 103 | 321M | return total_distance - a.entropy - b.entropy; | 104 | 321M | } |
Unexecuted instantiation: jxl::N_AVX3::HistogramDistance(jxl::Histogram const&, jxl::Histogram const&) Unexecuted instantiation: jxl::N_AVX3_ZEN4::HistogramDistance(jxl::Histogram const&, jxl::Histogram const&) Unexecuted instantiation: jxl::N_AVX3_SPR::HistogramDistance(jxl::Histogram const&, jxl::Histogram const&) Unexecuted instantiation: jxl::N_SSE2::HistogramDistance(jxl::Histogram const&, jxl::Histogram const&) |
105 | | |
106 | | constexpr const float kInfinity = std::numeric_limits<float>::infinity(); |
107 | | |
108 | | // With prefix codes, a histogram with a single symbol costs nothing per |
109 | | // symbol, and one with more costs at least one bit per symbol. The Shannon |
110 | | // entropy misses that: a mostly-zero 2-symbol histogram looks nearly free to |
111 | | // merge into large all-zero ones, which then cost a bit per zero. So such a |
112 | | // merge costs at least a bit per symbol of the single-symbol side(s). |
113 | 0 | size_t NumSymbols(const Histogram& a, const Histogram* b) { |
114 | 0 | size_t n = 0; |
115 | 0 | const size_t size = |
116 | 0 | std::max(a.counts.size(), b ? b->counts.size() : size_t{0}); |
117 | 0 | for (size_t i = 0; i < size; i++) { |
118 | 0 | const int32_t count = (i < a.counts.size() ? a.counts[i] : 0) + |
119 | 0 | (b && i < b->counts.size() ? b->counts[i] : 0); |
120 | 0 | n += count > 0; |
121 | 0 | } |
122 | 0 | return n; |
123 | 0 | } Unexecuted instantiation: jxl::N_SSE4::NumSymbols(jxl::Histogram const&, jxl::Histogram const*) Unexecuted instantiation: jxl::N_AVX2::NumSymbols(jxl::Histogram const&, jxl::Histogram const*) Unexecuted instantiation: jxl::N_AVX3::NumSymbols(jxl::Histogram const&, jxl::Histogram const*) Unexecuted instantiation: jxl::N_AVX3_ZEN4::NumSymbols(jxl::Histogram const&, jxl::Histogram const*) Unexecuted instantiation: jxl::N_AVX3_SPR::NumSymbols(jxl::Histogram const&, jxl::Histogram const*) Unexecuted instantiation: jxl::N_SSE2::NumSymbols(jxl::Histogram const&, jxl::Histogram const*) |
124 | | float ClusterDistance(const Histogram& a, const Histogram& b, |
125 | 321M | bool prefix_codes) { |
126 | 321M | const float distance = HistogramDistance(a, b); |
127 | 321M | if (!prefix_codes || a.total_count == 0 || b.total_count == 0 || |
128 | 321M | NumSymbols(a, &b) <= 1) { |
129 | 321M | return distance; |
130 | 321M | } |
131 | 0 | float single = 0; |
132 | 0 | if (NumSymbols(a, nullptr) <= 1) single += a.total_count; |
133 | 0 | if (NumSymbols(b, nullptr) <= 1) single += b.total_count; |
134 | 0 | return std::max(distance, single); |
135 | 321M | } Unexecuted instantiation: jxl::N_SSE4::ClusterDistance(jxl::Histogram const&, jxl::Histogram const&, bool) jxl::N_AVX2::ClusterDistance(jxl::Histogram const&, jxl::Histogram const&, bool) Line | Count | Source | 125 | 321M | bool prefix_codes) { | 126 | 321M | const float distance = HistogramDistance(a, b); | 127 | 321M | if (!prefix_codes || a.total_count == 0 || b.total_count == 0 || | 128 | 321M | NumSymbols(a, &b) <= 1) { | 129 | 321M | return distance; | 130 | 321M | } | 131 | 0 | float single = 0; | 132 | 0 | if (NumSymbols(a, nullptr) <= 1) single += a.total_count; | 133 | 0 | if (NumSymbols(b, nullptr) <= 1) single += b.total_count; | 134 | 0 | return std::max(distance, single); | 135 | 321M | } |
Unexecuted instantiation: jxl::N_AVX3::ClusterDistance(jxl::Histogram const&, jxl::Histogram const&, bool) Unexecuted instantiation: jxl::N_AVX3_ZEN4::ClusterDistance(jxl::Histogram const&, jxl::Histogram const&, bool) Unexecuted instantiation: jxl::N_AVX3_SPR::ClusterDistance(jxl::Histogram const&, jxl::Histogram const&, bool) Unexecuted instantiation: jxl::N_SSE2::ClusterDistance(jxl::Histogram const&, jxl::Histogram const&, bool) |
136 | | |
137 | 35.9k | float HistogramKLDivergence(const Histogram& actual, const Histogram& coding) { |
138 | 35.9k | if (actual.total_count == 0) return 0; |
139 | 35.9k | if (coding.total_count == 0) return kInfinity; |
140 | | |
141 | 35.9k | const HWY_CAPPED(float, Histogram::kRounding) df; |
142 | 35.9k | const HWY_CAPPED(int32_t, Histogram::kRounding) di; |
143 | | |
144 | 35.9k | const auto coding_inv = Set(df, 1.0f / coding.total_count); |
145 | 35.9k | auto cost_lanes = Zero(df); |
146 | | |
147 | 91.1k | for (size_t i = 0; i < actual.counts.size(); i += Lanes(di)) { |
148 | 55.1k | const auto counts = LoadU(di, &actual.counts[i]); |
149 | 55.1k | const auto coding_counts = |
150 | 55.1k | coding.counts.size() > i ? LoadU(di, &coding.counts[i]) : Zero(di); |
151 | 55.1k | const auto coding_probs = Mul(ConvertTo(df, coding_counts), coding_inv); |
152 | 55.1k | const auto neg_coding_cost = BitCast( |
153 | 55.1k | df, |
154 | 55.1k | IfThenZeroElse(Eq(counts, Zero(di)), |
155 | 55.1k | IfThenElse(Eq(coding_counts, Zero(di)), |
156 | 55.1k | BitCast(di, Set(df, -kInfinity)), |
157 | 55.1k | BitCast(di, FastLog2f(df, coding_probs))))); |
158 | 55.1k | cost_lanes = NegMulAdd(ConvertTo(df, counts), neg_coding_cost, cost_lanes); |
159 | 55.1k | } |
160 | 35.9k | const float total_cost = GetLane(SumOfLanes(df, cost_lanes)); |
161 | 35.9k | return total_cost - actual.entropy; |
162 | 35.9k | } Unexecuted instantiation: jxl::N_SSE4::HistogramKLDivergence(jxl::Histogram const&, jxl::Histogram const&) jxl::N_AVX2::HistogramKLDivergence(jxl::Histogram const&, jxl::Histogram const&) Line | Count | Source | 137 | 35.9k | float HistogramKLDivergence(const Histogram& actual, const Histogram& coding) { | 138 | 35.9k | if (actual.total_count == 0) return 0; | 139 | 35.9k | if (coding.total_count == 0) return kInfinity; | 140 | | | 141 | 35.9k | const HWY_CAPPED(float, Histogram::kRounding) df; | 142 | 35.9k | const HWY_CAPPED(int32_t, Histogram::kRounding) di; | 143 | | | 144 | 35.9k | const auto coding_inv = Set(df, 1.0f / coding.total_count); | 145 | 35.9k | auto cost_lanes = Zero(df); | 146 | | | 147 | 91.1k | for (size_t i = 0; i < actual.counts.size(); i += Lanes(di)) { | 148 | 55.1k | const auto counts = LoadU(di, &actual.counts[i]); | 149 | 55.1k | const auto coding_counts = | 150 | 55.1k | coding.counts.size() > i ? LoadU(di, &coding.counts[i]) : Zero(di); | 151 | 55.1k | const auto coding_probs = Mul(ConvertTo(df, coding_counts), coding_inv); | 152 | 55.1k | const auto neg_coding_cost = BitCast( | 153 | 55.1k | df, | 154 | 55.1k | IfThenZeroElse(Eq(counts, Zero(di)), | 155 | 55.1k | IfThenElse(Eq(coding_counts, Zero(di)), | 156 | 55.1k | BitCast(di, Set(df, -kInfinity)), | 157 | 55.1k | BitCast(di, FastLog2f(df, coding_probs))))); | 158 | 55.1k | cost_lanes = NegMulAdd(ConvertTo(df, counts), neg_coding_cost, cost_lanes); | 159 | 55.1k | } | 160 | 35.9k | const float total_cost = GetLane(SumOfLanes(df, cost_lanes)); | 161 | 35.9k | return total_cost - actual.entropy; | 162 | 35.9k | } |
Unexecuted instantiation: jxl::N_AVX3::HistogramKLDivergence(jxl::Histogram const&, jxl::Histogram const&) Unexecuted instantiation: jxl::N_AVX3_ZEN4::HistogramKLDivergence(jxl::Histogram const&, jxl::Histogram const&) Unexecuted instantiation: jxl::N_AVX3_SPR::HistogramKLDivergence(jxl::Histogram const&, jxl::Histogram const&) Unexecuted instantiation: jxl::N_SSE2::HistogramKLDivergence(jxl::Histogram const&, jxl::Histogram const&) |
163 | | |
164 | | // First step of a k-means clustering with a fancy distance metric. |
165 | | Status FastClusterHistograms(const std::vector<Histogram>& in, |
166 | | size_t max_histograms, bool prefix_codes, |
167 | | std::vector<Histogram>* out, |
168 | 34.7k | std::vector<uint32_t>* histogram_symbols) { |
169 | 34.7k | const size_t prev_histograms = out->size(); |
170 | 34.7k | out->reserve(max_histograms); |
171 | 34.7k | histogram_symbols->clear(); |
172 | 34.7k | histogram_symbols->resize(in.size(), max_histograms); |
173 | | |
174 | 34.7k | std::vector<float> dists(in.size(), std::numeric_limits<float>::max()); |
175 | 34.7k | size_t largest_idx = 0; |
176 | 33.0M | for (size_t i = 0; i < in.size(); i++) { |
177 | 33.0M | if (in[i].total_count == 0) { |
178 | 27.2M | (*histogram_symbols)[i] = 0; |
179 | 27.2M | dists[i] = 0.0f; |
180 | 27.2M | continue; |
181 | 27.2M | } |
182 | 5.74M | HistogramEntropy(in[i]); |
183 | 5.74M | if (in[i].total_count > in[largest_idx].total_count) { |
184 | 65.9k | largest_idx = i; |
185 | 65.9k | } |
186 | 5.74M | } |
187 | | |
188 | 34.7k | if (prev_histograms > 0) { |
189 | 176 | for (size_t j = 0; j < prev_histograms; ++j) { |
190 | 88 | HistogramEntropy((*out)[j]); |
191 | 88 | } |
192 | 183k | for (size_t i = 0; i < in.size(); i++) { |
193 | 183k | if (dists[i] == 0.0f) continue; |
194 | 36.5k | for (size_t j = 0; j < prev_histograms; ++j) { |
195 | 18.2k | dists[i] = std::min(HistogramKLDivergence(in[i], (*out)[j]), dists[i]); |
196 | 18.2k | } |
197 | 18.2k | } |
198 | 88 | auto max_dist = std::max_element(dists.begin(), dists.end()); |
199 | 88 | if (*max_dist > 0.0f) { |
200 | 88 | largest_idx = max_dist - dists.begin(); |
201 | 88 | } |
202 | 88 | } |
203 | | |
204 | 34.7k | constexpr float kMinDistanceForDistinct = 48.0f; |
205 | 246k | while (out->size() < max_histograms) { |
206 | 246k | (*histogram_symbols)[largest_idx] = out->size(); |
207 | 246k | out->push_back(in[largest_idx]); |
208 | 246k | dists[largest_idx] = 0.0f; |
209 | 246k | largest_idx = 0; |
210 | 617M | for (size_t i = 0; i < in.size(); i++) { |
211 | 616M | if (dists[i] == 0.0f) continue; |
212 | 162M | dists[i] = |
213 | 162M | std::min(ClusterDistance(in[i], out->back(), prefix_codes), dists[i]); |
214 | 162M | if (dists[i] > dists[largest_idx]) largest_idx = i; |
215 | 162M | } |
216 | 246k | if (dists[largest_idx] < kMinDistanceForDistinct) break; |
217 | 246k | } |
218 | | |
219 | 33.0M | for (size_t i = 0; i < in.size(); i++) { |
220 | 33.0M | if ((*histogram_symbols)[i] != max_histograms) continue; |
221 | 5.49M | size_t best = 0; |
222 | 5.49M | float best_dist = std::numeric_limits<float>::max(); |
223 | 164M | for (size_t j = 0; j < out->size(); j++) { |
224 | 158M | float dist = j < prev_histograms |
225 | 158M | ? HistogramKLDivergence(in[i], (*out)[j]) |
226 | 158M | : ClusterDistance(in[i], (*out)[j], prefix_codes); |
227 | 158M | if (dist < best_dist) { |
228 | 15.6M | best = j; |
229 | 15.6M | best_dist = dist; |
230 | 15.6M | } |
231 | 158M | } |
232 | 5.49M | JXL_ENSURE(best_dist < std::numeric_limits<float>::max()); |
233 | 5.49M | if (best >= prev_histograms) { |
234 | 5.49M | (*out)[best].AddHistogram(in[i]); |
235 | 5.49M | HistogramEntropy((*out)[best]); |
236 | 5.49M | } |
237 | 5.49M | (*histogram_symbols)[i] = best; |
238 | 5.49M | } |
239 | 34.7k | return true; |
240 | 34.7k | } Unexecuted instantiation: jxl::N_SSE4::FastClusterHistograms(std::__1::vector<jxl::Histogram, std::__1::allocator<jxl::Histogram> > const&, unsigned long, bool, std::__1::vector<jxl::Histogram, std::__1::allocator<jxl::Histogram> >*, std::__1::vector<unsigned int, std::__1::allocator<unsigned int> >*) jxl::N_AVX2::FastClusterHistograms(std::__1::vector<jxl::Histogram, std::__1::allocator<jxl::Histogram> > const&, unsigned long, bool, std::__1::vector<jxl::Histogram, std::__1::allocator<jxl::Histogram> >*, std::__1::vector<unsigned int, std::__1::allocator<unsigned int> >*) Line | Count | Source | 168 | 34.7k | std::vector<uint32_t>* histogram_symbols) { | 169 | 34.7k | const size_t prev_histograms = out->size(); | 170 | 34.7k | out->reserve(max_histograms); | 171 | 34.7k | histogram_symbols->clear(); | 172 | 34.7k | histogram_symbols->resize(in.size(), max_histograms); | 173 | | | 174 | 34.7k | std::vector<float> dists(in.size(), std::numeric_limits<float>::max()); | 175 | 34.7k | size_t largest_idx = 0; | 176 | 33.0M | for (size_t i = 0; i < in.size(); i++) { | 177 | 33.0M | if (in[i].total_count == 0) { | 178 | 27.2M | (*histogram_symbols)[i] = 0; | 179 | 27.2M | dists[i] = 0.0f; | 180 | 27.2M | continue; | 181 | 27.2M | } | 182 | 5.74M | HistogramEntropy(in[i]); | 183 | 5.74M | if (in[i].total_count > in[largest_idx].total_count) { | 184 | 65.9k | largest_idx = i; | 185 | 65.9k | } | 186 | 5.74M | } | 187 | | | 188 | 34.7k | if (prev_histograms > 0) { | 189 | 176 | for (size_t j = 0; j < prev_histograms; ++j) { | 190 | 88 | HistogramEntropy((*out)[j]); | 191 | 88 | } | 192 | 183k | for (size_t i = 0; i < in.size(); i++) { | 193 | 183k | if (dists[i] == 0.0f) continue; | 194 | 36.5k | for (size_t j = 0; j < prev_histograms; ++j) { | 195 | 18.2k | dists[i] = std::min(HistogramKLDivergence(in[i], (*out)[j]), dists[i]); | 196 | 18.2k | } | 197 | 18.2k | } | 198 | 88 | auto max_dist = std::max_element(dists.begin(), dists.end()); | 199 | 88 | if (*max_dist > 0.0f) { | 200 | 88 | largest_idx = max_dist - dists.begin(); | 201 | 88 | } | 202 | 88 | } | 203 | | | 204 | 34.7k | constexpr float kMinDistanceForDistinct = 48.0f; | 205 | 246k | while (out->size() < max_histograms) { | 206 | 246k | (*histogram_symbols)[largest_idx] = out->size(); | 207 | 246k | out->push_back(in[largest_idx]); | 208 | 246k | dists[largest_idx] = 0.0f; | 209 | 246k | largest_idx = 0; | 210 | 617M | for (size_t i = 0; i < in.size(); i++) { | 211 | 616M | if (dists[i] == 0.0f) continue; | 212 | 162M | dists[i] = | 213 | 162M | std::min(ClusterDistance(in[i], out->back(), prefix_codes), dists[i]); | 214 | 162M | if (dists[i] > dists[largest_idx]) largest_idx = i; | 215 | 162M | } | 216 | 246k | if (dists[largest_idx] < kMinDistanceForDistinct) break; | 217 | 246k | } | 218 | | | 219 | 33.0M | for (size_t i = 0; i < in.size(); i++) { | 220 | 33.0M | if ((*histogram_symbols)[i] != max_histograms) continue; | 221 | 5.49M | size_t best = 0; | 222 | 5.49M | float best_dist = std::numeric_limits<float>::max(); | 223 | 164M | for (size_t j = 0; j < out->size(); j++) { | 224 | 158M | float dist = j < prev_histograms | 225 | 158M | ? HistogramKLDivergence(in[i], (*out)[j]) | 226 | 158M | : ClusterDistance(in[i], (*out)[j], prefix_codes); | 227 | 158M | if (dist < best_dist) { | 228 | 15.6M | best = j; | 229 | 15.6M | best_dist = dist; | 230 | 15.6M | } | 231 | 158M | } | 232 | 5.49M | JXL_ENSURE(best_dist < std::numeric_limits<float>::max()); | 233 | 5.49M | if (best >= prev_histograms) { | 234 | 5.49M | (*out)[best].AddHistogram(in[i]); | 235 | 5.49M | HistogramEntropy((*out)[best]); | 236 | 5.49M | } | 237 | 5.49M | (*histogram_symbols)[i] = best; | 238 | 5.49M | } | 239 | 34.7k | return true; | 240 | 34.7k | } |
Unexecuted instantiation: jxl::N_AVX3::FastClusterHistograms(std::__1::vector<jxl::Histogram, std::__1::allocator<jxl::Histogram> > const&, unsigned long, bool, std::__1::vector<jxl::Histogram, std::__1::allocator<jxl::Histogram> >*, std::__1::vector<unsigned int, std::__1::allocator<unsigned int> >*) Unexecuted instantiation: jxl::N_AVX3_ZEN4::FastClusterHistograms(std::__1::vector<jxl::Histogram, std::__1::allocator<jxl::Histogram> > const&, unsigned long, bool, std::__1::vector<jxl::Histogram, std::__1::allocator<jxl::Histogram> >*, std::__1::vector<unsigned int, std::__1::allocator<unsigned int> >*) Unexecuted instantiation: jxl::N_AVX3_SPR::FastClusterHistograms(std::__1::vector<jxl::Histogram, std::__1::allocator<jxl::Histogram> > const&, unsigned long, bool, std::__1::vector<jxl::Histogram, std::__1::allocator<jxl::Histogram> >*, std::__1::vector<unsigned int, std::__1::allocator<unsigned int> >*) Unexecuted instantiation: jxl::N_SSE2::FastClusterHistograms(std::__1::vector<jxl::Histogram, std::__1::allocator<jxl::Histogram> > const&, unsigned long, bool, std::__1::vector<jxl::Histogram, std::__1::allocator<jxl::Histogram> >*, std::__1::vector<unsigned int, std::__1::allocator<unsigned int> >*) |
241 | | |
242 | | // NOLINTNEXTLINE(google-readability-namespace-comments) |
243 | | } // namespace HWY_NAMESPACE |
244 | | } // namespace jxl |
245 | | HWY_AFTER_NAMESPACE(); |
246 | | |
247 | | #if HWY_ONCE |
248 | | namespace jxl { |
249 | | HWY_EXPORT(FastClusterHistograms); // Local function |
250 | | HWY_EXPORT(HistogramEntropy); // Local function |
251 | | HWY_EXPORT(HistogramCondition); // Local function |
252 | | |
253 | 1.01M | void Histogram::Condition() { HWY_DYNAMIC_DISPATCH(HistogramCondition)(*this); } |
254 | | |
255 | 29.7M | float Histogram::ShannonEntropy() const { |
256 | 29.7M | HWY_DYNAMIC_DISPATCH(HistogramEntropy)(*this); |
257 | 29.7M | return entropy; |
258 | 29.7M | } |
259 | | |
260 | | namespace { |
261 | | // ----------------------------------------------------------------------------- |
262 | | // Histogram refinement |
263 | | |
264 | | // Reorder histograms in *out so that the new symbols in *symbols come in |
265 | | // increasing order. |
266 | | void HistogramReindex(std::vector<Histogram>* out, size_t prev_histograms, |
267 | 34.7k | std::vector<uint32_t>* symbols) { |
268 | 34.7k | std::vector<Histogram> tmp(*out); |
269 | 34.7k | std::map<int, int> new_index; |
270 | 34.8k | for (size_t i = 0; i < prev_histograms; ++i) { |
271 | 88 | new_index[i] = i; |
272 | 88 | } |
273 | 34.7k | int next_index = prev_histograms; |
274 | 33.0M | for (uint32_t symbol : *symbols) { |
275 | 33.0M | if (new_index.find(symbol) == new_index.end()) { |
276 | 244k | new_index[symbol] = next_index; |
277 | 244k | (*out)[next_index] = tmp[symbol]; |
278 | 244k | ++next_index; |
279 | 244k | } |
280 | 33.0M | } |
281 | 34.7k | out->resize(next_index); |
282 | 33.0M | for (uint32_t& symbol : *symbols) { |
283 | 33.0M | symbol = new_index[symbol]; |
284 | 33.0M | } |
285 | 34.7k | } |
286 | | |
287 | | } // namespace |
288 | | |
289 | | // Clusters similar histograms in 'in' together, the selected histograms are |
290 | | // placed in 'out', and for each index in 'in', *histogram_symbols will |
291 | | // indicate which of the 'out' histograms is the best approximation. |
292 | | Status ClusterHistograms(const HistogramParams& params, |
293 | | const std::vector<Histogram>& in, |
294 | | size_t max_histograms, std::vector<Histogram>* out, |
295 | 34.7k | std::vector<uint32_t>* histogram_symbols) { |
296 | 34.7k | size_t prev_histograms = out->size(); |
297 | 34.7k | max_histograms = std::min(max_histograms, params.max_histograms); |
298 | 34.7k | max_histograms = std::min(max_histograms, in.size()); |
299 | 34.7k | if (params.clustering == HistogramParams::ClusteringType::kFastest) { |
300 | 0 | max_histograms = std::min(max_histograms, static_cast<size_t>(4)); |
301 | 0 | } |
302 | | |
303 | 34.7k | JXL_RETURN_IF_ERROR(HWY_DYNAMIC_DISPATCH(FastClusterHistograms)( |
304 | 34.7k | in, prev_histograms + max_histograms, params.force_huffman, out, |
305 | 34.7k | histogram_symbols)); |
306 | | |
307 | 34.7k | if (prev_histograms == 0 && |
308 | 34.6k | params.clustering == HistogramParams::ClusteringType::kBest) { |
309 | 39.4k | for (auto& histo : *out) { |
310 | 39.4k | JXL_ASSIGN_OR_RETURN(histo.entropy, histo.ANSPopulationCost()); |
311 | 39.4k | } |
312 | 13.8k | uint32_t next_version = 2; |
313 | 13.8k | std::vector<uint32_t> version(out->size(), 1); |
314 | 13.8k | std::vector<uint32_t> renumbering(out->size()); |
315 | 13.8k | std::iota(renumbering.begin(), renumbering.end(), 0); |
316 | | |
317 | | // Try to pair up clusters if doing so reduces the total cost. |
318 | | |
319 | 13.8k | struct HistogramPair { |
320 | | // validity of a pair: p.version == max(version[i], version[j]) |
321 | 13.8k | float cost; |
322 | 13.8k | uint32_t first; |
323 | 13.8k | uint32_t second; |
324 | 13.8k | uint32_t version; |
325 | | // We use > because priority queues sort in *decreasing* order, but we |
326 | | // want lower cost elements to appear first. |
327 | 13.8k | bool operator<(const HistogramPair& other) const { |
328 | 7.93k | return std::make_tuple(cost, first, second, version) > |
329 | 7.93k | std::make_tuple(other.cost, other.first, other.second, |
330 | 7.93k | other.version); |
331 | 7.93k | } |
332 | 13.8k | }; |
333 | | |
334 | | // Create list of all pairs by increasing merging cost. |
335 | 13.8k | std::priority_queue<HistogramPair> pairs_to_merge; |
336 | 53.3k | for (uint32_t i = 0; i < out->size(); i++) { |
337 | 102k | for (uint32_t j = i + 1; j < out->size(); j++) { |
338 | 63.3k | Histogram histo; |
339 | 63.3k | histo.AddHistogram((*out)[i]); |
340 | 63.3k | histo.AddHistogram((*out)[j]); |
341 | 63.3k | JXL_ASSIGN_OR_RETURN(float cost, histo.ANSPopulationCost()); |
342 | 63.3k | cost -= (*out)[i].entropy + (*out)[j].entropy; |
343 | | // Avoid enqueueing pairs that are not advantageous to merge. |
344 | 63.3k | if (cost >= 0) continue; |
345 | 3.87k | pairs_to_merge.push( |
346 | 3.87k | HistogramPair{cost, i, j, std::max(version[i], version[j])}); |
347 | 3.87k | } |
348 | 39.4k | } |
349 | | |
350 | | // Merge the best pair to merge, add new pairs that get formed as a |
351 | | // consequence. |
352 | 18.2k | while (!pairs_to_merge.empty()) { |
353 | 4.40k | uint32_t first = pairs_to_merge.top().first; |
354 | 4.40k | uint32_t second = pairs_to_merge.top().second; |
355 | 4.40k | uint32_t ver = pairs_to_merge.top().version; |
356 | 4.40k | pairs_to_merge.pop(); |
357 | 4.40k | if (ver != std::max(version[first], version[second]) || |
358 | 3.17k | version[first] == 0 || version[second] == 0) { |
359 | 2.19k | continue; |
360 | 2.19k | } |
361 | 2.21k | (*out)[first].AddHistogram((*out)[second]); |
362 | 2.21k | JXL_ASSIGN_OR_RETURN((*out)[first].entropy, |
363 | 2.21k | (*out)[first].ANSPopulationCost()); |
364 | 11.2k | for (uint32_t& item : renumbering) { |
365 | 11.2k | if (item == second) { |
366 | 2.30k | item = first; |
367 | 2.30k | } |
368 | 11.2k | } |
369 | 2.21k | version[second] = 0; |
370 | 2.21k | version[first] = next_version++; |
371 | 13.4k | for (uint32_t j = 0; j < out->size(); j++) { |
372 | 11.2k | if (j == first) continue; |
373 | 9.04k | if (version[j] == 0) continue; |
374 | 6.03k | Histogram histo; |
375 | 6.03k | histo.AddHistogram((*out)[first]); |
376 | 6.03k | histo.AddHistogram((*out)[j]); |
377 | 6.03k | JXL_ASSIGN_OR_RETURN(float merge_cost, histo.ANSPopulationCost()); |
378 | 6.03k | merge_cost -= (*out)[first].entropy + (*out)[j].entropy; |
379 | | // Avoid enqueueing pairs that are not advantageous to merge. |
380 | 6.03k | if (merge_cost >= 0) continue; |
381 | 532 | pairs_to_merge.push( |
382 | 532 | HistogramPair{merge_cost, std::min(first, j), std::max(first, j), |
383 | 532 | std::max(version[first], version[j])}); |
384 | 532 | } |
385 | 2.21k | } |
386 | 13.8k | std::vector<uint32_t> reverse_renumbering(out->size(), -1); |
387 | 13.8k | size_t num_alive = 0; |
388 | 53.3k | for (size_t i = 0; i < out->size(); i++) { |
389 | 39.4k | if (version[i] == 0) continue; |
390 | 37.2k | (*out)[num_alive++] = (*out)[i]; |
391 | 37.2k | reverse_renumbering[i] = num_alive - 1; |
392 | 37.2k | } |
393 | 13.8k | out->resize(num_alive); |
394 | 76.2k | for (uint32_t& item : *histogram_symbols) { |
395 | 76.2k | item = reverse_renumbering[renumbering[item]]; |
396 | 76.2k | } |
397 | 13.8k | } |
398 | | |
399 | | // Convert the context map to a canonical form. |
400 | 34.7k | HistogramReindex(out, prev_histograms, histogram_symbols); |
401 | 34.7k | return true; |
402 | 34.7k | } |
403 | | |
404 | | } // namespace jxl |
405 | | #endif // HWY_ONCE |