Skip to content

Commit aa565b9

Browse files
author
Johannes Ballé
committed
Adds bazel BUILD files for dynamically linked library.
Also makes a few adjustments to C code for GCC.
1 parent bb5ca4d commit aa565b9

File tree

8 files changed

+183
-17
lines changed

8 files changed

+183
-17
lines changed

BUILD

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
licenses(["notice"]) # Apache 2.0
2+
3+
package(default_visibility = ["//visibility:public"])
4+
5+
cc_binary(
6+
name = "_range_coding_ops.so",
7+
srcs = [
8+
"cc/kernels/range_coder.cc",
9+
"cc/kernels/range_coder.h",
10+
"cc/kernels/range_coding_helper_kernels.cc",
11+
"cc/kernels/range_coding_kernels_util.cc",
12+
"cc/kernels/range_coding_kernels_util.h",
13+
"cc/kernels/range_coding_kernels.cc",
14+
"cc/ops/range_coding_ops.cc",
15+
],
16+
linkshared = 1,
17+
deps = [
18+
"@local_config_tf//:libtensorflow_framework",
19+
"@local_config_tf//:tf_header_lib",
20+
],
21+
copts = [
22+
"-pthread", "-std=c++11", "-D_GLIBCXX_USE_CXX11_ABI=0",
23+
"-Wno-sign-compare",
24+
],
25+
)
26+
27+
py_library(
28+
name = "tensorflow_compression",
29+
srcs = [
30+
"__init__.py",
31+
"python/__init__.py",
32+
"python/layers/__init__.py",
33+
"python/layers/entropy_models.py",
34+
"python/layers/gdn.py",
35+
"python/layers/initializers.py",
36+
"python/layers/parameterizers.py",
37+
"python/layers/signal_conv.py",
38+
"python/ops/__init__.py",
39+
"python/ops/math_ops.py",
40+
"python/ops/padding_ops.py",
41+
"python/ops/range_coding_ops.py",
42+
"python/ops/spectral_ops.py",
43+
],
44+
data = [
45+
":_range_coding_ops.so"
46+
],
47+
srcs_version = "PY2AND3",
48+
)
49+
50+
py_test(
51+
name = "entropy_models_test",
52+
timeout = "long",
53+
srcs = ["python/layers/entropy_models_test.py"],
54+
deps = [":tensorflow_compression"],
55+
srcs_version = "PY2AND3",
56+
)
57+
58+
py_test(
59+
name = "gdn_test",
60+
srcs = ["python/layers/gdn_test.py"],
61+
deps = [":tensorflow_compression"],
62+
srcs_version = "PY2AND3",
63+
)
64+
65+
py_test(
66+
name = "parameterizers_test",
67+
srcs = ["python/layers/parameterizers_test.py"],
68+
deps = [":tensorflow_compression"],
69+
srcs_version = "PY2AND3",
70+
)
71+
72+
py_test(
73+
name = "signal_conv_test",
74+
timeout = "long",
75+
srcs = ["python/layers/signal_conv_test.py"],
76+
deps = [":tensorflow_compression"],
77+
srcs_version = "PY2AND3",
78+
)
79+
80+
py_test(
81+
name = "math_ops_test",
82+
srcs = ["python/ops/math_ops_test.py"],
83+
deps = [":tensorflow_compression"],
84+
srcs_version = "PY2AND3",
85+
)
86+
87+
py_test(
88+
name = "padding_ops_test",
89+
srcs = ["python/ops/padding_ops_test.py"],
90+
deps = [":tensorflow_compression"],
91+
srcs_version = "PY2AND3",
92+
)
93+
94+
py_test(
95+
name = "spectral_ops_test",
96+
srcs = ["python/ops/spectral_ops_test.py"],
97+
deps = [":tensorflow_compression"],
98+
srcs_version = "PY2AND3",
99+
)
100+
101+
py_test(
102+
name = "range_coding_ops_test",
103+
srcs = ["python/ops/range_coding_ops_test.py"],
104+
deps = [":tensorflow_compression"],
105+
srcs_version = "PY2AND3",
106+
)

cc/kernels/range_coder.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ limitations under the License.
3030

