* initial commit Signed-off-by: raver119@gmail.com <raver119@gmail.com> * another initial commit Signed-off-by: raver119@gmail.com <raver119@gmail.com> * another initial commit Signed-off-by: raver119@gmail.com <raver119@gmail.com> * one more initial commit Signed-off-by: raver119@gmail.com <raver119@gmail.com> * next step Signed-off-by: raver119@gmail.com <raver119@gmail.com> * next step Signed-off-by: raver119@gmail.com <raver119@gmail.com> * next step Signed-off-by: raver119@gmail.com <raver119@gmail.com> * next step Signed-off-by: raver119@gmail.com <raver119@gmail.com> * Refactored buffer() and shapeInfo() methods usage with NDArray class. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopt Graph class methods to use const shapes. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopt choose op to use constant shapes. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopt where op shape method to use constant shapes. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopt lstsq op to use constant empty shapes. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopt matrix_diag_part op shape routine to use constant shapes. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopt determinant ops to use constant shapes. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopt mean_pairwssqerr_loss ops to use constant shapes. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopt ops shape methods. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopt shape methods for loss ops. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopt log_loss op shape method. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopt shape methods for ops. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopt dilation2d ops shape methods. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopted deconv2d ops shape methods. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopted dynamicRNN op shape method. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopted shape methods for ops. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopted shape methods for lstm layer ops. Signed-off-by: shugeo <sgazeos@gmail.com> * few updates Signed-off-by: raver119@gmail.com <raver119@gmail.com> * first cuda tweak Signed-off-by: raver119@gmail.com <raver119@gmail.com> * Adopt constant shapes for sconv2d ops. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopt constant shapes for gru ops. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopt constant shapes with shape methods for segment ops and so on. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopted constant shapes with unsorted_segment_* ops. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopted constant shapes with gamma op shape method. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopted shape methods of reduce_stddev ops. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopted shape methods for reduce_* ops. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopt shape method for squeeze op. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopt strided_slice shape method. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored concat op shape method to adopt constant shapes. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopted shape method for mirror_pad op. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopted split op shape method. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopted tile ops shape methods. Signed-off-by: shugeo <sgazeos@gmail.com> * Added const cast for mkldnn routines handles. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored logSoftMaxForVector_ routine to conform with proper data and shape pointer casts. Signed-off-by: shugeo <sgazeos@gmail.com> * Cosmetic changes to proper usage of constant pointers. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored a couple shape comparators for strides and addBias helpers to proper use data pointers with inplace option. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored depthToSpace helpers. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored histogram helpers. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored im2col helpers. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored gather and gatherND helpers. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed buffer usage on percentile helper. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed gather shape with helpers and range buffer usage. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed buffer usage with space to depth helpers. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed buffer usage and constant shapes. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed buffer usage with LUP decomposition> Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored onehot_ helper. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored pad and prefix to use constant shapes. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactoed softmax helpers. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed space to batch helpers to use buffers properly. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed stack and split helpers. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed buffer usage with sparse to dense helpers. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed buffer usage with mindistance_ helpers. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed buffer usage with tile helper. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed constant shape usage. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed constant shape usage with legacy pairwise bool ops. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored a couple of methods to adopt constant shape usage. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed broadcasting with constant shape." Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed const usage with inplace reverse and constant shapes with legacy reduction. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored legacy ops with const shapes. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored sort to adopt constant shapes. Signed-off-by: shugeo <sgazeos@gmail.com> * Corrected sort for constant shape usage. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed constant shape usage with special methods. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored Context to conform with constant shape usage. Signed-off-by: shugeo <sgazeos@gmail.com> * CUDA broadcasting headers Signed-off-by: raver119@gmail.com <raver119@gmail.com> * pairwise/indexreduce/random headers Signed-off-by: raver119@gmail.com <raver119@gmail.com> * Refactored native ops to adopt constant shapes. Signed-off-by: shugeo <sgazeos@gmail.com> * legacy reduce3/scalar headers Signed-off-by: raver119@gmail.com <raver119@gmail.com> * Corrected pullRow signature and tests. Signed-off-by: shugeo <sgazeos@gmail.com> * Corrected routines to proper use of constant shapes. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored tests to use constant shapes properly. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored legacy ops tests to use constant shapes properly. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored buffer usage with NDArray tests. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed native ops tests. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed special concat routine. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed buffer usage with test. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed buffer usage with a test. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored TAD.h and tests. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored calcStrides* routines to use constant shapes. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed miscelaneous errors with constant shapes. Signed-off-by: shugeo <sgazeos@gmail.com> * NativeOps const changes Signed-off-by: raver119@gmail.com <raver119@gmail.com> * Corrected definitions for declared functions. Signed-off-by: shugeo <sgazeos@gmail.com> * NativeOps const changes Signed-off-by: raver119@gmail.com <raver119@gmail.com> * few more const changes Signed-off-by: raver119@gmail.com <raver119@gmail.com> * Fixed const shapes with shape routines. Signed-off-by: shugeo <sgazeos@gmail.com> * few more const changes Signed-off-by: raver119@gmail.com <raver119@gmail.com> * Fixed shape method for broadcastable case. Signed-off-by: shugeo <sgazeos@gmail.com> * few more const changes Signed-off-by: raver119@gmail.com <raver119@gmail.com> * xw_plus_b BP shape fn restored Signed-off-by: raver119@gmail.com <raver119@gmail.com> * Fixed signatures with broadcasting. Signed-off-by: shugeo <sgazeos@gmail.com> * Repaired backprops shape methods for a set of operations. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored broadcast bool for cuda. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored methods for 3 args with const qualifier. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed a couple of kernel signatures for broadcasting. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed kernels signatures for const buffers and shapes. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored pairwise methods to persistent buffers and shapes usage. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopt const to buffers and shapes with kernels. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopt const to buffers and shapes with scalar kernels. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored indexreduce kernels signatures to use const buffers and shapes. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored pairwise kernels to adopt cons shapes and buffers. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored pairwise bool kernels to adopt cons shapes and buffers. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored random special ops to conform with const shapes and buffers. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored native ops to conform with const shapes and buffers under cuda platform. Signed-off-by: shugeo <sgazeos@gmail.com> * Cosmetical changes only. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed const shapes and buffers error. Signed-off-by: shugeo <sgazeos@gmail.com> * Corrected start pos routine. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored methods to conform with const shapes and buffers. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored helpers to use proper methods instead. Signed-off-by: shugeo <sgazeos@gmail.com> * bunch of changes Signed-off-by: raver119@gmail.com <raver119@gmail.com> * next bunch of changes Signed-off-by: raver119@gmail.com <raver119@gmail.com> * next bunch of changes Signed-off-by: raver119@gmail.com <raver119@gmail.com> * Fixed execScalar declaration. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed execScalar declaration. Signed-off-by: shugeo <sgazeos@gmail.com> * Corrected const shape cases with sort and so on. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed const shapes for sort. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored kernel declarations to adopt const shapes. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed kernels declarations to adopt const shapes. Signed-off-by: shugeo <sgazeos@gmail.com> * Corrected kernel declarations to adopt const shapes and buffers. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed kernels declarations to adopt const shapes. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed segment helpers kernels declarations and so on to adopt const shapes. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed const shape usage with segment and solve helpers. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed kernel declaration with adjustWeight helper. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed cuda implementations for constant shape helpers. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopted const shape usage with kernels. Signed-off-by: shugeo <sgazeos@gmail.com> * Adopted top_k kernels to use const shapes and buffers. Signed-off-by: shugeo <sgazeos@gmail.com> * Corrected kernels declarations to adopt const shapes with helpers. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored NDArray definitions to adopt const shapes and buffers. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed const shapes with image suppression helpers. Signed-off-by: shugeo <sgazeos@gmail.com> * Slight improvement with buffers. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored buffer usage. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored buffer usage with tests. Signed-off-by: shugeo <sgazeos@gmail.com> * Fixed const shape usage with definitions. Signed-off-by: shugeo <sgazeos@gmail.com> * minor updates on cpu side Signed-off-by: raver119@gmail.com <raver119@gmail.com> * Refactored const shape usage with ConstantDescritor and native ops with cuda platform. Signed-off-by: shugeo <sgazeos@gmail.com> * Refactored tear and tile kernels to adopt with const shapes. Signed-off-by: shugeo <sgazeos@gmail.com> * softmax_loop fix Signed-off-by: raver119 <raver119@gmail.com> * update missing signature Signed-off-by: raver119@gmail.com <raver119@gmail.com> * softmax again Signed-off-by: raver119@gmail.com <raver119@gmail.com> * few more missing consts Signed-off-by: raver119 <raver119@gmail.com> * new methods updated Signed-off-by: raver119@gmail.com <raver119@gmail.com> Co-authored-by: shugeo <sgazeos@gmail.com>
599 lines
21 KiB
C++
599 lines
21 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 <graph/Context.h>
|
|
#include <helpers/ShapeUtils.h>
|
|
#include <graph/Context.h>
|
|
#include <array/InteropDataBuffer.h>
|
|
|
|
|
|
namespace sd {
|
|
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();
|
|
}
|
|
sd::DataType Context::dataType(int index) {
|
|
|
|
return _dataType;
|
|
}
|
|
|
|
sd::DataType Context::dataType() {
|
|
return dataType(0);
|
|
}
|
|
|
|
void Context::setDataType(int index, sd::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;
|
|
}
|
|
|
|
void Context::setTargetEngine(samediff::Engine engine) {
|
|
_engine = engine;
|
|
}
|
|
|
|
bool Context::hasWorkspaceProvided() {
|
|
return this->_workspace != nullptr;
|
|
}
|
|
|
|
void Context::attachWorkspace(sd::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() {
|
|
auto ie = _fastpath_in.empty();
|
|
auto io = _fastpath_out.empty();
|
|
// two options here.
|
|
// either both IN/OUT are filled
|
|
auto b1 = (!ie && !io) || (!ie && _isInplace);
|
|
|
|
// or at least something is filled, and FastPath is NOT forbidden
|
|
auto b2 = (!ie || !io) && !_forbidFastPath;
|
|
return b1 || b2;
|
|
}
|
|
|
|
void Context::forbidFastPath(bool reallyForbid) {
|
|
_forbidFastPath = reallyForbid;
|
|
}
|
|
|
|
VariableSpace *Context::getVariableSpace() {
|
|
return _variableSpace;
|
|
}
|
|
|
|
sd::memory::Workspace* Context::getWorkspace() {
|
|
return _workspace;
|
|
}
|
|
|
|
sd::memory::Workspace* Context::workspace() {
|
|
return _workspace;
|
|
}
|
|
|
|
sd::random::RandomBuffer* Context::getRNG() {
|
|
return _rng;
|
|
}
|
|
|
|
void Context::setRNG(sd::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 sd::graph::Context::getOuterTime(){
|
|
return this->_executionTime.first;
|
|
}
|
|
|
|
Nd4jLong sd::graph::Context::getInnerTime(){
|
|
return this->_executionTime.second;
|
|
}
|
|
|
|
void sd::graph::Context::setOuterTime(Nd4jLong time){
|
|
this->_executionTime.first = time;
|
|
}
|
|
|
|
void sd::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) {
|
|
try {
|
|
return _variableSpace->getVariable(p);
|
|
} catch (std::exception &e) {
|
|
nd4j_printf("Node %i; Non-existent variable requested: [%i:%i]\n", this->_nodeId, p.first, p.second);
|
|
throw std::runtime_error("Bad variable");
|
|
}
|
|
}
|
|
|
|
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();
|
|
}
|
|
|
|
sd::memory::Workspace *Context::fWorkspace() {
|
|
return workspace();
|
|
}
|
|
|
|
sd::memory::Workspace *Context::tWorkspace() {
|
|
return nullptr;
|
|
}
|
|
|
|
sd::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) {
|
|
this->setInputArray(index, buffer, const_cast<const void*>(shapeInfo), specialBuffer, const_cast<const void *>(specialShapeInfo));
|
|
}
|
|
|
|
void Context::setInputArray(int index, void *buffer, void const* shapeInfo, void *specialBuffer, void const* specialShapeInfo) {
|
|
auto array = new NDArray(buffer, specialBuffer, reinterpret_cast<Nd4jLong const*>(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) {
|
|
this->setOutputArray(index, buffer, const_cast<const void *>(shapeInfo), specialBuffer, const_cast<const void *>(specialShapeInfo));
|
|
}
|
|
|
|
void Context::setOutputArray(int index, void *buffer, const void * shapeInfo, void *specialBuffer, const void * specialShapeInfo) {
|
|
if (_fastpath_out.size() < index + 1)
|
|
_fastpath_out.resize(index+1);
|
|
|
|
auto array = new NDArray(buffer, specialBuffer, reinterpret_cast<Nd4jLong const*>(shapeInfo));
|
|
|
|
_fastpath_out[index] = array;
|
|
_handles.emplace_back(array);
|
|
|
|
if (_context != nullptr)
|
|
array->setContext(_context);
|
|
}
|
|
|
|
void Context::setInputArray(int index, void *vdatabuffer, void const* shapeInfo, void const* 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 const*>(shapeInfo), sd::LaunchContext::defaultContext(), dataBuffer->offset() / DataTypeUtils::sizeOf(ArrayOptions::dataType(reinterpret_cast<Nd4jLong const*>(shapeInfo))));
|
|
else
|
|
array = new NDArray(nullptr, nullptr, reinterpret_cast<Nd4jLong const*>(shapeInfo));
|
|
|
|
_fastpath_in[index] = array;
|
|
_handles.emplace_back(array);
|
|
|
|
if (_context != nullptr)
|
|
array->setContext(_context);
|
|
}
|
|
|
|
void Context::setOutputArray(int index, void *vdatabuffer, void const* shapeInfo, void const* 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 const*>(shapeInfo), sd::LaunchContext::defaultContext(), dataBuffer->offset() / DataTypeUtils::sizeOf(ArrayOptions::dataType(reinterpret_cast<Nd4jLong const*>(shapeInfo))));
|
|
else
|
|
array = new NDArray(nullptr, nullptr, reinterpret_cast<Nd4jLong const*>(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;
|
|
}
|
|
|
|
samediff::ExecutionMode Context::executionMode() {
|
|
return _execMode;
|
|
}
|
|
|
|
void Context::setExecutionMode(samediff::ExecutionMode executionMode) {
|
|
_execMode = executionMode;
|
|
}
|
|
|
|
bool Context::isTraining() {
|
|
return _execMode == samediff::ExecutionMode::MODE_TRAINING;
|
|
}
|
|
|
|
bool Context::isInference() {
|
|
return _execMode == samediff::ExecutionMode::MODE_INFERENCE;
|
|
}
|
|
|
|
void Context::setDArguments(sd::DataType *arguments, int numberOfArguments) {
|
|
_dArgs.clear();
|
|
for (int e = 0; e < numberOfArguments; e++)
|
|
_dArgs.emplace_back(arguments[e]);
|
|
}
|
|
|
|
void Context::setDArguments(const std::vector<sd::DataType> &dArgs) {
|
|
_dArgs.clear();
|
|
for (auto d:dArgs)
|
|
_dArgs.emplace_back(d);
|
|
}
|
|
|
|
void Context::clearFastPath() {
|
|
_fastpath_in.clear();
|
|
_fastpath_out.clear();
|
|
|
|
for (auto v:_handles)
|
|
delete v;
|
|
|
|
_handles.clear();
|
|
}
|
|
}
|
|
}
|
|
|