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);
103 if(
type ==
"float32" ||
type ==
"float" ||
type ==
"Float"){
106 else if(
type ==
"int64" ||
type ==
"int64_t"){
109 else if (
type ==
"double" ||
type ==
"float64"){
112 else if (
type ==
"bool" ){
121 std::stringstream out;
123 for (
size_t i = 0; i < shape.size(); i++) {
125 if (i < shape.size()-1) out <<
" , ";
132 std::stringstream out;
134 for (
size_t i = 0; i < shape.size(); i++) {
136 if (i < shape.size()-1) out <<
" , ";
147 if (shape.empty())
return "1";
149 for (
size_t i = 0; i < shape.size(); i++) {
150 if (shape[i].isParam) {
166 }
else if (
length.empty()) {
199 auto [ptr,
ec] = std::from_chars(s.data(), s.data() + s.size(),
value);
200 return ec == std::errc() && ptr == s.data() + s.size();
207 for (
size_t dim = 0; dim <
shapeA.size(); dim++) {
218 for (
size_t dim = 0; dim <
shapeA.size(); dim++) {
219 if (
shapeB[dim].isParam)
return false;
230 for (
size_t dim = 0; dim <
shapeA.size(); dim++) {
240 if (shape.size() < 2) {
242 std::runtime_error(
"TMVA::SOFIE - MultidirectionalBroadcastShape requires at least 2 input shapes.");
245 size_t n = shape.size();
248 for (
size_t i = 1; i <
n; i++) {
253 for (
size_t i = 0; i <
n; i++) {
262 for (
size_t i = 1; i <
n; i++) {
263 for (
size_t dim = 0; dim < shape[0].size(); dim++) {
264 if (shape[i][dim] != shape[0][dim]) {
278 for (
size_t i = 0; i <
n; i++) {
279 for (
size_t dim = 0; dim <
targetSize; dim++) {
285 for (
size_t i = 0; i <
n; i++) {
286 for (
size_t dim = 0; dim <
targetSize; dim++) {
300 std::stringstream
ss;
301 ss <<
"TMVA::SOFIE - Error multidirectional broadcasting shapes ";
302 for (
size_t i = 0; i <
n; i++) {
304 if (
n > 2 && i <
n - 2) {
306 }
else if (
n >=2 && i ==
n - 2) {
310 ss <<
" to the same shape.";
312 std::runtime_error(
ss.str());
317 for (
size_t i = 0; i <
n; i++) {
327 for (
size_t i = 0; i <
n; i++) {
328 for (
size_t dim = 0; dim <
targetSize; dim++) {
334 for (
size_t i = 0; i <
n; i++) {
335 for (
size_t dim = 0; dim <
targetSize; dim++) {
348 std::stringstream
ss;
349 ss <<
"TMVA::SOFIE - Error multidirectional broadcasting shapes ";
350 for (
size_t i = 0; i <
n; i++) {
352 if (
n > 2 && i <
n - 2) {
354 }
else if (
n >=2 && i ==
n - 2) {
358 ss <<
" to the same shape.";
360 std::runtime_error(
ss.str());
376 return std::make_pair(0,
shapeA);
394 for (
size_t i = 0; i <
size; i++) {
404 for (
size_t i = 0; i <
size; i++) {
412 std::runtime_error(
"TMVA::SOFIE - Error multidirectional broadcasting tensors of shape "
414 +
" to a common shape.");
423 std::runtime_error(
"TMVA::SOFIE - Error unidirectional broadcasting tensors of shape "
425 +
" in a common shape.");
440 return std::make_pair(0,
shapeA);
461 for (
size_t i = 0; i <
size; i++) {
465 }
else if (
shapeA[i].isParam &&
shapeB[i].GetVal() ==
"1" ) {
469 }
else if (
shapeA[i].GetVal() ==
"1" &&
shapeB[i].isParam) {
477 }
else if (
shapeA[i].dim == 1) {
487 s <<
"std::max(" <<
shapeA[i] <<
"," <<
shapeB[i] <<
")";
501 throw std::runtime_error(
"TMVA::SOFIE - Fatal error in MultiDirectionalBroadCastDimShape");
505 throw std::runtime_error(
"TMVA::SOFIE - Error multidirectional broadcasting tensors of shape " +
507 " to a common shape.");
515 std::replace( s.begin(), s.end(),
'-',
'_');
517 s.erase(std::remove_if(s.begin(), s.end(), [](
char const&
c ) ->
bool { return !std::isalnum(c) && c !=
'_'; } ), s.end());
523 const auto size = shape.size();
524 std::vector<size_t> strides(
size,1);
525 for (std::size_t i = 1; i <
size; i++) {
526 strides[
size - 1 - i] = strides[
size - i ] * shape[
size - i];
533 const auto size = shape.size();
534 std::vector<Dim> strides(
size);
537 for (std::size_t i = 1; i <
size; i++) {
538 if (!shape[
size-i].isParam && !strides[
size-i].isParam)
541 if (strides[
size-i].GetVal() ==
"1")
542 strides[
size - 1 - i] = shape[
size-i];
543 else if (shape[
size-i].GetVal() ==
"1")
544 strides[
size - 1 - i] = strides[
size-i];
546 strides[
size - 1 - i] =
Dim{std::string(strides[
size-i].GetVal() +
"*" + shape[
size-i].GetVal())};
567 if (
t != o.
t)
return t < o.
t;
577 if (!(t.end > t.begin)) {
578 throw std::runtime_error(
"Each tensor must have end > begin.");
583 std::vector<MemoryEvent> events;
589 std::sort(events.begin(), events.end());
595 std::set<FreeBlock> free_list;
598 std::unordered_map<int, std::size_t>
live_size;
601 std::size_t total_bytes = 0;
607 auto best = free_list.end();
608 for (
auto it = free_list.begin(); it != free_list.end(); ++it) {
609 if (it->size >=
need) {
610 if (
best == free_list.end() || it->size <
best->size)
614 if (
best != free_list.end()) {
615 std::size_t off =
best->offset;
617 free_list.erase(
best);
620 free_list.erase(
best);
626 std::size_t off = total_bytes;
631 auto try_coalesce = [&](std::set<FreeBlock>::iterator it) {
633 if (it != free_list.begin()) {
634 auto prev = std::prev(it);
635 if (prev->offset + prev->size == it->offset) {
637 free_list.erase(prev);
638 it = free_list.erase(it);
639 it = free_list.insert(
merged).first;
643 auto next = std::next(it);
644 if (next != free_list.end() && it->offset + it->size == next->offset) {
646 free_list.erase(next);
647 it = free_list.erase(it);
653 for (
const auto &
e : events) {
660 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::string ConvertDynamicShapeToLength(const std::vector< Dim > &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)
std::string ConvertShapeToString(const std::vector< size_t > &shape)
bool IsInteger(const std::string &s)
create variable transformations
bool operator<(const FreeBlock &other) const
bool operator<(const MemoryEvent &o) const