1/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15
16#ifndef TENSORFLOW_CORE_FRAMEWORK_OP_REQUIRES_H_
17#define TENSORFLOW_CORE_FRAMEWORK_OP_REQUIRES_H_
18
19#include "tensorflow/core/platform/macros.h"
20
21namespace tensorflow {
22
23// Convenience macros for asserting and handling exceptional conditions.
24// Analogous to the CHECK* macros provided by logging.h.
25//
26// Example use:
27// void Compute(OperationContext* context) {
28// OP_REQUIRES(context, context->num_inputs() == 2,
29// errors::InvalidArgument("FooOp requires 2 arguments"));
30// ...
31// Status status = SomeUncertainMethod();
32// OP_REQUIRES_OK(context, status);
33// ...
34// }
35//
36// These macros depend on CheckNotInComputeAsync, which must be defined before
37// invoking the macro. We specifically don't include op_kernel.h from this
38// header to reduce this header's dependencies. These macros may be used with
39// alternative implementations of OpKernelContext with fewer dependencies.
40
41#define OP_REQUIRES(CTX, EXP, STATUS) \
42 do { \
43 if (!TF_PREDICT_TRUE(EXP)) { \
44 CheckNotInComputeAsync((CTX), "OP_REQUIRES_ASYNC"); \
45 (CTX)->CtxFailure(__FILE__, __LINE__, (STATUS)); \
46 return; \
47 } \
48 } while (0)
49
50#define OP_REQUIRES_OK(CTX, ...) \
51 do { \
52 ::tensorflow::Status _s(__VA_ARGS__); \
53 if (!TF_PREDICT_TRUE(_s.ok())) { \
54 CheckNotInComputeAsync((CTX), "OP_REQUIRES_OK_ASYNC"); \
55 (CTX)->CtxFailureWithWarning(__FILE__, __LINE__, _s); \
56 return; \
57 } \
58 } while (0)
59
60#define OP_REQUIRES_OK_OR_SET_PAYLOAD(CTX, PAYLOAD_KEY, PAYLOAD_VALUE, STATUS) \
61 do { \
62 if (!TF_PREDICT_TRUE(STATUS.ok())) { \
63 CheckNotInComputeAsync((CTX), "OP_REQUIRES_OK_ASYNC"); \
64 if (!PAYLOAD_VALUE.empty()) { \
65 STATUS.SetPayload(PAYLOAD_KEY, PAYLOAD_VALUE); \
66 } \
67 (CTX)->CtxFailureWithWarning(__FILE__, __LINE__, STATUS); \
68 return; \
69 } \
70 } while (0)
71
72#define OP_REQUIRES_ASYNC(CTX, EXP, STATUS, CALLBACK) \
73 do { \
74 if (!TF_PREDICT_TRUE(EXP)) { \
75 (CTX)->CtxFailure(__FILE__, __LINE__, (STATUS)); \
76 (CALLBACK)(); \
77 return; \
78 } \
79 } while (0)
80
81#define OP_REQUIRES_OK_ASYNC(CTX, STATUS, CALLBACK) \
82 do { \
83 const ::tensorflow::Status& _s(STATUS); \
84 if (!TF_PREDICT_TRUE(_s.ok())) { \
85 (CTX)->CtxFailureWithWarning(__FILE__, __LINE__, _s); \
86 (CALLBACK)(); \
87 return; \
88 } \
89 } while (0)
90
91#define OP_REQUIRES_VALUE(lhs, ctx, rexpr) \
92 OP_REQUIRES_VALUE_IMPL( \
93 TF_STATUS_MACROS_CONCAT_NAME(_status_or_value, __COUNTER__), lhs, ctx, \
94 rexpr)
95
96#define OP_REQUIRES_VALUE_IMPL(statusor, lhs, ctx, rexpr) \
97 auto statusor = (rexpr); \
98 OP_REQUIRES_OK(ctx, statusor.status()); \
99 lhs = std::move(statusor.value())
100
101} // namespace tensorflow
102
103#endif // TENSORFLOW_CORE_FRAMEWORK_OP_REQUIRES_H_
104