cavis/libnd4j/include/ops/declarable/generic/linalg/matrix_band_part.cpp

72 lines
3.0 KiB
C++
Raw Normal View History

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
******************************************************************************/
//
// @author GS <sgazeos@gmail.com>, created on 8/22/2018
//
#include <system/op_boilerplate.h>
2019-06-06 14:21:15 +02:00
#if NOT_EXCLUDED(OP_matrix_band_part)
#include <ops/declarable/helpers/matrix_band.h>
#include <ops/declarable/CustomOperations.h>
namespace sd {
2019-06-06 14:21:15 +02:00
namespace ops {
Eliminated error with resize implementation. (#418) * Eliminated error with resize implementation. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored resize caller implementation. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored image.resize op helper. Signed-off-by: shugeo <sgazeos@gmail.com> * Added dumb implementations for missed resize methods. Signed-off-by: shugeo <sgazeos@gmail.com> * Added resize_images op. Refactored image_resize op. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored matrix_band_part op and test. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored resize_images op to comply with preserve_aspect_ratio flag properly. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored resize_images and tests for resizeArea method. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored resize methods and test. Signed-off-by: shugeo <sgazeos@gmail.com> * Added new methods for TF2 resize op. Signed-off-by: shugeo <sgazeos@gmail.com> * Portion of resize algorithms from TF2 Signed-off-by: shugeo <sgazeos@gmail.com> * Added routine to process resize with given algorithm. Signed-off-by: shugeo <sgazeos@gmail.com> * Added new image resize via scale and translate process helper. Signed-off-by: shugeo <sgazeos@gmail.com> * Cpu implementation for V2 image resize operation helpers. Signed-off-by: shugeo <sgazeos@gmail.com> * Added implementation for lancos5 algorithm of resize and test. Signed-off-by: shugeo <sgazeos@gmail.com> * Added prints for span computing. Signed-off-by: shugeo <sgazeos@gmail.com> * The first working implementation and tests for lancos5 resize. Signed-off-by: shugeo <sgazeos@gmail.com> * Eliminated waste prints. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored image_resize op and tests." Signed-off-by: shugeo <sgazeos@gmail.com> * Lanczos3 resize implementation and tests. Signed-off-by: shugeo <sgazeos@gmail.com> * Implemented bicubic resize algorithm and tests. Signed-off-by: shugeo <sgazeos@gmail.com> * Added a couple of tests and cosmetic changes with image resize helper. Signed-off-by: shugeo <sgazeos@gmail.com> * Added bilinear implementation for image resize. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored bicubic algorithm and also implement area and neighbor algoritms for image resize on cpu arch. Signed-off-by: shugeo <sgazeos@gmail.com> * Added a couple of tests for nearest neighbor and area resize. Signed-off-by: shugeo <sgazeos@gmail.com> * Cosmetic changes for cpu implementation and added cuda implementation for resize methods. Signed-off-by: shugeo <sgazeos@gmail.com> * Separated cuda implementation of v2 image resize. Signed-off-by: shugeo <sgazeos@gmail.com> * Added kernels for span calculation and span gathering with new image resize cuda implementation. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored cuda implementation of image resize kernels. Signed-off-by: shugeo <sgazeos@gmail.com> * Finished the first working implementation of image resize op and tests. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed resize_images and image_resize ops. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored shape construction and output validation. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed test to properly initalized with float. Signed-off-by: shugeo <sgazeos@gmail.com> * Added 3D input opotunity for resize ops. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed test for resize_images op. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed test and call for resize_images op. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored image_resize op output data type handling for nearest neighbors method and tests. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed issue with wrong resize method. Signed-off-by: shugeo <sgazeos@gmail.com> * Added checkup for wrong resize methods for resize ops. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored resize methods and test. Signed-off-by: shugeo <sgazeos@gmail.com> * Added output data type validation for given resize method. Signed-off-by: shugeo <sgazeos@gmail.com> * - ResizeMethod rearranged in order to match C++ side - minor test fix Signed-off-by: raver119@gmail.com <raver119@gmail.com> * Refactored resize_images op. Signed-off-by: shugeo <sgazeos@gmail.com> Co-authored-by: raver119@gmail.com <raver119@gmail.com>
2020-05-27 20:15:03 +02:00
CONFIGURABLE_OP_IMPL(matrix_band_part, 1, 1, true, 0, 0) {
2019-06-06 14:21:15 +02:00
auto input = INPUT_VARIABLE(0);
auto output = OUTPUT_VARIABLE(0);
Eliminated error with resize implementation. (#418) * Eliminated error with resize implementation. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored resize caller implementation. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored image.resize op helper. Signed-off-by: shugeo <sgazeos@gmail.com> * Added dumb implementations for missed resize methods. Signed-off-by: shugeo <sgazeos@gmail.com> * Added resize_images op. Refactored image_resize op. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored matrix_band_part op and test. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored resize_images op to comply with preserve_aspect_ratio flag properly. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored resize_images and tests for resizeArea method. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored resize methods and test. Signed-off-by: shugeo <sgazeos@gmail.com> * Added new methods for TF2 resize op. Signed-off-by: shugeo <sgazeos@gmail.com> * Portion of resize algorithms from TF2 Signed-off-by: shugeo <sgazeos@gmail.com> * Added routine to process resize with given algorithm. Signed-off-by: shugeo <sgazeos@gmail.com> * Added new image resize via scale and translate process helper. Signed-off-by: shugeo <sgazeos@gmail.com> * Cpu implementation for V2 image resize operation helpers. Signed-off-by: shugeo <sgazeos@gmail.com> * Added implementation for lancos5 algorithm of resize and test. Signed-off-by: shugeo <sgazeos@gmail.com> * Added prints for span computing. Signed-off-by: shugeo <sgazeos@gmail.com> * The first working implementation and tests for lancos5 resize. Signed-off-by: shugeo <sgazeos@gmail.com> * Eliminated waste prints. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored image_resize op and tests." Signed-off-by: shugeo <sgazeos@gmail.com> * Lanczos3 resize implementation and tests. Signed-off-by: shugeo <sgazeos@gmail.com> * Implemented bicubic resize algorithm and tests. Signed-off-by: shugeo <sgazeos@gmail.com> * Added a couple of tests and cosmetic changes with image resize helper. Signed-off-by: shugeo <sgazeos@gmail.com> * Added bilinear implementation for image resize. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored bicubic algorithm and also implement area and neighbor algoritms for image resize on cpu arch. Signed-off-by: shugeo <sgazeos@gmail.com> * Added a couple of tests for nearest neighbor and area resize. Signed-off-by: shugeo <sgazeos@gmail.com> * Cosmetic changes for cpu implementation and added cuda implementation for resize methods. Signed-off-by: shugeo <sgazeos@gmail.com> * Separated cuda implementation of v2 image resize. Signed-off-by: shugeo <sgazeos@gmail.com> * Added kernels for span calculation and span gathering with new image resize cuda implementation. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored cuda implementation of image resize kernels. Signed-off-by: shugeo <sgazeos@gmail.com> * Finished the first working implementation of image resize op and tests. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed resize_images and image_resize ops. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored shape construction and output validation. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed test to properly initalized with float. Signed-off-by: shugeo <sgazeos@gmail.com> * Added 3D input opotunity for resize ops. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed test for resize_images op. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed test and call for resize_images op. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored image_resize op output data type handling for nearest neighbors method and tests. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed issue with wrong resize method. Signed-off-by: shugeo <sgazeos@gmail.com> * Added checkup for wrong resize methods for resize ops. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored resize methods and test. Signed-off-by: shugeo <sgazeos@gmail.com> * Added output data type validation for given resize method. Signed-off-by: shugeo <sgazeos@gmail.com> * - ResizeMethod rearranged in order to match C++ side - minor test fix Signed-off-by: raver119@gmail.com <raver119@gmail.com> * Refactored resize_images op. Signed-off-by: shugeo <sgazeos@gmail.com> Co-authored-by: raver119@gmail.com <raver119@gmail.com>
2020-05-27 20:15:03 +02:00
Nd4jLong minLower(0LL);
Nd4jLong maxUpper(0LL);
if (block.width() == 1) {
REQUIRE_TRUE(block.numI() == 2, 0, "matrix_band_part: min and max band numbers should be given before.");
minLower = INT_ARG(0);
maxUpper = INT_ARG(1);
}
else {
REQUIRE_TRUE(block.width() == 3, 0, "matrix_band_part: min and max band numbers should be given as scalars before.");
auto minLowerT = INPUT_VARIABLE(1);
auto maxUpperT = INPUT_VARIABLE(2);
REQUIRE_TRUE(minLowerT->isScalar() && maxUpperT->isScalar(), 0, "matrix_band_part: min and max should be scalars, but %i and %i ranks given", minLowerT->rankOf(), maxUpperT->rankOf());
minLower = minLowerT->e<Nd4jLong>(0);
maxUpper = maxUpperT->e<Nd4jLong>(0);
}
2019-06-06 14:21:15 +02:00
REQUIRE_TRUE(input->rankOf() >= 2, 0, "matrix_band_part: Input rank should be 2 or greater.");
Nd4jLong N = input->sizeAt(-2);
Nd4jLong M = input->sizeAt(-1);
REQUIRE_TRUE(minLower > -N && minLower < N, 0, "matrix_band_part: lower diagonal count %i should be less than %i.",
minLower, N);
REQUIRE_TRUE(maxUpper > -M && maxUpper < M, 0, "matrix_band_part: upper diagonal count %i should be less than %i.",
maxUpper, M);
helpers::matrixBandPart(block.launchContext(), input, output, minLower, maxUpper);
return ND4J_STATUS_OK;
}
DECLARE_SYN(band_part, matrix_band_part);
}
DECLARE_TYPES(matrix_band_part) {
getOpDescriptor()
Eliminated error with resize implementation. (#418) * Eliminated error with resize implementation. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored resize caller implementation. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored image.resize op helper. Signed-off-by: shugeo <sgazeos@gmail.com> * Added dumb implementations for missed resize methods. Signed-off-by: shugeo <sgazeos@gmail.com> * Added resize_images op. Refactored image_resize op. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored matrix_band_part op and test. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored resize_images op to comply with preserve_aspect_ratio flag properly. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored resize_images and tests for resizeArea method. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored resize methods and test. Signed-off-by: shugeo <sgazeos@gmail.com> * Added new methods for TF2 resize op. Signed-off-by: shugeo <sgazeos@gmail.com> * Portion of resize algorithms from TF2 Signed-off-by: shugeo <sgazeos@gmail.com> * Added routine to process resize with given algorithm. Signed-off-by: shugeo <sgazeos@gmail.com> * Added new image resize via scale and translate process helper. Signed-off-by: shugeo <sgazeos@gmail.com> * Cpu implementation for V2 image resize operation helpers. Signed-off-by: shugeo <sgazeos@gmail.com> * Added implementation for lancos5 algorithm of resize and test. Signed-off-by: shugeo <sgazeos@gmail.com> * Added prints for span computing. Signed-off-by: shugeo <sgazeos@gmail.com> * The first working implementation and tests for lancos5 resize. Signed-off-by: shugeo <sgazeos@gmail.com> * Eliminated waste prints. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored image_resize op and tests." Signed-off-by: shugeo <sgazeos@gmail.com> * Lanczos3 resize implementation and tests. Signed-off-by: shugeo <sgazeos@gmail.com> * Implemented bicubic resize algorithm and tests. Signed-off-by: shugeo <sgazeos@gmail.com> * Added a couple of tests and cosmetic changes with image resize helper. Signed-off-by: shugeo <sgazeos@gmail.com> * Added bilinear implementation for image resize. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored bicubic algorithm and also implement area and neighbor algoritms for image resize on cpu arch. Signed-off-by: shugeo <sgazeos@gmail.com> * Added a couple of tests for nearest neighbor and area resize. Signed-off-by: shugeo <sgazeos@gmail.com> * Cosmetic changes for cpu implementation and added cuda implementation for resize methods. Signed-off-by: shugeo <sgazeos@gmail.com> * Separated cuda implementation of v2 image resize. Signed-off-by: shugeo <sgazeos@gmail.com> * Added kernels for span calculation and span gathering with new image resize cuda implementation. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored cuda implementation of image resize kernels. Signed-off-by: shugeo <sgazeos@gmail.com> * Finished the first working implementation of image resize op and tests. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed resize_images and image_resize ops. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored shape construction and output validation. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed test to properly initalized with float. Signed-off-by: shugeo <sgazeos@gmail.com> * Added 3D input opotunity for resize ops. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed test for resize_images op. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed test and call for resize_images op. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored image_resize op output data type handling for nearest neighbors method and tests. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed issue with wrong resize method. Signed-off-by: shugeo <sgazeos@gmail.com> * Added checkup for wrong resize methods for resize ops. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored resize methods and test. Signed-off-by: shugeo <sgazeos@gmail.com> * Added output data type validation for given resize method. Signed-off-by: shugeo <sgazeos@gmail.com> * - ResizeMethod rearranged in order to match C++ side - minor test fix Signed-off-by: raver119@gmail.com <raver119@gmail.com> * Refactored resize_images op. Signed-off-by: shugeo <sgazeos@gmail.com> Co-authored-by: raver119@gmail.com <raver119@gmail.com>
2020-05-27 20:15:03 +02:00
->setAllowedInputTypes(0, {ALL_INTS, ALL_FLOATS})
->setAllowedInputTypes(1, {ALL_INTS})
->setAllowedInputTypes(2, {ALL_INTS})
->setAllowedInputTypes({ALL_INTS, ALL_FLOATS});
2019-06-06 14:21:15 +02:00
}
}
#endif