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 #ifndef TENSORFLOW_COMPILER_XLA_SERVICE_LOGICAL_BUFFER_ANALYSIS_H_
     17 #define TENSORFLOW_COMPILER_XLA_SERVICE_LOGICAL_BUFFER_ANALYSIS_H_
     18 
     19 #include "tensorflow/compiler/xla/service/hlo_instruction.h"
     20 #include "tensorflow/compiler/xla/service/hlo_module.h"
     21 #include "tensorflow/compiler/xla/service/logical_buffer.h"
     22 #include "tensorflow/compiler/xla/statusor.h"
     23 #include "tensorflow/core/lib/hash/hash.h"
     24 
     25 namespace xla {
     26 // A class to create all the logical buffers defined by the HLO ops in a module.
     27 class LogicalBufferAnalysis : public DfsHloVisitorWithDefault {
     28  public:
     29   // Runs points-to analysis on 'module'.
     30   static StatusOr<std::unique_ptr<LogicalBufferAnalysis>> Run(
     31       const HloModule* module);
     32 
     33   // Returns the logical buffer with the given ID.
     34   LogicalBuffer& GetBuffer(LogicalBuffer::Id id) const;
     35 
     36   // Returns the logical buffer that represents the output of a given HLO
     37   // at a given index.
     38   LogicalBuffer& GetBuffer(HloInstruction* instruction,
     39                            const ShapeIndex& index) const;
     40 
     41   const std::vector<std::unique_ptr<LogicalBuffer>>& logical_buffers() const {
     42     return logical_buffers_;
     43   }
     44   LogicalBuffer::Id num_logical_buffers() const { return next_buffer_id_; }
     45 
     46  private:
     47   explicit LogicalBufferAnalysis(const HloModule* module) : module_(module) {}
     48   Status Analyze();
     49 
     50   // The module this analysis is performed on.
     51   const HloModule* module_;
     52 
     53   // Create a new logical buffer and return a reference to it. The newly created
     54   // buffer is stored in an internal vector of LogicalBuffers and can be
     55   // accessed with GetBuffer.
     56   void NewLogicalBuffer(HloInstruction* instruction, const ShapeIndex& index);
     57 
     58   Status DefaultAction(HloInstruction* hlo_instruction) override;
     59   Status HandleTuple(HloInstruction* tuple) override;
     60   Status HandleGetTupleElement(HloInstruction* get_tuple_element) override;
     61   Status HandleBitcast(HloInstruction* bitcast) override;
     62   Status HandleCopy(HloInstruction* copy) override;
     63   Status HandleRecvDone(HloInstruction* recv_done) override;
     64   Status HandleSend(HloInstruction* send) override;
     65   Status HandleSelect(HloInstruction* select) override;
     66 
     67   // A map from the buffer ID to the logical buffer
     68   std::vector<std::unique_ptr<LogicalBuffer>> logical_buffers_;
     69 
     70   struct Hasher {
     71     size_t operator()(
     72         std::pair<const HloInstruction*, const ShapeIndex> p) const {
     73       size_t inst_hash = tensorflow::hash<const HloInstruction*>()(p.first);
     74       for (auto index = p.second.begin(); index != p.second.end(); ++index) {
     75         inst_hash = tensorflow::Hash64Combine(*index, inst_hash);
     76       }
     77       return inst_hash;
     78     }
     79   };
     80 
     81   // A map from an hlo + shape index to the logical buffer representing
     82   // the appropriate output.
     83   std::unordered_map<std::pair<const HloInstruction*, const ShapeIndex>,
     84                      LogicalBuffer*, Hasher>
     85       output_buffers_;
     86 
     87   // The ID of the next logical buffer created.
     88   LogicalBuffer::Id next_buffer_id_ = 0;
     89 };
     90 
     91 }  // namespace xla
     92 
     93 #endif  // TENSORFLOW_COMPILER_XLA_SERVICE_LOGICAL_BUFFER_ANALYSIS_H_
     94