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(backend): condition combinators #5405

Closed

Conversation

Udiknedormin
Copy link
Contributor

Description of your changes:

Addresses issue #5404.

  1. Changes ConditionOperator and PipelineParamTuple into attr classes.
  2. Overwriting ConditionOperator's __bool__ method to produce a warning in case of using and or or on it (as it's probably not what the user intended to do)
  3. Defining __and__ and __or__ operators on ConditionOperator, which generate a tree.
  4. Handling trees of ConditionOperator in the compiler.
  5. Optimising braces for cases where they actually decrease readability (multiple operators of the same kind)

Checklist:

Please consider cherry-picking this change.

@google-oss-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
To complete the pull request process, please assign chensun after the PR has been reviewed.
You can assign the PR to them by writing /assign @chensun in a comment when ready.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@google-oss-robot
Copy link

Hi @Udiknedormin. Thanks for your PR.

I'm waiting for a kubeflow member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@Tomcli
Copy link
Member

Tomcli commented Mar 31, 2021

/ok-to-test

@Tomcli
Copy link
Member

Tomcli commented Mar 31, 2021

This PR also addresses the and and or logical operators for issue #482

"If operator combinator was meant, use & or | instead."
])
warnings.warn(msg)
return super().__bool__()
Copy link
Member

Choose a reason for hiding this comment

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

Looks like you didn't assign a parent class to ConditionOperator?

Copy link
Contributor Author

@Udiknedormin Udiknedormin Apr 6, 2021

Choose a reason for hiding this comment

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

It's inherited from object.

Copy link
Member

@Tomcli Tomcli Apr 6, 2021

Choose a reason for hiding this comment

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

The build is failing with these errors, so probably the object didn't inherent correctly

Traceback (most recent call last):
  File "/home/prow/go/src/github.com/kubeflow/pipelines/sdk/python/kfp/compiler/v2_compatible_compiler_test.py", line 93, in test_two_step_pipeline
    self._assert_compiled_pipeline_equals_golden(
  File "/home/prow/go/src/github.com/kubeflow/pipelines/sdk/python/kfp/compiler/v2_compatible_compiler_test.py", line 59, in _assert_compiled_pipeline_equals_golden
skipped 9 lines unfold_more
  File "/home/prow/go/src/github.com/kubeflow/pipelines/sdk/python/kfp/dsl/_pipeline_param.py", line 32, in __bool__
    return super().__bool__()
AttributeError: 'super' object has no attribute '__bool__

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks like you're right, so it's not inheritance, but some magic instead. I'll just replace it with True then. Good catch!

Comment on lines +21 to +23
operator = attr.ib(type=str)
operand1 = attr.ib(type=Union['PipelineParam', Any])
operand2 = attr.ib(type=Union['PipelineParam', Any])
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we use normal __init__ or typed class variables?
What is the advantage of using attrs over the standard Python typing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What is the advantage of using attrs

It provides __str__, __repr__, __eq__, __hash__ and lots of other utilities out of the box.

Can we use normal __init__

Normal __init__ will provide neither of the good stuff mentioned above.

or typed class variables?

We don't use any class variables here, these are all instance variables. Class variables are introduced via ClassVar[T].

import warnings

@attr.s
class ConditionOperator:
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we name this BinaryPredicate?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure! I just wanted to change as little as possible, but if you're ok with that --- I'd actually prefer that. Especially considering that UnaryPredicate would be useful too (e.g. for __not__).

def __and__(self, other) -> 'ConditionOperator':
return ConditionOperator('&&', self, other)

def __or__(self, other) -> 'ConditionOperator':
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we support not?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, it would require either:

  • ignoring one of the arguments (probably setting it to None?), like postfix operators in C++
  • creating a separate class UnaryOperator (and rename this one to BinaryOperator)
  • changing operand1 and operand2 into a sequence/list/ordered-iterable of operands

import kfp.dsl as dsl


class FlipCoinOp(dsl.ContainerOp):
Copy link
Contributor

Choose a reason for hiding this comment

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

We should not directly use ContainerOp in new code

@kfp.components.create_component_from_func
def flip_coin_op() -> str:
    import random
    return random.choice(["heads", "tails"])

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's an example based on flip-coin. Should we change flip-coin too then?

file_outputs={'output': '/tmp/output'})


class PrintOp(dsl.ContainerOp):
Copy link
Contributor

Choose a reason for hiding this comment

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

We should not directly use ContainerOp in new code

@kfp.components.create_component_from_func
def print_op(msg: str):
    print(msg)

object doesn't implement __bool__, it's some internal magic instead

therefore, super().__bool__() cannot be used
@google-oss-robot
Copy link

@Udiknedormin: The following tests failed, say /retest to rerun all failed tests:

Test name Commit Details Rerun command
kubeflow-pipelines-sdk-python38 2245bab link /test kubeflow-pipelines-sdk-python38
kubeflow-pipelines-sdk-python39 2245bab link /test kubeflow-pipelines-sdk-python39
kubeflow-pipelines-sdk-python37 2245bab link /test kubeflow-pipelines-sdk-python37
kubeflow-pipelines-sdk-python36 2245bab link /test kubeflow-pipelines-sdk-python36
kubeflow-pipeline-e2e-test 2245bab link /test kubeflow-pipeline-e2e-test

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here.

@stale
Copy link

stale bot commented Mar 2, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the lifecycle/stale The issue / pull request is stale, any activities remove this label. label Mar 2, 2022
@rimolive
Copy link
Member

Closing this PR. No activity for more than a year.

/close

@stale stale bot removed the lifecycle/stale The issue / pull request is stale, any activities remove this label. label Mar 24, 2024
@google-oss-prow google-oss-prow bot closed this Mar 24, 2024
Copy link

@rimolive: Closed this PR.

In response to this:

Closing this PR. No activity for more than a year.

/close

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

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.

5 participants