raver119 29e8e09db6
String changes (#3)
* initial commit

* additional data types & tensor type

Signed-off-by: raver119 <raver119@gmail.com>

* next step

Signed-off-by: raver119 <raver119@gmail.com>

* missing include

* sparse_to_dense

Signed-off-by: raver119 <raver119@gmail.com>

* few more tests files

Signed-off-by: raver119 <raver119@gmail.com>

* draft

Signed-off-by: raver119 <raver119@gmail.com>

* numeric sparse_to_dense

Signed-off-by: raver119 <raver119@gmail.com>

* comment

Signed-off-by: raver119 <raver119@gmail.com>

* string sparse_to_dense version

Signed-off-by: raver119 <raver119@gmail.com>

* CUDA DataBuffer expand

Signed-off-by: raver119 <raver119@gmail.com>

* few tweaks for CUDA build

Signed-off-by: raver119 <raver119@gmail.com>

* shape fn for string_split

Signed-off-by: raver119 <raver119@gmail.com>

* one more comment

Signed-off-by: raver119 <raver119@gmail.com>

* string_split indices

Signed-off-by: raver119 <raver119@gmail.com>

* next step

Signed-off-by: raver119 <raver119@gmail.com>

* test passes

Signed-off-by: raver119 <raver119@gmail.com>

* few rearrangements for databuffer implementations

Signed-off-by: raver119 <raver119@gmail.com>

* DataBuffer: move inline methods to common implementations

Signed-off-by: raver119 <raver119@gmail.com>

* add native DataBuffer to Nd4j presets

Signed-off-by: raver119 <raver119@gmail.com>

* DataBuffer creation

Signed-off-by: raver119 <raver119@gmail.com>

* use DataBuffer for allocation

Signed-off-by: raver119 <raver119@gmail.com>

* cpu databuffer as deallocatable

Signed-off-by: raver119 <raver119@gmail.com>

* DataBuffer setters for bufers

Signed-off-by: raver119 <raver119@gmail.com>

* couple of wrappers

Signed-off-by: raver119 <raver119@gmail.com>

* DataBuffers being passed around

Signed-off-by: raver119 <raver119@gmail.com>

* Bunch of ByteBuffer-related signatures gone

Signed-off-by: raver119 <raver119@gmail.com>

* - few more Nd4j signatures removed
- minor fix for bfloat16

Signed-off-by: raver119 <raver119@gmail.com>

* nullptr pointer is still a pointer, but 0 as address :)

Signed-off-by: raver119 <raver119@gmail.com>

* one special test

Signed-off-by: raver119 <raver119@gmail.com>

* empty string array init

Signed-off-by: raver119 <raver119@gmail.com>

* one more test in cpp

Signed-off-by: raver119 <raver119@gmail.com>

* memcpy instead of databuffer swap

Signed-off-by: raver119 <raver119@gmail.com>

* special InteropDataBuffer for front-end languages

Signed-off-by: raver119 <raver119@gmail.com>

* few tweaks for java

Signed-off-by: raver119 <raver119@gmail.com>

* pointer/indexer actualization

Signed-off-by: raver119 <raver119@gmail.com>

* CustomOp returns list for inputArumgents and outputArguments instead of array

Signed-off-by: raver119 <raver119@gmail.com>

* redundant call

Signed-off-by: raver119 <raver119@gmail.com>

* print_variable op

Signed-off-by: raver119 <raver119@gmail.com>

* - view handling (but wrong one)
- print_variable java wrapper

Signed-off-by: raver119 <raver119@gmail.com>

* one more test

Signed-off-by: raver119 <raver119@gmail.com>

* - empty arrays handling

Signed-off-by: raver119 <raver119@gmail.com>

* - deserialization works now

Signed-off-by: raver119 <raver119@gmail.com>

* minor fix

Signed-off-by: raver119 <raver119@gmail.com>

* meh

Signed-off-by: raver119 <raver119@gmail.com>

* one more fix

Signed-off-by: raver119 <raver119@gmail.com>

* initial cuda commit

Signed-off-by: raver119 <raver119@gmail.com>

* print_variable message validation

Signed-off-by: raver119 <raver119@gmail.com>

* CUDA views

Signed-off-by: raver119 <raver119@gmail.com>

* CUDA special buffer size

Signed-off-by: raver119 <raver119@gmail.com>

* minor update to match master changes

Signed-off-by: raver119 <raver119@gmail.com>

