chromium/third_party/tflite/src/tensorflow/lite/core/api/op_resolver.h

/* 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_LITE_CORE_API_OP_RESOLVER_H_
#define TENSORFLOW_LITE_CORE_API_OP_RESOLVER_H_

#include <functional>
#include <limits>
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>

#include "tensorflow/lite/core/api/error_reporter.h"
#include "tensorflow/lite/core/c/common.h"
#include "tensorflow/lite/schema/schema_generated.h"

namespace tflite {

#ifndef DOXYGEN_SKIP
class OpResolverInternal;  // For friend declaration below.
class Subgraph;            // For friend declaration below.

namespace internal {
class CommonOpaqueConversionUtil;  // For friend declaration below.
class OperatorsCache;              // Forward decl.
}  // namespace internal
#endif

/// Abstract interface that returns TfLiteRegistrations given op codes or custom
/// op names. This is the mechanism that ops being referenced in the flatbuffer
/// model are mapped to executable function pointers (TfLiteRegistrations).
///
/// The lifetime of the TfLiteRegistration object whose address is
/// returned by FindOp must exceed the lifetime of any InterpreterBuilder or
/// Interpreter created with this OpResolver.
/// Likewise the lifetime of the TfLiteOperator object referenced
/// from the TfLiteRegistration object, if any, must exceed the lifetime of
/// any InterpreterBuilder or Interpreter created with this OpResolver.
class OpResolver {};

#ifndef DOXYGEN_SKIP
// Type for a set of owned 'TfLiteOperator' objects.
// This is needed when converting TfLiteRegistration to
// TfLiteOperator, to ensure that the number of
// TfLiteOperator objects that we allocate is bounded, and to
// ensure that those objects get deallocated at the appropriate time.
// We use a public class rather than a typedef or using declaration here,
// to ensure that the class can be forward-declared.
// WARNING: Experimental interface, subject to change.
namespace internal {
class OperatorsCache
    : private std::unordered_map<OpResolver::OpId,
                                 std::unique_ptr<TfLiteOperator>,
                                 OpResolver::OpId::Hasher> {};
}  // namespace internal
#endif

// Handles the logic for converting between an OperatorCode structure extracted
// from a flatbuffer and information about a registered operator
// implementation.
TfLiteStatus GetRegistrationFromOpCode(const OperatorCode* opcode,
                                       const OpResolver& op_resolver,
                                       ErrorReporter* error_reporter,
                                       const TfLiteRegistration** registration);

}  // namespace tflite

#endif  // TENSORFLOW_LITE_CORE_API_OP_RESOLVER_H_