Skip to content

Commit

Permalink
[tflchef] Revise cook with symbol_table(s) (#13643)
Browse files Browse the repository at this point in the history
This will revise cook method to have symbol_table as parameter instead
of return value and symbol_tables as ModelChef member.

ONE-DCO-1.0-Signed-off-by: SaeHie Park <[email protected]>
Co-authored-by: Hyukjin Jeong <[email protected]>
  • Loading branch information
seanshpark and jinevening authored Aug 12, 2024
1 parent 6fafda7 commit e9d8711
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions compiler/tflchef/core/src/ModelChef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ class ModelChef
void init(void);
void cook(const ::tflchef::ModelRecipe &model_recipe);

template <typename T> std::map<std::string, int32_t> cook_graph(const T &graph);
template <typename T>
void cook_graph(const T &graph, std::map<std::string, int32_t> &symbol_table);

public:
const char *get_buffer_pointer(void) const;
Expand All @@ -201,6 +202,10 @@ class ModelChef
std::vector<flatbuffers::Offset<::tflite::SubGraph>> _subgraph_vec;
std::map<tflite::BuiltinOperator, int32_t> _builtin_code_map;
std::vector<std::string> _custom_code_vec;
// _symbol_tables stores symbol_table of each sub graph
// this is used to find tensor ID(index) with tensor name
std::vector<std::map<std::string, int32_t>> _symbol_tables;

std::string _graph_name;
};

Expand Down Expand Up @@ -245,10 +250,13 @@ make_dim_metadata_vec(flatbuffers::FlatBufferBuilder *flatbuffer_builder, int32_
return dim_metadata_vec;
}

template <typename T> std::map<std::string, int32_t> ModelChef::cook_graph(const T &graph)
template <typename T>
void ModelChef::cook_graph(const T &graph, std::map<std::string, int32_t> &symbol_table)
{
LOGGER(l);

assert(symbol_table.empty()); // FIX_CALLER_UNLESS

// Operand-related
std::vector<flatbuffers::Offset<::tflite::Tensor>> tensor_vec;

Expand All @@ -260,9 +268,6 @@ template <typename T> std::map<std::string, int32_t> ModelChef::cook_graph(const
if (graph.has_name())
graph_name = graph.name();

// Tensor Name -> Tensor ID mapping (per Graph)
std::map<std::string, int32_t> symbol_table;

auto lookup = [&symbol_table, &graph_name](const std::string &name) {
if (symbol_table.find(name) != symbol_table.end())
return symbol_table.at(name);
Expand Down Expand Up @@ -699,8 +704,6 @@ template <typename T> std::map<std::string, int32_t> ModelChef::cook_graph(const
subgraph_builder.add_name(name);

_subgraph_vec.emplace_back(subgraph_builder.Finish());

return symbol_table;
}

void ModelChef::cook(const ::tflchef::ModelRecipe &model_recipe)
Expand Down Expand Up @@ -755,17 +758,15 @@ void ModelChef::cook(const ::tflchef::ModelRecipe &model_recipe)
_buffer_vec.emplace_back(buffer_builder.Finish());
}

// symbol_tables stores symbol_table of each sub graph
// this is used to find tensor ID(index) with tensor name
std::vector<std::map<std::string, int32_t>> symbol_tables;

//
// Create Main graph
//

_graph_name = "main";
auto table = cook_graph<::tflchef::ModelRecipe>(model_recipe);
symbol_tables.push_back(table);
// Tensor Name -> Tensor ID mapping (per Graph)
std::map<std::string, int32_t> symbol_table;
cook_graph<::tflchef::ModelRecipe>(model_recipe, symbol_table);
_symbol_tables.push_back(symbol_table);

//
// Create subgraphs if exist
Expand All @@ -779,8 +780,9 @@ void ModelChef::cook(const ::tflchef::ModelRecipe &model_recipe)

_graph_name = stringStream.str();

auto table = cook_graph<::tflchef::Graph>(graph);
symbol_tables.push_back(table);
symbol_table.clear();
cook_graph<::tflchef::Graph>(graph, symbol_table);
_symbol_tables.push_back(symbol_table);
}

// Create Signature-Def
Expand All @@ -799,8 +801,8 @@ void ModelChef::cook(const ::tflchef::ModelRecipe &model_recipe)
{
subgraph_index = rec_signature_def.subgraph_index();
}
assert(subgraph_index < symbol_tables.size());
auto &symbol_table = symbol_tables[subgraph_index];
assert(subgraph_index < _symbol_tables.size());
auto &symbol_table = _symbol_tables[subgraph_index];

// cook for inputs
for (int si = 0; si < rec_signature_def.inputs_size(); ++si)
Expand Down

0 comments on commit e9d8711

Please sign in to comment.