Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2.8 backport] Resolve bug with re-saving Estimator-exported SavedModel #53676

Merged
merged 1 commit into from Jan 7, 2022

Conversation

k-w-w
Copy link
Contributor

@k-w-w k-w-w commented Jan 6, 2022

Change description

Sort signature inputs by input key instead of tensor name when loading a TF1 model in TF2.

Signatures are saved using nest.flatten, which sorts the keys alphabetically for determinism. This can cause a mismatch between the lifted signature function's arg_keywords and structured_input_signature, resulting in users not being able to re-save the signature.

Detailed example below.

Say there is a SavedModel signature with the inputs:

  inputs['a'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 1)
        name: input_2:0
    inputs['b'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 2)
        name: input_1:0

When this signature is loaded using the TF2 API, it becomes a concrete function with arg keywords = ['b', 'a'] (sorted by tensor name)

The structured_input_signature becomes ([], {'b': spec_for_b, 'a': spec_for_a})

When this function is exported as a signature in a new SavedModel, the structured inputs are flattened and zipped with the arg keywords. However, the flattened structured_input_signature is [spec_for_a, spec_for_b], since nest.flatten sorts the keys for determinism. This results in the wrong spec being mapped to the wrong arguments.

PiperOrigin-RevId: 417889747
Change-Id: Icdce72a43033027c18f5530068f2d6fd47e8e32d

…g a TF1 model in TF2.

Signatures are saved using nest.flatten, which sorts the keys alphabetically for determinism. This can cause a mismatch between the lifted signature function's arg_keywords and structured_input_signature, resulting in users not being able to re-save the signature.

Detailed example below.

Say there is a SavedModel signature with the inputs:
```
  inputs['a'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 1)
        name: input_2:0
    inputs['b'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 2)
        name: input_1:0
```

When this signature is loaded using the TF2 API, it becomes a concrete function with arg keywords = `['b', 'a']` (sorted by tensor name)

The `structured_input_signature` becomes `([], {'b': spec_for_b, 'a': spec_for_a})`

When this function is exported as a signature in a new SavedModel, the structured inputs are flattened and zipped with the arg keywords. However, the flattened `structured_input_signature` is `[spec_for_a, spec_for_b]`, since `nest.flatten` sorts the keys for determinism. This results in the wrong spec being mapped to the wrong arguments.

PiperOrigin-RevId: 417889747
Change-Id: Icdce72a43033027c18f5530068f2d6fd47e8e32d
@gbaned gbaned added this to Assigned Reviewer in PR Queue via automation Jan 7, 2022
PR Queue automation moved this from Assigned Reviewer to Approved by Reviewer Jan 7, 2022
@mihaimaruseac mihaimaruseac merged commit 7859210 into tensorflow:r2.8 Jan 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
PR Queue
  
Approved by Reviewer
Development

Successfully merging this pull request may close these issues.

None yet

2 participants