2019-06-06 14:21:15 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
* Copyright (c) 2015-2018 Skymind, Inc.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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/d_t_s.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 __depthToSpace(const NDArray &input, NDArray *output, int block_size, bool isNHWC) {
|
|
|
|
T *input_ptr = reinterpret_cast<T *>(input.getBuffer());
|
2019-06-06 14:21:15 +02:00
|
|
|
T *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_area = input_width * input_height;
|
|
|
|
const int input_depth_by_input_area = input_depth * input_area;
|
|
|
|
const int output_depth_by_input_height = output_depth * input_height;
|
|
|
|
|
|
|
|
if (isNHWC) {
|
|
|
|
const int total_count = batch_size * output_height * output_width * output_depth;
|
2019-11-13 15:15:18 +01:00
|
|
|
auto func = PRAGMA_THREADS_FOR {
|
2020-02-20 09:43:26 +01:00
|
|
|
for (auto out_idx = start; out_idx < stop; out_idx++) {
|
2019-11-13 15:15:18 +01:00
|
|
|
const int d = out_idx % output_depth;
|
|
|
|
const int out_idx2 = out_idx / output_depth;
|
|
|
|
const int w = out_idx2 % output_width;
|
|
|
|
const int out_idx3 = out_idx2 / output_width;
|
|
|
|
const int h = out_idx3 % output_height;
|
|
|
|
const int b = out_idx3 / output_height;
|
|
|
|
|
|
|
|
const int in_h = h / block_size;
|
|
|
|
const int offset_h = h % block_size;
|
|
|
|
const int in_w = w / block_size;
|
|
|
|
const int offset_w = w % block_size;
|
|
|
|
const int offset_d = (offset_h * block_size + offset_w) * output_depth;
|
|
|
|
const int in_d = d + offset_d;
|
|
|
|
const int inp_idx = in_d + input_depth * (in_w + input_width * (in_h + input_height * b));
|
|
|
|
(output_ptr + out_idx)[0] = (input_ptr + inp_idx)[0];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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 * input_depth_by_input_area;
|
|
|
|
|
2019-11-13 15:15:18 +01:00
|
|
|
auto func = PRAGMA_THREADS_FOR {
|
2020-02-20 09:43:26 +01:00
|
|
|
for (int input_idx = start; input_idx < stop; input_idx++) {
|
2019-11-13 15:15:18 +01:00
|
|
|
const int n_bY_bX_oC_iY = input_idx / input_width;
|
|
|
|
const int iX = input_idx - n_bY_bX_oC_iY * input_width;
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2019-11-13 15:15:18 +01:00
|
|
|
const int n_bY_bX = n_bY_bX_oC_iY / output_depth_by_input_height;
|
|
|
|
const int oC_iY = n_bY_bX_oC_iY - n_bY_bX * output_depth_by_input_height;
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2019-11-13 15:15:18 +01:00
|
|
|
const int n_bY = n_bY_bX / block_size;
|
|
|
|
const int bX = n_bY_bX - n_bY * block_size;
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2019-11-13 15:15:18 +01:00
|
|
|
const int n = n_bY / block_size;
|
|
|
|
const int bY = n_bY - n * block_size;
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2019-11-13 15:15:18 +01:00
|
|
|
const int output_idx = bX + block_size * (iX + input_width * (bY + block_size * (oC_iY + n * output_depth_by_input_height)));
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2019-11-13 15:15:18 +01:00
|
|
|
(output_ptr + output_idx)[0] = (input_ptr + input_idx)[0];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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 _depthToSpace(sd::LaunchContext * context, const NDArray &input, NDArray *output, int block_size, bool isNHWC) {
|
|
|
|
auto xType = input.dataType();
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
BUILD_SINGLE_SELECTOR(xType, __depthToSpace, (input, output, block_size, isNHWC), LIBND4J_TYPES);
|
|
|
|
}
|
|
|
|
|
2020-04-21 11:41:30 +02:00
|
|
|
BUILD_SINGLE_TEMPLATE(template void __depthToSpace, (const NDArray &input, NDArray *output, int block_size, bool isNHWC);, LIBND4J_TYPES);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|