Home | History | Annotate | Download | only in service
      1 /* Copyright 2017 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/compiler/xla/service/hlo_buffer.h"
     17 
     18 #include <algorithm>
     19 #include <ostream>
     20 #include <utility>
     21 #include <vector>
     22 
     23 #include "tensorflow/compiler/xla/map_util.h"
     24 #include "tensorflow/compiler/xla/service/hlo_instruction.h"
     25 #include "tensorflow/compiler/xla/shape_util.h"
     26 #include "tensorflow/compiler/xla/types.h"
     27 #include "tensorflow/compiler/xla/util.h"
     28 #include "tensorflow/core/lib/core/errors.h"
     29 #include "tensorflow/core/lib/gtl/flatset.h"
     30 #include "tensorflow/core/lib/strings/str_util.h"
     31 #include "tensorflow/core/lib/strings/strcat.h"
     32 #include "tensorflow/core/platform/logging.h"
     33 
     34 namespace xla {
     35 
     36 using ::tensorflow::str_util::Join;
     37 using ::tensorflow::strings::StrCat;
     38 
     39 bool HloBuffer::operator==(const HloBuffer& other) const {
     40   bool equal = id() == other.id();
     41   if (equal) {
     42     // DCHECK because these comparisons are expensive (linear time).
     43     DCHECK(values_ == other.values_);
     44   }
     45   return equal;
     46 }
     47 
     48 std::vector<HloPosition> HloBuffer::ComputePositions() const {
     49   std::vector<HloPosition> positions;
     50   for (const HloValue* value : values_) {
     51     positions.insert(positions.end(), value->positions().begin(),
     52                      value->positions().end());
     53   }
     54   // Remove duplicates and sort positions.
     55   std::sort(positions.begin(), positions.end());
     56   positions.erase(std::unique(positions.begin(), positions.end()),
     57                   positions.end());
     58   return positions;
     59 }
     60 
     61 string HloBuffer::ToString() const {
     62   return StrCat("HloBuffer ", id_, ", values: ",
     63                 Join(values_, ", ", [](string* result, const HloValue* value) {
     64                   result->append(value->ToShortString());
     65                 }));
     66 }
     67 
     68 std::ostream& operator<<(std::ostream& out, const HloBuffer& buffer) {
     69   out << buffer.ToString();
     70   return out;
     71 }
     72 
     73 }  // namespace xla
     74