Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automated rollback of commit 24787842adfefe35f5a520313d775b14c29f143a #22131

Merged
merged 1 commit into from
Sep 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions tensorflow/compiler/aot/embedded_protocol_buffers.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ limitations under the License.
#ifndef TENSORFLOW_COMPILER_AOT_EMBEDDED_PROTOCOL_BUFFERS_H_
#define TENSORFLOW_COMPILER_AOT_EMBEDDED_PROTOCOL_BUFFERS_H_

#include "absl/strings/string_view.h"
#include "absl/types/span.h"
#include "tensorflow/compiler/xla/statusor.h"
#include "tensorflow/core/platform/protobuf.h"
Expand Down
1 change: 1 addition & 0 deletions tensorflow/compiler/jit/xla_cluster_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ limitations under the License.
#ifndef TENSORFLOW_COMPILER_JIT_XLA_CLUSTER_UTIL_H_
#define TENSORFLOW_COMPILER_JIT_XLA_CLUSTER_UTIL_H_

#include "absl/strings/string_view.h"
#include "absl/types/optional.h"
#include "tensorflow/compiler/jit/graphcycles/graphcycles.h"
#include "tensorflow/core/graph/algorithm.h"
Expand Down
4 changes: 2 additions & 2 deletions tensorflow/compiler/jit/xla_device_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,8 @@ void XlaDeviceContext::CopyDeviceTensorToCPU(const Tensor* device_tensor,
StringPiece tensor_name,
Device* device, Tensor* cpu_tensor,
StatusCallback done) {
manager_.CopyDeviceTensorToCPU(device_tensor, tensor_name, device, cpu_tensor,
done);
manager_.CopyDeviceTensorToCPU(device_tensor, absl::string_view(tensor_name),
device, cpu_tensor, done);
}