3131
namespace tensorflow_compression {
3232
namespace gtl = tensorflow::gtl;
33+
using tensorflow::int32;
34+
using tensorflow::string;
35+
using tensorflow::uint32;
36+
using tensorflow::uint64;
37+
using tensorflow::uint8;
3338

3439
RangeEncoder::RangeEncoder(int precision) : precision_(precision) {
3540
CHECK_GT(precision, 0);

cc/kernels/range_coder.h

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,19 @@ class RangeEncoder {
4747
// ...
4848
//
4949
// REQUIRES: 0 <= lower < upper <= 2^precision.
50-
void Encode(int32 lower, int32 upper, string* sink);
50+
void Encode(tensorflow::int32 lower, tensorflow::int32 upper,
51+
tensorflow::string* sink);
5152

5253
// The encode may contain some under-determined values from previous encoding.
5354
// After Encode() calls, Finalize() must be called. Otherwise the encoded
5455
// string may not be decoded.
55-
void Finalize(string* sink);
56+
void Finalize(tensorflow::string* sink);
5657

5758
private:
58-
uint32 base_ = 0;
59-
uint32 size_minus1_ = std::numeric_limits<uint32>::max();
60-
uint64 delay_ = 0;
59+
tensorflow::uint32 base_ = 0;
60+
tensorflow::uint32 size_minus1_ =
61+
std::numeric_limits<tensorflow::uint32>::max();
62+
tensorflow::uint64 delay_ = 0;
6163

6264
const int precision_;
6365
};
@@ -69,7 +71,7 @@ class RangeDecoder {
6971
//
7072
// REQUIRES: `precision` must be the same as the encoder's precision.
7173
// REQUIRES: 0 < precision <= 16.
72-
RangeDecoder(const string& source, int precision);
74+
RangeDecoder(const tensorflow::string& source, int precision);
7375

7476
// Decodes a character from `source` using CDF. The size of `cdf` should be
7577
// one more than the number of the character in the alphabet.
@@ -90,18 +92,19 @@ class RangeDecoder {
9092
// REQUIRES: cdf[cdf.size() - 1] <= 2^precision.
9193
//
9294
// In practice the last element of `cdf` should equal to 2^precision.
93-
int32 Decode(tensorflow::gtl::ArraySlice<int32> cdf);
95+
tensorflow::int32 Decode(tensorflow::gtl::ArraySlice<tensorflow::int32> cdf);
9496

9597
private:
9698
void Read16BitValue();
9799

98-
uint32 base_ = 0;
99-
uint32 size_minus1_ = std::numeric_limits<uint32>::max();
100-
uint32 value_ = 0;
100+
tensorflow::uint32 base_ = 0;
101+
tensorflow::uint32 size_minus1_ =
102+
std::numeric_limits<tensorflow::uint32>::max();
103+
tensorflow::uint32 value_ = 0;
101104

102-
string::const_iterator current_;
103-
const string::const_iterator begin_;
104-
const string::const_iterator end_;
105+
tensorflow::string::const_iterator current_;
106+
const tensorflow::string::const_iterator begin_;
107+
const tensorflow::string::const_iterator end_;
105108

106109
const int precision_;
107110
};

cc/kernels/range_coding_helper_kernels.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ using tensorflow::Tensor;
4444
using tensorflow::TensorShape;
4545
using tensorflow::TensorShapeUtils;
4646
using tensorflow::errors::InvalidArgument;
47+
using tensorflow::int32;
48+
using tensorflow::int64;
49+
using tensorflow::string;
50+
using tensorflow::uint8;
51+
using tensorflow::uint32;
52+
using tensorflow::uint64;
4753

4854
class PmfToCdfOp : public OpKernel {
4955
public:

cc/kernels/range_coding_kernels.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ using tensorflow::Tensor;
4848
using tensorflow::TensorShape;
4949
using tensorflow::TensorShapeUtils;
5050
using tensorflow::TTypes;
51+
using tensorflow::int16;
52+
using tensorflow::int32;
53+
using tensorflow::int64;
54+
using tensorflow::string;
55+
using tensorflow::uint8;
56+
using tensorflow::uint32;
57+
using tensorflow::uint64;
5158

5259
// A helper class to iterate over data and cdf simultaneously, while cdf is
5360
// broadcasted to data.

cc/kernels/range_coding_kernels_util.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ namespace tensorflow_compression {
2626
using tensorflow::Status;
2727
using tensorflow::TensorShape;
2828
using tensorflow::errors::InvalidArgument;
29+
using tensorflow::int32;
30+
using tensorflow::int64;
31+
using tensorflow::string;
32+
using tensorflow::uint8;
33+
using tensorflow::uint32;
34+
using tensorflow::uint64;
2935

3036
Status MergeAxes(const TensorShape& broadcast_shape,
3137
const TensorShape& storage_shape,

cc/kernels/range_coding_kernels_util.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ limitations under the License.
2525
namespace tensorflow_compression {
2626

2727
// The shapes are simplified to reduce indexing cost.
28-
tensorflow::Status MergeAxes(const tensorflow::TensorShape& broadcast_shape,
29-
const tensorflow::TensorShape& storage_shape,
30-
std::vector<int64>* merged_broadcast_shape_pointer,
31-
std::vector<int64>* merged_storage_shape_pointer);
28+
tensorflow::Status MergeAxes(
29+
const tensorflow::TensorShape& broadcast_shape,
30+
const tensorflow::TensorShape& storage_shape,
31+
std::vector<tensorflow::int64>* merged_broadcast_shape_pointer,
32+
std::vector<tensorflow::int64>* merged_storage_shape_pointer);
3233

3334
} // namespace tensorflow_compression
3435

python/ops/range_coding_ops.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# -*- coding: utf-8 -*-
2+
# Copyright 2018 Google LLC. All Rights Reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
# ==============================================================================
16+
"""Range coding operations."""
17+
18+
from __future__ import absolute_import
19+
from __future__ import division
20+
from __future__ import print_function
21+
22+
# Dependency imports
23+
24+
from tensorflow.python.framework import load_library
25+
from tensorflow.python.platform import resource_loader
26+
27+
_range_coding_ops = load_library.load_op_library(
28+
resource_loader.get_path_to_datafile("../../_range_coding_ops.so"))
29+
30+
pmf_to_quantized_cdf = _range_coding_ops.pmf_to_quantized_cdf
31+
range_decode = _range_coding_ops.range_decode
32+
range_encode = _range_coding_ops.range_encode

0 commit comments

Comments
 (0)