Skip to content

Fix tensor device mismatch and improve dataset module usability#4

Merged
fffasttime merged 5 commits into
fffasttime:mainfrom
Ziad-Ijja:main
Jun 1, 2026
Merged

Fix tensor device mismatch and improve dataset module usability#4
fffasttime merged 5 commits into
fffasttime:mainfrom
Ziad-Ijja:main

Conversation

@Ziad-Ijja

Copy link
Copy Markdown

This pull request focuses on improving data path handling, file saving/loading robustness, device compatibility, and configuration merging across the codebase. The main themes are: (1) making data and model file paths more robust and portable, (2) ensuring tensor indexing is device-aware, and (3) enhancing configuration merging logic.

Data and Model Path Handling Improvements:

  • Updated data loading in cnn_exp.py and lenet_cifar.py to dynamically determine the dataset directory using either the DATASETS environment variable or a path relative to the script, improving portability and flexibility. [1] [2]
  • Changed model saving and loading in both cnn_exp.py and lenet_cifar.py to use absolute paths based on the script's directory, ensuring consistency regardless of the working directory. [1] [2] [3]
  • Added dataset to the list of packages in pyproject.toml to ensure it is included during packaging and installation.
  • In imagenet.py, expanded the user path in the default testset folder location to support ~ for the home directory.

Device-Aware Tensor Indexing:

  • In mrfi.py, ensured that index tensors (error_list) used for activation and weight fault injection are moved to the same device as the target tensors, preventing device mismatch errors during tensor operations. [1] [2] [3] [4]

Configuration Merging Enhancement:

  • Improved configuration handling in the MRFI class by merging user-provided configuration dictionaries into a default configuration tree, allowing for more flexible and robust configuration overrides.

Ziad-Ijja added 3 commits May 28, 2026 10:03
- Add `dataset` to packages in `pyproject.toml` and create `dataset/__init__.py` so it can be installed and imported globally.
- Update `dataset/lenet_cifar.py` and `dataset/cnn_exp.py` to use dynamic absolute paths (via `__file__`) for the `_data` directory and `.pth` weight files instead of hardcoded relative paths.
- Add `os.path.expanduser` to `dataset/imagenet.py` to properly resolve paths containing the `~` shortcut.
…igTree YAML files

- Update `MRFI.__init__` to properly attach sub-modules when reading configuration from a raw string/YAML file format (`ConfigTree`).
- Merge the user-provided YAML configuration with a complete empty configuration tree dynamically generated from the model structue. This prevents `AttributeError` for missing `FI_config` keys during forward passes and weight fetching when using incomplete YAML configuration files.
@fffasttime

Copy link
Copy Markdown
Owner

Thank you very much. I'll read this later.

@fffasttime

Copy link
Copy Markdown
Owner

Nice pull request. This will be more convenient. There are small issues.

Tensor device operation

error_idx = error_list.to(weight.device) 

cause test failing. Maybe

if isinstance(error_list, torch.Tensor): 
     error_list= error_list.to(weight.device)

will pass.

  1. You added dataset to base python package. It's a common name and pip install MRFI && python -c import dataset may confict with exist package or files. I think making a global replacement to mrfi_dataset or mrfi.dataset will better.

If you later pass test by pytest --cov=mrfi, please change version to 2.1.0 in pyproject.toml. I'll merge and deploy this as mrfi==2.1.0.

@Ziad-Ijja

Copy link
Copy Markdown
Author

Thanks for the review. I've addressed both issues:

  • added a type check before moving error_list to the target device [1]
  • renamed the dataset package to avoid potential conflicts [2]

Tests are passing.

I also changed the version to 2.1.0

@fffasttime fffasttime merged commit 704b176 into fffasttime:main Jun 1, 2026
0 of 3 checks passed
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.

2 participants