* - consider arrays always actual on device for CUDA
- additional PrintVariable constructor
- CudaUtf8Buffer now allocates host buffer by default

Signed-off-by: raver119 <raver119@gmail.com>

* meh

Signed-off-by: raver119 <raver119@gmail.com>

* - print_variable now allows print from device

Signed-off-by: raver119 <raver119@gmail.com>

* InteropDataBuffer data type fix

Signed-off-by: raver119 <raver119@gmail.com>

* ...

Signed-off-by: raver119 <raver119@gmail.com>

* disable some debug messages

Signed-off-by: raver119 <raver119@gmail.com>

* master pulled in

Signed-off-by: raver119 <raver119@gmail.com>

* couple of new methods for DataBuffer interop

Signed-off-by: raver119 <raver119@gmail.com>

* java side

Signed-off-by: raver119 <raver119@gmail.com>

* offsetted constructor

Signed-off-by: raver119 <raver119@gmail.com>

* new CUDA deallocator

Signed-off-by: raver119 <raver119@gmail.com>

* CUDA backend torn apart

Signed-off-by: raver119 <raver119@gmail.com>

* CUDA backend torn apart 2

Signed-off-by: raver119 <raver119@gmail.com>

* CUDA backend torn apart 3

Signed-off-by: raver119 <raver119@gmail.com>

* - few new tests
- few new methods for DataBuffer management

Signed-off-by: raver119 <raver119@gmail.com>

* few more tests + few more tweaks

Signed-off-by: raver119 <raver119@gmail.com>

* two failing tests

Signed-off-by: raver119 <raver119@gmail.com>

* one more test

Signed-off-by: raver119 <raver119@gmail.com>

* two failing tests pass

Signed-off-by: raver119 <raver119@gmail.com>

* now we pass DataBuffer to legacy ops too

Signed-off-by: raver119 <raver119@gmail.com>

* Native DataBuffer for legacy ops, Java side

Signed-off-by: raver119 <raver119@gmail.com>

* CPU java side update

Signed-off-by: raver119 <raver119@gmail.com>

* CUDA java side update

Signed-off-by: raver119 <raver119@gmail.com>

* no more prepare/register action on java side

Signed-off-by: raver119 <raver119@gmail.com>

* NDArray::prepare/register use now accepts vectors

Signed-off-by: raver119 <raver119@gmail.com>

* InteropDataBuffer now has few more convenience methods

Signed-off-by: raver119 <raver119@gmail.com>

* java bindings update

Signed-off-by: raver119 <raver119@gmail.com>

* tick device in NativeOps

Signed-off-by: raver119 <raver119@gmail.com>

* Corrected usage of OpaqueBuffer for tests.

* Corrected usage of OpaqueBuffer for java tests.

* NativeOpsTests fixes.

* print_variable now returns scalar

Signed-off-by: raver119 <raver119@gmail.com>

* one more test

Signed-off-by: raver119 <raver119@gmail.com>

* compat_string_split fix for CUDA

Signed-off-by: raver119 <raver119@gmail.com>

* - CUDA execScalar fix
- CUDA lazyAllocateHostPointer now checks java indexer/pointer instead of native pointer

Signed-off-by: raver119 <raver119@gmail.com>

* legacy ops DataBuffer migration prototype

Signed-off-by: raver119 <raver119@gmail.com>

* ignore device shapeinfo coming from java

Signed-off-by: raver119 <raver119@gmail.com>

* minor fix

Signed-off-by: raver119 <raver119@gmail.com>

* minor transformAny fix

Signed-off-by: raver119 <raver119@gmail.com>

* minor tweak for lazy host allocation

Signed-off-by: raver119 <raver119@gmail.com>

* - DataBuffer::memcpy method
- bitcast now uses memcpy

Signed-off-by: raver119 <raver119@gmail.com>

* - IndexReduce CUDA dimension buffer fix

Signed-off-by: raver119 <raver119@gmail.com>

* views for CPU and CUDA

Signed-off-by: raver119 <raver119@gmail.com>

* less spam

Signed-off-by: raver119 <raver119@gmail.com>

* optional memory init

Signed-off-by: raver119 <raver119@gmail.com>

* async memset

Signed-off-by: raver119 <raver119@gmail.com>

* - SummaryStats CUDA fix
- DataBuffer.sameUnderlyingData() impl
- execBroadcast fix

Signed-off-by: raver119 <raver119@gmail.com>

