diff --git a/libnd4j/include/ops/declarable/generic/parity_ops/crop_and_resize.cpp b/libnd4j/include/ops/declarable/generic/parity_ops/crop_and_resize.cpp index 7afb24bd7..cdce8a95a 100644 --- a/libnd4j/include/ops/declarable/generic/parity_ops/crop_and_resize.cpp +++ b/libnd4j/include/ops/declarable/generic/parity_ops/crop_and_resize.cpp @@ -23,7 +23,8 @@ //#include #include -#include +#include + namespace nd4j { namespace ops { CUSTOM_OP_IMPL(crop_and_resize, 4, 1, false, 0, 0) { diff --git a/libnd4j/include/ops/declarable/helpers/cpu/addBias.cpp b/libnd4j/include/ops/declarable/helpers/cpu/addBias.cpp index e5242a5be..39e51f6d7 100644 --- a/libnd4j/include/ops/declarable/helpers/cpu/addBias.cpp +++ b/libnd4j/include/ops/declarable/helpers/cpu/addBias.cpp @@ -374,6 +374,28 @@ namespace nd4j { template static void addBias_(const NDArray& input, const NDArray& bias, NDArray& output, const bool isNCHW) { + /* + if (input.rankOf() == 2 && bias.rankOf() == 1 && input.sizeAt(1) == bias.sizeAt(0) && input.ordering() == 'c') { + int rows = input.sizeAt(0); + int biasLen = bias.lengthOf(); + + auto inB = input.bufferAsT(); + auto bB = bias.bufferAsT(); + auto outB = output.bufferAsT(); + + for (int e = 0; e < rows; e++) { + auto row = inB + (e * biasLen); + auto out = outB + (e * biasLen); + + for (int t = 0; t < biasLen; t++) { + out[t] = row[t] + bB[t]; + } + } + + return; + } + */ + Nd4jLong* x_shapeInfo = input.getShapeInfo(); Nd4jLong* z_shapeInfo = output.getShapeInfo(); X* x = input.bufferAsT(); diff --git a/libnd4j/include/ops/declarable/helpers/cpu/compilation_units/crop_and_resize_0.cpp b/libnd4j/include/ops/declarable/helpers/cpu/compilation_units/crop_and_resize_0.cpp new file mode 100644 index 000000000..3177cca34 --- /dev/null +++ b/libnd4j/include/ops/declarable/helpers/cpu/compilation_units/crop_and_resize_0.cpp @@ -0,0 +1,30 @@ +/******************************************************************************* + * 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 + ******************************************************************************/ + +// +// @author sgazeos@gmail.com +// + +#include +#include "../crop_and_resize.hpp" + +namespace nd4j { + namespace ops { + namespace helpers { + BUILD_TRIPLE_TEMPLATE(template void cropAndResizeFunctor_, (NDArray const *images, NDArray const *boxes, NDArray const *indices, NDArray const *cropSize, int method, double extrapolationVal, NDArray *crops), NUMERIC_TYPES_0, FLOAT_TYPES, INTEGER_TYPES); + } + } +} \ No newline at end of file diff --git a/libnd4j/include/ops/declarable/helpers/cpu/compilation_units/crop_and_resize_1.cpp b/libnd4j/include/ops/declarable/helpers/cpu/compilation_units/crop_and_resize_1.cpp new file mode 100644 index 000000000..cd9c00dc5 --- /dev/null +++ b/libnd4j/include/ops/declarable/helpers/cpu/compilation_units/crop_and_resize_1.cpp @@ -0,0 +1,30 @@ +/******************************************************************************* + * 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 + ******************************************************************************/ + +// +// @author sgazeos@gmail.com +// + +#include +#include "../crop_and_resize.hpp" + +namespace nd4j { + namespace ops { + namespace helpers { + BUILD_TRIPLE_TEMPLATE(template void cropAndResizeFunctor_, (NDArray const *images, NDArray const *boxes, NDArray const *indices, NDArray const *cropSize, int method, double extrapolationVal, NDArray *crops), NUMERIC_TYPES_1, FLOAT_TYPES, INTEGER_TYPES); + } + } +} \ No newline at end of file diff --git a/libnd4j/include/ops/declarable/helpers/cpu/compilation_units/crop_and_resize_2.cpp b/libnd4j/include/ops/declarable/helpers/cpu/compilation_units/crop_and_resize_2.cpp new file mode 100644 index 000000000..3b126d288 --- /dev/null +++ b/libnd4j/include/ops/declarable/helpers/cpu/compilation_units/crop_and_resize_2.cpp @@ -0,0 +1,30 @@ +/******************************************************************************* + * 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 + ******************************************************************************/ + +// +// @author sgazeos@gmail.com +// + +#include +#include "../crop_and_resize.hpp" + +namespace nd4j { + namespace ops { + namespace helpers { + BUILD_TRIPLE_TEMPLATE(template void cropAndResizeFunctor_, (NDArray const *images, NDArray const *boxes, NDArray const *indices, NDArray const *cropSize, int method, double extrapolationVal, NDArray *crops), NUMERIC_TYPES_2, FLOAT_TYPES, INTEGER_TYPES); + } + } +} \ No newline at end of file diff --git a/libnd4j/include/ops/declarable/helpers/cpu/compilation_units/crop_and_resize_3.cpp b/libnd4j/include/ops/declarable/helpers/cpu/compilation_units/crop_and_resize_3.cpp new file mode 100644 index 000000000..cca97a1ac --- /dev/null +++ b/libnd4j/include/ops/declarable/helpers/cpu/compilation_units/crop_and_resize_3.cpp @@ -0,0 +1,30 @@ +/******************************************************************************* + * 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 + ******************************************************************************/ + +// +// @author sgazeos@gmail.com +// + +#include +#include "../crop_and_resize.hpp" + +namespace nd4j { + namespace ops { + namespace helpers { + BUILD_TRIPLE_TEMPLATE(template void cropAndResizeFunctor_, (NDArray const *images, NDArray const *boxes, NDArray const *indices, NDArray const *cropSize, int method, double extrapolationVal, NDArray *crops), NUMERIC_TYPES_3, FLOAT_TYPES, INTEGER_TYPES); + } + } +} \ No newline at end of file diff --git a/libnd4j/include/ops/declarable/helpers/cpu/compilation_units/crop_and_resize_4.cpp b/libnd4j/include/ops/declarable/helpers/cpu/compilation_units/crop_and_resize_4.cpp new file mode 100644 index 000000000..568492c08 --- /dev/null +++ b/libnd4j/include/ops/declarable/helpers/cpu/compilation_units/crop_and_resize_4.cpp @@ -0,0 +1,30 @@ +/******************************************************************************* + * 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 + ******************************************************************************/ + +// +// @author sgazeos@gmail.com +// + +#include +#include "../crop_and_resize.hpp" + +namespace nd4j { + namespace ops { + namespace helpers { + BUILD_TRIPLE_TEMPLATE(template void cropAndResizeFunctor_, (NDArray const *images, NDArray const *boxes, NDArray const *indices, NDArray const *cropSize, int method, double extrapolationVal, NDArray *crops), NUMERIC_TYPES_4, FLOAT_TYPES, INTEGER_TYPES); + } + } +} \ No newline at end of file diff --git a/libnd4j/include/ops/declarable/helpers/cpu/compilation_units/crop_and_resize_5.cpp b/libnd4j/include/ops/declarable/helpers/cpu/compilation_units/crop_and_resize_5.cpp new file mode 100644 index 000000000..1491c9e1d --- /dev/null +++ b/libnd4j/include/ops/declarable/helpers/cpu/compilation_units/crop_and_resize_5.cpp @@ -0,0 +1,30 @@ +/******************************************************************************* + * 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 + ******************************************************************************/ + +// +// @author sgazeos@gmail.com +// + +#include +#include "../crop_and_resize.hpp" + +namespace nd4j { + namespace ops { + namespace helpers { + BUILD_TRIPLE_TEMPLATE(template void cropAndResizeFunctor_, (NDArray const *images, NDArray const *boxes, NDArray const *indices, NDArray const *cropSize, int method, double extrapolationVal, NDArray *crops), NUMERIC_TYPES_5, FLOAT_TYPES, INTEGER_TYPES); + } + } +} \ No newline at end of file diff --git a/libnd4j/include/ops/declarable/helpers/cpu/compilation_units/crop_and_resize_6.cpp b/libnd4j/include/ops/declarable/helpers/cpu/compilation_units/crop_and_resize_6.cpp new file mode 100644 index 000000000..8517a39e9 --- /dev/null +++ b/libnd4j/include/ops/declarable/helpers/cpu/compilation_units/crop_and_resize_6.cpp @@ -0,0 +1,30 @@ +/******************************************************************************* + * 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 + ******************************************************************************/ + +// +// @author sgazeos@gmail.com +// + +#include +#include "../crop_and_resize.hpp" + +namespace nd4j { + namespace ops { + namespace helpers { + BUILD_TRIPLE_TEMPLATE(template void cropAndResizeFunctor_, (NDArray const *images, NDArray const *boxes, NDArray const *indices, NDArray const *cropSize, int method, double extrapolationVal, NDArray *crops), NUMERIC_TYPES_6, FLOAT_TYPES, INTEGER_TYPES); + } + } +} \ No newline at end of file diff --git a/libnd4j/include/ops/declarable/helpers/cpu/compilation_units/crop_and_resize_7.cpp b/libnd4j/include/ops/declarable/helpers/cpu/compilation_units/crop_and_resize_7.cpp new file mode 100644 index 000000000..e12190170 --- /dev/null +++ b/libnd4j/include/ops/declarable/helpers/cpu/compilation_units/crop_and_resize_7.cpp @@ -0,0 +1,30 @@ +/******************************************************************************* + * 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 + ******************************************************************************/ + +// +// @author sgazeos@gmail.com +// + +#include +#include "../crop_and_resize.hpp" + +namespace nd4j { + namespace ops { + namespace helpers { + BUILD_TRIPLE_TEMPLATE(template void cropAndResizeFunctor_, (NDArray const *images, NDArray const *boxes, NDArray const *indices, NDArray const *cropSize, int method, double extrapolationVal, NDArray *crops), NUMERIC_TYPES_7, FLOAT_TYPES, INTEGER_TYPES); + } + } +} \ No newline at end of file diff --git a/libnd4j/include/ops/declarable/helpers/cpu/compilation_units/crop_and_resize_8.cpp b/libnd4j/include/ops/declarable/helpers/cpu/compilation_units/crop_and_resize_8.cpp new file mode 100644 index 000000000..c4ddd7066 --- /dev/null +++ b/libnd4j/include/ops/declarable/helpers/cpu/compilation_units/crop_and_resize_8.cpp @@ -0,0 +1,30 @@ +/******************************************************************************* + * 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 + ******************************************************************************/ + +// +// @author sgazeos@gmail.com +// + +#include +#include "../crop_and_resize.hpp" + +namespace nd4j { + namespace ops { + namespace helpers { + BUILD_TRIPLE_TEMPLATE(template void cropAndResizeFunctor_, (NDArray const *images, NDArray const *boxes, NDArray const *indices, NDArray const *cropSize, int method, double extrapolationVal, NDArray *crops), NUMERIC_TYPES_8, FLOAT_TYPES, INTEGER_TYPES); + } + } +} \ No newline at end of file diff --git a/libnd4j/include/ops/declarable/helpers/cpu/compilation_units/crop_and_resize_9.cpp b/libnd4j/include/ops/declarable/helpers/cpu/compilation_units/crop_and_resize_9.cpp new file mode 100644 index 000000000..38cf05787 --- /dev/null +++ b/libnd4j/include/ops/declarable/helpers/cpu/compilation_units/crop_and_resize_9.cpp @@ -0,0 +1,30 @@ +/******************************************************************************* + * 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 + ******************************************************************************/ + +// +// @author sgazeos@gmail.com +// + +#include +#include "../crop_and_resize.hpp" + +namespace nd4j { + namespace ops { + namespace helpers { + BUILD_TRIPLE_TEMPLATE(template void cropAndResizeFunctor_, (NDArray const *images, NDArray const *boxes, NDArray const *indices, NDArray const *cropSize, int method, double extrapolationVal, NDArray *crops), NUMERIC_TYPES_9, FLOAT_TYPES, INTEGER_TYPES); + } + } +} \ No newline at end of file diff --git a/libnd4j/include/ops/declarable/helpers/cpu/crop_and_resize.cpp b/libnd4j/include/ops/declarable/helpers/cpu/crop_and_resize.cpp new file mode 100644 index 000000000..233699163 --- /dev/null +++ b/libnd4j/include/ops/declarable/helpers/cpu/crop_and_resize.cpp @@ -0,0 +1,63 @@ +/******************************************************************************* + * Copyright (c) 2015-2018 Skymind, Inc. + * Copyright (c) 2019-2020 Konduit K.K. + * + * 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 + ******************************************************************************/ +/* Copyright 2016 The TensorFlow Authors. All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://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. +==============================================================================*/ + +// +// @author sgazeos@gmail.com +// + +#include +#include + +namespace nd4j { + namespace ops { + namespace helpers { + +// ------------------------------------------------------------------------------------------------------------------ // +// ------------------------------------------------------------------------------------------------------------------ // +// crop and resize helper functor: +// \@param context - launch context for operation +// \@param images - batch of images (4D tensor) with shape {batch, width, height, channels} with given type +// \@param boxes - float boxes for crop +// \@param indices - integer boxes indices for crop +// \@param cropSize - integer size (newWidth, newHeight) +// \@param method - one of bilinear (0) or nearest neighbour (1) interpolation algorithm +// \@param extrapolationVal - radix to increase/decrease image +// \@param crops - output image batch (4D with given type) +// + void + cropAndResizeFunctor(nd4j::LaunchContext * context, NDArray const *images, NDArray const *boxes, + NDArray const *indices, NDArray const *cropSize, + int method, double extrapolationVal, NDArray *crops) { + BUILD_TRIPLE_SELECTOR(images->dataType(), boxes->dataType(), indices->dataType(), cropAndResizeFunctor_, (images, boxes, indices, cropSize, method, extrapolationVal, crops), NUMERIC_TYPES, FLOAT_TYPES, INTEGER_TYPES); + } + } + } +} \ No newline at end of file diff --git a/libnd4j/include/ops/declarable/helpers/cpu/crop_and_resize.hpp b/libnd4j/include/ops/declarable/helpers/cpu/crop_and_resize.hpp new file mode 100644 index 000000000..ca30d73bd --- /dev/null +++ b/libnd4j/include/ops/declarable/helpers/cpu/crop_and_resize.hpp @@ -0,0 +1,123 @@ +/******************************************************************************* + * 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 + ******************************************************************************/ + +// +// @author sgazeos@gmail.com +// + +#include +#include + +namespace nd4j { + namespace ops { + namespace helpers { + template + void cropAndResizeFunctor_(NDArray const *images, NDArray const *boxes, NDArray const *indices, NDArray const *cropSize, int method, double extrapolationVal, NDArray *crops) { + const int batchSize = images->sizeAt(0); + const int imageHeight = images->sizeAt(1); + const int imageWidth = images->sizeAt(2); + + const int numBoxes = crops->sizeAt(0); + const int cropHeight = crops->sizeAt(1); + const int cropWidth = crops->sizeAt(2); + const int depth = crops->sizeAt(3); + + for (auto b = 0; b < numBoxes; ++b) { + T y1 = boxes->t(b, 0); + T x1 = boxes->t(b, 1); + T y2 = boxes->t(b, 2); + T x2 = boxes->t(b, 3); + + int bIn = indices->e(b); + if (bIn >= batchSize) { + continue; + } + + T heightScale = (cropHeight > 1) ? (y2 - y1) * (imageHeight - 1) / (cropHeight - 1) : T(0); + T widthScale = (cropWidth > 1) ? (x2 - x1) * (imageWidth - 1) / (cropWidth - 1) : T(0); + + auto func = PRAGMA_THREADS_FOR { + for (auto y = start; y < stop; y += increment) { + const float inY = (cropHeight > 1) + ? y1 * (imageHeight - 1) + y * heightScale + : 0.5 * (y1 + y2) * (imageHeight - 1); + + if (inY < 0 || inY > imageHeight - 1) { + for (auto x = 0; x < cropWidth; ++x) { + for (auto d = 0; d < depth; ++d) { + crops->p(b, y, x, d, extrapolationVal); + } + } + continue; + } + if (method == 0 /* bilinear */) { + const int topYIndex = nd4j::math::p_floor(inY); + const int bottomYIndex = nd4j::math::p_ceil(inY); + const float y_lerp = inY - topYIndex; + + for (auto x = 0; x < cropWidth; ++x) { + const float in_x = (cropWidth > 1) + ? x1 * (imageWidth - 1) + x * widthScale + : 0.5 * (x1 + x2) * (imageWidth - 1); + + if (in_x < 0 || in_x > imageWidth - 1) { + for (auto d = 0; d < depth; ++d) { + crops->p(b, y, x, d, extrapolationVal); + } + continue; + } + int left_x_index = math::p_floor(in_x); + int right_x_index = math::p_ceil(in_x); + T x_lerp = in_x - left_x_index; + + for (auto d = 0; d < depth; ++d) { + const float topLeft(images->e(bIn, topYIndex, left_x_index, d)); + const float topRight(images->e(bIn, topYIndex, right_x_index, d)); + const float bottomLeft(images->e(bIn, bottomYIndex, left_x_index, d)); + const float bottomRight(images->e(bIn, bottomYIndex, right_x_index, d)); + const float top = topLeft + (topRight - topLeft) * x_lerp; + const float bottom = bottomLeft + (bottomRight - bottomLeft) * x_lerp; + crops->p(b, y, x, d, top + (bottom - top) * y_lerp); + } + } + } else { // method is "nearest neighbor" + for (auto x = 0; x < cropWidth; ++x) { + const float inX = (cropWidth > 1) + ? x1 * (imageWidth - 1) + x * widthScale + : 0.5 * (x1 + x2) * (imageWidth - 1); + + if (inX < 0 || inX > imageWidth - 1) { + for (auto d = 0; d < depth; ++d) { + crops->p(b, y, x, d, extrapolationVal); + } + continue; + } + const int closestXIndex = roundf(inX); + const int closestYIndex = roundf(inY); + for (auto d = 0; d < depth; ++d) { + crops->p(b, y, x, d, images->e(bIn, closestYIndex, closestXIndex, d)); + } + } + } + } + }; + + samediff::Threads::parallel_for(func, 0, cropHeight); + } + } + } + } +} \ No newline at end of file diff --git a/libnd4j/include/ops/declarable/helpers/cpu/image_resize.cpp b/libnd4j/include/ops/declarable/helpers/cpu/image_resize.cpp index d4089359f..9d30ddcf7 100644 --- a/libnd4j/include/ops/declarable/helpers/cpu/image_resize.cpp +++ b/libnd4j/include/ops/declarable/helpers/cpu/image_resize.cpp @@ -366,102 +366,6 @@ namespace helpers { BUILD_SINGLE_SELECTOR(images->dataType(), return resizeNeighborFunctor_, (images, width, height, alignCorners, halfPixelCenter, output), LIBND4J_TYPES); } - - template - static void cropAndResizeFunctor_(NDArray const *images, NDArray const *boxes, NDArray const *indices, - NDArray const *cropSize, int method, double extrapolationVal, NDArray *crops) { - const int batchSize = images->sizeAt(0); - const int imageHeight = images->sizeAt(1); - const int imageWidth = images->sizeAt(2); - - const int numBoxes = crops->sizeAt(0); - const int cropHeight = crops->sizeAt(1); - const int cropWidth = crops->sizeAt(2); - const int depth = crops->sizeAt(3); - - for (auto b = 0; b < numBoxes; ++b) { - T y1 = boxes->t(b, 0); - T x1 = boxes->t(b, 1); - T y2 = boxes->t(b, 2); - T x2 = boxes->t(b, 3); - - int bIn = indices->e(b); - if (bIn >= batchSize) { - continue; - } - - T heightScale = (cropHeight > 1) ? (y2 - y1) * (imageHeight - 1) / (cropHeight - 1) : T(0); - T widthScale = (cropWidth > 1) ? (x2 - x1) * (imageWidth - 1) / (cropWidth - 1) : T(0); - - auto func = PRAGMA_THREADS_FOR { - for (auto y = start; y < stop; y += increment) { - const float inY = (cropHeight > 1) - ? y1 * (imageHeight - 1) + y * heightScale - : 0.5 * (y1 + y2) * (imageHeight - 1); - - if (inY < 0 || inY > imageHeight - 1) { - for (auto x = 0; x < cropWidth; ++x) { - for (auto d = 0; d < depth; ++d) { - crops->p(b, y, x, d, extrapolationVal); - } - } - continue; - } - if (method == 0 /* bilinear */) { - const int topYIndex = nd4j::math::p_floor(inY); - const int bottomYIndex = nd4j::math::p_ceil(inY); - const float y_lerp = inY - topYIndex; - - for (auto x = 0; x < cropWidth; ++x) { - const float in_x = (cropWidth > 1) - ? x1 * (imageWidth - 1) + x * widthScale - : 0.5 * (x1 + x2) * (imageWidth - 1); - - if (in_x < 0 || in_x > imageWidth - 1) { - for (auto d = 0; d < depth; ++d) { - crops->p(b, y, x, d, extrapolationVal); - } - continue; - } - int left_x_index = math::p_floor(in_x); - int right_x_index = math::p_ceil(in_x); - T x_lerp = in_x - left_x_index; - - for (auto d = 0; d < depth; ++d) { - const float topLeft(images->e(bIn, topYIndex, left_x_index, d)); - const float topRight(images->e(bIn, topYIndex, right_x_index, d)); - const float bottomLeft(images->e(bIn, bottomYIndex, left_x_index, d)); - const float bottomRight(images->e(bIn, bottomYIndex, right_x_index, d)); - const float top = topLeft + (topRight - topLeft) * x_lerp; - const float bottom = bottomLeft + (bottomRight - bottomLeft) * x_lerp; - crops->p(b, y, x, d, top + (bottom - top) * y_lerp); - } - } - } else { // method is "nearest neighbor" - for (auto x = 0; x < cropWidth; ++x) { - const float inX = (cropWidth > 1) - ? x1 * (imageWidth - 1) + x * widthScale - : 0.5 * (x1 + x2) * (imageWidth - 1); - - if (inX < 0 || inX > imageWidth - 1) { - for (auto d = 0; d < depth; ++d) { - crops->p(b, y, x, d, extrapolationVal); - } - continue; - } - const int closestXIndex = roundf(inX); - const int closestYIndex = roundf(inY); - for (auto d = 0; d < depth; ++d) { - crops->p(b, y, x, d, images->e(bIn, closestYIndex, closestXIndex, d)); - } - } - } - } - }; - - samediff::Threads::parallel_for(func, 0, cropHeight); - } - } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // ------------------------------------------------------------------------------------------------------------------ // // Bicubic interpolation @@ -1105,25 +1009,7 @@ namespace helpers { return ND4J_STATUS_OK; } -// ------------------------------------------------------------------------------------------------------------------ // -// ------------------------------------------------------------------------------------------------------------------ // -// crop and resize helper functor: -// \@param context - launch context for operation -// \@param images - batch of images (4D tensor) with shape {batch, width, height, channels} with given type -// \@param boxes - float boxes for crop -// \@param indices - integer boxes indices for crop -// \@param cropSize - integer size (newWidth, newHeight) -// \@param method - one of bilinear (0) or nearest neighbour (1) interpolation algorithm -// \@param extrapolationVal - radix to increase/decrease image -// \@param crops - output image batch (4D with given type) -// - void - cropAndResizeFunctor(nd4j::LaunchContext * context, NDArray const *images, NDArray const *boxes, - NDArray const *indices, NDArray const *cropSize, - int method, double extrapolationVal, NDArray *crops) { - BUILD_TRIPLE_SELECTOR(images->dataType(), boxes->dataType(), indices->dataType(), cropAndResizeFunctor_, - (images, boxes, indices, cropSize, method, extrapolationVal, crops), NUMERIC_TYPES, FLOAT_TYPES, INTEGER_TYPES); - } + } } } \ No newline at end of file diff --git a/libnd4j/include/ops/declarable/helpers/crop_and_resize.h b/libnd4j/include/ops/declarable/helpers/crop_and_resize.h new file mode 100644 index 000000000..3926dbfb0 --- /dev/null +++ b/libnd4j/include/ops/declarable/helpers/crop_and_resize.h @@ -0,0 +1,40 @@ +/******************************************************************************* + * 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 + ******************************************************************************/ + + +// +// @author sgazeos@gmail.com +// + +#ifndef SD_CROP_AND_RESIZE_H +#define SD_CROP_AND_RESIZE_H + +#include +#include + + +namespace nd4j { + namespace ops { + namespace helpers { + template + void cropAndResizeFunctor_(NDArray const *images, NDArray const *boxes, NDArray const *indices, NDArray const *cropSize, int method, double extrapolationVal, NDArray *crops); + + void cropAndResizeFunctor(nd4j::LaunchContext * context, NDArray const* images, NDArray const* boxes, NDArray const* indices, NDArray const* cropSize, int method, double extrapolationVal, NDArray* crops); + } + } +} + +#endif //SD_CROP_AND_RESIZE_H diff --git a/libnd4j/include/ops/declarable/helpers/cuda/image_resize.cu b/libnd4j/include/ops/declarable/helpers/cuda/image_resize.cu index c028daff3..6096f3a85 100644 --- a/libnd4j/include/ops/declarable/helpers/cuda/image_resize.cu +++ b/libnd4j/include/ops/declarable/helpers/cuda/image_resize.cu @@ -1326,7 +1326,7 @@ namespace helpers { // crops - output (4D tensor - [batch, outWidth, outHeight, pixels]) // template - static void cropAndResizeFunctor_(nd4j::LaunchContext* context, NDArray const *images, NDArray const *boxes, NDArray const *indices, + void cropAndResizeFunctor_(nd4j::LaunchContext* context, NDArray const *images, NDArray const *boxes, NDArray const *indices, NDArray const *cropSize, int method, double extrapolationVal, NDArray *crops) { const int batchSize = images->sizeAt(0); const int imageHeight = images->sizeAt(1); diff --git a/libnd4j/include/ops/declarable/helpers/image_resize.h b/libnd4j/include/ops/declarable/helpers/image_resize.h index 047b2cf70..decac3db9 100644 --- a/libnd4j/include/ops/declarable/helpers/image_resize.h +++ b/libnd4j/include/ops/declarable/helpers/image_resize.h @@ -50,9 +50,6 @@ namespace helpers { int resizeFunctor(nd4j::LaunchContext * context, NDArray const* image, int const width, int const height, ImageResizeMethods method, bool preserveAspectRatio, bool antialias, NDArray* output); - - void cropAndResizeFunctor(nd4j::LaunchContext * context, NDArray const* images, NDArray const* boxes, - NDArray const* indices, NDArray const* cropSize, int method, double extrapolationVal, NDArray* crops); } } } diff --git a/libnd4j/include/types/types.h b/libnd4j/include/types/types.h index 92fada8d3..7322c6bd5 100644 --- a/libnd4j/include/types/types.h +++ b/libnd4j/include/types/types.h @@ -159,6 +159,38 @@ (nd4j::DataType::INT64, Nd4jLong), \ (nd4j::DataType::BFLOAT16, bfloat16) +#define NUMERIC_TYPES_0 \ + (nd4j::DataType::HALF, float16) + +#define NUMERIC_TYPES_1 \ + (nd4j::DataType::FLOAT32, float) + +#define NUMERIC_TYPES_2 \ + (nd4j::DataType::DOUBLE, double) + +#define NUMERIC_TYPES_3 \ + (nd4j::DataType::INT8, int8_t), \ + (nd4j::DataType::BFLOAT16, bfloat16) + +#define NUMERIC_TYPES_4 \ + (nd4j::DataType::UINT8, uint8_t) + +#define NUMERIC_TYPES_5 \ + (nd4j::DataType::UINT16, uint16_t) + +#define NUMERIC_TYPES_6 \ + (nd4j::DataType::UINT32, uint32_t) + +#define NUMERIC_TYPES_7 \ + (nd4j::DataType::UINT64, uint64_t) + +#define NUMERIC_TYPES_8 \ + (nd4j::DataType::INT16, int16_t) + +#define NUMERIC_TYPES_9 \ + (nd4j::DataType::INT32, int32_t), \ + (nd4j::DataType::INT64, Nd4jLong) + #define GENERIC_NUMERIC_TYPES \ (nd4j::DataType::HALF, float16), \ diff --git a/libnd4j/tests_cpu/layers_tests/PlaygroundTests.cpp b/libnd4j/tests_cpu/layers_tests/PlaygroundTests.cpp index 3ceec003e..9ce56d463 100644 --- a/libnd4j/tests_cpu/layers_tests/PlaygroundTests.cpp +++ b/libnd4j/tests_cpu/layers_tests/PlaygroundTests.cpp @@ -65,7 +65,32 @@ TEST_F(PlaygroundTests, test_avx) { nd4j_printf("Optimal level: %i; Binary level: %i;\n", ::optimalLevel(), ::binaryLevel()); } -/* + +TEST_F(PlaygroundTests, test_biasAdd_1) { + auto x = NDArrayFactory::create('c', {512, 3072}); + auto y = NDArrayFactory::create('c', {3072}); + + std::vector values; + + nd4j::ops::biasadd op; + + for (int e = 0; e < 100; e++) { + auto timeStart = std::chrono::system_clock::now(); + + op.execute({&x, &y}, {&x}); + + auto timeEnd = std::chrono::system_clock::now(); + auto outerTime = std::chrono::duration_cast(timeEnd - timeStart).count(); + values.emplace_back(outerTime); + } + + std::sort(values.begin(), values.end()); + + nd4j_printf("Time: %lld us;\n", values[values.size() / 2]); +} + + + TEST_F(PlaygroundTests, test_bert_1) { // this test will run ONLY if this model exists if (nd4j::graph::getFileSize("/home/raver119/Downloads/Bert_minimal_model/bert_minimal_model.fb") < 0) @@ -86,15 +111,15 @@ TEST_F(PlaygroundTests, test_bert_1) { graph->getVariableSpace()->putVariable(86,0, u); graph->getVariableSpace()->putVariable(87,0, v); - +/* // validating graph now - // auto status = GraphExecutioner::execute(graph); - // ASSERT_EQ(Status::OK(), status); - // ASSERT_TRUE(graph->getVariableSpace()->hasVariable(198)); - - // auto array = graph->getVariableSpace()->getVariable(198)->getNDArray(); - // ASSERT_EQ(z, *array); + auto status = GraphExecutioner::execute(graph); + ASSERT_EQ(Status::OK(), status); + ASSERT_TRUE(graph->getVariableSpace()->hasVariable(198)); + auto array = graph->getVariableSpace()->getVariable(198)->getNDArray(); + ASSERT_EQ(z, *array); +*/ nd4j::Environment::getInstance()->setProfiling(true); auto profile = GraphProfilingHelper::profile(graph, 1); @@ -104,43 +129,47 @@ TEST_F(PlaygroundTests, test_bert_1) { nd4j::Environment::getInstance()->setProfiling(false); delete profile; +/* + std::vector values; - // std::vector values; + for (int e = 0; e < 1; e++) { + auto timeStart = std::chrono::system_clock::now(); - // for (int e = 0; e < 1; e++) { - // auto timeStart = std::chrono::system_clock::now(); + GraphExecutioner::execute(graph); - // GraphExecutioner::execute(graph); + auto timeEnd = std::chrono::system_clock::now(); + auto outerTime = std::chrono::duration_cast(timeEnd - timeStart).count(); + values.emplace_back(outerTime); + } - // auto timeEnd = std::chrono::system_clock::now(); - // auto outerTime = std::chrono::duration_cast(timeEnd - timeStart).count(); - // values.emplace_back(outerTime); - // } - - // std::sort(values.begin(), values.end()); - - // nd4j_printf("Time: %lld us;\n", values[values.size() / 2]); + std::sort(values.begin(), values.end()); + nd4j_printf("Time: %lld us;\n", values[values.size() / 2]); +*/ delete graph; } +/* TEST_F(PlaygroundTests, test_broadcast_1) { - int pool = 10; + int pool = 1000; std::vector aX(pool); std::vector aY(pool); std::vector aZ(pool); for (int e = 0; e < pool; e++) { - aX[e] = NDArrayFactory::create_('c', {64, 128, 1}); - aY[e] = NDArrayFactory::create_('c', {768}); - aZ[e] = NDArrayFactory::create_('c', {64, 128, 768}); + aX[e] = NDArrayFactory::create_('c', {512, 3072}); + aY[e] = NDArrayFactory::create_('c', {3072}); + aZ[e] = NDArrayFactory::create_('c', {512, 3072}); aX[e]->assign(119 * (e+1)); aY[e]->assign(119 * (e+3)); } std::vector values; + Context ctx(1); + + nd4j::ops::biasadd op; for (int e = 0; e < 1000; e++) { auto x = aX[e < pool ? e : e % pool]; @@ -149,7 +178,8 @@ TEST_F(PlaygroundTests, test_broadcast_1) { auto timeStart = std::chrono::system_clock::now(); - x->applyTrueBroadcast(BroadcastOpsTuple::Multiply(), *y, *z); + //op.execute({x, y}, {z}); + nd4j::ops::helpers::addBias(ctx, *x, *y, *z, false); auto timeEnd = std::chrono::system_clock::now(); auto outerTime = std::chrono::duration_cast(timeEnd - timeStart).count(); @@ -166,7 +196,7 @@ TEST_F(PlaygroundTests, test_broadcast_1) { delete aZ[e]; } } -*/ + /* TEST_F(PlaygroundTests, test_broadcast_1) { diff --git a/libnd4j/tests_cpu/libnd4j_tests/CMakeLists.txt b/libnd4j/tests_cpu/libnd4j_tests/CMakeLists.txt index a852a0c4c..fbba329e3 100644 --- a/libnd4j/tests_cpu/libnd4j_tests/CMakeLists.txt +++ b/libnd4j/tests_cpu/libnd4j_tests/CMakeLists.txt @@ -273,7 +273,7 @@ add_executable(runtests ${LOOPS_SOURCES} ../../blas/cpu/NativeOps.cpp ../../blas ../../include/cnpy/cnpy.cpp ../../include/nd4jmemset.h ../../include/nd4jmalloc.h ../../blas/Environment.cpp ../../blas/Environment.h ${EXEC_SOURCES} ${HELPERS_SOURCES} ${ARRAY_SOURCES} ${TYPES_SOURCES} ${MEMORY_SOURCES} ${GRAPH_SOURCES} ${CUSTOMOPS_SOURCES} ${EXCEPTIONS_SOURCES} ${INDEXING_SOURCES} ${CUSTOMOPS_PLATFORM_SOURCES} ${CUSTOMOPS_GENERIC_SOURCES} - ${OPS_SOURCES} ${TEST_SOURCES} ${PERF_SOURCES} ../../include/loops/cpu/compilation_units/TrueBroadcastHelper_1.cpp) + ${OPS_SOURCES} ${TEST_SOURCES} ${PERF_SOURCES}) target_link_libraries(runtests gtest ${MKLDNN} gtest_main ${BLAS_LIBRARIES})