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

67 torch.compile tests #81

Merged
merged 4 commits into from
Dec 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions tests/test_torch_compile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
"""

Tests for torch.compile

References:
- https://pytorch.org/tutorials/intermediate/torch_compile_tutorial.html
- https://github.com/pytorch/pytorch/issues/122094

"""

# global modules
import unittest

import torch

# Local modules
from torchsurv.loss.cox import neg_partial_log_likelihood as cox
from torchsurv.loss.weibull import neg_log_likelihood as weibull

# set seed for reproducibility
torch.manual_seed(42)

N = 512


class TestTorchCompile(unittest.TestCase):
"""
Tests using torch.compile with cox
"""

def test_cox_equivalence(self):
"""
whether the compiled version of cox evaluates to the same value
"""

# random data and parameters
log_hz = torch.randn(N)
event = torch.randint(low=0, high=2, size=(N,)).bool()
time = torch.randint(low=1, high=100, size=(N,))

# compiled version of cox
ccox = torch.compile(cox)

loss_cox = cox(log_hz, event, time)
loss_ccox = ccox(log_hz, event, time)

self.assertTrue(torch.allclose(loss_cox, loss_ccox, rtol=1e-3, atol=1e-3))

def test_weibull_equivalence(self):
"""
whether the compiled version of weibull evaluates to the same value
"""

# random data and parameters
log_hz = torch.randn(N)
event = torch.randint(low=0, high=2, size=(N,)).bool()
time = torch.randint(low=1, high=100, size=(N,))

# compiled version of weibull
cweibull = torch.compile(weibull)

loss_weibull = weibull(log_hz, event, time)
loss_cweibull = cweibull(log_hz, event, time)

self.assertTrue(
Copy link
Collaborator

Choose a reason for hiding this comment

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

@qiancao using Torch.allclose() to prevent converting to numpy. All good else!

torch.allclose(loss_weibull, loss_cweibull, rtol=1e-3, atol=1e-3)
)


if __name__ == "__main__":

unittest.main()
Loading