From a7421d38cc64abaebb5ba4d7616b3bc625a9a3ae Mon Sep 17 00:00:00 2001 From: Oceania2018 Date: Sat, 30 Jan 2021 05:10:24 -0600 Subject: [PATCH 1/6] Expose TString related C API #46803 --- tensorflow/c/BUILD | 12 ++++++++++++ tensorflow/c/tf_tstring.cc | 39 ++++++++++++++++++++++++++++++++++++++ tensorflow/c/tf_tstring.h | 32 +++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 tensorflow/c/tf_tstring.cc diff --git a/tensorflow/c/BUILD b/tensorflow/c/BUILD index 954f9dcb303564..e10d1c7ca9ef46 100644 --- a/tensorflow/c/BUILD +++ b/tensorflow/c/BUILD @@ -156,6 +156,7 @@ tf_cuda_library( "tf_file_statistics.h", "tf_status.h", "tf_tensor.h", + "tf_tstring.h", ], copts = tf_copts(), visibility = ["//visibility:public"], @@ -166,6 +167,7 @@ tf_cuda_library( ":tf_status_internal", ":tf_file_statistics", ":tf_tensor_internal", + ":tf_tstring", ] + select({ "//tensorflow:with_xla_support": [ "//tensorflow/compiler/tf2xla:xla_compiler", @@ -313,6 +315,16 @@ cc_library( visibility = ["//visibility:public"], ) +cc_library( + name = "tf_tstring", + srcs = [ + "tf_tstring.cc", + "//tensorflow/core/platform:ctstring" + ], + hdrs = ["tf_tstring.h"], + visibility = ["//visibility:public"] +) + cc_library( name = "tf_file_statistics", hdrs = ["tf_file_statistics.h"], diff --git a/tensorflow/c/tf_tstring.cc b/tensorflow/c/tf_tstring.cc new file mode 100644 index 00000000000000..a5cc48bbd5cf2f --- /dev/null +++ b/tensorflow/c/tf_tstring.cc @@ -0,0 +1,39 @@ +/* Copyright 2021 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/c/tf_tstring.h" +#include "tensorflow/core/platform/ctstring_internal.h" + +TF_TString* TF_StringInit() { + TF_TString* tstr; + TF_TString_Init(tstr); + return tstr; +} + +void TF_StringCopy(TF_TString *dst, const char *src, size_t size) { + TF_TString_Copy(dst, src, size); +} + +const char* TF_StringGetDataPointer(TF_TString* tstr) { + return TF_TString_GetDataPointer(tstr); +} + +size_t TF_StringGetSize(TF_TString* tstr) { + return TF_TString_GetSize(tstr); +} + +void TF_StringDealloc(TF_TString* tstr) { + TF_TString_Dealloc(tstr); +} \ No newline at end of file diff --git a/tensorflow/c/tf_tstring.h b/tensorflow/c/tf_tstring.h index 8b576ff8197bc5..996dfb76cd025f 100644 --- a/tensorflow/c/tf_tstring.h +++ b/tensorflow/c/tf_tstring.h @@ -17,4 +17,36 @@ limitations under the License. #include "tensorflow/core/platform/ctstring.h" +#ifdef SWIG +#define TF_CAPI_EXPORT +#else +#if defined(_WIN32) +#ifdef TF_COMPILE_LIBRARY +#define TF_CAPI_EXPORT __declspec(dllexport) +#else +#define TF_CAPI_EXPORT __declspec(dllimport) +#endif // TF_COMPILE_LIBRARY +#else +#define TF_CAPI_EXPORT __attribute__((visibility("default"))) +#endif // _WIN32 +#endif // SWIG + +#ifdef __cplusplus +extern "C" { +#endif + +TF_CAPI_EXPORT extern TF_TString* TF_StringInit(); + +TF_CAPI_EXPORT extern void TF_StringCopy(TF_TString *dst, const char *src, size_t size); + +TF_CAPI_EXPORT extern const char* TF_StringGetDataPointer(TF_TString* tstr); + +TF_CAPI_EXPORT extern size_t TF_StringGetSize(TF_TString* tstr); + +TF_CAPI_EXPORT extern void TF_StringDealloc(TF_TString* tstr); + +#ifdef __cplusplus +} /* end extern "C" */ +#endif + #endif // THIRD_PARTY_TENSORFLOW_C_TF_TSTRING_H_ From 76d8a9bb520b411eab51109f3a0b211bf0bf8a35 Mon Sep 17 00:00:00 2001 From: Oceania2018 Date: Tue, 2 Feb 2021 09:30:48 -0600 Subject: [PATCH 2/6] fix buildifier errors for BUILD #46803 --- tensorflow/c/BUILD | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tensorflow/c/BUILD b/tensorflow/c/BUILD index e10d1c7ca9ef46..7886c2044125c1 100644 --- a/tensorflow/c/BUILD +++ b/tensorflow/c/BUILD @@ -319,10 +319,10 @@ cc_library( name = "tf_tstring", srcs = [ "tf_tstring.cc", - "//tensorflow/core/platform:ctstring" + "//tensorflow/core/platform:ctstring", ], hdrs = ["tf_tstring.h"], - visibility = ["//visibility:public"] + visibility = ["//visibility:public"], ) cc_library( From ce5855fbf8d380e1d6d585533700ad88af3a9ee9 Mon Sep 17 00:00:00 2001 From: Oceania2018 Date: Tue, 2 Feb 2021 11:52:03 -0600 Subject: [PATCH 3/6] Init TString from tensor handle #46803 --- tensorflow/c/BUILD | 8 +++++++- tensorflow/c/tf_tstring.cc | 11 ++++++++--- tensorflow/c/tf_tstring.h | 7 +++++-- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/tensorflow/c/BUILD b/tensorflow/c/BUILD index 7886c2044125c1..dbb34d85821b7b 100644 --- a/tensorflow/c/BUILD +++ b/tensorflow/c/BUILD @@ -321,7 +321,13 @@ cc_library( "tf_tstring.cc", "//tensorflow/core/platform:ctstring", ], - hdrs = ["tf_tstring.h"], + hdrs = [ + "tf_tstring.h", + "tf_tensor.h", + "c_api_macros.h", + "tf_datatype.h", + "tf_status.h", + ], visibility = ["//visibility:public"], ) diff --git a/tensorflow/c/tf_tstring.cc b/tensorflow/c/tf_tstring.cc index a5cc48bbd5cf2f..85a20ba6235fc9 100644 --- a/tensorflow/c/tf_tstring.cc +++ b/tensorflow/c/tf_tstring.cc @@ -13,11 +13,12 @@ See the License for the specific language governing permissions and limitations under the License. ==============================================================================*/ +#include "tensorflow/c/tf_tensor.h" #include "tensorflow/c/tf_tstring.h" #include "tensorflow/core/platform/ctstring_internal.h" -TF_TString* TF_StringInit() { - TF_TString* tstr; +TF_TString* TF_StringInit(TF_Tensor* t) { + TF_TString *tstr = static_cast(TF_TensorData(t)); TF_TString_Init(tstr); return tstr; } @@ -26,11 +27,15 @@ void TF_StringCopy(TF_TString *dst, const char *src, size_t size) { TF_TString_Copy(dst, src, size); } +void TF_StringAssignView(TF_TString *dst, const char *src, size_t size) { + TF_TString_AssignView(dst, src, size); +} + const char* TF_StringGetDataPointer(TF_TString* tstr) { return TF_TString_GetDataPointer(tstr); } -size_t TF_StringGetSize(TF_TString* tstr) { +size_t TF_StringGetSize(const TF_TString* tstr) { return TF_TString_GetSize(tstr); } diff --git a/tensorflow/c/tf_tstring.h b/tensorflow/c/tf_tstring.h index 996dfb76cd025f..50491d3154f512 100644 --- a/tensorflow/c/tf_tstring.h +++ b/tensorflow/c/tf_tstring.h @@ -15,6 +15,7 @@ limitations under the License. #ifndef TENSORFLOW_C_TF_TSTRING_H_ #define TENSORFLOW_C_TF_TSTRING_H_ +#include "tensorflow/c/tf_tensor.h" #include "tensorflow/core/platform/ctstring.h" #ifdef SWIG @@ -35,13 +36,15 @@ limitations under the License. extern "C" { #endif -TF_CAPI_EXPORT extern TF_TString* TF_StringInit(); +TF_CAPI_EXPORT extern TF_TString* TF_StringInit(TF_Tensor* t); TF_CAPI_EXPORT extern void TF_StringCopy(TF_TString *dst, const char *src, size_t size); +TF_CAPI_EXPORT extern void TF_StringAssignView(TF_TString *dst, const char *src, size_t size); + TF_CAPI_EXPORT extern const char* TF_StringGetDataPointer(TF_TString* tstr); -TF_CAPI_EXPORT extern size_t TF_StringGetSize(TF_TString* tstr); +TF_CAPI_EXPORT extern size_t TF_StringGetSize(const TF_TString* tstr); TF_CAPI_EXPORT extern void TF_StringDealloc(TF_TString* tstr); From 6ff0edfd98b242022712585aa91e7ca3f6cf6ea7 Mon Sep 17 00:00:00 2001 From: Oceania2018 Date: Tue, 2 Feb 2021 13:47:32 -0600 Subject: [PATCH 4/6] Add TF_StringGetCapacity & TF_StringGetType #46804 --- tensorflow/c/tf_tstring.cc | 8 ++++++++ tensorflow/c/tf_tstring.h | 4 ++++ 2 files changed, 12 insertions(+) diff --git a/tensorflow/c/tf_tstring.cc b/tensorflow/c/tf_tstring.cc index 85a20ba6235fc9..46e41ff85f4fac 100644 --- a/tensorflow/c/tf_tstring.cc +++ b/tensorflow/c/tf_tstring.cc @@ -35,10 +35,18 @@ const char* TF_StringGetDataPointer(TF_TString* tstr) { return TF_TString_GetDataPointer(tstr); } +TF_TString_Type TF_StringGetType(const TF_TString *str) { + return TF_TString_GetType(str); +} + size_t TF_StringGetSize(const TF_TString* tstr) { return TF_TString_GetSize(tstr); } +size_t TF_StringGetCapacity(const TF_TString *str) { + return TF_TString_GetCapacity(str); +} + void TF_StringDealloc(TF_TString* tstr) { TF_TString_Dealloc(tstr); } \ No newline at end of file diff --git a/tensorflow/c/tf_tstring.h b/tensorflow/c/tf_tstring.h index 50491d3154f512..0024da0c441c37 100644 --- a/tensorflow/c/tf_tstring.h +++ b/tensorflow/c/tf_tstring.h @@ -44,8 +44,12 @@ TF_CAPI_EXPORT extern void TF_StringAssignView(TF_TString *dst, const char *src, TF_CAPI_EXPORT extern const char* TF_StringGetDataPointer(TF_TString* tstr); +TF_CAPI_EXPORT extern TF_TString_Type TF_StringGetType(const TF_TString *str); + TF_CAPI_EXPORT extern size_t TF_StringGetSize(const TF_TString* tstr); +TF_CAPI_EXPORT extern size_t TF_StringGetCapacity(const TF_TString *str); + TF_CAPI_EXPORT extern void TF_StringDealloc(TF_TString* tstr); #ifdef __cplusplus From dcd87b7ceea243c7535aced5c133516c612866f0 Mon Sep 17 00:00:00 2001 From: Oceania2018 Date: Tue, 2 Feb 2021 14:27:15 -0600 Subject: [PATCH 5/6] Adjust hdrs order for buildifier #46804 --- tensorflow/c/BUILD | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tensorflow/c/BUILD b/tensorflow/c/BUILD index dbb34d85821b7b..cca7a00fbce7f9 100644 --- a/tensorflow/c/BUILD +++ b/tensorflow/c/BUILD @@ -322,11 +322,11 @@ cc_library( "//tensorflow/core/platform:ctstring", ], hdrs = [ - "tf_tstring.h", - "tf_tensor.h", "c_api_macros.h", "tf_datatype.h", "tf_status.h", + "tf_tensor.h", + "tf_tstring.h", ], visibility = ["//visibility:public"], ) From 8c692202e93c1393a8fefa127ebc5326394223e2 Mon Sep 17 00:00:00 2001 From: Oceania2018 Date: Mon, 8 Feb 2021 15:43:12 -0600 Subject: [PATCH 6/6] Update TF_StringInit C_API #46803 --- tensorflow/c/tf_tstring.cc | 6 ++---- tensorflow/c/tf_tstring.h | 4 ++-- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/tensorflow/c/tf_tstring.cc b/tensorflow/c/tf_tstring.cc index 46e41ff85f4fac..15624060add69c 100644 --- a/tensorflow/c/tf_tstring.cc +++ b/tensorflow/c/tf_tstring.cc @@ -17,10 +17,8 @@ limitations under the License. #include "tensorflow/c/tf_tstring.h" #include "tensorflow/core/platform/ctstring_internal.h" -TF_TString* TF_StringInit(TF_Tensor* t) { - TF_TString *tstr = static_cast(TF_TensorData(t)); +void TF_StringInit(TF_TString* tstr) { TF_TString_Init(tstr); - return tstr; } void TF_StringCopy(TF_TString *dst, const char *src, size_t size) { @@ -31,7 +29,7 @@ void TF_StringAssignView(TF_TString *dst, const char *src, size_t size) { TF_TString_AssignView(dst, src, size); } -const char* TF_StringGetDataPointer(TF_TString* tstr) { +const char* TF_StringGetDataPointer(const TF_TString *tstr) { return TF_TString_GetDataPointer(tstr); } diff --git a/tensorflow/c/tf_tstring.h b/tensorflow/c/tf_tstring.h index 0024da0c441c37..12eba52fb6765d 100644 --- a/tensorflow/c/tf_tstring.h +++ b/tensorflow/c/tf_tstring.h @@ -36,13 +36,13 @@ limitations under the License. extern "C" { #endif -TF_CAPI_EXPORT extern TF_TString* TF_StringInit(TF_Tensor* t); +TF_CAPI_EXPORT extern void TF_StringInit(TF_TString* t); TF_CAPI_EXPORT extern void TF_StringCopy(TF_TString *dst, const char *src, size_t size); TF_CAPI_EXPORT extern void TF_StringAssignView(TF_TString *dst, const char *src, size_t size); -TF_CAPI_EXPORT extern const char* TF_StringGetDataPointer(TF_TString* tstr); +TF_CAPI_EXPORT extern const char* TF_StringGetDataPointer(const TF_TString *tstr); TF_CAPI_EXPORT extern TF_TString_Type TF_StringGetType(const TF_TString *str);