cinn_launch_context_test.cc 5.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* Copyright (c) 2021 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 "paddle/fluid/operators/cinn/cinn_launch_context.h"
16 17 18
#include "cinn/hlir/framework/scope.h"
#include "cinn/hlir/framework/tensor.h"
#include "cinn/runtime/cinn_runtime.h"
19 20
#include "gtest/gtest.h"
#include "paddle/fluid/framework/scope.h"
21
#include "paddle/pten/core/ddim.h"
22 23

namespace paddle {
24
namespace operators::details {
25

26
using LoDTensor = framework::LoDTensor;
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
using CinnShape = ::cinn::hlir::framework::Shape;

std::unique_ptr<CinnLaunchContext> CreateDefaultLaunchContext() {
  static std::once_flag initialized;
  static std::unordered_map<std::string, std::string> paddle2cinn_varmap;
  static std::shared_ptr<CinnScope> cinn_scope;
  std::call_once(initialized, [&paddle2cinn_varmap, &cinn_scope]() {
    auto& scope = cinn_scope;
    scope = std::make_shared<CinnScope>();

    scope->Var<CinnTensor>("cinn_var1");
    scope->GetTensor("cinn_var1")->Resize(CinnShape({3, 4}));
    scope->Var<CinnTensor>("cinn_var2");
    scope->GetTensor("cinn_var2")->Resize(CinnShape({6, 7, 8}));
    scope->Var<CinnTensor>("cinn_var3");
    scope->GetTensor("cinn_var3")->Resize(CinnShape({10, 16}));

    paddle2cinn_varmap = {
        {"var1", "cinn_var1"}, {"var3", "cinn_var3"}, {"var4", "cinn_var4"}};
  });

  return std::make_unique<CinnLaunchContext>(paddle2cinn_varmap, cinn_scope);
}

51
TEST(CinnLaunchContextTest, TestBasic) {
52
  auto launch_context = CreateDefaultLaunchContext();
53
  // test IsVariableUsed
54 55
  ASSERT_EQ(launch_context->IsVariableUsed("var1"), true);
  ASSERT_EQ(launch_context->IsVariableUsed("var4"), false);
56 57 58 59 60 61
  // test UpdateCapturedEnv
  platform::CPUPlace place;
  framework::Scope scope;
  ASSERT_NO_THROW(launch_context->UpdateCapturedEnv(scope, place));
  // test IsArgumentsInitialized
  ASSERT_FALSE(launch_context->IsArgumentsInitialized());
62 63 64 65 66
}

TEST(CinnLaunchContextTest, TestCheckTensorEquivalent) {
  platform::CPUPlace place;
  framework::Scope scope;
67 68
  auto launch_context = CreateDefaultLaunchContext();
  launch_context->UpdateCapturedEnv(scope, place);
69 70 71
  auto* tensor1 = scope.Var("var1")->GetMutable<LoDTensor>();

  // CheckTensorEquivalent: tensor dimension not equivalent
72
  tensor1->mutable_data<float>(pten::make_ddim({3, 5}), place);
73
  ASSERT_THROW(launch_context->AssignExternalVariable("var1"),
74 75 76 77 78 79
               paddle::platform::EnforceNotMet);
}

TEST(CinnLaunchContextTest, TestAssignVariablePreCondition) {
  platform::CPUPlace place;
  framework::Scope scope;
80 81
  auto launch_context = CreateDefaultLaunchContext();
  launch_context->UpdateCapturedEnv(scope, place);
82 83 84
  auto* tensor4 = scope.Var("var4")->GetMutable<LoDTensor>();

  // not used
85
  ASSERT_THROW(launch_context->AssignExternalVariable("var4"),
86 87
               paddle::platform::EnforceNotMet);
  // not found
88 89
  ASSERT_THROW(launch_context->AssignInternalVariable("cinn_var4"),
               paddle::platform::EnforceNotMet);
90 91
}

92
TEST(CinnLaunchContextTest, TestAppendArgument) {
93 94 95
  platform::CPUPlace cpu_place;
  platform::Place place(cpu_place);
  framework::Scope scope;
96
  auto launch_context = CreateDefaultLaunchContext();
97
  launch_context->UpdateCapturedEnv(scope, place);
98

99
  // assign external variables
100
  auto* tensor1 = scope.Var("var1")->GetMutable<LoDTensor>();
101
  float* data1 = tensor1->mutable_data<float>(pten::make_ddim({3, 4}), place);
102 103
  data1[0] = 9.99f;
  data1[10] = 19.99f;
104
  ASSERT_NO_THROW(launch_context->AssignExternalVariable("var1"));
105 106

  auto* tensor3 = scope.Var("var3")->GetMutable<LoDTensor>();
107
  tensor3->mutable_data<float>(pten::make_ddim({10, 16}), place);
108 109 110 111 112 113
  ASSERT_NO_THROW(launch_context->AssignExternalVariable("var3"));

  // FinalizeArguments missed check
  ASSERT_THROW(launch_context->FinalizeArguments(),
               paddle::platform::EnforceNotMet);
  // test get internal variables
114 115
  auto internal_variable_names =
      launch_context->ExtractInternalVarNames({"var1"}, {"var3"});
116 117
  ASSERT_EQ(internal_variable_names.size(), 1);
  EXPECT_EQ(*internal_variable_names.begin(), "cinn_var2");
118

119
  auto* tensor2 = scope.Var("var2")->GetMutable<LoDTensor>();
120
  tensor2->mutable_data<float>(pten::make_ddim({6, 7, 8}), place);
121 122 123
  ASSERT_NO_THROW(launch_context->AssignInternalVariable("cinn_var2"));

  // check argument is set correctly and alloc/free callbacks work well
124 125 126
  auto name2argument = launch_context->FinalizeArguments();
  ASSERT_EQ(name2argument.size(), 3);
  ASSERT_EQ(name2argument.count("cinn_var1"), 1);
127 128
  ASSERT_TRUE(launch_context->IsArgumentsInitialized());

129 130 131 132 133 134 135 136 137 138 139
  auto* cinn_buffer =
      static_cast<cinn_buffer_t*>(name2argument.at("cinn_var1"));
  ASSERT_EQ(cinn_buffer->memory, nullptr);
  cinn_buffer->external_malloc->operator()(nullptr, cinn_buffer);
  ASSERT_NE(cinn_buffer->memory, nullptr);
  ASSERT_EQ(cinn_buffer->num_elements(), 12);
  auto* shadow_data = reinterpret_cast<float*>(cinn_buffer->memory);
  EXPECT_FLOAT_EQ(shadow_data[0], 9.99f);
  EXPECT_FLOAT_EQ(shadow_data[10], 19.99f);
}

140
}  // namespace operators::details
141
}  // namespace paddle