1 /* Copyright 2022 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/service/gpu/gpu_hlo_cost_analysis.h"
17
18 #include "tensorflow/compiler/xla/tests/hlo_test_base.h"
19
20 namespace xla {
21 namespace gpu {
22
23 constexpr int64_t kPointerSize = 8;
24
ShapeSize(const Shape & shape)25 int64_t ShapeSize(const Shape& shape) {
26 return ShapeUtil::ByteSizeOf(shape, kPointerSize);
27 }
28
29 using GpuHloCostAnalysisTest = HloTestBase;
30
TEST_F(GpuHloCostAnalysisTest,ConvCustomCall)31 TEST_F(GpuHloCostAnalysisTest, ConvCustomCall) {
32 absl::string_view hlo_string = R"(
33 HloModule module, is_scheduled=true
34
35 ENTRY entry {
36 p0 = s8[128,12,24,24,4]{4,3,2,1,0} parameter(0)
37 p1 = s8[16,12,5,5,4]{4,3,2,1,0} parameter(1)
38 p2 = f32[16]{0} parameter(2)
39 conv1 = (s8[128,4,24,24,4]{4,3,2,1,0}, u8[0]{0}) custom-call(p0, p1, p2),
40 window={size=5x5 pad=2_2x2_2},
41 dim_labels=bf01_oi01->bf01,
42 custom_call_target="__cudnn$convBiasActivationForward"
43 ROOT tuple = tuple(conv1)
44 }
45 )";
46 TF_ASSERT_OK_AND_ASSIGN(auto module,
47 ParseAndReturnVerifiedModule(hlo_string));
48 HloCostAnalysis::Options options{ShapeSize};
49 GpuHloCostAnalysis analysis(options);
50 ASSERT_IS_OK(
51 module->entry_computation()->root_instruction()->Accept(&analysis));
52
53 HloComputation* comp = module->entry_computation();
54 const HloInstruction* conv1 = comp->GetInstructionWithName("conv1");
55 EXPECT_EQ(analysis.operand_bytes_accessed(*conv1, 0),
56 sizeof(int8_t) * 128 * 12 * 24 * 24 * 4);
57 EXPECT_EQ(analysis.operand_bytes_accessed(*conv1, 1),
58 sizeof(int8_t) * 16 * 12 * 5 * 5 * 4);
59 EXPECT_EQ(analysis.output_bytes_accessed(*conv1),
60 sizeof(int8_t) * 128 * 4 * 24 * 24 * 4);
61 EXPECT_EQ(analysis.flop_count(*conv1), 159694848);
62 }
63
64 } // namespace gpu
65 } // namespace xla
66