1 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 16 #include "tensorflow/core/util/util.h" 17 18 #include "tensorflow/core/lib/gtl/inlined_vector.h" 19 #include "tensorflow/core/lib/strings/strcat.h" 20 #include "tensorflow/core/platform/logging.h" 21 22 namespace tensorflow { 23 24 StringPiece NodeNamePrefix(const StringPiece& op_name) { 25 StringPiece sp(op_name); 26 auto p = sp.find('/'); 27 if (p == StringPiece::npos || p == 0) { 28 return ""; 29 } else { 30 return StringPiece(sp.data(), p); 31 } 32 } 33 34 StringPiece NodeNameFullPrefix(const StringPiece& op_name) { 35 StringPiece sp(op_name); 36 auto p = sp.rfind('/'); 37 if (p == StringPiece::npos || p == 0) { 38 return ""; 39 } else { 40 return StringPiece(sp.data(), p); 41 } 42 } 43 44 MovingAverage::MovingAverage(int window) 45 : window_(window), 46 sum_(0.0), 47 data_(new double[window_]), 48 head_(0), 49 count_(0) { 50 CHECK_GE(window, 1); 51 } 52 53 MovingAverage::~MovingAverage() { delete[] data_; } 54 55 void MovingAverage::Clear() { 56 count_ = 0; 57 head_ = 0; 58 sum_ = 0; 59 } 60 61 double MovingAverage::GetAverage() const { 62 if (count_ == 0) { 63 return 0; 64 } else { 65 return static_cast<double>(sum_) / count_; 66 } 67 } 68 69 void MovingAverage::AddValue(double v) { 70 if (count_ < window_) { 71 // This is the warmup phase. We don't have a full window's worth of data. 72 head_ = count_; 73 data_[count_++] = v; 74 } else { 75 if (window_ == ++head_) { 76 head_ = 0; 77 } 78 // Toss the oldest element 79 sum_ -= data_[head_]; 80 // Add the newest element 81 data_[head_] = v; 82 } 83 sum_ += v; 84 } 85 86 static char hex_char[] = "0123456789abcdef"; 87 88 string PrintMemory(const char* ptr, size_t n) { 89 string ret; 90 ret.resize(n * 3); 91 for (int i = 0; i < n; ++i) { 92 ret[i * 3] = ' '; 93 ret[i * 3 + 1] = hex_char[ptr[i] >> 4]; 94 ret[i * 3 + 2] = hex_char[ptr[i] & 0xf]; 95 } 96 return ret; 97 } 98 99 string SliceDebugString(const TensorShape& shape, const int64 flat) { 100 // Special case rank 0 and 1 101 const int dims = shape.dims(); 102 if (dims == 0) return ""; 103 if (dims == 1) return strings::StrCat("[", flat, "]"); 104 105 // Compute strides 106 gtl::InlinedVector<int64, 32> strides(dims); 107 strides.back() = 1; 108 for (int i = dims - 2; i >= 0; i--) { 109 strides[i] = strides[i + 1] * shape.dim_size(i + 1); 110 } 111 112 // Unflatten index 113 int64 left = flat; 114 string result; 115 for (int i = 0; i < dims; i++) { 116 strings::StrAppend(&result, i ? "," : "[", left / strides[i]); 117 left %= strides[i]; 118 } 119 strings::StrAppend(&result, "]"); 120 return result; 121 } 122 123 } // namespace tensorflow 124