2021-02-10 14:40:04 +01:00
|
|
|
/*******************************************************************************
|
|
|
|
* Copyright (c) 2015-2018 Skymind, Inc.
|
|
|
|
* Copyright (c) 2019-2020 Konduit K.K.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
******************************************************************************/
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// @author raver119@gmail.com
|
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef LIBND4J_CONTEXT_H
|
|
|
|
#define LIBND4J_CONTEXT_H
|
|
|
|
|
|
|
|
#include <vector>
|
2020-03-02 10:49:41 +01:00
|
|
|
#include <array/NDArray.h>
|
2019-06-06 14:21:15 +02:00
|
|
|
#include <graph/Variable.h>
|
|
|
|
#include <graph/VariableSpace.h>
|
|
|
|
#include <graph/ContextPrototype.h>
|
|
|
|
#include <memory/Workspace.h>
|
2020-01-20 19:32:46 +01:00
|
|
|
#include <execution/Engine.h>
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
// CUDA-specific includes
|
|
|
|
#ifdef __CUDACC__
|
|
|
|
|
|
|
|
#include <cuda.h>
|
|
|
|
#include <cuda_runtime_api.h>
|
|
|
|
#include <cuda_runtime.h>
|
|
|
|
#include <cuda_device_runtime_api.h>
|
|
|
|
#endif
|
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
namespace sd {
|
2019-06-06 14:21:15 +02:00
|
|
|
namespace graph {
|
|
|
|
/**
|
|
|
|
* This class defines input desired for any given node/operation within graph
|
|
|
|
*/
|
2020-03-02 10:49:41 +01:00
|
|
|
class ND4J_EXPORT Context : public sd::graph::ContextPrototype {
|
2019-06-06 14:21:15 +02:00
|
|
|
protected:
|
2020-03-02 10:49:41 +01:00
|
|
|
sd::memory::Workspace* _workspace = nullptr;
|
|
|
|
sd::graph::VariableSpace* _variableSpace = nullptr;
|
2019-06-06 14:21:15 +02:00
|
|
|
std::pair<Nd4jLong, Nd4jLong> _executionTime;
|
2020-03-02 10:49:41 +01:00
|
|
|
sd::random::RandomBuffer* _rng = nullptr;
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
sd::DataType _dataType = sd::DataType::FLOAT32;
|
2019-06-06 14:21:15 +02:00
|
|
|
// branch for divergent_op
|
|
|
|
int _branch = 0;
|
|
|
|
|
|
|
|
// temporary context for standalone ops execution
|
|
|
|
LaunchContext* _context = nullptr;
|
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
std::vector<sd::DataType> _dataTypes;
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2019-11-14 12:35:02 +01:00
|
|
|
// fields for fast execution (out-of-graph ops use)
|
2019-06-06 14:21:15 +02:00
|
|
|
std::vector<NDArray*> _fastpath_in;
|
|
|
|
std::vector<NDArray*> _fastpath_out;
|
|
|
|
std::vector<NDArray*> _handles;
|
2019-11-14 12:35:02 +01:00
|
|
|
|
|
|
|
bool _helpersAllowed = true;
|
2020-01-04 07:06:44 +01:00
|
|
|
|
|
|
|
// in some cases we might be able to skip shape function for validation purposes
|
|
|
|
bool _shapeFunctionOverride = false;
|
2020-02-27 14:10:38 +01:00
|
|
|
|
|
|
|
// special flag used during conversion from Graph exec to FastPath exec
|
|
|
|
bool _forbidFastPath = false;
|
2019-06-06 14:21:15 +02:00
|
|
|
public:
|
|
|
|
Context(ContextPrototype* prototype, VariableSpace* variableSpace);
|
|
|
|
|
|
|
|
explicit Context(int nodeId, VariableSpace *variableSpace = nullptr);
|
|
|
|
Context(int nodeId, VariableSpace *variableSpace, bool isInplace);
|
|
|
|
|
|
|
|
// default destructor
|
|
|
|
~Context();
|
|
|
|
|
|
|
|
// these methods are for execution timing
|
|
|
|
void setOuterTime(Nd4jLong time);
|
|
|
|
void setInnerTime(Nd4jLong time);
|
|
|
|
Nd4jLong getOuterTime();
|
|
|
|
Nd4jLong getInnerTime();
|
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
sd::DataType dataType() override;
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
sd::DataType dataType(int index) override;
|
|
|
|
void setDataType(int index, sd::DataType type) override;
|
2019-06-06 14:21:15 +02:00
|
|
|
// these methods are related to Workspace abstraction
|
|
|
|
bool hasWorkspaceProvided();
|
2020-03-02 10:49:41 +01:00
|
|
|
void attachWorkspace(sd::memory::Workspace* workspace);
|
2019-06-06 14:21:15 +02:00
|
|
|
void forgetWorkspace();
|
|
|
|
|
|
|
|
// these methods return full-time workspace
|
2020-03-02 10:49:41 +01:00
|
|
|
sd::memory::Workspace* getWorkspace();
|
|
|
|
sd::memory::Workspace* workspace();
|
|
|
|
sd::memory::Workspace* fWorkspace();
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
// this method returns workspace for temporary allocations
|
2020-03-02 10:49:41 +01:00
|
|
|
sd::memory::Workspace* tWorkspace();
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
// this method returns workspace for object allocations
|
2020-03-02 10:49:41 +01:00
|
|
|
sd::memory::Workspace* oWorkspace();
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
void setVariableSpace(VariableSpace* variableSpace);
|
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
sd::random::RandomBuffer* getRNG();
|
|
|
|
void setRNG(sd::random::RandomBuffer* rng);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2020-03-09 06:22:49 +01:00
|
|
|
void setTargetEngine(samediff::Engine engine);
|
2020-01-20 19:32:46 +01:00
|
|
|
|
2019-06-06 14:21:15 +02:00
|
|
|
VariableSpace *getVariableSpace();
|
|
|
|
|
|
|
|
LaunchContext* launchContext();
|
|
|
|
|
|
|
|
// these fields define, if we can execute specific node in-place, without generating new array
|
|
|
|
|
|
|
|
|
|
|
|
// these variables are only for Divergent Nodes
|
|
|
|
int getBranch();
|
|
|
|
void setBranch(int branch);
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
Stash* getStash();
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void trackList(NDArrayList* list);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method returns variable for a given input index for this block
|
|
|
|
* @param idx
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
Variable* getVariable(int idx);
|
|
|
|
Variable* variable(int idx);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method is shortcut to getVariable(int idx);
|
|
|
|
*
|
|
|
|
* + it check fastpath for array availability (preferred)
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
NDArray* getNDArray(int idx);
|
|
|
|
NDArray* array(int idx);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method fetches variable from VariableSpace DIRECTLY
|
|
|
|
* @param p
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
Variable* variable(int node, int index);
|
|
|
|
Variable* variable(std::pair<int,int>& p);
|
|
|
|
Variable* variable(std::initializer_list<int> p);
|
|
|
|
|
|
|
|
|
|
|
|
void pushNDArrayToVariableSpace(int nodeId, int index, NDArray* array, bool removable = true);
|
|
|
|
void pushNDArrayToVariableSpace(std::pair<int, int>& pair, NDArray* array, bool removable = true);
|
|
|
|
|
|
|
|
void pushNDArrayListToVariableSpace(int nodeId, int index, NDArrayList* list, bool track = true);
|
|
|
|
void pushNDArrayListToVariableSpace(std::pair<int, int>& pair, NDArrayList* list, bool track = true);
|
|
|
|
|
|
|
|
bool isValueAvailable(int idx = 0);
|
|
|
|
|
|
|
|
Variable* ensureVariable(int idx = 0);
|
|
|
|
|
|
|
|
unsigned long width() override;
|
|
|
|
|
|
|
|
// methods used in java interop
|
|
|
|
/**
|
2020-02-27 14:10:38 +01:00
|
|
|
* This method checks if Context uses fastpath variable access
|
2019-06-06 14:21:15 +02:00
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
bool isFastPath();
|
|
|
|
|
2020-02-27 14:10:38 +01:00
|
|
|
/**
|
|
|
|
* Method allows to forbid FastPath execution
|
|
|
|
* @param reallyForbid
|
|
|
|
*/
|
|
|
|
void forbidFastPath(bool reallyForbid);
|
|
|
|
|
2019-06-06 14:21:15 +02:00
|
|
|
#ifndef __JAVACPP_HACK__
|
|
|
|
std::vector<NDArray*>& fastpath_in();
|
|
|
|
std::vector<NDArray*>& fastpath_out();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void setInputArray(int index, NDArray *array, bool removable = false);
|
2020-05-09 07:06:14 +02:00
|
|
|
void setInputArray(int index, void *buffer, void const* shapeInfo, void *specialBuffer, void const* specialShapeInfo);
|
|
|
|
void setInputArray(int index, void *buffer, void * shapeInfo, void *specialBuffer, void * specialShapeInfo);
|
|
|
|
void setInputArray(int index, void *databuffer, void const* shapeInfo, void const* specialShapeInfo);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
void setOutputArray(int index, NDArray *array, bool removable = false);
|
2020-05-09 07:06:14 +02:00
|
|
|
void setOutputArray(int index, void *buffer, const void * shapeInfo, void *specialBuffer, const void * specialShapeInfo);
|
|
|
|
void setOutputArray(int index, void *buffer, void * shapeInfo, void *specialBuffer, void * specialShapeInfo);
|
|
|
|
void setOutputArray(int index, void *databuffer, void const* shapeInfo, void const* specialShapeInfo);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
void setTArguments(double *arguments, int numberOfArguments);
|
|
|
|
void setIArguments(Nd4jLong *arguments, int numberOfArguments);
|
|
|
|
void setBArguments(bool *arguments, int numberOfArguments);
|
2020-03-02 10:49:41 +01:00
|
|
|
void setDArguments(sd::DataType *arguments, int numberOfArguments);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2019-11-15 15:04:29 +01:00
|
|
|
void setTArguments(const std::vector<double> &tArgs);
|
|
|
|
void setIArguments(const std::vector<Nd4jLong> &tArgs);
|
|
|
|
void setBArguments(const std::vector<bool> &tArgs);
|
2020-03-02 10:49:41 +01:00
|
|
|
void setDArguments(const std::vector<sd::DataType> &dArgs);
|
2019-11-15 15:04:29 +01:00
|
|
|
|
2020-02-05 05:27:24 +01:00
|
|
|
/**
|
|
|
|
* This method purges fastpath in/out contents and releases all the handles.
|
|
|
|
*
|
|
|
|
* PLEASE NOTE: I/T/B/D args will stay intact
|
|
|
|
*/
|
|
|
|
void clearFastPath();
|
|
|
|
|
2019-06-06 14:21:15 +02:00
|
|
|
void setCudaContext(Nd4jPointer cudaStream, Nd4jPointer reductionPointer, Nd4jPointer allocationPointer);
|
2019-11-14 12:35:02 +01:00
|
|
|
|
|
|
|
void allowHelpers(bool reallyAllow);
|
|
|
|
bool helpersAllowed();
|
2020-01-04 07:06:44 +01:00
|
|
|
|
|
|
|
void setShapeFunctionOverride(bool reallyOverride);
|
|
|
|
bool shapeFunctionOverride();
|
2020-01-27 08:00:07 +01:00
|
|
|
|
2020-03-09 06:22:49 +01:00
|
|
|
samediff::ExecutionMode executionMode();
|
|
|
|
void setExecutionMode(samediff::ExecutionMode executionMode);
|
2020-01-27 08:00:07 +01:00
|
|
|
|
|
|
|
bool isTraining();
|
|
|
|
bool isInference();
|
2019-06-06 14:21:15 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif //LIBND4J_BLOCK_H
|