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 raver119@gmail.com
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <ops/declarable/helpers/top_k.h>
|
|
|
|
#include <MmulHelper.h>
|
|
|
|
#include <NDArrayFactory.h>
|
|
|
|
#include <Status.h>
|
Merge master to upstream (#7945)
* Shugeo strided slice zeros (#14)
* Modified strided_slice op to properly work with empty-like shapes.
* Fixed test for reduce_mean with empty-like input.
* [WIP] Last merge (#15)
* correct logsoftmax looss (#2)
* Small SameDiff listener fix (#4)
* Various fixes (#6)
* #7839 Fix for asXMatrix and tests
* #7866 EmbeddingSequenceLayer dtype fix + test
* #7856 SameDiff save/load stream methods
* #7859 RegressionEvaluation rank 4 fix + tests + axis configuration
* EvaluationBinary 3d/4d
* More evaluation 3d/4d tests
* #7847 Evaluation empty checks
* Small test ifx
* #7848 Fix median edge case
* Improve DL4J samediff layer tests
* [WIP] FastText wrapper implemented (#8)
* FastText implemented
* Some fixes
* Fix shapes for wordsNearest
* Validation of input vectors
* Fixes
* Fixed test
* Thread tagged
* Some tweaks
* setContextClassLoader for DeallocatorServiceThread
* Numpy format tests (#1)
* Various fixes (#11)
* #7852 SameDiff gather fix
* #7892 SameDiff placeholder to constant conversion
* #7890 validate input rank for MLN/CG init methods
* Fix broken permute shape calculation
* Permute and gather fixes
* Tests
* #7850 LogSumExp fix + test
* Handful of test fixes
* Empty arrays with non-scalar shapes (#10)
* minor rearrangements for lambdas
* empty tensors with non-scalar shapes
* numpy empty tensors with non-scalar shapes
* few more empty tweaks
* Small fixes
* conv3d signature update
* micro fix in batchnorm mkldnn
* Import fixes
* Fix
* MKL-DNN update
* Small fill fix
* fill with empty input + test
* Fixes
* Small error improvement
* Fix
* one special test
* couple of fixes for lstm
* Rewrite TFGraphMapper.getNDArrayFromTensor to be maintainable and less error prone
* Fixes
* FP16
* Unsigned
* BFloat16
* Fill op - empty tweaks
* - couple of fixes for empty arrays construction
- stack updated
* strided slice fix
* one transform test
* provide method for reducing shapeInfo in case of input array is empty
* Fixed reduceAlongDimensions to use empty input properly.
* couple of broadcast tests
* couple of tests broadcast tests + tweak to make them pass
* add check of non-empty to methods producing sub-arrays
* Fixed reshapeC with zeros in shape.
* complete empty check in reduce_... legacy ops
* Concat and cumsum/prod
* Tweak to empty shape inference on import
* add empty check to the rest of reduce legacy ops
* one more test
* correct typo in evalReduceShapeInfoEmpty
* Added tests for reduce_* ops to tests with zero shapes.
* few more tests for empty reductions
* Fixed strided_slice op with empty case and tests.
* one more empty reduction test
* Fixed strided_slice test.
* add empty check to NDArray::reshapei
* infOrMax
* empty min/max with infinity tests
* made unstack working correctly with empty arrays
* few IndexReduce tests + tweaks for empty shapes
* add test for empty concat
* few tests fixed
* Validation fix for reductions on empty shapes
* Reverse fix
* Reduction shape calc fixes
* SameDiff.generateOutputVariable: don't use shape function to determine number of outputs
* Range fix
* - NDArray constructor updated for scalars/empty arrays
- few tests fixed
* More fixes
* Empty creator fixes
* concat fix
* concat fix
* TF import tests: allow 'both all NaN' and 'both all inf' to pass
* Slice, zero fraction, and reshape fixes
* transpose, gather
* Zero fraction
* scalar cast fix
* Empty reduction axis support
* few more tests fixed
* Fixed input checks conforming with TF for concat op and tests.
* few tests fixed
* matmul scalar shape fix
* Fixed checkout for data type and scalarity with concat to allow non-empty scalars with vector concats.
* broadcast bool fix
* few more tests
* few more tests
* correct evalReduceShapeInfoEmpty
* argmax/argmin + tests
* one more empty edge case + one more test
* argmax/argmin/realdiv_bp tweaks
* empty reshape test + fix
* Helper fixes
* Small fixes
* Gather test fix
* Gather test fix
* Small fixes
* reduce scalar zero values
* scalar mean workaround
* Remove debug code
* along dim mean workaround
* one more test
* - equalsTo() tweak for empty arrays
- one more test
* broadcast tweaks
* [WIP] Fixing outstanding issues for NLP (#9)
* Avoid using not-inited objects
* Test fixed.
* Redundant method avoided for models like FastText
* KMeans++ implementation
* KMeans++ implementation
* Disable parallel execution
* KMeans++
* Tests
* Dev branch merge (#16)
* SameDiff: convertDataType and gradient check util improvements (#12)
* GradCheck util improvements
* StopGradient constructor + test
* SameDiff: Add datatype conversion
* Javadoc and add DataType.isNumerical()
* Small fix
* Fix SameDiff TF import test cases intermediate naming (workaround for bad default)
* TFGraphTestAllHelper: check intermediates in execution order
* Add missing debug listener
* [WIP] lstmBlock fix + other changes (#13)
- fixes lstmBlock issue
- changes NDArray method reshape(), permute(), transpose() by making them return instance instead of pointer
- CheckNumerics op
- fixes for ReduceBool IsInfOrNan & IsFinite
* Small test fix
* CheckNumerics op wrapper
* Fix some issues on master (#17)
* Fix DataVec test issue
* Fix issue with dl4j SameDiff output layer
* Dtype fix for lambda layers
* #7912 BertIterator dtype fix (use float32 not global default)
* [WIP] Next set of CUDA stuff (#7)
New CUDA implementations and improvements
* bad file
* Dev branch master merge (#23)
* SameDiff: convertDataType and gradient check util improvements (#12)
* GradCheck util improvements
* StopGradient constructor + test
* SameDiff: Add datatype conversion
* Javadoc and add DataType.isNumerical()
* Small fix
* Fix SameDiff TF import test cases intermediate naming (workaround for bad default)
* TFGraphTestAllHelper: check intermediates in execution order
* Add missing debug listener
* [WIP] lstmBlock fix + other changes (#13)
- fixes lstmBlock issue
- changes NDArray method reshape(), permute(), transpose() by making them return instance instead of pointer
- CheckNumerics op
- fixes for ReduceBool IsInfOrNan & IsFinite
* Small test fix
* CheckNumerics op wrapper
* Compatibility of deserialization (#18)
Signed-off-by: Alexander Stoyakin <alexander.stoyakin@gmail.com>
* SameDiff: add activation gradient checking support for debugging (#19)
* SameDiff gradient checker: first pass on activation gradient checks
* Fixes + tests for activation gradient checking
* Javadoc
* [WIP] Some nd4j data type corrections (#20)
* Adjust data type
* Set correct Data type.
* Size of proper data type.
* fix averaged cpu load (#22)
* SameDiff ops, TF import and fixes (#24)
* CheckNumerics tests + fixes + misc fixes
Signed-off-by: AlexDBlack <blacka101@gmail.com>
* Fake quant
Signed-off-by: AlexDBlack <blacka101@gmail.com>
* Fixes
Signed-off-by: AlexDBlack <blacka101@gmail.com>
* FakeQuantWithMinMaxArgs
Signed-off-by: AlexDBlack <blacka101@gmail.com>
* CheckNumerics fix
Signed-off-by: AlexDBlack <blacka101@gmail.com>
* Fix libnd4j ALL_INTS and ALL_FLOATS declaration (uint and bfloat types)
Signed-off-by: AlexDBlack <blacka101@gmail.com>
* Small fix
Signed-off-by: AlexDBlack <blacka101@gmail.com>
* Javadoc
Signed-off-by: AlexDBlack <blacka101@gmail.com>
* Exception tweak
Signed-off-by: AlexDBlack <blacka101@gmail.com>
* fix
Signed-off-by: AlexDBlack <blacka101@gmail.com>
* Fix for out of scope stack allocated var use
Signed-off-by: AlexDBlack <blacka101@gmail.com>
* Ignores
Signed-off-by: AlexDBlack <blacka101@gmail.com>
* Ignore for known failing test (already logged issue)
Signed-off-by: AlexDBlack <blacka101@gmail.com>
* Merge upstream to fork (#25)
* Add thousand-separator commas to TotalParams (#7915)
* Add thousand-separator commas to TotalParams
The number of parameters can be quite large, and it would help the reading of the summary printout to have the TotalParams column & values at the bottom have thousand-separator-commas in them.
* Add thousand-separator commas to MultiLayerNetwork
Corresponding change to MultiLayerNetwork
Signed-off-by: Jxtps Jxtps <jxtps435@gmail.com>
* Update contributing and issue/PR templates (#7934)
Signed-off-by: AlexDBlack <blacka101@gmail.com>
* Fix link to AdaDelta paper (#7942)
Fix link to AdaDelta paper hosted on matthewzeiler.com
Signed-off-by: Jxtps
* Fixes, and ignores for known/logged failing issues (#7943)
Signed-off-by: AlexDBlack <blacka101@gmail.com>
* SameDiff + DL4J/SameDiff: Multiple fixes (#28)
* #7919 HDF5 attribute buffer length fix
Signed-off-by: AlexDBlack <blacka101@gmail.com>
* #7909 Arbiter constructor exception ux improvements
Signed-off-by: AlexDBlack <blacka101@gmail.com>
* #7925 RNN output layer length checks
Signed-off-by: AlexDBlack <blacka101@gmail.com>
* #7939 Add listener for validating inputs are not incorrectly modified
Signed-off-by: AlexDBlack <blacka101@gmail.com>
* #7939 Integrate NonInplaceValidationListener into tests
* #7844 DL4J SameDiff fixes for variable minibatch size
* DL4J SameDiff fixes - ensure gradient for input placeholder is available
Signed-off-by: AlexDBlack <blacka101@gmail.com>
* Tweaks to ExternalErrorsFunction - use placeholders, make more robust
* Another fix
* More fixes
* More SameDiff/DL4J fixes
* Scope out scalar array creation in BaseScalarOp
* Remove debug code
Signed-off-by: AlexDBlack <blacka101@gmail.com>
* [WIP] Final dev branch merge (#29)
* SameDiff: convertDataType and gradient check util improvements (#12)
* GradCheck util improvements
* StopGradient constructor + test
* SameDiff: Add datatype conversion
* Javadoc and add DataType.isNumerical()
* Small fix
* Fix SameDiff TF import test cases intermediate naming (workaround for bad default)
* TFGraphTestAllHelper: check intermediates in execution order
* Add missing debug listener
* [WIP] lstmBlock fix + other changes (#13)
- fixes lstmBlock issue
- changes NDArray method reshape(), permute(), transpose() by making them return instance instead of pointer
- CheckNumerics op
- fixes for ReduceBool IsInfOrNan & IsFinite
* Small test fix
* CheckNumerics op wrapper
* Compatibility of deserialization (#18)
Signed-off-by: Alexander Stoyakin <alexander.stoyakin@gmail.com>
* SameDiff: add activation gradient checking support for debugging (#19)
* SameDiff gradient checker: first pass on activation gradient checks
* Fixes + tests for activation gradient checking
* Javadoc
* [WIP] Some nd4j data type corrections (#20)
* Adjust data type
* Set correct Data type.
* Size of proper data type.
* fix averaged cpu load (#22)
* [WIP] Multiple dataset iterators (#27)
* Splitting dataset into arbitrary number
* Fixes
* Multiple split of iterator
* Test
* Test
* Some fixes
* signature change
* one more tweak
Signed-off-by: raver119 <raver119@gmail.com>
* one more test for sequential use of DataSetIteratorSplitter
Signed-off-by: raver119 <raver119@gmail.com>
* Fixes
* Fixes
* one more test for Alexander
Signed-off-by: raver119 <raver119@gmail.com>
* Some fixes
* Some fixes
* one more test for Alexander
Signed-off-by: raver119 <raver119@gmail.com>
* minor test fix
Signed-off-by: raver119 <raver119@gmail.com>
* Some fixes
* Some fixes
* couple of assertions tweaked
Signed-off-by: raver119 <raver119@gmail.com>
* MDS splitter test :/
Signed-off-by: raver119 <raver119@gmail.com>
* Minor refactoring
* Multi dataset
* Some fixes
* More tests
* Small number of test fixes/improvements (failures on CI) (#31)
Signed-off-by: AlexDBlack <blacka101@gmail.com>
* [WIP] More CUDA stuff (#26)
* initial commit
Signed-off-by: raver119 <raver119@gmail.com>
* LRN BP CUDA
Signed-off-by: raver119 <raver119@gmail.com>
* less memory
Signed-off-by: raver119 <raver119@gmail.com>
* Fixed bug with crop_and_resize op helper.
* get rid of unnecessary index-calculation dunction
Signed-off-by: Yurii <yurii@skymind.io>
* Fixed sort with nth_element cuda-based helper.
* Refactored nth_element.
* Refactored nth_element op and tests.
* Modified usage of dim array with sortTad routine.
* Refactored main routine of helper for non_max_image_suppression op.
* non_max_image_suppression op helper with cuda kernel implementation. Initial revision.
* fix vol2col cuda kernel
* meh
Signed-off-by: raver119 <raver119@gmail.com>
* topK concept
Signed-off-by: raver119 <raver119@gmail.com>
* unsorted topK with scanWitdh of 1
Signed-off-by: raver119 <raver119@gmail.com>
* correct vol2col tests
* sorted/unsorted topK
Signed-off-by: raver119 <raver119@gmail.com>
* implementation and fixing col2im/col2vol
* Corrected usage flags with input/output with reverse op.
* dup is const now
Signed-off-by: raver119 <raver119@gmail.com>
* percentile op
Signed-off-by: raver119 <raver119@gmail.com>
* group tests for mapool2d
Signed-off-by: Yurii <yurii@skymind.io>
* special test for george
Signed-off-by: raver119 <raver119@gmail.com>
* less threads for sortTad
Signed-off-by: raver119 <raver119@gmail.com>
* provide conv2d for cuda
Signed-off-by: Yurii <yurii@skymind.io>
* remove auther in sort tad kernel code
Signed-off-by: Yurii <yurii@skymind.io>
* provide depthwise_conv2d for cuda
Signed-off-by: Yurii <yurii@skymind.io>
* - max_pooling_with_argmax
- null check for special use
Signed-off-by: raver119 <raver119@gmail.com>
* dts cuda
Signed-off-by: raver119 <raver119@gmail.com>
* provide sconv2d for cuda
Signed-off-by: Yurii <yurii@skymind.io>
* std cuda
Signed-off-by: raver119 <raver119@gmail.com>
* Refactored non_max_suppression op to conform TF implementation.
* Improved suppression helper.
* provide pooling3d for cuda
Signed-off-by: Yurii <yurii@skymind.io>
* minor lstm rearrangements
Signed-off-by: raver119 <raver119@gmail.com>
* more of minor lstm rearrangements
Signed-off-by: raver119 <raver119@gmail.com>
* (bi)dynamic_rnn
Signed-off-by: raver119 <raver119@gmail.com>
* templates init order
Signed-off-by: raver119 <raver119@gmail.com>
* Refactored non_max_suppression op.
* Added cuda kernel for non_max_suppression.
* CPU sort by key/value
Signed-off-by: raver119 <raver119@gmail.com>
* CPU sort TAD by key/value
Signed-off-by: raver119 <raver119@gmail.com>
* CPU sort TAD by key/value tests
Signed-off-by: raver119 <raver119@gmail.com>
* Eliminate compiler error with cuda implementation.
* - repaired gradCheck in cuda
- provide conv2d_bp for cuda
Signed-off-by: Yurii <yurii@skymind.io>
* missed signature
Signed-off-by: raver119 <raver119@gmail.com>
* provide depthwise_conv2d_bp for cuda
Signed-off-by: Yurii <yurii@skymind.io>
* Implementation of lup helper with cuda kernel. Initial commit.
* further work on backprops for convolutions
Signed-off-by: Yurii <yurii@skymind.io>
* CUDA linear sort by key/val
Signed-off-by: raver119 <raver119@gmail.com>
* CUDA tad sort by key/val
Signed-off-by: raver119 <raver119@gmail.com>
* start providing of backprop for pooling2d/3d
Signed-off-by: Yurii <yurii@skymind.io>
* Added atomicAdd for bool datatype.
* dynamic partition concept
Signed-off-by: raver119 <raver119@gmail.com>
* dynamic partition concept
Signed-off-by: raver119 <raver119@gmail.com>
* dynamic partition scalar CUDA
Signed-off-by: raver119 <raver119@gmail.com>
* important comment
Signed-off-by: raver119 <raver119@gmail.com>
* fix pooling2d/3d backprop helpers
Signed-off-by: Yurii <yurii@skymind.io>
* Added non-linear test with dynamic_partition.
* Improved test for dynamic_partition.
* dynamic_partition TAD concept
Signed-off-by: raver119 <raver119@gmail.com>
* - dynamic_partition TAD CUDA impl
- dynamic_partition TAD CPU fix
Signed-off-by: raver119 <raver119@gmail.com>
* - rewrite cpu code for usampling2d/3d
- write cuda code for usampling2d/3d
Signed-off-by: Yurii <yurii@skymind.io>
* dynamic_stitch CUDA vector case
Signed-off-by: raver119 <raver119@gmail.com>
* dynamic_stitch CUDA TAD case concept
Signed-off-by: raver119 <raver119@gmail.com>
* dynamic_stitch CUDA TAD case impl
Signed-off-by: raver119 <raver119@gmail.com>
* Added tests for dynamic_stitch 3D-4D cases.
* minor tests tweaks
Signed-off-by: raver119 <raver119@gmail.com>
* Fixed type check for dynamic stitch.
* min/max bp
Signed-off-by: raver119 <raver119@gmail.com>
* rewrite code for upsampling2d/3d cpu
Signed-off-by: Yurii <yurii@skymind.io>
* reduce min/max/norm_max bp
Signed-off-by: raver119 <raver119@gmail.com>
* lup implementation. Additional enhancements.
* provide code for upsamling2d/3d backprop
Signed-off-by: Yurii <yurii@skymind.io>
* weightedCrossEntropyWithLogits
Signed-off-by: raver119 <raver119@gmail.com>
* Fixed template math atomicMul for 64bit ints.
* Refactored dynamic_partition_bp op.
* inverseBroadcast fix
Signed-off-by: raver119 <raver119@gmail.com>
* DynamicPartitionBP test datatype fixed.
* - nd4j_atomicMul Windows fix
- cpu/NDArrayLambda.hpp excluded from CUDA
Signed-off-by: raver119 <raver119@gmail.com>
2019-06-27 17:37:04 +02:00
|
|
|
#include <ConstantTadHelper.h>
|
2019-07-12 10:51:51 +02:00
|
|
|
#include <ShapeUtils.h>
|
2019-12-20 15:56:28 +01:00
|
|
|
//#include <ops/declarable/generic/helpers/BroadcastHelper.h>
|
2019-07-12 10:51:51 +02:00
|
|
|
|
|
|
|
#include <cusolverDn.h>
|
|
|
|
#include <cuda_exception.h>
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
namespace nd4j {
|
|
|
|
namespace ops {
|
|
|
|
namespace helpers {
|
2019-09-09 15:27:45 +02:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------------------------ //
|
|
|
|
// invert the second diagonal for lower diagonal matrix
|
2019-08-23 18:20:50 +02:00
|
|
|
template<typename T>
|
|
|
|
static __global__ void
|
|
|
|
invertKernelLow(void *invertedBuf, Nd4jLong *invertedShape, void *inputBuf, Nd4jLong *inputShape, Nd4jLong n) {
|
2019-09-09 15:27:45 +02:00
|
|
|
T* inverted = reinterpret_cast<T *>(invertedBuf);
|
|
|
|
T* input = reinterpret_cast<T*>(inputBuf);
|
2019-07-12 10:51:51 +02:00
|
|
|
|
|
|
|
auto start = threadIdx.x + blockIdx.x * blockDim.x;
|
|
|
|
auto step = blockDim.x * gridDim.x;
|
|
|
|
|
|
|
|
for (int i = start + 1; i < n; i += step) {
|
|
|
|
Nd4jLong pos[] = {i, i - 1};
|
2019-07-20 07:58:44 +02:00
|
|
|
Nd4jLong posX[] = {i, i};
|
|
|
|
Nd4jLong posY[] = {i - 1, i - 1};
|
2019-09-11 19:12:09 +02:00
|
|
|
auto xIndex = shape::getOffset(inputShape, pos);
|
|
|
|
auto dxIndex = shape::getOffset(inputShape, posX);
|
|
|
|
auto dyIndex = shape::getOffset(inputShape, posY);
|
|
|
|
auto zIndex = shape::getOffset(invertedShape, pos);
|
2019-09-09 15:27:45 +02:00
|
|
|
// invert lower triangular matrix
|
2019-07-20 07:58:44 +02:00
|
|
|
inverted[zIndex] = -input[xIndex] / (input[dxIndex] * input[dyIndex]);
|
|
|
|
// math::atomics::nd4j_atomicAdd(&inverted[zIndex], - input[xIndex] * inverted[iIndex] / input[dIndex]);
|
2019-07-12 10:51:51 +02:00
|
|
|
}
|
|
|
|
}
|
2019-09-09 15:27:45 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------------------------ //
|
|
|
|
// invert diagonal vals to upper diagonal matrix
|
2019-08-23 18:20:50 +02:00
|
|
|
template<typename T>
|
|
|
|
static __global__ void
|
|
|
|
upvertKernel(void *invertedBuf, Nd4jLong *invertedShape, void *inputBuf, Nd4jLong *inputShape, Nd4jLong n) {
|
|
|
|
T *inverted = reinterpret_cast<T *>(invertedBuf);
|
|
|
|
T *input = reinterpret_cast<T *>(inputBuf);
|
2019-07-12 10:51:51 +02:00
|
|
|
|
|
|
|
auto start = threadIdx.x + blockIdx.x * blockDim.x;
|
|
|
|
auto step = blockDim.x * gridDim.x;
|
|
|
|
|
2019-07-20 07:58:44 +02:00
|
|
|
for (int i = start; i < n; i += step) {
|
2019-07-12 10:51:51 +02:00
|
|
|
Nd4jLong pos[] = {i, i};
|
2019-09-11 19:12:09 +02:00
|
|
|
auto xIndex = shape::getOffset(inputShape, pos);
|
|
|
|
auto zIndex = shape::getOffset(invertedShape, pos);
|
2019-07-20 07:58:44 +02:00
|
|
|
// math::atomics::nd4j_atomicDiv(&inverted[zIndex], input[xIndex]);
|
2019-09-09 15:27:45 +02:00
|
|
|
// invert diagonal elements
|
2019-07-12 10:51:51 +02:00
|
|
|
inverted[zIndex] /= input[xIndex];
|
|
|
|
}
|
|
|
|
}
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2019-09-09 15:27:45 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------------------------ //
|
|
|
|
// invert upper second diagonal
|
2019-08-23 18:20:50 +02:00
|
|
|
template<typename T>
|
|
|
|
static __global__ void
|
|
|
|
upvertKernelUp(void *invertedBuf, Nd4jLong *invertedShape, void *inputBuf, Nd4jLong *inputShape, Nd4jLong n) {
|
|
|
|
|
|
|
|
__shared__ T* inverted;
|
|
|
|
__shared__ T* input;
|
|
|
|
if (threadIdx.x == 0) {
|
|
|
|
inverted = reinterpret_cast<T *>(invertedBuf);
|
|
|
|
input = reinterpret_cast<T *>(inputBuf);
|
|
|
|
}
|
|
|
|
__syncthreads();
|
2019-07-12 10:51:51 +02:00
|
|
|
|
|
|
|
auto start = threadIdx.x + blockIdx.x * blockDim.x;
|
|
|
|
auto step = blockDim.x * gridDim.x;
|
|
|
|
|
2019-07-20 07:58:44 +02:00
|
|
|
for (int i = start; i < n - 1; i += step) {
|
2019-07-12 10:51:51 +02:00
|
|
|
Nd4jLong pos[] = {i, i + 1};
|
|
|
|
Nd4jLong posX[] = {i + 1, i + 1};
|
2019-09-11 19:12:09 +02:00
|
|
|
auto xIndex = shape::getOffset(inputShape, pos);
|
|
|
|
auto iIndex = shape::getOffset(invertedShape, posX);
|
|
|
|
auto zIndex = shape::getOffset(invertedShape, pos);
|
2019-09-09 15:27:45 +02:00
|
|
|
// invert upper matrix
|
2019-08-23 18:20:50 +02:00
|
|
|
math::atomics::nd4j_atomicAdd(&inverted[zIndex], -input[xIndex] * inverted[iIndex]); // / input[yIndex]);
|
2019-07-12 10:51:51 +02:00
|
|
|
//inputMatrix->t<T>(i, i + 1) * invertedMatrix->t<T>(i + 1, i + 1) / inputMatrix->t<T>(i, i)
|
|
|
|
}
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
|
2019-09-09 15:27:45 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------------------------ //
|
2019-08-23 18:20:50 +02:00
|
|
|
template<typename T>
|
|
|
|
static __global__ void
|
|
|
|
invertLowKernel(void *invertedBuf, Nd4jLong *invertedShape, void *inputBuf, Nd4jLong *inputShape, Nd4jLong n) {
|
2019-12-02 19:40:54 +01:00
|
|
|
|
2019-08-23 18:20:50 +02:00
|
|
|
T *inverted = reinterpret_cast<T *>(invertedBuf);
|
|
|
|
T *input = reinterpret_cast<T *>(inputBuf);
|
2019-12-02 19:40:54 +01:00
|
|
|
if (threadIdx.x == 0) {
|
|
|
|
inverted = reinterpret_cast<T *>(invertedBuf);
|
|
|
|
input = reinterpret_cast<T *>(inputBuf);
|
|
|
|
}
|
|
|
|
__syncthreads();
|
|
|
|
|
|
|
|
auto tid = blockIdx.x * blockDim.x + threadIdx.x;
|
|
|
|
auto step = gridDim.x * blockDim.x;
|
2019-07-12 10:51:51 +02:00
|
|
|
|
2019-12-02 19:40:54 +01:00
|
|
|
for (int i = tid + 2; i < n; i += step) {
|
2019-07-20 07:58:44 +02:00
|
|
|
for (int j = i - 2; j >= 0; --j)
|
2019-12-02 19:40:54 +01:00
|
|
|
for (int k = 0; k < i; k++) {
|
2019-07-12 10:51:51 +02:00
|
|
|
Nd4jLong posZ[] = {i, j};
|
2019-07-20 07:58:44 +02:00
|
|
|
Nd4jLong posY[] = {k, j};
|
|
|
|
Nd4jLong posX[] = {i, k};
|
|
|
|
Nd4jLong posD[] = {i, i};
|
2019-07-12 10:51:51 +02:00
|
|
|
|
2019-09-11 19:12:09 +02:00
|
|
|
auto xIndex = shape::getOffset(inputShape, posX);
|
|
|
|
auto yIndex = shape::getOffset(invertedShape, posY);
|
|
|
|
auto dIndex = shape::getOffset(inputShape, posD);
|
|
|
|
auto zIndex = shape::getOffset(invertedShape, posZ);
|
2019-09-09 15:27:45 +02:00
|
|
|
// invert non-diagonal elements
|
2019-08-23 18:20:50 +02:00
|
|
|
math::atomics::nd4j_atomicAdd(&inverted[zIndex], -inverted[yIndex] * input[xIndex] / input[dIndex]);
|
2019-07-12 10:51:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-09 15:27:45 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------------------------ //
|
|
|
|
// Invertion of upper triangular matrix non-diagonal elements when main and second diagonals already processed
|
2019-08-23 18:20:50 +02:00
|
|
|
template<typename T>
|
|
|
|
static __global__ void
|
|
|
|
invertUpKernel(void *invertedBuf, Nd4jLong *invertedShape, void *inputBuf, Nd4jLong *inputShape, Nd4jLong n) {
|
|
|
|
__shared__ T* inverted;
|
|
|
|
__shared__ T* input;
|
|
|
|
|
|
|
|
if (threadIdx.x == 0) {
|
|
|
|
inverted = reinterpret_cast<T *>(invertedBuf);;
|
|
|
|
input = reinterpret_cast<T *>(inputBuf);
|
|
|
|
}
|
|
|
|
__syncthreads();
|
2019-12-02 19:40:54 +01:00
|
|
|
auto tid = blockIdx.x * blockDim.x + threadIdx.x;
|
|
|
|
auto step = blockDim.x * gridDim.x;
|
2019-07-12 10:51:51 +02:00
|
|
|
|
2019-12-02 19:40:54 +01:00
|
|
|
for (int i = (int)n - tid - 2; i >= 0; i -= step) {
|
2019-08-23 18:20:50 +02:00
|
|
|
for (int j = i + 2; j < (int)n; j++)
|
2019-12-02 19:40:54 +01:00
|
|
|
for (int k = i; k < (int)n; k++) {
|
2019-07-12 10:51:51 +02:00
|
|
|
Nd4jLong posZ[] = {i, j};
|
|
|
|
Nd4jLong posY[] = {k, j};
|
|
|
|
Nd4jLong posX[] = {i, k};
|
2019-09-09 15:27:45 +02:00
|
|
|
// inversion with Joardan Gauss transformation
|
2019-09-11 19:12:09 +02:00
|
|
|
auto xIndex = shape::getOffset(inputShape, posX);
|
|
|
|
auto yIndex = shape::getOffset(invertedShape, posY);
|
|
|
|
auto zIndex = shape::getOffset(invertedShape, posZ);
|
2019-09-09 15:27:45 +02:00
|
|
|
// invert upper non-diagonal elements
|
|
|
|
math::atomics::nd4j_atomicAdd(&inverted[zIndex], -inverted[yIndex] * input[xIndex]);
|
2019-07-12 10:51:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-09 15:27:45 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------------------------ //
|
|
|
|
// procedure to invert lower-triangular matrix.
|
|
|
|
// In current case lower triangular matrix has main diagonal with general values
|
|
|
|
//
|
2019-08-23 18:20:50 +02:00
|
|
|
template<typename T>
|
2019-08-24 15:59:30 +02:00
|
|
|
static void invertLowerMatrix_(LaunchContext *context, NDArray *inputMatrix, NDArray *invertedMatrix) {
|
2019-07-12 10:51:51 +02:00
|
|
|
int n = inputMatrix->rows();
|
|
|
|
invertedMatrix->setIdentity();
|
|
|
|
|
|
|
|
if (inputMatrix->isIdentityMatrix()) return;
|
2019-08-23 18:20:50 +02:00
|
|
|
|
2019-08-24 15:59:30 +02:00
|
|
|
auto stream = context->getCudaStream();
|
2019-07-12 10:51:51 +02:00
|
|
|
|
2019-09-09 15:27:45 +02:00
|
|
|
// invert lower matrix
|
2019-07-20 07:58:44 +02:00
|
|
|
// invert main diagonal
|
2019-08-24 15:59:30 +02:00
|
|
|
upvertKernel<T><<<1, n, 512, *stream>>>(invertedMatrix->specialBuffer(), invertedMatrix->specialShapeInfo(), inputMatrix->specialBuffer(), inputMatrix->specialShapeInfo(), n);
|
2019-07-20 07:58:44 +02:00
|
|
|
// invert the second diagonal
|
2019-08-24 15:59:30 +02:00
|
|
|
invertKernelLow<T><<<1, n, 512, *stream>>>(invertedMatrix->specialBuffer(), invertedMatrix->specialShapeInfo(), inputMatrix->specialBuffer(), inputMatrix->specialShapeInfo(), n);
|
2019-09-09 15:27:45 +02:00
|
|
|
// invert non-diagonal elements
|
2019-08-24 15:59:30 +02:00
|
|
|
invertLowKernel<T><<<n, n, 512, *stream>>>(invertedMatrix->specialBuffer(), invertedMatrix->specialShapeInfo(), inputMatrix->specialBuffer(), inputMatrix->specialShapeInfo(), n);
|
2019-07-12 10:51:51 +02:00
|
|
|
}
|
|
|
|
|
2019-09-09 15:27:45 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------------------------ //
|
|
|
|
// caller for invert lower matrix routine
|
2019-08-24 15:59:30 +02:00
|
|
|
void invertLowerMatrix(LaunchContext *context, NDArray *inputMatrix, NDArray *invertedMatrix) {
|
2019-08-21 19:18:29 +02:00
|
|
|
NDArray::prepareSpecialUse({invertedMatrix}, {inputMatrix});
|
2019-08-24 15:59:30 +02:00
|
|
|
BUILD_SINGLE_SELECTOR(inputMatrix->dataType(), invertLowerMatrix_, (context, inputMatrix, invertedMatrix), FLOAT_NATIVE);
|
2019-08-21 19:18:29 +02:00
|
|
|
NDArray::registerSpecialUse({invertedMatrix}, {inputMatrix});
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
|
2019-09-09 15:27:45 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------------------------ //
|
|
|
|
// procedure to invert upper-triangular matrix.
|
|
|
|
// In current case upper triangular matrix has main diagonal with all ones on it.
|
2019-08-23 18:20:50 +02:00
|
|
|
template<typename T>
|
2019-08-24 15:59:30 +02:00
|
|
|
static void invertUpperMatrix_(LaunchContext *context, NDArray* inputMatrix, NDArray* invertedMatrix) {
|
2019-07-12 10:51:51 +02:00
|
|
|
int n = inputMatrix->rows();
|
|
|
|
invertedMatrix->setIdentity();
|
2019-08-24 15:59:30 +02:00
|
|
|
auto stream = context->getCudaStream();
|
2019-07-12 10:51:51 +02:00
|
|
|
if (inputMatrix->isIdentityMatrix()) { // the inverse for I is I
|
|
|
|
return;
|
|
|
|
}
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2019-09-09 15:27:45 +02:00
|
|
|
// invert upper matrix
|
|
|
|
// invert the second diagonal
|
2019-08-23 18:20:50 +02:00
|
|
|
upvertKernelUp<T><<<1, n, 512, *stream >>>(invertedMatrix->specialBuffer(), invertedMatrix->specialShapeInfo(),
|
|
|
|
inputMatrix->specialBuffer(), inputMatrix->specialShapeInfo(), n);
|
2019-09-09 15:27:45 +02:00
|
|
|
|
|
|
|
// invert other elements
|
2019-08-24 15:59:30 +02:00
|
|
|
invertUpKernel<T><<<n, n, 512, *stream >>>(invertedMatrix->specialBuffer(), invertedMatrix->specialShapeInfo(),inputMatrix->specialBuffer(), inputMatrix->specialShapeInfo(), n);
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
|
2019-09-09 15:27:45 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------------------------ //
|
|
|
|
// invertion of upper triangular matrix - runner routine
|
2019-08-24 15:59:30 +02:00
|
|
|
void invertUpperMatrix(LaunchContext *context, NDArray *inputMatrix, NDArray *invertedMatrix) {
|
2019-08-21 19:18:29 +02:00
|
|
|
NDArray::prepareSpecialUse({invertedMatrix}, {inputMatrix});
|
2019-08-24 15:59:30 +02:00
|
|
|
BUILD_SINGLE_SELECTOR(invertedMatrix->dataType(), invertUpperMatrix_, (context, inputMatrix, invertedMatrix), FLOAT_NATIVE);
|
2019-08-21 19:18:29 +02:00
|
|
|
NDArray::prepareSpecialUse({invertedMatrix}, {inputMatrix});
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
|
2019-09-09 15:27:45 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------------------------ //
|
|
|
|
// determinant kernel - accumulation product of all values on the main diagonal
|
|
|
|
template<typename T>
|
|
|
|
static __global__ void determinantKernel(T *compound, T *result, Nd4jLong len) {
|
|
|
|
auto start = blockIdx.x * blockDim.x + threadIdx.x;
|
|
|
|
auto step = blockDim.x * gridDim.x;
|
|
|
|
for (auto i = start; i < len; i += step) {
|
|
|
|
auto pos = i * len + i; //shape::getOffset(0, shape::shapeOf(shape), shape::stride(shape), di, 2);
|
|
|
|
// multiply all diagonal elements
|
|
|
|
math::atomics::nd4j_atomicMul(&result[0], compound[pos]);
|
2019-07-12 10:51:51 +02:00
|
|
|
}
|
2019-09-09 15:27:45 +02:00
|
|
|
}
|
2019-07-12 10:51:51 +02:00
|
|
|
|
2019-09-09 15:27:45 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------------------------ //
|
|
|
|
// determinant logarithm - accumulation sum of all logarithm values on the main diagonal. All in logarithic values
|
|
|
|
// should be positive
|
|
|
|
template<typename T>
|
|
|
|
static __global__ void determinantLogKernel(T *compound, T *result, Nd4jLong len) {
|
|
|
|
auto start = blockIdx.x * blockDim.x + threadIdx.x;
|
|
|
|
auto step = blockDim.x * gridDim.x;
|
|
|
|
for (auto i = start; i < len; i += step) {
|
|
|
|
auto pos = i * len + i; //shape::getOffset(0, shape::shapeOf(shape), shape::stride(shape), di, 2);
|
|
|
|
// sum logs of all diagonal elements
|
|
|
|
math::atomics::nd4j_atomicAdd(result, math::nd4j_log<T,T>(math::nd4j_abs(compound[pos])));
|
2019-07-12 10:51:51 +02:00
|
|
|
}
|
2019-09-09 15:27:45 +02:00
|
|
|
}
|
2019-07-12 10:51:51 +02:00
|
|
|
|
2019-09-09 15:27:45 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------------------------ //
|
|
|
|
// kernel to copy matrix with given shape to compound tensor with given pos
|
|
|
|
// output - a N-D tensor buffer with rank not less than 2, input - 2D square n x n matrix with n = rowLen
|
|
|
|
template<typename T, typename F>
|
|
|
|
static __global__ void
|
|
|
|
fillMatrix(void *output, Nd4jLong *outShape, void *input, Nd4jLong *inputShape, Nd4jLong pos, Nd4jLong rowLen) {
|
|
|
|
__shared__ F *matrix;
|
|
|
|
__shared__ T *inputBuf;
|
|
|
|
__shared__ Nd4jLong inputLen;
|
|
|
|
__shared__ Nd4jLong n2;
|
2019-07-20 07:58:44 +02:00
|
|
|
|
2019-09-09 15:27:45 +02:00
|
|
|
if (threadIdx.x == 0) {
|
|
|
|
matrix = reinterpret_cast<F*>(output);
|
|
|
|
inputBuf = reinterpret_cast<T*>(input);
|
|
|
|
inputLen = shape::length(inputShape);
|
|
|
|
n2 = rowLen * rowLen;
|
2019-07-20 07:58:44 +02:00
|
|
|
}
|
2019-09-09 15:27:45 +02:00
|
|
|
__syncthreads();
|
2019-07-20 07:58:44 +02:00
|
|
|
|
2019-09-09 15:27:45 +02:00
|
|
|
auto start = blockIdx.x * blockDim.x + threadIdx.x;
|
|
|
|
auto step = blockDim.x * gridDim.x;
|
2019-07-12 10:51:51 +02:00
|
|
|
|
2019-09-09 15:27:45 +02:00
|
|
|
for (int k = pos + start, j = start; j < n2; k += step, j += step) {
|
2019-09-11 19:12:09 +02:00
|
|
|
auto xIndex = shape::getIndexOffset(k, inputShape);
|
2019-09-09 15:27:45 +02:00
|
|
|
matrix[j] = (F) inputBuf[xIndex];
|
2019-07-12 10:51:51 +02:00
|
|
|
}
|
2019-09-09 15:27:45 +02:00
|
|
|
}
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2019-09-09 15:27:45 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------------------------ //
|
|
|
|
// same as above, but without type conversion
|
|
|
|
template<typename T>
|
|
|
|
static __global__ void
|
|
|
|
returnMatrix(void *output, Nd4jLong *outputShape, void *input, Nd4jLong *inputShape, Nd4jLong pos, Nd4jLong rowLen) {
|
|
|
|
__shared__ T* matrix;
|
|
|
|
__shared__ T* outputBuf;
|
|
|
|
__shared__ Nd4jLong outputLen;
|
|
|
|
__shared__ Nd4jLong n2;
|
|
|
|
|
|
|
|
if (threadIdx.x == 0) {
|
|
|
|
matrix = reinterpret_cast<T *>(input);
|
|
|
|
outputBuf = reinterpret_cast<T *>(output);
|
|
|
|
outputLen = shape::length(inputShape);
|
|
|
|
n2 = rowLen * rowLen;
|
2019-07-12 10:51:51 +02:00
|
|
|
}
|
2019-09-09 15:27:45 +02:00
|
|
|
__syncthreads();
|
|
|
|
auto start = blockIdx.x * blockDim.x + threadIdx.x;
|
|
|
|
auto step = blockDim.x * gridDim.x;
|
Merge master to upstream (#7945)
* Shugeo strided slice zeros (#14)
* Modified strided_slice op to properly work with empty-like shapes.
* Fixed test for reduce_mean with empty-like input.
* [WIP] Last merge (#15)
* correct logsoftmax looss (#2)
* Small SameDiff listener fix (#4)
* Various fixes (#6)
* #7839 Fix for asXMatrix and tests
* #7866 EmbeddingSequenceLayer dtype fix + test
* #7856 SameDiff save/load stream methods
* #7859 RegressionEvaluation rank 4 fix + tests + axis configuration
* EvaluationBinary 3d/4d
* More evaluation 3d/4d tests
* #7847 Evaluation empty checks
* Small test ifx
* #7848 Fix median edge case
* Improve DL4J samediff layer tests
* [WIP] FastText wrapper implemented (#8)
* FastText implemented
* Some fixes
* Fix shapes for wordsNearest
* Validation of input vectors
* Fixes
* Fixed test
* Thread tagged
* Some tweaks
* setContextClassLoader for DeallocatorServiceThread
* Numpy format tests (#1)
* Various fixes (#11)
* #7852 SameDiff gather fix
* #7892 SameDiff placeholder to constant conversion
* #7890 validate input rank for MLN/CG init methods
* Fix broken permute shape calculation
* Permute and gather fixes
* Tests
* #7850 LogSumExp fix + test
* Handful of test fixes
* Empty arrays with non-scalar shapes (#10)
* minor rearrangements for lambdas
* empty tensors with non-scalar shapes
* numpy empty tensors with non-scalar shapes
* few more empty tweaks
* Small fixes
* conv3d signature update
* micro fix in batchnorm mkldnn
* Import fixes
* Fix
* MKL-DNN update
* Small fill fix
* fill with empty input + test
* Fixes
* Small error improvement
* Fix
* one special test
* couple of fixes for lstm
* Rewrite TFGraphMapper.getNDArrayFromTensor to be maintainable and less error prone
* Fixes
* FP16
* Unsigned
* BFloat16
* Fill op - empty tweaks
* - couple of fixes for empty arrays construction
- stack updated
* strided slice fix
* one transform test
* provide method for reducing shapeInfo in case of input array is empty
* Fixed reduceAlongDimensions to use empty input properly.
* couple of broadcast tests
* couple of tests broadcast tests + tweak to make them pass
* add check of non-empty to methods producing sub-arrays
* Fixed reshapeC with zeros in shape.
* complete empty check in reduce_... legacy ops
* Concat and cumsum/prod
* Tweak to empty shape inference on import
* add empty check to the rest of reduce legacy ops
* one more test
* correct typo in evalReduceShapeInfoEmpty
* Added tests for reduce_* ops to tests with zero shapes.
* few more tests for empty reductions
* Fixed strided_slice op with empty case and tests.
* one more empty reduction test
* Fixed strided_slice test.
* add empty check to NDArray::reshapei
* infOrMax
* empty min/max with infinity tests
* made unstack working correctly with empty arrays
* few IndexReduce tests + tweaks for empty shapes
* add test for empty concat
* few tests fixed
* Validation fix for reductions on empty shapes
* Reverse fix
* Reduction shape calc fixes
* SameDiff.generateOutputVariable: don't use shape function to determine number of outputs
* Range fix
* - NDArray constructor updated for scalars/empty arrays
- few tests fixed
* More fixes
* Empty creator fixes
* concat fix
* concat fix
* TF import tests: allow 'both all NaN' and 'both all inf' to pass
* Slice, zero fraction, and reshape fixes
* transpose, gather
* Zero fraction
* scalar cast fix
* Empty reduction axis support
* few more tests fixed
* Fixed input checks conforming with TF for concat op and tests.
* few tests fixed
* matmul scalar shape fix
* Fixed checkout for data type and scalarity with concat to allow non-empty scalars with vector concats.
* broadcast bool fix
* few more tests
* few more tests
* correct evalReduceShapeInfoEmpty
* argmax/argmin + tests
* one more empty edge case + one more test
* argmax/argmin/realdiv_bp tweaks
* empty reshape test + fix
* Helper fixes
* Small fixes
* Gather test fix
* Gather test fix
* Small fixes
* reduce scalar zero values
* scalar mean workaround
* Remove debug code
* along dim mean workaround
* one more test
* - equalsTo() tweak for empty arrays
- one more test
* broadcast tweaks
* [WIP] Fixing outstanding issues for NLP (#9)
* Avoid using not-inited objects
* Test fixed.
* Redundant method avoided for models like FastText
* KMeans++ implementation
* KMeans++ implementation
* Disable parallel execution
* KMeans++
* Tests
* Dev branch merge (#16)
* SameDiff: convertDataType and gradient check util improvements (#12)
* GradCheck util improvements
* StopGradient constructor + test
* SameDiff: Add datatype conversion
* Javadoc and add DataType.isNumerical()
* Small fix
* Fix SameDiff TF import test cases intermediate naming (workaround for bad default)
* TFGraphTestAllHelper: check intermediates in execution order
* Add missing debug listener
* [WIP] lstmBlock fix + other changes (#13)
- fixes lstmBlock issue
- changes NDArray method reshape(), permute(), transpose() by making them return instance instead of pointer
- CheckNumerics op
- fixes for ReduceBool IsInfOrNan & IsFinite
* Small test fix
* CheckNumerics op wrapper
* Fix some issues on master (#17)
* Fix DataVec test issue
* Fix issue with dl4j SameDiff output layer
* Dtype fix for lambda layers
* #7912 BertIterator dtype fix (use float32 not global default)
* [WIP] Next set of CUDA stuff (#7)
New CUDA implementations and improvements
* bad file
* Dev branch master merge (#23)
* SameDiff: convertDataType and gradient check util improvements (#12)
* GradCheck util improvements
* StopGradient constructor + test
* SameDiff: Add datatype conversion
* Javadoc and add DataType.isNumerical()
* Small fix
* Fix SameDiff TF import test cases intermediate naming (workaround for bad default)
* TFGraphTestAllHelper: check intermediates in execution order
* Add missing debug listener
* [WIP] lstmBlock fix + other changes (#13)
- fixes lstmBlock issue
- changes NDArray method reshape(), permute(), transpose() by making them return instance instead of pointer
- CheckNumerics op
- fixes for ReduceBool IsInfOrNan & IsFinite
* Small test fix
* CheckNumerics op wrapper
* Compatibility of deserialization (#18)
Signed-off-by: Alexander Stoyakin <alexander.stoyakin@gmail.com>
* SameDiff: add activation gradient checking support for debugging (#19)
* SameDiff gradient checker: first pass on activation gradient checks
* Fixes + tests for activation gradient checking
* Javadoc
* [WIP] Some nd4j data type corrections (#20)
* Adjust data type
* Set correct Data type.
* Size of proper data type.
* fix averaged cpu load (#22)
* SameDiff ops, TF import and fixes (#24)
* CheckNumerics tests + fixes + misc fixes
Signed-off-by: AlexDBlack <blacka101@gmail.com>
* Fake quant
Signed-off-by: AlexDBlack <blacka101@gmail.com>
* Fixes
Signed-off-by: AlexDBlack <blacka101@gmail.com>
* FakeQuantWithMinMaxArgs
Signed-off-by: AlexDBlack <blacka101@gmail.com>
* CheckNumerics fix
Signed-off-by: AlexDBlack <blacka101@gmail.com>
* Fix libnd4j ALL_INTS and ALL_FLOATS declaration (uint and bfloat types)
Signed-off-by: AlexDBlack <blacka101@gmail.com>
* Small fix
Signed-off-by: AlexDBlack <blacka101@gmail.com>
* Javadoc
Signed-off-by: AlexDBlack <blacka101@gmail.com>
* Exception tweak
Signed-off-by: AlexDBlack <blacka101@gmail.com>
* fix
Signed-off-by: AlexDBlack <blacka101@gmail.com>
* Fix for out of scope stack allocated var use
Signed-off-by: AlexDBlack <blacka101@gmail.com>
* Ignores
Signed-off-by: AlexDBlack <blacka101@gmail.com>
* Ignore for known failing test (already logged issue)
Signed-off-by: AlexDBlack <blacka101@gmail.com>
* Merge upstream to fork (#25)
* Add thousand-separator commas to TotalParams (#7915)
* Add thousand-separator commas to TotalParams
The number of parameters can be quite large, and it would help the reading of the summary printout to have the TotalParams column & values at the bottom have thousand-separator-commas in them.
* Add thousand-separator commas to MultiLayerNetwork
Corresponding change to MultiLayerNetwork
Signed-off-by: Jxtps Jxtps <jxtps435@gmail.com>
* Update contributing and issue/PR templates (#7934)
Signed-off-by: AlexDBlack <blacka101@gmail.com>
* Fix link to AdaDelta paper (#7942)
Fix link to AdaDelta paper hosted on matthewzeiler.com
Signed-off-by: Jxtps
* Fixes, and ignores for known/logged failing issues (#7943)
Signed-off-by: AlexDBlack <blacka101@gmail.com>
* SameDiff + DL4J/SameDiff: Multiple fixes (#28)
* #7919 HDF5 attribute buffer length fix
Signed-off-by: AlexDBlack <blacka101@gmail.com>
* #7909 Arbiter constructor exception ux improvements
Signed-off-by: AlexDBlack <blacka101@gmail.com>
* #7925 RNN output layer length checks
Signed-off-by: AlexDBlack <blacka101@gmail.com>
* #7939 Add listener for validating inputs are not incorrectly modified
Signed-off-by: AlexDBlack <blacka101@gmail.com>
* #7939 Integrate NonInplaceValidationListener into tests
* #7844 DL4J SameDiff fixes for variable minibatch size
* DL4J SameDiff fixes - ensure gradient for input placeholder is available
Signed-off-by: AlexDBlack <blacka101@gmail.com>
* Tweaks to ExternalErrorsFunction - use placeholders, make more robust
* Another fix
* More fixes
* More SameDiff/DL4J fixes
* Scope out scalar array creation in BaseScalarOp
* Remove debug code
Signed-off-by: AlexDBlack <blacka101@gmail.com>
* [WIP] Final dev branch merge (#29)
* SameDiff: convertDataType and gradient check util improvements (#12)
* GradCheck util improvements
* StopGradient constructor + test
* SameDiff: Add datatype conversion
* Javadoc and add DataType.isNumerical()
* Small fix
* Fix SameDiff TF import test cases intermediate naming (workaround for bad default)
* TFGraphTestAllHelper: check intermediates in execution order
* Add missing debug listener
* [WIP] lstmBlock fix + other changes (#13)
- fixes lstmBlock issue
- changes NDArray method reshape(), permute(), transpose() by making them return instance instead of pointer
- CheckNumerics op
- fixes for ReduceBool IsInfOrNan & IsFinite
* Small test fix
* CheckNumerics op wrapper
* Compatibility of deserialization (#18)
Signed-off-by: Alexander Stoyakin <alexander.stoyakin@gmail.com>
* SameDiff: add activation gradient checking support for debugging (#19)
* SameDiff gradient checker: first pass on activation gradient checks
* Fixes + tests for activation gradient checking
* Javadoc
* [WIP] Some nd4j data type corrections (#20)
* Adjust data type
* Set correct Data type.
* Size of proper data type.
* fix averaged cpu load (#22)
* [WIP] Multiple dataset iterators (#27)
* Splitting dataset into arbitrary number
* Fixes
* Multiple split of iterator
* Test
* Test
* Some fixes
* signature change
* one more tweak
Signed-off-by: raver119 <raver119@gmail.com>
* one more test for sequential use of DataSetIteratorSplitter
Signed-off-by: raver119 <raver119@gmail.com>
* Fixes
* Fixes
* one more test for Alexander
Signed-off-by: raver119 <raver119@gmail.com>
* Some fixes
* Some fixes
* one more test for Alexander
Signed-off-by: raver119 <raver119@gmail.com>
* minor test fix
Signed-off-by: raver119 <raver119@gmail.com>
* Some fixes
* Some fixes
* couple of assertions tweaked
Signed-off-by: raver119 <raver119@gmail.com>
* MDS splitter test :/
Signed-off-by: raver119 <raver119@gmail.com>
* Minor refactoring
* Multi dataset
* Some fixes
* More tests
* Small number of test fixes/improvements (failures on CI) (#31)
Signed-off-by: AlexDBlack <blacka101@gmail.com>
* [WIP] More CUDA stuff (#26)
* initial commit
Signed-off-by: raver119 <raver119@gmail.com>
* LRN BP CUDA
Signed-off-by: raver119 <raver119@gmail.com>
* less memory
Signed-off-by: raver119 <raver119@gmail.com>
* Fixed bug with crop_and_resize op helper.
* get rid of unnecessary index-calculation dunction
Signed-off-by: Yurii <yurii@skymind.io>
* Fixed sort with nth_element cuda-based helper.
* Refactored nth_element.
* Refactored nth_element op and tests.
* Modified usage of dim array with sortTad routine.
* Refactored main routine of helper for non_max_image_suppression op.
* non_max_image_suppression op helper with cuda kernel implementation. Initial revision.
* fix vol2col cuda kernel
* meh
Signed-off-by: raver119 <raver119@gmail.com>
* topK concept
Signed-off-by: raver119 <raver119@gmail.com>
* unsorted topK with scanWitdh of 1
Signed-off-by: raver119 <raver119@gmail.com>
* correct vol2col tests
* sorted/unsorted topK
Signed-off-by: raver119 <raver119@gmail.com>
* implementation and fixing col2im/col2vol
* Corrected usage flags with input/output with reverse op.
* dup is const now
Signed-off-by: raver119 <raver119@gmail.com>
* percentile op
Signed-off-by: raver119 <raver119@gmail.com>
* group tests for mapool2d
Signed-off-by: Yurii <yurii@skymind.io>
* special test for george
Signed-off-by: raver119 <raver119@gmail.com>
* less threads for sortTad
Signed-off-by: raver119 <raver119@gmail.com>
* provide conv2d for cuda
Signed-off-by: Yurii <yurii@skymind.io>
* remove auther in sort tad kernel code
Signed-off-by: Yurii <yurii@skymind.io>
* provide depthwise_conv2d for cuda
Signed-off-by: Yurii <yurii@skymind.io>
* - max_pooling_with_argmax
- null check for special use
Signed-off-by: raver119 <raver119@gmail.com>
* dts cuda
Signed-off-by: raver119 <raver119@gmail.com>
* provide sconv2d for cuda
Signed-off-by: Yurii <yurii@skymind.io>
* std cuda
Signed-off-by: raver119 <raver119@gmail.com>
* Refactored non_max_suppression op to conform TF implementation.
* Improved suppression helper.
* provide pooling3d for cuda
Signed-off-by: Yurii <yurii@skymind.io>
* minor lstm rearrangements
Signed-off-by: raver119 <raver119@gmail.com>
* more of minor lstm rearrangements
Signed-off-by: raver119 <raver119@gmail.com>
* (bi)dynamic_rnn
Signed-off-by: raver119 <raver119@gmail.com>
* templates init order
Signed-off-by: raver119 <raver119@gmail.com>
* Refactored non_max_suppression op.
* Added cuda kernel for non_max_suppression.
* CPU sort by key/value
Signed-off-by: raver119 <raver119@gmail.com>
* CPU sort TAD by key/value
Signed-off-by: raver119 <raver119@gmail.com>
* CPU sort TAD by key/value tests
Signed-off-by: raver119 <raver119@gmail.com>
* Eliminate compiler error with cuda implementation.
* - repaired gradCheck in cuda
- provide conv2d_bp for cuda
Signed-off-by: Yurii <yurii@skymind.io>
* missed signature
Signed-off-by: raver119 <raver119@gmail.com>
* provide depthwise_conv2d_bp for cuda
Signed-off-by: Yurii <yurii@skymind.io>
* Implementation of lup helper with cuda kernel. Initial commit.
* further work on backprops for convolutions
Signed-off-by: Yurii <yurii@skymind.io>
* CUDA linear sort by key/val
Signed-off-by: raver119 <raver119@gmail.com>
* CUDA tad sort by key/val
Signed-off-by: raver119 <raver119@gmail.com>
* start providing of backprop for pooling2d/3d
Signed-off-by: Yurii <yurii@skymind.io>
* Added atomicAdd for bool datatype.
* dynamic partition concept
Signed-off-by: raver119 <raver119@gmail.com>
* dynamic partition concept
Signed-off-by: raver119 <raver119@gmail.com>
* dynamic partition scalar CUDA
Signed-off-by: raver119 <raver119@gmail.com>
* important comment
Signed-off-by: raver119 <raver119@gmail.com>
* fix pooling2d/3d backprop helpers
Signed-off-by: Yurii <yurii@skymind.io>
* Added non-linear test with dynamic_partition.
* Improved test for dynamic_partition.
* dynamic_partition TAD concept
Signed-off-by: raver119 <raver119@gmail.com>
* - dynamic_partition TAD CUDA impl
- dynamic_partition TAD CPU fix
Signed-off-by: raver119 <raver119@gmail.com>
* - rewrite cpu code for usampling2d/3d
- write cuda code for usampling2d/3d
Signed-off-by: Yurii <yurii@skymind.io>
* dynamic_stitch CUDA vector case
Signed-off-by: raver119 <raver119@gmail.com>
* dynamic_stitch CUDA TAD case concept
Signed-off-by: raver119 <raver119@gmail.com>
* dynamic_stitch CUDA TAD case impl
Signed-off-by: raver119 <raver119@gmail.com>
* Added tests for dynamic_stitch 3D-4D cases.
* minor tests tweaks
Signed-off-by: raver119 <raver119@gmail.com>
* Fixed type check for dynamic stitch.
* min/max bp
Signed-off-by: raver119 <raver119@gmail.com>
* rewrite code for upsampling2d/3d cpu
Signed-off-by: Yurii <yurii@skymind.io>
* reduce min/max/norm_max bp
Signed-off-by: raver119 <raver119@gmail.com>
* lup implementation. Additional enhancements.
* provide code for upsamling2d/3d backprop
Signed-off-by: Yurii <yurii@skymind.io>
* weightedCrossEntropyWithLogits
Signed-off-by: raver119 <raver119@gmail.com>
* Fixed template math atomicMul for 64bit ints.
* Refactored dynamic_partition_bp op.
* inverseBroadcast fix
Signed-off-by: raver119 <raver119@gmail.com>
* DynamicPartitionBP test datatype fixed.
* - nd4j_atomicMul Windows fix
- cpu/NDArrayLambda.hpp excluded from CUDA
Signed-off-by: raver119 <raver119@gmail.com>
2019-06-27 17:37:04 +02:00
|
|
|
|
2019-09-09 15:27:45 +02:00
|
|
|
for (int k = pos + start, j = start; j < n2; k += step, j += step) {
|
2019-09-11 19:12:09 +02:00
|
|
|
auto zIndex = shape::getIndexOffset(k, outputShape);
|
2019-09-09 15:27:45 +02:00
|
|
|
outputBuf[zIndex] = matrix[j];
|
|
|
|
}
|
|
|
|
}
|
2019-07-12 10:51:51 +02:00
|
|
|
|
2019-09-09 15:27:45 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------------------------ //
|
|
|
|
// fill up permutaion matrix kernel. Permutation matrix filled with zeros and ones
|
|
|
|
template<typename F>
|
|
|
|
static __global__ void fillUpPermutation(void *output, Nd4jLong *shape, int *source, int rowNum) {
|
|
|
|
F *permutation = reinterpret_cast<F *>(output);
|
|
|
|
|
|
|
|
auto start = blockIdx.x * blockDim.x + threadIdx.x;
|
|
|
|
auto step = blockDim.x * gridDim.x;
|
|
|
|
for (auto i = start; i < rowNum; i += step) {
|
|
|
|
int val = source[i] - 1;
|
|
|
|
Nd4jLong posF[] = {i, val};
|
2019-09-11 19:12:09 +02:00
|
|
|
auto pos = shape::getOffset(shape, posF);
|
2019-09-09 15:27:45 +02:00
|
|
|
permutation[pos] = F(1.f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------------------------ //
|
|
|
|
// LUP decomposition runner - using CUBLAS SOLVER
|
|
|
|
// if permutation is given, then using LUP decomposition, LU decomposition otherwise
|
|
|
|
// L - lower triangular, U - upper triangular, P - permutation matricies
|
|
|
|
// PA = LU
|
|
|
|
//
|
|
|
|
// input - A matrix nxn
|
|
|
|
// compound - C matrix L + U - I, or main diagonal and lower - L matrix, from the 2nd diagonal - U matrix
|
2019-12-20 15:56:28 +01:00
|
|
|
template<typename T, typename I>
|
2019-09-09 15:27:45 +02:00
|
|
|
static void lup_(LaunchContext *context, NDArray *input, NDArray *compound, NDArray *permutation) {
|
|
|
|
auto stream = context->getCudaStream();
|
|
|
|
auto n = input->rows();
|
|
|
|
cusolverDnHandle_t cusolverH = nullptr;
|
|
|
|
// create solver handle
|
|
|
|
cusolverStatus_t status = cusolverDnCreate(&cusolverH);
|
|
|
|
if (CUSOLVER_STATUS_SUCCESS != status) {
|
|
|
|
throw cuda_exception::build("Cannot create cuSolver handle", status);
|
|
|
|
}
|
|
|
|
// set solver stream
|
|
|
|
status = cusolverDnSetStream(cusolverH, *stream);
|
|
|
|
if (CUSOLVER_STATUS_SUCCESS != status) {
|
|
|
|
throw cuda_exception::build("Cannot set up stream for cuda solver", status);
|
|
|
|
}
|
|
|
|
int lwork = 0;
|
|
|
|
int *d_info = nullptr;
|
|
|
|
// allocate memory for permutation vector
|
|
|
|
auto err = cudaMalloc((void **) &d_info, sizeof(int));
|
|
|
|
if (err) {
|
|
|
|
throw cuda_exception::build("helpers::lup_: Cannot allocate memory for solver info buffer", err);
|
|
|
|
}
|
2019-07-12 10:51:51 +02:00
|
|
|
|
2019-09-09 15:27:45 +02:00
|
|
|
DataType dtype = input->dataType();
|
|
|
|
switch (dtype) { // there are two implementations with cublas for LUP decomposition - double and float
|
|
|
|
|
|
|
|
case DataType::DOUBLE: {
|
|
|
|
double *d_work = nullptr;
|
|
|
|
// compute internal buffer size
|
|
|
|
double *matrix = reinterpret_cast<double *>(input->specialBuffer());
|
|
|
|
status = cusolverDnDgetrf_bufferSize(
|
|
|
|
cusolverH,
|
|
|
|
n,
|
|
|
|
n,
|
|
|
|
matrix,
|
|
|
|
n,
|
|
|
|
&lwork);
|
|
|
|
if (CUSOLVER_STATUS_SUCCESS != status) {
|
|
|
|
throw cuda_exception::build("helpers::lup_: Cannot create cuSolver handle", status);
|
|
|
|
}
|
|
|
|
|
|
|
|
err = cudaMalloc((void **) &d_work, sizeof(float) * lwork);
|
|
|
|
if (err) {
|
|
|
|
throw cuda_exception::build("helpers::lup_: Cannot allocate memory for solver data buffer",
|
|
|
|
err);
|
|
|
|
}
|
|
|
|
|
2019-12-20 15:56:28 +01:00
|
|
|
if (permutation == nullptr) {
|
2019-09-09 15:27:45 +02:00
|
|
|
status = cusolverDnDgetrf(
|
2019-07-12 10:51:51 +02:00
|
|
|
cusolverH,
|
|
|
|
n,
|
|
|
|
n,
|
|
|
|
matrix,
|
|
|
|
n,
|
2019-09-09 15:27:45 +02:00
|
|
|
d_work,
|
|
|
|
nullptr,
|
|
|
|
d_info);
|
2019-12-20 15:56:28 +01:00
|
|
|
|
|
|
|
if (status != CUSOLVER_STATUS_SUCCESS) {
|
|
|
|
throw cuda_exception::build("helpers::lup_: LU factorization is failed due ",
|
|
|
|
status);
|
|
|
|
}
|
|
|
|
}
|
2019-09-09 15:27:45 +02:00
|
|
|
else {
|
|
|
|
NDArray permutVector('c', {n}, nd4j::DataType::INT32, context);
|
2019-12-20 15:56:28 +01:00
|
|
|
int* permutationBuf = permutVector.dataBuffer()->specialAsT<int>();
|
2019-09-09 15:27:45 +02:00
|
|
|
status = cusolverDnDgetrf(
|
2019-07-12 10:51:51 +02:00
|
|
|
cusolverH,
|
|
|
|
n,
|
|
|
|
n,
|
|
|
|
matrix,
|
|
|
|
n,
|
2019-09-09 15:27:45 +02:00
|
|
|
d_work,
|
|
|
|
permutationBuf,
|
|
|
|
d_info);
|
2019-12-20 15:56:28 +01:00
|
|
|
if (status != CUSOLVER_STATUS_SUCCESS) {
|
|
|
|
throw cuda_exception::build("helpers::lup_: LU factorization is failed due ",
|
|
|
|
status);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (permutation->rankOf() == 2) {
|
|
|
|
fillUpPermutation<double> <<< n, n, 1024, *stream >>>
|
|
|
|
(permutation->specialBuffer(), permutation->specialShapeInfo(), permutationBuf, n);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
permutVector.tickWriteDevice();
|
|
|
|
input->tickWriteDevice();
|
|
|
|
compound->assign(input);
|
|
|
|
permutation->assign(permutVector);
|
|
|
|
}
|
2019-09-09 15:27:45 +02:00
|
|
|
}
|
|
|
|
err = cudaFree(d_work);
|
|
|
|
if (err) {
|
|
|
|
throw cuda_exception::build("helpers::lup_: Cannot deallocate memory for solver data buffer",
|
|
|
|
err);
|
2019-08-23 18:20:50 +02:00
|
|
|
}
|
2019-07-12 10:51:51 +02:00
|
|
|
}
|
2019-09-09 15:27:45 +02:00
|
|
|
break;
|
|
|
|
case DataType::FLOAT32: {
|
|
|
|
float *matrix = reinterpret_cast<float*>(input->specialBuffer());
|
|
|
|
float *d_work = nullptr;
|
|
|
|
|
|
|
|
status = cusolverDnSgetrf_bufferSize(
|
|
|
|
cusolverH,
|
|
|
|
n,
|
|
|
|
n,
|
|
|
|
matrix,
|
|
|
|
n,
|
|
|
|
&lwork);
|
|
|
|
if (CUSOLVER_STATUS_SUCCESS != status) {
|
|
|
|
throw cuda_exception::build("helpers::lup_: Cannot create cuSolver handle", status);
|
|
|
|
}
|
|
|
|
|
|
|
|
err = cudaMalloc((void **) &d_work, sizeof(float) * lwork);
|
|
|
|
if (err) {
|
|
|
|
throw cuda_exception::build("helpers::lup_: Cannot allocate memory for solver data buffer",
|
|
|
|
err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (permutation == nullptr)
|
|
|
|
status = cusolverDnSgetrf(
|
|
|
|
cusolverH,
|
|
|
|
n,
|
|
|
|
n,
|
|
|
|
matrix,
|
|
|
|
n,
|
|
|
|
d_work,
|
|
|
|
nullptr,
|
|
|
|
d_info);
|
|
|
|
else {
|
2019-12-20 15:56:28 +01:00
|
|
|
NDArray permutVector('c', {n}, DataType::INT32, context);
|
2019-09-09 15:27:45 +02:00
|
|
|
int *permutationBuf = reinterpret_cast<int *>(permutVector.specialBuffer());
|
|
|
|
status = cusolverDnSgetrf(
|
|
|
|
cusolverH,
|
|
|
|
n,
|
|
|
|
n,
|
|
|
|
matrix,
|
|
|
|
n,
|
|
|
|
d_work,
|
|
|
|
permutationBuf,
|
|
|
|
d_info);
|
2019-12-20 15:56:28 +01:00
|
|
|
if (permutation->rankOf() == 2) {
|
|
|
|
fillUpPermutation<I> <<< n, n, 128, *stream >>>
|
|
|
|
(permutation->specialBuffer(), permutation->specialShapeInfo(), permutationBuf, n);
|
|
|
|
permutation->tickWriteDevice();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
input->tickWriteDevice();
|
|
|
|
compound->assign(input);
|
|
|
|
permutation->assign(permutVector);
|
|
|
|
}
|
2019-09-09 15:27:45 +02:00
|
|
|
}
|
|
|
|
err = cudaFree(d_work);
|
|
|
|
if (err) {
|
|
|
|
throw cuda_exception::build("helpers::lup_: Cannot deallocate memory for solver data buffer",
|
|
|
|
err);
|
|
|
|
}
|
|
|
|
|
2019-08-23 18:20:50 +02:00
|
|
|
}
|
|
|
|
}
|
2019-09-09 15:27:45 +02:00
|
|
|
if (CUSOLVER_STATUS_SUCCESS != status) {
|
|
|
|
throw cuda_exception::build("helpers::lup_: Cannot make LU decomposition", status);
|
|
|
|
}
|
|
|
|
err = cudaFree(d_info);
|
|
|
|
if (err) {
|
|
|
|
throw cuda_exception::build("helpers::lup_: Cannot deallocate memory for solver info buffer", err);
|
|
|
|
}
|
|
|
|
cusolverDnDestroy(cusolverH);
|
|
|
|
// NDArray::registerSpecialUse({input}, {input});
|
|
|
|
input->tickWriteDevice();
|
|
|
|
}
|
|
|
|
// ------------------------------------------------------------------------------------------------------------------ //
|
2019-08-23 18:20:50 +02:00
|
|
|
|
2019-12-20 15:56:28 +01:00
|
|
|
BUILD_DOUBLE_TEMPLATE(template void lup_,(LaunchContext * context, NDArray * input, NDArray * output, NDArray * permutation), FLOAT_NATIVE, INDEXING_TYPES);
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
static __device__ void swapRows(T* matrix, Nd4jLong* shape, Nd4jLong theFirst, Nd4jLong theSecond, Nd4jLong n) {
|
|
|
|
if (theFirst != theSecond) {
|
|
|
|
for (auto i = 0; i < n; i++) {
|
|
|
|
Nd4jLong theFirstPos[] = {theFirst, i};
|
|
|
|
Nd4jLong theSecondPos[] = {theSecond, i};
|
|
|
|
auto theFirstIndex = shape::getOffset(shape, theFirstPos, 0);
|
|
|
|
auto theSecondIndex = shape::getOffset(shape, theSecondPos, 0);
|
|
|
|
math::nd4j_swap(matrix[theFirstIndex], matrix[theSecondIndex]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-23 18:20:50 +02:00
|
|
|
|
2019-12-20 15:56:28 +01:00
|
|
|
template <typename T>
|
|
|
|
static __device__ void processColumns(Nd4jLong currentRow, Nd4jLong rowNum, T* compoundBuf, Nd4jLong* compoundShape) {
|
|
|
|
Nd4jLong xDiag[] = {currentRow, currentRow};
|
|
|
|
auto diagIndex = shape::getOffset(compoundShape, xDiag, 0);
|
|
|
|
for (auto j = currentRow + 1; j < rowNum; j++) {
|
|
|
|
Nd4jLong xRow[] = {j, currentRow};
|
|
|
|
auto rowIndex = shape::getOffset(compoundShape, xRow, 0);
|
|
|
|
compoundBuf[rowIndex] /= compoundBuf[diagIndex]; //output->t<T>(i, i);
|
|
|
|
for (auto k = currentRow + 1; k < rowNum; k++) {
|
|
|
|
Nd4jLong yRow[] = {j, k};
|
|
|
|
Nd4jLong yCol[] = {currentRow, k};
|
|
|
|
auto rowIndexY = shape::getOffset(compoundShape, yRow, 0);
|
|
|
|
auto colIndex = shape::getOffset(compoundShape, yCol, 0);
|
|
|
|
compoundBuf[rowIndexY] -= compoundBuf[rowIndex] * compoundBuf[colIndex];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
__device__ Nd4jLong argmaxCol(Nd4jLong column, T* compoundBuffer, Nd4jLong* compoundShape) {
|
|
|
|
auto rowNum = shape::sizeAt(compoundShape, 0);
|
|
|
|
Nd4jLong xInitial[] = {column, column};
|
|
|
|
auto xInitialIndex = shape::getOffset(compoundShape, xInitial, 0);
|
|
|
|
auto maxValue = T(0); //nd4j::math::nd4j_abs(compoundBuffer[xInitialIndex]);
|
|
|
|
auto result = -1LL;
|
|
|
|
|
|
|
|
for (auto rowCounter = column; rowCounter < rowNum; rowCounter++) {
|
|
|
|
Nd4jLong xPos[] = {rowCounter, column};
|
|
|
|
auto xIndex = shape::getOffset(compoundShape, xPos, 0);
|
|
|
|
if (nd4j::math::nd4j_abs(compoundBuffer[xIndex]) > maxValue) {
|
|
|
|
maxValue = nd4j::math::nd4j_max(maxValue, nd4j::math::nd4j_abs(compoundBuffer[xIndex]));
|
|
|
|
result = rowCounter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename I>
|
|
|
|
static __device__ int luNN(T* matrix, Nd4jLong* shape, I* permutation, Nd4jLong* permuShape, Nd4jLong n) {
|
|
|
|
|
|
|
|
for (auto i = 0; i < n - 1; i++) {
|
|
|
|
auto pivotIndex = argmaxCol(i, matrix, shape);
|
|
|
|
if (pivotIndex < 0) {
|
|
|
|
return -1;//throw std::runtime_error("helpers::luNN_: input matrix is singular.");
|
|
|
|
}
|
|
|
|
math::nd4j_swap(permutation[shape::getIndexOffset(i, permuShape)], permutation[shape::getIndexOffset(pivotIndex, permuShape)]);
|
|
|
|
swapRows(matrix, shape, (Nd4jLong)i, pivotIndex, n);
|
|
|
|
|
|
|
|
processColumns(i, n, matrix, shape);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename I>
|
|
|
|
static __global__ void luBatchedKernel(T* outputBuf, Nd4jLong* outputShape, I* permutations, Nd4jLong* permuShape,
|
|
|
|
Nd4jLong* outputTadShape, Nd4jLong* outputTadOffsets, Nd4jLong* permuTadShape, Nd4jLong* permuTadOffsets,
|
|
|
|
Nd4jLong batchNum) {
|
|
|
|
|
|
|
|
auto start = blockIdx.x * blockDim.x + threadIdx.x;
|
|
|
|
auto step = blockDim.x * gridDim.x;
|
|
|
|
|
|
|
|
for (auto b = start; b < batchNum; b += step) {
|
|
|
|
T* matrix = outputBuf + outputTadOffsets[b];
|
|
|
|
I* permutation = permutations + permuTadOffsets[b];
|
|
|
|
|
|
|
|
if (0 != luNN(matrix, outputTadShape, permutation, permuTadShape, shape::length(permuTadShape))) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename I>
|
|
|
|
static void lu_(LaunchContext * context, NDArray* input, NDArray* output, NDArray* permutationVectors) {
|
|
|
|
auto n = input->sizeAt(-1);
|
|
|
|
auto stream = context->getCudaStream();
|
2020-01-02 21:25:41 +01:00
|
|
|
NDArray iota('c', {n}, permutationVectors->dataType());// = NDArrayFactory::create(); // <int>('c', {n});
|
2019-12-20 15:56:28 +01:00
|
|
|
iota.linspace(0); iota.syncToDevice();
|
|
|
|
|
|
|
|
output->assign(input); // fill up output tensor with zeros
|
2020-01-02 21:25:41 +01:00
|
|
|
// output->tickWriteDevice();
|
2019-12-20 20:35:39 +01:00
|
|
|
permutationVectors->applyTrueBroadcast(nd4j::BroadcastOpsTuple::Assign(), iota, *permutationVectors, true, nullptr);
|
2020-01-02 21:25:41 +01:00
|
|
|
// permutationVectors->tickWriteDevice();
|
2019-12-20 15:56:28 +01:00
|
|
|
auto tads = ConstantTadHelper::getInstance()->tadForDimensions(output->shapeInfo(), {-2, -1});
|
|
|
|
auto permutaionTads = ConstantTadHelper::getInstance()->tadForDimensions(output->shapeInfo(), {-1});
|
|
|
|
auto batchNum = tads.numberOfTads();
|
|
|
|
luBatchedKernel<T,I><<<batchNum, 256, 1024, *stream>>>(reinterpret_cast<T*>(output->platformBuffer()),
|
|
|
|
output->specialShapeInfo(), reinterpret_cast<I*>(permutationVectors->platformBuffer()),
|
|
|
|
permutationVectors->specialShapeInfo(), tads.specialShapeInfo(), tads.specialOffsets(),
|
|
|
|
permutaionTads.specialShapeInfo(), permutaionTads.specialOffsets(), batchNum);
|
|
|
|
}
|
|
|
|
|
|
|
|
void lu(LaunchContext* context, NDArray* input, NDArray* output, NDArray* permutations) {
|
|
|
|
NDArray::prepareSpecialUse({output, permutations}, {input});
|
|
|
|
BUILD_DOUBLE_SELECTOR(input->dataType(), permutations->dataType(), lu_, (context, input, output, permutations), FLOAT_NATIVE, INDEXING_TYPES);
|
|
|
|
NDArray::registerSpecialUse({output, permutations}, {input});
|
|
|
|
}
|
2019-09-09 15:27:45 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------------------------ //
|
|
|
|
template<typename T>
|
|
|
|
static int determinant_(nd4j::LaunchContext *context, NDArray *input, NDArray *output) {
|
|
|
|
Nd4jLong n = input->sizeAt(-1);
|
|
|
|
Nd4jLong n2 = n * n;
|
|
|
|
std::vector<int> dims();
|
|
|
|
auto packX = ConstantTadHelper::getInstance()->tadForDimensions(input->getShapeInfo(), {input->rankOf() - 2, input->rankOf() - 1});
|
|
|
|
//auto packZ = ConstantTadHelper::getInstance()->tadForDimensions(output->shapeInfo(), {output->rankOf() - 1});
|
2019-08-23 18:20:50 +02:00
|
|
|
// DataType dtype = input->dataType();
|
|
|
|
// if (dtype != DataType::DOUBLE)
|
|
|
|
// dtype = DataType::FLOAT32;
|
2019-09-09 15:27:45 +02:00
|
|
|
auto matrix = NDArrayFactory::create(input->ordering(), {n, n}, DataTypeUtils::fromT<T>(), context); //, block.getWorkspace());
|
|
|
|
auto det = NDArrayFactory::create<T>(1);
|
|
|
|
auto stream = context->getCudaStream();
|
|
|
|
NDArray::prepareSpecialUse({output}, {input});
|
|
|
|
dim3 launchDims(256, 256, 1024);
|
|
|
|
output->assign(1.f);
|
|
|
|
for (int e = 0; e < output->lengthOf(); e++) {
|
|
|
|
Nd4jLong pos = e * n2;
|
2019-07-20 07:58:44 +02:00
|
|
|
// if (matrix.dataType() == input->dataType())
|
2019-09-09 15:27:45 +02:00
|
|
|
fillMatrix<T, T><<<launchDims.x, launchDims.y, launchDims.z, *stream>>>(matrix.specialBuffer(), matrix.specialShapeInfo(), input->specialBuffer(), input->specialShapeInfo(), pos, n);
|
2019-07-20 07:58:44 +02:00
|
|
|
// else
|
|
|
|
// fillMatrix<T, float><<<launchDims.x, launchDims.y, launchDims.z, *stream>>>(matrix.specialBuffer(), matrix.specialShapeInfo(), input->specialBuffer(), input->specialShapeInfo(), pos, n);
|
2019-12-20 15:56:28 +01:00
|
|
|
lup_<T, int>(context, &matrix, nullptr, nullptr);
|
2019-07-20 07:58:44 +02:00
|
|
|
// else
|
|
|
|
// lup_<float>(context, &matrix, nullptr, nullptr);
|
2019-09-11 19:12:09 +02:00
|
|
|
auto offset = shape::getIndexOffset(e, output->shapeInfo());
|
2019-09-09 15:27:45 +02:00
|
|
|
auto inputBuf = reinterpret_cast<T *>(matrix.specialBuffer());
|
|
|
|
auto outputBuf = reinterpret_cast<T *>(output->specialBuffer()) + offset;
|
2019-07-20 07:58:44 +02:00
|
|
|
// if (matrix.dataType() == input->dataType())
|
2019-09-09 15:27:45 +02:00
|
|
|
determinantKernel<T> << < launchDims.x, launchDims.y, launchDims.z, *stream >> >
|
|
|
|
(inputBuf, outputBuf, n);
|
2019-07-20 07:58:44 +02:00
|
|
|
// else
|
|
|
|
// determinantKernel<T, float><<<launchDims.x, launchDims.y, launchDims.z, *stream >>> (inputBuf, outputBuf, n);
|
2019-08-23 18:20:50 +02:00
|
|
|
}
|
2019-09-09 15:27:45 +02:00
|
|
|
NDArray::registerSpecialUse({output}, {input});
|
|
|
|
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2019-08-23 18:20:50 +02:00
|
|
|
|
|
|
|
int determinant(nd4j::LaunchContext *context, NDArray *input, NDArray *output) {
|
|
|
|
NDArray::prepareSpecialUse({output}, {input});
|
|
|
|
BUILD_SINGLE_SELECTOR(input->dataType(), return determinant_, (context, input, output), FLOAT_NATIVE);
|
|
|
|
NDArray::registerSpecialUse({output}, {input});
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
int logAbsDeterminant_(LaunchContext *context, NDArray *input, NDArray *output) {
|
|
|
|
Nd4jLong n = input->sizeAt(-1);
|
|
|
|
Nd4jLong n2 = n * n;
|
|
|
|
std::vector<int> dims();
|
2019-08-24 15:59:30 +02:00
|
|
|
auto packX = ConstantTadHelper::getInstance()->tadForDimensions(input->getShapeInfo(), {input->rankOf() - 2, input->rankOf() - 1});
|
2019-08-23 18:20:50 +02:00
|
|
|
//auto packZ = ConstantTadHelper::getInstance()->tadForDimensions(output->shapeInfo(), {output->rankOf() - 1});
|
|
|
|
DataType dtype = input->dataType();
|
|
|
|
if (dtype != DataType::DOUBLE)
|
|
|
|
dtype = DataType::FLOAT32;
|
|
|
|
|
2019-08-24 15:59:30 +02:00
|
|
|
auto matrix = NDArrayFactory::create(input->ordering(), {n, n}, dtype, context); //, block.getWorkspace());
|
2019-08-23 18:20:50 +02:00
|
|
|
auto det = NDArrayFactory::create<T>(1);
|
|
|
|
auto stream = context->getCudaStream();
|
|
|
|
NDArray::prepareSpecialUse({output}, {input});
|
|
|
|
dim3 launchDims(256, 256, 1024);
|
|
|
|
output->assign(0.f);
|
|
|
|
for (int e = 0; e < output->lengthOf(); e++) {
|
|
|
|
Nd4jLong pos = e * n2;
|
2019-07-20 07:58:44 +02:00
|
|
|
// if (matrix.dataType() == input->dataType())
|
2019-08-24 15:59:30 +02:00
|
|
|
fillMatrix<T, T><<<launchDims.x, launchDims.y, launchDims.z, *stream>>>(matrix.specialBuffer(), matrix.specialShapeInfo(), input->specialBuffer(), input->specialShapeInfo(), pos, n);
|
2019-07-20 07:58:44 +02:00
|
|
|
// else
|
|
|
|
// fillMatrix<T, float><<<launchDims.x, launchDims.y, launchDims.z, *stream>>>(matrix.specialBuffer(), matrix.specialShapeInfo(), input->specialBuffer(), input->specialShapeInfo(), pos, n);
|
2019-07-12 10:51:51 +02:00
|
|
|
|
2019-07-20 07:58:44 +02:00
|
|
|
// if (matrix.dataType() == input->dataType())
|
2019-12-20 15:56:28 +01:00
|
|
|
lup_<T, int>(context, &matrix, nullptr, nullptr);
|
2019-07-20 07:58:44 +02:00
|
|
|
// else
|
|
|
|
// lup_<float>(context, &matrix, nullptr, nullptr);
|
2019-09-11 19:12:09 +02:00
|
|
|
auto offset = shape::getIndexOffset(e, output->shapeInfo());
|
2019-08-23 18:20:50 +02:00
|
|
|
auto inputBuf = reinterpret_cast<T *>(matrix.specialBuffer());
|
|
|
|
auto outputBuf = reinterpret_cast<T *>(output->specialBuffer()) + offset;
|
2019-07-20 07:58:44 +02:00
|
|
|
// if (matrix.dataType() == input->dataType())
|
2019-08-24 15:59:30 +02:00
|
|
|
determinantLogKernel<T><<<launchDims.x, launchDims.y, launchDims.z, *stream>>>(inputBuf, outputBuf, n);
|
2019-07-20 07:58:44 +02:00
|
|
|
// else
|
|
|
|
// determinantLogKernel<T, float><<<launchDims.x, launchDims.y, launchDims.z, *stream >>> (inputBuf, outputBuf, n);
|
2019-08-23 18:20:50 +02:00
|
|
|
}
|
|
|
|
NDArray::registerSpecialUse({output}, {input});
|
|
|
|
|
|
|
|
return Status::OK();
|
|
|
|
|
|
|
|
return ND4J_STATUS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int logAbsDeterminant(nd4j::LaunchContext *context, NDArray *input, NDArray *output) {
|
|
|
|
NDArray::prepareSpecialUse({output}, {input});
|
|
|
|
BUILD_SINGLE_SELECTOR(input->dataType(), return logAbsDeterminant_, (context, input, output), FLOAT_NATIVE);
|
|
|
|
NDArray::registerSpecialUse({output}, {input});
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
static __global__ void
|
|
|
|
fillLowerUpperKernel(void *lowerBuf, Nd4jLong *lowerShape, void *upperBuf, Nd4jLong *upperShape,
|
|
|
|
void *matrixBuf, Nd4jLong *matrixShape, Nd4jLong n) {
|
|
|
|
|
2019-09-11 19:12:09 +02:00
|
|
|
__shared__ T *lowerMatrix;
|
|
|
|
__shared__ T *upperMatrix;
|
|
|
|
__shared__ T *matrix;
|
2019-07-12 10:51:51 +02:00
|
|
|
|
2019-08-23 18:20:50 +02:00
|
|
|
if (threadIdx.x == 0) {
|
|
|
|
lowerMatrix = reinterpret_cast<T *>(lowerBuf);
|
|
|
|
upperMatrix = reinterpret_cast<T *>(upperBuf);
|
|
|
|
matrix = reinterpret_cast<T *>(matrixBuf);
|
|
|
|
}
|
|
|
|
__syncthreads();
|
2019-07-20 07:58:44 +02:00
|
|
|
|
2019-08-23 18:20:50 +02:00
|
|
|
for (int k = blockIdx.x; k < n; k += gridDim.x) { // and then put all values under main diagonal on to it
|
|
|
|
for (int j = threadIdx.x; j < n; j += blockDim.x) {
|
|
|
|
Nd4jLong posX[] = {k, j};
|
|
|
|
Nd4jLong posD[] = {j, j};
|
2019-09-11 19:12:09 +02:00
|
|
|
auto xPos = shape::getOffset(lowerShape, posX);
|
|
|
|
auto yPos = shape::getOffset(upperShape, posX);
|
|
|
|
auto iPos = shape::getOffset(matrixShape, posX);
|
|
|
|
auto dPos = shape::getOffset(matrixShape, posD);
|
2019-08-23 18:20:50 +02:00
|
|
|
if (k >= j)
|
|
|
|
lowerMatrix[xPos] = matrix[iPos];//(k, j);
|
|
|
|
else
|
|
|
|
upperMatrix[yPos] = matrix[iPos]; //k, j);
|
|
|
|
}
|
|
|
|
}
|
2019-07-12 10:51:51 +02:00
|
|
|
}
|
|
|
|
|
2019-08-23 18:20:50 +02:00
|
|
|
template<typename T>
|
|
|
|
static int inverse_(nd4j::LaunchContext *context, NDArray *input, NDArray *output) {
|
|
|
|
auto n = input->sizeAt(-1);
|
|
|
|
auto n2 = n * n;
|
|
|
|
auto dtype = DataTypeUtils::fromT<T>(); //input->dataType();
|
|
|
|
// if (dtype != DataType::DOUBLE)
|
|
|
|
// dtype = DataType::FLOAT32;
|
2019-08-24 15:59:30 +02:00
|
|
|
NDArray matrix = NDArrayFactory::create('c', {n, n}, dtype, context);
|
|
|
|
NDArray upper = NDArrayFactory::create('c', {n, n}, dtype, context);
|
|
|
|
NDArray lower = NDArrayFactory::create('c', {n, n}, dtype, context);
|
|
|
|
NDArray compound = NDArrayFactory::create('c', {n, n}, dtype, context);
|
|
|
|
NDArray permutation = NDArrayFactory::create('c', {n, n}, dtype, context);
|
2019-08-23 18:20:50 +02:00
|
|
|
auto packX = nd4j::ConstantTadHelper::getInstance()->tadForDimensions(input->getShapeInfo(),
|
|
|
|
{input->rankOf() - 2,
|
|
|
|
input->rankOf() - 1});
|
|
|
|
auto packZ = nd4j::ConstantTadHelper::getInstance()->tadForDimensions(output->getShapeInfo(),
|
|
|
|
{output->rankOf() - 2,
|
|
|
|
output->rankOf() - 1});
|
|
|
|
auto stream = context->getCudaStream();
|
|
|
|
|
|
|
|
for (auto i = 0LL; i < packX.numberOfTads(); i++) {
|
2019-08-24 15:59:30 +02:00
|
|
|
fillMatrix<T, T><<<1, n2, 1024, *stream>>>(matrix.specialBuffer(), matrix.specialShapeInfo(), input->specialBuffer(), input->specialShapeInfo(), i * n2, n);
|
2019-08-23 18:20:50 +02:00
|
|
|
matrix.tickWriteDevice();
|
2019-12-02 19:40:54 +01:00
|
|
|
//compound.assign(matrix);
|
|
|
|
// if (matrix.dataType() == input->dataType())
|
2019-12-20 15:56:28 +01:00
|
|
|
lup_<T, int>(context, &matrix, nullptr, nullptr);
|
2019-12-02 19:40:54 +01:00
|
|
|
fillLowerUpperKernel<T><<<n, n, 1024, *stream>>>(lower.specialBuffer(), lower.specialShapeInfo(), upper.specialBuffer(), upper.specialShapeInfo(), matrix.specialBuffer(), matrix.specialShapeInfo(), n);
|
|
|
|
lower.tickWriteDevice();
|
|
|
|
upper.tickWriteDevice();
|
|
|
|
// lower.printIndexedBuffer("LOWER");
|
|
|
|
// upper.printIndexedBuffer("UPPER");
|
2019-08-23 18:20:50 +02:00
|
|
|
matrix.assign(0);
|
2019-08-24 15:59:30 +02:00
|
|
|
invertUpperMatrix(context, &upper, &matrix); // U^{-1}
|
2019-08-23 18:20:50 +02:00
|
|
|
matrix.tickWriteDevice();
|
|
|
|
// matrix.printIndexedBuffer("Upper Inverted");
|
|
|
|
compound.assign(0);
|
2019-08-24 15:59:30 +02:00
|
|
|
invertLowerMatrix(context, &lower, &compound); // L{-1}
|
2019-08-23 18:20:50 +02:00
|
|
|
compound.tickWriteDevice();
|
|
|
|
// compound.printIndexedBuffer("Lower Inverted");
|
|
|
|
// matrix.tickWriteDevice();
|
|
|
|
// compound.tickWriteDevice();
|
|
|
|
nd4j::MmulHelper::mmul(&matrix, &compound, &upper, 1.0, 0.0);
|
|
|
|
upper.tickWriteDevice();
|
|
|
|
// upper.printIndexedBuffer("Full inverted");
|
2019-08-24 15:59:30 +02:00
|
|
|
returnMatrix<T> <<<1, n2, 1024, *stream>>>(output->specialBuffer(), output->specialShapeInfo(), upper.specialBuffer(), upper.specialShapeInfo(), i * n2, n);
|
2019-07-12 10:51:51 +02:00
|
|
|
}
|
2019-08-23 18:20:50 +02:00
|
|
|
return Status::OK();
|
2019-07-12 10:51:51 +02:00
|
|
|
}
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2019-08-23 18:20:50 +02:00
|
|
|
int inverse(nd4j::LaunchContext *context, NDArray *input, NDArray *output) {
|
|
|
|
NDArray::prepareSpecialUse({output}, {input});
|
|
|
|
BUILD_SINGLE_SELECTOR(input->dataType(), return inverse_, (context, input, output), FLOAT_NATIVE);
|
|
|
|
NDArray::registerSpecialUse({output}, {input});
|
2019-07-20 07:58:44 +02:00
|
|
|
}
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2019-08-23 18:20:50 +02:00
|
|
|
bool checkCholeskyInput(nd4j::LaunchContext *context, NDArray const *input) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2019-08-23 18:20:50 +02:00
|
|
|
template<typename F>
|
|
|
|
__global__ void fillBatchKernel(F **dArrayBatch, F *buf, Nd4jLong *offsets, Nd4jLong batchSize) {
|
|
|
|
auto start = blockIdx.x * blockDim.x + threadIdx.x;
|
|
|
|
auto step = blockDim.x * gridDim.x;
|
2019-07-12 10:51:51 +02:00
|
|
|
|
2019-08-23 18:20:50 +02:00
|
|
|
for (auto i = start; i < batchSize; i += step) {
|
|
|
|
dArrayBatch[i] = buf + offsets[i];
|
|
|
|
}
|
2019-07-12 10:51:51 +02:00
|
|
|
}
|
|
|
|
|
2019-08-23 18:20:50 +02:00
|
|
|
template<typename F>
|
|
|
|
__global__ void
|
|
|
|
adjustResultsKernel(F *dArray, Nd4jLong *shape, Nd4jLong *offsets, Nd4jLong batchSize, Nd4jLong n) {
|
|
|
|
//auto i = blockIdx.x * blockDim.x + threadIdx.x;
|
|
|
|
Nd4jLong *shapeOf = shape::shapeOf(shape);
|
|
|
|
Nd4jLong *strideOf = shape::stride(shape);
|
|
|
|
|
|
|
|
for (auto i = blockIdx.x; i < batchSize; i += gridDim.x) {
|
|
|
|
auto current = dArray + offsets[i];
|
|
|
|
for (auto r = threadIdx.x; r < n; r += blockDim.x) {
|
|
|
|
for (auto c = r + 1; c < n; c++) {
|
|
|
|
Nd4jLong posRC[] = {r, c};
|
|
|
|
auto pos = r * n + c; //shape::getOffset(0, shapeOf, strideOf, posRC, 2);
|
|
|
|
current[pos] = 0.;
|
|
|
|
}
|
2019-07-12 10:51:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-23 18:20:50 +02:00
|
|
|
template<typename F>
|
|
|
|
int cholesky__(LaunchContext *context, NDArray *input, NDArray *output, bool inplace) {
|
|
|
|
if (!inplace)
|
|
|
|
output->assign(input);
|
2019-12-20 20:35:39 +01:00
|
|
|
auto tempOutput =output->dup();
|
2019-08-23 18:20:50 +02:00
|
|
|
cusolverDnHandle_t handle = nullptr;
|
|
|
|
auto n = input->sizeAt(-1);
|
|
|
|
auto n2 = n * n;
|
|
|
|
NDArray::prepareSpecialUse({output}, {input});
|
|
|
|
auto status = cusolverDnCreate(&handle);
|
|
|
|
if (CUSOLVER_STATUS_SUCCESS != status) {
|
|
|
|
throw cuda_exception::build("helpers::cholesky_: Cannot create solver handle", status);
|
|
|
|
}
|
|
|
|
F **dArrayBatch = nullptr;
|
2019-12-20 20:35:39 +01:00
|
|
|
auto packX = nd4j::ConstantTadHelper::getInstance()->tadForDimensions(tempOutput.getShapeInfo(),
|
|
|
|
{tempOutput.rankOf() - 2,
|
|
|
|
tempOutput.rankOf() - 1});
|
2019-08-23 18:20:50 +02:00
|
|
|
const Nd4jLong batchSize = packX.numberOfTads();
|
|
|
|
int *dInfoArray = nullptr;
|
|
|
|
auto err = cudaMalloc((void **) &dArrayBatch, sizeof(F *) * batchSize);
|
|
|
|
if (err) {
|
|
|
|
throw cuda_exception::build("helpers::cholesky_: Cannot allocate memory for solver batch data buffer",
|
|
|
|
err);
|
|
|
|
}
|
|
|
|
err = cudaMalloc((void **) &dInfoArray, sizeof(int) * batchSize);
|
|
|
|
if (err) {
|
|
|
|
throw cuda_exception::build("helpers::cholesky_: Cannot allocate memory for solver errors buffer", err);
|
|
|
|
}
|
|
|
|
auto stream = context->getCudaStream();
|
|
|
|
fillBatchKernel<F> << < 1, batchSize, 128, *stream >> >
|
2019-12-20 20:35:39 +01:00
|
|
|
(dArrayBatch, reinterpret_cast<F *>(tempOutput.specialBuffer()), packX.specialOffsets(), batchSize);
|
2019-07-12 10:51:51 +02:00
|
|
|
|
2019-08-23 18:20:50 +02:00
|
|
|
status = cusolverDnSetStream(handle, *stream);
|
|
|
|
if (CUSOLVER_STATUS_SUCCESS != status) {
|
|
|
|
throw cuda_exception::build("helpers::cholesky_: Cannot set stream to solver handle", status);
|
|
|
|
}
|
|
|
|
const cublasFillMode_t uplo = CUBLAS_FILL_MODE_UPPER;
|
|
|
|
if (input->dataType() == DataType::DOUBLE)
|
|
|
|
status = cusolverDnDpotrfBatched(
|
|
|
|
handle,
|
|
|
|
uplo,
|
|
|
|
n,
|
|
|
|
(double **) dArrayBatch,
|
|
|
|
n,
|
|
|
|
dInfoArray,
|
|
|
|
batchSize);
|
|
|
|
else
|
|
|
|
status = cusolverDnSpotrfBatched(
|
|
|
|
handle,
|
|
|
|
uplo,
|
|
|
|
n,
|
|
|
|
(float **) dArrayBatch,
|
|
|
|
n,
|
|
|
|
dInfoArray,
|
|
|
|
batchSize);
|
2019-07-12 10:51:51 +02:00
|
|
|
|
2019-08-23 18:20:50 +02:00
|
|
|
if (CUSOLVER_STATUS_SUCCESS != status) {
|
|
|
|
throw cuda_exception::build("helpers::cholesky_: Cholesky factorization failed for batch", status);
|
|
|
|
}
|
|
|
|
adjustResultsKernel<F> << < batchSize, n2, 128, *stream >> >
|
2019-12-20 20:35:39 +01:00
|
|
|
(reinterpret_cast<F *>(tempOutput.specialBuffer()), packX.specialShapeInfo(), packX.specialOffsets(), batchSize, n);
|
2019-08-23 18:20:50 +02:00
|
|
|
|
|
|
|
err = cudaFree(dArrayBatch);
|
|
|
|
if (err) {
|
|
|
|
throw cuda_exception::build("helpers::cholesky_: Cannot deallocate memory for solver batch data buffer",
|
|
|
|
err);
|
|
|
|
}
|
|
|
|
err = cudaFree(dInfoArray);
|
|
|
|
if (err) {
|
|
|
|
throw cuda_exception::build("helpers::cholesky_: Cannot allocate memory for solver errors buffer", err);
|
|
|
|
}
|
2019-07-12 10:51:51 +02:00
|
|
|
|
2019-08-23 18:20:50 +02:00
|
|
|
if (!inplace)
|
2019-12-20 20:35:39 +01:00
|
|
|
output->assign(tempOutput);
|
2019-08-23 18:20:50 +02:00
|
|
|
else
|
2019-12-20 20:35:39 +01:00
|
|
|
input->assign(tempOutput);
|
2019-07-12 10:51:51 +02:00
|
|
|
|
2019-08-23 18:20:50 +02:00
|
|
|
NDArray::registerSpecialUse({output}, {input});
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2019-07-12 10:51:51 +02:00
|
|
|
|
|
|
|
// template <typename T>
|
2019-08-23 18:20:50 +02:00
|
|
|
int cholesky_(LaunchContext *context, NDArray *input, NDArray *output, bool inplace) {
|
|
|
|
NDArray::prepareSpecialUse({output}, {input});
|
|
|
|
if (input->dataType() == DataType::DOUBLE)
|
|
|
|
cholesky__<double>(context, input, output, inplace);
|
|
|
|
else if (input->dataType() == DataType::FLOAT32)
|
|
|
|
cholesky__<float>(context, input, output, inplace);
|
|
|
|
else {
|
|
|
|
std::unique_ptr<NDArray> tempOutput(
|
2019-08-24 15:59:30 +02:00
|
|
|
NDArrayFactory::create_('c', input->getShapeAsVector(), DataType::FLOAT32, context));
|
2019-08-23 18:20:50 +02:00
|
|
|
tempOutput->assign(input);
|
|
|
|
cholesky__<float>(context, tempOutput.get(), tempOutput.get(), true);
|
|
|
|
output->assign(tempOutput.get());
|
|
|
|
}
|
|
|
|
NDArray::registerSpecialUse({output}, {input});
|
|
|
|
return Status::OK();
|
2019-07-12 10:51:51 +02:00
|
|
|
}
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2019-08-23 18:20:50 +02:00
|
|
|
int cholesky(nd4j::LaunchContext *context, NDArray *input, NDArray *output, bool inplace) {
|
2019-07-12 10:51:51 +02:00
|
|
|
// BUILD_SINGLE_SELECTOR(input->dataType(), return cholesky_, (context, input, output, inplace), FLOAT_TYPES);
|
2019-08-23 18:20:50 +02:00
|
|
|
return cholesky_(context, input, output, inplace);
|
|
|
|
}
|
2019-07-12 10:51:51 +02:00
|
|
|
// BUILD_SINGLE_TEMPLATE(template int cholesky_, (LaunchContext* context, NDArray* input, NDArray* output, bool inplace), FLOAT_TYPES);
|
2019-08-23 18:20:50 +02:00
|
|
|
BUILD_SINGLE_TEMPLATE(template int inverse_, (nd4j::LaunchContext * context, NDArray * input, NDArray * output),
|
|
|
|
FLOAT_NATIVE);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2019-08-23 18:20:50 +02:00
|
|
|
template<typename T>
|
|
|
|
__global__ void
|
|
|
|
logDetKernel(T *inputBuf, Nd4jLong *inputShape, Nd4jLong batchNum, Nd4jLong *tadShape, Nd4jLong *tadOffsets,
|
|
|
|
T *outputBuf, Nd4jLong *outputShape) {
|
2019-08-02 19:01:03 +02:00
|
|
|
|
2019-08-23 18:20:50 +02:00
|
|
|
__shared__ int n;
|
|
|
|
if (threadIdx.x == 0) {
|
|
|
|
n = shape::sizeAt(inputShape, -1); // * shape::sizeAt(inputShape, -1);
|
|
|
|
}
|
|
|
|
__syncthreads();
|
2019-07-12 10:51:51 +02:00
|
|
|
|
2019-08-23 18:20:50 +02:00
|
|
|
T *output = outputBuf;
|
|
|
|
T *input = inputBuf;
|
2019-08-02 19:01:03 +02:00
|
|
|
|
2019-08-23 18:20:50 +02:00
|
|
|
for (auto i = blockIdx.x; i < batchNum; i += gridDim.x) {
|
|
|
|
T *current = input + tadOffsets[i];
|
2019-08-21 19:18:29 +02:00
|
|
|
|
2019-09-11 19:12:09 +02:00
|
|
|
auto zIndex = shape::getIndexOffset(i, outputShape);
|
2019-08-23 18:20:50 +02:00
|
|
|
for (auto e = threadIdx.x; e < n; e += blockDim.x) {
|
|
|
|
Nd4jLong diag[] = {e, e};
|
2019-09-11 19:12:09 +02:00
|
|
|
auto xIndex = shape::getOffset(tadShape, diag);
|
|
|
|
math::atomics::nd4j_atomicAdd(&output[zIndex],math::nd4j_log<T, T>(current[xIndex] * current[xIndex]));
|
2019-08-23 18:20:50 +02:00
|
|
|
}
|
2019-07-12 10:51:51 +02:00
|
|
|
}
|
|
|
|
}
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2019-08-23 18:20:50 +02:00
|
|
|
template<typename T>
|
|
|
|
int logdetFunctor_(nd4j::LaunchContext *context, NDArray *input, NDArray *output) {
|
|
|
|
NDArray::prepareSpecialUse({output}, {input});
|
|
|
|
auto n2 = input->sizeAt(-1) * input->sizeAt(-2);
|
|
|
|
auto stream = context->getCudaStream();
|
2019-11-28 19:08:51 +01:00
|
|
|
NDArray tempOutput(*input);
|
|
|
|
|
|
|
|
cholesky(context, input, &tempOutput, false);
|
|
|
|
|
|
|
|
auto outputBuf = output->dataBuffer()->specialAsT<T>(); //reinterpret_cast<T*>(output->specialBuffer()); // + e * n2; // + e * n2;
|
2019-12-20 20:35:39 +01:00
|
|
|
auto inputBuf = tempOutput.dataBuffer()->specialAsT<T>(); //reinterpret_cast<T*>(tempOutput.specialBuffer());
|
2019-11-28 19:08:51 +01:00
|
|
|
output->nullify();
|
|
|
|
auto packX = nd4j::ConstantTadHelper::getInstance()->tadForDimensions(tempOutput.getShapeInfo(),
|
|
|
|
{tempOutput.rankOf() - 2,
|
|
|
|
tempOutput.rankOf() - 1});
|
2019-12-20 15:56:28 +01:00
|
|
|
logDetKernel<T> <<<128, 512, 256, *stream>>>(inputBuf, tempOutput.specialShapeInfo(),
|
2019-11-28 19:08:51 +01:00
|
|
|
packX.numberOfTads(), packX.specialShapeInfo(),
|
|
|
|
packX.specialOffsets(), outputBuf, output->specialShapeInfo());
|
|
|
|
output->tickWriteDevice();
|
2019-08-23 18:20:50 +02:00
|
|
|
NDArray::registerSpecialUse({output}, {input});
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
int logdetFunctor(nd4j::LaunchContext *context, NDArray *input, NDArray *output) {
|
2019-11-28 19:08:51 +01:00
|
|
|
BUILD_SINGLE_SELECTOR(output->dataType(), return logdetFunctor_, (context, input, output), FLOAT_NATIVE);
|
2019-08-23 18:20:50 +02:00
|
|
|
}
|
|
|
|
|
2019-12-20 15:56:28 +01:00
|
|
|
/*
|
|
|
|
* lup - batched input, batched outputs
|
|
|
|
* */
|
|
|
|
int lup(LaunchContext *context, NDArray *input, NDArray *compound, NDArray *permutation) {
|
|
|
|
BUILD_DOUBLE_SELECTOR(input->dataType(), permutation->dataType(), lup_,(context, input, compound, permutation), FLOAT_NATIVE, INDEXING_TYPES);
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
2019-11-28 19:08:51 +01:00
|
|
|
// BUILD_SINGLE_TEMPLATE(template int logdetFunctor_,
|
|
|
|
// (nd4j::LaunchContext * context, NDArray * input, NDArray * output), FLOAT_NATIVE);
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|