Loading...
Searching...
No Matches
tokenizer_layer.h
Go to the documentation of this file.
1// SPDX-License-Identifier: AGPL-3.0-only
2// Copyright (C) 2026 Artificial Intelligence Techniques, S.L. and Roberto López González
3
4#pragma once
5
9
10namespace opennn
11{
12
13class Tokenizer final : public Layer
14{
15public:
16
17 Tokenizer(const Shape& = {},
18 const string& = "tokenizer",
20
21 Shape get_output_shape() const noexcept override { return input_shape; }
22
23 bool accepts_input_rank(Index rank) const override { return is_one_of(rank, 1); }
24 bool allows_bf16_input_cast(size_t) const noexcept override { return false; }
25
26 vector<TensorSpec> get_forward_specs(Index) const override { return {}; }
27
28 void set_tokenizer(unique_ptr<TokenizerOperator>);
29 const TokenizerOperator* get_tokenizer() const noexcept { return tokenizer.get(); }
30 VariableRole get_variable_role() const noexcept { return variable_role; }
31
32 void set_vocabulary(const vector<string>&);
33 const vector<string>& get_vocabulary() const;
35 Index get_vocabulary_size() const { return tokenizer ? tokenizer->get_vocabulary_size() : 0; }
36
37 void read_JSON_body(const Json*) override;
38 void write_JSON_body(JsonWriter&) const override;
39
40private:
41
42 unique_ptr<TokenizerOperator> tokenizer;
43 VariableRole variable_role = VariableRole::Input;
44};
45
46}
Definition json.h:92
Definition json.h:24
Layer(LayerType type, Trainability trainability=Trainability::Trainable)
Definition layer.h:210
Shape input_shape
Definition layer.h:219
Definition tokenizer_operator.h:35
StringMap< Index > VocabularyMap
Definition tokenizer_operator.h:37
Shape get_output_shape() const noexcept override
Definition tokenizer_layer.h:21
void read_JSON_body(const Json *) override
vector< TensorSpec > get_forward_specs(Index) const override
Definition tokenizer_layer.h:26
const vector< string > & get_vocabulary() const
const TokenizerOperator::VocabularyMap & get_vocabulary_map() const
bool accepts_input_rank(Index rank) const override
Definition tokenizer_layer.h:23
VariableRole get_variable_role() const noexcept
Definition tokenizer_layer.h:30
void set_vocabulary(const vector< string > &)
void set_tokenizer(unique_ptr< TokenizerOperator >)
void write_JSON_body(JsonWriter &) const override
Tokenizer(const Shape &={}, const string &="tokenizer", VariableRole=VariableRole::Input)
bool allows_bf16_input_cast(size_t) const noexcept override
Definition tokenizer_layer.h:24
const TokenizerOperator * get_tokenizer() const noexcept
Definition tokenizer_layer.h:29
Index get_vocabulary_size() const
Definition tokenizer_layer.h:35
Definition configuration.h:10
VariableRole
Definition variable.h:72
@ Input
Definition variable.h:74
constexpr bool is_one_of(const T &value, const Candidates &... candidates)
Definition opennn_types.h:273
Definition tensor_types.h:172