2019-06-06 14:21:15 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
* Copyright (c) 2015-2018 Skymind, Inc.
|
|
|
|
*
|
|
|
|
* This program and the accompanying materials are made available under the
|
|
|
|
* terms of the Apache License, Version 2.0 which is available at
|
|
|
|
* https://www.apache.org/licenses/LICENSE-2.0.
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
//
|
|
|
|
// @author raver119@gmail.com
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "../ConstantShapeHelper.h"
|
|
|
|
#include <exceptions/cuda_exception.h>
|
|
|
|
#include <ShapeDescriptor.h>
|
|
|
|
#include <ShapeBuilders.h>
|
2019-08-20 17:52:41 +02:00
|
|
|
#include <AffinityManager.h>
|
2019-06-06 14:21:15 +02:00
|
|
|
#include <ConstantHelper.h>
|
|
|
|
|
|
|
|
namespace nd4j {
|
|
|
|
|
|
|
|
ConstantShapeHelper::ConstantShapeHelper() {
|
2019-08-20 17:52:41 +02:00
|
|
|
auto numDevices = AffinityManager::numberOfDevices();
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
_cache.resize(numDevices);
|
|
|
|
for (int e = 0; e < numDevices; e++) {
|
2020-02-24 05:51:01 +01:00
|
|
|
MAP_IMPL<ShapeDescriptor, ConstantDataBuffer> cache;
|
2019-06-06 14:21:15 +02:00
|
|
|
_cache[e] = cache;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ConstantShapeHelper* ConstantShapeHelper::getInstance() {
|
|
|
|
if (!_INSTANCE)
|
|
|
|
_INSTANCE = new ConstantShapeHelper();
|
|
|
|
|
|
|
|
return _INSTANCE;
|
|
|
|
}
|
|
|
|
|
2019-09-03 21:02:02 +02:00
|
|
|
ConstantDataBuffer ConstantShapeHelper::bufferForShapeInfo(nd4j::DataType dataType, char order, const std::vector<Nd4jLong> &shape) {
|
2019-06-06 14:21:15 +02:00
|
|
|
ShapeDescriptor descriptor(dataType, order, shape);
|
|
|
|
return bufferForShapeInfo(descriptor);
|
|
|
|
}
|
|
|
|
|
2019-09-03 21:02:02 +02:00
|
|
|
ConstantDataBuffer ConstantShapeHelper::bufferForShapeInfo(const nd4j::DataType dataType, const char order, const int rank, const Nd4jLong* shape) {
|
2019-06-06 14:21:15 +02:00
|
|
|
ShapeDescriptor descriptor(dataType, order, shape, rank);
|
|
|
|
return bufferForShapeInfo(descriptor);
|
|
|
|
}
|
|
|
|
|
2019-09-03 21:02:02 +02:00
|
|
|
ConstantDataBuffer ConstantShapeHelper::bufferForShapeInfo(const ShapeDescriptor &descriptor) {
|
2019-08-20 17:52:41 +02:00
|
|
|
int deviceId = AffinityManager::currentDeviceId();
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2020-02-07 10:34:55 +01:00
|
|
|
std::lock_guard<std::mutex> lock(_mutex);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
if (_cache[deviceId].count(descriptor) == 0) {
|
|
|
|
auto hPtr = descriptor.toShapeInfo();
|
|
|
|
auto dPtr = ConstantHelper::getInstance()->replicatePointer(hPtr, shape::shapeInfoByteLength(hPtr));
|
|
|
|
ConstantDataBuffer buffer(hPtr, dPtr, shape::shapeInfoLength(hPtr) * sizeof(Nd4jLong), DataType::INT64);
|
|
|
|
ShapeDescriptor descriptor1(descriptor);
|
|
|
|
_cache[deviceId][descriptor1] = buffer;
|
2020-02-07 10:34:55 +01:00
|
|
|
return _cache[deviceId][descriptor1];
|
2019-06-06 14:21:15 +02:00
|
|
|
} else {
|
2020-02-07 10:34:55 +01:00
|
|
|
return _cache[deviceId].at(descriptor);
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-03 21:02:02 +02:00
|
|
|
ConstantDataBuffer ConstantShapeHelper::bufferForShapeInfo(const Nd4jLong *shapeInfo) {
|
2019-06-06 14:21:15 +02:00
|
|
|
ShapeDescriptor descriptor(shapeInfo);
|
|
|
|
return bufferForShapeInfo(descriptor);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ConstantShapeHelper::checkBufferExistenceForShapeInfo(ShapeDescriptor &descriptor) {
|
2019-08-20 17:52:41 +02:00
|
|
|
auto deviceId = AffinityManager::currentDeviceId();
|
2020-02-07 10:34:55 +01:00
|
|
|
std::lock_guard<std::mutex> lock(_mutex);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2020-02-07 10:34:55 +01:00
|
|
|
return _cache[deviceId].count(descriptor) != 0;
|
2019-06-06 14:21:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Nd4jLong* ConstantShapeHelper::createShapeInfo(const nd4j::DataType dataType, const char order, const int rank, const Nd4jLong* shape) {
|
|
|
|
ShapeDescriptor descriptor(dataType, order, shape, rank);
|
|
|
|
return bufferForShapeInfo(descriptor).primaryAsT<Nd4jLong>();
|
|
|
|
}
|
|
|
|
|
2019-10-26 13:14:21 +02:00
|
|
|
Nd4jLong* ConstantShapeHelper::createShapeInfo(const nd4j::DataType dataType, const Nd4jLong* shapeInfo) {
|
|
|
|
return ConstantShapeHelper::createShapeInfo(dataType, shape::order(shapeInfo), shape::rank(shapeInfo), shape::shapeOf(const_cast<Nd4jLong*>(shapeInfo)));
|
|
|
|
}
|
|
|
|
|
2019-06-06 14:21:15 +02:00
|
|
|
Nd4jLong* ConstantShapeHelper::emptyShapeInfo(const nd4j::DataType dataType) {
|
|
|
|
auto descriptor = ShapeDescriptor::emptyDescriptor(dataType);
|
|
|
|
return bufferForShapeInfo(descriptor).primaryAsT<Nd4jLong>();
|
|
|
|
}
|
|
|
|
|
|
|
|
Nd4jLong* ConstantShapeHelper::scalarShapeInfo(const nd4j::DataType dataType) {
|
|
|
|
auto descriptor = ShapeDescriptor::scalarDescriptor(dataType);
|
|
|
|
return bufferForShapeInfo(descriptor).primaryAsT<Nd4jLong>();
|
|
|
|
}
|
|
|
|
|
|
|
|
Nd4jLong* ConstantShapeHelper::vectorShapeInfo(const Nd4jLong length, const nd4j::DataType dataType) {
|
|
|
|
auto descriptor = ShapeDescriptor::vectorDescriptor(length, dataType);
|
|
|
|
return bufferForShapeInfo(descriptor).primaryAsT<Nd4jLong>();
|
|
|
|
}
|
|
|
|
|
|
|
|
Nd4jLong* ConstantShapeHelper::createShapeInfo(const nd4j::DataType dataType, const char order, const std::vector<Nd4jLong> &shape) {
|
|
|
|
ShapeDescriptor descriptor(dataType, order, shape);
|
|
|
|
return bufferForShapeInfo(descriptor).primaryAsT<Nd4jLong>();
|
|
|
|
}
|
|
|
|
|
|
|
|
Nd4jLong* ConstantShapeHelper::createShapeInfo(const ShapeDescriptor &descriptor) {
|
|
|
|
return bufferForShapeInfo(descriptor).primaryAsT<Nd4jLong>();
|
|
|
|
}
|
|
|
|
|
|
|
|
Nd4jLong* ConstantShapeHelper::createFromExisting(Nd4jLong *shapeInfo, bool destroyOriginal) {
|
|
|
|
ShapeDescriptor descriptor(shapeInfo);
|
|
|
|
auto result = createShapeInfo(descriptor);
|
|
|
|
|
|
|
|
if (destroyOriginal)
|
|
|
|
RELEASE(shapeInfo, nullptr);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Nd4jLong* ConstantShapeHelper::createFromExisting(Nd4jLong *shapeInfo, nd4j::memory::Workspace *workspace) {
|
|
|
|
ShapeDescriptor descriptor(shapeInfo);
|
|
|
|
auto result = createShapeInfo(descriptor);
|
|
|
|
|
|
|
|
RELEASE(shapeInfo, workspace);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
nd4j::ConstantShapeHelper* nd4j::ConstantShapeHelper::_INSTANCE = 0;
|
|
|
|
}
|