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
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
//
|
2019-09-02 15:25:58 +02:00
|
|
|
// @author GS <sgazeos@gmail.com> 3/21/2018
|
|
|
|
// @author Yurii Shyrma (iuriish@yahoo.com)
|
2019-06-06 14:21:15 +02:00
|
|
|
//
|
|
|
|
|
|
|
|
#include <ops/declarable/CustomOperations.h>
|
2019-09-02 15:25:58 +02:00
|
|
|
#include <ops/declarable/helpers/matrixSetDiag.h>
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
namespace sd {
|
2019-09-02 15:25:58 +02:00
|
|
|
namespace ops {
|
|
|
|
|
|
|
|
CUSTOM_OP_IMPL(matrix_diag, 1, 1, false, 0, 0) {
|
|
|
|
|
|
|
|
auto diagonal = INPUT_VARIABLE(0);
|
|
|
|
auto output = OUTPUT_VARIABLE(0);
|
|
|
|
|
|
|
|
REQUIRE_TRUE(!diagonal->isScalar(), 0, "CUSTOM_OP matrix_diag: input diagonal array must be at list a vector, but scalar was given!");
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2019-09-02 15:25:58 +02:00
|
|
|
helpers::matrixSetDiag(block.launchContext(), *output, *diagonal, *output, true);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2019-09-02 15:25:58 +02:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
DECLARE_SHAPE_FN(matrix_diag) {
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2019-09-02 15:25:58 +02:00
|
|
|
Nd4jLong* outShapeInfo = nullptr;
|
|
|
|
auto in = inputShape->at(0);
|
|
|
|
int inRank = shape::rank(in);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2019-09-11 19:12:09 +02:00
|
|
|
// if for example diagonal array has shape [A,B,C] then output array has shape [A,B,C,C]
|
|
|
|
|
2019-09-02 15:25:58 +02:00
|
|
|
int outRank = inRank + 1;
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2019-09-02 15:25:58 +02:00
|
|
|
ALLOCATE(outShapeInfo, block.getWorkspace(), shape::shapeInfoLength(outRank), Nd4jLong);
|
|
|
|
outShapeInfo[0] = outRank;
|
|
|
|
for(int i = 0; i < inRank; ++i)
|
|
|
|
outShapeInfo[i + 1] = shape::sizeAt(in, i);
|
2019-09-11 19:12:09 +02:00
|
|
|
outShapeInfo[outRank] = shape::sizeAt(in, -1);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2019-09-02 15:25:58 +02:00
|
|
|
ShapeUtils::updateStridesAndType(outShapeInfo, in, shape::order(in));
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2019-09-02 15:25:58 +02:00
|
|
|
return SHAPELIST(CONSTANT(outShapeInfo));
|
|
|
|
}
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2019-09-02 15:25:58 +02:00
|
|
|
DECLARE_TYPES(matrix_diag) {
|
|
|
|
getOpDescriptor()
|
2020-03-02 10:49:41 +01:00
|
|
|
->setAllowedInputTypes(sd::DataType::ANY)
|
2019-09-02 15:25:58 +02:00
|
|
|
->setSameMode(true);
|
|
|
|
}
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|