Skip to content

[Java][Feature] Generating operation methods used to build a graph #7149

Closed

Description

@karllessard

As suggested in https://www.tensorflow.org/versions/r0.11/how_tos/language_bindings, the list of operation methods for building a graph should be generated dynamically from the list exposed by the core, using preferably protobuf.

Is anyone already working on that feature? If not, I'm tempted to try it. I was thinking of generating a builder hierarchy that allows to optionally set an argument after adding an operation. For example

GraphBuider
    .matMul(a, b)
        .withTransposeB(true)
    .softmax(logits)
    ...

and the GraphBuilder classes would be generated at build time by Bazel and would make use of the already existing OperationBuilder. Since I'm new to Tensorflow, please tell me if that doesn't make any sense to you

Thanks

(p.s. as suggested by @drpngx, I've started this discussion as a seperate issue to avoid continuing the #5 saga)

Activity

  1. added
    type:feature
    on Jan 30, 2017
  2. drpngx commented on Jan 30, 2017

    @drpngx
    Contributor

    Thanks @karllessard! @asimshankar should know if that's in the plans or if it would be a welcome contribution.

  3. asimshankar commented on Jan 30, 2017

    @asimshankar
    Contributor

    Having generated code for ops in Java is indeed a next step for the Java API, but we haven't gotten around to it, or figuring out what an appropriate API would be.

    We'd be happy to hear any suggestions/proposals you may have. As for the suggestion above, I don't think we want to put all operations as methods in a single GraphBuilder class for a few reasons such as (a) Per-op attribute handling seems a bit tricky, (b) What about custom operations defined by other external libraries (e.g. as per the Adding a new op HOWTO).

    We can take inspiration from the generated C++ API (see the label_image example and the Go API, but it does require more thought.

  4. karllessard commented on Jan 31, 2017

    @karllessard
    ContributorAuthor

    I agree that exposing operations in their own particular object would create a richer API than declaring everything into a single builder class, I'll also get my mind into it.

    Now, without insisting, I'll clarify how my suggestion would have handled point a), maybe some of those ideas can morph into something else:

    1. The generated builder class implements each operations by returning a specialized builder that inherits this same class (e.g. GraphBuilder.matMul() returns a MatMulGraphBuilder that inherits GraphBuilder).

    2. The specialized builder exposes all other operations directly from its superclass (for going on with graph building) + methods for setting optional attributes of the current operation, prefixed by "with" (e.g. withTransposeA() and withTransposeB()).

    3. The specialized builder is responsible of building the current operation, using internally an OperationBuilder.

    4. Invoking another operation on a specialized builder will therefore end the construction of the current operation and return another specialized builder for building the new operation.

  5. karllessard commented on Feb 6, 2017

    @karllessard
    ContributorAuthor

    Anyone curious to see how a static API for building a graph may look like, I've pushed a little POC to my repo (this commit) that minimally fulfill the requirements of the LabelImage example.

    This POC focuses on the following point:

    1. Group operations into related topics, like the C++ client does, by generating a static API per /tensorflow/core/ops/* library (e.g. ImageOps, ArrayOps, ...)

    2. Allow optional parametrization using operation-specific methods encapsulated in their own classes (e.g. the DecodeJpeg interface)

    3. Keep the simplicity and elegance of the solution from the previous version of LabelImage while allowing more flexibility to support all cases (e.g. multi-output operations, etc). The following lists supported technics to connect nodes together.

    a) Functionally

    op1(s, op2(s, const(s, x)), const(s, y));

    b) By node references

    Const c1 = const(s, x);
    Const c2 = const(s, y);
    op1(s, op2(s, c1), c2);

    c) By node lookups

    op2(s, "op2", const(s, x));
    op1(s, s.node("op2"), const(s, y));

    Technics b) and c) allows us to access output value other than the default (0) one for multi-output operations. For example:

    op1(s, s.node("op2").output(1), const(s, y));

    Any comments are more than welcomed

  6. added a commit that references this issue on May 12, 2017
  7. kbsriram commented on May 23, 2017

    @kbsriram
    Contributor

    @karllessard and I (with guidance from @asimshankar) have consolidated ideas on this thread into a proposal for the API. We'd love to hear your feedback, so please continue to add comments on this issue.

    We'd also like to help move things forward via code contributions, and Karl and I plan to start some related PRs. Please let us know if you're interested/working on something related, so we can jointly work to bring Java bindings for Tensorflow operators.

  8. removed their assignment
    on Jun 6, 2017
  9. added
    stat:contribution welcome
    on Jun 6, 2017
  10. 7 remaining items

  11. HedgehogCode commented on Dec 12, 2017

    @HedgehogCode

    What is the current status of this issue? Is someone working on it and is it supposed to be resolved soon or is it a goal which lies far in the future?

  12. karllessard commented on Dec 12, 2017

    @karllessard
    ContributorAuthor

    It is still ongoing and actively being committed but it takes some time, there is a lot of code and iterations involved. Pull request #14094 is related to this story and is currently being reviewed by @asimshankar .

  13. wt-huang commented on Nov 8, 2018

    @wt-huang

    Closing as this is resolved

  14. added a commit that references this issue on Oct 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stat:contribution welcometype:feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions