models icon indicating copy to clipboard operation
models copied to clipboard

SyntaxNet: where does parser_eval.py receive stdin?

Open j-min opened this issue 10 years ago • 4 comments

  • TL;DR when we run models/syntaxnet/$ echo "sentence to parse" | ./syntaxnet/demo.sh, which specific tensor does receive "sentence to parse"?

I made a SyntaxNet server (AWS, django) to help my other conversational tasks.

Every time I send a sentence query to my server, it takes around 3.5 seconds to get parsed sentences back.

This is not a fast enough for my task. So I tried to find where is bottleneck. I found import tensorflow as tf takes 0.8sec , and it actually takes 1.6 seconds (2*0.8 seconds) since SyntaxNet has two steps (POS tagging and parsing), even before it loads parameters and builds graphs.

I want my server always 'awake' and ready to parse sentence with pre-loaded graphs and parameters. So I try to tweak SyntaxNet to work as below. The session receives user input continuously by input() and prints calculated tensor and never turns off.

import tensorflow as tf
def multiplication(sess):
    x1 = int(input())
    matrix1 = tf.constant([[x1, x1]]
    matrix2 = tf.constant([[2],[2]])
    product = sess.run([tf.matmul(matrix1, matrix2)])   
    return product
with tf.Session() as sess:
    while True:
        print(multiplication(sess))
------------------------------------------
1
[array([[4]], dtype=int32)]
2
[array([[8]], dtype=int32)]
3
[array([[12]], dtype=int32)]

However, I cannot where to implement input() part. when we run models/syntaxnet/$ echo "sentence to parse" | ./syntaxnet/demo.sh, how does demo.sh receive stdin? In other words, where does "sentence to parse" go? I couldn't find any read inside the bash script.

So I just directly ran parser_eval by

python bazel-bin/syntaxnet/parser_eval \
  --input=stdin \
  --output=stdout-conll \
  --hidden_layer_sizes=64 \
  --arg_prefix=brain_tagger \
  --graph_builder=structured \
  --task_context=syntaxnet/models/parsey_mcparseface/context.pbtxt \
  --model_path=syntaxnet/models/parsey_mcparseface/tagger-params \
  --slim_model \
  --batch_size=1024 \
  --alsologtostderr \

and tried to find where does the python file syntaxnet/parser_eval.py receive input.

It seems parser.evaluation['documents'] below receives stdin somehow.

def Eval(sess):
...
...
    while True:
    tf_eval_epochs, tf_eval_metrics, tf_documents = sess.run([
              parser.evaluation['epochs'],
              parser.evaluation['eval_metrics'],
              parser.evaluation['documents'],
          ])
  
          if len(tf_documents):
            logging.info('Processed %d documents', len(tf_documents))
            num_documents += len(tf_documents)
            sess.run(sink, feed_dict={sink_documents: tf_documents})
  
          num_tokens += tf_eval_metrics[0]
          num_correct += tf_eval_metrics[1]
          if num_epochs is None:
            num_epochs = tf_eval_epochs
          elif num_epochs < tf_eval_epochs:
            break
...
...
def main(unused_argv):
  print >> sys.stderr, "parser_eval.py main start", time.time()
  logging.set_verbosity(logging.INFO)
  temp_counter = 0
  
  while True:
    with tf.Session() as sess:
      Eval(sess, temp_counter)
      temp_counter +=1

if __name__ == '__main__':
  tf.app.run()

I also tracked down graph_builder.py, gen_parser_ops.py, but couldn't find which specific tensor or variable receives the stdin sentence yet.

Could you please explain where does SyntaxNet receive stdin sentence?

It would be also helpful if you can answer some related questions.

  • How can I put while True: loop inside parser_eval.py (I tried this some parts at parser_eval.py, but it receives stdin only once.)
  • Can tensorflow serving help this issue?

Thanks in advance.

j-min avatar Nov 22 '16 07:11 j-min

Syntaxnet has a few layers of abstractions.

1 - protoio.h defines a few type of file format readers. 2 - document_filters.cc defines a few types of ways to read in files. 3 - reader_ops.cc defines a few interfaces for reading things as tensorflow operators 4 - ops/parser_ops.cc defines the cpp to python interfaces for the custom tensorflow operators above 5 - graph_builder.py will use those operators to build the model 6 - parser_eval.py and parser_trainer.py will use the model building functions to train and evaluate 7 - context.pbtxt and command line flags define what formats, readers, data, models to use

In order to change the way things are read in you need to adjust your configuration. You can use both command line flags and the context.pbtxt to configure different parts.

In the version google put out there are two main types of readers document readers and protobuf file readers. In the document reader it will hook the reader up to stdin if given the right configuration. All this abstraction makes it really configurable, but only with the things that are included.

In general the system is designed to be run on a server that can access something like google cloud storage files as its own operating system ie gs://bucket_name/file.conll

If you want to run it as a server I think the easiest path would be to run it on compute engine and send it tasks to format lots of documents on Google cloud storage at a time.

If instead you want a server that runs and can process one document at a time as requests come in you can write new interfaces in each of the levels of abstraction to read from a tensor provided at the top level. This requires a ton of major plumbing work but I got it working.

nburn42 avatar Dec 24 '16 01:12 nburn42

hi ,I have a problem with your description,the parser_eval.py you run is located in bazel-bin/syntaxnet/, but the python file you paste next is located in syntaxnet/syntaxnet/parser_eval.py, or the former program points to the latter?

zhou-zh avatar Apr 03 '17 13:04 zhou-zh

Bazel symlinks it on build.

nburn42 avatar Apr 03 '17 18:04 nburn42

This question is better asked on StackOverflow since it is not a bug or feature request. There is also a larger community that reads questions there. Thanks!

karmel avatar Feb 22 '18 17:02 karmel