1 /* 2 * Copyright (c) 2017-2018 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 std::uniform_real_distribution<> distribution(0.5f, 1.f); 68 library->fill(tensor, distribution, i); 69 } 70 compute_target()71 TensorType compute_target() 72 { 73 // Create tensors 74 TensorType w1 = create_tensor<TensorType>(TensorShape(180000U, 150U), DataType::F32, 1); 75 TensorType b1 = create_tensor<TensorType>(TensorShape(150U), DataType::F32, 1); 76 TensorType src = create_tensor<TensorType>(TensorShape(1U, 150U, 1200U, _max_batches), DataType::F32, 1); 77 TensorType dst = create_tensor<TensorType>(TensorShape(150U, _max_batches), DataType::F32, 1); 78 79 // Create and configure function 80 FullyConnectedFunction fc_layer_1; 81 fc_layer_1.configure(&src, &w1, &b1, &dst); 82 83 // Allocate persistent tensors 84 w1.allocator()->allocate(); 85 b1.allocator()->allocate(); 86 87 // Allocate tensors (1st iteration) 88 src.allocator()->allocate(); 89 dst.allocator()->allocate(); 90 91 // Fill tensors (1st iteration) 92 fill(AccessorType(src), 0); 93 fill(AccessorType(w1), 1); 94 fill(AccessorType(b1), 2); 95 96 // Compute functions (1st iteration) 97 fc_layer_1.run(); 98 99 // Update tensor shapes (2nd iteration) 100 auto src_padding = src.allocator()->info().padding(); 101 auto dst_padding = dst.allocator()->info().padding(); 102 int diff = _max_batches - _cur_batches; 103 auto new_src_padding = PaddingSize(src_padding.top, src_padding.right, src_padding.bottom + diff, src_padding.left); 104 auto new_dst_padding = PaddingSize(dst_padding.top, dst_padding.right, dst_padding.bottom + diff, dst_padding.left); 105 src.allocator()->info().set_tensor_shape(TensorShape(1U, 150U, 1200U, _cur_batches)).set_is_resizable(true).extend_padding(new_src_padding); 106 src.allocator()->info().set_is_resizable(false); 107 dst.allocator()->info().set_tensor_shape(TensorShape(150U, _cur_batches)).set_is_resizable(true).extend_padding(new_dst_padding); 108 dst.allocator()->info().set_is_resizable(false); 109 110 // Configure FC info 111 FullyConnectedLayerInfo fc_info; 112 fc_info.retain_internal_weights = true; 113 114 // Configure functions (2nd iteration) 115 fc_layer_1.configure(&src, &w1, &b1, &dst, fc_info); 116 117 // Fill tensors (2nd iteration) 118 fill(AccessorType(src), 5); 119 120 // Compute functions (2nd iteration) 121 fc_layer_1.run(); 122 123 return dst; 124 } 125 compute_reference()126 SimpleTensor<T> compute_reference() 127 { 128 // Create reference 129 SimpleTensor<T> w1{ TensorShape(180000U, 150U), DataType::F32 }; 130 SimpleTensor<T> b1{ TensorShape(150U), DataType::F32 }; 131 SimpleTensor<T> src{ TensorShape(1U, 150U, 1200U, _cur_batches), DataType::F32 }; 132 133 // Fill reference 134 fill(src, 5); 135 fill(w1, 1); 136 fill(b1, 2); 137 138 return reference::fully_connected_layer(src, w1, b1, TensorShape(150U, _cur_batches)); 139 } 140 141 protected: 142 TensorType _target{}; 143 SimpleTensor<T> _reference{}; 144 unsigned int _max_batches{}; 145 unsigned int _cur_batches{}; 146 }; 147 } // namespace validation 148 } // namespace test 149 } // namespace arm_compute 150 #endif /* ARM_COMPUTE_TEST_UNIT_WEIGHTS_RETENTION */ 151