chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,372 @@
|
||||
/* ******************************************************************************
|
||||
*
|
||||
*
|
||||
* 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 raver119@gmail.com
|
||||
// @author Yurii Shyrma (iuriish@yahoo.com)
|
||||
//
|
||||
#include <array/DataBuffer.h>
|
||||
#include <array/DataTypeUtils.h>
|
||||
#include <types/types.h>
|
||||
#include <system/type_boilerplate.h>
|
||||
|
||||
|
||||
namespace sd {
|
||||
void DataBuffer::expand(const uint64_t size) {
|
||||
if (static_cast<LongType>(size) > _lenInBytes) {
|
||||
// allocate new buffer
|
||||
int8_t* newBuffer = nullptr;
|
||||
ALLOCATE(newBuffer, _workspace, size, int8_t);
|
||||
|
||||
// copy data from existing buffer
|
||||
std::memcpy(newBuffer, _primaryBuffer, _lenInBytes);
|
||||
|
||||
if (_isOwnerPrimary) {
|
||||
RELEASE(reinterpret_cast<int8_t*>(_primaryBuffer), _workspace);
|
||||
}
|
||||
|
||||
_primaryBuffer = newBuffer;
|
||||
_lenInBytes = size;
|
||||
_isOwnerPrimary = true;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void DataBuffer::printHostBufferContent(void* buffer, sd::LongType offset, sd::LongType length) {
|
||||
T* typedBuffer = reinterpret_cast<T*>(buffer);
|
||||
|
||||
sd_printf("[ ", 0);
|
||||
for (sd::LongType i = offset; i < offset + length; i++) {
|
||||
// For numeric types, cast to double for consistent formatting
|
||||
if (std::is_arithmetic<T>::value) {
|
||||
sd_printf("%g ", (double)typedBuffer[i]);
|
||||
} else {
|
||||
// For non-numeric types, print as hex
|
||||
sd_printf("0x%x ", *reinterpret_cast<int*>(&typedBuffer[i]));
|
||||
}
|
||||
}
|
||||
sd_printf("]", 0);
|
||||
}
|
||||
BUILD_SINGLE_TEMPLATE(void DataBuffer::printHostBufferContent,(void* buffer, sd::LongType offset, sd::LongType length),SD_COMMON_TYPES);
|
||||
|
||||
|
||||
|
||||
|
||||
// DataBuffer implementation for .cpp file
|
||||
void DataBuffer::printBufferDebug(const char* msg, sd::LongType offset, sd::LongType limit) {
|
||||
if (msg) sd_printf("%s:\n", msg);
|
||||
|
||||
// Print metadata
|
||||
sd_printf("DataBuffer: DataType=%s, Length=%lld elements, DeviceId=%d\n",
|
||||
DataTypeUtils::asString(_dataType).c_str(), (long long)getNumElements(), deviceId());
|
||||
|
||||
// Print host buffer content
|
||||
if (_primaryBuffer != nullptr) {
|
||||
sd_printf("Host buffer (@%p): ", _primaryBuffer);
|
||||
|
||||
sd::LongType len = getNumElements();
|
||||
sd::LongType printLen = limit < 0 ? len : std::min(len - offset, limit);
|
||||
|
||||
// Print based on datatype
|
||||
BUILD_SINGLE_SELECTOR(_dataType, printHostBufferContent,
|
||||
(_primaryBuffer, offset, printLen), SD_COMMON_TYPES);
|
||||
|
||||
if (offset + printLen < len) sd_printf("... ", 0);
|
||||
sd_printf("\n", 0);
|
||||
} else {
|
||||
sd_printf("Host buffer: nullptr\n", 0);
|
||||
}
|
||||
|
||||
// Print device info but not content (CPU version)
|
||||
if (_specialBuffer != nullptr) {
|
||||
sd_printf("Device buffer (@%p): [Not accessible from CPU]\n", _specialBuffer);
|
||||
} else {
|
||||
sd_printf("Device buffer: nullptr\n", 0);
|
||||
}
|
||||
|
||||
#if defined(SD_CUDA)
|
||||
// Print sync state counters
|
||||
sd_printf("Sync state: _counter=%lld, _writePrimary=%lld, _writeSpecial=%lld, _readPrimary=%lld, _readSpecial=%lld\n",
|
||||
(long long)_counter.load(), (long long)_writePrimary.load(), (long long)_writeSpecial.load(),
|
||||
(long long)_readPrimary.load(), (long long)_readSpecial.load());
|
||||
sd_printf("isPrimaryActual=%d, isSpecialActual=%d\n", isPrimaryActual(), isSpecialActual());
|
||||
#endif
|
||||
}
|
||||
|
||||
// Helper template to print host buffer content
|
||||
template <typename T>
|
||||
void printHostBufferContent(void* buffer, sd::LongType offset, sd::LongType length) {
|
||||
T* typedBuffer = reinterpret_cast<T*>(buffer);
|
||||
|
||||
sd_printf("[ ", 0);
|
||||
for (sd::LongType i = offset; i < offset + length; i++) {
|
||||
// For numeric types, cast to double for consistent formatting
|
||||
if (std::is_arithmetic<T>::value) {
|
||||
sd_printf("%g ", (double)typedBuffer[i]);
|
||||
} else {
|
||||
// For non-numeric types, print as hex
|
||||
sd_printf("0x%x ", *reinterpret_cast<int*>(&typedBuffer[i]));
|
||||
}
|
||||
}
|
||||
sd_printf("]", 0);
|
||||
}
|
||||
|
||||
void DataBuffer::printSpecialAllocationTraces() {
|
||||
//no op on purpose
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
void DataBuffer::allocateBuffers(const bool allocBoth) { // always allocate primary buffer only (cpu case)
|
||||
allocatePrimary();
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
void DataBuffer::copyBufferFrom(const DataBuffer& other,
|
||||
size_t sizeToCopyinBytes,
|
||||
const sd::LongType offsetThis,
|
||||
const sd::LongType offsetOther) {
|
||||
if(other._dataType != _dataType) {
|
||||
THROW_EXCEPTION("DataBuffer::copyBufferFrom: data types of buffers are different");
|
||||
}
|
||||
if (sizeToCopyinBytes == 0) {
|
||||
LongType otherBytes = other.getLenInBytes() - offsetOther;
|
||||
LongType thisBytes = getLenInBytes() - offsetThis;
|
||||
sizeToCopyinBytes = otherBytes < thisBytes ? otherBytes : thisBytes;
|
||||
}
|
||||
if (sizeToCopyinBytes == 0) return;
|
||||
if(static_cast<LongType>(sizeToCopyinBytes) > other._lenInBytes - offsetOther) {
|
||||
std::string errorMessage;
|
||||
errorMessage = "DataBuffer::copyBufferFrom: size to copy is larger than source buffer ";
|
||||
errorMessage += std::to_string(sizeToCopyinBytes);
|
||||
errorMessage += " > ";
|
||||
errorMessage += std::to_string(other._lenInBytes - offsetOther);
|
||||
THROW_EXCEPTION(errorMessage.c_str());
|
||||
}
|
||||
|
||||
if(sizeToCopyinBytes > getLenInBytes() - offsetThis) {
|
||||
std::string errorMessage;
|
||||
errorMessage = "DataBuffer::copyBufferFrom: size to copy is larger than destination buffer ";
|
||||
errorMessage += std::to_string(sizeToCopyinBytes);
|
||||
errorMessage += " > ";
|
||||
errorMessage += std::to_string(getLenInBytes() - offsetThis);
|
||||
THROW_EXCEPTION(errorMessage.c_str());
|
||||
}
|
||||
|
||||
if (other._primaryBuffer != nullptr) {
|
||||
auto sizeOfElement = DataTypeUtils::sizeOfElement(_dataType);
|
||||
auto sizeOfOtherElement = DataTypeUtils::sizeOfElement(_dataType);
|
||||
if(sizeOfElement != sizeOfOtherElement) {
|
||||
THROW_EXCEPTION("DataBuffer::copyBufferFrom: size of elements in buffers are different");
|
||||
}
|
||||
std::memcpy(
|
||||
static_cast<int8_t*>(_primaryBuffer) + offsetThis * sizeOfElement,
|
||||
static_cast<const int8_t*>(other._primaryBuffer) + offsetOther * sizeOfOtherElement,
|
||||
sizeToCopyinBytes);
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
void DataBuffer::copyBufferFromHost(const void* hostBuffer, size_t sizeToCopyinBytes, const sd::LongType offsetThis,
|
||||
const sd::LongType offsetHostBuffer) {
|
||||
if (sizeToCopyinBytes == 0) sizeToCopyinBytes = getLenInBytes();
|
||||
if (sizeToCopyinBytes == 0) return;
|
||||
|
||||
if (hostBuffer != nullptr)
|
||||
std::memcpy(static_cast<int8_t*>(_primaryBuffer) + offsetThis * DataTypeUtils::sizeOfElement(_dataType),
|
||||
static_cast<const int8_t*>(hostBuffer) + offsetHostBuffer * DataTypeUtils::sizeOfElement(_dataType),
|
||||
sizeToCopyinBytes);
|
||||
}
|
||||
|
||||
/////////////////////////
|
||||
|
||||
template <typename T>
|
||||
void memcpyWithT(DataBuffer* dst, DataBuffer* src, sd::LongType startingOffset, sd::LongType dstOffset) {
|
||||
if(src->getLenInBytes() != dst->getLenInBytes()) {
|
||||
THROW_EXCEPTION("DataBuffer::memcpy: source and destination buffers have different length in bytes");
|
||||
}
|
||||
|
||||
std::memcpy(dst->primaryAtOffset<T>(dstOffset), src->primaryAtOffset<T>(startingOffset), src->getLenInBytes());
|
||||
dst->readPrimary();
|
||||
}
|
||||
|
||||
void DataBuffer::memcpy(DataBuffer* dst, DataBuffer* src,
|
||||
sd::LongType startingOffset, sd::LongType dstOffset) {
|
||||
BUILD_SINGLE_SELECTOR(dst->_dataType, memcpyWithT,(dst, src, startingOffset, dstOffset),
|
||||
SD_COMMON_TYPES);
|
||||
|
||||
dst->readPrimary();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
void DataBuffer::deleteSpecial() {}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
void DataBuffer::syncToPrimary(const LaunchContext* context, const bool forceSync) {}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
void DataBuffer::setCountersToZero() {}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
void DataBuffer::copyCounters(const DataBuffer& other) {}
|
||||
|
||||
void DataBuffer::writePrimary() const {}
|
||||
void DataBuffer::writeSpecial() const {}
|
||||
void DataBuffer::readPrimary() const {}
|
||||
void DataBuffer::readSpecial() const {}
|
||||
bool DataBuffer::isPrimaryActual() const { return true; }
|
||||
bool DataBuffer::isSpecialActual() const { return false; }
|
||||
void DataBuffer::showBufferLimited() {}
|
||||
|
||||
|
||||
|
||||
DataBuffer DataBuffer::dup() {
|
||||
DataBuffer result;
|
||||
result._dataType = _dataType;
|
||||
result._lenInBytes = _lenInBytes;
|
||||
result._primaryBuffer = _primaryBuffer;
|
||||
result._specialBuffer = _specialBuffer;
|
||||
result._isOwnerPrimary = _isOwnerPrimary;
|
||||
result._isOwnerSpecial = _isOwnerSpecial;
|
||||
result.allocateBuffers(true);
|
||||
result.copyCounters(*this);
|
||||
result.copyBufferFrom(*this);
|
||||
return result;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
void DataBuffer::setSpecial(void* special, const bool isOwnerSpecial) {}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
void DataBuffer::setToZeroBuffers(const bool both) { memset(primary(), 0, getLenInBytes()); }
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
void DataBuffer::syncToSpecial(const bool forceSync) {}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
void DataBuffer::allocateSpecial() {}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
void DataBuffer::migrate() {}
|
||||
|
||||
|
||||
template <typename T>
|
||||
void _printHostBuffer(DataBuffer* buffer, long offset) {
|
||||
sd::LongType len = buffer->getNumElements();
|
||||
auto buff = buffer->template primaryAsT<T>();
|
||||
|
||||
|
||||
sd::LongType limit = len;
|
||||
if (limit == -1 || limit >= static_cast<LongType>(buffer->getNumElements())) {
|
||||
limit = buffer->getNumElements();
|
||||
}
|
||||
|
||||
const char* msg = nullptr;
|
||||
if (msg != nullptr) {
|
||||
printf("%s: ", msg);
|
||||
} else {
|
||||
printf("[");
|
||||
}
|
||||
|
||||
sd::DataType dataType = buffer->getDataType();
|
||||
auto baseOffset = offset;
|
||||
if (dataType == sd::DataType::DOUBLE || dataType == sd::DataType::FLOAT32) {
|
||||
for (sd::LongType e = baseOffset; e < limit; e++) {
|
||||
if (e > offset) printf(", ");
|
||||
if (dataType == sd::DataType::DOUBLE) {
|
||||
double val = static_cast<double>(buff[e]);
|
||||
printf("%.15f",val);
|
||||
} else {
|
||||
printf("%.15f", static_cast<float>(buff[e]));
|
||||
}
|
||||
}
|
||||
} else if (dataType == sd::DataType::INT64 || dataType == sd::DataType::UINT64 ||
|
||||
dataType == sd::DataType::INT32 || dataType == sd::DataType::UINT32) {
|
||||
for (sd::LongType e = baseOffset; e < limit; e++) {
|
||||
if (dataType == sd::DataType::INT64 || dataType == sd::DataType::UINT64) {
|
||||
printf("%lld", static_cast<long long>(buff[e]));
|
||||
} else {
|
||||
printf("%d", static_cast<int>(buff[e]));
|
||||
}
|
||||
|
||||
if (e < limit - 1) {
|
||||
printf(", ");
|
||||
}
|
||||
}
|
||||
} else if (dataType == sd::DataType::BOOL) {
|
||||
for (sd::LongType e = baseOffset; e < limit; e++) {
|
||||
if (static_cast<bool>(buff[e])) {
|
||||
printf("true");
|
||||
} else {
|
||||
printf("false");
|
||||
}
|
||||
|
||||
if (e < limit - 1) {
|
||||
printf(", ");
|
||||
}
|
||||
}
|
||||
} else if (dataType == sd::DataType::UTF8 || dataType == sd::DataType::UTF16 ||
|
||||
dataType == sd::DataType::UTF32) {
|
||||
for (sd::LongType e = baseOffset; e < limit; e++) {
|
||||
printf("\"%s\"", reinterpret_cast<const char*>(&buff[e]));
|
||||
if (e < limit - 1) {
|
||||
printf(", ");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
printf("]\n");
|
||||
fflush(stdout);
|
||||
}
|
||||
void DataBuffer::printHostDevice(long offset) {
|
||||
auto xType = getDataType();
|
||||
BUILD_SINGLE_SELECTOR(xType, _printHostBuffer,(this,offset),SD_COMMON_TYPES);
|
||||
|
||||
}
|
||||
|
||||
|
||||
void DataBuffer::showCounters(const char* msg1, const char* msg2) {
|
||||
|
||||
}
|
||||
template <typename T>
|
||||
void* DataBuffer::primaryAtOffset(const LongType offset) {
|
||||
if(_primaryBuffer == nullptr)
|
||||
return nullptr;
|
||||
T *type = reinterpret_cast<T*>(_primaryBuffer);
|
||||
return reinterpret_cast<void *>(type + offset);
|
||||
}
|
||||
|
||||
#define PRIMARYOFFSET(T) template void* DataBuffer::primaryAtOffset<GET_SECOND(T)>(const LongType offset);
|
||||
ITERATE_LIST((SD_COMMON_TYPES),PRIMARYOFFSET)
|
||||
|
||||
template <typename T>
|
||||
void* DataBuffer::specialAtOffset(const LongType offset) {
|
||||
if(_specialBuffer == nullptr)
|
||||
return nullptr;
|
||||
T *type = reinterpret_cast<T*>(_specialBuffer);
|
||||
return reinterpret_cast<void *>(type + offset);
|
||||
}
|
||||
|
||||
#define SPECIALOFFSET(T) template void* DataBuffer::specialAtOffset<GET_SECOND(T)>(const LongType offset);
|
||||
ITERATE_LIST((SD_COMMON_TYPES),SPECIALOFFSET)
|
||||
} // namespace sd
|
||||
@@ -0,0 +1,541 @@
|
||||
/* ******************************************************************************
|
||||
*
|
||||
*
|
||||
* 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
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef NDARRAY_CPP
|
||||
#define NDARRAY_CPP
|
||||
#include <array/NDArray.h>
|
||||
|
||||
#include <helpers/ArrayUtils.h>
|
||||
#include <helpers/ConstantTadHelper.h>
|
||||
#include <helpers/ShapeUtils.h>
|
||||
#include <helpers/logger.h>
|
||||
|
||||
#include <indexing/NDIndex.h>
|
||||
#include <loops/BroadcastPairwiseConverter.h>
|
||||
|
||||
#include <loops/random.h>
|
||||
|
||||
#include <ops/ops.h>
|
||||
|
||||
#include <array/NDArray.hXX>
|
||||
|
||||
#include <memory>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <system/selective_rendering.h>
|
||||
namespace sd {
|
||||
|
||||
|
||||
// NDArray implementation for .cpp file
|
||||
void NDArray::printBufferDebug(const char* msg, sd::LongType offset, sd::LongType limit) {
|
||||
if (msg) sd_printf("%s:\n", msg);
|
||||
if(limit < 0) limit = lengthOf();
|
||||
|
||||
// Print array info
|
||||
sd_printf("NDArray: Shape=[", 0);
|
||||
for (int i = 0; i < rankOf(); i++) {
|
||||
sd_printf("%lld", (long long)sizeAt(i));
|
||||
if (i < rankOf() - 1) sd_printf(",", 0);
|
||||
}
|
||||
sd_printf("], DataType=%s, EWS=%lld, Order=%c\n",
|
||||
DataTypeUtils::asString(dataType()).c_str(), (long long)ews(), ordering());
|
||||
|
||||
// Print buffer state
|
||||
if (_buffer != nullptr) {
|
||||
_buffer->printBufferDebug("Buffer contents", offset, limit);
|
||||
} else {
|
||||
sd_printf("Buffer is nullptr\n", 0);
|
||||
}
|
||||
}
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
|
||||
void* NDArray::platformBuffer() { return buffer(); }
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
template <typename T>
|
||||
void NDArray::fillAsTriangular(const float val, int lower, int upper, NDArray& target, const char direction, const bool includeEdges) {
|
||||
if (isS()) THROW_EXCEPTION("NDArray::fillArrayAsTriangular: you can't use this method on String array!");
|
||||
|
||||
if (!isSameShape(target) &&
|
||||
!(rankOf() == 1 && target.rankOf() == 2 && sizeAt(0) == target.sizeAt(0) && sizeAt(0) == target.sizeAt(1)))
|
||||
THROW_EXCEPTION("NDArray::fillArrayAsTriangular method: wrong shape of target array !");
|
||||
|
||||
const T value = static_cast<T>(val);
|
||||
const auto x = reinterpret_cast<const T*>(buffer());
|
||||
auto z = reinterpret_cast<T*>(target.buffer());
|
||||
|
||||
const int xRank = rankOf();
|
||||
const int zRank = target.rankOf();
|
||||
|
||||
const auto zLen = target.lengthOf();
|
||||
|
||||
const bool areSameOffsets = shape::haveSameShapeAndStrides(shapeInfo(), target.shapeInfo());
|
||||
|
||||
sd::LongType *targetShape = shape::shapeOf(target.shapeInfo());
|
||||
sd::LongType *targetStride = shape::stride(target.shapeInfo());
|
||||
sd::LongType targetRank = target.rankOf();
|
||||
|
||||
sd::LongType *xShape = shape::shapeOf(shapeInfo());
|
||||
sd::LongType *xStride = shape::stride(shapeInfo());
|
||||
sd::LongType thisRank = this->rankOf();
|
||||
auto func = PRAGMA_THREADS_FOR {
|
||||
sd::LongType coords[SD_MAX_RANK], temp;
|
||||
sd::LongType vectorCoord[1] = {0};
|
||||
|
||||
bool notVectorScalar = targetRank == 2 && thisRank == 2;
|
||||
bool thisNotVectorScalar = !shape::isScalar(this->shapeInfo()) && !shape::isVector(this->shapeInfo());
|
||||
bool targetNotVectorScalar = !shape::isScalar(target.shapeInfo()) && !shape::isVector(target.shapeInfo());
|
||||
|
||||
for (sd::LongType i = start; i < stop; i++) {
|
||||
INDEX2COORDS(i, targetRank,targetShape, coords);
|
||||
sd::LongType row = targetNotVectorScalar ? coords[zRank - 2] : 0;
|
||||
sd::LongType col = targetNotVectorScalar ? coords[zRank - 1] : 1;
|
||||
sd::LongType zOffset, xOffset;
|
||||
|
||||
if (target.rankOf() < 2) {
|
||||
COORDS2INDEX(targetRank, targetStride, vectorCoord, zOffset);
|
||||
} else {
|
||||
COORDS2INDEX(targetRank, targetStride, coords, zOffset);
|
||||
}
|
||||
|
||||
if (!areSameOffsets && rankOf() < 2) {
|
||||
COORDS2INDEX(thisRank, xStride, vectorCoord, xOffset);
|
||||
} else if (areSameOffsets) {
|
||||
xOffset = zOffset;
|
||||
} else {
|
||||
COORDS2INDEX(thisRank, xStride, coords, xOffset);
|
||||
}
|
||||
|
||||
bool rowExclusive = this->rankOf() == target.rankOf();
|
||||
bool colExclusive = this->rankOf() == target.rankOf();
|
||||
auto lCompare = includeEdges ? row <= (col - lower) : row < (col - lower);
|
||||
auto uCompare = includeEdges ? row >= (col - upper) : row > (col - upper);
|
||||
|
||||
if ((direction == 'u' && lCompare) || (direction == 'l' && uCompare)) {
|
||||
z[zOffset] = value;
|
||||
} else {
|
||||
z[zOffset] = x[xOffset];
|
||||
}
|
||||
|
||||
if (this != &target) {
|
||||
if (xRank != zRank) {
|
||||
temp = coords[0];
|
||||
coords[0] = coords[1];
|
||||
}
|
||||
|
||||
if (xRank != zRank) // restore first coordinate
|
||||
coords[0] = temp;
|
||||
}
|
||||
|
||||
if (vectorCoord[0] == this->lengthOf() - 1) {
|
||||
vectorCoord[0] = 0;
|
||||
} else {
|
||||
vectorCoord[0] = vectorCoord[0] + 1;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
samediff::Threads::parallel_for(func, 0, zLen);
|
||||
}
|
||||
BUILD_SINGLE_TEMPLATE( void NDArray::fillAsTriangular,
|
||||
(const float val, int lower, int upper, NDArray& target, const char direction,const bool includeEdges), SD_COMMON_TYPES);
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
void NDArray::setIdentity() {
|
||||
if (isS()) THROW_EXCEPTION("NDArray::setIdentity: you can't use this method on String array!");
|
||||
|
||||
this->nullify();
|
||||
|
||||
int rank = rankOf();
|
||||
auto shape = shapeOf();
|
||||
int minDim = SD_MAX_INT;
|
||||
sd::LongType indices[SD_MAX_RANK];
|
||||
for (int j = 0; j < rank; ++j) indices[j] = 1;
|
||||
|
||||
sd::LongType offset;
|
||||
COORDS2INDEX(rank, shape::stride(shapeInfo()), indices, offset);
|
||||
|
||||
for (int i = 0; i < rank; ++i)
|
||||
if (minDim > shape[i]) minDim = shape[i];
|
||||
|
||||
float v = 1.0f;
|
||||
|
||||
for (int i = 0; i < minDim; ++i) templatedSet<float,float>(buffer(), i * offset, this->dataType(), &v);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
template <typename T>
|
||||
static void templatedSwap(void* xBuffer, void* yBuffer, const sd::LongType* xShapeInfo, const sd::LongType* yShapeInfo,
|
||||
sd::LongType length) {
|
||||
auto x = reinterpret_cast<T*>(xBuffer);
|
||||
auto y = reinterpret_cast<T*>(yBuffer);
|
||||
|
||||
const bool isSameOrders = shape::order(xShapeInfo) == shape::order(xShapeInfo);
|
||||
|
||||
sd::LongType xRank = shape::rank(xShapeInfo);
|
||||
sd::LongType yRank = shape::rank(yShapeInfo);
|
||||
sd::LongType *xShape = shape::shapeOf(xShapeInfo);
|
||||
sd::LongType *yShape = shape::shapeOf(yShapeInfo);
|
||||
sd::LongType *xStride = shape::stride(xShapeInfo);
|
||||
sd::LongType *yStride = shape::stride(yShapeInfo);
|
||||
|
||||
auto func = PRAGMA_THREADS_FOR {
|
||||
if (isSameOrders) {
|
||||
for (sd::LongType i = start; i < stop; i++) {
|
||||
LongType xCoords[SD_MAX_RANK];
|
||||
LongType yCoords[SD_MAX_RANK];
|
||||
LongType xOffset;
|
||||
LongType yOffset;
|
||||
|
||||
INDEX2COORDS(i, xRank, xShape, xCoords);
|
||||
COORDS2INDEX(xRank, shape::stride(xShapeInfo), xCoords, xOffset);
|
||||
INDEX2COORDS(i, yRank,yShape, yCoords);
|
||||
COORDS2INDEX(yRank, yStride, yCoords, yOffset);
|
||||
|
||||
sd::math::sd_swap(x[xOffset], y[yOffset]);
|
||||
}
|
||||
} else if (shape::haveSameShapeAndStrides(xShapeInfo, yShapeInfo)) {
|
||||
for (sd::LongType i = start; i < stop; i++) {
|
||||
LongType coords[SD_MAX_RANK];
|
||||
LongType ind;
|
||||
|
||||
INDEX2COORDS(i, xRank, xShape, coords);
|
||||
COORDS2INDEX(xRank, xStride, coords, ind);
|
||||
|
||||
sd::math::sd_swap(x[ind], y[ind]);
|
||||
}
|
||||
} else {
|
||||
for (sd::LongType i = start; i < stop; i++) {
|
||||
LongType xCoords[SD_MAX_RANK];
|
||||
LongType yCoords[SD_MAX_RANK];
|
||||
LongType xInd;
|
||||
LongType yInd;
|
||||
|
||||
INDEX2COORDS(i, xRank, xShape, xCoords);
|
||||
COORDS2INDEX(xRank, xStride, xCoords, xInd);
|
||||
INDEX2COORDS(i, yRank, yShape, yCoords);
|
||||
COORDS2INDEX(yRank, yStride, yCoords, yInd);
|
||||
|
||||
sd::math::sd_swap(x[xInd], y[yInd]);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
samediff::Threads::parallel_for(func, 0, length);
|
||||
}
|
||||
BUILD_SINGLE_TEMPLATE( void templatedSwap,
|
||||
(void* xBuffer, void* yBuffer, const sd::LongType* xShapeInfo, const sd::LongType* yShapeInfo,
|
||||
sd::LongType length),
|
||||
SD_COMMON_TYPES);
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
void NDArray::swapUnsafe(NDArray& other) {
|
||||
auto xType = this->dataType();
|
||||
|
||||
if (xType != other.dataType())
|
||||
THROW_EXCEPTION("NDArray::swapUnsage method: both arrays must have the same data type");
|
||||
|
||||
if (buffer() == nullptr || other.buffer() == nullptr)
|
||||
THROW_EXCEPTION("NDArray::swapUnsafe method: input array should not be empty!");
|
||||
|
||||
if (lengthOf() != other.lengthOf())
|
||||
THROW_EXCEPTION("NDArray::swapUnsafe method: input arrays should have the same length!");
|
||||
|
||||
BUILD_SINGLE_SELECTOR(xType, templatedSwap,
|
||||
(buffer(), other.buffer(), shapeInfo(), other.shapeInfo(), this->lengthOf()), SD_COMMON_TYPES);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
void NDArray::synchronize(const char* msg) {
|
||||
// no-op
|
||||
}
|
||||
|
||||
void NDArray::syncToDevice() {}
|
||||
void NDArray::syncToHost() {}
|
||||
void NDArray::tickWriteHost() {}
|
||||
void NDArray::tickWriteDevice() {}
|
||||
void NDArray::tickReadHost() {}
|
||||
void NDArray::tickReadDevice() {}
|
||||
void NDArray::tickBothActual() {}
|
||||
bool NDArray::isActualOnHostSide() { return true; }
|
||||
bool NDArray::isActualOnDeviceSide() { return true; }
|
||||
void NDArray::makeBothBuffersActual() {}
|
||||
|
||||
void NDArray::preparePrimaryUse(const std::vector<NDArray*>& writeList,
|
||||
const std::vector<NDArray*>& readList, bool synchronizeWritables) {
|
||||
// no-op
|
||||
}
|
||||
void NDArray::registerPrimaryUse(const std::vector<NDArray*>& writeList,
|
||||
const std::vector<NDArray*>& readList) {
|
||||
// no-op
|
||||
}
|
||||
|
||||
|
||||
void NDArray::prepareSpecialUse(const std::vector<NDArray*>& writeList,
|
||||
const std::vector<NDArray*>& readList, bool synchronizeWritables) {
|
||||
// no-op
|
||||
}
|
||||
void NDArray::registerSpecialUse(const std::vector<NDArray*>& writeList,
|
||||
const std::vector<NDArray*>& readList) {
|
||||
// no-op
|
||||
}
|
||||
|
||||
void NDArray::syncShape() {
|
||||
// no-op
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
template <typename T>
|
||||
void NDArray::printCurrentBuffer(const bool host, const char* msg, const int precision) {}
|
||||
template void NDArray::printCurrentBuffer<int>(const bool host, const char* msg, const int precision) ;
|
||||
template void NDArray::printCurrentBuffer<float>(const bool host, const char* msg, const int precision) ;
|
||||
template void NDArray::printCurrentBuffer<double>(const bool host, const char* msg, const int precision);
|
||||
template void NDArray::printCurrentBuffer<sd::LongType>(const bool host, const char* msg, const int precision) ;
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
void* NDArray::specialBuffer() {
|
||||
if (_buffer == nullptr) {
|
||||
THROW_EXCEPTION("NDArray::specialBuffer(): _buffer is nullptr - array not properly initialized");
|
||||
}
|
||||
|
||||
void* specialBuf = _buffer->special();
|
||||
|
||||
// On CPU, special buffer is nullptr (only used for GPU/CUDA) - this is expected and normal
|
||||
if (specialBuf == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return static_cast<int8_t*>(specialBuf) + (_offset * sizeOfT());
|
||||
}
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
// change an array by repeating it the number of times given by reps.
|
||||
NDArray NDArray::tile(const std::vector<sd::LongType>& reps) {
|
||||
const int repsSize = reps.size();
|
||||
|
||||
sd::LongType product = 1;
|
||||
for (const auto& item : reps) product *= item;
|
||||
if (product == 0) THROW_EXCEPTION("NDArray::tile method: one of the elements in reps array is zero !");
|
||||
|
||||
int rankOld = rankOf();
|
||||
int diff = rankOld - repsSize;
|
||||
if (product == 1) { // in this case 2 possibilities are present: just reshape or nothing to do
|
||||
NDArray result(*this);
|
||||
if (diff < 0) { // reshape to higher dimension
|
||||
std::vector<sd::LongType> shapeNew =
|
||||
reps; // there is requirement to have unities at first "diff" positions of new shape
|
||||
memcpy(&shapeNew[-diff], result.shapeInfo() + 1,
|
||||
rankOld * sizeof(sd::LongType)); // put old shape numbers at rest of positions
|
||||
result.reshapei(ordering(), shapeNew);
|
||||
}
|
||||
return result; // nothing to do, if diff >= 0 -> identity tile
|
||||
}
|
||||
|
||||
// evaluate shapeInfo for resulting array
|
||||
auto newShapeInfo = ShapeUtils::evalTileShapeInfo(*this, reps, getContext()->getWorkspace());
|
||||
// create new buffer, in any case the memory amount new buffer points to is bigger then those for old _buffer
|
||||
DataBuffer * newBuff =
|
||||
new DataBuffer(shape::length(newShapeInfo) * sizeOfT(), dataType(), getContext()->getWorkspace());
|
||||
// assign new shape and new buffer to resulting array
|
||||
NDArray result(newBuff,newShapeInfo , getContext());
|
||||
// fill newBuff, loop through all elements of newBuff
|
||||
// looping through _buffer goes automatically by means of getSubArrayIndex applying
|
||||
const auto resultLen = result.lengthOf();
|
||||
auto xType = this->dataType();
|
||||
auto func = PRAGMA_THREADS_FOR {
|
||||
for (auto i = start; i < stop; i++) {
|
||||
auto xOffset = result.getOffset(i);
|
||||
auto yOffset = shape::subArrayOffset(i, newShapeInfo, shapeInfo());
|
||||
BUILD_SINGLE_SELECTOR(xType, this->template templatedAssign,
|
||||
(result.buffer(), xOffset, this->buffer(), yOffset), SD_COMMON_TYPES);
|
||||
}
|
||||
};
|
||||
|
||||
samediff::Threads::parallel_for(func, 0, resultLen);
|
||||
result.tickWriteHost();
|
||||
return result;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
// change an array by repeating it the number of times given by reps.
|
||||
void NDArray::tile(const std::vector<LongType>& reps, NDArray& target) {
|
||||
// Validate the tile operation
|
||||
auto repProd = shape::prodLong(reps.data(), reps.size());
|
||||
if (repProd < 1)
|
||||
THROW_EXCEPTION("NDArray::tile: reps can't contain 0s");
|
||||
|
||||
// Validate the target shape
|
||||
auto correctShapeInfo = ShapeUtils::evalTileShapeInfo(*this, reps, getContext()->getWorkspace());
|
||||
if (!shape::equalsSoft(correctShapeInfo, target.shapeInfo())) {
|
||||
THROW_EXCEPTION("NDArray::tile method - shapeInfo of target array is not suitable for tile operation!");
|
||||
}
|
||||
|
||||
const auto targetLen = target.lengthOf();
|
||||
|
||||
// Safely calculate source array offset
|
||||
for (LongType i = 0; i < targetLen; ++i) {
|
||||
// Calculate target array offset
|
||||
auto xOffset = target.getOffset(i);
|
||||
|
||||
// Calculate source coordinates based on target coordinates
|
||||
LongType targetCoords[SD_MAX_RANK];
|
||||
INDEX2COORDS(i, shape::rank(target.shapeInfo()), shape::shapeOf(target.shapeInfo()), targetCoords);
|
||||
|
||||
// Map target coordinates to source coordinates manually
|
||||
LongType sourceCoords[SD_MAX_RANK];
|
||||
for (int d = 0; d < shape::rank(shapeInfo()); d++) {
|
||||
// Apply modulo for each dimension
|
||||
sourceCoords[d] = targetCoords[d] % shape::sizeAt(shapeInfo(), d);
|
||||
}
|
||||
|
||||
// Calculate source offset from source coordinates
|
||||
LongType sourceOffset;
|
||||
COORDS2INDEX(shape::rank(shapeInfo()), shape::stride(shapeInfo()), sourceCoords, sourceOffset);
|
||||
|
||||
auto targetDataType = target.dataType();
|
||||
auto selfDType = dataType();
|
||||
// Copy the value
|
||||
BUILD_DOUBLE_SELECTOR(target.dataType(), dataType(), templatedDoubleAssign,
|
||||
(target.buffer(), xOffset, buffer(), sourceOffset), SD_COMMON_TYPES, SD_COMMON_TYPES);
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
void NDArray::tile(NDArray& target) {
|
||||
if (rankOf() > target.rankOf())
|
||||
THROW_EXCEPTION(
|
||||
"NDArray::tile method - rank of target array must be bigger or equal to the rank of this array !");
|
||||
|
||||
if (!ShapeUtils::areShapesBroadcastable(*this, target))
|
||||
THROW_EXCEPTION("NDArray::tile method - shapeInfo of target array is not suitable for tile operation !");
|
||||
|
||||
// fill newBuff, loop through all elements of newBuff
|
||||
// looping through _buffer goes automatically by means of getSubArrayIndex applying
|
||||
const auto ews = target.ews();
|
||||
const auto targetLen = target.lengthOf();
|
||||
if (target.ordering() == 'c' && ews >= 1) {
|
||||
for (sd::LongType i = 0; i < targetLen; ++i) {
|
||||
auto yOffset = shape::subArrayOffset(i, target.shapeInfo(), shapeInfo());
|
||||
auto targetDataType = target.dataType();
|
||||
auto selfDType = dataType();
|
||||
BUILD_DOUBLE_SELECTOR(target.dataType(), dataType(), templatedDoubleAssign,
|
||||
(target.buffer(), i * ews, buffer(), yOffset), SD_COMMON_TYPES, SD_COMMON_TYPES);
|
||||
}
|
||||
} else {
|
||||
for (sd::LongType i = 0; i < targetLen; ++i) {
|
||||
auto xOffset = target.getOffset(i);
|
||||
auto yOffset = shape::subArrayOffset(i, target.shapeInfo(), shapeInfo());
|
||||
auto targetDataType = target.dataType();
|
||||
auto selfDType = dataType();
|
||||
BUILD_DOUBLE_SELECTOR(target.dataType(), dataType(), templatedDoubleAssign,
|
||||
(target.buffer(), xOffset, buffer(), yOffset), SD_COMMON_TYPES, SD_COMMON_TYPES);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
template <typename X, typename Z>
|
||||
static void repeat_(NDArray& input, NDArray& output, const std::vector<LongType>& repeats, const LongType axis) {
|
||||
const X* x = input.bufferAsT<X>();
|
||||
Z* z = output.bufferAsT<Z>();
|
||||
|
||||
const sd::LongType rank = input.rankOf(); // xRank = zRank
|
||||
const sd::LongType zLen = output.lengthOf(); // xLen <= zLen
|
||||
const sd::LongType repSize = repeats.size();
|
||||
|
||||
sd::LongType outputRank = output.rankOf();
|
||||
sd::LongType* outputShape = shape::shapeOf(output.shapeInfo());
|
||||
sd::LongType* outputStride = shape::stride(output.shapeInfo());
|
||||
|
||||
sd::LongType inputRank = input.rankOf();
|
||||
sd::LongType* inputShape = shape::shapeOf(input.shapeInfo());
|
||||
sd::LongType* inputStride = shape::stride(input.shapeInfo());
|
||||
|
||||
// loop through input array
|
||||
auto func = PRAGMA_THREADS_FOR {
|
||||
sd::LongType coords[SD_MAX_RANK], temp;
|
||||
|
||||
for (sd::LongType i = start; i < stop; i++) {
|
||||
INDEX2COORDS(i, outputRank, outputShape, coords);
|
||||
sd::LongType zOffset;
|
||||
COORDS2INDEX(outputRank, outputStride, coords, zOffset);
|
||||
|
||||
temp = coords[axis];
|
||||
|
||||
if (repSize > 1) {
|
||||
for (sd::LongType j = 0; j < repSize; ++j) {
|
||||
coords[axis] -= repeats[j];
|
||||
if (coords[axis] < 0) {
|
||||
coords[axis] = j;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else
|
||||
coords[axis] /= repeats[0];
|
||||
|
||||
sd::LongType xOffset;
|
||||
COORDS2INDEX(inputRank,inputStride, coords, xOffset);
|
||||
|
||||
z[zOffset] = x[xOffset];
|
||||
|
||||
coords[axis] = temp;
|
||||
}
|
||||
};
|
||||
|
||||
samediff::Threads::parallel_for(func, 0, zLen);
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
// create new array by repeating it the number of times given by repeats
|
||||
NDArray NDArray::repeat(const int axis, const std::vector<LongType>& repeats) {
|
||||
NDArray *thisArr = const_cast<NDArray*>(this);
|
||||
std::vector<sd::LongType> repeatShape = ShapeUtils::evalRepeatShape(axis, repeats, *thisArr);
|
||||
NDArray output('c',repeatShape, dataType(), getContext());
|
||||
|
||||
BUILD_SINGLE_SELECTOR_TWICE(dataType(), repeat_, (*this, output, repeats, axis), SD_COMMON_TYPES);
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
// fill array by repeating it the number of times given by reps
|
||||
void NDArray::repeat(const int axis, const std::vector<LongType>& repeats, NDArray& target) {
|
||||
NDArray *thisArr = const_cast<NDArray*>(this);
|
||||
if (!target.isSameShape(ShapeUtils::evalRepeatShape(axis, repeats, *thisArr)))
|
||||
THROW_EXCEPTION(
|
||||
"NDArray::repeat(const int axis, const std::vector<int>& repeats, NDArray& target) method: wrong shape of "
|
||||
"target array!");
|
||||
auto targetDataType = target.dataType();
|
||||
auto selfDType = dataType();
|
||||
BUILD_DOUBLE_SELECTOR(dataType(), target.dataType(), repeat_, (*this, target, repeats, axis), SD_COMMON_TYPES,
|
||||
SD_COMMON_TYPES);
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
#ifndef __JAVACPP_HACK__
|
||||
#include "NDArrayLambda.hpp"
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
} // namespace sd
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,149 @@
|
||||
################################################################################
|
||||
#
|
||||
#
|
||||
# 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
|
||||
################################################################################
|
||||
|
||||
#ifndef NDARRAY_MACRO
|
||||
#define NDARRAY_MACRO
|
||||
#include <op_boilerplate.h>
|
||||
|
||||
//NDArray<T> *other, T *extraParams
|
||||
BUILD_CALL_1(template void NDArray<float>::template applyPairwiseTransform, float, (NDArray<float>* other, float* extraParams), PAIRWISE_TRANSFORM_OPS)
|
||||
BUILD_CALL_1(template void NDArray<float16>::applyPairwiseTransform, float16, (NDArray<float16>* other, float16* extraParams), PAIRWISE_TRANSFORM_OPS)
|
||||
BUILD_CALL_1(template void NDArray<double>::applyPairwiseTransform, double, (NDArray<double>* other, double* extraParams), PAIRWISE_TRANSFORM_OPS)
|
||||
|
||||
// NDArray<T> *other, NDArray<T> *target, T *extraParams
|
||||
BUILD_CALL_1(template void sd::NDArray<float>::applyPairwiseTransform, float, (NDArray<float>* other, NDArray<float>* target, float* extraParams), PAIRWISE_TRANSFORM_OPS)
|
||||
BUILD_CALL_1(template void sd::NDArray<float16>::applyPairwiseTransform, float16, (NDArray<float16>* other, NDArray<float16>* target, float16* extraParams), PAIRWISE_TRANSFORM_OPS)
|
||||
BUILD_CALL_1(template void sd::NDArray<double>::applyPairwiseTransform, double, (NDArray<double>* other, NDArray<double>* target, double* extraParams), PAIRWISE_TRANSFORM_OPS)
|
||||
|
||||
BUILD_CALL_1(template void sd::NDArray<float16>::applyScalar, float16, (NDArray<float16>& scalar, NDArray<float16>* target, float16 *extraParams) const, SCALAR_OPS)
|
||||
BUILD_CALL_1(template void sd::NDArray<float16>::applyScalar, float16, (float16 scalar, NDArray<float16>* target, float16 *extraParams) const, SCALAR_OPS)
|
||||
|
||||
BUILD_CALL_1(template void sd::NDArray<float>::applyScalar, float, (NDArray<float>& scalar, NDArray<float>* target, float *extraParams) const, SCALAR_OPS)
|
||||
BUILD_CALL_1(template void sd::NDArray<float>::applyScalar, float, (float scalar, NDArray<float>* target, float *extraParams) const, SCALAR_OPS)
|
||||
|
||||
BUILD_CALL_1(template void sd::NDArray<double>::applyScalar, double, (NDArray<double>& scalar, NDArray<double>* target, double *extraParams) const, SCALAR_OPS)
|
||||
BUILD_CALL_1(template void sd::NDArray<double>::applyScalar, double, (double scalar, NDArray<double>* target, double *extraParams) const, SCALAR_OPS)
|
||||
|
||||
|
||||
BUILD_CALL_1(template float16 sd::NDArray<float16>::reduceNumber, float16, (float16 *extraParams) const, REDUCE_OPS)
|
||||
BUILD_CALL_1(template float sd::NDArray<float>::reduceNumber, float, (float *extraParams) const, REDUCE_OPS)
|
||||
BUILD_CALL_1(template double sd::NDArray<double>::reduceNumber, double, (double *extraParams) const, REDUCE_OPS)
|
||||
|
||||
BUILD_CALL_1(template LongType sd::NDArray<float16>::indexReduceNumber, float16, (float16 *extraParams), INDEX_REDUCE_OPS)
|
||||
BUILD_CALL_1(template LongType sd::NDArray<float>::indexReduceNumber, float, (float *extraParams), INDEX_REDUCE_OPS)
|
||||
BUILD_CALL_1(template LongType sd::NDArray<double>::indexReduceNumber, double, (double *extraParams), INDEX_REDUCE_OPS)
|
||||
|
||||
BUILD_CALL_1(template void sd::NDArray<float16>::applyBroadcast, float16, (std::initializer_list<int> list, const sd::NDArray<float16>* a, sd::NDArray<float16>* b, float16* c), BROADCAST_OPS)
|
||||
BUILD_CALL_1(template void sd::NDArray<float>::applyBroadcast, float, (std::initializer_list<int> list, const sd::NDArray<float>* a, sd::NDArray<float>* b, float* c), BROADCAST_OPS)
|
||||
BUILD_CALL_1(template void sd::NDArray<double>::applyBroadcast, double, (std::initializer_list<int> list, const sd::NDArray<double>* a, sd::NDArray<double>* b, double* c), BROADCAST_OPS)
|
||||
|
||||
BUILD_CALL_1(template void sd::NDArray<float16>::applyTrueBroadcast, float16,(const sd::NDArray<float16>* a, sd::NDArray<float16>* target, const bool checkTargetShape, float16* c) const, BROADCAST_OPS)
|
||||
BUILD_CALL_1(template void sd::NDArray<float>::applyTrueBroadcast, float, (const sd::NDArray<float>* a, sd::NDArray<float>* target, const bool checkTargetShape, float* c) const, BROADCAST_OPS)
|
||||
BUILD_CALL_1(template void sd::NDArray<double>::applyTrueBroadcast, double, (const sd::NDArray<double>* a, sd::NDArray<double>* target, const bool checkTargetShape, double* c) const, BROADCAST_OPS)
|
||||
|
||||
BUILD_CALL_1(template sd::NDArray<float16>* sd::NDArray<float16>::applyTrueBroadcast, float16, (const sd::NDArray<float16>* a, float16* c) const, BROADCAST_OPS)
|
||||
BUILD_CALL_1(template sd::NDArray<float>* sd::NDArray<float>::applyTrueBroadcast, float, (const sd::NDArray<float>* a, float* c) const, BROADCAST_OPS)
|
||||
BUILD_CALL_1(template sd::NDArray<double>* sd::NDArray<double>::applyTrueBroadcast, double, (const sd::NDArray<double>* a, double* c) const, BROADCAST_OPS)
|
||||
|
||||
BUILD_CALL_1(template sd::NDArray<float16> sd::NDArray<float16>::applyTrueBroadcast, float16, (const sd::NDArray<float16>& a, float16* c) const, BROADCAST_OPS)
|
||||
BUILD_CALL_1(template sd::NDArray<float> sd::NDArray<float>::applyTrueBroadcast, float, (const sd::NDArray<float>& a, float* c) const, BROADCAST_OPS)
|
||||
BUILD_CALL_1(template sd::NDArray<double> sd::NDArray<double>::applyTrueBroadcast, double, (const sd::NDArray<double>& a, double* c) const, BROADCAST_OPS)
|
||||
|
||||
BUILD_CALL_1(template void sd::NDArray<float16>::applyTransform, float16, (NDArray<float16>* target, float16* extraParams), TRANSFORM_OPS)
|
||||
BUILD_CALL_1(template void sd::NDArray<float>::applyTransform, float, (NDArray<float>* target, float* extraParams), TRANSFORM_OPS)
|
||||
BUILD_CALL_1(template void sd::NDArray<double>::applyTransform, double, (NDArray<double>* target, double* extraParams), TRANSFORM_OPS)
|
||||
|
||||
BUILD_CALL_1(template void sd::NDArray<float16>::applyTransform, float16, (float16* extraParams), TRANSFORM_OPS)
|
||||
BUILD_CALL_1(template void sd::NDArray<float>::applyTransform, float, (float* extraParams), TRANSFORM_OPS)
|
||||
BUILD_CALL_1(template void sd::NDArray<double>::applyTransform, double, (double* extraParams), TRANSFORM_OPS)
|
||||
|
||||
BUILD_CALL_1(template void sd::NDArray<float16>::applyRandom, float16, (sd::random::RandomBuffer *buffer, NDArray<float16>* y, NDArray<float16>* z, float16* extraParams), RANDOM_OPS)
|
||||
BUILD_CALL_1(template void sd::NDArray<float>::applyRandom, float, (sd::random::RandomBuffer *buffer, NDArray<float>* y, NDArray<float>* z, float* extraParams), RANDOM_OPS)
|
||||
BUILD_CALL_1(template void sd::NDArray<double>::applyRandom, double, (sd::random::RandomBuffer *buffer, NDArray<double>* y, NDArray<double>* z, double* extraParams), RANDOM_OPS)
|
||||
|
||||
BUILD_CALL_1(template NDArray<float16> sd::NDArray<float16>::transform, float16, (float16* extraParams) const, TRANSFORM_OPS)
|
||||
BUILD_CALL_1(template NDArray<float> sd::NDArray<float>::transform, float, (float* extraParams) const, TRANSFORM_OPS)
|
||||
BUILD_CALL_1(template NDArray<double> sd::NDArray<double>::transform, double, (double* extraParams) const, TRANSFORM_OPS)
|
||||
|
||||
BUILD_CALL_1(template NDArray<float> *sd::NDArray<float>::template reduceAlongDimension, float, (const std::vector<LongType>& dimensions, const bool keepDims, const bool supportOldShapes) const, REDUCE_OPS)
|
||||
BUILD_CALL_1(template NDArray<float16> *sd::NDArray<float16>::template reduceAlongDimension, float16, (const std::vector<LongType>& dimensions, const bool keepDims, const bool supportOldShapes) const, REDUCE_OPS)
|
||||
BUILD_CALL_1(template NDArray<double> *sd::NDArray<double>::template reduceAlongDimension, double, (const std::vector<LongType>& dimensions, const bool keepDims, const bool supportOldShapes) const, REDUCE_OPS)
|
||||
|
||||
BUILD_CALL_1(template NDArray<float> sd::NDArray<float>::template reduceAlongDims, float, (const std::vector<LongType>& dimensions, const bool keepDims, const bool supportOldShapes) const, REDUCE_OPS)
|
||||
BUILD_CALL_1(template NDArray<float16> sd::NDArray<float16>::template reduceAlongDims, float16, (const std::vector<LongType>& dimensions, const bool keepDims, const bool supportOldShapes) const, REDUCE_OPS)
|
||||
BUILD_CALL_1(template NDArray<double> sd::NDArray<double>::template reduceAlongDims, double, (const std::vector<LongType>& dimensions, const bool keepDims, const bool supportOldShapes) const, REDUCE_OPS)
|
||||
|
||||
BUILD_CALL_1(template NDArray<float> *sd::NDArray<float>::template reduceAlongDimension, float, (const std::initializer_list<int>& dimensions, const bool keepDims, const bool supportOldShapes) const, REDUCE_OPS)
|
||||
BUILD_CALL_1(template NDArray<float16> *sd::NDArray<float16>::template reduceAlongDimension, float16, (const std::initializer_list<int>& dimensions, const bool keepDims, const bool supportOldShapes) const, REDUCE_OPS)
|
||||
BUILD_CALL_1(template NDArray<double> *sd::NDArray<double>::template reduceAlongDimension, double, (const std::initializer_list<int>& dimensions, const bool keepDims, const bool supportOldShapes) const, REDUCE_OPS)
|
||||
|
||||
BUILD_CALL_1(template void sd::NDArray<float>::template reduceAlongDimension, float, (NDArray<float>* target, const std::vector<LongType>& dimensions, const bool keepDims, const bool supportOldShapes, float * extras) const, REDUCE_OPS)
|
||||
BUILD_CALL_1(template void sd::NDArray<float16>::template reduceAlongDimension, float16, (NDArray<float16>* target, const std::vector<LongType>& dimensions, const bool keepDims, const bool supportOldShapes, float16 * extras) const, REDUCE_OPS)
|
||||
BUILD_CALL_1(template void sd::NDArray<double>::template reduceAlongDimension, double, (NDArray<double>* target, const std::vector<int>& dimension, const bool keepDims, const bool supportOldShapes, double * extras) const, REDUCE_OPS)
|
||||
|
||||
BUILD_CALL_1(template NDArray<float> *sd::NDArray<float>::template varianceAlongDimension, float, (const bool biasCorrected, const std::initializer_list<int>& dimensions) const, SUMMARY_STATS_OPS)
|
||||
BUILD_CALL_1(template NDArray<float16> *sd::NDArray<float16>::template varianceAlongDimension, float16, (const bool biasCorrected, const std::initializer_list<int>& dimensions) const, SUMMARY_STATS_OPS)
|
||||
BUILD_CALL_1(template NDArray<double> *sd::NDArray<double>::template varianceAlongDimension, double, (const bool biasCorrected, const std::initializer_list<int>& dimensions) const, SUMMARY_STATS_OPS)
|
||||
|
||||
BUILD_CALL_1(template void sd::NDArray<float>::template varianceAlongDimension, float, (const NDArray<float> *target, const bool biasCorrected, const std::initializer_list<int>& dimensions), SUMMARY_STATS_OPS)
|
||||
BUILD_CALL_1(template void sd::NDArray<float16>::template varianceAlongDimension, float16, (const NDArray<float16> *target,const bool biasCorrected, const std::initializer_list<int>& dimensions), SUMMARY_STATS_OPS)
|
||||
BUILD_CALL_1(template void sd::NDArray<double>::template varianceAlongDimension, double, (const NDArray<double> *target, const bool biasCorrected, const std::initializer_list<int>& dimensions), SUMMARY_STATS_OPS)
|
||||
|
||||
BUILD_CALL_1(template void sd::NDArray<float>::template varianceAlongDimension, float, (const NDArray<float> *target, const bool biasCorrected, const std::vector<LongType>& dimensions), SUMMARY_STATS_OPS)
|
||||
BUILD_CALL_1(template void sd::NDArray<float16>::template varianceAlongDimension, float16, (const NDArray<float16> *target,const bool biasCorrected, const std::vector<LongType>& dimensions), SUMMARY_STATS_OPS)
|
||||
BUILD_CALL_1(template void sd::NDArray<double>::template varianceAlongDimension, double, (const NDArray<double> *target, const bool biasCorrected, const std::vector<LongType>& dimensions), SUMMARY_STATS_OPS)
|
||||
|
||||
BUILD_CALL_1(template float sd::NDArray<float>::template varianceNumber, float, (bool biasCorrected), SUMMARY_STATS_OPS)
|
||||
BUILD_CALL_1(template float16 sd::NDArray<float16>::template varianceNumber, float16, (bool biasCorrected), SUMMARY_STATS_OPS)
|
||||
BUILD_CALL_1(template double sd::NDArray<double>::template varianceNumber, double, (bool biasCorrected), SUMMARY_STATS_OPS)
|
||||
|
||||
BUILD_CALL_1(template NDArray<float> *sd::NDArray<float>::template applyReduce3, float, (const NDArray<float>* other, const float* extraParams) const, REDUCE3_OPS)
|
||||
BUILD_CALL_1(template NDArray<float16> *sd::NDArray<float16>::template applyReduce3, float16, (const NDArray<float16>* other, const float16* extraParams) const, REDUCE3_OPS)
|
||||
BUILD_CALL_1(template NDArray<double> *sd::NDArray<double>::template applyReduce3, double, (const NDArray<double>* other, const double* extraParams) const, REDUCE3_OPS)
|
||||
|
||||
BUILD_CALL_1(template NDArray<float> *sd::NDArray<float>::template applyReduce3, float, (const NDArray<float>* other, const std::vector<int> &dims, const float* extraParams) const, REDUCE3_OPS)
|
||||
BUILD_CALL_1(template NDArray<float16> *sd::NDArray<float16>::template applyReduce3, float16, (const NDArray<float16>* other, const std::vector<int> &dims, const float16* extraParams) const, REDUCE3_OPS)
|
||||
BUILD_CALL_1(template NDArray<double> *sd::NDArray<double>::template applyReduce3, double, (const NDArray<double>* other, const std::vector<int> &dims, const double* extraParams) const, REDUCE3_OPS)
|
||||
|
||||
BUILD_CALL_1(template void sd::NDArray<float>::template applyIndexReduce, float, (const NDArray<float>* target, const std::vector<int> & alpha, const float* beta) const, INDEX_REDUCE_OPS)
|
||||
BUILD_CALL_1(template void sd::NDArray<float16>::template applyIndexReduce, float16, (const NDArray<float16>* target, const std::vector<int> & alpha, const float16* beta) const, INDEX_REDUCE_OPS)
|
||||
BUILD_CALL_1(template void sd::NDArray<double>::template applyIndexReduce, double, (const NDArray<double>* target, const std::vector<int> & alpha, const double* beta) const, INDEX_REDUCE_OPS)
|
||||
|
||||
BUILD_CALL_1(template NDArray<float> *sd::NDArray<float>::template applyIndexReduce, float, (const std::vector<int> & alpha, const float* beta) const, INDEX_REDUCE_OPS)
|
||||
BUILD_CALL_1(template NDArray<float16> *sd::NDArray<float16>::template applyIndexReduce, float16, (const std::vector<int> & alpha, const float16* beta) const, INDEX_REDUCE_OPS)
|
||||
BUILD_CALL_1(template NDArray<double> *sd::NDArray<double>::template applyIndexReduce, double, (const std::vector<int> & alpha, const double* beta) const, INDEX_REDUCE_OPS)
|
||||
|
||||
BUILD_CALL_1(template NDArray<float> *sd::NDArray<float>::template applyAllReduce3, float, (const sd::NDArray<float>* alpha, const std::vector<int> & beta, float const* gamma) const, REDUCE3_OPS)
|
||||
BUILD_CALL_1(template NDArray<float16> *sd::NDArray<float16>::template applyAllReduce3, float16, (const sd::NDArray<float16>* alpha, const std::vector<int> & beta, float16 const* gamma) const, REDUCE3_OPS)
|
||||
BUILD_CALL_1(template NDArray<double> *sd::NDArray<double>::template applyAllReduce3, double, (const sd::NDArray<double>* alpha, const std::vector<int> & beta, double const* gamma) const, REDUCE3_OPS)
|
||||
|
||||
template NDArray<float> mmul(const NDArray<float>& left, const NDArray<float>& right);
|
||||
template NDArray<float16> mmul(const NDArray<float16>& left, const NDArray<float16>& right);
|
||||
template NDArray<double> mmul(const NDArray<double>& left, const NDArray<double>& right);
|
||||
|
||||
// template NDArray<float> operator-(const float, const NDArray<float>&);
|
||||
// template NDArray<float16> operator-(const float16, const NDArray<float16>&);
|
||||
// template NDArray<double> operator-(const double, const NDArray<double>&);
|
||||
|
||||
// template NDArray<float> operator+(const float, const NDArray<float>&);
|
||||
// template NDArray<float16> operator+(const float16, const NDArray<float16>&);
|
||||
// template NDArray<double> operator+(const double, const NDArray<double>&);
|
||||
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,559 @@
|
||||
/*
|
||||
* ******************************************************************************
|
||||
* *
|
||||
* *
|
||||
* * 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
|
||||
* *****************************************************************************
|
||||
*/
|
||||
template <typename T>
|
||||
SD_LIB_HIDDEN void NDArray::applyTriplewiseLambda(NDArray* second, NDArray* third,
|
||||
std::function<T(T, T, T)>& func, NDArray* target) {
|
||||
if (dataType() != DataTypeUtils::fromT<T>())
|
||||
THROW_EXCEPTION(
|
||||
"NDArray::applyTriplewiseLambda<T> method: wrong template parameter T, its type should be the same as type of "
|
||||
"this array!");
|
||||
if (dataType() != second->dataType() || dataType() != third->dataType() || dataType() != target->dataType())
|
||||
THROW_EXCEPTION(
|
||||
"NDArray::applyTriplewiseLambda<T> method: bother four arrays (this, second, third, target) should have the "
|
||||
"same type !");
|
||||
|
||||
if (this->lengthOf() != second->lengthOf() || this->lengthOf() != third->lengthOf() || !this->isSameShape(second) ||
|
||||
!this->isSameShape(third)) {
|
||||
std::string errorMessage;
|
||||
errorMessage += "applyTriplewiseLambda requires all operands to have the same shape\n";
|
||||
errorMessage += "this shape: " + ShapeUtils::shapeAsString(this->shapeInfo()) + "\n";
|
||||
errorMessage += "second shape: " + ShapeUtils::shapeAsString(second->shapeInfo()) + "\n";
|
||||
errorMessage += "third shape: " + ShapeUtils::shapeAsString(third->shapeInfo()) + "\n";
|
||||
errorMessage += "target shape: " + ShapeUtils::shapeAsString(target->shapeInfo()) + "\n";
|
||||
THROW_EXCEPTION(errorMessage.c_str());
|
||||
}
|
||||
|
||||
auto f = this->bufferAsT<T>();
|
||||
auto s = second->bufferAsT<T>();
|
||||
auto t = third->bufferAsT<T>();
|
||||
auto z = target->bufferAsT<T>();
|
||||
if (f == z) {
|
||||
auto loop = PRAGMA_THREADS_FOR {
|
||||
for (auto e = start; e < stop; e++) {
|
||||
auto tOffset = this->getOffset(e);
|
||||
auto uOffset = second->getOffset(e);
|
||||
auto vOffset = third->getOffset(e);
|
||||
|
||||
f[tOffset] = func(f[tOffset], s[uOffset], t[vOffset]);
|
||||
}
|
||||
};
|
||||
|
||||
samediff::Threads::parallel_for(loop, 0, _length);
|
||||
} else {
|
||||
auto loop = PRAGMA_THREADS_FOR {
|
||||
for (auto e = start; e < stop; e++) {
|
||||
auto tOffset = this->getOffset(e);
|
||||
auto uOffset = second->getOffset(e);
|
||||
auto vOffset = third->getOffset(e);
|
||||
auto zOffset = target->getOffset(e);
|
||||
|
||||
z[zOffset] = func(f[tOffset], s[uOffset], t[vOffset]);
|
||||
}
|
||||
};
|
||||
|
||||
samediff::Threads::parallel_for(loop, 0, _length);
|
||||
}
|
||||
|
||||
}
|
||||
#if defined(HAS_DOUBLE)
|
||||
template SD_LIB_HIDDEN void NDArray::applyTriplewiseLambda(NDArray* second, NDArray* third,
|
||||
std::function<double(double, double, double)>& func,
|
||||
NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_FLOAT32)
|
||||
template SD_LIB_HIDDEN void NDArray::applyTriplewiseLambda(NDArray* second, NDArray* third,
|
||||
std::function<float(float, float, float)>& func,
|
||||
NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_FLOAT16)
|
||||
template SD_LIB_HIDDEN void NDArray::applyTriplewiseLambda(
|
||||
NDArray* second, NDArray* third, std::function<float16(float16, float16, float16)>& func, NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_BFLOAT16)
|
||||
template SD_LIB_HIDDEN void NDArray::applyTriplewiseLambda(
|
||||
NDArray* second, NDArray* third, std::function<bfloat16(bfloat16, bfloat16, bfloat16)>& func,
|
||||
NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_INT64)
|
||||
template SD_LIB_HIDDEN void NDArray::applyTriplewiseLambda(
|
||||
NDArray* second, NDArray* third, std::function<sd::LongType(sd::LongType, sd::LongType, sd::LongType)>& func,
|
||||
NDArray* target);
|
||||
template SD_LIB_HIDDEN void NDArray::applyTriplewiseLambda(
|
||||
NDArray* second, NDArray* third, std::function<long(long, long, long)>& func,
|
||||
NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_INT32)
|
||||
template SD_LIB_HIDDEN void NDArray::applyTriplewiseLambda(NDArray* second, NDArray* third,
|
||||
std::function<int(int, int, int)>& func,
|
||||
NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_INT16)
|
||||
template SD_LIB_HIDDEN void NDArray::applyTriplewiseLambda(
|
||||
NDArray* second, NDArray* third, std::function<int16_t(int16_t, int16_t, int16_t)>& func, NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_INT8)
|
||||
template SD_LIB_HIDDEN void NDArray::applyTriplewiseLambda(
|
||||
NDArray* second, NDArray* third, std::function<char(char, char, char)>& func, NDArray* target);
|
||||
template SD_LIB_HIDDEN void NDArray::applyTriplewiseLambda(
|
||||
NDArray* second, NDArray* third, std::function<signed char(signed char, signed char, signed char)>& func, NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_UINT8)
|
||||
template SD_LIB_HIDDEN void NDArray::applyTriplewiseLambda(
|
||||
NDArray* second, NDArray* third, std::function<uint8_t(uint8_t, uint8_t, uint8_t)>& func, NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_UINT16)
|
||||
template SD_LIB_HIDDEN void NDArray::applyTriplewiseLambda(
|
||||
NDArray* second, NDArray* third, std::function<uint16_t(uint16_t, uint16_t, uint16_t)>& func,
|
||||
NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_UINT32)
|
||||
template SD_LIB_HIDDEN void NDArray::applyTriplewiseLambda(
|
||||
NDArray* second, NDArray* third, std::function<uint32_t(uint32_t, uint32_t, uint32_t)>& func,
|
||||
NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_UINT64)
|
||||
template SD_LIB_HIDDEN void NDArray::applyTriplewiseLambda(
|
||||
NDArray* second, NDArray* third, std::function<uint64_t(uint64_t, uint64_t, uint64_t)>& func,
|
||||
NDArray* target);
|
||||
#endif
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
template <typename T>
|
||||
SD_LIB_HIDDEN void NDArray::applyPairwiseLambda(NDArray* other, std::function<T(T, T)>& func,
|
||||
NDArray* target) {
|
||||
if (dataType() != DataTypeUtils::fromT<T>())
|
||||
THROW_EXCEPTION(
|
||||
"NDArray::applyPairwiseLambda<T> method: wrong template parameter T, its type should be the same as type of "
|
||||
"this array!");
|
||||
if (dataType() != other->dataType() || dataType() != target->dataType())
|
||||
THROW_EXCEPTION(
|
||||
"NDArray::applyPairwiseLambda<T> method: all three arrays (this, other, target) must have the same type !");
|
||||
|
||||
// scalar is broadcastable
|
||||
if (this->lengthOf() != other->lengthOf() && !this->isScalar() && !other->isScalar()) {
|
||||
THROW_EXCEPTION("applyPairwiseLambda requires both operands to have the same shape");
|
||||
}
|
||||
|
||||
auto f = this->bufferAsT<T>();
|
||||
auto s = other->bufferAsT<T>();
|
||||
auto z = target->bufferAsT<T>();
|
||||
auto isTargetOrderEws = !isView() && !target->isView() && this->ordering() == target->ordering() && (this->ews() == 1 && target->ews() == 1);
|
||||
if (other->isScalar()) {
|
||||
auto otherVal = s[other->getOffset(0)];
|
||||
if (isTargetOrderEws) {
|
||||
auto loop = PRAGMA_THREADS_FOR {
|
||||
for (auto e = start; e < stop; e++) z[e] = func(f[e], otherVal);
|
||||
};
|
||||
|
||||
samediff::Threads::parallel_for(loop, 0, _length);
|
||||
} else {
|
||||
if (f == z) {
|
||||
auto loop = PRAGMA_THREADS_FOR {
|
||||
for (auto e = start; e < stop; e++) {
|
||||
auto xOffset = this->getOffset(e);
|
||||
f[xOffset] = func(f[xOffset], otherVal);
|
||||
}
|
||||
};
|
||||
|
||||
samediff::Threads::parallel_for(loop, 0, _length);
|
||||
} else {
|
||||
auto loop = PRAGMA_THREADS_FOR {
|
||||
for (auto e = start; e < stop; e++) {
|
||||
auto xOffset = this->getOffset(e);
|
||||
auto zOffset = target->getOffset(e);
|
||||
|
||||
z[zOffset] = func(f[xOffset], otherVal);
|
||||
}
|
||||
};
|
||||
|
||||
samediff::Threads::parallel_for(loop, 0, _length);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (f == z && !this->isView() && !other->isView() && this->ordering() == other->ordering()) {
|
||||
auto loop = PRAGMA_THREADS_FOR {
|
||||
for (auto e = start; e < stop; e++) {
|
||||
auto xOffset = this->getOffset(e);
|
||||
auto yOffset = other->getOffset(e);
|
||||
|
||||
f[xOffset] = func(f[xOffset], s[yOffset]);
|
||||
}
|
||||
};
|
||||
|
||||
samediff::Threads::parallel_for(loop, 0, _length);
|
||||
} else {
|
||||
auto loop = PRAGMA_THREADS_FOR {
|
||||
for (auto e = start; e < stop; e++) {
|
||||
auto xOffset = this->getOffset(e);
|
||||
auto yOffset = other->getOffset(e);
|
||||
auto zOffset = target->getOffset(e);
|
||||
|
||||
z[zOffset] = func(f[xOffset], s[yOffset]);
|
||||
}
|
||||
};
|
||||
|
||||
samediff::Threads::parallel_for(loop, 0, _length);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#if defined(HAS_DOUBLE)
|
||||
template SD_LIB_HIDDEN void NDArray::applyPairwiseLambda(NDArray* other,
|
||||
std::function<double(double, double)>& func,
|
||||
NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_FLOAT32)
|
||||
template SD_LIB_HIDDEN void NDArray::applyPairwiseLambda(NDArray* other,
|
||||
std::function<float(float, float)>& func,
|
||||
NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_FLOAT16)
|
||||
template SD_LIB_HIDDEN void NDArray::applyPairwiseLambda(NDArray* other,
|
||||
std::function<float16(float16, float16)>& func,
|
||||
NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_BFLOAT16)
|
||||
template SD_LIB_HIDDEN void NDArray::applyPairwiseLambda(NDArray* other,
|
||||
std::function<bfloat16(bfloat16, bfloat16)>& func,
|
||||
NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_INT64)
|
||||
template SD_LIB_HIDDEN void NDArray::applyPairwiseLambda(
|
||||
NDArray* other, std::function<sd::LongType(sd::LongType, sd::LongType)>& func, NDArray* target);
|
||||
template SD_LIB_HIDDEN void NDArray::applyPairwiseLambda(
|
||||
NDArray* other, std::function<long(long, long)>& func, NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_INT32)
|
||||
template SD_LIB_HIDDEN void NDArray::applyPairwiseLambda(NDArray* other, std::function<int(int, int)>& func,
|
||||
NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_INT16)
|
||||
template SD_LIB_HIDDEN void NDArray::applyPairwiseLambda(NDArray* other,
|
||||
std::function<int16_t(int16_t, int16_t)>& func,
|
||||
NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_UINT8)
|
||||
template SD_LIB_HIDDEN void NDArray::applyPairwiseLambda(NDArray* other,
|
||||
std::function<uint8_t(uint8_t, uint8_t)>& func,
|
||||
NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_UINT16)
|
||||
template SD_LIB_HIDDEN void NDArray::applyPairwiseLambda(NDArray* other,
|
||||
std::function<uint16_t(uint16_t, uint16_t)>& func,
|
||||
NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_UINT32)
|
||||
template SD_LIB_HIDDEN void NDArray::applyPairwiseLambda(NDArray* other,
|
||||
std::function<uint32_t(uint32_t, uint32_t)>& func,
|
||||
NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_UNSIGNEDLONG)
|
||||
template SD_LIB_HIDDEN void NDArray::applyPairwiseLambda(NDArray* other,
|
||||
std::function<uint64_t(uint64_t, uint64_t)>& func,
|
||||
NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_INT8)
|
||||
template SD_LIB_HIDDEN void NDArray::applyPairwiseLambda(NDArray* other,
|
||||
std::function<int8_t(int8_t, int8_t)>& func,
|
||||
NDArray* target);
|
||||
template SD_LIB_HIDDEN void NDArray::applyPairwiseLambda(NDArray* other,
|
||||
std::function<SignedChar(SignedChar, SignedChar)>& func,
|
||||
NDArray* target);
|
||||
template SD_LIB_HIDDEN void NDArray::applyPairwiseLambda(NDArray* other,
|
||||
std::function<char(char, char)>& func,
|
||||
NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_BOOL)
|
||||
template SD_LIB_HIDDEN void NDArray::applyPairwiseLambda(NDArray* other,
|
||||
std::function<bool(bool, bool)>& func, NDArray* target);
|
||||
#endif
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
template <typename T>
|
||||
SD_LIB_HIDDEN void NDArray::applyLambda(std::function<T(T)>& func, NDArray* target) {
|
||||
if (dataType() != DataTypeUtils::fromT<T>())
|
||||
THROW_EXCEPTION(
|
||||
"NDArray::applyLambda<T> method: wrong template parameter T, its type should be the same as type of this "
|
||||
"array!");
|
||||
if (dataType() != target->dataType())
|
||||
THROW_EXCEPTION("NDArray::applyLambda<T> method: types of this and target array should match !");
|
||||
|
||||
auto f = this->bufferAsT<T>();
|
||||
auto z = target->bufferAsT<T>();
|
||||
|
||||
if (f == z) {
|
||||
auto loop = PRAGMA_THREADS_FOR {
|
||||
for (auto e = start; e < stop; e+= increment) {
|
||||
auto xOffset = this->getOffset(e);
|
||||
f[xOffset] = func(f[xOffset]);
|
||||
}
|
||||
};
|
||||
|
||||
samediff::Threads::parallel_for(loop, 0, _length,1);
|
||||
} else {
|
||||
auto loop = PRAGMA_THREADS_FOR {
|
||||
for (auto e = start; e < stop; e+= increment) {
|
||||
auto xOffset = this->getOffset(e);
|
||||
auto zOffset = target->getOffset(e);
|
||||
z[zOffset] = func(f[xOffset]);
|
||||
}
|
||||
};
|
||||
|
||||
samediff::Threads::parallel_for(loop, 0, _length,1);
|
||||
}
|
||||
|
||||
}
|
||||
#if defined(HAS_DOUBLE)
|
||||
template SD_LIB_HIDDEN void NDArray::applyLambda(std::function<double(double)>& func, NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_FLOAT32)
|
||||
template SD_LIB_HIDDEN void NDArray::applyLambda(std::function<float(float)>& func, NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_FLOAT16)
|
||||
template SD_LIB_HIDDEN void NDArray::applyLambda(std::function<float16(float16)>& func, NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_BFLOAT16)
|
||||
template SD_LIB_HIDDEN void NDArray::applyLambda(std::function<bfloat16(bfloat16)>& func, NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_INT64)
|
||||
template SD_LIB_HIDDEN void NDArray::applyLambda(std::function<sd::LongType(sd::LongType)>& func,
|
||||
NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_INT16)
|
||||
template SD_LIB_HIDDEN void NDArray::applyLambda(std::function<int16_t(int16_t)>& func, NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_INT32)
|
||||
template SD_LIB_HIDDEN void NDArray::applyLambda(std::function<int32_t(int32_t)>& func, NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_UINT8)
|
||||
template SD_LIB_HIDDEN void NDArray::applyLambda(std::function<uint8_t(uint8_t)>& func, NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_UINT16)
|
||||
template SD_LIB_HIDDEN void NDArray::applyLambda(std::function<uint16_t(uint16_t)>& func, NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_UINT32)
|
||||
template SD_LIB_HIDDEN void NDArray::applyLambda(std::function<uint32_t(uint32_t)>& func, NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_UNSIGNEDLONG)
|
||||
template SD_LIB_HIDDEN void NDArray::applyLambda(std::function<uint64_t(uint64_t)>& func, NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_INT8)
|
||||
template SD_LIB_HIDDEN void NDArray::applyLambda(std::function<int8_t(int8_t)>& func, NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_BOOL)
|
||||
template SD_LIB_HIDDEN void NDArray::applyLambda(std::function<bool(bool)>& func, NDArray* target);
|
||||
#endif
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
template <typename T>
|
||||
SD_LIB_HIDDEN void NDArray::applyIndexedLambda(std::function<T(sd::LongType, T)>& func, NDArray* target) {
|
||||
if (dataType() != DataTypeUtils::fromT<T>())
|
||||
THROW_EXCEPTION(
|
||||
"NDArray::applyIndexedLambda<T> method: wrong template parameter T, its type should be the same as type of "
|
||||
"this array!");
|
||||
if (dataType() != target->dataType())
|
||||
THROW_EXCEPTION("NDArray::applyIndexedLambda<T> method: types of this and target array should match !");
|
||||
|
||||
auto f = this->bufferAsT<T>();
|
||||
auto z = target->bufferAsT<T>();
|
||||
|
||||
if (this->ordering() == target->ordering() && (this->ews() == 1 && target->ews() == 1)) {
|
||||
auto loop = PRAGMA_THREADS_FOR {
|
||||
for (auto e = start; e < stop; e++) z[e] = func(e, f[e]);
|
||||
};
|
||||
|
||||
samediff::Threads::parallel_for(loop, 0, _length);
|
||||
} else {
|
||||
if (f == z) {
|
||||
auto loop = PRAGMA_THREADS_FOR {
|
||||
for (auto e = start; e < stop; e++) {
|
||||
auto xOffset = this->getOffset(e);
|
||||
|
||||
f[xOffset] = func(e, f[xOffset]);
|
||||
}
|
||||
};
|
||||
|
||||
samediff::Threads::parallel_for(loop, 0, _length);
|
||||
} else {
|
||||
auto loop = PRAGMA_THREADS_FOR {
|
||||
for (auto e = start; e < stop; e++) {
|
||||
auto xOffset = this->getOffset(e);
|
||||
auto zOffset = target->getOffset(e);
|
||||
|
||||
z[zOffset] = func(e, f[xOffset]);
|
||||
}
|
||||
};
|
||||
|
||||
samediff::Threads::parallel_for(loop, 0, _length);
|
||||
}
|
||||
}
|
||||
}
|
||||
#if defined(HAS_DOUBLE)
|
||||
template SD_LIB_HIDDEN void NDArray::applyIndexedLambda(std::function<double(sd::LongType, double)>& func,
|
||||
NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_FLOAT32)
|
||||
template SD_LIB_HIDDEN void NDArray::applyIndexedLambda(std::function<float(sd::LongType, float)>& func,
|
||||
NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_FLOAT16)
|
||||
template SD_LIB_HIDDEN void NDArray::applyIndexedLambda(std::function<float16(sd::LongType, float16)>& func,
|
||||
NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_BFLOAT16)
|
||||
template SD_LIB_HIDDEN void NDArray::applyIndexedLambda(std::function<bfloat16(sd::LongType, bfloat16)>& func,
|
||||
NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_INT64)
|
||||
template SD_LIB_HIDDEN void NDArray::applyIndexedLambda(
|
||||
std::function<sd::LongType(sd::LongType, sd::LongType)>& func, NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_INT32)
|
||||
template SD_LIB_HIDDEN void NDArray::applyIndexedLambda(std::function<int(sd::LongType, int)>& func,
|
||||
NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_INT16)
|
||||
template SD_LIB_HIDDEN void NDArray::applyIndexedLambda(std::function<int16_t(sd::LongType, int16_t)>& func,
|
||||
NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_UINT8)
|
||||
template SD_LIB_HIDDEN void NDArray::applyIndexedLambda(std::function<uint8_t(sd::LongType, uint8_t)>& func,
|
||||
NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_UINT16)
|
||||
template SD_LIB_HIDDEN void NDArray::applyIndexedLambda(std::function<uint16_t(sd::LongType, uint16_t)>& func,
|
||||
NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_UINT32)
|
||||
template SD_LIB_HIDDEN void NDArray::applyIndexedLambda(std::function<uint32_t(sd::LongType, uint32_t)>& func,
|
||||
NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_UNSIGNEDLONG)
|
||||
template SD_LIB_HIDDEN void NDArray::applyIndexedLambda(std::function<uint64_t(sd::LongType, uint64_t)>& func,
|
||||
NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_INT8)
|
||||
template SD_LIB_HIDDEN void NDArray::applyIndexedLambda(std::function<int8_t(sd::LongType, int8_t)>& func,
|
||||
NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_BOOL)
|
||||
template SD_LIB_HIDDEN void NDArray::applyIndexedLambda(std::function<bool(sd::LongType, bool)>& func,
|
||||
NDArray* target);
|
||||
#endif
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
template <typename T>
|
||||
SD_LIB_HIDDEN void NDArray::applyIndexedPairwiseLambda(NDArray* other, std::function<T(sd::LongType, T, T)>& func,
|
||||
NDArray* target) {
|
||||
if (dataType() != DataTypeUtils::fromT<T>())
|
||||
THROW_EXCEPTION(
|
||||
"NDArray::applyIndexedPairwiseLambda<T> method: wrong template parameter T, its type should be the same as "
|
||||
"type of this array!");
|
||||
if (dataType() != target->dataType())
|
||||
THROW_EXCEPTION(
|
||||
"NDArray::applyIndexedPairwiseLambda<T> method: types of this and target array should match !");
|
||||
if (this->lengthOf() != other->lengthOf()) {
|
||||
sd_printf("applyIndexedPairwiseLambda requires both operands to have the same shape\n", "");
|
||||
THROW_EXCEPTION("Shapes mismatch");
|
||||
}
|
||||
|
||||
auto f = this->bufferAsT<T>();
|
||||
auto s = other->bufferAsT<T>();
|
||||
auto z = target->bufferAsT<T>();
|
||||
if (f == z) {
|
||||
auto loop = PRAGMA_THREADS_FOR {
|
||||
for (auto e = start; e < stop; e++) {
|
||||
auto xOffset = this->getOffset(e);
|
||||
auto yOffset = other->getOffset(e);
|
||||
|
||||
f[xOffset] = func((sd::LongType)e, f[xOffset], s[yOffset]);
|
||||
}
|
||||
};
|
||||
|
||||
samediff::Threads::parallel_for(loop, 0, _length);
|
||||
} else {
|
||||
auto loop = PRAGMA_THREADS_FOR {
|
||||
for (auto e = start; e < stop; e++) {
|
||||
auto xOffset = this->getOffset(e);
|
||||
auto yOffset = other->getOffset(e);
|
||||
auto zOffset = target->getOffset(e);
|
||||
|
||||
z[zOffset] = func((sd::LongType)e, f[xOffset], s[yOffset]);
|
||||
}
|
||||
};
|
||||
|
||||
samediff::Threads::parallel_for(loop, 0, _length);
|
||||
}
|
||||
|
||||
}
|
||||
#if defined(HAS_DOUBLE)
|
||||
template SD_LIB_HIDDEN void NDArray::applyIndexedPairwiseLambda(
|
||||
NDArray* other, std::function<double(sd::LongType, double, double)>& func, NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_FLOAT32)
|
||||
template SD_LIB_HIDDEN void NDArray::applyIndexedPairwiseLambda(
|
||||
NDArray* other, std::function<float(sd::LongType, float, float)>& func, NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_FLOAT16)
|
||||
template SD_LIB_HIDDEN void NDArray::applyIndexedPairwiseLambda(
|
||||
NDArray* other, std::function<float16(sd::LongType, float16, float16)>& func, NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_BFLOAT16)
|
||||
template SD_LIB_HIDDEN void NDArray::applyIndexedPairwiseLambda(
|
||||
NDArray* other, std::function<bfloat16(sd::LongType, bfloat16, bfloat16)>& func, NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_INT64)
|
||||
template SD_LIB_HIDDEN void NDArray::applyIndexedPairwiseLambda(
|
||||
NDArray* other, std::function<sd::LongType(sd::LongType, sd::LongType, sd::LongType)>& func, NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_INT32)
|
||||
template SD_LIB_HIDDEN void NDArray::applyIndexedPairwiseLambda(NDArray* other,
|
||||
std::function<int(sd::LongType, int, int)>& func,
|
||||
NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_INT16)
|
||||
template SD_LIB_HIDDEN void NDArray::applyIndexedPairwiseLambda(
|
||||
NDArray* other, std::function<int16_t(sd::LongType, int16_t, int16_t)>& func, NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_UINT8)
|
||||
template SD_LIB_HIDDEN void NDArray::applyIndexedPairwiseLambda(
|
||||
NDArray* other, std::function<uint8_t(sd::LongType, uint8_t, uint8_t)>& func, NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_UINT16)
|
||||
template SD_LIB_HIDDEN void NDArray::applyIndexedPairwiseLambda(
|
||||
NDArray* other, std::function<uint16_t(sd::LongType, uint16_t, uint16_t)>& func, NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_UINT32)
|
||||
template SD_LIB_HIDDEN void NDArray::applyIndexedPairwiseLambda(
|
||||
NDArray* other, std::function<uint32_t(sd::LongType, uint32_t, uint32_t)>& func, NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_UNSIGNEDLONG)
|
||||
template SD_LIB_HIDDEN void NDArray::applyIndexedPairwiseLambda(
|
||||
NDArray* other, std::function<uint64_t(sd::LongType, uint64_t, uint64_t)>& func, NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_INT8)
|
||||
template SD_LIB_HIDDEN void NDArray::applyIndexedPairwiseLambda(
|
||||
NDArray* other, std::function<int8_t(sd::LongType, int8_t, int8_t)>& func, NDArray* target);
|
||||
#endif
|
||||
#if defined(HAS_BOOL)
|
||||
template SD_LIB_HIDDEN void NDArray::applyIndexedPairwiseLambda(
|
||||
NDArray* other, std::function<bool(sd::LongType, bool, bool)>& func, NDArray* target);
|
||||
#endif
|
||||
Reference in New Issue
Block a user