new_ir_compiler_test.cc 5.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <glog/logging.h>
#include <gtest/gtest.h>
17
#include <memory>
18
#include <sstream>
19 20
#include <string>
#include <unordered_map>
21

22 23
#include "paddle/fluid/ir/dialect/paddle_dialect/ir/pd_dialect.h"
#include "paddle/fluid/ir/dialect/paddle_dialect/ir/pd_op.h"
24 25 26
#include "paddle/ir/core/ir_context.h"
#include "paddle/ir/core/program.h"

27
#include "paddle/cinn/utils/data_util.h"
28

29 30 31
#include "paddle/cinn/hlir/dialect/jit_kernel_op.h"
#include "paddle/cinn/hlir/dialect/runtime_dialect.h"
#include "paddle/cinn/hlir/framework/convert_to_dialect.h"
32 33
#include "paddle/cinn/hlir/framework/new_ir_compiler.h"

34
std::unique_ptr<::ir::Program> BuildProgram() {
35 36
  ::ir::IrContext* ctx = ::ir::IrContext::Instance();
  ctx->GetOrRegisterDialect<paddle::dialect::PaddleDialect>();
37 38
  auto program = std::make_unique<::ir::Program>(ctx);
  ::ir::Builder builder = ::ir::Builder(ctx, program->block());
39

40 41
  const float value_one = 1.0;  // relu(tan(1.)) = 1.5;
  const float value_two = 2.0;  // relu(tan(2.)) = 0.
42 43
  auto full_op_x =
      builder.Build<paddle::dialect::FullOp>(std::vector<int64_t>{64, 128},
44
                                             value_one,
45
                                             phi::DataType::FLOAT32,
46
                                             phi::GPUPlace());
47 48

  auto full_op_y =
49 50
      builder.Build<paddle::dialect::FullOp>(std::vector<int64_t>{64, 128},
                                             value_two,
51
                                             phi::DataType::FLOAT32,
52
                                             phi::GPUPlace());
53 54 55 56 57 58

  auto tanh_op_x = builder.Build<paddle::dialect::TanOp>(full_op_x->result(0));
  auto relu_op_x = builder.Build<paddle::dialect::ReluOp>(tanh_op_x->result(0));
  auto tanh_op_y = builder.Build<paddle::dialect::TanOp>(full_op_y->result(0));
  auto relu_op_y = builder.Build<paddle::dialect::ReluOp>(tanh_op_y->result(0));

59 60
  return std::move(program);
}
61

62 63 64
TEST(NewIRCompier, CompilerAndRun) {
  // Step 1: Construct ir::Program
  std::unique_ptr<::ir::Program> program = BuildProgram();
65 66
  EXPECT_EQ(program->block()->size(), 6u);
  LOG(INFO) << program->block()->size();
67 68

  std::stringstream ss;
69
  program->Print(ss);
70 71
  LOG(INFO) << ss.str();

72
  // Step 2: Compiler New ir::Program into Runtime Program
73
  auto target = cinn::common::DefaultNVGPUTarget();
74
  auto scope = cinn::hlir::framework::BuildScope(target, *program);
75
  ASSERT_EQ(scope->var_names().size(), 6);
76

77
  cinn::hlir::framework::NewIRCompiler ir_compiler(*program, target, scope);
78 79
  auto runtime_program = ir_compiler.Build();

80
  // Step 3: Execute Runtime Instruction and check Scope.
81
  ASSERT_NO_THROW(runtime_program->Execute());
82 83 84 85
  for (auto& var_name : scope->var_names()) {
    std::string name = {var_name.begin(), var_name.end()};
    std::vector<float> data =
        cinn::GetTensorData<float>(scope->GetTensor(name), target);
86 87
    for (int i = 0; i < 1; ++i) {
      LOG_FIRST_N(INFO, 10) << "data: " << data[i];
88 89 90 91 92 93 94
    }
  }
}

TEST(RuntimeDialect, CompilerAndRun) {
  // Step 1: Construct ir::Program
  std::unique_ptr<::ir::Program> program = BuildProgram();
95
  EXPECT_EQ(program->block()->size(), 6u);
96 97 98 99

  // Step 2: Compiler New ir::Program into Runtime Program
  auto target = cinn::common::DefaultNVGPUTarget();
  auto scope = cinn::hlir::framework::BuildScope(target, *program);
100
  ASSERT_EQ(scope->var_names().size(), 6u);
101

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
  cinn::hlir::framework::NewIRCompiler ir_compiler(*program, target, scope);
  auto runtime_program = ir_compiler.Build();

  // Step 3: Convert into cinn::dialect::RuntimeDialect
  std::unique_ptr<::ir::Program> ir_runtime_program =
      cinn::hlir::framework::ConvertToRuntimeDialect(*runtime_program);

  // Step 4: Run cinn::dialect::RuntimeDialect
  for (auto iter = ir_runtime_program->block()->begin();
       iter != ir_runtime_program->block()->end();
       ++iter) {
    auto op = (*iter)->dyn_cast<cinn::dialect::JitKernelOp>();
    auto* instr = op.instruction();
    instr->Run(/*name2podargs=*/nullptr,
               false,
               /*stream=*/nullptr,
               /*use_cache=*/true);
  }
#ifdef CINN_WITH_CUDA
  CUDA_CALL(cudaDeviceSynchronize());
#endif

  // Step 5: Check Scope Tensor Value.
125 126 127 128
  for (auto& var_name : scope->var_names()) {
    std::string name = {var_name.begin(), var_name.end()};
    std::vector<float> data =
        cinn::GetTensorData<float>(scope->GetTensor(name), target);
129 130
    for (int i = 0; i < 1; ++i) {
      LOG_FIRST_N(INFO, 10) << "data: " << data[i];
131 132
    }
  }
133
}