|
| 1 | +// Copyright (c) 2019 PaddlePaddle 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 | +#include <string> |
| 16 | +#include "glog/logging.h" |
| 17 | +#include "gtest/gtest.h" |
| 18 | +#include "paddle/fluid/framework/op_registry.h" |
| 19 | +#include "paddle/fluid/framework/operator.h" |
| 20 | + |
| 21 | +USE_OP(elementwise_add_grad); |
| 22 | + |
| 23 | +namespace paddle { |
| 24 | +namespace operators { |
| 25 | + |
| 26 | +TEST(op_debug_str, test_unknown_dtype) { |
| 27 | + platform::Place place = platform::CPUPlace(); |
| 28 | + framework::DDim dim{3, 4, 5, 6}; |
| 29 | + const std::string unknown_dtype = "unknown_dtype"; |
| 30 | + |
| 31 | + framework::OpDesc desc; |
| 32 | + framework::Scope scope; |
| 33 | + |
| 34 | + desc.SetType("elementwise_add_grad"); |
| 35 | + desc.SetInput("Y", {"Y"}); |
| 36 | + desc.SetInput(framework::GradVarName("Out"), {framework::GradVarName("Out")}); |
| 37 | + desc.SetOutput(framework::GradVarName("X"), {framework::GradVarName("X")}); |
| 38 | + desc.SetOutput(framework::GradVarName("Y"), {framework::GradVarName("Y")}); |
| 39 | + desc.SetAttr("axis", -1); |
| 40 | + desc.SetAttr("use_mkldnn", false); |
| 41 | + desc.SetAttr("x_data_format", ""); |
| 42 | + desc.SetAttr("y_data_format", ""); |
| 43 | + |
| 44 | + auto y_tensor = scope.Var("Y")->GetMutable<framework::LoDTensor>(); |
| 45 | + y_tensor->Resize(dim); |
| 46 | + y_tensor->mutable_data<float>(place); |
| 47 | + |
| 48 | + auto out_grad_tensor = scope.Var(framework::GradVarName("Out")) |
| 49 | + ->GetMutable<framework::LoDTensor>(); |
| 50 | + out_grad_tensor->Resize(dim); |
| 51 | + out_grad_tensor->mutable_data<float>(place); |
| 52 | + |
| 53 | + scope.Var(framework::GradVarName("X"))->GetMutable<framework::LoDTensor>(); |
| 54 | + scope.Var(framework::GradVarName("Y"))->GetMutable<framework::LoDTensor>(); |
| 55 | + |
| 56 | + auto op = framework::OpRegistry::CreateOp(desc); |
| 57 | + |
| 58 | + auto before_run_str = op->DebugStringEx(&scope); |
| 59 | + LOG(INFO) << before_run_str; |
| 60 | + ASSERT_TRUE(before_run_str.find(unknown_dtype) != std::string::npos); |
| 61 | + |
| 62 | + op->Run(scope, place); |
| 63 | + platform::DeviceContextPool::Instance().Get(place)->Wait(); |
| 64 | + |
| 65 | + auto after_run_str = op->DebugStringEx(&scope); |
| 66 | + LOG(INFO) << after_run_str; |
| 67 | + ASSERT_TRUE(after_run_str.find(unknown_dtype) != std::string::npos); |
| 68 | +} |
| 69 | + |
| 70 | +} // namespace operators |
| 71 | +} // namespace paddle |
0 commit comments