1 /* Copyright 2020 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_LITE_SUPPORT_CUSTOM_OPS_KERNEL_SENTENCEPIECE_OPTIMIZED_ENCODER_H_ 17 #define TENSORFLOW_LITE_SUPPORT_CUSTOM_OPS_KERNEL_SENTENCEPIECE_OPTIMIZED_ENCODER_H_ 18 19 // Sentencepiece encoder optimized with memmapped model. 20 21 #include <string> 22 #include <tuple> 23 #include <vector> 24 25 #include "tensorflow_lite_support/custom_ops/kernel/sentencepiece/encoder_config_generated.h" 26 27 namespace tflite { 28 namespace ops { 29 namespace custom { 30 namespace sentencepiece { 31 32 enum class EncoderResultType { SUCCESS = 0, WRONG_CONFIG = 1 }; 33 34 struct EncoderResult { 35 EncoderResultType type = EncoderResultType::SUCCESS; 36 std::vector<int> codes; 37 std::vector<int> offsets; 38 }; 39 std::tuple<std::string, std::vector<int>> NormalizeString( 40 const std::string& in_string, const EncoderConfig& config); 41 42 // Encodes one string and returns ids and offsets. Takes the configuration as a 43 // type-erased buffer. 44 EncoderResult EncodeString(const std::string& string, const void* config_buffer, 45 bool add_bos, bool add_eos, bool reverse); 46 47 } // namespace sentencepiece 48 } // namespace custom 49 } // namespace ops 50 } // namespace tflite 51 52 #endif // TENSORFLOW_LITE_SUPPORT_CUSTOM_OPS_KERNEL_SENTENCEPIECE_OPTIMIZED_ENCODER_H_ 53