Integrate WASI-NN into WAMR (#1521)

Initial integration of WASI-NN based on #1225:
- Implement the library core/iwasm/libraries/wasi-nn
- Support TensorFlow, CPU, F32 at the first stage
- Add cmake variable `-DWAMR_BUILD_WASI_NN`
- Add test case based on Docker image and update document

Refer to #1573
This commit is contained in:
tonibofarull
2022-10-12 06:09:29 +02:00
committed by GitHub
parent 78c38d088e
commit e53ab91439
25 changed files with 1459 additions and 0 deletions

View File

@ -0,0 +1,16 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
import tensorflow as tf
from utils import save_model
model = tf.keras.Sequential([
tf.keras.layers.InputLayer(input_shape=[5, 5, 1]),
tf.keras.layers.AveragePooling2D(
pool_size=(5, 5), strides=None, padding="valid", data_format=None)
])
# Export model to tflite
save_model(model, "average.tflite")

View File

@ -0,0 +1,17 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
import tensorflow as tf
from utils import save_model
model = tf.keras.Sequential([
tf.keras.layers.InputLayer(input_shape=[5, 5, 1]),
tf.keras.layers.MaxPooling2D(
pool_size=(5, 5), strides=None, padding="valid", data_format=None)
])
# Export model to tflite
save_model(model, "max.tflite")

View File

@ -0,0 +1,15 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
import tensorflow as tf
from utils import save_model
model = tf.keras.Sequential([
tf.keras.layers.InputLayer(input_shape=[3, 3, 1]),
tf.keras.layers.Conv2D(1, (1, 1), kernel_initializer=tf.keras.initializers.Constant(
value=1), bias_initializer='zeros'
)
])
# Export model to tflite
save_model(model, "mult_dim.tflite")

View File

@ -0,0 +1,33 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
import tensorflow as tf
import numpy as np
from keras.layers import AveragePooling2D, Conv2D
from tensorflow.keras import Input, Model
from utils import save_model
inputs = Input(shape=(4, 4, 1))
output1 = Conv2D(1, (4, 1), kernel_initializer=tf.keras.initializers.Constant(
value=1), bias_initializer='zeros'
)(inputs)
output2 = AveragePooling2D(pool_size=(
4, 1), strides=None, padding="valid", data_format=None)(inputs)
model = Model(inputs=inputs, outputs=[output1, output2])
inp = np.arange(16).reshape((1, 4, 4, 1))
print(inp)
res = model.predict(inp)
print(res)
print(res[0].shape)
print(res[1].shape)
save_model(model, "mult_out.tflite")

View File

@ -0,0 +1,17 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
import tensorflow as tf
from utils import save_model
model = tf.keras.Sequential([
tf.keras.layers.InputLayer(input_shape=[5, 5, 1]),
tf.keras.layers.Conv2D(1, (5, 5), kernel_initializer=tf.keras.initializers.Constant(
value=1), bias_initializer='zeros'
)
])
# Export model to tflite
save_model(model, "sum.tflite")

View File

@ -0,0 +1,13 @@
# Copyright (C) 2019 Intel Corporation. All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
import tensorflow as tf
import pathlib
def save_model(model, filename):
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
tflite_models_dir = pathlib.Path("./")
tflite_model_file = tflite_models_dir/filename
tflite_model_file.write_bytes(tflite_model)