Skip to content

Conversation

@wchargin
Copy link
Contributor

@wchargin wchargin commented Oct 18, 2019

Summary:
This commit teaches tb_proto_library a new has_services flag. When
set, the proto library will be compiled with gRPC service support, and
an additional *_py_pb2_grpc py_library target will be created.

The py_proto_library macro provided by the protobuf build rules no
longer suits our purpose, as it can only create one *_py_pb2 build
target that includes both the *_pb2 and *_pb2_grpc Python modules.
This is inconsistent with TensorFlow and some Google internal rules. We
now directly use proto_gen rather than using py_proto_library. This
is fine: that macro is marked as unstable because its interface may
change, and our goal is precisely to change the interface.

Test Plan:
Run

bazel query --output=build \
    'filter(".*protos_all.*", //tensorboard/...:all)'

and note that the output is identical before and after this change, so
existing protos will be unchanged.

Then, add a simple service to tensorboard/plugins/hparams/api.proto:

service TestService {
  rpc ListSesssionGroups(ListSessionGroupsRequest)
      returns (ListSessionGroupsResponse);
}

Build the Pip package and install it into a new virtualenv. Try to
import tensorboard.plugins.hparams.api_pb2_grpc, but note that
this fails. Then, add has_services = True to the hparams BUILD file,
and rebuild and reinstall the Pip package. The import should still fail.
Finally, add a :protos_all_py_pb2_grpc dep to :hparams_plugin, and
try once more. The import should now work, and the imported module
should have a TestServiceStub attribute.

wchargin-branch: proto-grpc

Summary:
This commit teaches `tb_proto_library` a new `has_services` flag. When
set, the proto library will be compiled with gRPC service support. The
flag name and implementation are adapted from TensorFlow’s.

Indentation changes are due to buildifier.

Test Plan:
Append a simple service to `tensorboard/plugins/hparams/api.proto`:

```proto
service TestService {
  rpc ListSesssionGroups(ListSessionGroupsRequest)
      returns (ListSessionGroupsResponse);
}
```

Build the Pip package and install it into a new virtualenv. Try to
import `tensorboard.compat.proto.test_service_pb2_grpc`, but note that
this fails. Then, add `has_services = True` to the hparams `BUILD` file,
and rebuild and reinstall the Pip package: note that the import now
works, and the module has a `TestServiceStub` attribute.

wchargin-branch: proto-grpc
@davidsoergel
Copy link
Member

Can you confirm that this change works internally as well? It seems to me that the external py_proto_library knows about use_grpc_plugin, but I'm not clear that the internal one does. (Note TF redefines the py_proto_library rule, so copying that usage may not work as expected). If this works as is, great! But if not, maybe there is also a copybara thing needed too, etc. (?)

@davidsoergel
Copy link
Member

Oh nevermind, protos.bzl is only for external use anyway. LGTM

@wchargin
Copy link
Contributor Author

Right; this will have an accompanying internal change.

@wchargin
Copy link
Contributor Author

I will restructure this to more cleanly separate the _py_pb2 targets
from the _py_pb2_grpc targets. Currently, they’re in separate Python
modules but one build rule, which is not what TensorFlow does and is
also not what is used internally. TensorFlow hacks around this by
committing the generated Python code directly to the repository,
but I think that can do better than that.

wchargin-branch: proto-grpc
wchargin-source: 363716e75b3ce34a720c014d2c9a726afce54cf6
@wchargin
Copy link
Contributor Author

Can you confirm that this change works internally as well?

Thanks for this prompt—you’re correct that this protos.bzl is only
used in open-source TensorBoard, but the rules were still not as close
as I’d have liked them to be. Fixed; thanks! See http://cl/275570880.

@wchargin
Copy link
Contributor Author

Also cc @caisq, who has some experience with gRPC in open source. If
anything here seems obviously wrong to you (presumably there’s a reason
that TensorFlow doesn’t due this, but it might just be “historical”),
I’d love to hear it.

Copy link
Member

