Skip to content

Claude/fix cuda device selection 01 k6 e xnxy5o5 pn lsqbj vl23 g - #30

Open
nnknk0802 wants to merge 20 commits into
AI4HealthUOL:mainfrom
nnknk0802:claude/fix-cuda-device-selection-01K6EXnxy5o5PnLsqbjVL23G
Open

Claude/fix cuda device selection 01 k6 e xnxy5o5 pn lsqbj vl23 g#30
nnknk0802 wants to merge 20 commits into
AI4HealthUOL:mainfrom
nnknk0802:claude/fix-cuda-device-selection-01K6EXnxy5o5PnLsqbjVL23G

Conversation

@nnknk0802

Copy link
Copy Markdown

No description provided.

claude and others added 20 commits November 19, 2025 00:59
This commit introduces a clean interface for the SSSD-ECG diffusion model:

New Features:
- model_wrapper.py: SSSDECG class with simplified API
  - model(x, y) for training loss computation
  - model.generate() for sample generation
  - Built-in checkpoint save/load functionality

- dataset.py: Dedicated Dataset classes
  - ECGDataset: Generic ECG dataset class
  - PTBXLDataset: PTB-XL specific dataset
  - create_dataloaders(): Utility function for easy DataLoader creation

- train_new.py: Clean training script using new interface
  - Progress tracking with tqdm
  - Flexible configuration via command line or JSON
  - Better checkpoint management

- example_usage.py: Comprehensive usage examples
  - Basic training loop
  - Sample generation
  - Checkpoint operations
  - Real data handling

- README_REFACTORED.md: Japanese documentation
  - API reference
  - Usage examples
  - Troubleshooting guide

Benefits:
- Cleaner separation of concerns (data, model, training)
- Easier to use and extend
- Better code organization
- Original implementation remains unchanged
…ader-01FjNF9yHomAMfj5bjHrSeNF

Refactor SSSD-ECG: Separate data loading from model implementation
Create a standalone, self-contained implementation of SSSD-ECG that can be easily copied to other projects.

Structure:
- sssd_standalone/: Main package directory
  - model_wrapper.py: Clean API wrapper for SSSDECG model
  - dataset.py: Dataset classes (ECGDataset, PTBXLDataset)
  - models/: Model implementations (SSSD_ECG, S4Model)
  - utils/: Utility functions for diffusion, training, sampling
  - config/: Configuration file (config_SSSD_ECG.json)
  - examples/: Training and usage examples
  - README.md: Comprehensive documentation
  - requirements.txt: Dependencies

Features:
- Simple API for training and inference
- Modular design with separated components
- Well-documented with usage examples
- Easy to integrate into other projects
- Includes parameter configuration file
…-setup-01BqMf5XWmL8yFxJuHDcvdZ9

Add standalone SSSD-ECG implementation
- Replace pytorch_lightning.utilities.rank_zero_only with simple local implementation
- Add missing dependencies (einops, opt_einsum) to requirements.txt
- This makes the standalone implementation truly independent without heavy dependencies

The rank_zero_only decorator is now a simple pass-through function for standalone usage.
For distributed training, users can modify this decorator as needed.
…-setup-01BqMf5XWmL8yFxJuHDcvdZ9

Fix: Remove pytorch_lightning dependency from standalone implementation
Convert the Jupyter notebook ecg_data_preprocessing.ipynb to a reusable
Python module for creating PTB-XL dataloaders. This provides a cleaner
API for data loading and makes it easier to integrate PTB-XL data into
training pipelines.

Changes:
- Add ptbxl_dataloader.py: Main module with prepare_ptbxl_data(),
  create_ptbxl_dataloaders(), and get_ptbxl_dataloaders() functions
- Add example_ptbxl_dataloader.py: Example usage scripts demonstrating
  different use cases
- Add __init__.py: Package initialization for proper module structure
- Update README.md: Add documentation for using the new Python module
  alongside the existing Jupyter notebook option

The module supports:
- Multi-label classification with configurable label types
- Customizable sampling rates (100 Hz or 500 Hz)
- Train/val/test splits based on stratified folds
- Memory-efficient memmap data loading
- Flexible batch size and worker configuration
…-01EsSESzvsw2on8AHCaRCHgc

Claude/ptbxl dataloader creation 01 es se szvsw2on8 ah ca rc hgc
…ript

- Fix bug in prepare_ptbxl_data() where reformat_as_memmap() return value
  was not being captured, which could cause KeyError when loading data
- Add else branch to load already prepared data when recreate_data=False
- Add debug_dataloader.py script to help troubleshoot data loading issues
  and verify PTB-XL data preparation step-by-step

