tensorflow/rust

cant generate &[u8] slice for ConfigProto to SessionOptions::set_config()

Corallus-Caninus opened this issue · 4 comments

I am not familiar with protobuff enough to generate my own messages. The Documentation mentions this but just references the C++ API and not protobuff examples or Rust ways to generate this. I noticed src/protos/config.rs has some protobuff stuff (ConfigProto) that might have a trait to generate &[u8] or cast a Message type to aligned bytes to generate &[u8] but its private. Got any pointers or references?

Within the crate code, for example, you can serialize message as follow:

rust/src/eager/context.rs

Lines 145 to 149 in 3f3f618

let mut config = ConfigProto::new();
config.set_gpu_options(gpu_options);
let mut buf = vec![];
config.write_to_writer(&mut buf).unwrap();

On top of that, it seems to be impossible to use the above code from outside the crate since the proto module is private as you pointed. We need to make the proto module public.

Are we planning to build an abstraction or wrapper on top of proto? If not I can patch this.

@Corallus-Caninus

I can't immediately answer whether the proto module should be public. For example, the above method I tried in a unit test can be generated in Python as follows:

from tensorflow.core.protobuf.config_pb2 import ConfigProto, GPUOptions

gpu_options = GPUOptions(
    per_process_gpu_memory_fraction=0.5
    )

config = ConfigProto(gpu_options=gpu_options)

with open("config.protobuf", "wb") as f:
    f.write(config.SerializeToString())

Making proto module public may take time for some reason, so please try the above method.

I've avoided exposing the generated proto code so far because that would lock in a choice of protobuf libraries, and it wasn't clear which protobuf crate was going to "win". We might have to bite the bullet at some point, though.