Skip to content

Commit 6ca6dab

Browse files
Johannes Ballécopybara-github
authored andcommitted
Fixes C++ platform incompatibilities.
PiperOrigin-RevId: 423905712 Change-Id: Ifa43c14799dfcd47434bef936a06c107f4d0fce3
1 parent ebd1eff commit 6ca6dab

File tree

6 files changed

+40
-321
lines changed

6 files changed

+40
-321
lines changed

tensorflow_compression/cc/BUILD

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ cc_binary(
77
"**/*.h",
88
"**/*.cc",
99
],
10-
exclude = ["**/*_test.cc"],
10+
exclude = [
11+
"**/*_test.cc",
12+
],
1113
),
1214
copts = [
1315
"-pthread",
14-
"-std=c++11",
16+
"-std=c++14",
1517
"-D_GLIBCXX_USE_CXX11_ABI=0",
1618
],
1719
linkshared = 1,

tensorflow_compression/cc/kernels/range_coder_kernels.cc

Lines changed: 30 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ limitations under the License.
1919
#include <string>
2020
#include <utility>
2121

22-
#include "absl/base/integral_types.h"
23-
#include "tensorflow_compression/cc/lib/range_coder.h"
2422
#include "absl/strings/string_view.h"
2523
#include "absl/strings/substitute.h"
2624
#include "absl/types/span.h"
@@ -36,6 +34,7 @@ limitations under the License.
3634
#include "tensorflow/core/framework/variant_tensor_data.h"
3735
#include "tensorflow/core/lib/core/errors.h"
3836
#include "tensorflow/core/lib/core/threadpool.h"
37+
#include "tensorflow_compression/cc/lib/range_coder.h"
3938

