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

[Question] Customize the number of input/output variables in generated graphs #117

Closed
ntcmp2u opened this issue Jun 6, 2023 · 9 comments

Comments

@ntcmp2u
Copy link

ntcmp2u commented Jun 6, 2023

Hi. I am currently learning to use nnsmith. I would like to know if I can customize the generated graph as a graph with only a single input variable and a single output variable.

Is there any way to quickly implement this?

@ganler
Copy link
Member

ganler commented Jun 6, 2023

Do you want to only generate a chain of operators? Or you hope at model-wise you only need to create one input and compare one output?

Both of these need to be implemented. Just let me know :)

@lazycal
Copy link
Collaborator

lazycal commented Jun 6, 2023

Hi @ntcmp2u , thanks for your interest in nnsmith. I assume you mean "model-wise" single input output.

  • For single input, one quick way you could try is to set the forward_prob to 1 here:
    self.forward_prob = 0.5 if forward_prob is None else forward_prob
    , though this might hurt some diversity in the graph structures (since no backward_gen any more).
  • For single output there might be no easy way and need some proper reimplementation. Probably we need to change this function
    def pick_var_group(
    , which is used to pick after what operator should we insert the next one.

@ntcmp2u
Copy link
Author

ntcmp2u commented Jun 6, 2023

@ganler @lazycal Thank you so much for quick response. Yes, what I mean is the "model-wise" single input output. Although this may impact the diversity of generated graphs, I believe that it could be valuable to simulate a real-world senario -- most of time our trained model is a single input model. I will investigate the pick_var_group function to implement it.

Thank you again for your assist.

@ntcmp2u ntcmp2u closed this as completed Jun 6, 2023
@ganler
Copy link
Member

ganler commented Jun 6, 2023

There are two ways to make a model produce only one output. The first way is what I said to make only one leaf node in the graph. Another simple way is to "cheat" by only marking one value in the graph as output (others could be cut by DCE).

Because you are talking about real-world scenarios, I bet you are talking about the first one. For that pick_var_groups is not the right place to work on and it is completely unnecessary to hack the codebase (which is hard too), as I made it essentially for grouping connections to rank-dtype compatible variables/placeholders while your desired constraint is on model topology. The easiest way IMO to achieve the topological constraint is to (i) generate a large model; and (ii) picking an intermediate value, marking it alive and then do a use-def analysis + only preserve its usee chain (this is similar to clipping a subgraph from a large one such that the subgraph is single i/o). This is very easy in NNSmith as the GraphIR is an SSA and has built-in use-def analysis support.

That being said, let me know if you want me to quickly implement that for you and then you shall be able to go for it with a commandline flag. Of course you are always welcome to try it on your own and even upstream the patch.

NB: for real-world-like models or whatever structure in user intents, we are building a DSL for describing any desired model patterns. It is not going to ship very recently but yeah stay tuned :)

@ntcmp2u
Copy link
Author

ntcmp2u commented Jun 8, 2023

let me know if you want me to quickly implement that for you and then you shall be able to go for it with a commandline flag. Of course you are always welcome to try it on your own and even upstream the patch.

@ganler I tried to implement it for a few hours but failed to do it in a right way. Perhaps I still need time to get familiar with NNSmith's implementation. If possible, could you implement that and export a command line flag? Thank you so much for assisting.

@ganler
Copy link
Member

ganler commented Jun 8, 2023

@ntcmp2u No worries at all. I will find a time this weekend. -- And don't feel frustrated as it is hard to extend on big and weakly documented codebase in the beginning (enhancing the doc is a longer-term plan... since it is currently maintained/developed by a very small team). Meanwhile feel free to post any questions regarding the implementation if you are interested. Thanks.

@ntcmp2u
Copy link
Author

ntcmp2u commented Jul 16, 2023

@ganler Hi, sorry for bothering you. Just want to know if the one-leaf generation is implemented. If you can't find a time, perhaps I can try to understand the use-def analysis and implement one.

@ganler
Copy link
Member

ganler commented Jul 18, 2023

@ntcmp2u Sorry for the delay (I totally forgot that... I need to update my TODO list more timely lol).

Once #119 is merged you can try:

python nnsmith/cli/model_gen.py model.type=torch backend.type="torchjit" debug.viz=1 mgen.method="single-io-cinit" mgen.max_nodes=10

It shall get you something like:

image

image

@ntcmp2u
Copy link
Author

ntcmp2u commented Jul 21, 2023

@ganler Thank you so much for the assist!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants