Skip to content

Commit

Permalink
[TFLite] Add support to int16 data type in TFLite frontend (apache#10915
Browse files Browse the repository at this point in the history
)

* [TFLite] Add support to int16 data type in TFLite frontend

Add support for int16 data type and int64 biases/accumulators in
the TFLite frontend.

Adjusts TFLite tests to cover int16 convolutions and element-wise;
Fixes a minor typo negtive->negative in the element-wise tests.

* Update src/relay/qnn/op/convolution.cc

Co-authored-by: Elen Kalda <elen.kalda@arm.com>

Co-authored-by: Elen Kalda <elen.kalda@arm.com>
  • Loading branch information
2 people authored and driazati committed May 18, 2022
1 parent 1b32245 commit 328608f
Show file tree
Hide file tree
Showing 9 changed files with 679 additions and 260 deletions.
465 changes: 378 additions & 87 deletions Jenkinsfile

Large diffs are not rendered by default.

112 changes: 36 additions & 76 deletions jenkins/Jenkinsfile.j2
Original file line number Diff line number Diff line change
Expand Up @@ -399,53 +399,14 @@ def make(docker_type, path, make_flag) {
}
}

// Specifications to Jenkins "stash" command for use with various pack_ and unpack_ functions.
tvm_runtime = 'build/libtvm_runtime.so, build/config.cmake' // use libtvm_runtime.so.
tvm_lib = 'build/libtvm.so, ' + tvm_runtime // use libtvm.so to run the full compiler.
// LLVM upstream lib
tvm_multilib = 'build/libtvm.so, ' +
'build/libvta_fsim.so, ' +
tvm_runtime

tvm_multilib_tsim = 'build/libvta_tsim.so, ' +
tvm_multilib

microtvm_tar_gz = 'build/microtvm_template_projects.tar.gz'

// pack libraries for later use
def pack_lib(name, libs) {
sh (script: """
echo "Packing ${libs} into ${name}"
echo ${libs} | sed -e 's/,/ /g' | xargs md5sum
""", label: 'Stash libraries and show md5')
stash includes: libs, name: name
}

// unpack libraries saved before
def unpack_lib(name, libs) {
unstash name
sh (script: """
echo "Unpacked ${libs} from ${name}"
echo ${libs} | sed -e 's/,/ /g' | xargs md5sum
""", label: 'Unstash libraries and show md5')
}
// Filenames for stashing between build and test steps
{% set tvm_runtime = ['build/libtvm_runtime.so', 'build/config.cmake'] %}
{% set tvm_lib = ['build/libtvm.so'] + tvm_runtime %}
{% set tvm_multilib = ['build/libtvm.so', 'build/libvta_fsim.so'] + tvm_runtime %}
{% set tvm_multilib_tsim = ['build/libvta_tsim.so'] + tvm_multilib %}
{% set microtvm_template_projects = ['build/microtvm_template_projects',] %}
s3_prefix = "tvm-jenkins-artifacts-prod/tvm/${env.BRANCH_NAME}/${env.BUILD_NUMBER}"

// compress microtvm template projects and pack the tar.
def pack_microtvm_template_projects(name) {
sh(
script: 'cd build && tar -czvf microtvm_template_projects.tar.gz microtvm_template_projects/',
label: 'Compress microtvm_template_projects'
)
pack_lib(name + '-microtvm-libs', microtvm_tar_gz)
}

def unpack_microtvm_template_projects(name) {
unpack_lib(name + '-microtvm-libs', microtvm_tar_gz)
sh(
script: 'cd build && tar -xzvf microtvm_template_projects.tar.gz',
label: 'Unpack microtvm_template_projects'
)
}

