1 /* Copyright 2020 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 #ifndef TENSORFLOW_C_EAGER_UNIFIED_API_TESTUTIL_H_ 16 #define TENSORFLOW_C_EAGER_UNIFIED_API_TESTUTIL_H_ 17 18 #include "tensorflow/c/eager/abstract_context.h" 19 #include "tensorflow/c/eager/abstract_tensor_handle.h" 20 #include "tensorflow/c/tf_tensor.h" 21 #include "tensorflow/core/platform/status.h" 22 23 namespace tensorflow { 24 25 // Builds and returns a `TracingContext` using the default tracing impl. 26 AbstractContext* BuildFunction(const char* fn_name); 27 28 // Creates parameters (placeholders) in the tracing `ctx` using the shape and 29 // dtype of `inputs`. 30 Status CreateParamsForInputs(AbstractContext* ctx, 31 absl::Span<AbstractTensorHandle* const> inputs, 32 std::vector<AbstractTensorHandle*>* params); 33 34 // A callable that takes tensor inputs and returns zero or more tensor outputs. 35 using Model = std::function<Status(AbstractContext*, 36 absl::Span<AbstractTensorHandle* const>, 37 absl::Span<AbstractTensorHandle*>)>; 38 39 // Runs `model` maybe wrapped in a function call op. This can be thought as 40 // being equivalent to the following python code. 41 // 42 // if use_function: 43 // outputs = tf.function(model)(inputs) 44 // else: 45 // outputs = model(inputs) 46 Status RunModel(Model model, AbstractContext* ctx, 47 absl::Span<AbstractTensorHandle* const> inputs, 48 absl::Span<AbstractTensorHandle*> outputs, bool use_function); 49 50 Status BuildImmediateExecutionContext(bool use_tfrt, AbstractContext** ctx); 51 52 // Get a Scalar TensorHandle with given float value. 53 Status TestScalarTensorHandle(AbstractContext* ctx, float value, 54 AbstractTensorHandle** tensor); 55 56 // Get a Matrix TensorHandle with given float values and dimensions. 57 Status TestTensorHandleWithDimsFloat(AbstractContext* ctx, float* data, 58 int64_t* dims, int num_dims, 59 AbstractTensorHandle** tensor); 60 61 // Get a TensorHandle with given int values and dimensions 62 Status TestTensorHandleWithDimsInt(AbstractContext* ctx, int* data, 63 int64_t* dims, int num_dims, 64 AbstractTensorHandle** tensor); 65 66 // Places data from `t` into *result_tensor. 67 Status GetValue(AbstractTensorHandle* t, TF_Tensor** result_tensor); 68 } // namespace tensorflow 69 70 #endif // TENSORFLOW_C_EAGER_UNIFIED_API_TESTUTIL_H_ 71