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

[SDK] fix grpc related bugs in Python SDK #2398

Merged
merged 16 commits into from
Aug 23, 2024

Conversation

Electronic-Waste
Copy link
Member

What this PR does / why we need it:

This PR fixes the bug described in issue #2395

Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):
Fixes #2395

Checklist:

  • Docs included if any changes are user facing

@Electronic-Waste
Copy link
Member Author

I passed all the UTs in my local cluster:

================================================================================ 1 passed in 2.32s =================================================================================
cp ./pkg/apis/manager/v1beta1/python/api_pb2.py ./sdk/python/v1beta1/kubeflow/katib/katib_api_pb2.py
cp ./pkg/apis/manager/v1beta1/python/api_pb2_grpc.py ./sdk/python/v1beta1/kubeflow/katib/katib_api_pb2_grpc.py
sed -i "s/api_pb2/kubeflow\.katib\.katib_api_pb2/g" ./sdk/python/v1beta1/kubeflow/katib/katib_api_pb2_grpc.py
pytest ./sdk/python/v1beta1/kubeflow/katib
=============================================================================== test session starts ================================================================================
platform linux -- Python 3.10.14, pytest-7.2.0, pluggy-1.5.0
rootdir: /home/ws/katib/sdk/python/v1beta1
collected 14 items                                                                                                                                                                 

sdk/python/v1beta1/kubeflow/katib/api/katib_client_test.py ........                                                                                                          [ 57%]
sdk/python/v1beta1/kubeflow/katib/api/report_metrics_test.py ......                                                                                                          [100%]

================================================================================ 14 passed in 0.49s ================================================================================
rm ./sdk/python/v1beta1/kubeflow/katib/katib_api_pb2.py ./sdk/python/v1beta1/kubeflow/katib/katib_api_pb2_grpc.py

But it failed in the CI/CD:

==================================== ERRORS ====================================
__________ ERROR collecting kubeflow/katib/api/report_metrics_test.py __________
ImportError while importing test module '/home/runner/work/katib/katib/sdk/python/v1beta1/kubeflow/katib/api/report_metrics_test.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/opt/hostedtoolcache/Python/3.11.9/x64/lib/python3.11/importlib/__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
sdk/python/v1beta1/kubeflow/katib/api/report_metrics_test.py:3: in <module>
    from kubeflow.katib import report_metrics
E   ImportError: cannot import name 'report_metrics' from 'kubeflow.katib' (/home/runner/work/katib/katib/sdk/python/v1beta1/kubeflow/katib/__init__.py)
=========================== short test summary info ============================
ERROR sdk/python/v1beta1/kubeflow/katib/api/report_metrics_test.py
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
=============================== 1 error in 0.60s ===============================

@andreyvelich Could you help me with figuring out what's wrong with the report_metrics?

@Electronic-Waste
Copy link
Member Author

Changes I made:

  1. Copy api_pb2_grpc.py to Python SDK and make some compatibility changes to setup.py and Makefile
  2. Refactor the logics concerned with grpc
  3. Fix a field error in katib_client.py
  4. Add UTs for report_metrics and get_trial_metrics

Signed-off-by: Electronic-Waste <[email protected]>
Signed-off-by: Electronic-Waste <[email protected]>
Signed-off-by: Electronic-Waste <[email protected]>
Signed-off-by: Electronic-Waste <[email protected]>
Signed-off-by: Electronic-Waste <[email protected]>
Signed-off-by: Electronic-Waste <[email protected]>
Signed-off-by: Electronic-Waste <[email protected]>
Signed-off-by: Electronic-Waste <[email protected]>
@Electronic-Waste
Copy link
Member Author

I passed all the UTs in my local cluster:

================================================================================ 1 passed in 2.32s =================================================================================
cp ./pkg/apis/manager/v1beta1/python/api_pb2.py ./sdk/python/v1beta1/kubeflow/katib/katib_api_pb2.py
cp ./pkg/apis/manager/v1beta1/python/api_pb2_grpc.py ./sdk/python/v1beta1/kubeflow/katib/katib_api_pb2_grpc.py
sed -i "s/api_pb2/kubeflow\.katib\.katib_api_pb2/g" ./sdk/python/v1beta1/kubeflow/katib/katib_api_pb2_grpc.py
pytest ./sdk/python/v1beta1/kubeflow/katib
=============================================================================== test session starts ================================================================================
platform linux -- Python 3.10.14, pytest-7.2.0, pluggy-1.5.0
rootdir: /home/ws/katib/sdk/python/v1beta1
collected 14 items                                                                                                                                                                 

sdk/python/v1beta1/kubeflow/katib/api/katib_client_test.py ........                                                                                                          [ 57%]
sdk/python/v1beta1/kubeflow/katib/api/report_metrics_test.py ......                                                                                                          [100%]

================================================================================ 14 passed in 0.49s ================================================================================
rm ./sdk/python/v1beta1/kubeflow/katib/katib_api_pb2.py ./sdk/python/v1beta1/kubeflow/katib/katib_api_pb2_grpc.py

