1/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7    http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15#ifndef TENSORFLOW_CONTRIB_LITE_KERNELS_GEMM_SUPPORT_H_
16#define TENSORFLOW_CONTRIB_LITE_KERNELS_GEMM_SUPPORT_H_
17
18#include "public/gemmlowp.h"
19#include "tensorflow/contrib/lite/context.h"
20
21namespace tflite {
22namespace gemm_support {
23
24// Returns the GemmContext stored in 'context', allowing multiple ops to
25// share a single object, as long as they share a TfLiteContext. The caller
26// must ensure that this is called between IncrementUsageCounter() and
27// DecrementUsageCounter(). For example, in the implementation of an op:
28//   void* Init(TfLiteContext* context, const char*, size_t) {
29//     gemm_support::IncrementUsageCounter(context);
30//     return nullptr;
31//   }
32//   void Free(TfLiteContext* context, void*) {
33//     gemm_support::DecrementUsageCounter(context);
34//   }
35//   TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) {
36//     auto* gemm_context = gemm_support::GetFromContext(context);
37//   }
38gemmlowp::GemmContext* GetFromContext(TfLiteContext* context);
39
40// Let the framework know that the GemmContext stored in 'context' will be used
41// by an op. If necessary a new GemmContext is created and placed in 'context'.
42void IncrementUsageCounter(TfLiteContext* context);
43
44// Let the framework know that the op stopped using the GemmContext stored in
45// 'context'. If there are no more usages the GemmContext will be deleted.
46void DecrementUsageCounter(TfLiteContext* context);
47
48// Set the maximum number threads available for gemmlowp operations.
49void SetMaxNumThreads(TfLiteContext* context, int num_threads);
50
51}  // namespace gemm_support
52}  // namespace tflite
53
54#endif  // TENSORFLOW_CONTRIB_LITE_KERNELS_GEMM_SUPPORT_H_
55