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
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
//
|
|
|
|
// Created by GS <sgazeos@gmail.com> on 3/21/2018.
|
|
|
|
//
|
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
#include <array/ResultSet.h>
|
2019-06-06 14:21:15 +02:00
|
|
|
#include <ops/declarable/helpers/matrix_diag_part.h>
|
2020-03-02 10:49:41 +01:00
|
|
|
#include <graph/Status.h>
|
|
|
|
#include <helpers/ShapeUtils.h>
|
|
|
|
#include <helpers/ShapeUtils.h>
|
|
|
|
#include <helpers/TAD.h>
|
|
|
|
#include <exceptions/cuda_exception.h>
|
2019-06-06 14:21:15 +02:00
|
|
|
#include <helpers/ConstantTadHelper.h>
|
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
namespace sd {
|
2019-06-06 14:21:15 +02:00
|
|
|
namespace ops {
|
|
|
|
namespace helpers {
|
|
|
|
|
2019-09-11 20:04:43 +02:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// put diagonals from input batched matricies to output batched vectors
|
2019-06-06 14:21:15 +02:00
|
|
|
template <typename T>
|
|
|
|
static __global__ void matrixDiagPartKernel(void const* inputBuffer, void* outputBuffer, Nd4jLong numTads, Nd4jLong inputLength,
|
2020-05-09 07:06:14 +02:00
|
|
|
const Nd4jLong* tadOnlyInputShapeInfo, const Nd4jLong *tadInputOffsets,
|
|
|
|
const Nd4jLong* tadOnlyOutputShapeInfo, const Nd4jLong *tadOutputOffsets) {
|
2019-06-06 14:21:15 +02:00
|
|
|
int totalThreads = blockDim.x;
|
|
|
|
for (Nd4jLong i = blockIdx.x; i < numTads; i += gridDim.x) {
|
|
|
|
auto yOffset = tadInputOffsets[i];
|
|
|
|
auto xOffset = tadOutputOffsets[i];
|
|
|
|
for (Nd4jLong j = threadIdx.x; j < inputLength; j += totalThreads) {
|
|
|
|
Nd4jLong coords[2] = {j, j};
|
2019-09-11 19:12:09 +02:00
|
|
|
Nd4jLong tadOffset = shape::getOffset(tadOnlyInputShapeInfo, coords);
|
|
|
|
*(reinterpret_cast<T*>(outputBuffer) + xOffset + shape::getIndexOffset(j, tadOnlyOutputShapeInfo)) = *(reinterpret_cast<T const*>(inputBuffer) + yOffset + tadOffset);
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// Returns a batched matrix tensor with new batched diagonal values.
|
|
|
|
// for detailed explanations please take a look on web page: https://www.tensorflow.org/api_docs/python/tf/matrix_set_diag
|
2019-09-11 20:04:43 +02:00
|
|
|
//
|
2019-06-06 14:21:15 +02:00
|
|
|
template <typename T>
|
2020-03-02 10:49:41 +01:00
|
|
|
int _matrixDiagPart(sd::LaunchContext * context, const NDArray* input, NDArray* output) {
|
2019-06-06 14:21:15 +02:00
|
|
|
auto stream = context->getCudaStream();
|
|
|
|
auto listOut = output->allTensorsAlongDimension({output->rankOf() - 1});
|
|
|
|
auto listDiag = input->allTensorsAlongDimension({input->rankOf() - 2, input->rankOf() - 1});
|
|
|
|
|
2019-12-20 20:35:39 +01:00
|
|
|
if (listOut.size() != listDiag.size()) {
|
2019-06-06 14:21:15 +02:00
|
|
|
nd4j_printf("matrix_diag_part: Input matrix has wrong shape.", "");
|
|
|
|
return ND4J_STATUS_VALIDATION;
|
|
|
|
}
|
2020-03-02 10:49:41 +01:00
|
|
|
Nd4jLong lastDimension = sd::math::nd4j_min(input->sizeAt(-2), input->sizeAt(-1));
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
std::vector<int> dimsToExclude = ShapeUtils::evalDimsToExclude(output->rankOf(), {output->rankOf() - 1});
|
2020-05-09 07:06:14 +02:00
|
|
|
const Nd4jLong numTads = ShapeUtils::getNumOfSubArrs(input->shapeInfo(), dimsToExclude); //this->tensorsAlongDimension({dimension});
|
2019-06-06 14:21:15 +02:00
|
|
|
//printf("Repeat delta %lld, numTads %lld\n", repeatDelta, numTads);
|
|
|
|
//tadOnlyInputShapeInfo, tadInputOffsets, tadOnlyOutputShapeInfo, tadOutputOffsets;
|
|
|
|
std::vector<int> outputDims({output->rankOf() - 1});
|
|
|
|
std::vector<int> inputDims({input->rankOf() - 2, input->rankOf() - 1});
|
2020-06-06 14:26:55 +02:00
|
|
|
auto packX = sd::ConstantTadHelper::getInstance().tadForDimensions(input->shapeInfo(), inputDims);
|
|
|
|
auto packZ = sd::ConstantTadHelper::getInstance().tadForDimensions(output->shapeInfo(), outputDims);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
if (!output->isActualOnDeviceSide())
|
|
|
|
input->syncToDevice();
|
|
|
|
|
|
|
|
if (!input->isActualOnDeviceSide())
|
|
|
|
input->syncToDevice();
|
|
|
|
|
|
|
|
|
|
|
|
dim3 launchDims(256, 512, 8192);
|
2020-05-09 07:06:14 +02:00
|
|
|
matrixDiagPartKernel<T><<<launchDims.x, launchDims.y, launchDims.z, *stream>>>(input->specialBuffer(), output->specialBuffer(), numTads, lastDimension, packX.specialShapeInfo(), packX.specialOffsets(), packZ.specialShapeInfo(), packZ.specialOffsets());
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2019-09-11 20:04:43 +02:00
|
|
|
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// caller for _matrixDiagPart
|
|
|
|
//
|
2020-03-02 10:49:41 +01:00
|
|
|
int matrixDiagPart(sd::LaunchContext * context, const NDArray* input, NDArray* output) {
|
2019-06-06 14:21:15 +02:00
|
|
|
BUILD_SINGLE_SELECTOR(input->dataType(), return _matrixDiagPart, (context, input, output), LIBND4J_TYPES);
|
|
|
|
}
|
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
BUILD_SINGLE_TEMPLATE(template int _matrixDiagPart, (sd::LaunchContext * context, const NDArray* input, NDArray* output), LIBND4J_TYPES);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|