提交 99483205 编写于 作者: B BBuf

add some op test

上级 21e75382
......@@ -10,20 +10,9 @@
#### Introduction
oneflow_onnx tool package includes two major functions: one is to export OneFlow out of ONNX, while the other is to transform ONNX models, which are obtained from other training frameworks, into Oneflow models. This tool package has already been adapted to TensorFlow/Pytorch/PaddlePaddle pre-trained models. The process of oneflow_onnx extracting ONNX and transforming it into OneFlow's format is called X2OneFlow (X representing TensorFlow/Pytorch/PaddlePaddle).
- OneFlow2ONNX models are supported. Specifically, OneFlow's lazy mode model can be transfomed into ONNX's format. Transformable OneFlow model can be obtained by using the method explained on [flow.checkpoint.save ](https://docs.oneflow.org/basics_topics/model_load_save.html). For more information, please refer to [OneFlow2ONNX Model List](docs/oneflow2onnx/oneflow2onnx_model_zoo.md).
- X2OneFlow models are supported. TensorFlow/Pytorch/PaddlePaddle model can be transformed into OneFlow's format through ONNX.
- OneFlow2ONNX operators are supported. Currently, oneflow_onnx is fully capable of exporting ONNX Opset10, and parts of OneFlow operator can transform ONNX Opsets that are in lower order. Please refer to [OneFlow2ONNX Operator Lists](docs/oneflow2onnx/op_list.md) for more information.
- X2OneFlow operators are supported. Currently, oneflow_onnx is fully capable of supporting most CV operators in TensorFlow/Pytorch/PaddlePaddle. Please refer to [X2OneFlow Operator Lists](docs/x2oneflow/op_list.md) for more information.
- Code generation is also supported. oneflow_onnx is able to generate OneFlow code and transforming models simultaneously . Please refer to [X2OneFlow Code Generation List](docs/x2oneflow/code_gen.md) for more information.
> To sum up,
>
> - OneFlow2ONNX can support over 80 ONNX OP
> - X2OneFlow can support 80 ONNX OP, 50+ TensorFlow OP, 80+ Pytorch OP, and 50+ PaddlePaddle OP
>
> which covers most operations when doing CV model classifications. Since the OPs and models we support are all in eager mode API, users are required to install versions of PaddlePaddle >= 2.0.0, TensorFlow >= 2.0.0, and there is no specific requirements for Pytorch. Until now, X2OneFlow has successfully transformed 50+ official models from TensorFlow/Pytorch/PaddlePaddle, and you're always welcomed to experience our product.
#### Environment Dependencies
......@@ -38,22 +27,13 @@ onnxruntime>=1.6.0
oneflow (https://github.com/Oneflow-Inc/oneflow#install-with-pip-package)
```
If you'd llike to use X2OneFlow, the following versions of deep learning frameworks are needed:
```sh
pytorch>=1.7.0
paddlepaddle>=2.0.0
paddle2onnx>=0.6
tensorflow>=2.0.0
tf2onnx>=1.8.4
```
#### Installation
##### Method 1
```sh
pip install oneflow_onn
pip install oneflow_onnx
```
**Method 2**
......@@ -71,33 +51,6 @@ Please refer to [Examples](examples/README.md)
#### Related Documents
- [OneFlow2ONNX Model List](docs/oneflow2onnx/oneflow2onnx_model_zoo.md)
- [X2OneFlow Model List](docs/x2oneflow/x2oneflow_model_zoo.md)
- [OneFlow2ONNX Operator List](docs/oneflow2onnx/op_list.md)
- [X2OneFlow Operator List](docs/x2oneflow/op_list.md)
- [Examples](examples/README.md)
### nchw2nhwc_tool
#### Introduction
This tool is to transform NCHW, which is trained through OneFlow, into NHWC Format. Please click [here](nchw2nhwc_tool/README.md) for more information
### save_serving_tool
#### Introduction
This tool is to transform OneFlow models into models that can be used on the Serving end. Please click [here](save_serving_tool/README.md) for more information
### Project Status
- 2021/4/13 Successively supported code generations for ResNet18. C testing script was removed after failed OP transformation. 0.2.2 wheel package was announced.
- 2021/4/14 Refined CI errors. Supported code generations for all functions of X2OneFlow. 0.2.3 whell package was announced.
- 2020/4/15 Finalized code generations for all functions of X2OneFlow. 0.3.0 whell was announced.
- 2020/4/16 Merged Expand OP into the main branch. Debugged oneflow_api. 0.3.1 whell package was announced.
- 2020/4/16 Solve the remaining problems of automatic code generation, add the test of automatic code generation to CI. 0.3.2 whell package was announced.
- 2020/6/21 Add PreLU/LeakyReLU OP ONNX Export, fix automatic code generation bug. 0.3.3 whell package was announced.
- 2020/6/23 Add Constant OP ONNX Export,fix batchnorm op export onnx bug (which caused in insightface) and disable the global function that
is enabled by default when exporting ONNX . 0.3.3.20210623 whell package was announced.
- 2020/6/24 Add Flatten OP ONNX Export. 0.3.4 whell package was announced.
set -ex
python3 -m pip config set global.index-url https://mirrors.bfsu.edu.cn/pypi/web/simple
python3 -m pip install --user --upgrade pip
python3 -m pip install -r test-requirements.txt --user --extra-index-url https://pypi.ngc.nvidia.com
if [ -f requirements.txt ]; then python3 -m pip install -r requirements.txt --user; fi
python3 -m pip install oneflow --user -U -f https://staging.oneflow.info/branch/master/cu110
python3 setup.py install
......
......@@ -13,233 +13,49 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import tempfile
import oneflow as flow
import oneflow.typing as tp
from oneflow_onnx.oneflow2onnx.util import convert_to_onnx_and_check
class MathOps(flow.nn.Module):
def __init__(self) -> None:
super(MathOps, self).__init__()
def forward(self, x: flow.Tensor) -> flow.Tensor:
y1 = x * x
y2 = y1 / x
y2 = y2 - x
y2 = y2 + x
y2 = flow.abs(y2)
y2 = flow.ceil(y2)
# y3 = flow.clip(x, -1.0, 1.0)
# y3 = flow.acos(y3)
# y2 = y2 + y3
def generate_binary_op_test(flow_op, *args, opset=None, **kwargs):
@flow.global_function()
def job1():
x = flow.get_variable(
name="x1",
shape=(2, 3, 4),
dtype=flow.float,
initializer=flow.random_uniform_initializer(-10, 10),
)
y = flow.get_variable(
name="y1",
shape=(1, 3, 1),
dtype=flow.float,
initializer=flow.random_uniform_initializer(-10, 10),
)
return flow_op(x, y, *args, **kwargs)
return y2
convert_to_onnx_and_check(job1, opset=opset)
def generate_unary_op_test(
flow_op, *args, opset=None, min_val=-10, max_val=10, **kwargs
):
@flow.global_function()
def job1():
x = flow.get_variable(
name="x1",
shape=(2, 3, 4, 5),
dtype=flow.float,
initializer=flow.random_uniform_initializer(min_val, max_val),
)
return flow_op(x, *args, **kwargs)
math_ops = MathOps()
class MathOpGraph(flow.nn.Graph):
def __init__(self):
super().__init__()
self.m = math_ops
convert_to_onnx_and_check(job1, opset=opset)
def build(self, x):
out = self.m(x)
return out
def test_mul():
generate_binary_op_test(flow.math.multiply)
def test_math_ops():
math_ops_graph = MathOpGraph()
math_ops_graph._compile(flow.randn(1, 3, 224, 224))
# print(math_ops_graph._full_graph_proto)
with tempfile.TemporaryDirectory() as tmpdirname:
flow.save(math_ops.state_dict(), tmpdirname)
convert_to_onnx_and_check(math_ops_graph, flow_weight_dir=tmpdirname, onnx_model_path="/tmp")
def test_div():
generate_binary_op_test(flow.math.divide)
test_math_ops()
def test_sub():
generate_binary_op_test(flow.math.subtract)
def test_add():
generate_binary_op_test(flow.math.add)
def test_abs():
generate_unary_op_test(flow.math.abs)
def test_ceil():
generate_unary_op_test(flow.math.ceil)
def test_acos():
generate_unary_op_test(flow.math.acos, min_val=-1, max_val=1)
def test_asin():
generate_unary_op_test(flow.math.asin, min_val=-1, max_val=1)
def test_atan():
generate_unary_op_test(flow.math.atan, min_val=-1, max_val=1)
def test_acosh():
generate_unary_op_test(flow.math.acosh, min_val=1, max_val=100)
def test_asinh():
generate_unary_op_test(flow.math.asinh, min_val=-1, max_val=1)
def test_atanh():
generate_unary_op_test(flow.math.atanh, min_val=-1, max_val=1)
def test_sin():
generate_unary_op_test(flow.math.sin)
def test_cos():
generate_unary_op_test(flow.math.cos)
def test_tan():
generate_unary_op_test(flow.math.tan)
def test_sinh():
generate_unary_op_test(flow.math.sinh)
def test_cosh():
generate_unary_op_test(flow.math.cosh)
def test_tanh_v2():
generate_unary_op_test(flow.math.tanh_v2)
def test_tanh():
generate_unary_op_test(flow.math.tanh)
def test_erf():
generate_unary_op_test(flow.math.erf)
def test_log():
generate_unary_op_test(flow.math.log, min_val=0, max_val=100)
def test_floor():
generate_unary_op_test(flow.math.floor)
def test_reciprocal():
generate_unary_op_test(flow.math.reciprocal)
def test_round():
generate_unary_op_test(flow.math.round, opset=11)
def test_rsqrt():
generate_unary_op_test(flow.math.rsqrt, min_val=0, max_val=100)
def test_sigmoid_v2():
generate_unary_op_test(flow.math.sigmoid_v2)
def test_sigmoid():
generate_unary_op_test(flow.math.sigmoid)
def test_sign():
generate_unary_op_test(flow.math.sign)
def test_softplus():
generate_unary_op_test(flow.math.softplus)
def test_sigmoid():
generate_unary_op_test(flow.math.sigmoid)
def test_sqrt():
generate_unary_op_test(flow.math.sqrt, min_val=0, max_val=100)
def test_sqaure():
generate_unary_op_test(flow.math.square)
def test_maximum():
generate_binary_op_test(flow.math.maximum)
def test_minimum():
generate_binary_op_test(flow.math.minimum)
def test_equal():
generate_binary_op_test(flow.math.equal, opset=11)
def test_not_equal():
generate_binary_op_test(flow.math.not_equal, opset=11)
def test_less():
generate_binary_op_test(flow.math.less)
def test_greater():
generate_binary_op_test(flow.math.greater)
def test_less_equal():
generate_binary_op_test(flow.math.less_equal)
def test_greater_equal():
generate_binary_op_test(flow.math.greater_equal)
def test_squared_difference():
generate_binary_op_test(flow.math.squared_difference)
def test_cast():
generate_unary_op_test(flow.cast, dtype=flow.int32)
def test_scalar_mul_int():
generate_unary_op_test(flow.math.multiply, 5)
def test_scalar_mul_float():
generate_unary_op_test(flow.math.multiply, 5.1)
def test_scalar_add_int():
generate_unary_op_test(flow.math.add, 5)
def test_scalar_add_float():
generate_unary_op_test(flow.math.add, 5.1)
def test_leaky_relu():
generate_unary_op_test(flow.nn.leaky_relu, 0.6)
def test_prelu():
generate_unary_op_test(flow.layers.prelu)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册