1#ifndef TMVA_SOFIE_ROPERATOR_Concat
2 #define TMVA_SOFIE_ROPERATOR_Concat
16 namespace Experimental{
34 for (
auto &
name : inputs)
43 std::vector<std::vector<size_t>>
ShapeInference(std::vector<std::vector<size_t>> inputs){
44 std::vector<std::vector<size_t>> ret(1);
49 if (fAxis < 0 || fAxis >= (
int) inputs[0].
size())
50 throw std::runtime_error(
"TMVA SOFIE Concat Op - invalid axis value ");
54 for (
size_t i = 0; i < inputs.size(); i++) {
55 if (i > 0 && inputs[i].
size() != inputs[i - 1].
size())
56 throw std::runtime_error(
"TMVA SOFIE Concat Op - input tensors have different shapes " +
58 for (
size_t iaxis = 0; iaxis < inputs[i].size(); iaxis++) {
59 if ((
int)iaxis ==
fAxis)
60 concat_dim += inputs[i][iaxis];
61 else if (i > 0 && inputs[i][iaxis] != inputs[i - 1][iaxis])
62 throw std::runtime_error(
"TMVA SOFIE Concat Op - input tensors have wrong shapes " +
70 ret[0][
fAxis] = concat_dim;
72 std::vector<int> stack;
74 for(
size_t i = 0; i < inputs.size(); i++) {
75 if (i > 0 && inputs[i].
size() != inputs[i-1].
size() )
76 throw std::runtime_error(
"TMVA SOFIE Concat Op - input tensors have different shapes " +
fInputs[i] +
" : " +
78 for (
size_t iaxis = 0; iaxis < inputs[i].size(); iaxis++) {
79 if ((
int) iaxis ==
fAxis)
80 stack.push_back(inputs[i][iaxis]);
82 if (i> 0 && inputs[i][iaxis] != inputs[i-1][iaxis])
83 throw std::runtime_error(
"TMVA SOFIE Concat Op - input tensors have wrong shapes " +
96 std::vector<std::vector<Dim>>
ShapeInference(
const std::vector<std::vector<Dim>> & inputs){
97 std::vector<std::vector<Dim>> ret(1);
102 if (fAxis < 0 || fAxis >= (
int) inputs[0].
size())
103 throw std::runtime_error(
"TMVA SOFIE Concat Op - invalid axis value ");
107 for (
size_t i = 0; i < inputs.size(); i++) {
108 if (i > 0 && inputs[i].
size() != inputs[i - 1].
size())
109 throw std::runtime_error(
"TMVA SOFIE Concat Op - input tensors have different shapes " +
fInputs[i] +
" : " +
111 for (
size_t iaxis = 0; iaxis < inputs[i].size(); iaxis++) {
112 if ((
int)iaxis ==
fAxis) {
114 if (inputs[i][iaxis].isParam)
115 throw std::runtime_error(
"TMVA SOFIE Concat Op - not supporting input param dimensions for concatenation axis. Input shape is " +
117 concat_dim += inputs[i][iaxis].dim;
120 else if (i > 0 && inputs[i][iaxis].GetVal() != inputs[i - 1][iaxis].GetVal())
121 throw std::runtime_error(
"TMVA SOFIE Concat Op - input tensors have wrong shapes " +
129 ret[0][
fAxis].dim = concat_dim;
136 throw std::runtime_error(
"TMVA SOFIE Concat Op - stacking (i.e. COncatFromSequence with new_axis=1) is not supported ");
145 throw std::runtime_error(
"TMVA SOFIE Concat Op Input Tensor " + it +
" is not found in model");
170 std::copy(inputData, inputData + inputLength, outputData.begin() +
offset );
177 std::cout <<
"output of Concat is a constant tensor " <<
ConvertShapeToString(outputShape) <<
" : "
192 OpName =
"op_"+OpName;
194 throw std::runtime_error(
"TMVA SOFIE Concat called to Generate without being initialized first");
196 std::stringstream out;
197 out<<
"\n//--------- Concat\n";
199 bool hasShapeOnes =
true;
200 for(
int i = 0; i<
fAxis; ++i){
202 hasShapeOnes =
false;
206 if (
fAxis == 0 || hasShapeOnes) {
208 for(
size_t i=0; i<
fInputs.size(); ++i) {
219 std::vector<std::vector<Dim>> inStrides(
fInputs.size());
221 for (
auto &s : inStrides) {
225 for (
int i = 0; i <
fAxis; ++i) {
227 out <<
SP <<
"for (size_t i" << i <<
" = 0; i" << i <<
" < " <<
fOutputShape[i].GetVal() <<
"; ++i" << i <<
") {\n";
230 out <<
SP <<
SP <<
SP <<
"int idxOut = ";
231 for (
int k = 0; k <
fAxis; k++) {
232 if (k > 0) out <<
" + ";
233 out << outStride[k].GetVal() <<
"*i" << k;
237 for (
size_t j = 0; j <
fInputs.size(); j++) {
240 out <<
SP <<
SP <<
SP <<
"int idxIn" << j <<
" = ";
241 for (
int k = 0; k <
fAxis; k++) {
242 if (k > 0) out <<
" + ";
243 out << inStrides[j][k].GetVal() <<
"*i" << k;
247 out <<
SP <<
SP <<
SP <<
SP <<
"tensor_" <<
fOutput <<
"[idxOut+iC] = tensor_" <<
fInputs[j] <<
"[idxIn" << j <<
"+iC];\n";
248 out <<
SP <<
SP <<
SP <<
"}\n";
251 for (
int i = 0; i <
fAxis; ++i) {
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void input
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h offset
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h length
const ETensorType & GetTensorType(std::string name)
void AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector< Dim > dim_shape)
std::vector< Dim > GetDynamicTensorShape(std::string name)
bool CheckIfTensorAlreadyExist(std::string tensor_name)
void AddConstantTensor(std::string tensor_name, ETensorType type, std::vector< std::size_t > shape, std::shared_ptr< void > data)
bool IsInitializedTensor(const std::string &name) const
const std::vector< size_t > & GetTensorShape(std::string name)
std::shared_ptr< void > GetInitializedTensorData(std::string tensor_name)
void SetNotWritableInitializedTensor(const std::string &tensor_name)
std::vector< std::string > fInputs
std::vector< Dim > fOutputShape
std::vector< std::vector< size_t > > ShapeInference(std::vector< std::vector< size_t > > inputs)
std::vector< std::vector< Dim > > fInputShapes
std::vector< ETensorType > TypeInference(std::vector< ETensorType > input)
ROperator_Concat(std::vector< std::string > inputs, int axis, int newAxis, std::string output)
void Initialize(RModel &model)
std::string Generate(std::string OpName)
std::vector< std::vector< Dim > > ShapeInference(const std::vector< std::vector< Dim > > &inputs)
bool fIsOutputConstant
flag to identify if operator has a constant output (no need to generate code)
const std::string SP
space used to correctly indent the generated C++ code
std::string Clean_name(std::string input_tensor_name)
std::vector< size_t > ComputeStrideFromShape(const std::vector< size_t > &shape)
compute stride of a tensor given its shape (assume layout is row-major)
std::string ConvertDynamicShapeToLength(std::vector< Dim > shape)
std::string ConvertValuesToString(size_t n, const T *data)
std::string ConvertShapeToString(std::vector< size_t > shape)
std::string ConvertDynamicShapeToString(std::vector< Dim > shape)
std::vector< size_t > ConvertShapeToInt(std::vector< Dim > shape)
Convert shape based on Dim to integer format.
std::size_t ConvertShapeToLength(std::vector< size_t > shape)
create variable transformations