@@ -135,41 +135,21 @@ T* UTILITY::Unidirectional_broadcast(const T* original_data, const std::vector<s
135135 return new_datavector;
136136}
137137
138- template <typename T>
139- std::vector<size_t > UTILITY::Multidirectional_broadcast (const T* original_data, std::vector<size_t > input1_shape, std::vector<size_t > input2_shape)
138+
139+
140+ std::vector<size_t > UTILITY::Multidirectional_broadcast (std::vector<size_t > input1_shape, std::vector<size_t > input2_shape)
140141{
141142 std::vector<size_t > input_shape = (input1_shape > input2_shape)?input1_shape:input2_shape;
142143 std::vector<size_t > output_shape (input_shape);
143144
144- // check if both the input have same shape, nothing to do directly return the output_shape as the same shape.
145- if (input1_shape.size () == input2_shape.size ()){
146- if (input1_shape == input2_shape){
147- return output_shape;
148- }
149- else {
150- // Check the shape values, if input1[i] not equal to input2[i] we have the result shape equal to input1[i] if input2[i] = 1 or viceversa
151- for (size_t j = 0 ; j < input1_shape.size () ; j++){
152- if (input1_shape[j] == input2_shape[j]){
153- output_shape[j] = input1_shape[j];
154- }
155- else if (input1_shape[j] > input2_shape[j] && input2_shape[j] == 1 ){
156- output_shape[j] = input1_shape[j];
157- }
158- else if (input2_shape[j] > input1_shape[j] && input1_shape[j] == 1 ){
159- output_shape[j] = input2_shape[j];
160- }
161- }
162- return output_shape;
163- }
164- }
165- else if (input1_shape.size () < input2_shape.size ()){
145+ if (input1_shape.size () < input2_shape.size ()){
166146 // Check if input1_shape.size() < input2_shape.size() we insert in the shape vector values of 1 at the beginning of the tensor until input1_shape.size() == input2_shape.size()
167147 auto it = input1_shape.begin ();
168148 while (input1_shape.size () < input2_shape.size ()) {
169149 it = input1_shape.insert (it, 1 );
170150 }
171151 if (input1_shape.size ()==input1_shape.size ()){
172- UTILITY::Multidirectional_broadcast (original_data, input1_shape,input2_shape);
152+ UTILITY::Multidirectional_broadcast (input1_shape,input2_shape);
173153 }
174154 }
175155 else if (input2_shape.size () < input1_shape.size ()){
@@ -179,9 +159,28 @@ std::vector<size_t> UTILITY::Multidirectional_broadcast(const T* original_data,
179159 it = input2_shape.insert (it, 1 );
180160 }
181161 if (input1_shape.size ()==input1_shape.size ()){
182- UTILITY::Multidirectional_broadcast (original_data, input1_shape,input2_shape);
162+ UTILITY::Multidirectional_broadcast (input1_shape,input2_shape);
183163 }
184164 }
165+ // check if both the input have same shape, nothing to do directly return the output_shape as the same shape.
166+ else if (input1_shape.size () == input2_shape.size ()){
167+ if (input1_shape != input2_shape){
168+ // Check the shape values, if input1[i] not equal to input2[i] we have the result shape equal to input1[i] if input2[i] = 1 or viceversa
169+ for (size_t j = 0 ; j < input1_shape.size () ; j++){
170+ if (input1_shape[j] == input2_shape[j]){
171+ output_shape[j] = input1_shape[j];
172+ }
173+ else if (input1_shape[j] > input2_shape[j] && input2_shape[j] == 1 ){
174+ output_shape[j] = input1_shape[j];
175+ }
176+ else if (input2_shape[j] > input1_shape[j] && input1_shape[j] == 1 ){
177+ output_shape[j] = input2_shape[j];
178+ }
179+ }
180+ }
181+
182+ }
183+ return output_shape;
185184
186185}
187186
@@ -192,7 +191,6 @@ std::string UTILITY::Clean_name(std::string input_tensor_name){
192191}
193192
194193template float * UTILITY::Unidirectional_broadcast (const float * original_data, const std::vector<size_t > original_shape, const std::vector<size_t > target_shape);
195- template std::vector<size_t > UTILITY::Multidirectional_broadcast (const float * original_data, std::vector<size_t > input1_shape, std::vector<size_t > input2_shape);
196194
197195}// SOFIE
198196}// Experimental
0 commit comments