2021-02-01 13:31:45 +01:00
|
|
|
/* ******************************************************************************
|
|
|
|
*
|
2019-06-06 14:21:15 +02:00
|
|
|
*
|
|
|
|
* This program and the accompanying materials are made available under the
|
|
|
|
* terms of the Apache License, Version 2.0 which is available at
|
|
|
|
* https://www.apache.org/licenses/LICENSE-2.0.
|
|
|
|
*
|
2021-02-01 13:31:45 +01:00
|
|
|
* See the NOTICE file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
2019-06-06 14:21:15 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <ops/declarable/helpers/s_t_d.h>
|
2019-11-13 15:15:18 +01:00
|
|
|
#include <execution/Threads.h>
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
namespace sd {
|
2019-06-06 14:21:15 +02:00
|
|
|
namespace ops {
|
|
|
|
namespace helpers {
|
|
|
|
template <typename T>
|
2020-04-21 11:41:30 +02:00
|
|
|
static void _spaceTodepth_(const NDArray &input, NDArray *output, int block_size, bool isNHWC) {
|
2020-05-09 07:06:14 +02:00
|
|
|
auto input_ptr = reinterpret_cast<T const*>(input.buffer());
|
2019-06-06 14:21:15 +02:00
|
|
|
auto output_ptr = reinterpret_cast<T *>(output->buffer());
|
|
|
|
|
2020-04-21 11:41:30 +02:00
|
|
|
const int batch_size = input.sizeAt(0);
|
|
|
|
const int input_depth = isNHWC ? input.sizeAt(3) : input.sizeAt(1);
|
|
|
|
const int input_height = isNHWC ? input.sizeAt(1) : input.sizeAt(2);
|
|
|
|
const int input_width = isNHWC ? input.sizeAt(2) : input.sizeAt(3);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
const int output_depth = isNHWC ? output->sizeAt(3) : output->sizeAt(1);
|
|
|
|
const int output_height = isNHWC ? output->sizeAt(1) : output->sizeAt(2);
|
|
|
|
const int output_width = isNHWC ? output->sizeAt(2) : output->sizeAt(3);
|
|
|
|
|
|
|
|
const int input_depth_by_output_height = input_depth * output_height;
|
|
|
|
|
|
|
|
const int output_area = output_width * output_height;
|
|
|
|
const int output_depth_by_output_area = output_depth * output_area;
|
|
|
|
|
|
|
|
|
|
|
|
if (isNHWC) {
|
|
|
|
const int total_count = batch_size * input_height * input_width * input_depth;
|
|
|
|
|
2019-11-13 15:15:18 +01:00
|
|
|
auto func = PRAGMA_THREADS_FOR {
|
2020-02-20 09:43:26 +01:00
|
|
|
for (auto inp_idx = start; inp_idx < stop; inp_idx++) {
|
2019-11-13 15:15:18 +01:00
|
|
|
// inp_idx = d + input_depth * (w + input_width * (h + input_height * b))
|
|
|
|
const int d = inp_idx % input_depth;
|
|
|
|
const int inp_idx2 = inp_idx / input_depth;
|
|
|
|
const int w = inp_idx2 % input_width;
|
|
|
|
const int inp_idx3 = inp_idx2 / input_width;
|
|
|
|
const int h = inp_idx3 % input_height;
|
|
|
|
const int b = inp_idx3 / input_height;
|
|
|
|
|
|
|
|
const int out_h = h / block_size;
|
|
|
|
const int offset_h = h % block_size;
|
|
|
|
const int out_w = w / block_size;
|
|
|
|
const int offset_w = w % block_size;
|
|
|
|
const int offset_d = (offset_h * block_size + offset_w) * input_depth;
|
|
|
|
const int out_d = d + offset_d;
|
|
|
|
|
|
|
|
const int out_idx = out_d + output_depth * (out_w + output_width * (out_h + output_height * b));
|
|
|
|
*(output_ptr + out_idx) = *(input_ptr + inp_idx);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-09 06:22:49 +01:00
|
|
|
samediff::Threads::parallel_for(func, 0, total_count);
|
2019-06-06 14:21:15 +02:00
|
|
|
} else {
|
|
|
|
const int total_count = batch_size * output_depth_by_output_area;
|
|
|
|
|
2019-11-13 15:15:18 +01:00
|
|
|
auto func = PRAGMA_THREADS_FOR {
|
2020-02-20 09:43:26 +01:00
|
|
|
for (auto inp_idx = start; inp_idx < stop; inp_idx++) {
|
2019-11-13 15:15:18 +01:00
|
|
|
const int n_iC_oY_bY_oX = inp_idx / block_size;
|
|
|
|
const int bX = inp_idx - n_iC_oY_bY_oX * block_size;
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2019-11-13 15:15:18 +01:00
|
|
|
const int n_iC_oY_bY = n_iC_oY_bY_oX / output_width;
|
|
|
|
const int oX = n_iC_oY_bY_oX - n_iC_oY_bY * output_width;
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2019-11-13 15:15:18 +01:00
|
|
|
const int n_iC_oY = n_iC_oY_bY / block_size;
|
|
|
|
const int bY = n_iC_oY_bY - n_iC_oY * block_size;
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2019-11-13 15:15:18 +01:00
|
|
|
const int n = n_iC_oY / input_depth_by_output_height;
|
|
|
|
const int iC_oY = n_iC_oY - n * input_depth_by_output_height;
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2019-11-13 15:15:18 +01:00
|
|
|
const int output_idx = oX + (((n * block_size + bY) * block_size + bX) * input_depth_by_output_height + iC_oY) * output_width;
|
|
|
|
|
|
|
|
*(output_ptr + output_idx) = *(input_ptr + inp_idx);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-09 06:22:49 +01:00
|
|
|
samediff::Threads::parallel_for(func, 0, total_count);
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-21 11:41:30 +02:00
|
|
|
void _spaceTodepth(sd::LaunchContext * context, const NDArray &input, NDArray *output, int block_size, bool isNHWC) {
|
|
|
|
BUILD_SINGLE_SELECTOR(input.dataType(), _spaceTodepth_, (input, output, block_size, isNHWC), LIBND4J_TYPES);
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
|
2020-04-21 11:41:30 +02:00
|
|
|
BUILD_SINGLE_TEMPLATE(template void _spaceTodepth_, (const NDArray &input, NDArray *output, int block_size, bool isNHWC), LIBND4J_TYPES);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|