But it failed in the CI/CD:

==================================== ERRORS ====================================
__________ ERROR collecting kubeflow/katib/api/report_metrics_test.py __________
ImportError while importing test module '/home/runner/work/katib/katib/sdk/python/v1beta1/kubeflow/katib/api/report_metrics_test.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/opt/hostedtoolcache/Python/3.11.9/x64/lib/python3.11/importlib/__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
sdk/python/v1beta1/kubeflow/katib/api/report_metrics_test.py:3: in <module>
    from kubeflow.katib import report_metrics
E   ImportError: cannot import name 'report_metrics' from 'kubeflow.katib' (/home/runner/work/katib/katib/sdk/python/v1beta1/kubeflow/katib/__init__.py)
=========================== short test summary info ============================
ERROR sdk/python/v1beta1/kubeflow/katib/api/report_metrics_test.py
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
=============================== 1 error in 0.60s ===============================

@andreyvelich Could you help me with figuring out what's wrong with the report_metrics?

@andreyvelich @johnugeorge @tenzen-y I've resolved this bug. PTAL👀

@Electronic-Waste
Copy link
Member Author

This PR is related to my GSoC project. ref issue: #2340

sdk/python/v1beta1/kubeflow/katib/api/katib_client_test.py Outdated Show resolved Hide resolved

with open("kubeflow/katib/katib_api_pb2_grpc.py", 'r+') as file:
content = file.read()
new_content = content.replace("api_pb2", "kubeflow.katib.katib_api_pb2")
Copy link
Member

Choose a reason for hiding this comment

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

Is there a way to avoid this tricky import change ?
Maybe we could add those protobuf files as part of SDK __init__.py ?
https://github.com/kubeflow/katib/blob/8b095add55d7dd8983eba3d3b4bab34d51dc2f35/sdk/python/v1beta1/kubeflow/katib/__init__.py

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, we can achieve this by not renaming those files, i.e. copy api_pb2.py to python SDK as api_pb2.py rather than katib_api_pb2.py.

Copy link
Member Author

Choose a reason for hiding this comment

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

@andreyvelich I think we may need to do this tricky import change, because adding import kubeflow.katib.api_pb2 as api_pb2 to __init__.py makes no difference in resolving the import error:

  1. __init__.py and api_pb2_grpc.py are different modules in this directory. Importing api_pb2 in __init__.py will not affect package import in api_pb2_grpc.py.
  2. Python3 has banned relative package import like import api_pb2. Python will search for api_pb2.py in the sys.path rather than the same directory. If we want to use relative import, we need to specify the package path like from . import <filename>.

Copy link
Member

Choose a reason for hiding this comment

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

I see that makes sense, @droctothorpe @tenzen-y @johnugeorge any thoughts ?
I guess, we still need to modify the import to make sure we import the api_pb2.py from kubeflow.katib package.

Copy link
Member

Choose a reason for hiding this comment

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

@Electronic-Waste This import error happened only in the CI, and you have not seen it in your local, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure,I'll investigate it.

Copy link
Member Author

@Electronic-Waste Electronic-Waste Aug 21, 2024

Choose a reason for hiding this comment

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

@tenzen-y @andreyvelich I've found a related issue describing this: protocolbuffers/protobuf#7061

The python_package option is not on the developing schedule up to now. So I think we can't avoid this hack approach now :)

Copy link
Member

@andreyvelich andreyvelich Aug 22, 2024

Choose a reason for hiding this comment

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

@Electronic-Waste Please can you add the TODO comment in your post-gen script to track this issue: protocolbuffers/protobuf#7061, so maybe in the future we can remove this import re-write.

Copy link
Member Author

Choose a reason for hiding this comment

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

@andreyvelich I've added the TODO comment to the post-gen scripts now :)

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for investigating this problem.

Signed-off-by: Electronic-Waste <[email protected]>
@andreyvelich
Copy link
Member

Please run pre-commit to fix lint errors.

@Electronic-Waste
Copy link
Member Author

@andreyvelich Lint error has been fixed now.

Copy link
Member

@andreyvelich andreyvelich left a comment

Choose a reason for hiding this comment

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

Thanks @Electronic-Waste!
/lgtm
/assign @tenzen-y @johnugeorge

Copy link
Member

@tenzen-y tenzen-y left a comment

Choose a reason for hiding this comment

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

Thanks.
/lgtm
/approve


with open("kubeflow/katib/katib_api_pb2_grpc.py", 'r+') as file:
content = file.read()
new_content = content.replace("api_pb2", "kubeflow.katib.katib_api_pb2")
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for investigating this problem.

Copy link

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: tenzen-y

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

The pull request process is described 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-prow google-oss-prow bot merged commit a524f33 into kubeflow:master Aug 23, 2024
63 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.

[SDK] grpc-related bugs in Python SDK
4 participants