def ci_setup(image) {
sh (
Expand Down Expand Up @@ -487,19 +448,20 @@ stage('Build') {
environment {
SKIP_SLOW_TESTS = "${skip_slow_tests}"
}
parallel 'BUILD: GPU': {
parallel(
'BUILD: GPU': {
if (!skip_ci) {
node('CPU-SMALL') {
ws({{ m.per_exec_ws('tvm/build-gpu') }}) {
init_git()
sh "${docker_run} --no-gpu ${ci_gpu} ./tests/scripts/task_config_build_gpu.sh build"
make("${ci_gpu} --no-gpu", 'build', '-j2')
pack_lib('gpu', tvm_multilib)
pack_microtvm_template_projects('gpu')
{{ m.upload_artifacts(tag='gpu', filenames=tvm_multilib, folders=microtvm_template_projects) }}

// compiler test
sh "${docker_run} --no-gpu ${ci_gpu} ./tests/scripts/task_config_build_gpu_other.sh build2"
make("${ci_gpu} --no-gpu", 'build2', '-j2')
pack_lib('gpu2', tvm_multilib)
{{ m.upload_artifacts(tag='gpu2', filenames=tvm_multilib) }}
}
}
}
Expand All @@ -514,7 +476,7 @@ stage('Build') {
label: 'Create CPU cmake config',
)
make(ci_cpu, 'build', '-j2')
pack_lib('cpu', tvm_multilib_tsim)
{{ m.upload_artifacts(tag='cpu', filenames=tvm_multilib_tsim) }}
timeout(time: max_time, unit: 'MINUTES') {
ci_setup(ci_cpu)
// sh "${docker_run} ${ci_cpu} ./tests/scripts/task_golang.sh"
Expand Down Expand Up @@ -561,7 +523,7 @@ stage('Build') {
label: 'Create i386 cmake config',
)
make(ci_i386, 'build', '-j2')
pack_lib('i386', tvm_multilib_tsim)
{{ m.upload_artifacts(tag='i386', filenames=tvm_multilib_tsim) }}
}
}
} else {
Expand All @@ -578,7 +540,7 @@ stage('Build') {
label: 'Create ARM cmake config',
)
make(ci_arm, 'build', '-j4')
pack_lib('arm', tvm_multilib)
{{ m.upload_artifacts(tag='arm', filenames=tvm_multilib) }}
}
}
} else {
Expand All @@ -595,8 +557,7 @@ stage('Build') {
label: 'Create QEMU cmake config',
)
make(ci_qemu, 'build', '-j2')
pack_lib('qemu', tvm_lib)
pack_microtvm_template_projects('qemu')
{{ m.upload_artifacts(tag='qemu', filenames=tvm_lib, folders=microtvm_template_projects) }}
}
}
} else {
Expand All @@ -613,13 +574,14 @@ stage('Build') {
label: 'Create Hexagon cmake config',
)
make(ci_hexagon, 'build', '-j2')
pack_lib('hexagon', tvm_lib)
{{ m.upload_artifacts(tag='hexagon', filenames=tvm_lib) }}
}
}
} else {
Utils.markStageSkippedForConditional('BUILD: Hexagon')
}
}
},
)
}
}

Expand All @@ -640,14 +602,14 @@ stage('Test') {
platform="gpu",
) %}
{% if shard_index == 1 %}
unpack_lib('gpu2', tvm_multilib)
{{ m.download_artifacts(tag='gpu2', filenames=tvm_multilib) }}
cpp_unittest(ci_gpu)

