Skip to content
Snippets Groups Projects
Commit 26eb0e32 authored by Elizabeth's avatar Elizabeth
Browse files

Implemented fp16 source code generation method

parent 9e7264a5
No related branches found
No related tags found
No related merge requests found
......@@ -110,16 +110,49 @@ def get_new_function_calls(complete_line, knob_config):
new_line.append("%s%s, %s)" % (knob_config.modified_func_name, old_func_call[:-1], ', '.join(knob_config.params)))
orig_func_ind = complete_line.find(knob_config.orig_func_name, line_start_ind)
new_line.append(complete_line[line_start_ind : ])
#print(new_line)
return ''.join(new_line)
def generate_fp32_source(knob_config, new_file, source_file):
pass
def generate_fp16_source(knob_config, new_file, source_file):
pass
def generate_approx_source(knob_config, new_file, source_file, filename_dir):
def generate_fp32_source(new_file, source_file):
# Copy the source code over
new_file.write(source_file.read())
def generate_fp16_source(knob_config, new_file, source_file, orig_source_dir):
file_contents = source_file.read()
# Fix all local paths
last_include_ind = file_contents.rfind("#include")
last_include_newline_ind = file_contents.find("\n", last_include_ind)
include_lines = file_contents[ : last_include_newline_ind].split("\n")
new_file_contents = []
for line in include_lines:
if line.startswith("#"):
include_file = line.split()[1]
if include_file.startswith("\""):
new_include_path = get_new_path(include_file.replace("\"", ""), orig_source_dir.replace("\"", ""))
new_file_contents.append("#include \"%s\"\n" % new_include_path)
else:
new_file_contents.append(line)
new_file_contents.append(file_contents[last_include_newline_ind : ])
new_file_contents = '\n'.join(new_file_contents)
# Replace all tensorOperation calls with tensorHalfOperation calls
# Derived from ../bin/replace_half_calls.py
# NOTE: Not very portable but I don't see another way of ONLY replacing tensorOperation FUNCTION calls
new_file_contents = new_file_contents.replace("tensorConvolution", "tensorHalfConvolution")
new_file_contents = new_file_contents.replace("tensorAdd", "tensorHalfAdd")
new_file_contents = new_file_contents.replace("tensorRelu", "tensorHalfRelu")
new_file_contents = new_file_contents.replace("tensorRelu2", "tensorHalfRelu2")
new_file_contents = new_file_contents.replace("tensorTanh", "tensorHalfTanh")
new_file_contents = new_file_contents.replace("tensorPooling", "tensorHalfPooling")
new_file_contents = new_file_contents.replace("tensorGemmGPU", "tensorHalfGemmGPU")
new_file.write(new_file_contents)
def generate_approx_source(knob_config, new_file, source_file, orig_source_dir):
new_file_contents = []
# Store complete line to handle cases where one line of code is split into two lines
......@@ -130,7 +163,7 @@ def generate_approx_source(knob_config, new_file, source_file, filename_dir):
if line.startswith("#"):
include_file = line.split()[1]
if include_file.startswith("\""):
new_include_path = get_new_path(include_file.replace("\"", ""), filename_dir.replace("\"", ""))
new_include_path = get_new_path(include_file.replace("\"", ""), orig_source_dir.replace("\"", ""))
new_file_contents.append("#include \"%s\"\n" % new_include_path)
else:
new_file_contents.append(line)
......@@ -165,19 +198,18 @@ def generate_source_code(table, dir_name, filename, source_name):
source_name: Filename without the file extension (ex: foo/blah.cc --> blah)
'''
source_file = open(filename, "r")
filename_dir = os.path.dirname(filename)
orig_source_dir = os.path.dirname(filename)
for knob_config in table:
source_file.seek(0, 0)
new_filename = os.path.join(dir_name, "%s_%s.cc" % (source_name, knob_config.id))
new_file = open(new_filename, "w")
if knob_config.approx == Approx.FP16:
pass
generate_fp16_source(knob_config, new_file, source_file, orig_source_dir)
elif knob_config.approx == Approx.FP32:
pass
generate_fp32_source(new_file, source_file)
else:
generate_approx_source(knob_config, new_file, source_file, filename_dir)
generate_approx_source(knob_config, new_file, source_file, orig_source_dir)
new_file.close()
print("Generated source code as %s" % new_filename)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment