Skip to content

Commit

Permalink
[TF-DF] [YDF] Remove old Tensorflow Status compatibility
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 689818683
  • Loading branch information
rstz authored and copybara-github committed Oct 25, 2024
1 parent 523447e commit aa0584c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 132 deletions.
19 changes: 0 additions & 19 deletions yggdrasil_decision_forests/utils/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ cc_library_ydf(
":compatibility",
":logging",
":status_macros",
":tensorflow",
"@com_google_absl//absl/log",
"@com_google_absl//absl/log:check",
"@com_google_absl//absl/memory",
Expand Down Expand Up @@ -211,7 +210,6 @@ cc_library_ydf(
deps = [
":sharded_io",
":status_macros",
":tensorflow",
"@com_google_absl//absl/log:check",
"@com_google_absl//absl/memory",
"@com_google_absl//absl/status",
Expand Down Expand Up @@ -672,23 +670,6 @@ cc_library_ydf(
hdrs = ["random.h"],
)

cc_library_ydf(
name = "tensorflow",
hdrs = ["tensorflow.h"],
tags = ["tf_dep"],
deps = [
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings",
] + select({
"//yggdrasil_decision_forests:tensorflow_with_header_lib": [
"@release_or_nightly//:tensorflow_libtensorflow_framework",
"@release_or_nightly//:tensorflow_tf_header_lib",
],
"//conditions:default": ["@org_tensorflow//tensorflow/core/platform:status"],
}),
)

cc_library_ydf(
name = "compatibility",
hdrs = ["compatibility.h"],
Expand Down
62 changes: 32 additions & 30 deletions yggdrasil_decision_forests/utils/filesystem_tensorflow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,29 @@

#include "yggdrasil_decision_forests/utils/filesystem_tensorflow.h"

#include <algorithm>
#include <cstdint>
#include <cstring>
#include <initializer_list>
#include <memory>
#include <regex> // NOLINT
#include <string>
#include <vector>

#include "absl/log/log.h"
#include "absl/status/status.h"
#include "absl/strings/numbers.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
#include "absl/strings/string_view.h"
#include "src/google/protobuf/message.h"
#include "src/google/protobuf/message_lite.h"
#include "src/google/protobuf/text_format.h"
#include "tensorflow/core/platform/env.h"
#include "tensorflow/core/platform/file_system.h"
#include "tensorflow/core/platform/path.h"
#include "yggdrasil_decision_forests/utils/logging.h"
#include "yggdrasil_decision_forests/utils/status_macros.h"
#include "yggdrasil_decision_forests/utils/tensorflow.h"

namespace tensorflow {

Expand All @@ -46,8 +55,6 @@ WritableFileWrapper::~WritableFileWrapper() {

namespace file {

using yggdrasil_decision_forests::utils::ToUtilStatus;

std::string JoinPathList(std::initializer_list<absl::string_view> paths) {
return ::tensorflow::io::internal::JoinPathImpl(paths);
}
Expand Down Expand Up @@ -85,29 +92,26 @@ bool GenerateShardedFilenames(absl::string_view spec,

absl::Status Match(absl::string_view pattern, std::vector<std::string>* results,
const int options) {
RETURN_IF_ERROR(yggdrasil_decision_forests::utils::ToUtilStatus(
tensorflow::Env::Default()->GetMatchingPaths(std::string(pattern),
results)));
RETURN_IF_ERROR(tensorflow::Env::Default()->GetMatchingPaths(
std::string(pattern), results));
std::sort(results->begin(), results->end());
return absl::OkStatus();
}

absl::Status RecursivelyCreateDir(absl::string_view path, int options) {
return yggdrasil_decision_forests::utils::ToUtilStatus(
tensorflow::Env::Default()->RecursivelyCreateDir(std::string(path)));
return tensorflow::Env::Default()->RecursivelyCreateDir(std::string(path));
}

absl::Status RecursivelyDelete(absl::string_view path, int options) {
int64_t ignore_1, ignore_2;
return yggdrasil_decision_forests::utils::ToUtilStatus(
tensorflow::Env::Default()->DeleteRecursively(std::string(path),
&ignore_1, &ignore_2));
return tensorflow::Env::Default()->DeleteRecursively(std::string(path),
&ignore_1, &ignore_2);
}

absl::Status FileInputByteStream::Open(absl::string_view path) {
std::unique_ptr<::tensorflow::RandomAccessFile> file;
RETURN_IF_ERROR(ToUtilStatus(tensorflow::Env::Default()->NewRandomAccessFile(
std::string(path), &file)));
RETURN_IF_ERROR(tensorflow::Env::Default()->NewRandomAccessFile(
std::string(path), &file));
file_ =
std::make_unique<::tensorflow::RandomAccessFileWrapper>(file.release());
offset_ = 0;
Expand All @@ -119,10 +123,10 @@ absl::StatusOr<int> FileInputByteStream::ReadUpTo(char* buffer, int max_read) {
if (max_read > scrath_.size()) {
scrath_.resize(max_read);
}
const auto tf_status =
const auto status =
file_->item()->Read(offset_, max_read, &result, &scrath_[0]);
if (!tf_status.ok() && tf_status.code() != tensorflow::error::OUT_OF_RANGE) {
return ToUtilStatus(tf_status);
if (!status.ok() && status.code() != absl::StatusCode::kOutOfRange) {
return status;
}
offset_ += result.size();
std::memcpy(buffer, result.data(), result.size());
Expand All @@ -135,14 +139,14 @@ absl::StatusOr<bool> FileInputByteStream::ReadExactly(char* buffer,
if (num_read > scrath_.size()) {
scrath_.resize(num_read);
}
const auto tf_status =
const auto status =
file_->item()->Read(offset_, num_read, &result, &scrath_[0]);
if (!tf_status.ok()) {
if (tf_status.code() == tensorflow::error::OUT_OF_RANGE && result.empty() &&
if (!status.ok()) {
if (status.code() == absl::StatusCode::kOutOfRange && result.empty() &&
num_read > 0) {
return false;
}
return ToUtilStatus(tf_status);
return status;
}
offset_ += result.size();
std::memcpy(buffer, result.data(), result.size());
Expand All @@ -156,19 +160,17 @@ absl::Status FileInputByteStream::Close() {

absl::Status FileOutputByteStream::Open(absl::string_view path) {
std::unique_ptr<::tensorflow::WritableFile> file;
RETURN_IF_ERROR(ToUtilStatus(
tensorflow::Env::Default()->NewWritableFile(std::string(path), &file)));
RETURN_IF_ERROR(
tensorflow::Env::Default()->NewWritableFile(std::string(path), &file));
file_ = std::make_unique<::tensorflow::WritableFileWrapper>(file.release());
return absl::OkStatus();
}

absl::Status FileOutputByteStream::Write(absl::string_view chunk) {
return ToUtilStatus(file_->item()->Append(chunk));
return file_->item()->Append(chunk);
}

absl::Status FileOutputByteStream::Close() {
return ToUtilStatus(file_->item()->Close());
}
absl::Status FileOutputByteStream::Close() { return file_->item()->Close(); }

absl::Status SetBinaryProto(absl::string_view path,
const google::protobuf::MessageLite& message, int unused) {
Expand Down Expand Up @@ -225,15 +227,15 @@ absl::StatusOr<bool> FileExists(absl::string_view path) {
if (exist_status.ok()) {
return true;
}
if (exist_status.code() == tensorflow::error::NOT_FOUND) {
if (exist_status.code() == absl::StatusCode::kNotFound) {
return false;
}
return ToUtilStatus(exist_status);
return exist_status;
}

absl::Status Rename(absl::string_view from, absl::string_view to, int options) {
return ToUtilStatus(tensorflow::Env::Default()->RenameFile(std::string(from),
std::string(to)));
return tensorflow::Env::Default()->RenameFile(std::string(from),
std::string(to));
}

std::string GetBasename(absl::string_view path) {
Expand Down
26 changes: 13 additions & 13 deletions yggdrasil_decision_forests/utils/sharded_io_tfrecord.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@
#include "absl/status/statusor.h"
#include "tensorflow/core/lib/io/record_reader.h"
#include "tensorflow/core/lib/io/record_writer.h"
#include "tensorflow/core/platform/env.h"
#include "tensorflow/core/platform/file_system.h"

#include "absl/status/status.h"
#include "absl/strings/string_view.h"
#include "tensorflow/core/platform/tstring.h"
#include "yggdrasil_decision_forests/utils/sharded_io.h"
#include "yggdrasil_decision_forests/utils/status_macros.h"
#include "yggdrasil_decision_forests/utils/tensorflow.h"

namespace yggdrasil_decision_forests {
namespace utils {
Expand Down Expand Up @@ -73,9 +74,8 @@ class TFRecordShardedWriter : public ShardedWriter<T> {

template <typename T>
absl::Status TFRecordShardedReader<T>::OpenShard(const absl::string_view path) {
RETURN_IF_ERROR(
ToUtilStatus(::tensorflow::Env::Default()->NewRandomAccessFile(
std::string(path), &file_)));
RETURN_IF_ERROR(::tensorflow::Env::Default()->NewRandomAccessFile(
std::string(path), &file_));
reader_ = std::make_unique<::tensorflow::io::SequentialRecordReader>(
file_.get(),
::tensorflow::io::RecordReaderOptions::CreateRecordReaderOptions("GZIP"));
Expand All @@ -85,25 +85,25 @@ absl::Status TFRecordShardedReader<T>::OpenShard(const absl::string_view path) {

template <typename T>
absl::StatusOr<bool> TFRecordShardedReader<T>::NextInShard(T* example) {
const auto tf_status = reader_->ReadRecord(&buffer_);
if (tf_status.ok()) {
const auto status = reader_->ReadRecord(&buffer_);
if (status.ok()) {
// Valid example.
example->ParseFromArray(buffer_.data(), buffer_.size());
return true;
} else if (tf_status.code() == ::tensorflow::error::OUT_OF_RANGE) {
} else if (status.code() == absl::StatusCode::kOutOfRange) {
// No more examples available.
return false;
} else {
// Reading error.
return ToUtilStatus(tf_status);
return status;
}
}

template <typename T>
absl::Status TFRecordShardedWriter<T>::OpenShard(const absl::string_view path) {
RETURN_IF_ERROR(CloseWithStatus());
RETURN_IF_ERROR(ToUtilStatus(::tensorflow::Env::Default()->NewWritableFile(
std::string(path), &file_)));
RETURN_IF_ERROR(
::tensorflow::Env::Default()->NewWritableFile(std::string(path), &file_));
writer_ = std::make_unique<::tensorflow::io::RecordWriter>(
file_.get(),
::tensorflow::io::RecordWriterOptions::CreateRecordWriterOptions("GZIP"));
Expand All @@ -114,17 +114,17 @@ template <typename T>
absl::Status TFRecordShardedWriter<T>::WriteInShard(const T& value) {
buffer_.clear();
value.AppendToString(&buffer_);
return ToUtilStatus(writer_->WriteRecord(buffer_));
return writer_->WriteRecord(buffer_);
}

template <typename T>
absl::Status TFRecordShardedWriter<T>::CloseWithStatus() {
if (!writer_) {
return absl::OkStatus();
}
RETURN_IF_ERROR(ToUtilStatus(writer_->Close()));
RETURN_IF_ERROR(writer_->Close());
writer_ = nullptr;
RETURN_IF_ERROR(ToUtilStatus(file_->Close()));
RETURN_IF_ERROR(file_->Close());
file_ = nullptr;
return absl::OkStatus();
}
Expand Down
70 changes: 0 additions & 70 deletions yggdrasil_decision_forests/utils/tensorflow.h

This file was deleted.

0 comments on commit aa0584c

Please sign in to comment.