[go: nahoru, domu]

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

Python 3.7 fixes #20766

Closed
wants to merge 1 commit into from
Closed

Python 3.7 fixes #20766

wants to merge 1 commit into from

Conversation

MatthiasWinkelmann
Copy link
Contributor
@MatthiasWinkelmann MatthiasWinkelmann commented Jul 13, 2018

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

"async" became a reserved word. Replaced with "is_+async". 
Python C API now returns const for PyUnicode_AsUTF8AndSize and similar
@qlzh727 qlzh727 requested review from akshaym, alextp and asimshankar and removed request for akshaym July 13, 2018 14:46
@qlzh727 qlzh727 added the awaiting review Pull request awaiting review label Jul 13, 2018
Copy link
Contributor
@asimshankar asimshankar left a 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,
Copy link
Contributor

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?

Copy link

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()

Copy link
Contributor

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?

Copy link

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.

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.

Copy link
Contributor

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
)

Copy link

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.

Copy link
Contributor

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?

Copy link

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));
Copy link
Contributor

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));
Copy link
Contributor

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));
Copy link
Contributor

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.
Copy link
Contributor

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

@alextp
Copy link
Contributor
alextp commented Jul 13, 2018

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 alextp closed this Jul 13, 2018
@asimshankar
Copy link
Contributor

@alextp - We'd still want to address the change in the return type of PyUnicode_AsUTF8AndSize in 3.7. So reopening the PR.

@MatthiasWinkelmann : Can you reduce the PR to handle just that change?

@asimshankar asimshankar reopened this Jul 13, 2018
@tensorflowbutler tensorflowbutler removed the awaiting review Pull request awaiting review label Jul 14, 2018
@TheAifam5
Copy link
TheAifam5 commented Jul 16, 2018

Any update ? :D

Edit:
If someone does not want to wait...

Patch _pywrap_tensorflow_internal.pyd (its PE file) strings in Lib\site-packages\tensorflow\python using https://hexed.it/
python36 -> python37 (single match)
64_90 -> 64_92 (multiple matches)

Patch build_info.py strings in Lib\site-packages\tensorflow\python\platform
64_90 -> 64_92 (single match)
9.0 -> 9.2 (single match)

Patch pywrap_tensorflow_internal.py in Lib\site-packages\tensorflow\python:
async) -> is_async) (should be only 4 matches)

Yea i know, it's dirty way but works :D

@alextp alextp added the kokoro:force-run Tests on submitted change label Jul 24, 2018
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Jul 24, 2018
@asimshankar
Copy link
Contributor

Closing due to inactivity, and since #21202 seems like it subsumes this.

tensorflow-copybara pushed a commit that referenced this pull request Aug 1, 2018
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

10 participants