forked from onnx/onnx
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement function retrieval APIs; Add documentation for functions (o…
…nnx#1112) * Add for test function build * Add pybind functions for getFunction API * Implement FC function testcase * Lint code using clang-format * Disable warning in covert protobuf::int64 to int * Update doc generation scripts and functions doc * Trimming some comments * Fix doc generation scripts for typecheck * Improve cmake scripts for unittest project dependencies and fix linux build issues * Lint python code * Update API to support functions with multiple versions * Update line ending * Add changelogs for functions * Refactor and lint code * Fix bad linebreaks in markdown files generated * Resolve PR comments; Refine Function type check implementation * Add checker for function version; Bump version in onnx_domain * Update doc_string and generated md files * Add helper function; Update documents for MVN function * Fix a bug in MVN function definition * Experimental: add python test case for MVN * Update line ends * Add attribute mvn testcase * Update generated files on MVN function * Rename buildnode function * Remove node part in generated files; Reformat doc_string for MVN * Fix changelog generation script * Update MVN testcase to caculate expected output on the fly * Rephrase description for MVN function * Formatting docstring for MVN function * Refactor helper function code; Add epsilon before division * Move the helper functions into common folder * Fix typo in function docstring * Refactor model helper function; Add version check; Fix typo * Switch to use FunctionProto class generated in python * Remove unnecessary ignore flag * Minor fix in c++ tests; Generated function docs for ML domain * Refactor code to avoid hardcode domain name * Resolve PR comments
- Loading branch information
1 parent
5b9f9c1
commit 828d3c3
Showing
16 changed files
with
632 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
## Functions | ||
*This file is automatically generated from the | ||
[def files](/onnx/defs) via [this script](/onnx/defs/gen_doc.py). | ||
Do not modify directly and instead edit function definitions.* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
## Functions | ||
*This file is automatically generated from the | ||
[def files](/onnx/defs) via [this script](/onnx/defs/gen_doc.py). | ||
Do not modify directly and instead edit function definitions.* | ||
## ai.onnx (default) | ||
* <sub>experimental</sub><a href="#FuncMeanVarianceNormalization">FuncMeanVarianceNormalization</a> | ||
|
||
|
||
|
||
### <sub>experimental</sub> <a name="FuncMeanVarianceNormalization"></a><a name="funcmeanvariancenormalization">**FuncMeanVarianceNormalization**</a> | ||
|
||
A MeanVarianceNormalization Function: Perform mean variance normalization on the input tensor X using formula: <br/> ``` (X-EX)/sqrt(E(X-EX)^2) ``` <br/><br/><b>INPUT: </b>X(float/float16/double) with shape [N,C,W,H] or N-D shape <br/><br/><b>ATTRIBUTE: </b><br/> <tt>axes: </tt>will be passed to ReducedMean Ops. Use [0,2,3] (without C axis for N-D cases) for calculating means and variances along channels. Two variables with the same C-coordinate are associated with the same mean and variance. Use [0,1,2,3] (with C axis) to calculate global mean and global variance with all variables sharing the same mean/variance.<br/> (The KeepDims attribute in ReducedMean is set to true for calculation)<br/><br/><b>OUTPUT: </b>X_MVN(float/float16/double) with the same shape as input X<br/> | ||
|
||
#### Version | ||
|
||
This version of the function has been available since version 8 of the default ONNX operator set. | ||
|
||
#### Inputs | ||
|
||
<dl> | ||
<dt>X; </dt> | ||
<br/></dl> | ||
|
||
#### Outputs | ||
|
||
<dl> | ||
<dt>X_MVN; </dt> | ||
<br/></dl> | ||
|
||
#### Attributes | ||
|
||
<dl> | ||
<dt>axes;<br/></dt> | ||
</dl> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
## Function Changelog | ||
*This file is automatically generated from the | ||
[def files](/onnx/defs) via [this script](/onnx/defs/gen_doc.py). | ||
Do not modify directly and instead edit function definitions.* | ||
## ai.onnx.ml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
## Function Changelog | ||
*This file is automatically generated from the | ||
[def files](/onnx/defs) via [this script](/onnx/defs/gen_doc.py). | ||
Do not modify directly and instead edit function definitions.* | ||
# ai.onnx (default) | ||
## Version 8 of domain ai.onnx (default) | ||
### <a name="FuncMeanVarianceNormalization-8"></a>**FuncMeanVarianceNormalization-8**</a> | ||
|
||
A MeanVarianceNormalization Function: Perform mean variance normalization on the input tensor X using formula: <br/> ``` (X-EX)/sqrt(E(X-EX)^2) ``` <br/><br/><b>INPUT: </b>X(float/float16/double) with shape [N,C,W,H] or N-D shape <br/><br/><b>ATTRIBUTE: </b><br/> <tt>axes: </tt>will be passed to ReducedMean Ops. Use [0,2,3] (without C axis for N-D cases) for calculating means and variances along channels. Two variables with the same C-coordinate are associated with the same mean and variance. Use [0,1,2,3] (with C axis) to calculate global mean and global variance with all variables sharing the same mean/variance.<br/> (The KeepDims attribute in ReducedMean is set to true for calculation)<br/><br/><b>OUTPUT: </b>X_MVN(float/float16/double) with the same shape as input X<br/> | ||
|
||
#### Version | ||
|
||
This version of the function has been available since version 8 of the default ONNX operator set. | ||
|
||
#### Inputs | ||
|
||
<dl> | ||
<dt>X; </dt> | ||
<br/></dl> | ||
|
||
#### Outputs | ||
|
||
<dl> | ||
<dt>X_MVN; </dt> | ||
<br/></dl> | ||
|
||
#### Attributes | ||
|
||
<dl> | ||
<dt>axes;<br/></dt> | ||
</dl> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from __future__ import absolute_import | ||
from __future__ import division | ||
from __future__ import print_function | ||
from __future__ import unicode_literals | ||
|
||
import numpy as np # type: ignore | ||
|
||
import onnx | ||
from ..base import Base | ||
from . import expect | ||
|
||
|
||
class MVN(Base): | ||
|
||
@staticmethod | ||
def export(): # type: () -> None | ||
node = onnx.helper.make_node( | ||
'FuncMeanVarianceNormalization', | ||
inputs=['X'], | ||
outputs=['Y'], | ||
axes=[0, 2, 3] | ||
) | ||
|
||
input_data = np.array([[[[0.8439683], [0.5665144], [0.05836735]], | ||
[[0.02916367], [0.12964272], [0.5060197]], | ||
[[0.79538304], [0.9411346], [0.9546573]]], | ||
[[[0.17730942], [0.46192095], [0.26480448]], | ||
[[0.6746842], [0.01665257], [0.62473077]], | ||
[[0.9240844], [0.9722341], [0.11965699]]], | ||
[[[0.41356155], [0.9129373], [0.59330076]], | ||
[[0.81929934], [0.7862604], [0.11799799]], | ||
[[0.69248444], [0.54119414], [0.07513223]]]], dtype=np.float32) | ||
|
||
# Calculate expected output data | ||
data_mean = np.mean(input_data, axis=(0, 2, 3), keepdims=1) | ||
data_mean_squared = np.power(data_mean, 2) | ||
data_squared = np.power(input_data, 2) | ||
data_squared_mean = np.mean(data_squared, axis=(0, 2, 3), keepdims=1) | ||
std = np.sqrt(data_squared_mean - data_mean_squared) | ||
expected_output = (input_data - data_mean) / (std + 1e-9) | ||
|
||
expect(node, inputs=[input_data], outputs=[expected_output], | ||
name='test_mvn') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright (c) Facebook Inc. and Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
#include "onnx/common/model_helpers.h" | ||
#include "onnx/checker.h" | ||
#include "onnx/defs/schema.h" | ||
#include "onnx/string_utils.h" | ||
|
||
namespace ONNX_NAMESPACE { | ||
using namespace Common; | ||
|
||
Common::Status BuildNode( | ||
const std::string& name, | ||
const std::string& domain, | ||
const std::string& doc_string, | ||
const std::string& op_type, | ||
std::vector<std::string> const& inputs, | ||
std::vector<std::string> const& outputs, | ||
NodeProto* node) { | ||
if (node == NULL) { | ||
return Status( | ||
Common::CHECKER, | ||
Common::INVALID_ARGUMENT, | ||
"node_proto should not be nullptr."); | ||
} | ||
node->set_name(name); | ||
node->set_domain(domain); | ||
node->set_doc_string(doc_string); | ||
node->set_op_type(op_type); | ||
for (auto& input : inputs) { | ||
node->add_input(input); | ||
} | ||
for (auto& output : outputs) { | ||
node->add_output(output); | ||
} | ||
|
||
return Status::OK(); | ||
} | ||
} // namespace ONNX_NAMESPACE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright (c) Facebook Inc. and Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
#pragma once | ||
|
||
#include <string> | ||
#include <vector> | ||
#include "onnx/common/status.h" | ||
#include "onnx/onnx-operators_pb.h" | ||
|
||
namespace ONNX_NAMESPACE { | ||
|
||
// Helper function for register nodes in | ||
// a FunctionProto. Attributes need to be | ||
// registered separately. | ||
Common::Status BuildNode( | ||
const std::string& name, | ||
const std::string& domain, | ||
const std::string& doc_string, | ||
const std::string& op_type, | ||
std::vector<std::string> const& inputs, | ||
std::vector<std::string> const& outputs, | ||
/*OUT*/ NodeProto* node); | ||
} // namespace ONNX_NAMESPACE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.