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

Update transformer.py #210

Merged
merged 2 commits into from
Apr 25, 2024
Merged

Update transformer.py #210

merged 2 commits into from
Apr 25, 2024

Conversation

Miking98
Copy link
Collaborator

I get a numpy cuda / cpu conversion error due to the batches being moved to device before the collate() function.

Traceback (most recent call last):
  File "/local-scratch-nvme/nigam/ehrshot/lib/python3.10/pdb.py", line 1723, in main
    pdb._runscript(mainpyfile)
  File "/local-scratch-nvme/nigam/ehrshot/lib/python3.10/pdb.py", line 1583, in _runscript
    self.run(statement)
  File "/local-scratch-nvme/nigam/ehrshot/lib/python3.10/bdb.py", line 598, in run
    exec(cmd, globals, locals)
  File "<string>", line 1, in <module>
  File "/share/pi/nigam/mwornow/ehrshot-benchmark/ehrshot/4_generate_clmbr_features.py", line 123, in <module>
    results: Dict[str, Any] = compute_features(dataset, model_name, labels, ontology=None, num_proc=num_threads, tokens_per_batch=tokens_per_batch, device=device)
  File "/share/pi/nigam/mwornow/ehrshot-benchmark/ehrshot/4_generate_clmbr_features.py", line 69, in compute_features
    batch = processor.collate([batch])["batch"]
  File "/local-scratch-nvme/nigam/ehrshot/lib/python3.10/site-packages/femr/models/processor.py", line 401, in collate
    return {"batch": _add_dimension(self.creator.cleanup_batch(batches[0]))}
  File "/local-scratch-nvme/nigam/ehrshot/lib/python3.10/site-packages/femr/models/processor.py", line 322, in cleanup_batch
    batch["transformer"]["patient_lengths"] = np.array(batch["transformer"]["patient_lengths"])
  File "/local-scratch-nvme/nigam/ehrshot/lib/python3.10/site-packages/torch/_tensor.py", line 1062, in __array__
    return self.numpy()
TypeError: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.

This fixes that by waiting until after collate() to move to device.

Miking98 and others added 2 commits April 17, 2024 15:01
I get a numpy cuda / cpu conversion error due to the `batches` being moved to `device` before the `collate()` function.

```
Traceback (most recent call last):
  File "/local-scratch-nvme/nigam/ehrshot/lib/python3.10/pdb.py", line 1723, in main
    pdb._runscript(mainpyfile)
  File "/local-scratch-nvme/nigam/ehrshot/lib/python3.10/pdb.py", line 1583, in _runscript
    self.run(statement)
  File "/local-scratch-nvme/nigam/ehrshot/lib/python3.10/bdb.py", line 598, in run
    exec(cmd, globals, locals)
  File "<string>", line 1, in <module>
  File "/share/pi/nigam/mwornow/ehrshot-benchmark/ehrshot/4_generate_clmbr_features.py", line 123, in <module>
    results: Dict[str, Any] = compute_features(dataset, model_name, labels, ontology=None, num_proc=num_threads, tokens_per_batch=tokens_per_batch, device=device)
  File "/share/pi/nigam/mwornow/ehrshot-benchmark/ehrshot/4_generate_clmbr_features.py", line 69, in compute_features
    batch = processor.collate([batch])["batch"]
  File "/local-scratch-nvme/nigam/ehrshot/lib/python3.10/site-packages/femr/models/processor.py", line 401, in collate
    return {"batch": _add_dimension(self.creator.cleanup_batch(batches[0]))}
  File "/local-scratch-nvme/nigam/ehrshot/lib/python3.10/site-packages/femr/models/processor.py", line 322, in cleanup_batch
    batch["transformer"]["patient_lengths"] = np.array(batch["transformer"]["patient_lengths"])
  File "/local-scratch-nvme/nigam/ehrshot/lib/python3.10/site-packages/torch/_tensor.py", line 1062, in __array__
    return self.numpy()
TypeError: can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the tensor to host memory first.
```

This fixes that by waiting until after `collate()` to move to device.
@Miking98 Miking98 requested a review from EthanSteinberg April 25, 2024 09:54
Copy link
Contributor

@EthanSteinberg EthanSteinberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks!

@Miking98 Miking98 merged commit 0df7121 into main Apr 25, 2024
10 checks passed
@Miking98 Miking98 deleted the fix/numpy-error branch April 25, 2024 18:28
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