1/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7    http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15
16#include "tensorflow/contrib/coder/kernels/range_coder.h"
17
18#include <cmath>
19
20#include "tensorflow/core/lib/random/distribution_sampler.h"
21#include "tensorflow/core/lib/random/philox_random.h"
22#include "tensorflow/core/lib/random/random.h"
23#include "tensorflow/core/lib/random/simple_philox.h"
24#include "tensorflow/core/platform/test.h"
25
26namespace tensorflow {
27namespace {
28void RangeEncodeDecodeTest(int precision, random::SimplePhilox* gen) {
29  constexpr int kAlphabetSize = 256;
30
31  std::vector<float> distribution_weight;
32  distribution_weight.reserve(kAlphabetSize);
33  for (int i = 1; i <= kAlphabetSize; ++i) {
34    distribution_weight.push_back(std::pow(static_cast<float>(i), -2.0f));
35  }
36
37  random::DistributionSampler sampler(distribution_weight);
38
39  const int multiplier = (precision > 7) ? 32 : 1;
40  std::vector<int32> histogram(kAlphabetSize, multiplier - 1);
41
42  const int data_size =
43      (multiplier << precision) - histogram.size() * (multiplier - 1);
44  CHECK_GE(data_size, 0);
45  std::vector<uint8> data(data_size);
46  for (uint8& x : data) {
47    x = sampler.Sample(gen);
48    ++histogram[x];
49  }
50
51  std::vector<int32> cdf(histogram.size() + 1, 0);
52  int partial_sum = 0;
53  for (int i = 0; i < histogram.size(); ++i) {
54    partial_sum += histogram[i];
55    cdf[i + 1] = partial_sum / multiplier;
56  }
57
58  ASSERT_EQ(cdf.front(), 0);
59  ASSERT_EQ(cdf.back(), 1 << precision);
60
61  std::vector<double> ideal_code_length(histogram.size());
62  const double normalizer = static_cast<double>(1 << precision);
63  for (int i = 0; i < ideal_code_length.size(); ++i) {
64    ideal_code_length[i] = -std::log2((cdf[i + 1] - cdf[i]) / normalizer);
65  }
66
67  RangeEncoder encoder(precision);
68  string encoded;
69  double ideal_length = 0.0;
70  for (uint8 x : data) {
71    encoder.Encode(cdf[x], cdf[x + 1], &encoded);
72    ideal_length += ideal_code_length[x];
73  }
74  encoder.Finalize(&encoded);
75
76  LOG(INFO) << "Encoded string length (bits): " << 8 * encoded.size()
77            << ", whereas ideal " << ideal_length << " ("
78            << (8 * encoded.size()) / ideal_length << " of ideal) "
79            << " (ideal compression rate " << ideal_length / (8 * data.size())
80            << ")";
81
82  RangeDecoder decoder(encoded, precision);
83  for (int i = 0; i < data.size(); ++i) {
84    const int32 decoded = decoder.Decode(cdf);
85    ASSERT_EQ(decoded, static_cast<int32>(data[i])) << i;
86  }
87}
88
89TEST(RangeCoderTest, Precision1To11) {
90  random::PhiloxRandom gen(random::New64(), random::New64());
91  random::SimplePhilox rand(&gen);
92  const int precision = 1 + rand.Uniform(11);
93  RangeEncodeDecodeTest(precision, &rand);
94}
95
96TEST(RangeCoderTest, Precision12To16) {
97  random::PhiloxRandom gen(random::New64(), random::New64());
98  random::SimplePhilox rand(&gen);
99  for (int precision = 12; precision < 17; ++precision) {
100    RangeEncodeDecodeTest(precision, &rand);
101  }
102}
103
104TEST(RangeCoderTest, FinalizeState0) {
105  constexpr int kPrecision = 2;
106
107  string output;
108  RangeEncoder encoder(kPrecision);
109  encoder.Encode(0, 2, &output);
110  encoder.Finalize(&output);
111
112  RangeDecoder decoder(output, kPrecision);
113  EXPECT_EQ(decoder.Decode({0, 2, 4}), 0);
114}
115}  // namespace
116}  // namespace tensorflow
117