-
Notifications
You must be signed in to change notification settings - Fork 43
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
Optimize CDF Calculation and Convert NumPy Arrays to Tensors in Benchmark #399
Conversation
@JasonKChow has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's still some functions that return numpy arrays (despite the documentation specifically saying otherwise). Example: in f_true method of Problem class.
Some typehints are wrong, example f_true method again.
The evaluate method can be rewritten entirely to use torch. Replace pearsonr with corrcoef for torch, you'll need to reformat the data.
Thank you, @JasonKChow, for the valuable feedback! I’ve made the following updates:
Please let me know if there is anything I need to change! |
@JasonKChow has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
aepsych/benchmark/problem.py
Outdated
@@ -233,12 +234,13 @@ def f_threshold(self, model=None) -> torch.Tensor: | |||
inverse_torch = model.likelihood.objective.inverse | |||
|
|||
def inverse_link(x): | |||
return inverse_torch(torch.tensor(x)) | |||
return inverse_torch(torch.tensor(x).clone().detach()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this cloned and detached? Also why is it converted to tensor if we already assume that self.thresholds will be a tensor?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, there's no need to convert it again, and cloning and detaching are also unnecessary. I'll remove those, test it, and commit the changes.
@JasonKChow has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator. |
@JasonKChow merged this pull request in 45d8e2d. |
PR Description
This PR addresses the first step in making AEPsych's functions consistently return PyTorch tensors and expect tensors as input, improving compatibility with GPUs and reducing redundant conversions between NumPy arrays and PyTorch tensors(partially solving #365).
Key changes include:
Conversion of
np.arrays
to tensors in the following files:aepsych/models/base.py
:p_below_threshold
method to operate fully with PyTorch tensors.norm.cdf()
withtorch.distributions.Normal(0, 1).cdf()
for better GPU compatibility.aepsych/benchmark/problem.py
:f_threshold()
now directly returns a PyTorch tensor, ensuring consistency.detach().cpu().numpy()
in places where thesuper().evaluate()
method returns float values, ensuring compatibility.Updates in
aepsych/tests/test_benchmark.py
:torch.mean()
,torch.square()
,torch.isclose()
, andtorch.all()
to fully align with tensor operations.Stability:
All test cases have passed successfully in the workflow.