Skip to content

[Bug] Logical flaw in auto_control_deps_utils.py: Resource categorization fails for unsorted _read_only_resource_inputs #110516

Description

@amadhan882

Issue type

Bug

Have you reproduced the bug with TensorFlow Nightly?

Yes

Source

binary

TensorFlow version

2.19.0

Custom code

Yes

OS platform and distribution

Ubuntu 22.04.5 LTS (Jammy Jellyfish)

Mobile device

No response

Python version

3.12.12

Bazel version

No response

GCC/compiler version

No response

CUDA/cuDNN version

No response

GPU model and memory

No response

Current behavior?

The utility functions _get_read_only_resource_input_indices_op and get_read_write_resource_inputs in tensorflow/python/framework/auto_control_deps_utils.py fail to detect all read-only resources if the _read_only_resource_inputs attribute is unsorted.

The Logical Error

The implementation uses a pointer-based linear scan that assumes a strictly sorted order:

  1. It iterates through op.inputs using index i.
  2. It compares i == read_only_input_indices[read_only_index].
  3. If the attribute list is unsorted (e.g., [2, 0]), when the loop reaches index i=0, it compares 0 == 2.
  4. This returns False, so index 0 is skipped and misclassified as a Read-Write resource.
  5. The loop also contains a break condition: if read_only_index >= len(read_only_input_indices): break, which terminates the scan prematurely if the pointer logic doesn't find a sequential match.

Impact

This misclassification causes False Write Dependencies. TensorFlow's Automatic Control Dependencies (ACD) injects redundant control edges (locks) into the graph for resources that are actually read-only. This serializes operations that should run in parallel, creating a significant performance bottleneck in complex models.

Observed Output from Reproduction Script

  • Input Attribute Indices (Unsorted): [2, 0]
  • Detected Read-Only Indices: [2]
  • Status: Bug Confirmed. Index 0 was incorrectly marked as a "Write" operation.

Standalone code to reproduce the issue

import tensorflow as tf
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import auto_control_deps_utils as acd_utils

# Mocking an Operation-like object to demonstrate the utility flaw in isolation
class MockOp:
    def __init__(self, inputs, read_only_indices):
        self.inputs = inputs
        self.type = "MockOp"
        self._attrs = {"_read_only_resource_inputs": read_only_indices}
        
    def get_attr(self, name):
        if name in self._attrs: 
            return self._attrs[name]
        raise ValueError(f"Attribute {name} not found.")

def reproduce_acd_logic_bug():
    """
    Demonstrates how unsorted indices in _read_only_resource_inputs 
    cause the ACD utility to miss read-only resources.
    """
    print("--- Starting ACD Logic Reproduction ---")
    
    # Simulate 3 resource inputs at indices 0, 1, and 2
    mock_inputs = [tf.Variable(1.0).handle for _ in range(3)]
    
    # SCENARIO: Indices 0 and 2 are read-only, but the attribute list is unsorted: [2, 0]
    # This can happen via custom transformations or specific graph optimizations.
    unsorted_read_only_attr = [2, 0]
    mock_op = MockOp(mock_inputs, unsorted_read_only_attr)
    
    print(f"Input Resource Indices: [0, 1, 2]")
    print(f"Provided Read-Only Attr: {unsorted_read_only_attr}")

    # Calling the actual TensorFlow utility function
    detected = acd_utils._get_read_only_resource_input_indices_op(mock_op)
    
    print(f"Detected Read-Only Indices: {detected}")
    
    # Verification
    expected = [0, 2]
    missing = [idx for idx in expected if idx not in detected]
    
    if missing:
        print(f"\n[!!!] BUG CONFIRMED")
        print(f"The following read-only indices were MISSED: {missing}")
        print("Reason: The pointer-based linear scan failed to account for unsorted input.")
    else:
        print("\n[+] Logic check passed (No bug detected).")

if __name__ == "__main__":
    reproduce_acd_logic_bug()

Relevant log output

--- Starting ACD Logic Reproduction ---
Input Resource Indices: [0, 1, 2]
Provided Read-Only Attr: [2, 0]
Detected Read-Only Indices: [2]

[!!!] BUG CONFIRMED
The following read-only indices were MISSED: [0]
Reason: The pointer-based linear scan failed to account for unsorted input.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions