/******************************************************************************* * 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) // #include #include #include using namespace simdOps; namespace nd4j { namespace helpers { //////////////////////////////////////////////////////////////////////// template template void TrueBroadcastHelper::exec(const NDArray& xArr, const NDArray& yArr, NDArray& zArr) { const X* x = reinterpret_cast(xArr.getBuffer()); const Y* y = reinterpret_cast(yArr.getBuffer()); Z* z = reinterpret_cast(zArr.getBuffer()); const auto xShapeInfo = xArr.getShapeInfo(); const auto yShapeInfo = yArr.getShapeInfo(); const auto zShapeInfo = zArr.getShapeInfo(); const int xRank = xArr.rankOf(); const int yRank = yArr.rankOf(); const int zRank = zArr.rankOf(); bool bSpecialCase = (1 == xArr.ews() && 'c' == xArr.ordering() && 1 == yArr.ews() && 'c' == yArr.ordering() && 1 == zArr.ews() && 'c' == zArr.ordering()); if (bSpecialCase && yArr.isColumnVector() && 1 == xArr.sizeAt(-1) ) { auto yLen = (uint32_t)yArr.lengthOf(); auto func = PRAGMA_THREADS_FOR{ for (uint32_t i = start; i < stop; i++) { auto rZ = z + (i * yLen); auto v = x[i]; for (uint32_t j = 0; j < yLen; j++) { rZ[j] = OpType::op(v, y[j]); } } }; samediff::Threads::parallel_tad(func, 0, xArr.lengthOf()); return; } auto yShapeInt = yArr.getShapeAsVectorInt(); auto xShapeInt = xArr.getShapeAsVectorInt(); auto nCountY = std::count_if(yShapeInt.cbegin(), yShapeInt.cend(), [](int i) { return i == 1; }); auto nCountX = std::count_if(xShapeInt.cbegin(), xShapeInt.cend(), [](int i) { return i == 1; }); bool bSpecialCase2 = (xRank == zRank && yRank == zRank && 1 == xArr.sizeAt(-1) && 1 == yArr.sizeAt(-2) && 1 == nCountY && 1 == nCountX); if (bSpecialCase && bSpecialCase2) { int zDim1 = zArr.sizeAt(-2); int zDim2 = zArr.sizeAt(-1); int nLen = zArr.lengthOf() / yArr.sizeAt(-1); auto func = PRAGMA_THREADS_FOR{ for (uint32_t total = start; total < stop; total++) { uint32_t i = total / zDim1; uint32_t j = total % zDim1; uint32_t index = (i * zDim1) + j; auto rZ = z + (index * zDim2); auto rY = y + (i * zDim2); auto rX = x[index]; for (uint32_t n = 0; n < zDim2; n++) { rZ[n] = OpType::op(rX, rY[n]); } } }; samediff::Threads::parallel_tad(func, 0, nLen, 1); return; } const Nd4jLong zLen = zArr.lengthOf(); auto func = PRAGMA_THREADS_FOR{ std::vector xCoords(xArr.rankOf()), yCoords(yArr.rankOf()), zCoords(zArr.rankOf()); for (auto i = start; i < stop; ++i) { shape::index2coords(i, zShapeInfo, zCoords.data()); for (int ix = xRank - 1, iy = yRank - 1, iz = zRank - 1; iz >= 0; --iz) { if (ix >= 0) { if (xShapeInfo[ix + 1] == zShapeInfo[iz + 1]) { xCoords[ix--] = zCoords[iz]; } else { xCoords[ix--] = 0; } } if (iy >= 0) { if (yShapeInfo[iy + 1] == zShapeInfo[iz + 1]) { yCoords[iy--] = zCoords[iz]; } else { yCoords[iy--] = 0; } } } const auto xOffset = shape::getOffset(xShapeInfo, xCoords.data()); const auto yOffset = shape::getOffset(yShapeInfo, yCoords.data()); const auto zOffset = shape::getOffset(zShapeInfo, zCoords.data()); z[zOffset] = OpType::op(x[xOffset], y[yOffset]); } }; samediff::Threads::parallel_for(func, 0, zLen); } template void TrueBroadcastHelper::exec(const nd4j::broadcast::Ops opNum, const NDArray& xArr, const NDArray& yArr, NDArray& zArr) { DISPATCH_BY_OPNUM_TTT(exec, PARAMS(xArr, yArr, zArr), BROADCAST_OPS); } //////////////////////////////////////////////////////////////////////// template template void TrueBroadcastBoolHelper::exec(const NDArray& xArr, const NDArray& yArr, NDArray& zArr) { const X* x = reinterpret_cast(xArr.getBuffer()); const X* y = reinterpret_cast(yArr.getBuffer()); Z* z = reinterpret_cast(zArr.getBuffer()); const auto xShapeInfo = xArr.getShapeInfo(); const auto yShapeInfo = yArr.getShapeInfo(); const auto zShapeInfo = zArr.getShapeInfo(); const int xRank = xArr.rankOf(); const int yRank = yArr.rankOf(); const int zRank = zArr.rankOf(); const Nd4jLong zLen = zArr.lengthOf(); auto func = PRAGMA_THREADS_FOR{ std::vector xCoords(xArr.rankOf()), yCoords(yArr.rankOf()), zCoords(zArr.rankOf()); for (auto i = start; i < stop; ++i) { shape::index2coords(i, zShapeInfo, zCoords.data()); for (int ix = xRank - 1, iy = yRank - 1, iz = zRank - 1; iz >= 0; --iz) { if (ix >= 0) { if (xShapeInfo[ix + 1] == zShapeInfo[iz + 1]) { xCoords[ix--] = zCoords[iz]; } else { xCoords[ix--] = 0; } } if (iy >= 0) { if (yShapeInfo[iy + 1] == zShapeInfo[iz + 1]) { yCoords[iy--] = zCoords[iz]; } else { yCoords[iy--] = 0; } } } const auto xOffset = shape::getOffset(xShapeInfo, xCoords.data()); const auto yOffset = shape::getOffset(yShapeInfo, yCoords.data()); const auto zOffset = shape::getOffset(zShapeInfo, zCoords.data()); z[zOffset] = OpType::op(x[xOffset], y[yOffset], nullptr); } }; samediff::Threads::parallel_for(func, 0, zLen); } template void TrueBroadcastBoolHelper::exec(const nd4j::broadcast::BoolOps opNum, const NDArray& xArr, const NDArray& yArr, NDArray& zArr) { DISPATCH_BY_OPNUM_TT(exec, PARAMS(xArr, yArr, zArr), BROADCAST_BOOL_OPS); } //////////////////////////////////////////////////////////////////////// template template void TrueBroadcastIntHelper::exec(const NDArray& xArr, const NDArray& yArr, NDArray& zArr) { const X* x = reinterpret_cast(xArr.getBuffer()); const X* y = reinterpret_cast(yArr.getBuffer()); X* z = reinterpret_cast(zArr.getBuffer()); const auto xShapeInfo = xArr.getShapeInfo(); const auto yShapeInfo = yArr.getShapeInfo(); const auto zShapeInfo = zArr.getShapeInfo(); const int xRank = xArr.rankOf(); const int yRank = yArr.rankOf(); const int zRank = zArr.rankOf(); const Nd4jLong zLen = zArr.lengthOf(); auto func = PRAGMA_THREADS_FOR{ std::vector xCoords(xArr.rankOf()), yCoords(yArr.rankOf()), zCoords(zArr.rankOf()); for (auto i = start; i < stop; ++i) { shape::index2coords(i, zShapeInfo, zCoords.data()); for (int ix = xRank - 1, iy = yRank - 1, iz = zRank - 1; iz >= 0; --iz) { if (ix >= 0) { if (xShapeInfo[ix + 1] == zShapeInfo[iz + 1]) { xCoords[ix--] = zCoords[iz]; } else { xCoords[ix--] = 0; } } if (iy >= 0) { if (yShapeInfo[iy + 1] == zShapeInfo[iz + 1]) { yCoords[iy--] = zCoords[iz]; } else { yCoords[iy--] = 0; } } } const auto xOffset = shape::getOffset(xShapeInfo, xCoords.data()); const auto yOffset = shape::getOffset(yShapeInfo, yCoords.data()); const auto zOffset = shape::getOffset(zShapeInfo, zCoords.data()); z[zOffset] = OpType::op(x[xOffset], y[yOffset]); } }; samediff::Threads::parallel_for(func, 0, zLen); } template void TrueBroadcastIntHelper::exec(const nd4j::broadcast::IntOps opNum, const NDArray& xArr, const NDArray& yArr, NDArray& zArr) { DISPATCH_BY_OPNUM_T(exec, PARAMS(xArr, yArr, zArr), BROADCAST_INT_OPS); } /* BUILD_PAIRWISE_TEMPLATE(template class ND4J_EXPORT TrueBroadcastHelper, , PAIRWISE_TYPES_0); BUILD_PAIRWISE_TEMPLATE(template class ND4J_EXPORT TrueBroadcastHelper, , PAIRWISE_TYPES_1); BUILD_PAIRWISE_TEMPLATE(template class ND4J_EXPORT TrueBroadcastHelper, , PAIRWISE_TYPES_2); BUILD_PAIRWISE_TEMPLATE(template class ND4J_EXPORT TrueBroadcastHelper, , PAIRWISE_TYPES_3); BUILD_PAIRWISE_TEMPLATE(template class ND4J_EXPORT TrueBroadcastHelper, , PAIRWISE_TYPES_4); BUILD_PAIRWISE_TEMPLATE(template class ND4J_EXPORT TrueBroadcastHelper, , PAIRWISE_TYPES_5); BUILD_PAIRWISE_TEMPLATE(template class ND4J_EXPORT TrueBroadcastHelper, , PAIRWISE_TYPES_6); BUILD_PAIRWISE_TEMPLATE(template class ND4J_EXPORT TrueBroadcastHelper, , PAIRWISE_TYPES_7); BUILD_PAIRWISE_TEMPLATE(template class ND4J_EXPORT TrueBroadcastHelper, , PAIRWISE_TYPES_8); BUILD_PAIRWISE_TEMPLATE(template class ND4J_EXPORT TrueBroadcastHelper, , PAIRWISE_TYPES_9); BUILD_DOUBLE_TEMPLATE(template class ND4J_EXPORT TrueBroadcastBoolHelper, , LIBND4J_TYPES, BOOL_TYPES); BUILD_SINGLE_TEMPLATE(template class ND4J_EXPORT TrueBroadcastIntHelper, , INTEGER_TYPES); */ } }