/* ****************************************************************************** * * * 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. * * See the NOTICE file distributed with this work for additional * information regarding copyright ownership. * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. * * SPDX-License-Identifier: Apache-2.0 ******************************************************************************/ // // @author GS // #include #include #include #include #include #include #include #include #include #include "helpers/DebugHelper.h" namespace sd { namespace ops { namespace helpers { // -------------------------------------------------------------------------------------------------------------- // // Segment ops linear kernels // -------------------------------------------------------------------------------------------------------------- // template static SD_KERNEL void segmentMaxLinearKernel(void* input, LongType const* inputShape, LongType* starts, LongType* lengths, LongType numOfClasses, void* output, LongType const* outputShape) { __shared__ T* val; __shared__ LongType xLen, zLen, zIndex; __shared__ T* x; __shared__ T* z; __shared__ LongType threadsPerSegment, start, finish; // Cache shape information __shared__ sd::LongType inputRank, outputRank; __shared__ const sd::LongType* inputStridePtr; __shared__ const sd::LongType* outputStridePtr; auto segment = blockIdx.x; if (threadIdx.x == 0) { x = reinterpret_cast(input); z = reinterpret_cast(output); extern __shared__ unsigned char shmem[]; val = reinterpret_cast(shmem); xLen = shape::length(inputShape); zLen = shape::length(outputShape); // Cache shape information inputRank = shape::rank(inputShape); outputRank = shape::rank(outputShape); inputStridePtr = shape::stride(inputShape); outputStridePtr = shape::stride(outputShape); if (segment < numOfClasses) { LongType segmentCoords[] = {segment}; COORDS2INDEX(1, outputStridePtr, segmentCoords, zIndex); start = starts[segment]; finish = start + lengths[segment]; LongType startCoords[] = {start}; LongType xOffset; COORDS2INDEX(1, inputStridePtr, startCoords, xOffset); z[zIndex] = x[xOffset]; val[segment] = z[zIndex]; } } __syncthreads(); for (auto e = start + threadIdx.x + 1; e < finish; e += blockDim.x) { LongType eCoords[] = {e}; LongType xIndex; COORDS2INDEX(1, inputStridePtr, eCoords, xIndex); math::atomics::sd_atomicMax(&z[zIndex], x[xIndex]); } } template static SD_KERNEL void unsortedSegmentMaxLinearKernel(void* input, LongType const* inputShape, void* indices, LongType const* indicesShape, LongType* starts, LongType* lengths, LongType numOfClasses, void* output, LongType const* outputShape) { __shared__ LongType xLen, zLen, zIndex; __shared__ T* x; __shared__ T* z; __shared__ I* y; // Cache shape information __shared__ sd::LongType inputRank, outputRank, indicesRank; __shared__ const sd::LongType* inputStridePtr; __shared__ const sd::LongType* outputStridePtr; __shared__ const sd::LongType* indicesStridePtr; auto segment = blockIdx.x; if (threadIdx.x == 0) { x = reinterpret_cast(input); z = reinterpret_cast(output); y = reinterpret_cast(indices); xLen = shape::length(inputShape); zLen = shape::length(outputShape); // Cache shape information inputRank = shape::rank(inputShape); outputRank = shape::rank(outputShape); indicesRank = shape::rank(indicesShape); inputStridePtr = shape::stride(inputShape); outputStridePtr = shape::stride(outputShape); indicesStridePtr = shape::stride(indicesShape); LongType segmentCoords[] = {segment}; COORDS2INDEX(1, outputStridePtr, segmentCoords, zIndex); if (lengths[segment] > 0) { LongType startCoords[] = {starts[segment]}; LongType xOffset; COORDS2INDEX(1, inputStridePtr, startCoords, xOffset); z[zIndex] = x[xOffset]; } else { z[zIndex] = -DataTypeUtils::max(); } } __syncthreads(); if (lengths[segment] > 0) { for (auto e = threadIdx.x + 1; e < xLen; e += blockDim.x) { LongType eCoords[] = {e}; LongType xIndex, yIndex; COORDS2INDEX(1, inputStridePtr, eCoords, xIndex); COORDS2INDEX(1, indicesStridePtr, eCoords, yIndex); if (y[yIndex] == segment) { math::atomics::sd_atomicMax(&z[zIndex], x[xIndex]); } } } } template static SD_KERNEL void segmentMaxTadKernel(void* inputBuf, LongType const* inputShape, LongType const* inputTads, LongType const* inputTadOffsets, I* indices, LongType* starts, LongType* lengths, LongType numOfClasses, void* outputBuf, LongType const* outputShape, LongType const* outputTads, LongType const* outputTadOffsets, T filler, LongType indicesLength, LongType numInputTads, LongType numOutputTads) { __shared__ T* val; __shared__ LongType len, zIndex, total, zLen; __shared__ T* z; __shared__ int start, finish; __shared__ I segment; // Cache shape information __shared__ sd::LongType inputTadRank, outputTadRank; __shared__ const sd::LongType* inputTadShapePtr; __shared__ const sd::LongType* outputTadShapePtr; __shared__ const sd::LongType* inputTadStridePtr; __shared__ const sd::LongType* outputTadStridePtr; if (threadIdx.x == 0 && blockIdx.x < indicesLength) { segment = indices[blockIdx.x]; zLen = shape::length(outputShape); auto zOffset = outputTadOffsets[segment]; z = reinterpret_cast(outputBuf) + outputTadOffsets[segment]; len = shape::length(inputTads); // Cache shape information inputTadRank = shape::rank(inputTads); outputTadRank = shape::rank(outputTads); inputTadShapePtr = shape::shapeOf(inputTads); outputTadShapePtr = shape::shapeOf(outputTads); inputTadStridePtr = shape::stride(inputTads); outputTadStridePtr = shape::stride(outputTads); start = starts[segment]; finish = start + lengths[segment]; total = shape::sizeAt(inputShape, 0); } __syncthreads(); auto idx = blockIdx.x; if (idx < numInputTads) { auto x = reinterpret_cast(inputBuf) + inputTadOffsets[idx]; if (blockIdx.x == start) { for (auto e = threadIdx.x; e < len; e += blockDim.x) { LongType xCoords[SD_MAX_RANK]; LongType zCoords[SD_MAX_RANK]; LongType xIndex; LongType zIndex; INDEX2COORDS(e, inputTadRank, inputTadShapePtr, xCoords); COORDS2INDEX(inputTadRank, inputTadStridePtr, xCoords, xIndex); INDEX2COORDS(e, outputTadRank, outputTadShapePtr, zCoords); COORDS2INDEX(outputTadRank, outputTadStridePtr, zCoords, zIndex); math::atomics::sd_atomicMax(&z[zIndex], x[xIndex]); } } else { for (auto e = threadIdx.x; e < len; e += blockDim.x) { LongType xCoords[SD_MAX_RANK]; LongType zCoords[SD_MAX_RANK]; LongType xIndex; LongType zIndex; INDEX2COORDS(e, inputTadRank, inputTadShapePtr, xCoords); COORDS2INDEX(inputTadRank, inputTadStridePtr, xCoords, xIndex); INDEX2COORDS(e, outputTadRank, outputTadShapePtr, zCoords); COORDS2INDEX(outputTadRank, outputTadStridePtr, zCoords, zIndex); if (lengths[segment]) math::atomics::sd_atomicMax(&z[zIndex], x[xIndex]); } } } } // -------------------------------------------------------------------------------------------------------------- // template static void segmentMaxFunctor_(LaunchContext* context, NDArray* input, NDArray* indices, NDArray* output) { T val = -DataTypeUtils::max(); output->assign(val); auto stream = context->getCudaStream(); indices->syncToHost(); LongType numOfClasses = indices->e(indices->lengthOf() - 1) + 1; NDArray classesRangesLens = NDArrayFactory::create('c', {numOfClasses}, context); NDArray classesRangesBegs = NDArrayFactory::create('c', {numOfClasses}, context); sd::LongType len = indices->lengthOf(); classesRangesBegs.assign(len); int zero2 = 0; classesRangesLens.assign(zero2); LongType* begins = reinterpret_cast(classesRangesBegs.specialBuffer()); LongType* lengths = reinterpret_cast(classesRangesLens.specialBuffer()); fillUpSegments(indices, numOfClasses, classesRangesBegs, classesRangesLens); NDArray::prepareSpecialUse({output}, {input, indices, &classesRangesBegs, &classesRangesLens}); if (input->isVector() || input->isScalar()) { dim3 launchDims = segmentDims(numOfClasses,input->lengthOf()); segmentMaxLinearKernel<<>>( input->specialBuffer(), input->specialShapeInfo(), begins, lengths, numOfClasses, output->specialBuffer(), output->specialShapeInfo()); sd::DebugHelper::checkErrorCode(stream, "segmentMaxLinearKernel failed"); } else { LongType zero = 0; std::vector *dimensions = ShapeUtils::evalDimsToExclude(input->rankOf(), 1,&zero); auto packX = ConstantTadHelper::getInstance().tadForDimensions(input->shapeInfo(), dimensions); auto packZ = ConstantTadHelper::getInstance().tadForDimensions(output->shapeInfo(), dimensions); auto inputTads = packX->specialShapeInfo(); auto inputTadOffsets = packX->specialOffsets(); auto outputTads = packZ->specialShapeInfo(); auto outputTadOffsets = packZ->specialOffsets(); dim3 launchDims = segmentTad(packX->numberOfTads()); segmentMaxTadKernel<<>>( input->specialBuffer(), input->specialShapeInfo(), inputTads, inputTadOffsets, reinterpret_cast(indices->specialBuffer()), begins, lengths, numOfClasses, output->specialBuffer(), output->specialShapeInfo(), outputTads, outputTadOffsets,static_cast(0), indices->lengthOf(),packX->numberOfTads(),packZ->numberOfTads()); sd::DebugHelper::checkErrorCode(stream, "segmentMaxTadKernel failed"); delete dimensions; } NDArray::registerSpecialUse({output}, {input, indices, &classesRangesBegs, &classesRangesLens}); } // -------------------------------------------------------------------------------------------------------------- // void segmentMaxFunctor(LaunchContext* context, NDArray* input, NDArray* indices, NDArray* output) { NDArray::prepareSpecialUse({output}, {input, indices}); BUILD_DOUBLE_SELECTOR(input->dataType(), indices->dataType(), segmentMaxFunctor_, (context, input, indices, output), SD_NUMERIC_TYPES, SD_INDEXING_TYPES); NDArray::registerSpecialUse({output}, {input, indices}); } // -------------------------------------------------------------------------------------------------------------- // template static void unsortedSegmentMaxFunctor_(LaunchContext* context, NDArray* input, NDArray* indices, LongType numOfClasses, NDArray* output) { auto stream = context->getCudaStream(); T val = DataTypeUtils::infOrMax(); output->assign(val); NDArray classesRangesBegs = NDArrayFactory::create('c', {numOfClasses}, context); NDArray classesRangesLens = NDArrayFactory::create('c', {numOfClasses}, context); int zero2 = 0; sd::LongType len = indices->lengthOf(); classesRangesBegs.assign(len); classesRangesLens.assign(zero2); dim3 dims = getFillUpSegmentsDims(numOfClasses, indices->lengthOf()); fillUpSegments(indices, numOfClasses, classesRangesBegs, classesRangesLens); LongType* begins = reinterpret_cast(classesRangesBegs.specialBuffer()); LongType* lengths = reinterpret_cast(classesRangesLens.specialBuffer()); if (input->isVector() || input->isScalar()) { unsortedSegmentMaxLinearKernel<<>>( input->specialBuffer(), input->specialShapeInfo(), indices->specialBuffer(), indices->specialShapeInfo(), begins, lengths, numOfClasses, output->specialBuffer(), output->specialShapeInfo()); sd::DebugHelper::checkErrorCode(stream, "unsortedSegmentMaxLinearKernel failed"); } else { LongType zero = 0; std::vector *dimensions = ShapeUtils::evalDimsToExclude(input->rankOf(), 1,&zero); auto packX = ConstantTadHelper::getInstance().tadForDimensions(input->shapeInfo(), dimensions); auto packZ = ConstantTadHelper::getInstance().tadForDimensions(output->shapeInfo(), dimensions); auto inputTads = packX->specialShapeInfo(); auto inputTadOffsets = packX->specialOffsets(); auto outputTads = packZ->specialShapeInfo(); auto outputTadOffsets = packZ->specialOffsets(); dims.x = input->sizeAt(0); T val = -DataTypeUtils::max(); output->assign(val); segmentMaxTadKernel<<>>( input->specialBuffer(), input->specialShapeInfo(), inputTads, inputTadOffsets, reinterpret_cast(indices->specialBuffer()), begins, lengths, numOfClasses, output->specialBuffer(), output->specialShapeInfo(), outputTads, outputTadOffsets,static_cast(0),indices->lengthOf(),packX->numberOfTads(),packZ->numberOfTads()); delete dimensions; } } // -------------------------------------------------------------------------------------------------------------- // void unsortedSegmentMaxFunctor(LaunchContext* context, NDArray* input, NDArray* indices, LongType numOfClasses, NDArray* output) { NDArray::prepareSpecialUse({output}, {input, indices}); output->nullify(); BUILD_DOUBLE_SELECTOR(input->dataType(), indices->dataType(), unsortedSegmentMaxFunctor_, (context, input, indices, numOfClasses, output), SD_NUMERIC_TYPES, SD_INDEXING_TYPES); NDArray::registerSpecialUse({output}, {input, indices}); } // -------------------------------------------------------------------------------------------------------------- // // segment max // -------------------------------------------------------------------------------------------------------------- // template static SD_KERNEL void segmentMaxBPLinearKernel(void* inputBuf, LongType const* inputShape, void* forwardOutput, LongType const* forwardShape, void* eps, LongType const* epsShape, void* indicesBuf, LongType const* indicesShape, void* outputBuf, LongType const* outputShape, LongType indicesLen) { __shared__ T* x; __shared__ T* gradIn; __shared__ T* gradOut; __shared__ I* y; __shared__ T* z; __shared__ LongType xLen, gradLen; // Cache shape/stride/rank information in shared memory __shared__ sd::LongType xRank, yRank, zRank, gradInRank, gradOutRank; __shared__ const sd::LongType *xShapePtr, *yShapePtr, *zShapePtr, *gradInShapePtr, *gradOutShapePtr; __shared__ const sd::LongType *xStridePtr, *yStridePtr, *zStridePtr, *gradInStridePtr, *gradOutStridePtr; if (threadIdx.x == 0) { xLen = shape::length(inputShape); x = reinterpret_cast(inputBuf); y = reinterpret_cast(indicesBuf); z = reinterpret_cast(outputBuf); gradIn = reinterpret_cast(forwardOutput); gradOut = reinterpret_cast(eps); gradLen = shape::length(epsShape); // Cache all shape information xRank = shape::rank(inputShape); yRank = shape::rank(indicesShape); zRank = shape::rank(outputShape); gradInRank = shape::rank(forwardShape); gradOutRank = shape::rank(epsShape); xShapePtr = shape::shapeOf(inputShape); yShapePtr = shape::shapeOf(indicesShape); zShapePtr = shape::shapeOf(outputShape); gradInShapePtr = shape::shapeOf(forwardShape); gradOutShapePtr = shape::shapeOf(epsShape); xStridePtr = shape::stride(inputShape); yStridePtr = shape::stride(indicesShape); zStridePtr = shape::stride(outputShape); gradInStridePtr = shape::stride(forwardShape); gradOutStridePtr = shape::stride(epsShape); } __syncthreads(); auto start = blockIdx.x * blockDim.x + threadIdx.x; auto step = gridDim.x * blockDim.x; for (auto e = start; e < indicesLen; e += step) { LongType zCoords[SD_MAX_RANK]; LongType xCoords[SD_MAX_RANK]; LongType yCoords[SD_MAX_RANK]; LongType gradICoords[SD_MAX_RANK]; LongType gradOCoords[SD_MAX_RANK]; LongType zOffset; LongType xOffset; LongType yOffset; LongType gradOffsetI; LongType gradOffsetO; INDEX2COORDS(e, zRank, zShapePtr, zCoords); COORDS2INDEX(zRank, zStridePtr, zCoords, zOffset); INDEX2COORDS(e, xRank, xShapePtr, xCoords); COORDS2INDEX(xRank, xStridePtr, xCoords, xOffset); INDEX2COORDS(e, yRank, yShapePtr, yCoords); COORDS2INDEX(yRank, yStridePtr, yCoords, yOffset); auto classIndex = y[yOffset]; INDEX2COORDS(classIndex, gradInRank, gradInShapePtr, gradICoords); COORDS2INDEX(gradInRank, gradInStridePtr, gradICoords, gradOffsetI); INDEX2COORDS(classIndex, gradOutRank, gradOutShapePtr, gradOCoords); COORDS2INDEX(gradOutRank, gradOutStridePtr, gradOCoords, gradOffsetO); if (math::sd_abs(gradIn[gradOffsetI] - x[xOffset]) <= T(1.e-6)) { z[zOffset] = gradOut[gradOffsetO]; } } } // -------------------------------------------------------------------------------------------------------------- // template static SD_KERNEL void segmentMaxBPTadKernel(void* inputBuf, LongType const* inputShape, void* forwardOutput, LongType const* forwardShape, void* eps, LongType const* epsShape, void* indicesBuf, LongType const* indicesShape, void* outputBuf, LongType const* outputShape, LongType const* inputTadShapeInfo, LongType const* inputOffsets, LongType const* gradInTadShapeInfo, LongType const* gradInOffsets, LongType const* gradOutTadShapeInfo, LongType const* gradOutOffsets, LongType const* outTadShapeInfo, LongType const* outOffsets, LongType indicesLen) { __shared__ T* x; __shared__ I *indices; __shared__ T* gradIn; __shared__ T* gradOut; __shared__ I* y; __shared__ T* z; __shared__ LongType xLen, yLen, gradLen, currentLen, gradOutLen, inLen; // Cache shape information for all TADs __shared__ sd::LongType inputTadRank; __shared__ const sd::LongType* inputTadShapePtr; __shared__ const sd::LongType* inputTadStridePtr; __shared__ sd::LongType gradInTadRank; __shared__ const sd::LongType* gradInTadShapePtr; __shared__ const sd::LongType* gradInTadStridePtr; __shared__ sd::LongType gradOutTadRank; __shared__ const sd::LongType* gradOutTadShapePtr; __shared__ const sd::LongType* gradOutTadStridePtr; __shared__ sd::LongType outTadRank; __shared__ const sd::LongType* outTadShapePtr; __shared__ const sd::LongType* outTadStridePtr; if (threadIdx.x == 0) { xLen = shape::length(inputShape); indices = reinterpret_cast(indicesBuf); x = reinterpret_cast(inputBuf); y = reinterpret_cast(indicesBuf); z = reinterpret_cast(outputBuf); yLen = shape::length(indicesShape); gradOut = reinterpret_cast(eps); gradIn = reinterpret_cast(forwardOutput); gradLen = shape::length(epsShape); inLen = shape::length(gradInTadShapeInfo); gradOutLen = shape::length(gradOutTadShapeInfo); currentLen = shape::length(inputTadShapeInfo); // Cache all TAD shape information inputTadRank = shape::rank(inputTadShapeInfo); inputTadShapePtr = shape::shapeOf(inputTadShapeInfo); inputTadStridePtr = shape::stride(inputTadShapeInfo); gradInTadRank = shape::rank(gradInTadShapeInfo); gradInTadShapePtr = shape::shapeOf(gradInTadShapeInfo); gradInTadStridePtr = shape::stride(gradInTadShapeInfo); gradOutTadRank = shape::rank(gradOutTadShapeInfo); gradOutTadShapePtr = shape::shapeOf(gradOutTadShapeInfo); gradOutTadStridePtr = shape::stride(gradOutTadShapeInfo); outTadRank = shape::rank(outTadShapeInfo); outTadShapePtr = shape::shapeOf(outTadShapeInfo); outTadStridePtr = shape::stride(outTadShapeInfo); } __syncthreads(); for (auto i = blockIdx.x; i < indicesLen; i += gridDim.x) { I segment = indices[i]; T* current = x; T* currentOut = z; auto classNum = segment; auto currentOffset = inputOffsets[i]; auto currentOutOffset = outOffsets[i]; auto currentGradOutOffset = gradOutOffsets[classNum]; auto bPTensorOffset = gradInOffsets[classNum]; auto gradIn2 = gradIn + bPTensorOffset; auto current2 = current + currentOffset; auto currentGradOut2 = gradOut + currentGradOutOffset; auto currentOut2 = currentOut + currentOutOffset; for (auto e = threadIdx.x; e < currentLen; e += blockDim.x) { sd::LongType xCoords[SD_MAX_RANK]; sd::LongType gradInCoords[SD_MAX_RANK]; sd::LongType gradOutCoords[SD_MAX_RANK]; sd::LongType outCoords[SD_MAX_RANK]; sd::LongType xIndex; sd::LongType gradInIndex; sd::LongType gradOutIndex; sd::LongType outIndex; INDEX2COORDS(e, inputTadRank, inputTadShapePtr, xCoords); COORDS2INDEX(inputTadRank, inputTadStridePtr, xCoords, xIndex); INDEX2COORDS(e, gradInTadRank, gradInTadShapePtr, gradInCoords); COORDS2INDEX(gradInTadRank, gradInTadStridePtr, gradInCoords, gradInIndex); INDEX2COORDS(e, gradOutTadRank, gradOutTadShapePtr, gradOutCoords); COORDS2INDEX(gradOutTadRank, gradOutTadStridePtr, gradOutCoords, gradOutIndex); INDEX2COORDS(e, outTadRank, outTadShapePtr, outCoords); COORDS2INDEX(outTadRank, outTadStridePtr, outCoords, outIndex); if (math::sd_abs(gradIn2[gradInIndex] - current2[xIndex]) <= T(1.e-6)) { currentOut2[outIndex] = currentGradOut2[gradOutIndex]; } } } } // -------------------------------------------------------------------------------------------------------------- // template Status segmentMaxFunctorBP_(LaunchContext* context, NDArray* input, NDArray* indices, NDArray* gradOut, NDArray* output) { // if input is a vector: (as if in doc sample) auto stream = context->getCudaStream(); /* NDArray tempRes(gradOut->ordering(), gradOut->getShapeAsVector(), DataTypeUtils::fromT(), context); */ auto outShape = gradOut->getShapeAsVector(); NDArray tempRes(gradOut->ordering(), outShape, DataTypeUtils::fromT(), context); segmentMaxFunctor_(context, input, indices, &tempRes); NDArray::prepareSpecialUse({output}, {input, indices, gradOut, &tempRes}); if (input->isVector() || input->isScalar()) { LongType loop_size = input->lengthOf(); auto numOfClasses = gradOut->lengthOf(); dim3 segmentBpDims2 = segmentBpDims(1 + gradOut->lengthOf(),input->lengthOf()); segmentMaxBPLinearKernel<<>>( input->specialBuffer(), input->specialShapeInfo(), tempRes.specialBuffer(), tempRes.specialShapeInfo(), gradOut->specialBuffer(), gradOut->specialShapeInfo(), indices->specialBuffer(), indices->specialShapeInfo(), output->specialBuffer(), output->specialShapeInfo(), indices->lengthOf()); sd::DebugHelper::checkErrorCode(stream, "segmentMaxBPLinearKernel failed"); } else { LongType zero = 0; std::vector *dimensions = ShapeUtils::evalDimsToExclude(input->rankOf(), 1,&zero); auto packX = ConstantTadHelper::getInstance().tadForDimensions(input->shapeInfo(), dimensions); NDArray::preparePrimaryUse({&tempRes}, {&tempRes}); auto packZ = ConstantTadHelper::getInstance().tadForDimensions(output->shapeInfo(), dimensions); auto packGradIn = ConstantTadHelper::getInstance().tadForDimensions(tempRes.shapeInfo(), dimensions); auto packGradOut = ConstantTadHelper::getInstance().tadForDimensions(gradOut->shapeInfo(), dimensions); LongType const* inputTadShapeInfo = packX->specialShapeInfo(); LongType const* inputTadOffsets = packX->specialOffsets(); LongType const* outputTadShapeInfo = packZ->specialShapeInfo(); LongType const* outputTadOffsets = packZ->specialOffsets(); LongType const* gradInTadShapeInfo = packGradIn->specialShapeInfo(); LongType const* gradInTadOffsets = packGradIn->specialOffsets(); LongType const* gradOutTadShapeInfo = packGradOut->specialShapeInfo(); LongType const* gradOutTadOffsets = packGradOut->specialOffsets(); dim3 segmentBpTad2 = segmentBpTad(gradOut->lengthOf(),input->lengthOf()); segmentMaxBPTadKernel<<>>( input->specialBuffer(), input->specialShapeInfo(), tempRes.specialBuffer(), tempRes.specialShapeInfo(), gradOut->specialBuffer(), gradOut->specialShapeInfo(), indices->specialBuffer(), indices->specialShapeInfo(), output->specialBuffer(), output->specialShapeInfo(), inputTadShapeInfo, inputTadOffsets, gradInTadShapeInfo, gradInTadOffsets, gradOutTadShapeInfo, gradOutTadOffsets, outputTadShapeInfo, outputTadOffsets, indices->lengthOf()); sd::DebugHelper::checkErrorCode(stream, "segmentMaxBPTadKernel failed"); delete dimensions; } NDArray::registerSpecialUse({output}, {input, indices, gradOut, &tempRes}); return Status::OK; } // -------------------------------------------------------------------------------------------------------------- // Status segmentMaxFunctorBP(LaunchContext* context, NDArray* input, NDArray* indices, NDArray* gradOut, NDArray* output) { NDArray::prepareSpecialUse({output}, {input, indices, gradOut}); auto indicesDType = indices->dataType(); auto outputDType = output->dataType(); BUILD_DOUBLE_SELECTOR(output->dataType(), indices->dataType(), return segmentMaxFunctorBP_, (context, input, indices, gradOut, output), SD_FLOAT_TYPES, SD_INDEXING_TYPES); NDArray::registerSpecialUse({output}, {input, indices, gradOut}); } // -------------------------------------------------------------------------------------------------------------- // template static Status unsortedSegmentMaxFunctorBP_(LaunchContext* context, NDArray* input, NDArray* indices, NDArray* gradOut, LongType numOfClasses, NDArray* output) { // if input is a vector: (as if in doc sample) auto stream = context->getCudaStream(); auto outShape = gradOut->getShapeAsVector(); NDArray tempRes(gradOut->ordering(), outShape, DataTypeUtils::fromT(), context); unsortedSegmentMaxFunctor_(context, input, indices, numOfClasses, &tempRes); NDArray::prepareSpecialUse({output}, {input, indices, gradOut, &tempRes}); if (input->isVector() || input->isScalar()) { LongType loop_size = input->lengthOf(); auto numOfClasses = gradOut->lengthOf(); // indices->e(loop_size - 1); segmentMaxBPLinearKernel<<lengthOf(), input->lengthOf(), 256, *stream>>>( input->specialBuffer(), input->specialShapeInfo(), tempRes.specialBuffer(), tempRes.specialShapeInfo(), gradOut->specialBuffer(), gradOut->specialShapeInfo(), indices->specialBuffer(), indices->specialShapeInfo(), output->specialBuffer(), output->specialShapeInfo(),indices->lengthOf()); sd::DebugHelper::checkErrorCode(stream, "segmentMaxBPLinearKernel failed"); } else { LongType zero = 0; std::vector *dimensions = ShapeUtils::evalDimsToExclude(input->rankOf(), 1,&zero); auto packX = ConstantTadHelper::getInstance().tadForDimensions(input->shapeInfo(), dimensions); auto packZ = ConstantTadHelper::getInstance().tadForDimensions(output->shapeInfo(), dimensions); auto packGradIn = ConstantTadHelper::getInstance().tadForDimensions(tempRes.shapeInfo(), dimensions); auto packGradOut = ConstantTadHelper::getInstance().tadForDimensions(gradOut->shapeInfo(), dimensions); LongType const* inputTads = packX->specialShapeInfo(); LongType const* inputTadOffsets = packX->specialOffsets(); LongType const* outputTads = packZ->specialShapeInfo(); LongType const* outputTadOffsets = packZ->specialOffsets(); LongType const* gradInTads = packGradIn->specialShapeInfo(); LongType const* gradInTadOffsets = packGradIn->specialOffsets(); LongType const* gradOutTads = packGradOut->specialShapeInfo(); LongType const* gradOutTadOffsets = packGradOut->specialOffsets(); segmentMaxBPTadKernel<<lengthOf(), input->lengthOf(), 256, *stream>>>( input->specialBuffer(), input->specialShapeInfo(), tempRes.specialBuffer(), tempRes.specialShapeInfo(), gradOut->specialBuffer(), gradOut->specialShapeInfo(), indices->specialBuffer(), indices->specialShapeInfo(), output->specialBuffer(), output->specialShapeInfo(), inputTads, inputTadOffsets, gradInTads, gradInTadOffsets, gradOutTads, gradOutTadOffsets, outputTads, outputTadOffsets, indices->lengthOf()); sd::DebugHelper::checkErrorCode(stream, "segmentMaxBPTadKernel failed"); delete dimensions; } NDArray::registerSpecialUse({output}, {input, indices, gradOut, &tempRes}); return Status::OK; } // -------------------------------------------------------------------------------------------------------------- // Status unsortedSegmentMaxFunctorBP(LaunchContext* context, NDArray* input, NDArray* indices, NDArray* gradOut, LongType numOfClasses, NDArray* output) { NDArray::prepareSpecialUse({output}, {input, indices, gradOut}); auto indicesDType = indices->dataType(); auto outputDType = output->dataType(); BUILD_DOUBLE_SELECTOR(output->dataType(), indices->dataType(), return unsortedSegmentMaxFunctorBP_, (context, input, indices, gradOut, numOfClasses, output), SD_FLOAT_TYPES, SD_INDEXING_TYPES); NDArray::registerSpecialUse({output}, {input, indices, gradOut}); } } // namespace helpers } // namespace ops } // namespace sd