Enforce import order to make protobuf cpp implementation in python work (#18560)
authorDmytro Dzhulgakov <dzhulgakov@fb.com>
Wed, 3 Apr 2019 20:12:28 +0000 (13:12 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Wed, 3 Apr 2019 20:17:08 +0000 (13:17 -0700)
commit3af2d6d904bf1fc6d14ceb7cf8eb7bb60148b679
tree8d9139406f29c8e55e5be74834c28fd75f701b2e
parent3b71f2e1f2c4cfb651208a938d9080cd0e061e46
Enforce import order to make protobuf cpp implementation in python work (#18560)

Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/18560

We have to import python protobuf here **before** we load cpp extension.
Otherwise it breaks under certain build conditions if cpp implementation of
protobuf is used. Presumably there's some registry in protobuf library and
python side has to initialize the dictionary first, before static
initialization in python extension does so. Otherwise, duplicated protobuf
descriptors will be created and it can lead to obscure errors like

  Parameter to MergeFrom() must be instance of same class: expected caffe2.NetDef got caffe2.NetDef.

I think it also fixes https://github.com/facebookarchive/caffe2/issues/1573

Reviewed By: ezyang, iroot900

Differential Revision: D14622054

fbshipit-source-id: 2499eb88ecdee85ff8d845859048f7ae5da2a480
caffe2/contrib/tensorboard/tensorboard_test.py
caffe2/proto/__init__.py
caffe2/python/_import_c_extension.py
caffe2/python/test/python_protobuf_test.py [new file with mode: 0644]