Home | History | Annotate | Download | only in tests
      1 /* Copyright 2017 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 
     16 #include "tensorflow/compiler/xla/executable_run_options.h"
     17 #include "tensorflow/core/platform/dynamic_annotations.h"
     18 #include "tensorflow/core/platform/test.h"
     19 
     20 class LocalClientAotTest : public ::testing::Test {};
     21 
     22 // This is a compiled XLA computation which calls SumStructElements, and then
     23 // doubles the result.
     24 extern "C" void SumAndDouble(float* out, xla::ExecutableRunOptions* options,
     25                              void** parameters, void** temporary_buffers);
     26 
     27 // Just some structs with some arbitrary fields used to test the OPAQUE type.
     28 struct OpaqueData {
     29   int field1 : 15;
     30   int field2 : 14;
     31   int field3 : 3;
     32 };
     33 
     34 // This is the implementation of a custom op which will be called by
     35 // SumAndDouble.
     36 extern "C" void SumStructElements(float* out, void** parameters) {
     37   TF_ANNOTATE_MEMORY_IS_INITIALIZED(parameters, sizeof(OpaqueData*));
     38   const auto* opaque_data = static_cast<OpaqueData*>(parameters[0]);
     39   *out = opaque_data->field1 + opaque_data->field2 + opaque_data->field3;
     40 }
     41 
     42 TEST_F(LocalClientAotTest, Constant) {
     43   xla::ExecutableRunOptions run_options;
     44   OpaqueData opaque_data{100, 20, 3};
     45   void* parameters[] = {&opaque_data};
     46   float out = 0;
     47   void* temporary_buffers[] = {nullptr, &out};
     48   SumAndDouble(&out, &run_options, parameters, temporary_buffers);
     49   EXPECT_EQ(out, 246.0f);
     50 
     51   opaque_data = {1, 2, 3};
     52   SumAndDouble(&out, &run_options, parameters, temporary_buffers);
     53   EXPECT_EQ(out, 12.0f);
     54 }
     55