1 /* Copyright 2018 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 <memory>
17 #include <utility>
18
19 #include "tensorflow/compiler/xla/array2d.h"
20 #include "tensorflow/compiler/xla/service/cpu/tests/cpu_codegen_test.h"
21 #include "tensorflow/compiler/xla/service/hlo_computation.h"
22 #include "tensorflow/compiler/xla/service/hlo_instruction.h"
23 #include "tensorflow/compiler/xla/service/hlo_module.h"
24 #include "tensorflow/compiler/xla/shape_util.h"
25 #include "tensorflow/compiler/xla/tests/filecheck.h"
26 #include "tensorflow/core/platform/test.h"
27
28 namespace xla {
29 namespace cpu {
30 namespace {
31 class CpuExternalConstantsTest : public CpuCodegenTest {
32 public:
TestWithArray(int64_t rows,int64_t cols,const char * filecheck_pattern)33 void TestWithArray(int64_t rows, int64_t cols,
34 const char* filecheck_pattern) {
35 HloComputation::Builder builder(TestName());
36
37 Array2D<float> backing_array(rows, cols);
38 backing_array.FillUnique();
39
40 auto shape = ShapeUtil::MakeShape(F32, {rows, cols});
41
42 HloInstruction* constant =
43 builder.AddInstruction(HloInstruction::CreateConstant(
44 LiteralUtil::CreateR2FromArray2D(backing_array)));
45 HloInstruction* param =
46 builder.AddInstruction(HloInstruction::CreateParameter(0, shape, "x"));
47 builder.AddInstruction(
48 HloInstruction::CreateBinary(shape, HloOpcode::kAdd, param, constant));
49
50 std::unique_ptr<HloModule> module = CreateNewVerifiedModule();
51 module->AddEntryComputation(builder.Build());
52
53 CompileAndVerifyIr(std::move(module), filecheck_pattern,
54 /*match_optimized_ir=*/false);
55 }
56 };
57
TEST_F(CpuExternalConstantsTest,Basic)58 TEST_F(CpuExternalConstantsTest, Basic) {
59 TestWithArray(/*rows=*/1024, /*cols=*/1024, R"(
60 CHECK-NOT: @constant_global_0 = external unnamed_addr constant [1024 x [1024 x float]], align 16
61 CHECK: @constant = private unnamed_addr constant [4194304 x i8] {{.*}}, align 16
62 )");
63 }
64
TEST_F(CpuExternalConstantsTest,BasicNegative)65 TEST_F(CpuExternalConstantsTest, BasicNegative) {
66 // The constant array in this test case is small enough that there is no need
67 // to externalize it.
68 TestWithArray(/*rows=*/4, /*cols=*/4, R"(
69 CHECK-NOT: @constant_global_0 = external unnamed_addr constant [16 x float]
70 CHECK: @constant = private unnamed_addr constant [64 x i8] {{.*}}, align 16
71 )");
72 }
73 } // namespace
74 } // namespace cpu
75 } // namespace xla
76