* - reduce3All fix
switch to CUDA 10 temporarily

Signed-off-by: raver119 <raver119@gmail.com>

* CUDA version

Signed-off-by: raver119 <raver119@gmail.com>

* proper memory deallocator registration

Signed-off-by: raver119 <raver119@gmail.com>

* HOST_ONLY workspace allocation

Signed-off-by: raver119 <raver119@gmail.com>

* temp commit

Signed-off-by: raver119 <raver119@gmail.com>

* few conflicts resolved

Signed-off-by: raver119 <raver119@gmail.com>

* few minor fixes

Signed-off-by: raver119 <raver119@gmail.com>

* one more minor fix

Signed-off-by: raver119 <raver119@gmail.com>

* NDArray permute should operate on JVM primitives

Signed-off-by: raver119 <raver119@gmail.com>

* - create InteropDataBuffer for shapes as well
- update pointers after view creation in Java

Signed-off-by: raver119 <raver119@gmail.com>

* - addressPointer temporary moved to C++

Signed-off-by: raver119 <raver119@gmail.com>

* CUDA: don't account offset twice

Signed-off-by: raver119 <raver119@gmail.com>

* CUDA: DataBuffer pointer constructor updated

Signed-off-by: raver119 <raver119@gmail.com>

* CUDA NDArray.unsafeDuplication() simplified

Signed-off-by: raver119 <raver119@gmail.com>

* CUDA minor workspace-related fixes

Signed-off-by: raver119 <raver119@gmail.com>

* CPU DataBuffer.reallocate()

Signed-off-by: raver119 <raver119@gmail.com>

* print_affinity op

Signed-off-by: raver119 <raver119@gmail.com>

* print_affinity java side

Signed-off-by: raver119 <raver119@gmail.com>

* CUDA more tweaks for data locality

Signed-off-by: raver119 <raver119@gmail.com>

* - compat_string_split tweak
- CudaUtf8Buffer update

Signed-off-by: raver119 <raver119@gmail.com>

* INDArray.close() mechanic restored

Signed-off-by: raver119 <raver119@gmail.com>

* one more test fixed

Signed-off-by: raver119 <raver119@gmail.com>

* - CUDA DataBuffer.reallocate() updated
- cudaMemcpy (synchronous) restored

Signed-off-by: raver119 <raver119@gmail.com>

* one last fix

Signed-off-by: raver119 <raver119@gmail.com>

* bad import removed

Signed-off-by: raver119 <raver119@gmail.com>

* another small fix

Signed-off-by: raver119 <raver119@gmail.com>

* one special test

Signed-off-by: raver119 <raver119@gmail.com>

* fix bad databuffer size

Signed-off-by: raver119 <raver119@gmail.com>

* release primaryBuffer on replace

Signed-off-by: raver119 <raver119@gmail.com>

* higher timeout

Signed-off-by: raver119 <raver119@gmail.com>

* disable timeouts

Signed-off-by: raver119 <raver119@gmail.com>

* dbCreateView now validates offset and length of a view

Signed-off-by: raver119 <raver119@gmail.com>

* additional validation for dbExpand

Signed-off-by: raver119 <raver119@gmail.com>

* restore timeout back again

Signed-off-by: raver119 <raver119@gmail.com>

* smaller distribution for rng test to prevent timeouts

Signed-off-by: raver119 <raver119@gmail.com>

* CUDA DataBuffer::memcpy now copies to device all the time

Signed-off-by: raver119 <raver119@gmail.com>

* OpaqueDataBuffer now contains all required methods for interop

Signed-off-by: raver119 <raver119@gmail.com>

* some javadoc

Signed-off-by: raver119 <raver119@gmail.com>

* GC on failed allocations

Signed-off-by: raver119 <raver119@gmail.com>

* minoe memcpu tweak

Signed-off-by: raver119 <raver119@gmail.com>

* one more bitcast test

Signed-off-by: raver119 <raver119@gmail.com>

* - NDArray::deviceId() propagation
- special multi-threaded test for data locality checks

Signed-off-by: raver119 <raver119@gmail.com>

* DataBuffer additional syncStream

Signed-off-by: raver119 <raver119@gmail.com>

* DataBuffer additional syncStream

Signed-off-by: raver119 <raver119@gmail.com>

* one ignored test

Signed-off-by: raver119 <raver119@gmail.com>

* skip host alloc for empty arrays