@davidsoergel davidsoergel left a comment

Choose a reason for hiding this comment

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

Seems reasonable. Also Cc @jameswex; IIRC Facets did the same thing (committing generated protos) so maybe this approach would apply there as well.

@caisq
Copy link
Contributor

caisq commented Oct 21, 2019

@wchargin The debugger plugin checks in generated Python code because at the time that was done (mid 2017), there was no py grpc genrule that worked both internally and externally yet.

@wchargin
Copy link
Contributor Author

Great; thanks, all.

@wchargin wchargin merged commit f823951 into master Oct 21, 2019
@wchargin wchargin deleted the wchargin-proto-grpc branch October 21, 2019 14:49
wchargin added a commit to wchargin/tensorboard that referenced this pull request Oct 29, 2019
Summary:
This commit teaches `tb_proto_library` a new `has_services` flag. When
set, the proto library will be compiled with gRPC service support, and
an additional `*_py_pb2_grpc` `py_library` target will be created.

The `py_proto_library` macro provided by the protobuf build rules no
longer suits our purpose, as it can only create one `*_py_pb2` build
target that includes both the `*_pb2` and `*_pb2_grpc` Python modules.
This is inconsistent with TensorFlow and some Google internal rules. We
now directly use `proto_gen` rather than using `py_proto_library`. This
is fine: that macro is marked as unstable because its interface may
change, and our goal is precisely to change the interface.

Test Plan:
Run

```
bazel query --output=build \
    'filter(".*protos_all.*", //tensorboard/...:all)'
```

and note that the output is identical before and after this change, so
existing protos will be unchanged.

Then, add a simple service to `tensorboard/plugins/hparams/api.proto`:

```proto
service TestService {
  rpc ListSesssionGroups(ListSessionGroupsRequest)
      returns (ListSessionGroupsResponse);
}
```

Build the Pip package and install it into a new virtualenv. Try to
import `tensorboard.plugins.hparams.api_pb2_grpc`, but note that
this fails. Then, add `has_services = True` to the hparams `BUILD` file,
and rebuild and reinstall the Pip package. The import should still fail.
Finally, add a `:protos_all_py_pb2_grpc` dep to `:hparams_plugin`, and
try once more. The import should now work, and the imported module
should have a `TestServiceStub` attribute.

wchargin-branch: proto-grpc
@wchargin wchargin mentioned this pull request Oct 29, 2019
wchargin added a commit that referenced this pull request Oct 29, 2019
Summary:
This commit teaches `tb_proto_library` a new `has_services` flag. When
set, the proto library will be compiled with gRPC service support, and
an additional `*_py_pb2_grpc` `py_library` target will be created.

The `py_proto_library` macro provided by the protobuf build rules no
longer suits our purpose, as it can only create one `*_py_pb2` build
target that includes both the `*_pb2` and `*_pb2_grpc` Python modules.
This is inconsistent with TensorFlow and some Google internal rules. We
now directly use `proto_gen` rather than using `py_proto_library`. This
is fine: that macro is marked as unstable because its interface may
change, and our goal is precisely to change the interface.

Test Plan:
Run

```
bazel query --output=build \
    'filter(".*protos_all.*", //tensorboard/...:all)'
```

and note that the output is identical before and after this change, so
existing protos will be unchanged.

Then, add a simple service to `tensorboard/plugins/hparams/api.proto`:

```proto
service TestService {
  rpc ListSesssionGroups(ListSessionGroupsRequest)
      returns (ListSessionGroupsResponse);
}
```

Build the Pip package and install it into a new virtualenv. Try to
import `tensorboard.plugins.hparams.api_pb2_grpc`, but note that
this fails. Then, add `has_services = True` to the hparams `BUILD` file,
and rebuild and reinstall the Pip package. The import should still fail.
Finally, add a `:protos_all_py_pb2_grpc` dep to `:hparams_plugin`, and
try once more. The import should now work, and the imported module
should have a `TestServiceStub` attribute.

wchargin-branch: proto-grpc
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.

4 participants