4039
namespace tensorflow_compression {
4140
namespace {
@@ -367,11 +366,12 @@ class EntropyEncodeChannelOp : public tensorflow::OpKernel {
367366
tensorflow::thread::ThreadPool* workers =
368367
context->device()->tensorflow_cpu_worker_threads()->workers;
369368
tensorflow::mutex mu;
370-
workers->ParallelFor(
371-
handle.size(), cost_per_unit,
372-
[&handle, &mu, context, value, index_stride](int64 start, int64 limit) {
373-
PerShard(handle, value, index_stride, context, &mu, start, limit);
374-
});
369+
workers->ParallelFor(handle.size(), cost_per_unit,
370+
[&handle, &mu, context, value, index_stride](
371+
int64_t start, int64_t limit) {
372+
PerShard(handle, value, index_stride, context, &mu,
373+
start, limit);
374+
});
375375

376376
context->set_output(0, handle_tensor);
377377
}
@@ -382,19 +382,17 @@ class EntropyEncodeChannelOp : public tensorflow::OpKernel {
382382
const int64_t index_stride,
383383
tensorflow::OpKernelContext* context,
384384
tensorflow::mutex* mu, int64_t start, int64_t limit) {
385-
#define REQUIRES_OK(status) \
386-
if (auto s = (status); ABSL_PREDICT_FALSE(!s.ok())) { \
387-
tensorflow::mutex_lock lock(*mu); \
388-
context->SetStatus(s); \
389-
return; \
390-
}
391-
392385
#define REQUIRES(cond, status) \
393386
if (!ABSL_PREDICT_TRUE(cond)) { \
394387
tensorflow::mutex_lock lock(*mu); \
395388
context->SetStatus(status); \
396389
return; \
397390
}
391+
#define REQUIRES_OK(status) \
392+
{ \
393+
auto s = (status); \
394+
REQUIRES(s.ok(), s); \
395+
}
398396

399397
const int64_t num_elements = value.dimension(1);
400398
auto* p_value = &value(start, 0);
@@ -467,7 +465,7 @@ class EntropyEncodeIndexOp : public tensorflow::OpKernel {
467465
tensorflow::mutex mu;
468466
workers->ParallelFor(
469467
handle.size(), cost_per_unit,
470-
[&handle, &mu, context, value, index](int64 start, int64 limit) {
468+
[&handle, &mu, context, value, index](int64_t start, int64_t limit) {
471469
PerShard(handle, index, value, context, &mu, start, limit);
472470
});
473471

@@ -480,19 +478,17 @@ class EntropyEncodeIndexOp : public tensorflow::OpKernel {
480478
TTypes<int32_t>::ConstMatrix value,
481479
tensorflow::OpKernelContext* context,
482480
tensorflow::mutex* mu, int64_t start, int64_t limit) {
483-
#define REQUIRES_OK(status) \
484-
if (auto s = (status); ABSL_PREDICT_FALSE(!s.ok())) { \
485-
tensorflow::mutex_lock lock(*mu); \
486-
context->SetStatus(s); \
487-
return; \
488-
}
489-
490481
#define REQUIRES(cond, status) \
491482
if (!ABSL_PREDICT_TRUE(cond)) { \
492483
tensorflow::mutex_lock lock(*mu); \
493484
context->SetStatus(status); \
494485
return; \
495486
}
487+
#define REQUIRES_OK(status) \
488+
{ \
489+
auto s = (status); \
490+
REQUIRES(s.ok(), s); \
491+
}
496492

497493
const int64_t num_elements = value.dimension(1);
498494
const int32_t* p_value = &value(start, 0);
@@ -621,7 +617,7 @@ class EntropyDecodeChannelOp : public tensorflow::OpKernel {
621617
tensorflow::mutex mu;
622618
workers->ParallelFor(handle.size(), cost_per_unit,
623619
[&handle, &mu, context, index_stride, &output](
624-
int64 start, int64 limit) {
620+
int64_t start, int64_t limit) {
625621
PerShard(handle, index_stride, output, context, &mu,
626622
start, limit);
627623
});
@@ -634,19 +630,17 @@ class EntropyDecodeChannelOp : public tensorflow::OpKernel {
634630
TTypes<int32_t>::Matrix output,
635631
tensorflow::OpKernelContext* context,
636632
tensorflow::mutex* mu, int64_t start, int64_t limit) {
637-
#define REQUIRES_OK(status) \
638-
if (auto s = (status); ABSL_PREDICT_FALSE(!s.ok())) { \
639-
tensorflow::mutex_lock lock(*mu); \
640-
context->SetStatus(s); \
641-
return; \
642-
}
643-
644633
#define REQUIRES(cond, status) \
645634
if (!ABSL_PREDICT_TRUE(cond)) { \
646635
tensorflow::mutex_lock lock(*mu); \
647636
context->SetStatus(status); \
648637
return; \
649638
}
639+
#define REQUIRES_OK(status) \
640+
{ \
641+
auto s = (status); \
642+
REQUIRES(s.ok(), s); \
643+
}
650644

651645
const int64_t num_elements = output.dimension(1);
652646
auto* p_output = &output(start, 0);
@@ -723,7 +717,7 @@ class EntropyDecodeIndexOp : public tensorflow::OpKernel {
723717
tensorflow::mutex mu;
724718
workers->ParallelFor(
725719
handle.size(), cost_per_unit,
726-
[&handle, &mu, context, index, &output](int64 start, int64 limit) {
720+
[&handle, &mu, context, index, &output](int64_t start, int64_t limit) {
727721
PerShard(handle, index, output, context, &mu, start, limit);
728722
});
729723

@@ -736,19 +730,17 @@ class EntropyDecodeIndexOp : public tensorflow::OpKernel {
736730
TTypes<int32_t>::Matrix output,
737731
tensorflow::OpKernelContext* context,
738732
tensorflow::mutex* mu, int64_t start, int64_t limit) {
739-
#define REQUIRES_OK(status) \
740-
if (auto s = (status); ABSL_PREDICT_FALSE(!s.ok())) { \
741-
tensorflow::mutex_lock lock(*mu); \
742-
context->SetStatus(s); \
743-
return; \
744-
}
745-
746733
#define REQUIRES(cond, status) \
747734
if (!ABSL_PREDICT_TRUE(cond)) { \
748735
tensorflow::mutex_lock lock(*mu); \
749736
context->SetStatus(status); \
750737
return; \
751738
}
739+
#define REQUIRES_OK(status) \
740+
{ \
741+
auto s = (status); \
742+
REQUIRES(s.ok(), s); \
743+
}
752744

753745
const int64_t num_elements = output.dimension(1);
754746
const int32_t* p_index = &index(start, 0);

tensorflow_compression/cc/kernels/range_coder_kernels.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ limitations under the License.
1919
#include <cstdint>
2020
#include <string>
2121

22-
#include "absl/base/integral_types.h"
2322
#include "absl/types/span.h"
2423
#include "tensorflow/core/framework/tensor.h"
2524
#include "tensorflow/core/framework/tensor_types.h"

tensorflow_compression/cc/lib/range_coder.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,20 @@ limitations under the License.
1919
// a digitised message", presented to the Video & Data Recording Conference,
2020
// held in Southampton, July 24-27, 1979.
2121
//
22+
2223
#include "tensorflow_compression/cc/lib/range_coder.h"
2324

2425
#include <cstdint>
2526
#include <limits>
2627
#include <string>
2728

28-
#include "absl/base/integral_types.h"
2929
#include "absl/status/status.h"
3030
#include "absl/strings/string_view.h"
3131
#include "absl/types/span.h"
3232
#include "tensorflow/core/platform/logging.h"
3333

3434
namespace tensorflow_compression {
35+
3536
void RangeEncoder::Encode(int32_t lower, int32_t upper, int precision,
3637
std::string* sink) {
3738
// Input requirement: 0 <= lower < upper <= 2^precision.
@@ -364,7 +365,7 @@ absl::Status RangeDecoder::CheckForErrorInternal(absl::Span<const T> cdf,
364365
}
365366

366367
template absl::Status RangeDecoder::CheckForErrorInternal(
367-
absl::Span<const int16>, int, bool) const;
368+
absl::Span<const int16_t>, int, bool) const;
368369
template absl::Status RangeDecoder::CheckForErrorInternal(
369-
absl::Span<const int32>, int, bool) const;
370+
absl::Span<const int32_t>, int, bool) const;
370371
} // namespace tensorflow_compression

tensorflow_compression/cc/lib/range_coder.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ limitations under the License.
2121
#include <string>
2222
#include <type_traits>
2323

24-
#include "absl/base/integral_types.h"
2524
#include "absl/status/status.h"
2625
#include "absl/strings/string_view.h"
2726
#include "absl/types/span.h"
2827
#include "tensorflow/core/platform/logging.h"
2928

3029
namespace tensorflow_compression {
30+
3131
class RangeEncoder {
3232
public:
3333
RangeEncoder() = default;
@@ -196,7 +196,7 @@ class RangeDecoder {
196196
static const T* Search(uint64_t lower_bound, uint64_t size, const T* pv,
197197
int64_t len) {
198198
return std::find_if(pv, pv + len, [lower_bound, size](T value) {
199-
return lower_bound <= size * static_cast<uint64>(value);
199+
return lower_bound <= size * static_cast<uint64_t>(value);
200200
});
201201
}
202202
};

0 commit comments

Comments
 (0)