* Copyright (c) 2025 Huawei Technologies Co., Ltd.
* This program is free software, you can redistribute it and/or modify it under the terms and conditions of
* CANN Open Software License Agreement Version 2.0 (the "License").
* Please refer to the License for details. You may not use this file except in compliance with the License.
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
* See LICENSE in the root of the software repository for the full text of the License.
*/
#include <gtest/gtest.h>
#include <iostream>
#include "infershape_context_faker.h"
#include "base/registry/op_impl_space_registry_v2.h"
class Maximum : public testing::Test {
protected:
static void SetUpTestCase() {
std::cout << "Maximum SetUp" << std::endl;
}
static void TearDownTestCase() {
std::cout << "Maximum TearDown" << std::endl;
}
};
static std::vector<int64_t> ToVector(const gert::Shape& shape)
{
size_t shapeSize = shape.GetDimNum();
std::vector<int64_t> shapeVec(shapeSize, 0);
for (size_t i = 0; i < shapeSize; i++) {
shapeVec[i] = shape.GetDim(i);
}
return shapeVec;
}
static void ExeTestCase(
std::vector<std::vector<int64_t> > expectResults,
const std::vector<gert::StorageShape>& inputShapes,
const std::vector<ge::DataType>& dtypes,
gert::StorageShape& outStorageShape,
ge::graphStatus testCaseResult = ge::GRAPH_SUCCESS)
{
const auto& x1StorageShape = inputShapes[0];
const auto& x2StorageShape = inputShapes[1];
ge::DataType input1Dtype = dtypes[0];
ge::DataType outputDtype = dtypes[1];
std::vector<gert::Tensor *> inputTensors = {
(gert::Tensor *)&x1StorageShape,
(gert::Tensor *)&x2StorageShape
};
std::vector<gert::StorageShape *> outputShapes = {&outStorageShape};
auto contextHolder = gert::InferShapeContextFaker()
.SetOpType("Maximum")
.NodeIoNum(2, 1)
.NodeInputTd(0, input1Dtype, ge::FORMAT_ND, ge::FORMAT_ND)
.NodeInputTd(1, input1Dtype, ge::FORMAT_ND, ge::FORMAT_ND)
.NodeOutputTd(0, outputDtype, ge::FORMAT_ND, ge::FORMAT_ND)
.InputTensors(inputTensors)
.OutputShapes(outputShapes)
.Build();
auto spaceRegistry = gert::DefaultOpImplSpaceRegistryV2::GetInstance().GetSpaceRegistry();
auto inferShapeFunc = spaceRegistry->GetOpImpl("Maximum")->infer_shape;
ASSERT_NE(inferShapeFunc, nullptr);
EXPECT_EQ(inferShapeFunc(contextHolder.GetContext()), testCaseResult);
for (size_t i = 0; i < expectResults.size(); i++) {
EXPECT_EQ(ToVector(*contextHolder.GetContext()->GetOutputShape(i)), expectResults[i]);
}
}
TEST_F(Maximum, maximum_infershape_case_0)
{
std::vector<gert::StorageShape> inputShapes = {
{{8, 8}, {8, 8}}, {{8, 8}, {8, 8}}
};
std::vector<ge::DataType> dtypes = {
ge::DT_FLOAT16,
ge::DT_FLOAT16
};
std::vector<int64_t> expectResult = {8, 8};
gert::StorageShape outStorageShape = {};
ExeTestCase({expectResult}, inputShapes, dtypes, outStorageShape, ge::GRAPH_SUCCESS);
}