7#include <unordered_map>
11namespace Experimental {
19 for (
size_t i =0; i < shape.size(); i++){
29 std::vector<size_t>
ret_shape(shape.size());
30 for (
size_t i =0; i < shape.size(); i++){
31 if (shape[i].isParam) {
35 val = std::stoi(shape[i].param);
36 if (val >= 0)
ret_shape[i] =
static_cast<size_t>(val);
42 catch (
const std::invalid_argument& ) {
56 std::size_t fLength = 1;
57 for (
auto& dim: shape) fLength *= dim;
97 return "other_" + std::to_string( (
int)
type);
105 if(
type ==
"float32" ||
type ==
"float" ||
type ==
"Float"){
108 else if(
type ==
"int64" ||
type ==
"int64_t"){
111 else if(
type ==
"int32" ||
type ==
"int32_t"){
114 else if (
type ==
"double" ||
type ==
"float64"){
117 else if (
type ==
"bool" ||
type ==
"uint8_t" ){
126 std::stringstream out;
128 for (
size_t i = 0; i < shape.size(); i++) {
130 if (i < shape.size()-1) out <<
" , ";
137 std::stringstream out;
139 for (
size_t i = 0; i < shape.size(); i++) {
141 if (i < shape.size()-1) out <<
" , ";
152 if (shape.empty())
return "1";
154 for (
size_t i = 0; i < shape.size(); i++) {
155 if (shape[i].isParam) {
171 }
else if (
length.empty()) {
198 auto [ptr,
ec] = std::from_chars(s.data(), s.data() + s.size(),
value);
199 return ec == std::errc() && ptr == s.data() + s.size();
206 for (
size_t dim = 0; dim <
shapeA.size(); dim++) {
217 for (
size_t dim = 0; dim <
shapeA.size(); dim++) {
218 if (
shapeB[dim].isParam)
return false;
229 for (
size_t dim = 0; dim <
shapeA.size(); dim++) {
239 if (shape.size() < 2) {
241 std::runtime_error(
"TMVA::SOFIE - MultidirectionalBroadcastShape requires at least 2 input shapes.");
244 size_t n = shape.size();
247 for (
size_t i = 1; i <
n; i++) {
252 for (
size_t i = 0; i <
n; i++) {
261 for (
size_t i = 1; i <
n; i++) {
262 for (
size_t dim = 0; dim < shape[0].size(); dim++) {
263 if (shape[i][dim] != shape[0][dim]) {
277 for (
size_t i = 0; i <
n; i++) {
278 for (
size_t dim = 0; dim <
targetSize; dim++) {
284 for (
size_t i = 0; i <
n; i++) {
285 for (
size_t dim = 0; dim <
targetSize; dim++) {
299 std::stringstream
ss;
300 ss <<
"TMVA::SOFIE - Error multidirectional broadcasting shapes ";
301 for (
size_t i = 0; i <
n; i++) {
303 if (
n > 2 && i <
n - 2) {
305 }
else if (
n >=2 && i ==
n - 2) {
309 ss <<
" to the same shape.";
311 std::runtime_error(
ss.str());
316 for (
size_t i = 0; i <
n; i++) {
326 for (
size_t i = 0; i <
n; i++) {
327 for (
size_t dim = 0; dim <
targetSize; dim++) {
333 for (
size_t i = 0; i <
n; i++) {
334 for (
size_t dim = 0; dim <
targetSize; dim++) {
347 std::stringstream
ss;
348 ss <<
"TMVA::SOFIE - Error multidirectional broadcasting shapes ";
349 for (
size_t i = 0; i <
n; i++) {
351 if (
n > 2 && i <
n - 2) {
353 }
else if (
n >=2 && i ==
n - 2) {
357 ss <<
" to the same shape.";
359 std::runtime_error(
ss.str());
375 return std::make_pair(0,
shapeA);
393 for (
size_t i = 0; i <
size; i++) {
403 for (
size_t i = 0; i <
size; i++) {
411 std::runtime_error(
"TMVA::SOFIE - Error multidirectional broadcasting tensors of shape "
413 +
" to a common shape.");
422 std::runtime_error(
"TMVA::SOFIE - Error unidirectional broadcasting tensors of shape "
424 +
" in a common shape.");
439 return std::make_pair(0,
shapeA);
460 for (
size_t i = 0; i <
size; i++) {
464 }
else if (
shapeA[i].isParam &&
shapeB[i].GetVal() ==
"1" ) {
468 }
else if (
shapeA[i].GetVal() ==
"1" &&
shapeB[i].isParam) {
476 }
else if (
shapeA[i].dim == 1) {
486 s <<
"std::max(" <<
shapeA[i] <<
"," <<
shapeB[i] <<
")";
500 throw std::runtime_error(
"TMVA::SOFIE - Fatal error in MultiDirectionalBroadCastDimShape");
504 throw std::runtime_error(
"TMVA::SOFIE - Error multidirectional broadcasting tensors of shape " +
506 " to a common shape.");
514 std::replace( s.begin(), s.end(),
'-',
'_');
516 s.erase(std::remove_if(s.begin(), s.end(), [](
char const&
c ) ->
bool { return !std::isalnum(c) && c !=
'_'; } ), s.end());
522 const auto size = shape.size();
523 std::vector<size_t> strides(
size,1);
524 for (std::size_t i = 1; i <
size; i++) {
525 strides[
size - 1 - i] = strides[
size - i ] * shape[
size - i];
532 const auto size = shape.size();
533 std::vector<Dim> strides(
size);
536 for (std::size_t i = 1; i <
size; i++) {
537 if (!shape[
size-i].isParam && !strides[
size-i].isParam)
540 if (strides[
size-i].GetVal() ==
"1")
541 strides[
size - 1 - i] = shape[
size-i];
542 else if (shape[
size-i].GetVal() ==
"1")
543 strides[
size - 1 - i] = strides[
size-i];
545 strides[
size - 1 - i] =
Dim{std::string(strides[
size-i].GetVal() +
"*" + shape[
size-i].GetVal())};
557const std::string
SP =
" ";
559 for (
size_t i = 0; i <
loopRank; ++i) {
560 for (
size_t s = 0; s < i + 2; ++s) out <<
SP;
562 out <<
"for (size_t idx_" << i <<
" = 0; idx_" << i
563 <<
" < " << shape[i] <<
"; ++idx_" << i <<
") {\n";
567 for (int64_t i =
loopRank - 1; i >= 0; --i) {
568 for (int64_t s = 0; s < i + 2; ++s) out <<
SP;
589 if (
t != o.
t)
return t < o.
t;
599 if (!(t.end > t.begin)) {
600 throw std::runtime_error(
"Each tensor must have end > begin.");
605 std::vector<MemoryEvent> events;
611 std::sort(events.begin(), events.end());
617 std::set<FreeBlock> free_list;
620 std::unordered_map<int, std::size_t>
live_size;
623 std::size_t total_bytes = 0;
629 auto best = free_list.end();
630 for (
auto it = free_list.begin(); it != free_list.end(); ++it) {
631 if (it->size >= need) {
632 if (
best == free_list.end() || it->size <
best->size)
636 if (
best != free_list.end()) {
637 std::size_t off =
best->offset;
638 if (
best->size == need) {
639 free_list.erase(
best);
642 free_list.erase(
best);
648 std::size_t off = total_bytes;
653 auto try_coalesce = [&](std::set<FreeBlock>::iterator it) {
655 if (it != free_list.begin()) {
656 auto prev = std::prev(it);
657 if (prev->offset + prev->size == it->offset) {
659 free_list.erase(prev);
660 it = free_list.erase(it);
661 it = free_list.insert(
merged).first;
665 auto next = std::next(it);
666 if (next != free_list.end() && it->offset + it->size == next->offset) {
668 free_list.erase(next);
669 it = free_list.erase(it);
675 for (
const auto &
e : events) {
682 auto it = free_list.insert(fb).first;
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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 Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t target
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 value
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
const_iterator begin() const
const_iterator end() const
bool AreSameShape(const std::vector< size_t > &, const std::vector< size_t > &)
std::vector< size_t > UnidirectionalBroadcastShape(std::vector< size_t > &, std::vector< size_t > &)
std::string Clean_name(std::string input_tensor_name)
std::vector< size_t > MultidirectionalBroadcastShape(std::vector< std::vector< size_t > >)
std::vector< size_t > ComputeStrideFromShape(const std::vector< size_t > &shape)
compute stride of a tensor given its shape (assume layout is row-major)
MemoryResult OrganizeMemory(const std::vector< TensorLifeInfo > &tensorsInfo)
Greedy best-fit planner with coalescing free list.
std::string ConvertDimShapeToString(const std::vector< Dim > &shape)
std::size_t ConvertShapeToLength(const std::vector< size_t > &shape)
std::vector< Dim > ConvertShapeToDim(const std::vector< size_t > &shape)
Convert shape from integer format to dynamic one (based on Dim)
std::vector< size_t > ConvertShapeToInt(const std::vector< Dim > &shape)
Convert shape based on Dim to integer format.
std::string ConvertTypeToString(ETensorType type)
ETensorType ConvertStringToType(std::string type)
std::string ConvertDimShapeToLength(const std::vector< Dim > &shape)
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)
bool IsInteger(const std::string &s)
create variable transformations
bool operator<(const FreeBlock &other) const
bool operator<(const MemoryEvent &o) const