-
Notifications
You must be signed in to change notification settings - Fork 8
Refactor/unify terms for clarity #16
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0bb85b8
feat: Unify terminology - 'session' to 'run', 'history' to 'nodes'
yuxiang-wu 515229f
fix: Reduce termination report timeout for faster CLI exit
yuxiang-wu 5900d32
revert hello kernel world base script to optimize
DhruvSrikanth 8c6aa50
minor correction
DhruvSrikanth 31f8066
lint and format
DhruvSrikanth 2890e9b
add comments back
DhruvSrikanth 4c5ea49
Merge branch 'dev' into refactor/unify-terms-for-clarity
DhruvSrikanth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,28 +1,40 @@ | ||
| import torch | ||
| import torch.nn as nn | ||
|
|
||
| import torch.nn.functional as F | ||
|
|
||
| class Model(nn.Module): | ||
| """ | ||
| Model that performs a matrix multiplication, summation, and combined scaling. | ||
| Model that performs a matrix multiplication, summation, and combined scaling, | ||
| optimized by pre-computing the combined weight vector and using torch.linalg.vecdot. | ||
| Assumes torch.compile is applied externally. | ||
| """ | ||
|
|
||
| def __init__(self, input_size, hidden_size, scaling_factor): | ||
| super(Model, self).__init__() | ||
| # weight is (hidden_size, input_size) | ||
| self.weight = nn.Parameter(torch.randn(hidden_size, input_size)) | ||
| # Combine the division by 2 and the scaling factor into one operation | ||
| self.effective_scaling_factor = scaling_factor / 2.0 | ||
|
|
||
| # Pre-compute the combined weight vector and scaling factor | ||
| with torch.no_grad(): | ||
| summed_weight_vector = self.weight.sum(dim=0) # (input_size,) | ||
| effective_weight_vector = summed_weight_vector * (scaling_factor / 2.0) # (input_size,) | ||
| # Store as a buffer | ||
| self.register_buffer('effective_weight_vector', effective_weight_vector) # (input_size,) | ||
|
|
||
|
|
||
| def forward(self, x): | ||
| """ | ||
| Args: | ||
| x (torch.Tensor): Input tensor of shape (batch_size, input_size). | ||
| Returns: | ||
| torch.Tensor: Output tensor of shape (batch_size, hidden_size). | ||
| torch.Tensor: Output tensor of shape (batch_size, 1). | ||
| """ | ||
| # Original operations: matmul -> / 2 -> sum -> * scaling_factor | ||
| # Optimized operations: matmul -> sum -> * (scaling_factor / 2) | ||
| x = torch.matmul(x, self.weight.T) | ||
| x = torch.sum(x, dim=1, keepdim=True) | ||
| x = x * self.effective_scaling_factor | ||
| return x | ||
| # Perform the batched dot product using torch.linalg.vecdot | ||
| # x is (batch_size, input_size) -> (B, I) | ||
| # effective_weight_vector is (input_size,) -> (I) | ||
| # torch.linalg.vecdot(A (..., N), B (..., N) or (N)) -> (...) | ||
| # In our case: A is (B, I), B is (I). Result is (B,). | ||
| output = torch.linalg.vecdot(x, self.effective_weight_vector) | ||
|
|
||
| # Unsqueeze to match the required output shape (batch_size, 1) | ||
| return output.unsqueeze(1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.