tensorflow / tensorflow/model-optimization

Incorrect inputs reordering inside `ModelTransformer._get_layers` during pattern matchin

Open
#1,179 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
1.6k
Forks
349
Avg merge
3d 2h
Merged PRs (30d)
1

Description

ModelTransformer._match_layer_with_inputs calls self._get_layers(input_layer_names). input_layer_names have strict order, i. e. _get_layers's result in this case must have same order of tensors as in input_layer_names.
Current implementation is:

  def _get_layers(self, layer_names):
    return [
        layer for layer in self._config['layers']
        if layer['config']['name'] in layer_names
    ]

I. e. when first input is declared later than the second one, result would have incorrect order. The simple model to reproduce bug:

import tf_keras as K
import tf_keras.layers as L
a = K.Input(10)
b = L.Dense(10)(a)
c = K.Input(20)
m = K.Model([a, c], L.concatenate([c, b], -1))

Then quantize_model(m) would yield incorrect order for concatenation operation.

My suggestion would be to replace it with something like:

  def _get_layers(self, layer_names):
    name_to_layer = {layer['config']['name']: layer for layer in self._config['layers']}
    return [name_to_layer[name] for name in layer_names]

which preserves order of layer_names

This also seems to be the problem behind #1061

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at ModelTransformer._match_layer_with_inputs and _get_layers, then run the supplied quantize_model reproduction with the two-input concatenation model. Trace the layer names and returned layers to confirm the declared input order is preserved; done means quantization produces the concatenation inputs in the same order as input_layer_names.

Written by the indexing model from the issue text.

Assessment

Tech stack
keras, python, tensorflow
Domain
machine-learning
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.