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

feat: Generalize edge type polymorphism. #170

Merged
merged 2 commits into from
Jan 30, 2025

Conversation

norhan-synnada
Copy link
Collaborator

Description

Closes #168
User can define polymorphic type declarations other than ToBeDetermined like Tensor[int | float] | int | float.

What is Changed

Include the changes introduced in this PR.

  • Default types of primitives updated.
  • set_value, set_type methods updated.
  • edge_type_constraint and some other constraints updated.
  • find_intersection_type function improved and updated for generic Tensor type handling.

Checklist:

  • Tests that cover the code added.
  • Corresponding changes documented.
  • All tests passed.
  • The code linted and styled (pre-commit run --all-files has passed).

Copy link

codecov bot commented Jan 26, 2025

Codecov Report

Attention: Patch coverage is 97.16981% with 6 lines in your changes missing coverage. Please review.

Project coverage is 88.81%. Comparing base (492eb34) to head (10bbbce).
Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
mithril/framework/common.py 95.94% 6 Missing ⚠️
@@            Coverage Diff             @@
##             main     #170      +/-   ##
==========================================
+ Coverage   88.79%   88.81%   +0.01%     
==========================================
  Files          61       61              
  Lines       15931    15964      +33     
==========================================
+ Hits        14146    14178      +32     
- Misses       1785     1786       +1     
Files with missing lines Coverage Δ
mithril/framework/codegen/numpy_gen.py 95.31% <ø> (-0.03%) ⬇️
mithril/framework/codegen/python_gen.py 96.38% <ø> (ø)
mithril/framework/constraints.py 94.59% <100.00%> (+0.11%) ⬆️
mithril/framework/logical/base.py 87.69% <100.00%> (+0.05%) ⬆️
mithril/framework/logical/essential_primitives.py 99.50% <ø> (ø)
mithril/framework/logical/model.py 97.24% <100.00%> (ø)
mithril/framework/logical/primitive.py 96.15% <100.00%> (ø)
mithril/framework/physical/data_store.py 95.23% <100.00%> (ø)
mithril/framework/physical/model.py 92.45% <100.00%> (ø)
mithril/framework/utils.py 89.03% <100.00%> (-2.07%) ⬇️
... and 6 more

... and 4 files with indirect coverage changes

@norhan-synnada norhan-synnada changed the title feat: Edge type polymorphism generalized. feat: Generalize edge type polymorphism. Jan 26, 2025
# )
def __post_init__(self) -> None:
# Convert to generic Tensor type if Tensor type is provided.
if self.type is Tensor:
Copy link
Collaborator

Choose a reason for hiding this comment

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

could we remove post_init and handle Tensor in set_type

@@ -192,29 +189,36 @@ def edge_type_constraint(
) -> ConstrainResultType:
updates = Updates()
status = False
tensor_exists: bool = False
tensor_output: bool = output.is_tensor
for input in inputs:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Small thing, but I think we can do tensor_exists check similar to status check in line 221.

tensor_exists =  any(input.is_tensor for input in inputs)

# If no ToBeDetermined edge_type exists, return True.
if ToBeDetermined not in input_edge_types:
# If no polymorphic edge_type exists, return True.
if all({not input.is_polymorphic for input in inputs}):
Copy link
Collaborator

Choose a reason for hiding this comment

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

  1. all and any supports comprehension directly. So we do not need to put it in a set first

  2. Since we are setting status in there already, I think there is no need to setting status occasionally inside the function. It seems to me it would be correct if we return as follows:

return all(not input.is_polymorphic for input in inputs), updates

# if physical_data.edge_type not in (Tensor, ToBeDetermined):
if not (
physical_data.is_tensor
or physical_data.edge_type is ToBeDetermined
Copy link
Collaborator

Choose a reason for hiding this comment

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

could this check be as follows?

if not physically_data.is_tensor or phsical_data.is_polymorphic

for s_types in (subtypes_1, subtypes_2):
other_set = subtypes_2 if s_types == subtypes_1 else subtypes_1
for orig_type in (list, tuple, range):
if orig_type in s_types:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not critical but, instead of using

 if orig_type in s_types: 
if orig_type not in s_types:  
    continue

could be used, which will look better.

@kberat-synnada kberat-synnada merged commit c6ddcfa into synnada-ai:main Jan 30, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FEATURE] Add support for generic tensor types for edge_type attribute of IOHyperEdge
4 participants