|
2 | 2 |
|
3 | 3 | #include <gtest/gtest.h> |
4 | 4 | #include <random> |
| 5 | +#include "custom_parsers/dependency_registration.h" |
| 6 | +#include "openzl/cpp/CCtx.hpp" |
| 7 | +#include "openzl/cpp/codecs/ACE.hpp" |
| 8 | +#include "tools/training/ace/ace.h" |
5 | 9 | #include "tools/training/ace/ace_combination.h" |
6 | 10 |
|
7 | 11 | namespace openzl { |
@@ -142,6 +146,103 @@ TEST_F(ACECombinationTest, ProducesParetoOptimalCombination) |
142 | 146 | EXPECT_TRUE(isPareto(frontier)); |
143 | 147 | } |
144 | 148 |
|
| 149 | +TEST_F(ACECombinationTest, NoSaveAceStateProducesSmallerCompressor) |
| 150 | +{ |
| 151 | + // Create sample data: triple delta pattern compresses well with ACE |
| 152 | + std::vector<uint64_t> data(1000, 1); |
| 153 | + for (size_t i = 1; i < data.size(); ++i) { |
| 154 | + data[i] += data[i - 1]; |
| 155 | + } |
| 156 | + for (size_t i = 1; i < data.size(); ++i) { |
| 157 | + data[i] += data[i - 1]; |
| 158 | + } |
| 159 | + for (size_t i = 1; i < data.size(); ++i) { |
| 160 | + data[i] += data[i - 1]; |
| 161 | + } |
| 162 | + auto input = Input::refSerial(data.data(), data.size() * sizeof(data[0])); |
| 163 | + std::vector<Input> inputsVec; |
| 164 | + inputsVec.push_back(std::move(input)); |
| 165 | + std::vector<training::MultiInput> multiInputs; |
| 166 | + multiInputs.emplace_back(std::move(inputsVec)); |
| 167 | + |
| 168 | + auto compressorGenFunc = [](poly::string_view serialized) { |
| 169 | + auto compressor = std::make_unique<Compressor>(); |
| 170 | + compressor->deserialize(serialized); |
| 171 | + return compressor; |
| 172 | + }; |
| 173 | + |
| 174 | + // Train with saveAceState = true |
| 175 | + std::shared_ptr<const std::string_view> resultWithAceState; |
| 176 | + { |
| 177 | + Compressor compressor; |
| 178 | + compressor.selectStartingGraph(graphs::ACE()(compressor)); |
| 179 | + compressor.setParameter(CParam::FormatVersion, ZL_MAX_FORMAT_VERSION); |
| 180 | + training::TrainParams trainParams = { |
| 181 | + .compressorGenFunc = compressorGenFunc, |
| 182 | + .threads = 1, |
| 183 | + .saveAceState = true, |
| 184 | + }; |
| 185 | + ACETrainer trainer; |
| 186 | + auto results = |
| 187 | + trainer.train(multiInputs, compressor.serialize(), trainParams); |
| 188 | + ASSERT_FALSE(results.empty()); |
| 189 | + resultWithAceState = results[0]; |
| 190 | + } |
| 191 | + |
| 192 | + // Train with saveAceState = false (default) |
| 193 | + std::shared_ptr<const std::string_view> resultWithoutAceState; |
| 194 | + { |
| 195 | + Compressor compressor; |
| 196 | + compressor.selectStartingGraph(graphs::ACE()(compressor)); |
| 197 | + compressor.setParameter(CParam::FormatVersion, ZL_MAX_FORMAT_VERSION); |
| 198 | + training::TrainParams trainParams = { |
| 199 | + .compressorGenFunc = compressorGenFunc, |
| 200 | + .threads = 1, |
| 201 | + .saveAceState = false, |
| 202 | + }; |
| 203 | + ACETrainer trainer; |
| 204 | + auto results = |
| 205 | + trainer.train(multiInputs, compressor.serialize(), trainParams); |
| 206 | + ASSERT_FALSE(results.empty()); |
| 207 | + resultWithoutAceState = results[0]; |
| 208 | + } |
| 209 | + |
| 210 | + auto sizeWithAceState = resultWithAceState->size(); |
| 211 | + auto sizeWithoutAceState = resultWithoutAceState->size(); |
| 212 | + |
| 213 | + // Serialized compressor without ACE state should be significantly smaller |
| 214 | + EXPECT_GT(sizeWithAceState, 0); |
| 215 | + EXPECT_GT(sizeWithoutAceState, 0); |
| 216 | + EXPECT_LE(sizeWithoutAceState, sizeWithAceState / 2) |
| 217 | + << "Serialized compressor without ACE state (" |
| 218 | + << sizeWithoutAceState |
| 219 | + << " bytes) should be at most half the size of one with ACE state (" |
| 220 | + << sizeWithAceState << " bytes)"; |
| 221 | + |
| 222 | + // Compress data with both trained compressors and verify identical output |
| 223 | + auto compressWithResult = |
| 224 | + [&](const std::string_view& serializedCompressor) { |
| 225 | + auto comp = compressorGenFunc(serializedCompressor); |
| 226 | + CCtx cctx; |
| 227 | + cctx.setParameter(CParam::FormatVersion, ZL_MAX_FORMAT_VERSION); |
| 228 | + cctx.refCompressor(*comp); |
| 229 | + auto inputForCompress = Input::refSerial( |
| 230 | + data.data(), data.size() * sizeof(data[0])); |
| 231 | + return cctx.compressOne(inputForCompress); |
| 232 | + }; |
| 233 | + auto compressedWith = compressWithResult(*resultWithAceState); |
| 234 | + auto compressedWithout = compressWithResult(*resultWithoutAceState); |
| 235 | + |
| 236 | + // Compressed output should be nearly identical — the small difference |
| 237 | + // is due to ACE training being non-deterministic across independent runs. |
| 238 | + // Allow up to 10% tolerance. |
| 239 | + auto maxSize = std::max(compressedWith.size(), compressedWithout.size()); |
| 240 | + auto minSize = std::min(compressedWith.size(), compressedWithout.size()); |
| 241 | + EXPECT_LE(maxSize - minSize, maxSize / 10) |
| 242 | + << "Compressed data sizes should be within 10%: " |
| 243 | + << compressedWith.size() << " vs " << compressedWithout.size(); |
| 244 | +} |
| 245 | + |
145 | 246 | } // namespace tests |
146 | 247 | } // namespace training |
147 | 248 | } // namespace openzl |
0 commit comments