* initial commit Signed-off-by: raver119@gmail.com <raver119@gmail.com> * another initial commit Signed-off-by: raver119@gmail.com <raver119@gmail.com> * another initial commit Signed-off-by: raver119@gmail.com <raver119@gmail.com> * one more initial commit Signed-off-by: raver119@gmail.com <raver119@gmail.com> * next step Signed-off-by: raver119@gmail.com <raver119@gmail.com> * next step Signed-off-by: raver119@gmail.com <raver119@gmail.com> * next step Signed-off-by: raver119@gmail.com <raver119@gmail.com> * next step Signed-off-by: raver119@gmail.com <raver119@gmail.com> * Refactored buffer() and shapeInfo() methods usage with NDArray class. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopt Graph class methods to use const shapes. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopt choose op to use constant shapes. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopt where op shape method to use constant shapes. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopt lstsq op to use constant empty shapes. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopt matrix_diag_part op shape routine to use constant shapes. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopt determinant ops to use constant shapes. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopt mean_pairwssqerr_loss ops to use constant shapes. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopt ops shape methods. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopt shape methods for loss ops. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopt log_loss op shape method. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopt shape methods for ops. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopt dilation2d ops shape methods. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopted deconv2d ops shape methods. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopted dynamicRNN op shape method. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopted shape methods for ops. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopted shape methods for lstm layer ops. Signed-off-by: shugeo <sgazeos@gmail.com> * few updates Signed-off-by: raver119@gmail.com <raver119@gmail.com> * first cuda tweak Signed-off-by: raver119@gmail.com <raver119@gmail.com> * Adopt constant shapes for sconv2d ops. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopt constant shapes for gru ops. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopt constant shapes with shape methods for segment ops and so on. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopted constant shapes with unsorted_segment_* ops. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopted constant shapes with gamma op shape method. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopted shape methods of reduce_stddev ops. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopted shape methods for reduce_* ops. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopt shape method for squeeze op. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopt strided_slice shape method. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored concat op shape method to adopt constant shapes. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopted shape method for mirror_pad op. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopted split op shape method. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopted tile ops shape methods. Signed-off-by: shugeo <sgazeos@gmail.com> * Added const cast for mkldnn routines handles. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored logSoftMaxForVector_ routine to conform with proper data and shape pointer casts. Signed-off-by: shugeo <sgazeos@gmail.com> * Cosmetic changes to proper usage of constant pointers. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored a couple shape comparators for strides and addBias helpers to proper use data pointers with inplace option. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored depthToSpace helpers. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored histogram helpers. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored im2col helpers. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored gather and gatherND helpers. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed buffer usage on percentile helper. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed gather shape with helpers and range buffer usage. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed buffer usage with space to depth helpers. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed buffer usage and constant shapes. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed buffer usage with LUP decomposition> Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored onehot_ helper. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored pad and prefix to use constant shapes. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactoed softmax helpers. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed space to batch helpers to use buffers properly. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed stack and split helpers. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed buffer usage with sparse to dense helpers. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed buffer usage with mindistance_ helpers. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed buffer usage with tile helper. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed constant shape usage. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed constant shape usage with legacy pairwise bool ops. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored a couple of methods to adopt constant shape usage. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed broadcasting with constant shape." Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed const usage with inplace reverse and constant shapes with legacy reduction. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored legacy ops with const shapes. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored sort to adopt constant shapes. Signed-off-by: shugeo <sgazeos@gmail.com> * Corrected sort for constant shape usage. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed constant shape usage with special methods. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored Context to conform with constant shape usage. Signed-off-by: shugeo <sgazeos@gmail.com> * CUDA broadcasting headers Signed-off-by: raver119@gmail.com <raver119@gmail.com> * pairwise/indexreduce/random headers Signed-off-by: raver119@gmail.com <raver119@gmail.com> * Refactored native ops to adopt constant shapes. Signed-off-by: shugeo <sgazeos@gmail.com> * legacy reduce3/scalar headers Signed-off-by: raver119@gmail.com <raver119@gmail.com> * Corrected pullRow signature and tests. Signed-off-by: shugeo <sgazeos@gmail.com> * Corrected routines to proper use of constant shapes. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored tests to use constant shapes properly. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored legacy ops tests to use constant shapes properly. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored buffer usage with NDArray tests. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed native ops tests. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed special concat routine. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed buffer usage with test. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed buffer usage with a test. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored TAD.h and tests. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored calcStrides* routines to use constant shapes. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed miscelaneous errors with constant shapes. Signed-off-by: shugeo <sgazeos@gmail.com> * NativeOps const changes Signed-off-by: raver119@gmail.com <raver119@gmail.com> * Corrected definitions for declared functions. Signed-off-by: shugeo <sgazeos@gmail.com> * NativeOps const changes Signed-off-by: raver119@gmail.com <raver119@gmail.com> * few more const changes Signed-off-by: raver119@gmail.com <raver119@gmail.com> * Fixed const shapes with shape routines. Signed-off-by: shugeo <sgazeos@gmail.com> * few more const changes Signed-off-by: raver119@gmail.com <raver119@gmail.com> * Fixed shape method for broadcastable case. Signed-off-by: shugeo <sgazeos@gmail.com> * few more const changes Signed-off-by: raver119@gmail.com <raver119@gmail.com> * xw_plus_b BP shape fn restored Signed-off-by: raver119@gmail.com <raver119@gmail.com> * Fixed signatures with broadcasting. Signed-off-by: shugeo <sgazeos@gmail.com> * Repaired backprops shape methods for a set of operations. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored broadcast bool for cuda. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored methods for 3 args with const qualifier. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed a couple of kernel signatures for broadcasting. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed kernels signatures for const buffers and shapes. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored pairwise methods to persistent buffers and shapes usage. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopt const to buffers and shapes with kernels. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopt const to buffers and shapes with scalar kernels. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored indexreduce kernels signatures to use const buffers and shapes. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored pairwise kernels to adopt cons shapes and buffers. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored pairwise bool kernels to adopt cons shapes and buffers. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored random special ops to conform with const shapes and buffers. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored native ops to conform with const shapes and buffers under cuda platform. Signed-off-by: shugeo <sgazeos@gmail.com> * Cosmetical changes only. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed const shapes and buffers error. Signed-off-by: shugeo <sgazeos@gmail.com> * Corrected start pos routine. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored methods to conform with const shapes and buffers. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored helpers to use proper methods instead. Signed-off-by: shugeo <sgazeos@gmail.com> * bunch of changes Signed-off-by: raver119@gmail.com <raver119@gmail.com> * next bunch of changes Signed-off-by: raver119@gmail.com <raver119@gmail.com> * next bunch of changes Signed-off-by: raver119@gmail.com <raver119@gmail.com> * Fixed execScalar declaration. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed execScalar declaration. Signed-off-by: shugeo <sgazeos@gmail.com> * Corrected const shape cases with sort and so on. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed const shapes for sort. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored kernel declarations to adopt const shapes. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed kernels declarations to adopt const shapes. Signed-off-by: shugeo <sgazeos@gmail.com> * Corrected kernel declarations to adopt const shapes and buffers. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed kernels declarations to adopt const shapes. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed segment helpers kernels declarations and so on to adopt const shapes. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed const shape usage with segment and solve helpers. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed kernel declaration with adjustWeight helper. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed cuda implementations for constant shape helpers. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopted const shape usage with kernels. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopted top_k kernels to use const shapes and buffers. Signed-off-by: shugeo <sgazeos@gmail.com> * Corrected kernels declarations to adopt const shapes with helpers. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored NDArray definitions to adopt const shapes and buffers. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed const shapes with image suppression helpers. Signed-off-by: shugeo <sgazeos@gmail.com> * Slight improvement with buffers. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored buffer usage. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored buffer usage with tests. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed const shape usage with definitions. Signed-off-by: shugeo <sgazeos@gmail.com> * minor updates on cpu side Signed-off-by: raver119@gmail.com <raver119@gmail.com> * Refactored const shape usage with ConstantDescritor and native ops with cuda platform. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored tear and tile kernels to adopt with const shapes. Signed-off-by: shugeo <sgazeos@gmail.com> * softmax_loop fix Signed-off-by: raver119 <raver119@gmail.com> * update missing signature Signed-off-by: raver119@gmail.com <raver119@gmail.com> * softmax again Signed-off-by: raver119@gmail.com <raver119@gmail.com> * few more missing consts Signed-off-by: raver119 <raver119@gmail.com> * new methods updated Signed-off-by: raver119@gmail.com <raver119@gmail.com> Co-authored-by: shugeo <sgazeos@gmail.com>
547 lines
25 KiB
Plaintext
547 lines
25 KiB
Plaintext
/*******************************************************************************
|
|
* 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 Yurii Shyrma (iuriish@yahoo.com), created on 20.04.2018
|
|
//
|
|
|
|
|
|
#include<ops/declarable/helpers/transforms.h>
|
|
#include <array/ResultSet.h>
|
|
#include <helpers/ShapeUtils.h>
|
|
#include <numeric>
|
|
#include <array/NDArrayFactory.h>
|
|
#include <helpers/TAD.h>
|
|
#include <exceptions/cuda_exception.h>
|
|
#include <helpers/PointersManager.h>
|
|
#include <helpers/ConstantTadHelper.h>
|
|
|
|
namespace sd {
|
|
namespace ops {
|
|
namespace helpers {
|
|
//////////////////////////////////////////////////////////////////////////
|
|
template <typename T, typename Z>
|
|
static __global__ void mergeMaxIndexCudaLauncher(void** inArrs, void** inShapes, const int numArrays, void* voutput, const Nd4jLong* outputShape, Nd4jLong length) {
|
|
auto output = reinterpret_cast<Z*>(voutput);
|
|
|
|
const auto tid = blockIdx.x * blockDim.x + threadIdx.x;
|
|
const auto step = gridDim.x * blockDim.x;
|
|
|
|
for (Nd4jLong e = tid; e < length; e += step) {
|
|
T mVal = -DataTypeUtils::max<T>();
|
|
Z mIdx(0);
|
|
|
|
for (int i = 0; i < numArrays; i++) {
|
|
auto x = reinterpret_cast<T*>(inArrs[i]);
|
|
auto xShape = reinterpret_cast<Nd4jLong*>(inShapes[i]);
|
|
auto val = x[shape::getIndexOffset(e, xShape)];;
|
|
if (mVal < val) {
|
|
mIdx = static_cast<Z>(i);
|
|
mVal = val;
|
|
}
|
|
}
|
|
|
|
output[shape::getIndexOffset(e, outputShape)] = mIdx;
|
|
}
|
|
}
|
|
|
|
template <typename T, typename Z>
|
|
static void mergeMaxIndex_(sd::LaunchContext* context, const std::vector<const NDArray*>& inArrs, NDArray& output) {
|
|
|
|
int nArrSize = static_cast<int>(inArrs.size());
|
|
std::vector<const void*> inBuffers(nArrSize), inShapes(nArrSize);
|
|
|
|
for (int e = 0; e < nArrSize; e++) {
|
|
inBuffers[e] = inArrs[e]->specialBuffer();
|
|
inShapes[e] = inArrs[e]->specialShapeInfo();
|
|
}
|
|
|
|
PointersManager manager(context, "mergeMaxIndex");
|
|
|
|
auto pInBuffers = reinterpret_cast<void**>(manager.replicatePointer(inBuffers.data(), inBuffers.size() * sizeof(void*)));
|
|
auto pInShapes = reinterpret_cast<void**>(manager.replicatePointer(inShapes.data(), inShapes.size() * sizeof(void*)));
|
|
auto length = output.lengthOf();
|
|
|
|
const int threadsPerBlock = MAX_NUM_THREADS / 2;
|
|
const int blocksPerGrid = (length + threadsPerBlock - 1) / threadsPerBlock;
|
|
|
|
mergeMaxIndexCudaLauncher<T, Z><<<blocksPerGrid, threadsPerBlock, 512, *context->getCudaStream()>>>(pInBuffers, pInShapes, nArrSize, output.specialBuffer(), output.specialShapeInfo(), length);
|
|
|
|
manager.synchronize();
|
|
}
|
|
|
|
void mergeMaxIndex(sd::LaunchContext* context, const std::vector<const NDArray*>& inArrs, NDArray& output) {
|
|
|
|
NDArray::prepareSpecialUse({ &output }, inArrs);
|
|
|
|
BUILD_DOUBLE_SELECTOR(inArrs[0]->dataType(), output.dataType(), mergeMaxIndex_, (context, inArrs, output), LIBND4J_TYPES, INDEXING_TYPES);
|
|
|
|
NDArray::registerSpecialUse({ &output }, inArrs);
|
|
}
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
template <typename T>
|
|
static __global__ void mergeMaxCudaLauncher(void** inArrs, void** inShapes, const int numArrays, void* voutput, const Nd4jLong* outputShape, Nd4jLong length) {
|
|
auto output = reinterpret_cast<T*>(voutput);
|
|
|
|
const auto tid = blockIdx.x * blockDim.x + threadIdx.x;
|
|
const auto step = gridDim.x * blockDim.x;
|
|
|
|
for (Nd4jLong e = tid; e < length; e += step) {
|
|
T mVal = -DataTypeUtils::max<T>();
|
|
|
|
for (int i = 0; i < numArrays; i++) {
|
|
auto x = reinterpret_cast<const T*>(inArrs[i]);
|
|
auto xShape = reinterpret_cast<const Nd4jLong*>(inShapes[i]);
|
|
auto val = x[shape::getIndexOffset(e, xShape)];;
|
|
if (mVal < val)
|
|
mVal = val;
|
|
}
|
|
|
|
output[shape::getIndexOffset(e, outputShape)] = mVal;
|
|
}
|
|
}
|
|
|
|
template<typename T>
|
|
static void mergeMax_(sd::LaunchContext* context, const std::vector<const NDArray*>& inArrs, NDArray& output) {
|
|
|
|
int nArrsSize = static_cast<int>(inArrs.size());
|
|
|
|
std::vector<const void*> inBuffers(nArrsSize), inShapes(nArrsSize);
|
|
|
|
for (int e = 0; e < nArrsSize; e++) {
|
|
inBuffers[e] = inArrs[e]->specialBuffer();
|
|
inShapes[e] = inArrs[e]->specialShapeInfo();
|
|
}
|
|
|
|
PointersManager manager(context, "mergeMax");
|
|
|
|
auto pInBuffers = reinterpret_cast<void**>(manager.replicatePointer(inBuffers.data(), inBuffers.size() * sizeof(void*)));
|
|
auto pInShapes = reinterpret_cast<void**>(manager.replicatePointer(inShapes.data(), inShapes.size() * sizeof(void*)));
|
|
auto length = output.lengthOf();
|
|
|
|
const int threadsPerBlock = MAX_NUM_THREADS / 2;
|
|
const int blocksPerGrid = (length + threadsPerBlock - 1) / threadsPerBlock;
|
|
|
|
mergeMaxCudaLauncher<T><<<blocksPerGrid, threadsPerBlock, 512, *context->getCudaStream()>>>(pInBuffers, pInShapes, nArrsSize, output.specialBuffer(), output.specialShapeInfo(), length);
|
|
|
|
manager.synchronize();
|
|
}
|
|
|
|
void mergeMax(sd::LaunchContext* context, const std::vector<const NDArray*>& inArrs, NDArray& output) {
|
|
|
|
NDArray::prepareSpecialUse({ &output }, inArrs);
|
|
|
|
BUILD_SINGLE_SELECTOR(output.dataType(), mergeMax_, (context, inArrs, output), LIBND4J_TYPES);
|
|
|
|
NDArray::registerSpecialUse({ &output }, inArrs);
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
template <typename T>
|
|
static __global__ void mergeMaxBpCudaLauncher(
|
|
void** inArrs, void** inShapes,
|
|
const void* vgradient, const Nd4jLong* gradientShape,
|
|
const int numArrays,
|
|
void** outArrs, void** outShapes,
|
|
Nd4jLong length,
|
|
bool bSameOrderAndEws1) {
|
|
|
|
auto grad = reinterpret_cast<const T*>(vgradient);
|
|
|
|
const auto tid = blockIdx.x * blockDim.x + threadIdx.x;
|
|
const auto step = gridDim.x * blockDim.x;
|
|
|
|
int coords[MAX_RANK];
|
|
|
|
for (Nd4jLong e = tid; e < length; e += step) {
|
|
|
|
T mVal = -DataTypeUtils::max<T>();
|
|
int nMaxIndex = 0;
|
|
auto xOffset = e, zOffset = e, gradOffset = e;
|
|
|
|
if (!bSameOrderAndEws1) {
|
|
shape::index2coords(e, gradientShape, coords);
|
|
gradOffset = shape::getOffset(gradientShape, coords);
|
|
}
|
|
|
|
for (int i = 0; i < numArrays; i++) {
|
|
auto x = reinterpret_cast<T*>(inArrs[i]);
|
|
|
|
if (!bSameOrderAndEws1) {
|
|
auto xShape = reinterpret_cast<Nd4jLong*>(inShapes[i]);
|
|
xOffset = shape::getOffset(xShape, coords);
|
|
}
|
|
|
|
auto val = x[xOffset];
|
|
if (mVal < val) {
|
|
mVal = val;
|
|
nMaxIndex = i;
|
|
}
|
|
}
|
|
|
|
// outputs have to be pre-nullify
|
|
if (!bSameOrderAndEws1) {
|
|
auto outShape = reinterpret_cast<Nd4jLong*>(outShapes[nMaxIndex]);
|
|
zOffset = shape::getOffset(outShape, coords);
|
|
}
|
|
|
|
auto output = reinterpret_cast<T*>(outArrs[nMaxIndex]);
|
|
|
|
output[zOffset] = grad[gradOffset];
|
|
}
|
|
}
|
|
|
|
template<typename T>
|
|
static void mergeMaxBp_(sd::LaunchContext* context, const std::vector<const NDArray*>& inArrs, std::vector<NDArray*>& outArrs, int nArrSize, bool bSameOrderAndEws1) {
|
|
|
|
std::vector<const void*> inBuffers(nArrSize), inShapes(nArrSize), outBuffers(nArrSize), outShapes(nArrSize);
|
|
|
|
for (int e = 0; e < nArrSize; e++) {
|
|
inBuffers[e] = inArrs[e]->specialBuffer();
|
|
inShapes[e] = inArrs[e]->specialShapeInfo();
|
|
outBuffers[e] = outArrs[e]->specialBuffer();
|
|
outShapes[e] = outArrs[e]->specialShapeInfo();
|
|
}
|
|
|
|
PointersManager manager(context, "mergeMaxBp");
|
|
|
|
auto pInBuffers = reinterpret_cast<void**>(manager.replicatePointer(inBuffers.data(), inBuffers.size() * sizeof(void*)));
|
|
auto pInShapes = reinterpret_cast<void**>(manager.replicatePointer(inShapes.data(), inShapes.size() * sizeof(void*)));
|
|
|
|
auto pOutBuffers = reinterpret_cast<void**>(manager.replicatePointer(outBuffers.data(), outBuffers.size() * sizeof(void*)));
|
|
auto pOutShapes = reinterpret_cast<void**>(manager.replicatePointer(outShapes.data(), outShapes.size() * sizeof(void*)));
|
|
|
|
auto length = inArrs[nArrSize]->lengthOf();
|
|
|
|
const int threadsPerBlock = MAX_NUM_THREADS / 2;
|
|
const int blocksPerGrid = (length + threadsPerBlock - 1) / threadsPerBlock;
|
|
|
|
mergeMaxBpCudaLauncher<T><<<blocksPerGrid, threadsPerBlock, 512, *context->getCudaStream()>>>(pInBuffers, pInShapes, inArrs[nArrSize]->specialBuffer(),
|
|
inArrs[nArrSize]->specialShapeInfo(), nArrSize, pOutBuffers, pOutShapes,
|
|
length, bSameOrderAndEws1);
|
|
|
|
manager.synchronize();
|
|
}
|
|
|
|
void mergeMaxBp(sd::LaunchContext* context, const std::vector<const NDArray*>& inArrs, std::vector<NDArray*>& outArrs) {
|
|
|
|
// not use gradient
|
|
int nArrSize = static_cast<int>(inArrs.size() - 1);
|
|
|
|
const std::vector<const NDArray*>& out = reinterpret_cast<const std::vector<const NDArray*>&>(outArrs);
|
|
|
|
NDArray::prepareSpecialUse(out, inArrs);
|
|
|
|
bool bSameOrderAndEws1 = (1 == inArrs[nArrSize]->ews());
|
|
auto ordering = inArrs[nArrSize]->ordering();
|
|
|
|
for (int i = 0; i < nArrSize; ++i) {
|
|
bSameOrderAndEws1 &= (ordering == inArrs[i]->ordering());
|
|
bSameOrderAndEws1 &= (1 == inArrs[i]->ews());
|
|
|
|
bSameOrderAndEws1 &= (ordering == outArrs[i]->ordering());
|
|
bSameOrderAndEws1 &= (1 == outArrs[i]->ews());
|
|
}
|
|
|
|
BUILD_SINGLE_SELECTOR(inArrs[nArrSize]->dataType(), mergeMaxBp_, (context, inArrs, outArrs, nArrSize, bSameOrderAndEws1), LIBND4J_TYPES);
|
|
|
|
NDArray::registerSpecialUse( out, inArrs );
|
|
}
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
template <typename T>
|
|
static __global__ void mergeAvgCudaLauncher(void** inArrs, void** inShapes, const int numArrays, void* voutput, const Nd4jLong* outputShape, Nd4jLong length) {
|
|
auto output = reinterpret_cast<T*>(voutput);
|
|
|
|
const auto tid = blockIdx.x * blockDim.x + threadIdx.x;
|
|
const auto step = gridDim.x * blockDim.x;
|
|
|
|
for (Nd4jLong e = tid; e < length; e += step) {
|
|
T sum(0.0f);
|
|
|
|
for (int i = 0; i < numArrays; i++) {
|
|
auto x = reinterpret_cast<T*>(inArrs[i]);
|
|
auto xShape = reinterpret_cast<Nd4jLong*>(inShapes[i]);
|
|
|
|
sum += x[shape::getIndexOffset(e, xShape)];
|
|
}
|
|
|
|
output[shape::getIndexOffset(e, outputShape)] = sum / numArrays;
|
|
}
|
|
}
|
|
|
|
template<typename T>
|
|
static void mergeAvg_(sd::LaunchContext* context, const std::vector<const NDArray*>& inArrs, NDArray& output) {
|
|
|
|
std::vector<const void*> inBuffers(inArrs.size()), inShapes(inArrs.size());
|
|
|
|
for (int e = 0; e < inArrs.size(); e++) {
|
|
inBuffers[e] = inArrs[e]->specialBuffer();
|
|
inShapes[e] = inArrs[e]->specialShapeInfo();
|
|
}
|
|
|
|
PointersManager manager(context, "mergeAvg");
|
|
|
|
auto pInBuffers = reinterpret_cast<void**>(manager.replicatePointer(inBuffers.data(), inBuffers.size() * sizeof(void*)));
|
|
auto pInShapes = reinterpret_cast<void**>(manager.replicatePointer(inShapes.data(), inShapes.size() * sizeof(void*)));
|
|
auto length = output.lengthOf();
|
|
|
|
const int threadsPerBlock = MAX_NUM_THREADS / 2;
|
|
const int blocksPerGrid = (length + threadsPerBlock - 1) / threadsPerBlock;
|
|
|
|
mergeAvgCudaLauncher<T><<<blocksPerGrid, threadsPerBlock, 512, *context->getCudaStream()>>>(pInBuffers, pInShapes, (int)inArrs.size(), output.specialBuffer(), output.specialShapeInfo(), length);
|
|
|
|
manager.synchronize();
|
|
}
|
|
|
|
void mergeAvg(sd::LaunchContext* context, const std::vector<const NDArray*>& inArrs, NDArray& output) {
|
|
|
|
NDArray::prepareSpecialUse({ &output }, inArrs);
|
|
|
|
BUILD_SINGLE_SELECTOR(output.dataType(), mergeAvg_, (context, inArrs, output), FLOAT_TYPES);
|
|
|
|
NDArray::registerSpecialUse({ &output }, inArrs);
|
|
}
|
|
//////////////////////////////////////////////////////////////////////////
|
|
template <typename T>
|
|
static __global__ void mergeAvgBpCudaLauncher(
|
|
const void* vgradient, const Nd4jLong* gradientShape,
|
|
void** outArrs, void** outShapes,
|
|
const int numArrays,
|
|
Nd4jLong length,
|
|
bool bSameOrderAndEws1) {
|
|
|
|
auto grad = reinterpret_cast<const T*>(vgradient);
|
|
|
|
const auto tid = blockIdx.x * blockDim.x + threadIdx.x;
|
|
const auto step = gridDim.x * blockDim.x;
|
|
|
|
int coords[MAX_RANK];
|
|
|
|
for (Nd4jLong e = tid; e < length; e += step) {
|
|
|
|
auto zOffset = e, gradOffset = e;
|
|
if (!bSameOrderAndEws1) {
|
|
shape::index2coords(e, gradientShape, coords);
|
|
gradOffset = shape::getOffset(gradientShape, coords);
|
|
}
|
|
|
|
for (int i = 0; i < numArrays; i++) {
|
|
|
|
if (!bSameOrderAndEws1) {
|
|
auto outShape = reinterpret_cast<Nd4jLong*>(outShapes[i]);
|
|
zOffset = shape::getOffset(outShape, coords);
|
|
}
|
|
|
|
auto output = reinterpret_cast<T*>(outArrs[i]);
|
|
|
|
output[zOffset] = grad[gradOffset] / numArrays;
|
|
}
|
|
}
|
|
}
|
|
|
|
template<typename T>
|
|
static void mergeAvgBp_(sd::LaunchContext* context, const NDArray& gradient, std::vector<NDArray*>& outArrs, bool bSameOrderAndEws1) {
|
|
|
|
int nArrSize = static_cast<int>(outArrs.size());
|
|
|
|
std::vector<const void*> outBuffers(nArrSize), outShapes(nArrSize);
|
|
|
|
for (int e = 0; e < nArrSize; e++) {
|
|
outBuffers[e] = outArrs[e]->specialBuffer();
|
|
outShapes[e] = outArrs[e]->specialShapeInfo();
|
|
}
|
|
|
|
PointersManager manager(context, "mergeAvgBp");
|
|
|
|
auto pOutBuffers = reinterpret_cast<void**>(manager.replicatePointer(outBuffers.data(), outBuffers.size() * sizeof(void*)));
|
|
auto pOutShapes = reinterpret_cast<void**>(manager.replicatePointer(outShapes.data(), outShapes.size() * sizeof(void*)));
|
|
|
|
auto length = gradient.lengthOf();
|
|
|
|
const int threadsPerBlock = MAX_NUM_THREADS / 2;
|
|
const int blocksPerGrid = (length + threadsPerBlock - 1) / threadsPerBlock;
|
|
|
|
mergeAvgBpCudaLauncher<T><<<blocksPerGrid, threadsPerBlock, 512, *context->getCudaStream()>>>(gradient.specialBuffer(), gradient.specialShapeInfo(),
|
|
pOutBuffers, pOutShapes, nArrSize, length, bSameOrderAndEws1);
|
|
|
|
manager.synchronize();
|
|
}
|
|
|
|
void mergeAvgBp(sd::LaunchContext* context, const NDArray& gradient, std::vector<NDArray*>& outArrs) {
|
|
|
|
const std::vector<const NDArray*>& out = reinterpret_cast<const std::vector<const NDArray*>&>(outArrs);
|
|
|
|
NDArray::prepareSpecialUse( out, { &gradient });
|
|
|
|
bool bSameOrderAndEws1 = (1 == gradient.ews());
|
|
auto ordering = gradient.ordering();
|
|
|
|
for (const auto& v : outArrs) {
|
|
bSameOrderAndEws1 &= (ordering == v->ordering());
|
|
bSameOrderAndEws1 &= (1 == v->ews());
|
|
}
|
|
|
|
BUILD_SINGLE_SELECTOR(gradient.dataType(), mergeAvgBp_, (context, gradient, outArrs, bSameOrderAndEws1), LIBND4J_TYPES);
|
|
|
|
NDArray::prepareSpecialUse(out, { &gradient });
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
template <typename T>
|
|
static __global__ void mergeAddCudaLauncher(void** inArrs, void** inShapes, const int numArrays, void* voutput, const Nd4jLong* outputShape, Nd4jLong length) {
|
|
|
|
auto output = reinterpret_cast<T*>(voutput);
|
|
|
|
const auto tid = blockIdx.x * blockDim.x + threadIdx.x;
|
|
const auto step = gridDim.x * blockDim.x;
|
|
|
|
for (Nd4jLong e = tid; e < length; e += step) {
|
|
T sum(0.0f);
|
|
|
|
for (int i = 0; i < numArrays; i++) {
|
|
auto x = reinterpret_cast<T*>(inArrs[i]);
|
|
auto xShape = reinterpret_cast<Nd4jLong*>(inShapes[i]);
|
|
|
|
sum += x[shape::getIndexOffset(e, xShape)];
|
|
}
|
|
|
|
output[shape::getIndexOffset(e, outputShape)] = sum;
|
|
}
|
|
}
|
|
|
|
template<typename T>
|
|
static void mergeAdd_(sd::LaunchContext* context, const std::vector<const NDArray*>& inArrs, NDArray& output) {
|
|
|
|
int nArrSize = static_cast<int>(inArrs.size());
|
|
std::vector<const void*> inBuffers(nArrSize), inShapes(nArrSize);
|
|
|
|
for (int e = 0; e < nArrSize; e++) {
|
|
inBuffers[e] = inArrs[e]->specialBuffer();
|
|
inShapes[e] = inArrs[e]->specialShapeInfo();
|
|
}
|
|
|
|
PointersManager manager(context, "mergeAdd");
|
|
|
|
auto pInBuffers = reinterpret_cast<void**>(manager.replicatePointer(inBuffers.data(), inBuffers.size() * sizeof(void*)));
|
|
auto pInShapes = reinterpret_cast<void**>(manager.replicatePointer(inShapes.data(), inShapes.size() * sizeof(void*)));
|
|
auto length = output.lengthOf();
|
|
|
|
const int threadsPerBlock = MAX_NUM_THREADS / 2;
|
|
const int blocksPerGrid = (length + threadsPerBlock - 1) / threadsPerBlock;
|
|
|
|
mergeAddCudaLauncher<T><<<blocksPerGrid, threadsPerBlock, 512, *context->getCudaStream()>>>(pInBuffers, pInShapes, nArrSize, output.specialBuffer(), output.specialShapeInfo(), length);
|
|
|
|
manager.synchronize();
|
|
}
|
|
BUILD_SINGLE_TEMPLATE(template void mergeAdd_, (sd::LaunchContext* context, const std::vector<const NDArray*>& inArrs, NDArray& output), NUMERIC_TYPES);
|
|
|
|
void mergeAdd(sd::LaunchContext* context, const std::vector<const NDArray*>& inArrs, NDArray& output) {
|
|
|
|
NDArray::prepareSpecialUse({ &output }, inArrs);
|
|
|
|
BUILD_SINGLE_SELECTOR(output.dataType(), mergeAdd_, (context, inArrs, output), NUMERIC_TYPES);
|
|
|
|
NDArray::registerSpecialUse({ &output }, inArrs);
|
|
}
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
template <typename T>
|
|
static __global__ void mergeAddBpCudaLauncher(const void* vgradient, const Nd4jLong* gradientShape, void** outArrs, void** outShapes,
|
|
const int numArrays, Nd4jLong length, bool bSameOrderAndEws1) {
|
|
|
|
auto grad = reinterpret_cast<const T*>(vgradient);
|
|
|
|
const auto tid = blockIdx.x * blockDim.x + threadIdx.x;
|
|
const auto step = gridDim.x * blockDim.x;
|
|
|
|
int coords[MAX_RANK];
|
|
|
|
for (Nd4jLong e = tid; e < length; e += step) {
|
|
|
|
auto zOffset = e, gradOffset = e;
|
|
if (!bSameOrderAndEws1) {
|
|
shape::index2coords(e, gradientShape, coords);
|
|
gradOffset = shape::getOffset(gradientShape, coords);
|
|
}
|
|
|
|
for (int i = 0; i < numArrays; i++) {
|
|
|
|
if (!bSameOrderAndEws1) {
|
|
auto outShape = reinterpret_cast<Nd4jLong*>(outShapes[i]);
|
|
zOffset = shape::getOffset(outShape, coords);
|
|
}
|
|
|
|
auto output = reinterpret_cast<T*>(outArrs[i]);
|
|
|
|
output[zOffset] = grad[gradOffset];
|
|
}
|
|
}
|
|
}
|
|
|
|
template<typename T>
|
|
static void mergeAddBp_(sd::LaunchContext* context, const NDArray& gradient, std::vector<NDArray*>& outArrs, bool bSameOrderAndEws1) {
|
|
|
|
int nArrSize = static_cast<int>(outArrs.size());
|
|
|
|
std::vector<const void*> outBuffers(nArrSize), outShapes(nArrSize);
|
|
|
|
for (int e = 0; e < nArrSize; e++) {
|
|
outBuffers[e] = outArrs[e]->specialBuffer();
|
|
outShapes[e] = outArrs[e]->specialShapeInfo();
|
|
}
|
|
|
|
PointersManager manager(context, "mergeAddBp");
|
|
|
|
auto pOutBuffers = reinterpret_cast<void**>(manager.replicatePointer(outBuffers.data(), outBuffers.size() * sizeof(void*)));
|
|
auto pOutShapes = reinterpret_cast<void**>(manager.replicatePointer(outShapes.data(), outShapes.size() * sizeof(void*)));
|
|
|
|
auto length = gradient.lengthOf();
|
|
|
|
const int threadsPerBlock = MAX_NUM_THREADS / 2;
|
|
const int blocksPerGrid = (length + threadsPerBlock - 1) / threadsPerBlock;
|
|
|
|
mergeAddBpCudaLauncher<T><<<blocksPerGrid, threadsPerBlock, 512, *context->getCudaStream()>>>(gradient.specialBuffer(), gradient.specialShapeInfo(),
|
|
pOutBuffers, pOutShapes, nArrSize, length, bSameOrderAndEws1);
|
|
|
|
manager.synchronize();
|
|
}
|
|
|
|
void mergeAddBp(sd::LaunchContext* context, const NDArray& gradient, std::vector<NDArray*>& outArrs) {
|
|
|
|
const std::vector<const NDArray*>& out = reinterpret_cast<const std::vector<const NDArray*>& >(outArrs);
|
|
NDArray::prepareSpecialUse( out, { &gradient });
|
|
|
|
bool bSameOrderAndEws1 = (1 == gradient.ews());
|
|
auto ordering = gradient.ordering();
|
|
|
|
for (const auto& v : outArrs) {
|
|
bSameOrderAndEws1 &= (ordering == v->ordering());
|
|
bSameOrderAndEws1 &= (1 == v->ews());
|
|
}
|
|
|
|
BUILD_SINGLE_SELECTOR(gradient.dataType(), mergeAddBp_, (context, gradient, outArrs, bSameOrderAndEws1), LIBND4J_TYPES);
|
|
|
|
NDArray::prepareSpecialUse( out, { &gradient });
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|