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

Commits on Jan 6, 2022

  1. Sort signature inputs by input key instead of tensor name when loadin…

    …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
    k-w-w committed Jan 6, 2022
    Copy the full SHA
    a7a884c View commit details
    Browse the repository at this point in the history