// // Copyright © 2017 Arm Ltd and Contributors. All rights reserved. // SPDX-License-Identifier: MIT // #include "GatherTestImpl.hpp" #include #include #include #include namespace { template , size_t ParamsDim, size_t IndicesDim, size_t OutputDim> LayerTestResult GatherTestImpl( armnn::IWorkloadFactory& workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, const armnn::ITensorHandleFactory& tensorHandleFactory, const armnn::TensorInfo& paramsInfo, const armnn::TensorInfo& indicesInfo, const armnn::TensorInfo& outputInfo, const std::vector& paramsData, const std::vector& indicesData, const std::vector& outputData) { IgnoreUnused(memoryManager); std::vector actualOutput(outputInfo.GetNumElements()); std::unique_ptr paramsHandle = tensorHandleFactory.CreateTensorHandle(paramsInfo); std::unique_ptr indicesHandle = tensorHandleFactory.CreateTensorHandle(indicesInfo); std::unique_ptr outputHandle = tensorHandleFactory.CreateTensorHandle(outputInfo); armnn::GatherQueueDescriptor data; armnn::WorkloadInfo info; AddInputToWorkload(data, info, paramsInfo, paramsHandle.get()); AddInputToWorkload(data, info, indicesInfo, indicesHandle.get()); AddOutputToWorkload(data, info, outputInfo, outputHandle.get()); std::unique_ptr workload = workloadFactory.CreateWorkload(armnn::LayerType::Gather, data, info); paramsHandle->Allocate(); indicesHandle->Allocate(); outputHandle->Allocate(); CopyDataToITensorHandle(paramsHandle.get(), paramsData.data()); CopyDataToITensorHandle(indicesHandle.get(), indicesData.data()); workload->Execute(); CopyDataFromITensorHandle(actualOutput.data(), outputHandle.get()); return LayerTestResult(actualOutput, outputData, outputHandle->GetShape(), outputInfo.GetShape()); } template> struct GatherTestHelper { static LayerTestResult Gather1dParamsTestImpl( armnn::IWorkloadFactory& workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, const armnn::ITensorHandleFactory& tensorHandleFactory) { armnn::TensorInfo paramsInfo({ 8 }, ArmnnType); armnn::TensorInfo indicesInfo({ 4 }, armnn::DataType::Signed32); armnn::TensorInfo outputInfo({ 4 }, ArmnnType); if (armnn::IsQuantizedType()) { paramsInfo.SetQuantizationScale(1.0f); paramsInfo.SetQuantizationOffset(1); outputInfo.SetQuantizationScale(1.0f); outputInfo.SetQuantizationOffset(1); } const std::vector params = std::vector({ 1, 2, 3, 4, 5, 6, 7, 8 }); const std::vector indices = std::vector({ 0, 2, 1, 5 }); const std::vector expectedOutput = std::vector({ 1, 3, 2, 6 }); return GatherTestImpl( workloadFactory, memoryManager, tensorHandleFactory, paramsInfo, indicesInfo, outputInfo, params, indices, expectedOutput); } static LayerTestResult GatherMultiDimParamsTestImpl( armnn::IWorkloadFactory& workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, const armnn::ITensorHandleFactory& tensorHandleFactory) { armnn::TensorInfo paramsInfo({ 5, 2 }, ArmnnType); armnn::TensorInfo indicesInfo({ 3 }, armnn::DataType::Signed32); armnn::TensorInfo outputInfo({ 3, 2 }, ArmnnType); if (armnn::IsQuantizedType()) { paramsInfo.SetQuantizationScale(1.0f); paramsInfo.SetQuantizationOffset(1); outputInfo.SetQuantizationScale(1.0f); outputInfo.SetQuantizationOffset(1); } const std::vector params = std::vector({ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }); const std::vector indices = std::vector({ 1, 3, 4 }); const std::vector expectedOutput = std::vector({ 3, 4, 7, 8, 9, 10 }); return GatherTestImpl( workloadFactory, memoryManager, tensorHandleFactory, paramsInfo, indicesInfo, outputInfo, params, indices, expectedOutput); } static LayerTestResult GatherMultiDimParamsMultiDimIndicesTestImpl( armnn::IWorkloadFactory& workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, const armnn::ITensorHandleFactory& tensorHandleFactory) { armnn::TensorInfo paramsInfo({ 3, 2, 3}, ArmnnType); armnn::TensorInfo indicesInfo({ 2, 3 }, armnn::DataType::Signed32); armnn::TensorInfo outputInfo({ 2, 3, 2, 3 }, ArmnnType); if (armnn::IsQuantizedType()) { paramsInfo.SetQuantizationScale(1.0f); paramsInfo.SetQuantizationOffset(1); outputInfo.SetQuantizationScale(1.0f); outputInfo.SetQuantizationOffset(1); } const std::vector params = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18 }; const std::vector indices = { 1, 2, 1, 2, 1, 0 }; const std::vector expectedOutput = { 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6 }; return GatherTestImpl( workloadFactory, memoryManager, tensorHandleFactory, paramsInfo, indicesInfo, outputInfo, params, indices, expectedOutput); } }; template struct GatherTestHelper { static LayerTestResult Gather1dParamsTestImpl( armnn::IWorkloadFactory& workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, const armnn::ITensorHandleFactory& tensorHandleFactory) { using namespace half_float::literal; armnn::TensorInfo paramsInfo({ 8 }, armnn::DataType::Float16); armnn::TensorInfo indicesInfo({ 4 }, armnn::DataType::Signed32); armnn::TensorInfo outputInfo({ 4 }, armnn::DataType::Float16); const std::vector params = std::vector({ 1._h, 2._h, 3._h, 4._h, 5._h, 6._h, 7._h, 8._h }); const std::vector indices = std::vector({ 0, 2, 1, 5 }); const std::vector expectedOutput = std::vector({ 1._h, 3._h, 2._h, 6._h }); return GatherTestImpl( workloadFactory, memoryManager, tensorHandleFactory, paramsInfo, indicesInfo, outputInfo, params, indices, expectedOutput); } static LayerTestResult GatherMultiDimParamsTestImpl( armnn::IWorkloadFactory& workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, const armnn::ITensorHandleFactory& tensorHandleFactory) { using namespace half_float::literal; armnn::TensorInfo paramsInfo({ 5, 2 }, armnn::DataType::Float16); armnn::TensorInfo indicesInfo({ 3 }, armnn::DataType::Signed32); armnn::TensorInfo outputInfo({ 3, 2 }, armnn::DataType::Float16); const std::vector params = std::vector({ 1._h, 2._h, 3._h, 4._h, 5._h, 6._h, 7._h, 8._h, 9._h, 10._h }); const std::vector indices = std::vector({ 1, 3, 4 }); const std::vector expectedOutput = std::vector({ 3._h, 4._h, 7._h, 8._h, 9._h, 10._h }); return GatherTestImpl( workloadFactory, memoryManager, tensorHandleFactory, paramsInfo, indicesInfo, outputInfo, params, indices, expectedOutput); } static LayerTestResult GatherMultiDimParamsMultiDimIndicesTestImpl( armnn::IWorkloadFactory& workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, const armnn::ITensorHandleFactory& tensorHandleFactory) { using namespace half_float::literal; armnn::TensorInfo paramsInfo({ 3, 2, 3 }, armnn::DataType::Float16); armnn::TensorInfo indicesInfo({ 2, 3 }, armnn::DataType::Signed32); armnn::TensorInfo outputInfo({ 2, 3, 2, 3 }, armnn::DataType::Float16); const std::vector params = { 1._h, 2._h, 3._h, 4._h, 5._h, 6._h, 7._h, 8._h, 9._h, 10._h, 11._h, 12._h, 13._h, 14._h, 15._h, 16._h, 17._h, 18._h }; const std::vector indices = { 1, 2, 1, 2, 1, 0 }; const std::vector expectedOutput = { 7._h, 8._h, 9._h, 10._h, 11._h, 12._h, 13._h, 14._h, 15._h, 16._h, 17._h, 18._h, 7._h, 8._h, 9._h, 10._h, 11._h, 12._h, 13._h, 14._h, 15._h, 16._h, 17._h, 18._h, 7._h, 8._h, 9._h, 10._h, 11._h, 12._h, 1._h, 2._h, 3._h, 4._h, 5._h, 6._h }; return GatherTestImpl( workloadFactory, memoryManager, tensorHandleFactory, paramsInfo, indicesInfo, outputInfo, params, indices, expectedOutput); } }; } // anonymous namespace LayerTestResult Gather1dParamsFloat32Test( armnn::IWorkloadFactory& workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, const armnn::ITensorHandleFactory& tensorHandleFactory) { return GatherTestHelper::Gather1dParamsTestImpl( workloadFactory, memoryManager, tensorHandleFactory); } LayerTestResult Gather1dParamsFloat16Test( armnn::IWorkloadFactory& workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, const armnn::ITensorHandleFactory& tensorHandleFactory) { return GatherTestHelper::Gather1dParamsTestImpl( workloadFactory, memoryManager, tensorHandleFactory); } LayerTestResult Gather1dParamsUint8Test( armnn::IWorkloadFactory& workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, const armnn::ITensorHandleFactory& tensorHandleFactory) { return GatherTestHelper::Gather1dParamsTestImpl( workloadFactory, memoryManager, tensorHandleFactory); } LayerTestResult Gather1dParamsInt16Test( armnn::IWorkloadFactory& workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, const armnn::ITensorHandleFactory& tensorHandleFactory) { return GatherTestHelper::Gather1dParamsTestImpl( workloadFactory, memoryManager, tensorHandleFactory); } LayerTestResult Gather1dParamsInt32Test( armnn::IWorkloadFactory& workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, const armnn::ITensorHandleFactory& tensorHandleFactory) { return GatherTestHelper::Gather1dParamsTestImpl( workloadFactory, memoryManager, tensorHandleFactory); } LayerTestResult GatherMultiDimParamsFloat32Test( armnn::IWorkloadFactory& workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, const armnn::ITensorHandleFactory& tensorHandleFactory) { return GatherTestHelper::GatherMultiDimParamsTestImpl( workloadFactory, memoryManager, tensorHandleFactory); } LayerTestResult GatherMultiDimParamsFloat16Test( armnn::IWorkloadFactory& workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, const armnn::ITensorHandleFactory& tensorHandleFactory) { return GatherTestHelper::GatherMultiDimParamsTestImpl( workloadFactory, memoryManager, tensorHandleFactory); } LayerTestResult GatherMultiDimParamsUint8Test( armnn::IWorkloadFactory& workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, const armnn::ITensorHandleFactory& tensorHandleFactory) { return GatherTestHelper::GatherMultiDimParamsTestImpl( workloadFactory, memoryManager, tensorHandleFactory); } LayerTestResult GatherMultiDimParamsInt16Test( armnn::IWorkloadFactory& workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, const armnn::ITensorHandleFactory& tensorHandleFactory) { return GatherTestHelper::GatherMultiDimParamsTestImpl( workloadFactory, memoryManager, tensorHandleFactory); } LayerTestResult GatherMultiDimParamsInt32Test( armnn::IWorkloadFactory& workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, const armnn::ITensorHandleFactory& tensorHandleFactory) { return GatherTestHelper::GatherMultiDimParamsTestImpl( workloadFactory, memoryManager, tensorHandleFactory); } LayerTestResult GatherMultiDimParamsMultiDimIndicesFloat32Test( armnn::IWorkloadFactory& workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, const armnn::ITensorHandleFactory& tensorHandleFactory) { return GatherTestHelper::GatherMultiDimParamsMultiDimIndicesTestImpl( workloadFactory, memoryManager, tensorHandleFactory); } LayerTestResult GatherMultiDimParamsMultiDimIndicesFloat16Test( armnn::IWorkloadFactory& workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, const armnn::ITensorHandleFactory& tensorHandleFactory) { return GatherTestHelper::GatherMultiDimParamsMultiDimIndicesTestImpl( workloadFactory, memoryManager, tensorHandleFactory); } LayerTestResult GatherMultiDimParamsMultiDimIndicesUint8Test( armnn::IWorkloadFactory& workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, const armnn::ITensorHandleFactory& tensorHandleFactory) { return GatherTestHelper::GatherMultiDimParamsMultiDimIndicesTestImpl( workloadFactory, memoryManager, tensorHandleFactory); } LayerTestResult GatherMultiDimParamsMultiDimIndicesInt16Test( armnn::IWorkloadFactory& workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, const armnn::ITensorHandleFactory& tensorHandleFactory) { return GatherTestHelper::GatherMultiDimParamsMultiDimIndicesTestImpl( workloadFactory, memoryManager, tensorHandleFactory); } LayerTestResult GatherMultiDimParamsMultiDimIndicesInt32Test( armnn::IWorkloadFactory& workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, const armnn::ITensorHandleFactory& tensorHandleFactory) { return GatherTestHelper::GatherMultiDimParamsMultiDimIndicesTestImpl( workloadFactory, memoryManager, tensorHandleFactory); }