-
Notifications
You must be signed in to change notification settings - Fork 7.2k
Migrate Python C extension to Cython #3541
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
Conversation
|
Test FAILed. |
|
Test PASSed. |
python/ray/includes/unique_ids.pxi
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not necessary to duplicate code for each id type. We can define one base class and have all other classes extend it. In the super class, we can leverage cls and self.__class__ to get the concrete type. Here is a short example.
class UniqueID(object):
def __init__(self, data):
self.data = data
@classmethod
def from_random(cls):
return cls(b'')
def __eq__(self, other):
return self.__class__ == other.__class__ and self.data == other.data
class TaskID(UniqueID):
pass
class ObjectID(UniqueID):
pass
id1 = TaskID(b'')
id2 = TaskID.from_random()
id3 = ObjectID(b'')
assert type(id1) == TaskID
assert type(id2) == TaskID
assert type(id3) == ObjectID
assert id1 == id2
assert id1 != id3There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, it's worth noticing that this is not real type safe. Because types in Python are dynamic. For example, if I have a function that is supposed to take a TaskID parameter, I can still pass in an ObjectID.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One reason I am defining those IDs as independent classes is because those C++ types could be strong-typedefed in the near future.
I have also tried to reuse some duplicated parts. Unfortunately, Cython cannot recognize classes from cls or __class__ in many cases. So I still need to write a lot of extra code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you see any other way to get rid of the code duplication? It would be good to resolve this before the PR is merged.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that all of these types will be different C++ classes in the future, so we should plan for that setting. It's also possible that they will have different methods, e.g, with ObjectID you could potentially call GetTaskID() or IsPut(), but that wouldn't make sense with an ActorID.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, also this of course doesn't mean they can't share code. I'm pretty sure with interitence it can be made to work well that the common methods are defined only once, I'll give it a shot.
|
Test FAILed. |
|
I have implemented all features in Cython. Some code still needs to be updated to fit the Cython interface. |
|
Test FAILed. |
|
Test FAILed. |
|
Test FAILed. |
python/ray/function_manager.py
Outdated
| function_id = ray.ObjectID(function_id_str) | ||
| driver_id = ray.ObjectID(driver_id_str) | ||
| function_id = ray.FunctionID(function_id_str) | ||
| # TODO(suquark): Make sure the type of "driver_id". |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the right type, do you want to remove the TODO?
JobID and DriverID are the same, and in the future I'd say we should transition to using DriverID exclusively. But given that in C++ it's called JobID, I think it's ok to call it JobID in python too until we change it everywhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, we actually use both DriverID and JobID in C++. I agree about using only one (e.g., DriverID).
python/ray/function_manager.py
Outdated
| class_name = decode(class_name) | ||
| module = decode(module) | ||
| driver_id = ray.ObjectID(driver_id_str) | ||
| # TODO(suquark): Ensure the type of "driver_id". |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as above
python/ray/includes/common.pxd
Outdated
| from libcpp.string cimport string as c_string | ||
| from libcpp cimport bool as c_bool | ||
|
|
||
| from libc.stdint cimport int64_t as c_int64, uint32_t as c_uint32, uint16_t as c_uint16, uint8_t as c_uint8 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any clashes or should we just import these as int64_t etc?
python/ray/includes/common.pxd
Outdated
| cdef extern from "flatbuffers/flatbuffers.h" namespace "flatbuffers": | ||
| # Cython cannot rename a template class. | ||
| cdef cppclass Offset[T]: | ||
| uoffset_t o |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this needed? Shall we call it offset instead?
|
Test PASSed. |
|
Test PASSed. |
|
Test PASSed. |
|
Test PASSed. |
|
Test PASSed. |
|
Test PASSed. |
|
Test PASSed. |
|
Test PASSed. |
141bc91 to
308985c
Compare
|
Test FAILed. |
|
Test PASSed. |
|
Test PASSed. |
|
Test PASSed. |
|
Test PASSed. |
|
Ready to merge. The CI errors come from the master branch. |
What do these changes do?
This PR migrates Python C extension to Cython bindings for the raylet client.
Related issue number
#3474
This also fixes #405