diff options
Diffstat (limited to 'compiler/tflchef/tflite/src/RecipeChef.cpp')
-rw-r--r-- | compiler/tflchef/tflite/src/RecipeChef.cpp | 132 |
1 files changed, 125 insertions, 7 deletions
diff --git a/compiler/tflchef/tflite/src/RecipeChef.cpp b/compiler/tflchef/tflite/src/RecipeChef.cpp index 0a26ae066..d9215a4c4 100644 --- a/compiler/tflchef/tflite/src/RecipeChef.cpp +++ b/compiler/tflchef/tflite/src/RecipeChef.cpp @@ -35,9 +35,16 @@ void set_inputs(TFliteImport *import, tflchef::Operation *operation, const tflit for (auto input : inputs) { - auto tensor = tensors->Get(input); - std::string name = tensor_name(tensor); - operation->add_input(name); + if (input == -1) + { + operation->add_input(""); + } + else + { + auto tensor = tensors->Get(input); + std::string name = tensor_name(tensor); + operation->add_input(name); + } } } @@ -103,16 +110,21 @@ std::unique_ptr<ModelRecipe> generate_recipe(const tflite::Model *model) operand->set_name(tensor_name(tensor)); operand->set_type(as_tflchef_type(tensor->type())); + operand->set_is_variable(tensor->is_variable()); - std::vector<int32_t> dims = as_index_vector(tensor->shape()); - ::tflchef::TensorShape *shape = operand->mutable_shape(); - for (auto dim : dims) + if (tensor->shape()) { - shape->add_dim(dim); + std::vector<int32_t> dims = as_index_vector(tensor->shape()); + ::tflchef::TensorShape *shape = operand->mutable_shape(); + for (auto dim : dims) + { + shape->add_dim(dim); + } } // filler for weights, bias and so on std::vector<int32_t> expvalues; + std::vector<float> expfvalues; if (tflite_import.get_tensor_filler(i)) { tflchef::TensorFiller *filler = operand->mutable_filler(); @@ -132,6 +144,17 @@ std::unique_ptr<ModelRecipe> generate_recipe(const tflite::Model *model) filler->add_arg(ss.str()); } } + else if (tflite_import.get_tensor_filler(i, expfvalues)) + { + tflchef::TensorFiller *filler = operand->mutable_filler(); + filler->set_tag("explicit"); + for (auto value : expfvalues) + { + std::ostringstream ss; + ss << value; + filler->add_arg(ss.str()); + } + } auto quant = tensor->quantization(); if (quant != nullptr) @@ -162,6 +185,101 @@ std::unique_ptr<ModelRecipe> generate_recipe(const tflite::Model *model) for (uint32_t idx = 0; idx < quant->zero_point()->size(); ++idx) chef_quant->add_zero_point(quant->zero_point()->Get(idx)); } + tflchef::TensorQuantization *chef_quant = operand->mutable_quant(); + chef_quant->set_quantized_dimension(quant->quantized_dimension()); + } + + auto sparsity = tensor->sparsity(); + if (sparsity != nullptr) + { + tflchef::TensorSparsity *chef_sparsity = operand->mutable_sparsity(); + // traversal_order + auto chef_traversal_order = chef_sparsity->mutable_traversal_order(); + for (const auto &to : *(sparsity->traversal_order())) + { + chef_traversal_order->add_dim(to); + } + // block_map + auto chef_block_map = chef_sparsity->mutable_block_map(); + for (const auto &bm : *(sparsity->block_map())) + { + chef_block_map->add_dim(bm); + } + // dim_metadata + for (const auto &dm : *(sparsity->dim_metadata())) + { + auto chef_dm = chef_sparsity->add_dim_metadata(); + // format + chef_dm->set_format(as_tflchef_sparse_dim_type(dm->format())); + // dense_size + chef_dm->set_dense_size(dm->dense_size()); + // array_segments + auto chef_array_segments = chef_dm->mutable_array_segments(); + switch (dm->array_segments_type()) + { + case tflite::SparseIndexVector_NONE: + // DO NOTHING + break; + case tflite::SparseIndexVector_Int32Vector: + for (const auto &as : *(dm->array_segments_as_Int32Vector()->values())) + { + chef_array_segments->add_dim(as); + } + break; + case tflite::SparseIndexVector_Uint16Vector: + for (const auto &as : *(dm->array_segments_as_Uint16Vector()->values())) + { + chef_array_segments->add_dim(as); + } + break; + case tflite::SparseIndexVector_Uint8Vector: + for (const auto &as : *(dm->array_segments_as_Uint8Vector()->values())) + { + chef_array_segments->add_dim(as); + } + break; + default: + throw std::runtime_error("unsupported sparse index vector type"); + } + // array_indices + auto chef_array_indices = chef_dm->mutable_array_indices(); + switch (dm->array_indices_type()) + { + case tflite::SparseIndexVector_NONE: + // DO NOTHING + break; + case tflite::SparseIndexVector_Int32Vector: + for (const auto &as : *(dm->array_indices_as_Int32Vector()->values())) + { + chef_array_indices->add_dim(as); + } + break; + case tflite::SparseIndexVector_Uint16Vector: + for (const auto &as : *(dm->array_indices_as_Uint16Vector()->values())) + { + chef_array_indices->add_dim(as); + } + break; + case tflite::SparseIndexVector_Uint8Vector: + for (const auto &as : *(dm->array_indices_as_Uint8Vector()->values())) + { + chef_array_indices->add_dim(as); + } + break; + default: + throw std::runtime_error("unsupported sparse index vector type"); + } + } + } + + auto shape_signature = tensor->shape_signature(); + if (shape_signature != nullptr) + { + tflchef::ShapeSignature *chef_shape_signature = operand->mutable_shape_signature(); + for (uint32_t i = 0; i < shape_signature->size(); ++i) + { + chef_shape_signature->add_dim(shape_signature->Get(i)); + } } } |