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

Refactor transformer decoder #36

Merged
merged 4 commits into from
May 3, 2024
Merged

Conversation

aaprasad
Copy link
Contributor

@aaprasad aaprasad commented May 1, 2024

Refactor transformer implementation as requested by #34.

  • emphasis on better variable names
  • remove confused code paths
  • store embeddings as a dictionary and always return that dictionary instead of conditionally returning None

Summary by CodeRabbit

  • Refactor
    • Improved type annotations in methods across transformer models for better code clarity and error checking.
  • Tests
    • Enhanced clarity and consistency in variable naming within transformer model test cases.

* use better variable names
* remove complicated code pathways
* save embeddings in a dictionary and always return that dictionary instead of sometimes returning None
Copy link
Contributor

coderabbitai bot commented May 1, 2024

Walkthrough

The changes involve refining type annotations in the transformer model classes, improving variable naming in test functions for clarity, and enhancing anomaly detection for debugging in the training tests.

Changes

Files Change Summary
biogtr/models/.../transformer.py Added type annotations and refined return types in transformer classes.
tests/test_models.py Improved variable naming for clarity in transformer test cases.

Poem

🐇💻 CodeRabbit nibbles on code with glee,
🌟 Typing annotations with a dance and a spree.
Variables renamed, clarity in sight,
Bugs beware, we'll win this fight.
Hopping through tests, errors we smudge,
Joyful coding, a carrot-filled fudge! 🥕🎉


Recent Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 74f5bf4 and 67315e7.
Files selected for processing (2)
  • biogtr/models/transformer.py (10 hunks)
  • tests/test_models.py (4 hunks)
Files skipped from review as they are similar to previous changes (1)
  • biogtr/models/transformer.py
Additional Context Used
Ruff (2)
tests/test_models.py (2)

314-314: Local variable img_shape is assigned to but never used


386-386: Local variable cfg is assigned to but never used

Additional comments not posted (6)
tests/test_models.py (6)

235-239: Refactor variable names for clarity.

The renaming of variables from src to queries and out to encoder_features in test_transformer_encoder improves readability and aligns with the PR objectives of improved variable naming.


242-246: Ensure consistent use of positional embeddings.

The addition of positional embeddings (pos_emb) in the test for the transformer encoder layer is a good practice for testing the handling of additional inputs.


265-269: Refactor variable names for clarity.

The renaming of variables from tgt and memory to decoder_queries and encoder_features in test_transformer_decoder enhances clarity and consistency with the encoder tests.


272-278: Ensure consistent use of positional embeddings.

The handling of positional embeddings (pos_emb and query_pos_emb) in the test for the transformer decoder layer is consistent and thorough, testing the decoder's ability to handle complex inputs.


344-352: Validate embedding handling in transformer tests.

The test test_transformer_embedding checks the embeddings dictionary returned by the transformer, ensuring that each type of embedding is correctly sized and labeled, which aligns with the PR's objective of consistent embedding handling.


398-406: Validate embedding handling in tracking transformer tests.

The test test_tracking_transformer effectively checks the embeddings dictionary for correct sizing and labeling, similar to test_transformer_embedding, ensuring consistency across different transformer configurations.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Out of diff range and nitpick comments (6)
tests/test_training.py (3)

Line range hint 9-9: Remove unused import GlobalTrackingTransformer.

- from biogtr.models.global_tracking_transformer import GlobalTrackingTransformer

Line range hint 10-10: Remove unused import DictConfig.

- from omegaconf import DictConfig

Line range hint 46-46: The variable feats is declared but never used in test_basic_gtr_runner.

Consider removing or using the variable if it was intended for future use.

tests/test_models.py (2)

Line range hint 314-314: The variable img_shape is declared but never used in test_transformer_basic.

Consider removing or using the variable if it was intended for future use.


Line range hint 386-386: The variable cfg is declared but never used in test_tracking_transformer.

Consider removing or using the variable if it was intended for future use.

biogtr/models/transformer.py (1)

45-45: Ensure the constructor's documentation is updated to reflect all parameters.

Consider updating the documentation to include all new parameters and their descriptions to maintain clarity and developer understanding.

@aaprasad aaprasad requested a review from talmo May 1, 2024 19:05
Copy link
Contributor

@talmo talmo left a comment

Choose a reason for hiding this comment

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

Looks amazing as-is, but would make some small changes


reid_features = reid_features.permute(
1, 0, 2
) # (total_instances x batch_size x embed_dim)
Copy link
Contributor

Choose a reason for hiding this comment

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

Use standard shape tuple syntax for documenting shapes:

Suggested change
) # (total_instances x batch_size x embed_dim)
) # (total_instances, batch_size, embed_dim)

# print(f'T: {window_length}; N: {total_instances}; N_t: {instances_per_frame} n_reid: {reid_features.shape}')
pred_box, pred_time = get_boxes_times(frames) # total_instances x 4

temp_emb = self.temp_emb(pred_time / window_length)
if self.return_embedding:
embeddings_dict["temp"] = temp_emb # .detach().cpu()
Copy link
Contributor

Choose a reason for hiding this comment

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

unused comment


pos_emb = self.pos_emb(pred_box)
if self.return_embedding:
embeddings_dict["pos"] = pos_emb # .detach().cpu()
Copy link
Contributor

Choose a reason for hiding this comment

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

unused comment


encoder_queries = reid_features

encoder_logits = self.encoder(
Copy link
Contributor

Choose a reason for hiding this comment

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

Are you sure these are logits? That term is usually reserved for the pre-softmax/sigmoid activations in a classification head.

Maybe encoder_features?

q,
k,
value=src,
attn_logits = self.self_attn(
Copy link
Contributor

Choose a reason for hiding this comment

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

attn_features?

Comment on lines 315 to 317
encoder_logits = self.norm2(queries)

return src
return encoder_logits
Copy link
Contributor

Choose a reason for hiding this comment

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

encoder_features?

@talmo talmo changed the title Aadi/refactor transformer decoder Refactor transformer decoder May 2, 2024
* replace use of `logits` with more appropriate `features`
* remove unnecessary comments
* standardize shape hinting format
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Out of diff range and nitpick comments (2)
tests/test_models.py (2)

Line range hint 314-314: Unused variable img_shape.

-    img_shape = (1, 100, 100)

The variable img_shape is defined but never used within the test_transformer_basic function. Consider removing it if it's not needed.


Line range hint 386-386: Unused variable cfg.

-    cfg = {"resnet18", "ResNet18_Weights.DEFAULT"}

The variable cfg is defined but not used in the test_tracking_transformer function. It appears to be a mistake since a dictionary is expected but a set is provided. If cfg is intended for use, correct the syntax and ensure it's utilized.

@aaprasad aaprasad merged commit 9027ca8 into main May 3, 2024
3 checks passed
@talmo talmo deleted the aadi/refactor-transformer-decoder branch May 14, 2024 21:58
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