# Glow with generic model builder

**URL:** <https://discuss.pytorch.org/t/glow-with-generic-model-builder/42559>\
**Category:** glow\
**Created:** [April 14, 2019, 5:50pm UTC](https://discuss.pytorch.org/t/glow-with-generic-model-builder/42559 "2019-04-14T17:50:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![wny](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/wny/32/11056_2.png) [@wny](https://discuss.pytorch.org/u/wny)\
**Post date:** [April 14, 2019, 5:50pm UTC](https://discuss.pytorch.org/t/glow-with-generic-model-builder/42559/1 "2019-04-14T17:50:01Z")

</div>

There are plenty of examples for using the image-classifier builder, but I have not found any examples on using the generic model builder. In particular:

1. What would be the input format to the generic builder?
2. I would guess that I would have to write my own input handler. Where would I do that?
3. Relating to 2, is there a high level class that I can subclass from to create my own builder?

Ultimately, is there a barebones example somewhere?

Thanks!

---

<div class="post-metadata">

**Author:** ![wny](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/wny/32/11056_2.png) [@wny](https://discuss.pytorch.org/u/wny)\
**Post date:** [April 15, 2019, 8:03pm UTC](https://discuss.pytorch.org/t/glow-with-generic-model-builder/42559/2 "2019-04-15T20:03:37Z")

</div>

Hate answering my own question, but here goes… (I still have more questions below, though):

From Glow’s `tools/ModelRunner.cpp`:

```cpp
int main(int argc, char **argv) {
  PlaceholderBindings bindings;
  // The loader verifies/initializes command line parameters, and initializes
  // the ExecutionEngine and Function.
  Loader loader(argc, argv);

  // Create the model based on the input net, and get SaveNode for the output.
  std::unique_ptr<ProtobufLoader> LD;
  if (!loader.getCaffe2NetDescFilename().empty()) {
    LD.reset(new Caffe2ModelLoader(loader.getCaffe2NetDescFilename(),
                                   loader.getCaffe2NetWeightFilename(), {}, {},
                                   *loader.getFunction()));
  } else {
    LD.reset(new ONNXModelLoader(loader.getOnnxModelFilename(), {}, {},
                                 *loader.getFunction()));
  }
  Placeholder *output = EXIT_ON_ERR(LD->getSingleOutput());
  auto *outputT = bindings.allocate(output);

  // Compile the model, and perform quantization/emit a bundle/dump debug info
  // if requested from command line.
  loader.compile(bindings);

  // If in bundle mode, do not run inference.
  if (!emittingBundle()) {
    loader.runInference(bindings);

    llvm::outs() << "Model: " << loader.getFunction()->getName() << "\n";

    // Print out the result of output operator.
    outputT->getHandle().dump();

    // If profiling, generate and serialize the quantization infos now that we
    // have run inference to gather the profile.
    if (profilingGraph()) {
      loader.generateAndSerializeQuantizationInfos(bindings);
    }
  }

  return 0;
}

```

It looks like the generic model-runner does not accept input. Compare this with `tools/ImageClassifier.cpp`. We can probably modify it to accept custom input, or build on top of model-runner to create custom builders. My question, however, is this:

_Since model-runner does not accept input, how does it perform quantization (if asked) and other optimizations?_

In particular, documentation talks about the backend “watching” network activation as data flows through it, and makes decisions about which optimizations to make and how to correctly quantize the network.

---

<div class="post-metadata">

**Author:** ![jfix](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/jfix/32/21510_2.png) [@jfix](https://discuss.pytorch.org/u/jfix)\
**Post date:** [April 16, 2019, 5:03pm UTC](https://discuss.pytorch.org/t/glow-with-generic-model-builder/42559/3 "2019-04-16T17:03:58Z")

</div>

Correct – ModelRunner does not accept input. It assumes a model with no inputs and a single output.

> 1. I would guess that I would have to write my own input handler. Where would I do that?
> 2. Relating to 2, is there a high level class that I can subclass from to create my own builder?  
> Ultimately, is there a barebones example somewhere?

You’d need to create your own version that uses a specific number of inputs/outputs given your use case, as well as load the inputs yourself into the input tensors. I think the most barebones example we have is ModelRunner. You then could look to ImageClassifier to observe how we are loading in inputs and outputs. TextTranslator also exhibits multiple inputs and outputs for a model. I.e. for each, you can take a look at calls to create `Caffe2ModelLoader`/`ONNXModelLoader` to see how to use multiple inputs/outputs, how they’re used later, etc.

> _Since model-runner does not accept input, how does it perform quantization (if asked) and other optimizations?_

Good question – it would only be able to quantize given the Constant inputs already specified. However, the ModelRunner is really there as a simple tester for operator support, and is not used for serious models where you would want to gather a profile across many different inputs and then test accuracy across some test set.