unpack_lib('gpu', tvm_multilib)
{{ m.download_artifacts(tag='gpu', filenames=tvm_multilib) }}
ci_setup(ci_gpu)
cpp_unittest(ci_gpu)
{% else %}
unpack_lib('gpu', tvm_multilib)
{{ m.download_artifacts(tag='gpu', filenames=tvm_multilib) }}
ci_setup(ci_gpu)
{% endif %}
{% if shard_index == 2 or num_shards < 2 %}
Expand All @@ -672,7 +634,7 @@ stage('Test') {
ws="tvm/integration-python-cpu",
platform="cpu",
) %}
unpack_lib('cpu', tvm_multilib_tsim)
{{ m.download_artifacts(tag='cpu', filenames=tvm_multilib_tsim) }}
ci_setup(ci_cpu)
sh (
script: "${docker_run} ${ci_cpu} ./tests/scripts/task_python_integration.sh",
Expand All @@ -685,7 +647,7 @@ stage('Test') {
ws="tvm/ut-python-cpu",
platform="cpu",
) %}
unpack_lib('cpu', tvm_multilib_tsim)
{{ m.download_artifacts(tag='cpu', filenames=tvm_multilib_tsim) }}
ci_setup(ci_cpu)
cpp_unittest(ci_cpu)
python_unittest(ci_cpu)
Expand All @@ -702,7 +664,7 @@ stage('Test') {
ws="tvm/integration-python-i386",
platform="i386",
) %}
unpack_lib('i386', tvm_multilib)
{{ m.download_artifacts(tag='i386', filenames=tvm_multilib) }}
ci_setup(ci_i386)
{% if shard_index == 1 %}
cpp_unittest(ci_i386)
Expand All @@ -721,7 +683,7 @@ stage('Test') {
platform="hexagon",
num_shards=4,
) %}
unpack_lib('hexagon', tvm_lib)
{{ m.download_artifacts(tag='hexagon', filenames=tvm_lib) }}
ci_setup(ci_hexagon)
{% if shard_index == 1 %}
cpp_unittest(ci_hexagon)
Expand All @@ -741,8 +703,7 @@ stage('Test') {
ws="tvm/test-qemu",
platform="qemu",
) %}
unpack_lib('qemu', tvm_lib)
unpack_microtvm_template_projects('qemu')
{{ m.download_artifacts(tag='qemu', filenames=tvm_lib, folders=microtvm_template_projects) }}
ci_setup(ci_qemu)
cpp_unittest(ci_qemu)
sh (
Expand All @@ -760,7 +721,7 @@ stage('Test') {
ws="tvm/ut-python-arm",
platform="arm",
) %}
unpack_lib('arm', tvm_multilib)
{{ m.download_artifacts(tag='arm', filenames=tvm_multilib) }}
ci_setup(ci_arm)
cpp_unittest(ci_arm)
sh (
Expand All @@ -778,7 +739,7 @@ stage('Test') {
node="ARM", ws="tvm/ut-python-arm",
platform="arm",
) %}
unpack_lib('arm', tvm_multilib)
{{ m.download_artifacts(tag='arm', filenames=tvm_multilib) }}
ci_setup(ci_arm)
python_unittest(ci_arm)
sh (
Expand All @@ -793,7 +754,7 @@ stage('Test') {
ws="tvm/topi-python-gpu",
platform="gpu",
) %}
unpack_lib('gpu', tvm_multilib)
{{ m.download_artifacts(tag='gpu', filenames=tvm_multilib) }}
ci_setup(ci_gpu)
sh (
script: "${docker_run} ${ci_gpu} ./tests/scripts/task_python_topi.sh",
Expand All @@ -806,7 +767,7 @@ stage('Test') {
ws="tvm/frontend-python-gpu",
platform="gpu",
) %}
unpack_lib('gpu', tvm_multilib)
{{ m.download_artifacts(tag='gpu', filenames=tvm_multilib) }}
ci_setup(ci_gpu)
sh (
script: "${docker_run} ${ci_gpu} ./tests/scripts/task_python_frontend.sh",
Expand All @@ -819,7 +780,7 @@ stage('Test') {
ws="tvm/frontend-python-cpu",
platform="cpu",
) %}
unpack_lib('cpu', tvm_multilib)
{{ m.download_artifacts(tag='cpu', filenames=tvm_multilib) }}
ci_setup(ci_cpu)
sh (
script: "${docker_run} ${ci_cpu} ./tests/scripts/task_python_frontend_cpu.sh",
Expand All @@ -832,7 +793,7 @@ stage('Test') {
ws="tvm/frontend-python-arm",
platform="arm",
) %}
unpack_lib('arm', tvm_multilib)
{{ m.download_artifacts(tag='arm', filenames=tvm_multilib) }}
ci_setup(ci_arm)
sh (
script: "${docker_run} ${ci_arm} ./tests/scripts/task_python_frontend_cpu.sh",
Expand All @@ -844,16 +805,15 @@ stage('Test') {
node('GPU') {
ws({{ m.per_exec_ws('tvm/docs-python-gpu') }}) {
init_git()
unpack_lib('gpu', tvm_multilib)
unpack_microtvm_template_projects('gpu')
{{ m.download_artifacts(tag='gpu', filenames=tvm_multilib, folders=microtvm_template_projects) }}
timeout(time: 180, unit: 'MINUTES') {
ci_setup(ci_gpu)
sh (
script: "${docker_run} ${ci_gpu} ./tests/scripts/task_python_docs.sh",
label: 'Build docs',
)
}
pack_lib('docs', 'docs.tgz')
{{ m.upload_artifacts(tag='docs', filenames=["docs.tgz"]) }}
archiveArtifacts(artifacts: 'docs.tgz', fingerprint: true)
}
}
Expand Down Expand Up @@ -928,7 +888,7 @@ stage('Deploy') {
if (env.BRANCH_NAME == 'main' && env.DOCS_DEPLOY_ENABLED == 'yes') {
node('CPU') {
ws({{ m.per_exec_ws('tvm/deploy-docs') }}) {
unpack_lib('docs', 'docs.tgz')
{{ m.download_artifacts(tag='docs', filenames=["docs.tgz"]) }}
deploy_docs()
}
}
Expand Down
30 changes: 30 additions & 0 deletions jenkins/macros.j2
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,33 @@
}
},
{% endmacro %}

{% macro upload_artifacts(tag, filenames, folders=[]) %}
sh(
script: """
set -eux
{% for filename in filenames %}
aws s3 cp {{ filename }} s3://${s3_prefix}/{{ tag }}/{{ filename }}
{% endfor %}
{% for folder in folders %}
aws s3 cp {{ folder }} s3://${s3_prefix}/{{ tag }}/{{ folder }} --recursive
{% endfor %}
""",
label: 'Upload artifacts to S3',
)
{% endmacro %}

{% macro download_artifacts(tag, filenames, folders=[]) %}
sh(
script: """
set -eux
{% for filename in filenames %}
aws s3 cp s3://${s3_prefix}/{{ tag }}/{{ filename }} {{ filename }}
{% endfor %}
{% for folder in folders %}
aws s3 cp s3://${s3_prefix}/{{ tag }}/{{ folder }} {{ folder }} --recursive
{% endfor %}
""",
label: 'Download artifacts from S3',
)
{% endmacro %}
11 changes: 8 additions & 3 deletions python/tvm/relay/frontend/tflite.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ def get_tensor_type_as_numpy(self, tensor_wrapper):
return {
TensorType.UINT8: np.uint8,
TensorType.INT8: np.int8,
TensorType.INT16: np.int16,
TensorType.FLOAT16: np.float16,
TensorType.FLOAT32: np.float32,
TensorType.INT32: np.int32,
Expand Down Expand Up @@ -430,6 +431,8 @@ def get_tensor_type_str(self, tensor_type):

if tensor_type == TensorType.INT8:
return "int8"
if tensor_type == TensorType.INT16:
return "int16"
if tensor_type == TensorType.UINT8:
return "uint8"
if tensor_type == TensorType.FLOAT16:
Expand Down Expand Up @@ -2149,7 +2152,9 @@ def convert_conv(self, op, conv_type):
qnn_conv2d_params = dict(params)
qnn_conv2d_params["input_zero_point"] = input_tensor.qnn_params["zero_point"]
qnn_conv2d_params["kernel_zero_point"] = weight_tensor.qnn_params["zero_point"]
qnn_conv2d_params["out_dtype"] = "int32"
qnn_conv2d_params["out_dtype"] = (
"int64" if output_tensor_type_str == "int16" else "int32"
)
qnn_conv2d_params["input_scale"] = input_tensor.qnn_params["scale"]
qnn_conv2d_params["kernel_scale"] = weight_tensor.qnn_params["scale"]
out = _qnn.op.conv2d(in_expr, weight_expr, **qnn_conv2d_params)
Expand All @@ -2160,8 +2165,8 @@ def convert_conv(self, op, conv_type):
if len(input_tensors) == 3:
bias_tensor = input_tensors[2]
bias_tensor_type = bias_tensor.tensor.Type()
# bias tensor type should be INT32 (quantization) or FLOAT32
assert bias_tensor_type in (TensorType.INT32, TensorType.FLOAT32)
# bias tensor type should be INT32 (int8 qnn) or INT64 (int16 qnn) or FLOAT32
assert bias_tensor_type in (TensorType.INT32, TensorType.INT64, TensorType.FLOAT32)
bias_tensor_type_str = self.get_tensor_type_str(bias_tensor_type)
if self.has_expr(bias_tensor.tensor_idx):
bias_expr = self.get_expr(bias_tensor.tensor_idx)
Expand Down
Loading

0 comments on commit 328608f

Please sign in to comment.