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
NodeNamePrefix(const StringPiece & op_name)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
NodeNameFullPrefix(const StringPiece & op_name)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
MovingAverage(int window)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
~MovingAverage()53 MovingAverage::~MovingAverage() { delete[] data_; }
54
Clear()55 void MovingAverage::Clear() {
56 count_ = 0;
57 head_ = 0;
58 sum_ = 0;
59 }
60
GetAverage() const61 double MovingAverage::GetAverage() const {
62 if (count_ == 0) {
63 return 0;
64 } else {
65 return static_cast<double>(sum_) / count_;
66 }
67 }
68
AddValue(double v)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
PrintMemory(const char * ptr,size_t n)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
SliceDebugString(const TensorShape & shape,const int64 flat)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 #ifdef INTEL_MKL
DisableMKL()124 bool DisableMKL() {
125 enum MklStatus { MKL_DEFAULT = 0, MKL_ON = 1, MKL_OFF = 2 };
126 static MklStatus status = MKL_DEFAULT;
127 if (status == MKL_DEFAULT) {
128 char* tf_disable_mkl = getenv("TF_DISABLE_MKL");
129 if ((tf_disable_mkl != NULL) && (std::stoi(tf_disable_mkl) == 1)) {
130 VLOG(2) << "TF-MKL: Disabling MKL";
131 status = MKL_OFF;
132 } else {
133 status = MKL_ON;
134 }
135 }
136 return status == MKL_OFF ? true : false;
137 }
138 #endif // INTEL_MKL
139 } // namespace tensorflow
140