Signed-off-by: raver119 <raver119@gmail.com>

* ByteBuffer support is back

Signed-off-by: raver119 <raver119@gmail.com>

* DataBuffer::memcpy minor fix

Signed-off-by: raver119 <raver119@gmail.com>

* few minor prelu/bp tweaks

Signed-off-by: raver119 <raver119@gmail.com>

* nullify-related fixes

Signed-off-by: raver119 <raver119@gmail.com>

* PReLU fixes (#157)

Signed-off-by: Alex Black <blacka101@gmail.com>

* Build fixed

* Fix tests

* one more ByteBuffer signature restored

Signed-off-by: raver119 <raver119@gmail.com>

* nd4j-jdbc-hsql profiles fix

Signed-off-by: raver119 <raver119@gmail.com>

* nd4j-jdbc-hsql profiles fix

Signed-off-by: raver119 <raver119@gmail.com>

* PReLU weight init fix

Signed-off-by: Alex Black <blacka101@gmail.com>

* Small PReLU fix

Signed-off-by: Alex Black <blacka101@gmail.com>

* - INDArray.migrate() reactivated
- DataBuffer::setDeviceId(...) added
- InteropDataBuffer Z syncToDevice added for views

Signed-off-by: raver119 <raver119@gmail.com>

* missed file

Signed-off-by: raver119 <raver119@gmail.com>

* Small tweak

Signed-off-by: Alex Black <blacka101@gmail.com>

* cuda 10.2

Signed-off-by: raver119 <raver119@gmail.com>

* minor fix

Signed-off-by: raver119 <raver119@gmail.com>

Co-authored-by: shugeo <sgazeos@gmail.com>
Co-authored-by: Alex Black <blacka101@gmail.com>
Co-authored-by: Alexander Stoyakin <alexander.stoyakin@gmail.com>
2020-01-04 13:27:50 +03:00

537 lines
18 KiB
C++

/*******************************************************************************
* 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 <Context.h>
#include <helpers/ShapeUtils.h>
#include <graph/Context.h>
#include <array/InteropDataBuffer.h>
namespace nd4j {
namespace graph {
Context::Context(ContextPrototype* prototype, VariableSpace* variableSpace) {
_variableSpace = variableSpace;
_dataType = prototype->dataType();
if (prototype != nullptr) {
for (const auto &v: *(prototype->inputs())) {
this->_inputs.push_back(v);
}
for (const auto &v: *(prototype->getTArguments())) {
this->_tArgs.push_back(v);
}
for (const auto &v: *(prototype->getIArguments())) {
this->_iArgs.push_back(v);
}
for (const auto &v: *(prototype->getBArguments())) {
this->_bArgs.push_back(v);
}
for (const auto &v: *(prototype->getAxis())) {
this->_axis.push_back(v);
}
this->_opNum = prototype->opNum();
this->_isInplace = prototype->isInplace();
this->_nodeId = prototype->nodeId();
this->_useMKLDNN = prototype->isUseMKLDNN();
}
if (variableSpace != nullptr && variableSpace->launchContext()->getWorkspace() != nullptr)
this->_workspace = variableSpace->launchContext()->getWorkspace();
}
nd4j::DataType Context::dataType(int index) {
return _dataType;
}
nd4j::DataType Context::dataType() {
return dataType(0);
}
void Context::setDataType(int index, nd4j::DataType type) {
if (this->_dataTypes.size() > (size_t)index)
_dataTypes[index] = type;
_dataType = type;
}
Context::Context(int nodeId, VariableSpace *variableSpace) {
this->_nodeId = nodeId;
this->_variableSpace = variableSpace;
this->_isInplace = false;
this->_workspace = nullptr;
this->_executionTime.first = 0;
this->_executionTime.second = 0;
if (variableSpace != nullptr && variableSpace->launchContext()->getWorkspace() != nullptr)
this->_workspace = variableSpace->launchContext()->getWorkspace();
}
Context::Context(int nodeId, VariableSpace *variableSpace, bool isInplace) : Context(nodeId, variableSpace) {
this->_isInplace = isInplace;
}
Context::~Context() {
this->_iArgs.clear();
this->_tArgs.clear();
this->_inputs.clear();
this->_fastpath_in.clear();
this->_fastpath_out.clear();
for (auto v:_handles)
delete v;
if (_context != nullptr)
delete _context;
}
bool Context::hasWorkspaceProvided() {
return this->_workspace != nullptr;
}
void Context::attachWorkspace(nd4j::memory::Workspace* workspace) {
this->_workspace = workspace;
}
void Context::setVariableSpace(VariableSpace *variableSpace) {
this->_variableSpace = variableSpace;
}
void Context::forgetWorkspace() {
_workspace = nullptr;
}
std::vector<NDArray*>& Context::fastpath_in() {
return _fastpath_in;
}
std::vector<NDArray*>& Context::fastpath_out() {
return _fastpath_out;
}
bool Context::isFastPath() {
return !(_fastpath_in.empty() && _fastpath_out.empty());
}
VariableSpace *Context::getVariableSpace() {
return _variableSpace;
}
nd4j::memory::Workspace* Context::getWorkspace() {
return _workspace;
}
nd4j::memory::Workspace* Context::workspace() {
return _workspace;
}
nd4j::random::RandomBuffer* Context::getRNG() {
return _rng;
}
void Context::setRNG(nd4j::random::RandomBuffer* rng) {
_rng = rng;
}
/**
* This method returns variableSpace used in this block
* @return
*/
/*
VariableSpace* Context::getVariableSpace() {
return _variableSpace;
}
*/
Stash* Context::getStash() {
return _variableSpace->getStash();
}
void Context::trackList(NDArrayList* list) {
_variableSpace->trackList(list);
}
/*
void Block::updateVariables() {
_variables.clear();
auto x = _inputs.size();
for (auto &v:_inputs) {
auto var = _variableSpace->getVariable(v);
_variables.emplace_back(var);
}
}
*/
int Context::getBranch() {
return _variableSpace->flowPath()->branch(this->nodeId());
}
void Context::setBranch(int branch) {
//_branch = branch;
if (_variableSpace->flowPath() != nullptr)
_variableSpace->flowPath()->markBranch(this->nodeId(), branch);
}
Nd4jLong nd4j::graph::Context::getOuterTime(){
return this->_executionTime.first;
}
Nd4jLong nd4j::graph::Context::getInnerTime(){
return this->_executionTime.second;
}
void nd4j::graph::Context::setOuterTime(Nd4jLong time){
this->_executionTime.first = time;
}
void nd4j::graph::Context::setInnerTime(Nd4jLong time){
this->_executionTime.second = time;
}
Variable* Context::getVariable(int idx) {
if (idx >= this->_inputs.size()) {
nd4j_printf("Node %i; Variable [%i] requested, but only %i inputs available\n", this->_nodeId, idx, this->_inputs.size());
throw std::runtime_error("Context: bad Variable index");
}
auto p = this->_inputs[idx];
auto v = variable(p);
if (Environment::getInstance()->isDebugAndVerbose() && v != nullptr && v->getNDArray() != nullptr) {
auto array = v->getNDArray();
std::string shape_ = ShapeUtils::shapeAsString(array);
auto type = DataTypeUtils::asString(array->dataType());
float m = std::numeric_limits<float>::quiet_NaN();
if (!array->isEmpty()) {
auto values = array->asIndexedString(16);
nd4j_printf("Debug info for node_%i input[%i]; shape: %s; ews: [%i]; order: [%i]; dtype: [%s]; first values: %s\n", this->_nodeId, idx, shape_.c_str(), array->ews(), array->ordering(), type.c_str(), values.c_str());
} else {
nd4j_printf("Debug info for node_%i input[%i]; shape: %s; ews: [%i]; order: [%i]; dtype: [%s]; mean value: [%f]\n", this->_nodeId, idx, shape_.c_str(), array->ews(), array->ordering(), type.c_str(), m);
}
}
return v;
}
Variable* Context::variable(int idx) {
return getVariable(idx);
}
Variable* Context::variable(std::initializer_list<int> p) {
if (p.size() != 2)
throw std::runtime_error("Variable address should have size of 2");
// FIXME: lol
std::vector<int> vec(p);
std::pair<int, int> pair(vec[0], vec[1]);
return variable(pair);
}
Variable* Context::variable(int node, int idx) {
std::pair<int, int> pair(node, idx);
return variable(pair);
}
Variable* Context::variable(std::pair<int,int>& p) {
if (!_variableSpace->hasVariable(p)) {
nd4j_printf("Node %i; Non-existent variable requested: [%i:%i]\n", this->_nodeId, p.first, p.second);
throw std::runtime_error("Bad variable");
}
return _variableSpace->getVariable(p);
}
void Context::pushNDArrayToVariableSpace(int nodeId, int index, NDArray *array, bool removable) {
std::pair<int,int> pair(nodeId, index);
pushNDArrayToVariableSpace(pair, array, removable);
}
void Context::pushNDArrayToVariableSpace(std::pair<int, int> &pair, NDArray *array, bool removable) {
if (_variableSpace != nullptr) {
if (!_variableSpace->hasVariable(pair)) {
auto var = new Variable(array, nullptr, pair.first, pair.second);
_variableSpace->putVariable(pair, var);
var->markRemovable(removable);
} else {
auto var = _variableSpace->getVariable(pair);
if (var->hasNDArray()) {
if (var->getNDArray() != array) {
if (var->isRemovable() && var->hasNDArray())
delete var->getNDArray();
var->setNDArray(array);
var->markRemovable(removable);
}
} else {
var->setNDArray(array);
var->markRemovable(removable);
}
}
}
}
void Context::pushNDArrayListToVariableSpace(int nodeId, int index, NDArrayList* list, bool track) {
std::pair<int,int> pair(nodeId, index);
pushNDArrayListToVariableSpace(pair, list, track);
}
void Context::pushNDArrayListToVariableSpace(std::pair<int, int>& pair, NDArrayList* list, bool track) {
if (!_variableSpace->hasVariable(pair)) {
auto var = new Variable(nullptr, nullptr, pair.first, pair.second);
var->setNDArrayList(list);
_variableSpace->putVariable(pair, var);
} else {
auto var = _variableSpace->getVariable(pair);
var->setNDArrayList(list);
}
if (track)
_variableSpace->trackList(list);
}
Variable* Context::ensureVariable(int idx) {
std::pair<int, int> pair(this->nodeId(), idx);
if (_variableSpace == nullptr)
throw std::runtime_error("Context::ensureVariable VariableSpace is NULL!");
if (!_variableSpace->hasVariable(pair)) {
auto var = new Variable(nullptr, nullptr, this->nodeId(), idx);
_variableSpace->putVariable(pair, var);
return var;
} else {
return _variableSpace->getVariable(pair);
}
}
bool Context::isValueAvailable(int idx) {
auto var = ensureVariable(idx);
if (var->variableType() == VariableType::NDARRAY) {
return var->hasNDArray();
} else if (var->variableType() == VariableType::ARRAY_LIST) {
return var->hasNDArrayList();
}
return false;
}
NDArray* Context::getNDArray(int idx) {
return array(idx);
}
NDArray* Context::array(int idx) {
// we check for fastpath first
if (!_fastpath_in.empty() && _fastpath_in.size() > idx) {
return _fastpath_in[idx];
}
// if no luck for fastpath - return whatever is available
return getVariable(idx)->getNDArray();
}
nd4j::memory::Workspace *Context::fWorkspace() {
return workspace();
}
nd4j::memory::Workspace *Context::tWorkspace() {
return nullptr;
}
nd4j::memory::Workspace *Context::oWorkspace() {
return nullptr;
}
LaunchContext* Context::launchContext() {
//FIXME: we need proper context to be shared here
if (_context == nullptr) {
return LaunchContext::defaultContext();
} else {
return _context;
}
}
unsigned long Context::width() {
if (!_fastpath_in.empty())
return _fastpath_in.size();
else
return _inputs.size();
}
void Context::setInputArray(int index, NDArray *array, bool removable) {
if (_fastpath_in.size() < index + 1)
_fastpath_in.resize(index+1);
_fastpath_in[index] = array;
if (removable)
_handles.emplace_back(array);
}
void Context::setInputArray(int index, void *buffer, void *shapeInfo, void *specialBuffer, void *specialShapeInfo) {
auto array = new NDArray(buffer, specialBuffer, reinterpret_cast<Nd4jLong *>(shapeInfo));
if (_fastpath_in.size() < index + 1)
_fastpath_in.resize(index+1);
_fastpath_in[index] = array;
_handles.emplace_back(array);
if (_context != nullptr)
array->setContext(_context);
}
void Context::setOutputArray(int index, NDArray *array, bool removable) {
if (_fastpath_out.size() < index + 1)
_fastpath_out.resize(index+1);
_fastpath_out[index] = array;
if (removable)
_handles.emplace_back(array);
}
void Context::setOutputArray(int index, void *buffer, void *shapeInfo, void *specialBuffer, void *specialShapeInfo) {
if (_fastpath_out.size() < index + 1)
_fastpath_out.resize(index+1);
auto array = new NDArray(buffer, specialBuffer, reinterpret_cast<Nd4jLong *>(shapeInfo));
_fastpath_out[index] = array;
_handles.emplace_back(array);
if (_context != nullptr)
array->setContext(_context);
}
void Context::setInputArray(int index, void *vdatabuffer, void *shapeInfo, void *specialShapeInfo) {
auto dataBuffer = reinterpret_cast<InteropDataBuffer*>(vdatabuffer);
if (_fastpath_in.size() < index + 1)
_fastpath_in.resize(index+1);
NDArray *array;
if (dataBuffer != nullptr)
array = new NDArray(dataBuffer->dataBuffer(), reinterpret_cast<Nd4jLong *>(shapeInfo), nd4j::LaunchContext::defaultContext(), dataBuffer->offset() / DataTypeUtils::sizeOf(ArrayOptions::dataType(reinterpret_cast<Nd4jLong *>(shapeInfo))));
else
array = new NDArray(nullptr, nullptr, reinterpret_cast<Nd4jLong *>(shapeInfo));
_fastpath_in[index] = array;
_handles.emplace_back(array);
if (_context != nullptr)
array->setContext(_context);
}
void Context::setOutputArray(int index, void *vdatabuffer, void *shapeInfo, void *specialShapeInfo) {
auto dataBuffer = reinterpret_cast<InteropDataBuffer*>(vdatabuffer);
if (_fastpath_out.size() < index + 1)
_fastpath_out.resize(index+1);
NDArray *array;
if (dataBuffer != nullptr)
array = new NDArray(dataBuffer->dataBuffer(), reinterpret_cast<Nd4jLong *>(shapeInfo), nd4j::LaunchContext::defaultContext(), dataBuffer->offset() / DataTypeUtils::sizeOf(ArrayOptions::dataType(reinterpret_cast<Nd4jLong *>(shapeInfo))));
else
array = new NDArray(nullptr, nullptr, reinterpret_cast<Nd4jLong *>(shapeInfo));
_fastpath_out[index] = array;
_handles.emplace_back(array);
if (_context != nullptr)
array->setContext(_context);
}
void Context::setTArguments(double *arguments, int numberOfArguments) {
_tArgs.clear();
_tArgs.reserve(numberOfArguments);
for (int e = 0; e < numberOfArguments; e++)
_tArgs.push_back(arguments[e]);
}
void Context::setIArguments(Nd4jLong *arguments, int numberOfArguments) {
_iArgs.clear();
_iArgs.reserve(numberOfArguments);
for (int e = 0; e < numberOfArguments; e++)
_iArgs.push_back(arguments[e]);
}
void Context::setBArguments(bool *arguments, int numberOfArguments) {
_bArgs.clear();
_bArgs.reserve(numberOfArguments);
for (int e = 0; e < numberOfArguments; e++)
_bArgs.push_back(arguments[e]);
}
void Context::setCudaContext(Nd4jPointer cudaStream, Nd4jPointer reductionPointer, Nd4jPointer allocationPointer) {
#ifdef __CUDABLAS__
_context = new LaunchContext(cudaStream, reductionPointer, allocationPointer);
// FIXME: either pass handle from outside, or make sure outside we use the same handle
_context->setCublasHandle(LaunchContext::defaultContext()->getCublasHandle());
for (auto v: _fastpath_out)
v->setContext(_context);
for (auto v: _fastpath_in)
v->setContext(_context);
#endif
}
void Context::allowHelpers(bool reallyAllow) {
_helpersAllowed = reallyAllow;
}
bool Context::helpersAllowed() {
return _helpersAllowed;
}
void Context::setTArguments(const std::vector<double> &tArgs) {
for (auto t:tArgs)
_tArgs.emplace_back(t);
}
void Context::setIArguments(const std::vector<Nd4jLong> &iArgs) {
for (auto i:iArgs)
_iArgs.emplace_back(i);
}
void Context::setBArguments(const std::vector<bool> &bArgs) {
for (auto b:bArgs)
_bArgs.push_back(b);
}
void Context::setShapeFunctionOverride(bool reallyOverride) {
_shapeFunctionOverride = reallyOverride;
}
bool Context::shapeFunctionOverride() {
return _shapeFunctionOverride;
}
}
}