Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions docs/source/extension-module.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ In the [Running an ExecuTorch Model in C++ Tutorial](running-a-model-cpp-tutoria

## Example

Let's see how we can run the `SimpleConv` model generated from the [Exporting to ExecuTorch tutorial](./tutorials/export-to-executorch-tutorial) using the `Module` APIs:
Let's see how we can run the `SimpleConv` model generated from the [Exporting to ExecuTorch tutorial](./tutorials/export-to-executorch-tutorial) using the `Module` and [`TensorPtr`](extension-tensor.md) APIs:

```cpp
#include <executorch/extension/module/module.h>
#include <executorch/extension/tensor/tensor.h>

using namespace ::torch::executor;
using namespace ::executorch::extension;

// Create a Module.
Module module("/path/to/model.pte");

// Wrap the input data with a Tensor.
float input[1 * 3 * 256 * 256];
Tensor::SizesType sizes[] = {1, 3, 256, 256};
TensorImpl tensor(ScalarType::Float, std::size(sizes), sizes, input);
auto tensor = from_blob(input, {1, 3, 256, 256});

// Perform an inference.
const auto result = module.forward(Tensor(&tensor));
const auto result = module.forward(tensor);

// Check for success or failure.
if (result.ok()) {
Expand Down Expand Up @@ -62,6 +62,14 @@ assert(module.is_method_loaded("forward"));
```
Note: the `Program` is loaded automatically before any `Method` is loaded. Subsequent attemps to load them have no effect if one of the previous attemps was successful.

You can also force-load the "forward" method with a convenience syntax:

```cpp
const auto error = module.load_forward();

assert(module.is_method_loaded("forward"));
```

### Querying for Metadata

Get a set of method names that a Module contains udsing the `method_names()` function:
Expand Down Expand Up @@ -131,10 +139,11 @@ Use [ExecuTorch Dump](sdk-etdump.md) to trace model execution. Create an instanc
```cpp
#include <fstream>
#include <memory>

#include <executorch/extension/module/module.h>
#include <executorch/devtools/etdump/etdump_flatcc.h>

using namespace ::torch::executor;
using namespace ::executorch::extension;

Module module("/path/to/model.pte", Module::LoadMode::MmapUseMlock, std::make_unique<ETDumpGen>());

Expand Down
Loading
Loading