This should resolve the 'label_diag_filtered_numeric' KeyError that occurs
when the memmap DataFrame is not properly saved/loaded.
…-01EsSESzvsw2on8AHCaRCHgc

Fix: Properly handle reformat_as_memmap return value and add debug sc…
- Add check_dataframes.py to inspect saved DataFrames and compare df.pkl vs df_memmap.pkl
- Add ptbxl_dataloader_v2.py with workaround that uses df_mapped directly
  instead of loading via load_dataset(), which may lose label columns
- V2 includes detailed logging at each step to help identify where issues occur
…-01EsSESzvsw2on8AHCaRCHgc

Add DataFrame checker and v2 dataloader with workaround
When min_cnt=0, prepare_data_ptb_xl() does not create *_filtered_numeric
columns, only *_numeric columns. This caused KeyError when trying to access
label_diag_filtered_numeric.

Changes:
- Both ptbxl_dataloader.py and ptbxl_dataloader_v2.py now check if
  *_filtered_numeric columns exist
- If not found, fall back to *_numeric columns
- Add informative error messages showing available label columns

This allows users to use min_cnt=0 to include all labels without filtering,
or min_cnt>0 to filter rare labels.
…-01EsSESzvsw2on8AHCaRCHgc

Fix: Support min_cnt=0 by using unfiltered label columns
This commit adds a new generate_jit() method to the SSSDECG model wrapper
that uses PyTorch's JIT compilation to speed up inference.

Changes:
- Added _check_jit_availability() to detect torch.compile or torch.jit
- Added _sampling_label_jit() as JIT-compatible sampling function
- Added generate_jit() method with same interface as generate()
- Model is compiled on first call and cached for subsequent calls
- Automatically falls back to regular generate() if JIT unavailable
- Updated both src/sssd/model_wrapper.py and sssd_standalone/model_wrapper.py
- Added test_generate_jit.py to test the new functionality

Benefits:
- Significantly faster inference after initial compilation
- Supports PyTorch 2.0+ torch.compile and older torch.jit
- Backward compatible - falls back gracefully if JIT not available
- No changes needed to existing generate() usage
…QS5PbZSk9QawwuX4

Add JIT-compiled generate_jit method for faster inference
Previously, the model was hardcoded to use cuda:0 only. Multiple
`.cuda()` calls throughout the codebase prevented users from
specifying alternative CUDA devices like cuda:1.

Changes:
- Modified util.py functions to accept device parameter:
  * std_normal(): Added device parameter, changed .cuda() to .to(device)
  * calc_diffusion_step_embedding(): Added device parameter with auto-detection
  * sampling_label(): Added device parameter for tensor creation
  * training_loss_label(): Added device parameter for tensor creation

- Updated model_wrapper.py to pass device to utility functions in:
  * forward(): Pass device to training_loss_label()
  * generate(): Pass device to sampling_label()

- Updated SSSD_ECG.py Residual_group.forward() to pass device parameter
  to calc_diffusion_step_embedding()

- Applied changes to both src/sssd/ and sssd_standalone/ directories

Usage:
  # Use cuda:0 (first GPU)
  model = SSSDECG(config_path="config.json", device="cuda:0")

  # Use cuda:1 (second GPU)
  model = SSSDECG(config_path="config.json", device="cuda:1")

  # Use CPU
  model = SSSDECG(config_path="config.json", device="cpu")

All changes are backward compatible. Default behavior unchanged.

Added documentation (CUDA_DEVICE_SELECTION.md) and test script
(test_cuda_devices.py) to verify functionality across devices.
…ction

Improvements:
- Added device validation in __init__ to catch invalid device selections
  before attempting tensor operations
- Check if CUDA is available when requested
- Validate device index is within available range
- Provide clear error messages listing available devices

New helper methods:
- list_available_devices(): Returns list of available device strings
- print_available_devices(): Prints detailed device information

Enhanced documentation:
- Added "Checking Available Devices" section
- Added comprehensive "Troubleshooting" section covering:
  * Invalid device ordinal errors
  * CUDA not available errors
  * CUDA_VISIBLE_DEVICES usage
  * Device assignment verification

This prevents the cryptic "CUDA error: invalid device ordinal" that
occurred when specifying a non-existent device (e.g., cuda:1 when only
cuda:0 is available).

Example error message before:
  RuntimeError: CUDA error: invalid device ordinal

Example error message after:
  RuntimeError: CUDA device 'cuda:1' requested but only 1 device(s) available.
  Available devices: cuda:0, cpu
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