aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/contrib/lite/delegates/eager/buffer_map.h
blob: a28329ae7d14e3e0214c6602b28b09c43876bbf0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#ifndef TENSORFLOW_CONTRIB_LITE_DELEGATES_EAGER_BUFFER_MAP_H_
#define TENSORFLOW_CONTRIB_LITE_DELEGATES_EAGER_BUFFER_MAP_H_

#include <map>

#include "tensorflow/contrib/lite/context.h"
#include "tensorflow/core/framework/tensor.h"

namespace tflite {
namespace eager {

// Maps a TF Lite tensor index into a TensorFlow tensor.
//
// The TF Lite interpreter assigns integer indices to each of its tensors, but
// the Eager delegate deals in terms of TensorFlow tensors. This class maps
// from indices to tensors and allows the creation of new tensors to be
// associated with a given index.
class BufferMap {
 public:
  BufferMap();
  ~BufferMap();

  // Returns true if the given 'tensor_index' has a corresponding
  // tensorflow::Tensor.
  bool HasTensor(int tensor_index) const;

  // Returns the tensorflow::Tensor associated with the given 'tensor_index'.
  // Precondition: HasTensor() is true.
  tensorflow::Tensor GetTensor(int tensor_index) const;

  // Associates the given tensorflow::Tensor with the given 'tensor_index'.
  // Note that tensorflow Tensors share data buffers, so this method is only a
  // shallow copy.
  void SetFromTensorFlow(int tensor_index, tensorflow::Tensor tensor);

  // Same as above but creates a new tensorflow::Tensor with a copy of the
  // given TfLiteTensor's data.
  void SetFromTfLite(int tensor_index, const TfLiteTensor* tensor);

 private:
  std::map<int, tensorflow::Tensor> id_to_tensor_;
};

}  // namespace eager
}  // namespace tflite

#endif  // TENSORFLOW_CONTRIB_LITE_DELEGATES_EAGER_BUFFER_MAP_H_