Skip to content

Fix: backend config file not taking effect due to inverted logic and identity comparison#87

Open
huangzhenhua111 wants to merge 1 commit intotensorlayer:mainfrom
huangzhenhua111:fix/backend-config
Open

Fix: backend config file not taking effect due to inverted logic and identity comparison#87
huangzhenhua111 wants to merge 1 commit intotensorlayer:mainfrom
huangzhenhua111:fix/backend-config

Conversation

@huangzhenhua111
Copy link
Copy Markdown

Bug Description

In tensorlayerx/backend/ops/load_backend.py, the config file ~/.tl/tl_backend.json
does not take effect. Users who change the backend via config file still get the
hardcoded default (tensorflow).

Root Cause

Two issues in lines 34-37:

1. Inverted logic

# Current code (WRONG):                                                                           
if load_dict['backend'] is not config['backend']:                                                 
    BACKEND = config['backend']      # user changed backend, but we ignore it!                    
else:                                                                                             
    BACKEND = load_dict['backend']   # values are equal, so this is same as default               

When the user configures a different backend from the default, the code
overwrites it with the hardcoded default instead of respecting the user's choice.

2. is not for string comparison

is not compares object identity (memory address), not value equality.
It should use !=.

Fix

if load_dict.get('backend') != config['backend']:                                                 
    BACKEND = load_dict['backend']                                                                
else:                                                                                             
    BACKEND = config['backend']                                                                   

Reproduce

  1. Install tensorlayerx in an environment with PyTorch but without TensorFlow
  2. Edit ~/.tl/tl_backend.json: {"backend": "torch"}
  3. import tensorlayerx → ModuleNotFoundError: No module named 'tensorflow'

Expected: should load PyTorch backend.

Note

The TL_BACKEND environment variable workaround still works (lines 39-43),
so users relying on that are not affected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant