1 /* 2 * Copyright (c) 2017-2021 Arm Limited. 3 * 4 * SPDX-License-Identifier: MIT 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a copy 7 * of this software and associated documentation files (the "Software"), to 8 * deal in the Software without restriction, including without limitation the 9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 10 * sell copies of the Software, and to permit persons to whom the Software is 11 * furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in all 14 * copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 * SOFTWARE. 23 */ 24 #ifndef ARM_COMPUTE_TEST_UNIT_WEIGHTS_RETENTION 25 #define ARM_COMPUTE_TEST_UNIT_WEIGHTS_RETENTION 26 27 #include "arm_compute/core/TensorShape.h" 28 #include "arm_compute/core/Types.h" 29 #include "tests/AssetsLibrary.h" 30 #include "tests/Globals.h" 31 #include "tests/IAccessor.h" 32 #include "tests/framework/Asserts.h" 33 #include "tests/framework/Fixture.h" 34 #include "tests/validation/Helpers.h" 35 #include "tests/validation/reference/FullyConnectedLayer.h" 36 37 namespace arm_compute 38 { 39 namespace test 40 { 41 namespace validation 42 { 43 /** Test case to run a fully connected layer with weights retention, reconfigure 44 * with different shapes and rerun making sure the weights are retained. 45 * 46 * Runs a fully connected layer stimulating is_interleaved_transpose CLGEMM, 47 * then reconfigures with different batch size and reruns. 48 */ 49 template <typename TensorType, typename AccessorType, typename FullyConnectedFunction> 50 class WeightsRetentionReconfigureTestCaseFixture : public framework::Fixture 51 { 52 using T = float; 53 54 public: setup()55 void setup() 56 { 57 _max_batches = 8; 58 _cur_batches = 6; 59 _target = compute_target(); 60 _reference = compute_reference(); 61 }; 62 63 protected: 64 template <typename U> fill(U && tensor,int i)65 void fill(U &&tensor, int i) 66 { 67 static_assert(std::is_floating_point<T>::value || std::is_same<T, half>::value, "Only floating point data types supported."); 68 using DistributionType = typename std::conditional<std::is_same<T, half>::value, arm_compute::utils::uniform_real_distribution_16bit<T>, std::uniform_real_distribution<T>>::type; 69 70 DistributionType distribution{ T(0.5f), T(1.0f) }; 71 library->fill(tensor, distribution, i); 72 } 73 compute_target()74 TensorType compute_target() 75 { 76 // Create tensors 77 TensorType w1 = create_tensor<TensorType>(TensorShape(6000U, 15U), DataType::F32, 1); 78 TensorType b1 = create_tensor<TensorType>(TensorShape(15U), DataType::F32, 1); 79 TensorType src = create_tensor<TensorType>(TensorShape(1U, 15U, 400U, _max_batches), DataType::F32, 1); 80 TensorType dst = create_tensor<TensorType>(TensorShape(15U, _max_batches), DataType::F32, 1); 81 82 // Create and configure function 83 FullyConnectedFunction fc_layer_1; 84 fc_layer_1.configure(&src, &w1, &b1, &dst); 85 86 // Allocate persistent tensors 87 w1.allocator()->allocate(); 88 b1.allocator()->allocate(); 89 90 // Allocate tensors (1st iteration) 91 src.allocator()->allocate(); 92 dst.allocator()->allocate(); 93 94 // Fill tensors (1st iteration) 95 fill(AccessorType(src), 0); 96 fill(AccessorType(w1), 1); 97 fill(AccessorType(b1), 2); 98 99 // Compute functions (1st iteration) 100 fc_layer_1.run(); 101 102 // Update tensor shapes (2nd iteration) 103 auto src_padding = src.allocator()->info().padding(); 104 auto dst_padding = dst.allocator()->info().padding(); 105 int diff = _max_batches - _cur_batches; 106 auto new_src_padding = PaddingSize(src_padding.top, src_padding.right, src_padding.bottom + diff, src_padding.left); 107 auto new_dst_padding = PaddingSize(dst_padding.top, dst_padding.right, dst_padding.bottom + diff, dst_padding.left); 108 src.allocator()->info().set_tensor_shape(TensorShape(1U, 15U, 400U, _cur_batches)).set_is_resizable(true).extend_padding(new_src_padding); 109 src.allocator()->info().set_is_resizable(false); 110 dst.allocator()->info().set_tensor_shape(TensorShape(15U, _cur_batches)).set_is_resizable(true).extend_padding(new_dst_padding); 111 dst.allocator()->info().set_is_resizable(false); 112 113 // Configure FC info 114 FullyConnectedLayerInfo fc_info; 115 fc_info.retain_internal_weights = true; 116 117 // Configure functions (2nd iteration) 118 fc_layer_1.configure(&src, &w1, &b1, &dst, fc_info); 119 120 // Fill tensors (2nd iteration) 121 fill(AccessorType(src), 5); 122 123 // Compute functions (2nd iteration) 124 fc_layer_1.run(); 125 126 return dst; 127 } 128 compute_reference()129 SimpleTensor<T> compute_reference() 130 { 131 // Create reference 132 SimpleTensor<T> w1{ TensorShape(6000U, 15U), DataType::F32 }; 133 SimpleTensor<T> b1{ TensorShape(15U), DataType::F32 }; 134 SimpleTensor<T> src{ TensorShape(1U, 15U, 400U, _cur_batches), DataType::F32 }; 135 136 // Fill reference 137 fill(src, 5); 138 fill(w1, 1); 139 fill(b1, 2); 140 141 return reference::fully_connected_layer(src, w1, b1, TensorShape(15U, _cur_batches)); 142 } 143 144 protected: 145 TensorType _target{}; 146 SimpleTensor<T> _reference{}; 147 unsigned int _max_batches{}; 148 unsigned int _cur_batches{}; 149 }; 150 } // namespace validation 151 } // namespace test 152 } // namespace arm_compute 153 #endif /* ARM_COMPUTE_TEST_UNIT_WEIGHTS_RETENTION */ 154