Colm Donelan | c74b175 | 2021-03-12 15:58:48 +0000 | [diff] [blame] | 1 | // |
Colm Donelan | 68c60e9 | 2024-02-21 15:58:35 +0000 | [diff] [blame] | 2 | // Copyright © 2021, 2024 Arm Ltd. All rights reserved. |
Colm Donelan | c74b175 | 2021-03-12 15:58:48 +0000 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #include <cl/ClImportTensorHandleFactory.hpp> |
| 7 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 8 | #include <doctest/doctest.h> |
Colm Donelan | c74b175 | 2021-03-12 15:58:48 +0000 | [diff] [blame] | 9 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 10 | TEST_SUITE("ClImportTensorHandleFactoryTests") |
| 11 | { |
Colm Donelan | c74b175 | 2021-03-12 15:58:48 +0000 | [diff] [blame] | 12 | using namespace armnn; |
| 13 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 14 | TEST_CASE("ImportTensorFactoryAskedToCreateManagedTensorThrowsException") |
Colm Donelan | c74b175 | 2021-03-12 15:58:48 +0000 | [diff] [blame] | 15 | { |
| 16 | // Create the factory to import tensors. |
| 17 | ClImportTensorHandleFactory factory(static_cast<MemorySourceFlags>(MemorySource::Malloc), |
| 18 | static_cast<MemorySourceFlags>(MemorySource::Malloc)); |
| 19 | TensorInfo tensorInfo; |
| 20 | // This factory is designed to import the memory of tensors. Asking for a handle that requires |
| 21 | // a memory manager should result in an exception. |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 22 | REQUIRE_THROWS_AS(factory.CreateTensorHandle(tensorInfo, true), InvalidArgumentException); |
| 23 | REQUIRE_THROWS_AS(factory.CreateTensorHandle(tensorInfo, DataLayout::NCHW, true), InvalidArgumentException); |
Colm Donelan | c74b175 | 2021-03-12 15:58:48 +0000 | [diff] [blame] | 24 | } |
| 25 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 26 | TEST_CASE("ImportTensorFactoryCreateMallocTensorHandle") |
Colm Donelan | c74b175 | 2021-03-12 15:58:48 +0000 | [diff] [blame] | 27 | { |
| 28 | // Create the factory to import tensors. |
| 29 | ClImportTensorHandleFactory factory(static_cast<MemorySourceFlags>(MemorySource::Malloc), |
| 30 | static_cast<MemorySourceFlags>(MemorySource::Malloc)); |
| 31 | TensorShape tensorShape{ 6, 7, 8, 9 }; |
| 32 | TensorInfo tensorInfo(tensorShape, armnn::DataType::Float32); |
| 33 | // Start with the TensorInfo factory method. Create an import tensor handle and verify the data is |
| 34 | // passed through correctly. |
| 35 | auto tensorHandle = factory.CreateTensorHandle(tensorInfo); |
Colm Donelan | 68c60e9 | 2024-02-21 15:58:35 +0000 | [diff] [blame] | 36 | CHECK(tensorHandle); |
| 37 | CHECK(tensorHandle->GetImportFlags() == static_cast<MemorySourceFlags>(MemorySource::Malloc)); |
| 38 | CHECK(tensorHandle->GetShape() == tensorShape); |
Colm Donelan | c74b175 | 2021-03-12 15:58:48 +0000 | [diff] [blame] | 39 | |
| 40 | // Same method but explicitly specifying isManaged = false. |
| 41 | tensorHandle = factory.CreateTensorHandle(tensorInfo, false); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 42 | CHECK(tensorHandle); |
Colm Donelan | 68c60e9 | 2024-02-21 15:58:35 +0000 | [diff] [blame] | 43 | CHECK(tensorHandle->GetImportFlags() == static_cast<MemorySourceFlags>(MemorySource::Malloc)); |
| 44 | CHECK(tensorHandle->GetShape() == tensorShape); |
Colm Donelan | c74b175 | 2021-03-12 15:58:48 +0000 | [diff] [blame] | 45 | |
| 46 | // Now try TensorInfo and DataLayout factory method. |
| 47 | tensorHandle = factory.CreateTensorHandle(tensorInfo, DataLayout::NHWC); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 48 | CHECK(tensorHandle); |
Colm Donelan | 68c60e9 | 2024-02-21 15:58:35 +0000 | [diff] [blame] | 49 | CHECK(tensorHandle->GetImportFlags() == static_cast<MemorySourceFlags>(MemorySource::Malloc)); |
| 50 | CHECK(tensorHandle->GetShape() == tensorShape); |
Colm Donelan | c74b175 | 2021-03-12 15:58:48 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 53 | TEST_CASE("CreateSubtensorOfImportTensor") |
Colm Donelan | c74b175 | 2021-03-12 15:58:48 +0000 | [diff] [blame] | 54 | { |
| 55 | // Create the factory to import tensors. |
| 56 | ClImportTensorHandleFactory factory(static_cast<MemorySourceFlags>(MemorySource::Malloc), |
| 57 | static_cast<MemorySourceFlags>(MemorySource::Malloc)); |
| 58 | // Create a standard inport tensor. |
| 59 | TensorShape tensorShape{ 224, 224, 1, 1 }; |
| 60 | TensorInfo tensorInfo(tensorShape, armnn::DataType::Float32); |
| 61 | auto tensorHandle = factory.CreateTensorHandle(tensorInfo); |
| 62 | // Use the factory to create a 16x16 sub tensor. |
| 63 | TensorShape subTensorShape{ 16, 16, 1, 1 }; |
| 64 | // Starting at an offset of 1x1. |
| 65 | uint32_t origin[4] = { 1, 1, 0, 0 }; |
| 66 | auto subTensor = factory.CreateSubTensorHandle(*tensorHandle, subTensorShape, origin); |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 67 | CHECK(subTensor); |
Colm Donelan | 68c60e9 | 2024-02-21 15:58:35 +0000 | [diff] [blame] | 68 | CHECK(subTensor->GetShape() == subTensorShape); |
| 69 | CHECK(subTensor->GetParent() == tensorHandle.get()); |
Colm Donelan | c74b175 | 2021-03-12 15:58:48 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 72 | TEST_CASE("CreateSubtensorNonZeroXYIsInvalid") |
Colm Donelan | c74b175 | 2021-03-12 15:58:48 +0000 | [diff] [blame] | 73 | { |
| 74 | // Create the factory to import tensors. |
| 75 | ClImportTensorHandleFactory factory(static_cast<MemorySourceFlags>(MemorySource::Malloc), |
| 76 | static_cast<MemorySourceFlags>(MemorySource::Malloc)); |
| 77 | // Create a standard import tensor. |
| 78 | TensorShape tensorShape{ 224, 224, 1, 1 }; |
| 79 | TensorInfo tensorInfo(tensorShape, armnn::DataType::Float32); |
| 80 | auto tensorHandle = factory.CreateTensorHandle(tensorInfo); |
| 81 | // Use the factory to create a 16x16 sub tensor. |
| 82 | TensorShape subTensorShape{ 16, 16, 1, 1 }; |
| 83 | // This looks a bit backwards because of how Cl specifies tensors. Essentially we want to trigger our |
| 84 | // check "(coords.x() != 0 || coords.y() != 0)" |
| 85 | uint32_t origin[4] = { 0, 0, 1, 1 }; |
| 86 | auto subTensor = factory.CreateSubTensorHandle(*tensorHandle, subTensorShape, origin); |
| 87 | // We expect a nullptr. |
Colm Donelan | 68c60e9 | 2024-02-21 15:58:35 +0000 | [diff] [blame] | 88 | CHECK(subTensor == nullptr); |
Colm Donelan | c74b175 | 2021-03-12 15:58:48 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 91 | TEST_CASE("CreateSubtensorXYMustMatchParent") |
Colm Donelan | c74b175 | 2021-03-12 15:58:48 +0000 | [diff] [blame] | 92 | { |
| 93 | // Create the factory to import tensors. |
| 94 | ClImportTensorHandleFactory factory(static_cast<MemorySourceFlags>(MemorySource::Malloc), |
| 95 | static_cast<MemorySourceFlags>(MemorySource::Malloc)); |
| 96 | // Create a standard import tensor. |
| 97 | TensorShape tensorShape{ 224, 224, 1, 1 }; |
| 98 | TensorInfo tensorInfo(tensorShape, armnn::DataType::Float32); |
| 99 | auto tensorHandle = factory.CreateTensorHandle(tensorInfo); |
| 100 | // Use the factory to create a 16x16 sub tensor but make the CL x and y axis different. |
| 101 | TensorShape subTensorShape{ 16, 16, 2, 2 }; |
| 102 | // We want to trigger our ((parentShape.x() != shape.x()) || (parentShape.y() != shape.y())) |
| 103 | uint32_t origin[4] = { 1, 1, 0, 0 }; |
| 104 | auto subTensor = factory.CreateSubTensorHandle(*tensorHandle, subTensorShape, origin); |
| 105 | // We expect a nullptr. |
Colm Donelan | 68c60e9 | 2024-02-21 15:58:35 +0000 | [diff] [blame] | 106 | CHECK(subTensor == nullptr); |
Colm Donelan | c74b175 | 2021-03-12 15:58:48 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 109 | TEST_CASE("CreateSubtensorMustBeSmallerThanParent") |
Colm Donelan | c74b175 | 2021-03-12 15:58:48 +0000 | [diff] [blame] | 110 | { |
| 111 | // Create the factory to import tensors. |
| 112 | ClImportTensorHandleFactory factory(static_cast<MemorySourceFlags>(MemorySource::Malloc), |
| 113 | static_cast<MemorySourceFlags>(MemorySource::Malloc)); |
| 114 | // Create a standard import tensor. |
| 115 | TensorShape tensorShape{ 224, 224, 1, 1 }; |
| 116 | TensorInfo tensorInfo(tensorShape, armnn::DataType::Float32); |
| 117 | auto tensorHandle = factory.CreateTensorHandle(tensorInfo); |
| 118 | // Ask for a subtensor that's the same size as the parent. |
| 119 | TensorShape subTensorShape{ 224, 224, 1, 1 }; |
| 120 | uint32_t origin[4] = { 1, 1, 0, 0 }; |
| 121 | // This should result in a nullptr. |
| 122 | auto subTensor = factory.CreateSubTensorHandle(*tensorHandle, subTensorShape, origin); |
Colm Donelan | 68c60e9 | 2024-02-21 15:58:35 +0000 | [diff] [blame] | 123 | CHECK(subTensor == nullptr); |
Colm Donelan | c74b175 | 2021-03-12 15:58:48 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Sadik Armagan | 1625efc | 2021-06-10 18:24:34 +0100 | [diff] [blame] | 126 | } |