提交 06cfe05e 编写于 作者: L Liangliang He

Merge branch 'support_gpu_float' into 'support_float_in_gpu'

support float in gpu

See merge request !510
......@@ -55,8 +55,11 @@ def main(unused_args):
FLAGS.data_type, FLAGS.runtime, FLAGS.winograd)
if FLAGS.output_type == 'source':
gpu_data_type = ""
if FLAGS.runtime == 'gpu':
gpu_data_type = FLAGS.data_type
source_converter_lib.convert_to_source(output_graph_def, model_checksum, FLAGS.template, FLAGS.obfuscate,
FLAGS.model_tag, FLAGS.output, FLAGS.runtime, FLAGS.embed_model_data)
FLAGS.model_tag, FLAGS.output, FLAGS.runtime, FLAGS.embed_model_data, gpu_data_type)
else:
with open(FLAGS.output, "wb") as f:
f.write(output_graph_def.SerializeToString())
......
......@@ -78,11 +78,11 @@ def rename_tensor(net_def):
op.output[i] = tensor_map[op.output[i]]
class TensorInfo:
def __init__(self, id, t, runtime):
def __init__(self, id, t, runtime, gpu_data_type):
self.id = id
self.data_type = mace_pb2.DataType.Name(t.data_type)
if t.data_type == mace_pb2.DT_FLOAT:
if runtime == 'gpu':
if runtime == 'gpu' and gpu_data_type == 'half':
self.data_type = mace_pb2.DT_HALF
self.data = bytearray(np.array(t.float_data).astype(np.float16).tobytes())
else:
......@@ -96,7 +96,7 @@ class TensorInfo:
def stringfy(value):
return ', '.join('"{0}"'.format(w) for w in value)
def convert_to_source(net_def, mode_pb_checksum, template_dir, obfuscate, model_tag, output, runtime, embed_model_data):
def convert_to_source(net_def, mode_pb_checksum, template_dir, obfuscate, model_tag, output, runtime, embed_model_data, gpu_data_type):
if obfuscate:
obfuscate_name(net_def)
else:
......@@ -115,7 +115,7 @@ def convert_to_source(net_def, mode_pb_checksum, template_dir, obfuscate, model_
offset = 0
counter = 0
for t in net_def.tensors:
tensor_info = TensorInfo(counter, t, runtime)
tensor_info = TensorInfo(counter, t, runtime, gpu_data_type)
# align
if tensor_info.data_type != 'DT_UINT8' and offset % 4 != 0:
padding = 4 - offset % 4
......@@ -167,7 +167,7 @@ def convert_to_source(net_def, mode_pb_checksum, template_dir, obfuscate, model_
# generate model source files
template_name = 'model.jinja2'
tensors = [TensorInfo(i, net_def.tensors[i], runtime) for i in range(len(net_def.tensors))]
tensors = [TensorInfo(i, net_def.tensors[i], runtime, gpu_data_type) for i in range(len(net_def.tensors))]
source = j2_env.get_template(template_name).render(
tensors = tensors,
net = net_def,
......
......@@ -3,6 +3,10 @@
CURRENT_DIR=`dirname $0`
source ${CURRENT_DIR}/env.sh
if [ $# -eq 1 ]; then
DATA_TYPE=$1
fi
bazel build //mace/python/tools:converter || exit 1
rm -rf ${MODEL_CODEGEN_DIR}
mkdir -p ${MODEL_CODEGEN_DIR}
......
......@@ -132,8 +132,13 @@ def generate_random_input(target_soc, model_output_dir,
else:
shutil.copy(input_file_list[i], dst_input_file)
def generate_model_code():
command = "bash tools/generate_model_code.sh"
def generate_model_code(gpu_data_type):
data_type = ""
if gpu_data_type == "half":
data_type = "DT_HALF"
elif gpu_data_type == "float":
data_type = "DT_FLOAT"
command = "bash tools/generate_model_code.sh {}".format(data_type)
run_command(command)
......@@ -319,6 +324,11 @@ def parse_args():
type=str,
default="all",
help="SoCs to build, comma seperated list (getprop ro.board.platform)")
parser.add_argument(
"--gpu_data_type",
type=str,
default="",
help="[half|float]")
return parser.parse_known_args()
def set_environment(configs):
......@@ -412,7 +422,7 @@ def main(unused_args):
model_config['input_nodes'], input_file_list)
if FLAGS.mode == "build" or FLAGS.mode == "all":
generate_model_code()
generate_model_code(FLAGS.gpu_data_type)
build_mace_run_prod(model_name, global_runtime, target_abi,
target_soc, model_output_dir, FLAGS.tuning)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册