// // Copyright © 2021 Arm Ltd and Contributors. All rights reserved. // SPDX-License-Identifier: MIT // #include #include #include #include #include using namespace armnn; BOOST_AUTO_TEST_SUITE(ClImportTensorHandleTests) BOOST_FIXTURE_TEST_CASE(ClMallocImport, ClContextControlFixture) { ClImportTensorHandleFactory handleFactory(static_cast(MemorySource::Malloc), static_cast(MemorySource::Malloc)); TensorInfo info({ 1, 24, 16, 3 }, DataType::Float32); unsigned int numElements = info.GetNumElements(); // create TensorHandle for memory import auto handle = handleFactory.CreateTensorHandle(info); // Get CLtensor arm_compute::CLTensor& tensor = PolymorphicDowncast(handle.get())->GetTensor(); // Create and configure activation function const arm_compute::ActivationLayerInfo act_info(arm_compute::ActivationLayerInfo::ActivationFunction::RELU); arm_compute::CLActivationLayer act_func; act_func.configure(&tensor, nullptr, act_info); // Allocate user memory const size_t totalBytes = tensor.info()->total_size(); const size_t alignment = arm_compute::CLKernelLibrary::get().get_device().getInfo(); size_t space = totalBytes + alignment; auto testData = std::make_unique(space); void* alignedPtr = testData.get(); BOOST_CHECK(std::align(alignment, totalBytes, alignedPtr, space)); // Import memory BOOST_CHECK(handle->Import(alignedPtr, armnn::MemorySource::Malloc)); // Input with negative values auto* typedPtr = reinterpret_cast(alignedPtr); std::fill_n(typedPtr, numElements, -5.0f); // Execute function and sync act_func.run(); arm_compute::CLScheduler::get().sync(); // Validate result by checking that the output has no negative values for(unsigned int i = 0; i < numElements; ++i) { BOOST_ASSERT(typedPtr[i] >= 0); } } BOOST_FIXTURE_TEST_CASE(ClIncorrectMemorySourceImport, ClContextControlFixture) { ClImportTensorHandleFactory handleFactory(static_cast(MemorySource::Malloc), static_cast(MemorySource::Malloc)); TensorInfo info({ 1, 24, 16, 3 }, DataType::Float32); // create TensorHandle for memory import auto handle = handleFactory.CreateTensorHandle(info); // Get CLtensor arm_compute::CLTensor& tensor = PolymorphicDowncast(handle.get())->GetTensor(); // Allocate user memory const size_t totalBytes = tensor.info()->total_size(); const size_t alignment = arm_compute::CLKernelLibrary::get().get_device().getInfo(); size_t space = totalBytes + alignment; auto testData = std::make_unique(space); void* alignedPtr = testData.get(); BOOST_CHECK(std::align(alignment, totalBytes, alignedPtr, space)); // Import memory BOOST_CHECK_THROW(handle->Import(alignedPtr, armnn::MemorySource::Undefined), MemoryImportException); } BOOST_FIXTURE_TEST_CASE(ClInvalidMemorySourceImport, ClContextControlFixture) { MemorySource invalidMemSource = static_cast(256); ClImportTensorHandleFactory handleFactory(static_cast(invalidMemSource), static_cast(invalidMemSource)); TensorInfo info({ 1, 2, 2, 1 }, DataType::Float32); // create TensorHandle for memory import auto handle = handleFactory.CreateTensorHandle(info); // Allocate user memory std::vector inputData { 1.0f, 2.0f, 3.0f, 4.0f }; // Import non-support memory BOOST_CHECK_THROW(handle->Import(inputData.data(), invalidMemSource), MemoryImportException); } BOOST_AUTO_TEST_SUITE_END()