1#ifndef TMVA_SOFIE_ROPERATOR_TRANSPOSE
2#define TMVA_SOFIE_ROPERATOR_TRANSPOSE
12namespace Experimental{
33 fAttrPerm(attr_perm),
fNX(UTILITY::Clean_name(nameData)),
fNY(UTILITY::Clean_name(nameOutput)) {
50 auto inputData =
static_cast<T *
>(model.GetInitializedTensorData(
fNX).get());
52 std::vector<size_t> outputIdx(dim);
53 std::vector<T> outputData(
length);
54 for (
size_t i = 0; i <
length; i++) {
55 outputIdx[0] = i / outStrides[0];
56 for (
size_t j = 1; j < dim; j++) {
57 outputIdx[j] = (i % outStrides[j - 1]) / outStrides[j];
60 size_t inputIndex = 0;
61 for (
size_t j = 0; j < dim; j++) {
64 inputIndex += outputIdx[k] * inStrides[j];
66 outputData[i] = inputData[inputIndex];
68 model.AddConstantTensor<T>(
fNY, shapeY, outputData.data());
69 if (model.Verbose()) {
76 if (model.CheckIfTensorAlreadyExist(
fNX) ==
false){
77 std::cout<<
"Input tensor for transpose: "<<
fNX<<
'\n';
78 throw std::runtime_error(
"TMVA SOFIE Tranpose Op Input Tensor is not found in model");
83 for (
int i =
fShapeX.size() - 1; i >= 0; i--){
90 throw std::runtime_error(
"TMVA SOFIE Tranpose Op - Invalid axes attributes");
93 for (
size_t i = 0; i <
fAttrPerm.size(); i++){
97 if (model.IsInitializedTensor(
fNX) ) {
98 auto type = model.GetTensorType(
fNX);
101 ProcessInitializedTensor<float>(model);
104 ProcessInitializedTensor<int64_t>(model);
107 ProcessInitializedTensor<uint8_t>(model);
110 ProcessInitializedTensor<uint8_t>(model);
113 std::cout <<
"Transpose - no support for initialized tensor of type " <<
ConvertTypeToString(
type) << std::endl;
118 model.AddIntermediateTensor(
fNY, model.GetTensorType(
fNX),
fShapeY);
119 if (model.Verbose()) {
124 std::string
Generate(std::string opName)
override {
126 opName =
"op_" + opName;
128 throw std::runtime_error(
"TMVA SOFIE Transpose Op called to Generate without being initialized first");
135 bool isDynamic = (intShapeX.empty() && rank > 0);
137 std::string constQualifier = (isDynamic) ?
"const" :
"constexpr";
139 std::stringstream out;
150 out <<
SP <<
SP <<
"// Pre-baked input strides (row-major)\n";
151 out <<
SP <<
SP << constQualifier <<
" size_t " << opName <<
"_strX[] = {";
152 for (
size_t i = 0; i < rank; ++i)
153 out << stridesX[i] << (i + 1 < rank ?
", " :
"");
156 out <<
SP <<
SP <<
"// Pre-baked output strides (row-major)\n";
157 out <<
SP <<
SP << constQualifier <<
" size_t " << opName <<
"_strY[] = {";
158 for (
size_t i = 0; i < rank; ++i)
159 out << stridesY[i] << (i + 1 < rank ?
", " :
"");
163 bool innerContiguous = (
fAttrPerm.back() == (int64_t) (rank - 1));
164 size_t outerRank = innerContiguous ? rank - 1 : rank;
165 size_t innerSize = innerContiguous ? (isDynamic ? 0 : intShapeX[
fAttrPerm[rank - 1]])
168 if (innerContiguous && !isDynamic && innerSize > 1) {
171 <<
"// Fast path: last permuted axis is contiguous in source\n";
173 <<
"// Inner " << innerSize <<
" elements copied with pointer arithmetic\n";
179 out <<
SP <<
SP <<
SP <<
"size_t src_off = ";
180 for (
size_t i = 0; i < outerRank; ++i) {
181 out <<
"idx_" << i <<
" * " << opName <<
"_strX["
183 if (i + 1 < outerRank) out <<
" + ";
187 out <<
SP <<
SP <<
SP <<
"size_t dst_off = ";
188 for (
size_t i = 0; i < outerRank; ++i) {
189 out <<
"idx_" << i <<
" * " << opName <<
"_strY[" << i <<
"]";
190 if (i + 1 < outerRank) out <<
" + ";
196 <<
"std::copy(tensor_" <<
fNX <<
" + src_off, "
197 <<
"tensor_" <<
fNX <<
" + src_off + " << innerSize <<
", "
198 <<
"tensor_" <<
fNY <<
" + dst_off);\n";
205 out <<
SP <<
SP <<
"// General N-D transpose\n";
210 out <<
SP <<
SP <<
SP <<
"size_t src_idx = ";
211 for (
size_t i = 0; i < rank; ++i) {
212 out <<
"idx_" << i <<
" * " << opName <<
"_strX[" <<
fAttrPerm[i] <<
"]";
213 if (i + 1 < rank) out <<
" + ";
218 out <<
SP <<
SP <<
SP <<
"size_t dst_idx = ";
219 for (
size_t i = 0; i < rank; ++i) {
220 out <<
"idx_" << i <<
" * " << opName <<
"_strY[" << i <<
"]";
221 if (i + 1 < rank) out <<
" + ";
226 <<
"tensor_" <<
fNY <<
"[dst_idx] = "
227 <<
"tensor_" <<
fNX <<
"[src_idx];\n";
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
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 Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
std::string Generate(std::string opName) override
void Initialize(RModel &model) override
std::vector< Dim > fShapeX
void ProcessInitializedTensor(RModel &model)
ROperator_Transpose(std::vector< int64_t > attr_perm, std::string nameData, std::string nameOutput)
std::vector< Dim > fShapeY
std::vector< int64_t > fAttrPerm
std::vector< std::string_view > fInputTensorNames
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::vector< std::string_view > fOutputTensorNames
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 ConvertDimShapeToString(const std::vector< Dim > &shape)
std::size_t ConvertShapeToLength(const std::vector< size_t > &shape)
std::string ConvertValuesToString(size_t n, const T *data, size_t maxprint=-1)
std::vector< size_t > ConvertShapeToInt(const std::vector< Dim > &shape)
Convert shape based on Dim to integer format.
std::string ConvertTypeToString(ETensorType type)
void EmitNestedLoops(std::stringstream &out, size_t loopRank, const std::vector< Dim > shape)
std::string ConvertShapeToString(const std::vector< size_t > &shape)
void CloseNestedLoops(std::stringstream &out, size_t loopRank)
create variable transformations