Skip to content

Commit

Permalink
DRAFT: circle-quantize with alternative names
Browse files Browse the repository at this point in the history
on-going draft to support alternative names.

Signed-off-by: SaeHie Park <[email protected]>
  • Loading branch information
seanshpark committed Sep 11, 2023
1 parent f0a308f commit f632982
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 0 deletions.
19 changes: 19 additions & 0 deletions compiler/circle-quantizer/onnx_name.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[onecc]
one-import-onnx=True
one-optimize=True
one-quantize=True
include=O1

[one-import-onnx]
input_path=onnx_conv2d_conv2d.onnx
output_path=onnx_conv2d_conv2d.circle

[one-optimize]
input_path=onnx_conv2d_conv2d.circle
output_path=onnx_conv2d_conv2d.opt.circle
convert_nchw_to_nhwc=True

[one-quantize]
input_path=onnx_conv2d_conv2d.opt.circle
output_path=onnx_conv2d_conv2d.opt.qm.circle
quant_config=onnx_name.qconfig.json
21 changes: 21 additions & 0 deletions compiler/circle-quantizer/onnx_name.qconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"default_quantization_dtype" : "uint8",
"default_granularity" : "layer",
"layers" : [
{
"names" : [
"onnx_tf_prefix_Relu_3;Add_1;convolution_1;Const_3"
],
"alternate": [
{
"onnx_tf_prefix_Relu_3;Add_1;convolution_1;Const_3":
[
"onnx_tf_prefix_Relu_3;Add_1;convolution_1;Const"
]
}
],
"dtype" : "int16",
"granularity" : "channel"
}
]
}
33 changes: 33 additions & 0 deletions compiler/circle-quantizer/src/CircleQuantizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,39 @@ std::vector<std::shared_ptr<LayerParam>> read_layer_params(std::string &filename
}
}

// alternate names
for (auto layer : layers)
{
const std::string key_alt_names = "alternate";
if (layer.isMember(key_alt_names))
{
auto alternate = layer[key_alt_names][0];
for (auto altkey : alternate.getMemberNames())
{
bool found = false;
for (auto &l : p)
{
std::cout << "!!! altkey: " << altkey << std::endl;
if (l->name == altkey)
{
std::cout << "!!! found" << std::endl;
found = true;
for (auto altvalue : alternate[altkey])
{
std::cout << "!!! altvalue: " << altvalue << std::endl;
l->altnames.push_back(altvalue.asString());
}
break;
}
}
if (!found)
{
throw std::runtime_error("'" + key_alt_names + "' item not found for '" + altkey + "'");
}
}
}
}

return p;
}

Expand Down
1 change: 1 addition & 0 deletions compiler/luci/pass/include/luci/CircleQuantizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class CircleQuantizer final
std::string name;
std::string dtype;
std::string granularity;
std::vector<std::string> altnames;
};

enum Algorithm
Expand Down
100 changes: 100 additions & 0 deletions compiler/one-cmds/one-prepare-venv.tf280
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#!/bin/bash

# Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# 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.

set -e

DRIVER_PATH="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

VENV_ACTIVATE=${DRIVER_PATH}/venv/bin/activate
# NOTE please use venv's python instead of python after `source activation`.
# This script is called by debian maintainer script, i.e. `postinst`.
# Since debian maintainer script is called with sudo, `source activation` is ignored.
VENV_PYTHON=${DRIVER_PATH}/venv/bin/python

if [ ! -f ${VENV_ACTIVATE} ]; then
# Create python virtual enviornment
python3.8 -m venv "${DRIVER_PATH}/venv"
fi

# NOTE version
# - https://github.com/onnx/onnx/blob/master/docs/Versioning.md
# - https://github.com/onnx/onnx-tensorflow/blob/master/Versioning.md

VER_TENSORFLOW=2.8.0
VER_ONNX=1.11.0
VER_ONNXRUNTIME=1.11.0
VER_ONNX_TF=1.10.0
VER_PYDOT=1.4.2

# Install tensorflow

PIP_TRUSTED_HOST="--trusted-host pypi.org "
PIP_TRUSTED_HOST+="--trusted-host pypi.python.org "
PIP_TRUSTED_HOST+="--trusted-host files.pythonhosted.org "
PIP_TRUSTED_HOST+="--trusted-host download.pytorch.org "

PIP_TIMEOUT="--default-timeout=1000 "

PIP_OPTIONS="${PIP_TIMEOUT} ${PIP_TRUSTED_HOST}"

# NOTE $ONE_PREPVENV_PIP_OPTION is to provide additional PIP options
# such as ceritificate file behind firewall
# ex) ONE_PREPVENV_PIP_OPTION="--cert SomePrivateCetificate.crt" ./one-prepare-venv
if [[ ! -z "$ONE_PREPVENV_PIP_OPTION" ]]; then
PIP_OPTIONS+=" ${ONE_PREPVENV_PIP_OPTION} "
fi

${VENV_PYTHON} -m pip ${PIP_OPTIONS} install --upgrade pip setuptools
if [ -n "${EXT_TENSORFLOW_WHL}" ]; then
${VENV_PYTHON} -m pip ${PIP_OPTIONS} install ${EXT_TENSORFLOW_WHL}
else
${VENV_PYTHON} -m pip ${PIP_OPTIONS} install tensorflow-cpu==${VER_TENSORFLOW}
fi
${VENV_PYTHON} -m pip ${PIP_OPTIONS} install Pillow
# TODO remove version fix, https://github.com/Samsung/ONE/issues/9240
${VENV_PYTHON} -m pip ${PIP_OPTIONS} install tensorflow_probability==0.16.0
# TODO remove version fix, https://github.com/Samsung/ONE/issues/10481
${VENV_PYTHON} -m pip ${PIP_OPTIONS} install tensorflow_addons==0.16.1

# Install PyTorch and ONNX related
# NOTE set ONE_PREPVENV_TORCH_STABLE to override 'torch_stable.html' URL.
# torch_stable.html points to download URL of torch wheel file(s)
# but sometimes the server gets unstable, especially from in-house CI.
TORCH_STABLE_URL="https://download.pytorch.org/whl/torch_stable.html"
if [[ ! -z "$ONE_PREPVENV_TORCH_STABLE" ]]; then
TORCH_STABLE_URL="${ONE_PREPVENV_TORCH_STABLE}"
fi
# TODO remove torch message
echo "Torch from '${ONE_PREPVENV_TORCH_STABLE}' -> '${TORCH_STABLE_URL}'"
${VENV_PYTHON} -m pip ${PIP_OPTIONS} install torch==1.11.0+cpu -f ${TORCH_STABLE_URL}

${VENV_PYTHON} -m pip ${PIP_OPTIONS} install onnx==${VER_ONNX}

${VENV_PYTHON} -m pip ${PIP_OPTIONS} install onnxruntime==${VER_ONNXRUNTIME}

# Provide install of custom onnx-tf
if [ -n "${EXT_ONNX_TF_WHL}" ]; then
${VENV_PYTHON} -m pip ${PIP_OPTIONS} install ${EXT_ONNX_TF_WHL}
else
${VENV_PYTHON} -m pip ${PIP_OPTIONS} install onnx-tf==${VER_ONNX_TF}
fi

# NOTE refer https://github.com/protocolbuffers/protobuf/issues/10051
# TODO remove this when issue is resolved
${VENV_PYTHON} -m pip ${PIP_OPTIONS} install --upgrade protobuf==3.20.1

# Install pydot for visq
${VENV_PYTHON} -m pip ${PIP_OPTIONS} install pydot==${VER_PYDOT}

0 comments on commit f632982

Please sign in to comment.