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

[BUG] "Expected all tensors to be on the same device" when not specifying sample_weight and using cuda/gpu version of HMM #1131

Open
tobemo opened this issue Feb 18, 2025 · 0 comments

Comments

@tobemo
Copy link

tobemo commented Feb 18, 2025

Describe the bug
When fitting an uninitialized HMM, and not specifying sample_weight I get a runtime error saying that not all tensors are on the same device.
The error occurs on line 262 of distributions/normal.py

    261 if self.covariance_type == 'full':
--> 262 	self._w_sum += torch.sum(sample_weight, dim=0)
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!

To Reproduce

Note that I'm not sure if this is the correct way of fitting an HMM using this package.

import pandas as pd
import numpy as np
import torch

from pomegranate.hmm import DenseHMM
from pomegranate.distributions import Normal

# taken from a hmmlearn example
# https://hmmlearn.readthedocs.io/en/latest/auto_examples/plot_poisson_hmm.html#sphx-glr-auto-examples-plot-poisson-hmm-py
X = pd.DataFrame(
    np.array([13, 14, 8, 10, 16, 26, 32, 27, 18, 32, 36, 24, 22, 23, 22, 18,
    25, 21, 21, 14, 8, 11, 14, 23, 18, 17, 19, 20, 22, 19, 13, 26,
    13, 14, 22, 24, 21, 22, 26, 21, 23, 24, 27, 41, 31, 27, 35, 26,
    28, 36, 39, 21, 17, 22, 17, 19, 15, 34, 10, 15, 22, 18, 15, 20,
    15, 22, 19, 16, 30, 27, 29, 23, 20, 16, 21, 21, 25, 16, 18, 15,
    18, 14, 10, 15, 8, 15, 6, 11, 8, 7, 18, 16, 13, 12, 13, 20,
    15, 16, 12, 18, 15, 16, 13, 15, 16, 11, 11])
)

X_ = torch.from_numpy(X.to_numpy())
X_ = X_.unsqueeze(0)  # add batch dimension
X_ = X_.cuda()
X_.shape
# torch.Size([1, 107, 1])

n_components = 3
model = DenseHMM([Normal()] * n_components, max_iter=100, verbose=True)
model = model.cuda()
assert model.device == X_.device

model.fit(X_)

When I use the below I get another error, for which I will open a separate issue.

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

No branches or pull requests

1 participant