1 /* Copyright 2016 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 vcyou 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_CORE_KERNELS_HEXAGON_HEXAGON_CONTROL_WRAPPER_H_ 17 #define TENSORFLOW_CORE_KERNELS_HEXAGON_HEXAGON_CONTROL_WRAPPER_H_ 18 19 #include <unordered_map> 20 #include <vector> 21 22 #include "tensorflow/core/framework/types.h" 23 #include "tensorflow/core/kernels/hexagon/graph_transferer.h" 24 #include "tensorflow/core/kernels/i_remote_fused_graph_executor.h" 25 #include "tensorflow/core/platform/macros.h" 26 27 namespace tensorflow { 28 29 /* 30 HexagonControlWrapper is implementing interfaces in IRemoteFusedGraphExecutor. 31 This class calls APIs on hexagon via hexagon control binary. 32 TODO(satok): Add more documents about hexagon control binary. 33 */ 34 class HexagonControlWrapper final : public IRemoteFusedGraphExecutor { 35 public: 36 using ByteArray = 37 std::tuple<uint8* /* data */, uint64 /* size */, DataType /* type */>; 38 static constexpr const char* const REMOTE_FUSED_GRAPH_EXECUTOR_NAME = 39 "build_hexagon_remote_fused_graph_executor"; 40 41 HexagonControlWrapper() = default; 42 int GetVersion() final; 43 bool Init(const RemoteFusedGraphExecuteInfo& info) final; 44 bool Finalize() final; 45 bool SetupGraph() final; 46 bool ExecuteGraph() final; 47 bool TeardownGraph() final; 48 bool FillInputNode(const string& node_name, const Tensor& tensor) final; 49 bool ReadOutputNode(const string& node_name, 50 TensorAllocatorFunc tensor_allocator) final; 51 Status FuseRemoteGraph(const GraphDef& original_graph_def, 52 const std::vector<string>& inputs, 53 const std::vector<string>& outputs, 54 GraphDef* fused_graph_def) final; 55 bool IsEnabled() const final; 56 bool ReadOutputNode(const string& node_name, std::vector<ByteArray>* outputs); 57 58 private: 59 using ConstByteArray = std::tuple<const uint8* /* data */, uint64 /* size */, 60 DataType /* type */>; 61 62 bool FillInputNode( 63 const string& node_name, 64 const std::array<int64, GraphTransferer::SHAPE_ARRAY_SIZE>& shape, 65 const ConstByteArray bytes); 66 67 // CAVEAT: Need offset as HVX library reserves some ids 68 static constexpr int NODE_ID_OFFSET = 0x10000; 69 70 static GraphTransferNodeInfo* FindNodeInfo( 71 const string& name, GraphTransferInfo* graph_transfer_info); 72 73 const RemoteFusedGraphExecuteInfo* execute_info_{}; 74 GraphTransferer graph_transferer_{}; 75 // Dummy float array for input node. 76 // TODO(satok): Use actual data passed by FillInputNode and remove 77 // std::vector<float> dummy_input_float_{}; 78 std::unordered_map<int, std::vector<uint8>> input_tensor_data_{}; 79 // Dummy byte array for const node. 80 // TODO(satok): Remove 81 std::unordered_map<int, std::vector<uint8>> dummy_const_data_{}; 82 83 std::unordered_map<string, int> input_port_map_{}; 84 std::unordered_map<string, int> output_port_map_{}; 85 86 TF_DISALLOW_COPY_AND_ASSIGN(HexagonControlWrapper); 87 }; 88 89 } // namespace tensorflow 90 91 #endif // TENSORFLOW_CORE_KERNELS_HEXAGON_HEXAGON_CONTROL_WRAPPER_H_ 92