-
Notifications
You must be signed in to change notification settings - Fork 74k
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
Python 3.7 fixes #20766
Python 3.7 fixes #20766
Conversation
"async" became a reserved word. Replaced with "is_+async". Python C API now returns const for PyUnicode_AsUTF8AndSize and similar
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.
Thanks for the PR. Some questions/comments:
@@ -111,7 +111,7 @@ tensorflow::Status GetAllRemoteDevices( | |||
tensorflow::Status CreateRemoteContexts( | |||
const std::vector<string>& remote_workers, int64 rendezvous_id, | |||
const tensorflow::ServerDef& server_def, | |||
tensorflow::eager::EagerClientCache* remote_eager_workers, bool async, | |||
tensorflow::eager::EagerClientCache* remote_eager_workers, bool is_async, |
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.
If async
is a reserved word in Python 3.7, I don't quite follow why all these C files have to change :)
(c_api.cc
, c_api.h
, context.cc
, context.h
etc.)
Let's revert the changes to the C files?
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.
c_api.cc
and c_api.h
have to change because pywrap_tensorflow_internal.py
is generated from them, however I found that change should be rather minimal and touch only TFE_ContextOptionsSetAsync()
and TFE_ContextSetAsyncForThread()
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.
Shouldn't this be handled in pywrap_tfe.i
instead of changing the C 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.
You are right, this may be handled in pywrap_tfe.i
but from my small investigation I could solve this issue only with some sort of hack - define macro to redefine variable name, i.e. put
#define async is_async
into pywrap_tfe.i
. %rename
function didn't help because it works only with declarations but not with arguments.
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 managed to fix the syntax error by changing line 114 and 115, but in addition I needed to also change line 150 and 151 in the same way. That also contains the reserved word async
.
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.
Would writing a wrapper function in the SWIG file work?
(Not for the same reason, but we hide TF_SessionRun
from Python and instead use TF_SessionRun_wrapper
in
// We use TF_SessionRun_wrapper instead of TF_SessionRun |
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 thought of wrapping and I think it should work and it is more direct way than defining macro. But it will require watching for changes in tensorflow c api - watching for updated signatures of wrapped functions and for appearing async
parameter in functions.
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.
Isn't it just 2 functions that need to be wrapped?
The concerns around watching for updated signatures or changing signatures or new functions apply even if one were to change the C code, no?
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.
You are right, there are only 2 functions and one should watch for new signatures in case of updated C code anyway.
@@ -216,7 +216,7 @@ bool ParseStringValue(const string& key, PyObject* py_value, TF_Status* status, | |||
#if PY_MAJOR_VERSION >= 3 | |||
if (PyUnicode_Check(py_value)) { | |||
Py_ssize_t size = 0; | |||
char* buf = PyUnicode_AsUTF8AndSize(py_value, &size); | |||
char* buf = const_cast<char*>(PyUnicode_AsUTF8AndSize(py_value, &size)); |
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'd suggest we avoid the const_cast
and instead make buf
a const char*
:
const char* buf = PyUnicode_AsUTF8AndSize(py_value, &size);
@@ -154,7 +154,7 @@ Status PyBytesArrayMap(PyArrayObject* array, F f) { | |||
if (PyUnicode_Check(item.get())) { | |||
#if PY_VERSION_HEX >= 0x03030000 | |||
// Accept unicode by converting to UTF-8 bytes. | |||
ptr = PyUnicode_AsUTF8AndSize(item.get(), &len); | |||
ptr = const_cast<char*>(PyUnicode_AsUTF8AndSize(item.get(), &len)); |
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 comment here. Might have to change the f
provided at the call sites to use a const char*
instead, but seems like that should be easy.
@@ -352,7 +352,7 @@ Status ConvertNdarrayToTensor(PyObject* obj, Tensor* ret) { | |||
Py_ssize_t el_size; | |||
if (PyBytes_AsStringAndSize(input_data[i], &el, &el_size) == -1) { | |||
#if PY_MAJOR_VERSION >= 3 | |||
el = PyUnicode_AsUTF8AndSize(input_data[i], &el_size); | |||
el = const_cast<char*>(PyUnicode_AsUTF8AndSize(input_data[i], &el_size)); |
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.
Avoid const_cast
here too?
@@ -193,7 +193,7 @@ def distribute_dataset(self, dataset_fn): | |||
self._prefetch_on_device) | |||
|
|||
def _broadcast(self, tensor, destinations): | |||
# TODO(josh11b): In eager mode, use one thread per device, or async mode. |
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 change is also unnecessary as it's in a comment
This change only changes C++ files and one python comment to not use a python keyword. So nothing in here is necessary, so I'm closing this PR. |
@alextp - We'd still want to address the change in the return type of @MatthiasWinkelmann : Can you reduce the PR to handle just that change? |
Any update ? :D Edit: Patch Patch Patch Yea i know, it's dirty way but works :D |
Closing due to inactivity, and since #21202 seems like it subsumes this. |
…char*" instead of a "char*". (See https://docs.python.org/3/whatsnew/3.7.html#c-api-changes) There are additional changes needed for Python 3.7 compatibility, this change just pulls out one of them (and subsumes a related attempt in #21202 and #20766) Helps with #20517 PiperOrigin-RevId: 207008013
Reserved word:
async
"async" became a reserved word in Python 3.7. I replaced with "is_async", which was already used for such flags in other places. An alternative suggestion, if the
is_
construct is deemed too ugly, might be to use the full term, "asynchronous".Python C API changes
Python C API now returns
const
types for `PyUnicode_AsUTF8AndSize``` and similar. Fixed analogous to protocolbuffers/protobuf@0a59054.Fixes #20690