void XlaDeviceContext::CopyDeviceTensorToDevice(const Tensor& src_tensor,
Expand Down
8 changes: 6 additions & 2 deletions tensorflow/compiler/jit/xla_device_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ limitations under the License.
#include "tensorflow/core/framework/allocator.h"
#include "tensorflow/core/framework/device_base.h"
#include "tensorflow/core/lib/core/status.h"
#include "tensorflow/core/lib/core/stringpiece.h"

namespace tensorflow {

Expand Down Expand Up @@ -110,9 +111,12 @@ class XlaDeviceContext : public DeviceContext {
void CopyCPUTensorToDevice(const Tensor* cpu_tensor, Device* device,
Tensor* device_tensor,
StatusCallback done) const override;
// TODO(rlahaye): Replace StringPiece with absl::string_view when the
// StringPiece->absl::string_view change is rolled forward.
void CopyDeviceTensorToCPU(const Tensor* device_tensor,
StringPiece tensor_name, Device* device,
Tensor* cpu_tensor, StatusCallback done) override;
StringPiece tensor_name, // non-ABSL OK
Device* device, Tensor* cpu_tensor,
StatusCallback done) override;
void CopyDeviceTensorToDevice(const Tensor& src_tensor, Tensor* dst_tensor,
const StatusCallback& done);

Expand Down
1 change: 1 addition & 0 deletions tensorflow/compiler/tf2xla/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ cc_library(
"//tensorflow/core:protos_all_cc",
"//tensorflow/core:stream_executor_no_cuda",
"@com_google_absl//absl/memory",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/types:span",
],
alwayslink = 1,
Expand Down
10 changes: 5 additions & 5 deletions tensorflow/compiler/tf2xla/resource_operation_table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ limitations under the License.

#include "tensorflow/compiler/tf2xla/resource_operation_table.h"
#include "absl/algorithm/container.h"
#include "tensorflow/core/lib/core/stringpiece.h"
#include "tensorflow/core/lib/gtl/flatmap.h"

namespace tensorflow {
Expand All @@ -31,8 +32,7 @@ namespace tensorflow {
}

static gtl::FlatMap<StringPiece, XlaResourceOpInfo>* CreateResourceOpInfoMap() {
gtl::FlatMap<StringPiece, XlaResourceOpInfo>* result =
new gtl::FlatMap<StringPiece, XlaResourceOpInfo>;
auto* result = new gtl::FlatMap<StringPiece, XlaResourceOpInfo>;

auto add = [&](StringPiece op, XlaResourceOpKind op_kind,
XlaResourceKind resource_kind) {
Expand Down Expand Up @@ -110,18 +110,18 @@ GetStaticResourceOpInfoMap() {
return *op_info_map;
}

const XlaResourceOpInfo* GetResourceOpInfoForOp(StringPiece op) {
const XlaResourceOpInfo* GetResourceOpInfoForOp(absl::string_view op) {
const gtl::FlatMap<StringPiece, XlaResourceOpInfo>& op_infos =
GetStaticResourceOpInfoMap();
auto it = op_infos.find(op);
auto it = op_infos.find(StringPiece(op.data(), op.length()));
return it == op_infos.end() ? nullptr : &it->second;
}

namespace resource_op_table_internal {
std::vector<StringPiece> GetKnownResourceOps() {
std::vector<StringPiece> result;
for (const auto& p : GetStaticResourceOpInfoMap()) {
result.push_back(p.first);
result.push_back(absl::string_view(p.first));
}
absl::c_sort(result);
return result;
Expand Down
1 change: 1 addition & 0 deletions tensorflow/compiler/tf2xla/tf2xla_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ limitations under the License.

#include <unordered_map>

#include "absl/strings/string_view.h"
#include "tensorflow/compiler/tf2xla/tf2xla.pb.h"
#include "tensorflow/core/framework/graph.pb.h"
#include "tensorflow/core/framework/kernel_def.pb.h"
Expand Down
11 changes: 7 additions & 4 deletions tensorflow/compiler/tf2xla/xla_op_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ Status XlaOpKernelContext::ConstantInput(int index,
static xla::StatusOr<int> InputIndex(XlaOpKernelContext* context,
StringPiece name) {
int start, stop;
TF_RETURN_IF_ERROR(context->op_kernel().InputRange(name, &start, &stop));
TF_RETURN_IF_ERROR(context->op_kernel().InputRange(
StringPiece(name.data(), name.length()), &start, &stop));
if (stop != start + 1) {
return errors::InvalidArgument("OpKernel used list-valued input name '",
name,
Expand Down Expand Up @@ -365,7 +366,8 @@ Status XlaOpKernelContext::InputList(StringPiece name,
std::vector<xla::XlaOp>* handles,
std::vector<TensorShape>* shapes) {
OpInputList inputs;
TF_RETURN_IF_ERROR(context_->input_list(name, &inputs));
TF_RETURN_IF_ERROR(
context_->input_list(StringPiece(name.data(), name.size()), &inputs));
handles->clear();
shapes->clear();
for (const Tensor& input : inputs) {
Expand All @@ -378,7 +380,8 @@ Status XlaOpKernelContext::InputList(StringPiece name,
Status XlaOpKernelContext::ConstantInputList(
StringPiece name, std::vector<xla::Literal>* outputs) {
int start, stop;
TF_RETURN_IF_ERROR(op_kernel().InputRange(name, &start, &stop));
TF_RETURN_IF_ERROR(op_kernel().InputRange(
StringPiece(name.data(), name.size()), &start, &stop));
outputs->resize(stop - start);
for (int i = start; i < stop; ++i) {
TF_RETURN_IF_ERROR(ConstantInput(i, &(*outputs)[i]));
Expand Down Expand Up @@ -612,7 +615,7 @@ const xla::XlaComputation* XlaOpKernelContext::GetOrCreateMul(

const Tensor& XlaOpKernelContext::GetInputTensorByName(StringPiece name) {
const Tensor* tensor;
CHECK(context_->input(name, &tensor).ok());
CHECK(context_->input(StringPiece(name.data(), name.length()), &tensor).ok());
return *tensor;
}

Expand Down
1 change: 1 addition & 0 deletions tensorflow/compiler/tf2xla/xla_op_registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ limitations under the License.
#include <unordered_map>
#include <vector>

#include "absl/strings/string_view.h"
#include "tensorflow/core/common_runtime/device_factory.h"
#include "tensorflow/core/common_runtime/local_device.h"
#include "tensorflow/core/framework/device_base.h"
Expand Down
6 changes: 3 additions & 3 deletions tensorflow/compiler/xla/packed_literal_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ limitations under the License.
#include "tensorflow/compiler/xla/types.h"
#include "tensorflow/compiler/xla/util.h"
#include "tensorflow/compiler/xla/xla_data.pb.h"
#include "tensorflow/core/lib/core/casts.h"
#include "tensorflow/core/lib/core/stringpiece.h"
#include "tensorflow/core/platform/logging.h"
#include "tensorflow/core/platform/protobuf.h"
#include "tensorflow/core/platform/types.h"
Expand Down Expand Up @@ -64,7 +64,7 @@ StatusOr<std::unique_ptr<Literal>> PackedLiteralReader::Read(
absl::Span<const float> field = result->data<float>();
char* data = tensorflow::bit_cast<char*>(field.data());
uint64 bytes = elements * sizeof(float);
tensorflow::StringPiece sp; // non-absl OK
tensorflow::StringPiece sp;
auto s = file_->Read(offset_, bytes, &sp, data);
offset_ += sp.size();
if (!s.ok()) {
Expand All @@ -85,7 +85,7 @@ bool PackedLiteralReader::IsExhausted() const {
// Try to read a single byte from offset_. If we can't, we've
// exhausted the data.
char single_byte[1];
tensorflow::StringPiece sp; // non-absl OK
tensorflow::StringPiece sp;
auto s = file_->Read(offset_, sizeof(single_byte), &sp, single_byte);
return !s.ok();
}
Expand Down
1 change: 1 addition & 0 deletions tensorflow/contrib/makefile/proto_text_cc_files.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ tensorflow/core/lib/hash/hash.cc
tensorflow/core/lib/hash/crc32c.cc
tensorflow/core/lib/hash/crc32c_accelerate.cc
tensorflow/core/lib/core/threadpool.cc
tensorflow/core/lib/core/stringpiece.cc
tensorflow/core/lib/core/status.cc
tensorflow/core/lib/core/coding.cc
tensorflow/core/lib/core/arena.cc
54 changes: 54 additions & 0 deletions tensorflow/core/lib/core/stringpiece.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow/core/lib/core/stringpiece.h"

#include <algorithm>
#include <iostream>

namespace tensorflow {

std::ostream& operator<<(std::ostream& o, StringPiece piece) {
o.write(piece.data(), piece.size());
return o;
}

size_t StringPiece::find(char c, size_t pos) const {
if (pos >= size_) {
return npos;
}
const char* result =
reinterpret_cast<const char*>(memchr(data_ + pos, c, size_ - pos));
return result != nullptr ? result - data_ : npos;
}

// Search range is [0..pos] inclusive. If pos == npos, search everything.
size_t StringPiece::rfind(char c, size_t pos) const {
if (size_ == 0) return npos;
for (const char* p = data_ + std::min(pos, size_ - 1); p >= data_; p--) {
if (*p == c) {
return p - data_;
}
}
return npos;
}

StringPiece StringPiece::substr(size_t pos, size_t n) const {
if (pos > size_) pos = size_;
if (n > size_ - pos) n = size_ - pos;
return StringPiece(data_ + pos, n);
}

} // namespace tensorflow
117 changes: 114 additions & 3 deletions tensorflow/core/lib/core/stringpiece.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,124 @@ limitations under the License.
#include <string.h>
#include <iosfwd>
#include <string>
#include "absl/strings/string_view.h"
#include <type_traits>
#include "tensorflow/core/platform/types.h"

namespace tensorflow {

// Deprecated: please use absl::string_view directly.
using StringPiece = absl::string_view;
class StringPiece {
public:
typedef size_t size_type;

// Create an empty slice.
StringPiece() : data_(nullptr), size_(0) {}

// Create a slice that refers to d[0,n-1].
StringPiece(const char* d, size_t n) : data_(d), size_(n) {}

// Create a slice that refers to the contents of "s"
StringPiece(const string& s) : data_(s.data()), size_(s.size()) {}

// Create a slice that refers to s[0,strlen(s)-1]
StringPiece(const char* s) : data_(s), size_(strlen(s)) {}

// Return a pointer to the beginning of the referenced data
const char* data() const { return data_; }

// Return the length (in bytes) of the referenced data
size_t size() const { return size_; }

// Return true iff the length of the referenced data is zero
bool empty() const { return size_ == 0; }

typedef const char* const_iterator;
typedef const char* iterator;
iterator begin() const { return data_; }
iterator end() const { return data_ + size_; }

static const size_t npos = size_type(-1);

// Return the ith byte in the referenced data.
// REQUIRES: n < size()
char operator[](size_t n) const {
assert(n < size());
return data_[n];
}

// Drop the first "n" bytes from this slice.
void remove_prefix(size_t n) {
assert(n <= size());
data_ += n;
size_ -= n;
}

void remove_suffix(size_t n) {
assert(size_ >= n);
size_ -= n;
}

size_t find(char c, size_t pos = 0) const;
size_t rfind(char c, size_t pos = npos) const;

StringPiece substr(size_t pos, size_t n = npos) const;

// Three-way comparison. Returns value:
// < 0 iff "*this" < "b",
// == 0 iff "*this" == "b",
// > 0 iff "*this" > "b"
int compare(StringPiece b) const;

// Converts to various kinds of strings, including `std::basic_string`.
template <typename S>
explicit operator S() const {
static_assert(
std::is_same<char, typename S::value_type>::value,
"Type mismatch: S must be a string with character type char.");
static_assert(
std::is_same<std::char_traits<char>, typename S::traits_type>::value,
"Type mismatch: S must be a string with traits type "
"std::char_traits<char>.");
if (!data()) return {};
return S(data(), size());
}

private:
const char* data_;
size_t size_;

// Intentionally copyable
};

inline bool operator==(StringPiece x, StringPiece y) {
return ((x.size() == y.size()) &&
(memcmp(x.data(), y.data(), x.size()) == 0));
}

inline bool operator!=(StringPiece x, StringPiece y) { return !(x == y); }

inline bool operator<(StringPiece x, StringPiece y) { return x.compare(y) < 0; }
inline bool operator>(StringPiece x, StringPiece y) { return x.compare(y) > 0; }
inline bool operator<=(StringPiece x, StringPiece y) {
return x.compare(y) <= 0;
}
inline bool operator>=(StringPiece x, StringPiece y) {
return x.compare(y) >= 0;
}

inline int StringPiece::compare(StringPiece b) const {
const size_t min_len = (size_ < b.size_) ? size_ : b.size_;
int r = memcmp(data_, b.data_, min_len);
if (r == 0) {
if (size_ < b.size_)
r = -1;
else if (size_ > b.size_)
r = +1;
}
return r;
}

// allow StringPiece to be logged
extern std::ostream& operator<<(std::ostream& o, tensorflow::StringPiece piece);

} // namespace tensorflow

Expand Down
3 changes: 0 additions & 3 deletions tensorflow/core/lib/strings/strcat.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,6 @@ class AlphaNum {
AlphaNum(const StringPiece &pc) : piece_(pc) {} // NOLINT(runtime/explicit)
AlphaNum(const tensorflow::string &str) // NOLINT(runtime/explicit)
: piece_(str) {}
template <typename A>
AlphaNum(const std::basic_string<char, std::char_traits<char>, A> &str)
: piece_(str) {} // NOLINT(runtime/explicit)

StringPiece::size_type size() const { return piece_.size(); }
const char *data() const { return piece_.data(); }
Expand Down