Home | History | Annotate | Download | only in client
      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_CLIENT_COMPUTATION_H_
     17 #define TENSORFLOW_COMPILER_XLA_CLIENT_COMPUTATION_H_
     18 
     19 #include <memory>
     20 
     21 #include "tensorflow/compiler/xla/service/session.pb.h"
     22 #include "tensorflow/compiler/xla/service_interface.h"
     23 #include "tensorflow/compiler/xla/statusor.h"
     24 #include "tensorflow/compiler/xla/xla.pb.h"
     25 #include "tensorflow/compiler/xla/xla_data.pb.h"
     26 #include "tensorflow/core/platform/macros.h"
     27 
     28 namespace xla {
     29 
     30 // Wraps a ComputationHandle protobuf with a lifetime. Computation is
     31 // movable and not copyable to capture the same kind of unique
     32 // ownership that std::unique_ptr represents.
     33 class Computation {
     34  public:
     35   // Creates a null Computation.
     36   Computation();
     37 
     38   // parent: stub for the service on which we will deallocate the computation
     39   //   when it is no longer needed.
     40   // handle: the computation handle protobuf from the service.
     41   Computation(ServiceInterface* parent, const ComputationHandle& handle);
     42 
     43   Computation(Computation&& computation);
     44 
     45   // Deallocates the computation.
     46   ~Computation();
     47 
     48   Computation& operator=(Computation&& computation);
     49 
     50   // Returns the underlying handle.
     51   const ComputationHandle& handle() const { return handle_; }
     52 
     53   // Sets handle to a null state and clears any owned computation.
     54   void Reset();
     55 
     56   // Requests that we snapshot the computation into a serializable protocol
     57   // buffer form.
     58   StatusOr<std::unique_ptr<SessionModule>> Snapshot() const;
     59 
     60   // Returns true if this object is a null Computation.
     61   bool IsNull() const { return parent_ == nullptr; }
     62 
     63   // Returns the "program shape" (parameter and return shapes) for this
     64   // computation.
     65   StatusOr<ProgramShape> GetProgramShape() const;
     66 
     67  private:
     68   void ResetWithoutFreeing();
     69 
     70   ComputationHandle handle_;  // Handle that is wrapped by this class.
     71 
     72   // Stub that the handle is deallocated on when this object's lifetime ends.
     73   ServiceInterface* parent_;
     74 
     75   TF_DISALLOW_COPY_AND_ASSIGN(Computation);
     76 };
     77 
     78 }  // namespace xla
     79 
     80 #endif  // TENSORFLOW_COMPILER_XLA_CLIENT_COMPUTATION_H_
     81