Skip to content

Commit 78d8fdd

Browse files
tlongeritensorflower-gardener
authored andcommitted
Move tensorflow/c/tf_status... to TSL
This is done by forwarding as many function calls as possible. This will allow to cut some more dependencies from XLA to TensorFlow. PiperOrigin-RevId: 489582513
1 parent 02f340f commit 78d8fdd

16 files changed

Lines changed: 524 additions & 181 deletions

tensorflow/c/BUILD

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# C API for TensorFlow, for use by client language bindings.
33

44
load("@bazel_skylib//lib:selects.bzl", "selects")
5-
load("//tensorflow/core/platform:rules_cc.bzl", "cc_library")
5+
load("//tensorflow/tsl/platform:rules_cc.bzl", "cc_library")
66
load(
77
"//tensorflow:tensorflow.bzl",
88
"check_deps",
@@ -40,6 +40,7 @@ filegroup(
4040
"tf_tensor.h",
4141
"tf_tstring.h",
4242
"//tensorflow/core/platform:ctstring",
43+
"//tensorflow/tsl/c:headers",
4344
] + if_tensorrt([
4445
"//tensorflow/compiler/tf2tensorrt:headers",
4546
]),
@@ -61,7 +62,8 @@ filegroup(
6162
"*test*",
6263
],
6364
) + [
64-
"//tensorflow/core/platform:ctstring",
65+
"//tensorflow/tsl/c:srcs",
66+
"//tensorflow/tsl/platform:ctstring",
6567
"//tensorflow/cc:srcs_no_runtime",
6668
"//tensorflow/core/distributed_runtime:server_lib.h",
6769
],
@@ -80,6 +82,7 @@ cc_library(
8082
"tf_buffer_internal.h",
8183
"tf_status_internal.h",
8284
"tf_tensor_internal.h",
85+
"//tensorflow/tsl/c:tsl_status_internal_headers",
8386
],
8487
visibility = [
8588
"//tensorflow/core:__pkg__",
@@ -185,6 +188,7 @@ tf_cuda_library(
185188
":tf_tensor_internal",
186189
":tf_tstring",
187190
"//tensorflow/core/platform:tstring",
191+
"//tensorflow/tsl/c:tsl_status",
188192
] + select({
189193
"//tensorflow:with_xla_support": [
190194
"//tensorflow/compiler/tf2xla:xla_compiler",
@@ -274,6 +278,7 @@ tf_cuda_library(
274278
hdrs = [
275279
"tf_status.h",
276280
"tf_status_internal.h",
281+
"//tensorflow/tsl/c:tsl_status_internal_headers",
277282
],
278283
visibility = [
279284
"//tensorflow/c:__subpackages__",
@@ -286,7 +291,11 @@ tf_cuda_library(
286291
"//tensorflow/compiler/mlir/tensorflow/c:__subpackages__",
287292
"//tensorflow/core/transforms:__subpackages__",
288293
],
289-
deps = select({
294+
deps = [
295+
"//tensorflow/tsl/platform:status",
296+
"//tensorflow/tsl/c:tsl_status",
297+
"//tensorflow/tsl/c:tsl_status_internal",
298+
] + select({
290299
"//tensorflow:android": [
291300
"//tensorflow/core:portable_tensorflow_lib_lite", # TODO(annarev): exclude runtime srcs
292301
],
@@ -298,7 +307,10 @@ tf_cuda_library(
298307

299308
filegroup(
300309
name = "tf_status_internal_headers",
301-
srcs = ["tf_status_internal.h"],
310+
srcs = [
311+
"tf_status_internal.h",
312+
"//tensorflow/tsl/c:tsl_status_internal_headers",
313+
],
302314
visibility = [
303315
"//tensorflow/python:__subpackages__",
304316
],
@@ -332,9 +344,11 @@ cc_library(
332344
name = "tf_status",
333345
srcs = ["tf_status.cc"],
334346
hdrs = ["tf_status.h"],
347+
copts = tf_copts(),
335348
visibility = ["//visibility:public"],
336349
deps = [
337350
":tf_status_internal",
351+
"//tensorflow/tsl/c:tsl_status",
338352
] + select({
339353
"//tensorflow:android": [
340354
"//tensorflow/core:portable_tensorflow_lib_lite", # TODO(annarev): exclude runtime srcs
@@ -345,22 +359,13 @@ cc_library(
345359
}),
346360
)
347361

348-
tf_cc_test(
349-
name = "tf_status_test",
350-
srcs = ["tf_status_test.cc"],
351-
deps = [
352-
":tf_status",
353-
":tf_status_internal",
354-
"//tensorflow/core:lib",
355-
"//tensorflow/core:test",
356-
"//tensorflow/core:test_main",
357-
],
358-
)
359-
360362
cc_library(
361363
name = "tf_status_headers",
362364
hdrs = ["tf_status.h"],
363365
visibility = ["//visibility:public"],
366+
deps = [
367+
"//tensorflow/tsl/c:tsl_status",
368+
],
364369
)
365370

366371
cc_library(
@@ -380,6 +385,7 @@ cc_library(
380385
deps = [
381386
"//tensorflow/core/platform:status",
382387
"//tensorflow/core/platform:tstring",
388+
"//tensorflow/tsl/c:tsl_status",
383389
],
384390
)
385391

@@ -425,6 +431,7 @@ cc_library(
425431
name = "tf_tensor",
426432
srcs = ["tf_tensor.cc"],
427433
hdrs = ["tf_tensor.h"],
434+
copts = tf_copts(),
428435
visibility = ["//visibility:public"],
429436
deps = [
430437
":c_api_macros",
@@ -574,24 +581,9 @@ tf_cuda_library(
574581
deps = [
575582
":tf_status",
576583
":tf_status_internal",
577-
] + select({
578-
"//tensorflow:android": [
579-
"//tensorflow/core:portable_tensorflow_lib_lite", # TODO(annarev): exclude runtime srcs
580-
],
581-
"//conditions:default": [
582-
"//tensorflow/core:lib",
583-
],
584-
}),
585-
)
586-
587-
tf_cc_test(
588-
name = "tf_status_helper_test",
589-
srcs = ["tf_status_helper_test.cc"],
590-
deps = [
591-
":tf_status_helper",
592-
"//tensorflow/core:lib",
593-
"//tensorflow/core:test",
594-
"//tensorflow/core:test_main",
584+
"//tensorflow/core/platform:errors",
585+
"//tensorflow/core/platform:status",
586+
"//tensorflow/tsl/c:tsl_status_helper",
595587
],
596588
)
597589

tensorflow/c/tf_status.cc

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,39 +16,21 @@ limitations under the License.
1616
#include "tensorflow/c/tf_status.h"
1717

1818
#include "tensorflow/c/tf_status_internal.h"
19-
#include "tensorflow/core/platform/errors.h"
20-
#include "tensorflow/core/platform/status.h"
2119

22-
using ::tensorflow::Status;
23-
using ::tensorflow::error::Code;
24-
using ::tensorflow::errors::IOError;
25-
26-
TF_Status* TF_NewStatus() { return new TF_Status; }
27-
28-
void TF_DeleteStatus(TF_Status* s) { delete s; }
20+
// Trampoline implementation to redirect to TSL. Kept here for backward
21+
// compatibility only.
2922

23+
TF_Status* TF_NewStatus() { return TSL_NewStatus(); }
24+
void TF_DeleteStatus(TF_Status* s) { TSL_DeleteStatus(s); }
3025
void TF_SetStatus(TF_Status* s, TF_Code code, const char* msg) {
31-
if (code == TF_OK) {
32-
s->status = ::tensorflow::OkStatus();
33-
return;
34-
}
35-
s->status = Status(static_cast<Code>(code), tensorflow::StringPiece(msg));
26+
TSL_SetStatus(s, TSL_Code(code), msg);
3627
}
37-
3828
void TF_SetPayload(TF_Status* s, const char* key, const char* value) {
39-
s->status.SetPayload(key, value);
29+
TSL_SetPayload(s, key, value);
4030
}
41-
4231
void TF_SetStatusFromIOError(TF_Status* s, int error_code,
4332
const char* context) {
44-
// TODO(b/139060984): Handle windows when changing its filesystem
45-
s->status = IOError(context, error_code);
46-
}
47-
48-
TF_Code TF_GetCode(const TF_Status* s) {
49-
return static_cast<TF_Code>(s->status.code());
50-
}
51-
52-
const char* TF_Message(const TF_Status* s) {
53-
return s->status.error_message().c_str();
33+
TSL_SetStatusFromIOError(s, error_code, context);
5434
}
35+
TF_Code TF_GetCode(const TF_Status* s) { return TF_Code(TSL_GetCode(s)); }
36+
const char* TF_Message(const TF_Status* s) { return TSL_Message(s); }

tensorflow/c/tf_status.h

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ limitations under the License.
1616
#ifndef TENSORFLOW_C_TF_STATUS_H_
1717
#define TENSORFLOW_C_TF_STATUS_H_
1818

19+
#include "tensorflow/tsl/c/tsl_status.h"
20+
1921
#ifdef SWIG
2022
#define TF_CAPI_EXPORT
2123
#else
@@ -34,30 +36,29 @@ limitations under the License.
3436
extern "C" {
3537
#endif
3638

37-
typedef struct TF_Status TF_Status;
39+
typedef struct TSL_Status TF_Status;
3840

3941
// --------------------------------------------------------------------------
4042
// TF_Code holds an error code. The enum values here are identical to
4143
// corresponding values in error_codes.proto.
42-
typedef enum TF_Code {
43-
TF_OK = 0,
44-
TF_CANCELLED = 1,
45-
TF_UNKNOWN = 2,
46-
TF_INVALID_ARGUMENT = 3,
47-
TF_DEADLINE_EXCEEDED = 4,
48-
TF_NOT_FOUND = 5,
49-
TF_ALREADY_EXISTS = 6,
50-
TF_PERMISSION_DENIED = 7,
51-
TF_UNAUTHENTICATED = 16,
52-
TF_RESOURCE_EXHAUSTED = 8,
53-
TF_FAILED_PRECONDITION = 9,
54-
TF_ABORTED = 10,
55-
TF_OUT_OF_RANGE = 11,
56-
TF_UNIMPLEMENTED = 12,
57-
TF_INTERNAL = 13,
58-
TF_UNAVAILABLE = 14,
59-
TF_DATA_LOSS = 15,
60-
} TF_Code;
44+
typedef TSL_Code TF_Code;
45+
#define TF_OK TSL_OK
46+
#define TF_CANCELLED TSL_CANCELLED
47+
#define TF_UNKNOWN TSL_UNKNOWN
48+
#define TF_INVALID_ARGUMENT TSL_INVALID_ARGUMENT
49+
#define TF_DEADLINE_EXCEEDED TSL_DEADLINE_EXCEEDED
50+
#define TF_NOT_FOUND TSL_NOT_FOUND
51+
#define TF_ALREADY_EXISTS TSL_ALREADY_EXISTS
52+
#define TF_PERMISSION_DENIED TSL_PERMISSION_DENIED
53+
#define TF_UNAUTHENTICATED TSL_UNAUTHENTICATED
54+
#define TF_RESOURCE_EXHAUSTED TSL_RESOURCE_EXHAUSTED
55+
#define TF_FAILED_PRECONDITION TSL_FAILED_PRECONDITION
56+
#define TF_ABORTED TSL_ABORTED
57+
#define TF_OUT_OF_RANGE TSL_OUT_OF_RANGE
58+
#define TF_UNIMPLEMENTED TSL_UNIMPLEMENTED
59+
#define TF_INTERNAL TSL_INTERNAL
60+
#define TF_UNAVAILABLE TSL_UNAVAILABLE
61+
#define TF_DATA_LOSS TSL_DATA_LOSS
6162

6263
// --------------------------------------------------------------------------
6364

tensorflow/c/tf_status_helper.cc

Lines changed: 3 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -17,75 +17,16 @@ limitations under the License.
1717

1818
#include "tensorflow/c/tf_status_internal.h"
1919
#include "tensorflow/core/platform/errors.h"
20+
#include "tensorflow/tsl/c/tsl_status_helper.h"
2021

2122
namespace tsl {
2223

2324
void Set_TF_Status_from_Status(TF_Status* tf_status, const Status& status) {
24-
tensorflow::error::Code code = status.code();
25-
const char* message(status.error_message().c_str());
26-
27-
switch (code) {
28-
case tensorflow::error::OK:
29-
assert(TF_GetCode(tf_status) == TF_OK);
30-
break;
31-
case tensorflow::error::CANCELLED:
32-
TF_SetStatus(tf_status, TF_CANCELLED, message);
33-
break;
34-
case tensorflow::error::UNKNOWN:
35-
TF_SetStatus(tf_status, TF_UNKNOWN, message);
36-
break;
37-
case tensorflow::error::INVALID_ARGUMENT:
38-
TF_SetStatus(tf_status, TF_INVALID_ARGUMENT, message);
39-
break;
40-
case tensorflow::error::DEADLINE_EXCEEDED:
41-
TF_SetStatus(tf_status, TF_DEADLINE_EXCEEDED, message);
42-
break;
43-
case tensorflow::error::NOT_FOUND:
44-
TF_SetStatus(tf_status, TF_NOT_FOUND, message);
45-
break;
46-
case tensorflow::error::ALREADY_EXISTS:
47-
TF_SetStatus(tf_status, TF_ALREADY_EXISTS, message);
48-
break;
49-
case tensorflow::error::PERMISSION_DENIED:
50-
TF_SetStatus(tf_status, TF_PERMISSION_DENIED, message);
51-
break;
52-
case tensorflow::error::UNAUTHENTICATED:
53-
TF_SetStatus(tf_status, TF_UNAUTHENTICATED, message);
54-
break;
55-
case tensorflow::error::RESOURCE_EXHAUSTED:
56-
TF_SetStatus(tf_status, TF_RESOURCE_EXHAUSTED, message);
57-
break;
58-
case tensorflow::error::FAILED_PRECONDITION:
59-
TF_SetStatus(tf_status, TF_FAILED_PRECONDITION, message);
60-
break;
61-
case tensorflow::error::ABORTED:
62-
TF_SetStatus(tf_status, TF_ABORTED, message);
63-
break;
64-
case tensorflow::error::OUT_OF_RANGE:
65-
TF_SetStatus(tf_status, TF_OUT_OF_RANGE, message);
66-
break;
67-
case tensorflow::error::UNIMPLEMENTED:
68-
TF_SetStatus(tf_status, TF_UNIMPLEMENTED, message);
69-
break;
70-
case tensorflow::error::INTERNAL:
71-
TF_SetStatus(tf_status, TF_INTERNAL, message);
72-
break;
73-
case tensorflow::error::UNAVAILABLE:
74-
TF_SetStatus(tf_status, TF_UNAVAILABLE, message);
75-
break;
76-
case tensorflow::error::DATA_LOSS:
77-
TF_SetStatus(tf_status, TF_DATA_LOSS, message);
78-
break;
79-
default:
80-
assert(0);
81-
break;
82-
}
83-
84-
errors::CopyPayloads(status, tf_status->status);
25+
Set_TSL_Status_from_Status(tf_status, status);
8526
}
8627

8728
Status StatusFromTF_Status(const TF_Status* tf_status) {
88-
return tf_status->status;
29+
return StatusFromTSL_Status(tf_status);
8930
}
9031

9132
} // namespace tsl

tensorflow/c/tf_status_helper.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ limitations under the License.
2121

2222
namespace tsl {
2323
// Set the attribute of "tf_status" from the attributes of "status".
24-
void Set_TF_Status_from_Status(TF_Status* tf_status, const tsl::Status& status);
24+
void Set_TF_Status_from_Status(TF_Status* tf_status, const Status& status);
2525

2626
// Returns a "status" from "tf_status".
27-
tensorflow::Status StatusFromTF_Status(const TF_Status* tf_status);
27+
Status StatusFromTF_Status(const TF_Status* tf_status);
2828
} // namespace tsl
2929

3030
namespace tensorflow {

tensorflow/c/tf_status_internal.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,8 @@ limitations under the License.
1616
#ifndef TENSORFLOW_C_TF_STATUS_INTERNAL_H_
1717
#define TENSORFLOW_C_TF_STATUS_INTERNAL_H_
1818

19-
#include "tensorflow/core/platform/status.h"
19+
#include "tensorflow/tsl/c/tsl_status_internal.h"
2020

21-
// Internal structures used by the status C API. These are likely to change
22-
// and should not be depended on.
23-
24-
struct TF_Status {
25-
tensorflow::Status status;
26-
};
21+
typedef struct TSL_Status TF_Status;
2722

2823
#endif // TENSORFLOW_C_TF_STATUS_INTERNAL_H_

tensorflow/compiler/xla/stream_executor/tpu/c_api_decl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ limitations under the License.
2323

2424
extern "C" {
2525

26-
struct TF_Status;
27-
typedef struct TF_Status TF_Status;
26+
struct TSL_Status;
27+
typedef TSL_Status TF_Status;
2828

2929
// Maximum number of array elements to inline into structs for performance.
3030
#define TPU_C_API_MAX_INLINED 6

0 commit comments

Comments
 (0)