diff options
author | Ali Labbene <ali.labbene@st.com> | 2019-12-11 08:59:21 +0100 |
---|---|---|
committer | Ali Labbene <ali.labbene@st.com> | 2019-12-16 16:35:24 +0100 |
commit | 9f95ff5b6ba01db09552b84a0ab79607060a2666 (patch) | |
tree | 8a6e0dda832555c692307869aed49d07ee7facfe /NN/Scripts/NNFunctions/table_gen.py | |
parent | 76177aa280494bb36d7a0bcbda1078d4db717020 (diff) | |
download | st-cmsis-core-lowfat-9f95ff5b6ba01db09552b84a0ab79607060a2666.tar.gz st-cmsis-core-lowfat-9f95ff5b6ba01db09552b84a0ab79607060a2666.tar.bz2 st-cmsis-core-lowfat-9f95ff5b6ba01db09552b84a0ab79607060a2666.zip |
Official ARM version: v5.4.0
Add CMSIS V5.4.0, please refer to index.html available under \docs folder.
Note: content of \CMSIS\Core\Include has been copied under \Include to keep the same structure
used in existing projects, and thus avoid projects mass update
Note: the following components have been removed from ARM original delivery (as not used in ST packages)
- CMSIS_EW2018.pdf
- .gitattributes
- .gitignore
- \Device
- \CMSIS
- \CoreValidation
- \DAP
- \Documentation
- \DoxyGen
- \Driver
- \Pack
- \RTOS\CMSIS_RTOS_Tutorial.pdf
- \RTOS\RTX
- \RTOS\Template
- \RTOS2\RTX
- \Utilities
- All ARM/GCC projects files are deleted from \DSP, \RTOS and \RTOS2
Change-Id: Ia026c3f0f0d016627a4fb5a9032852c33d24b4d3
Diffstat (limited to 'NN/Scripts/NNFunctions/table_gen.py')
-rw-r--r-- | NN/Scripts/NNFunctions/table_gen.py | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/NN/Scripts/NNFunctions/table_gen.py b/NN/Scripts/NNFunctions/table_gen.py new file mode 100644 index 0000000..5db6d3e --- /dev/null +++ b/NN/Scripts/NNFunctions/table_gen.py @@ -0,0 +1,116 @@ +#!/usr/bin/python + +import math + +class Table(object): + + def __init__(self, table_entry=256, table_range=8): + self.table_entry = table_entry + self.table_range = table_range + pass + + def sigmoid(self, x): + return 1 / (1 + math.exp(-1*x)) + + def tanh(self, x): + return (math.exp(2*x)-1) / (math.exp(2*x)+1) + + def fp2q7(self, x): + x_int = math.floor(x*(2**7)+0.5) + if x_int >= 128 : + x_int = 127 + if x_int < -128 : + x_int = -128 + if x_int >= 0 : + return x_int + else : + return 0x100 + x_int + + def fp2q15(self, x): + x_int = math.floor(x*(2**15)+0.5) + if x_int >= 2**15 : + x_int = 2**15-1 + if x_int < -1*2**15 : + x_int = -1*2**15 + if x_int >= 0 : + return x_int + else : + return 0x10000 + x_int + + def table_gen(self): + outfile = open("NNCommonTable.c", "wb") + + outfile.write("/*\n * Common tables for NN\n *\n *\n *\n *\n */\n\n#include \"arm_math.h\"\n#include \"NNCommonTable.h\"\n\n/*\n * Table for sigmoid\n */\n") + + for function_type in ["sigmoid", "tanh"]: + for data_type in [7, 15]: + out_type = "q"+str(data_type)+"_t" + act_func = getattr(self, function_type) + quan_func = getattr(self, 'fp2q'+str(data_type)) + + # unified table + outfile.write('const %s %sTable_q%d[%d] = {\n' % (out_type, function_type, data_type, self.table_entry) ) + for i in range(self.table_entry): + # convert into actual value + if i < self.table_entry/2: + value_q7 = self.table_range * (i) + else: + value_q7 = self.table_range * (i - self.table_entry) + + if data_type == 7: + #outfile.write('%f, ' % (act_func(float(value_q7)/256))) + outfile.write('0x%02x, ' % (quan_func(act_func(float(value_q7)/self.table_entry)))) + else: + #outfile.write('%f, ' % (act_func(float(value_q7)/256))) + outfile.write('0x%04x, ' % (quan_func(act_func(float(value_q7)/self.table_entry)))) + if i % 8 == 7: + outfile.write("\n") + outfile.write("};\n\n") + + for data_type in [15]: + out_type = "q"+str(data_type)+"_t" + act_func = getattr(self, function_type) + quan_func = getattr(self, 'fp2q'+str(data_type)) + + # H-L tables + outfile.write('const %s %sLTable_q%d[%d] = {\n' % (out_type, function_type, data_type, self.table_entry/2)) + for i in range(self.table_entry/2): + # convert into actual value, max value is 16*self.table_entry/4 / 4 + # which is equivalent to self.table_entry / self.table_entry/2 = 2, i.e., 1/4 of 8 + if i < self.table_entry/4: + value_q7 = self.table_range * i / 4 + else: + value_q7 = self.table_range * (i - self.table_entry/2) / 4 + if data_type == 7: + #outfile.write('%f, ' % (act_func(float(value_q7)/256))) + outfile.write('0x%02x, ' % (quan_func(act_func(float(value_q7)/(self.table_entry/2))))) + else: + #outfile.write('%f, ' % (act_func(float(value_q7)/256))) + outfile.write('0x%04x, ' % (quan_func(act_func(float(value_q7)/(self.table_entry/2))))) + if i % 8 == 7: + outfile.write("\n") + outfile.write("};\n\n") + + outfile.write('const %s %sHTable_q%d[%d] = {\n' % (out_type, function_type, data_type, 3*self.table_entry/4)) + for i in range(3 * self.table_entry/4): + # convert into actual value, tageting range (2, 8) + if i < 3*self.table_entry/8 : + value_q7 = self.table_range * ( i + self.table_entry/8 ) + else: + value_q7 = self.table_range * ( i + self.table_entry/8 - self.table_entry) + if data_type == 7: + #outfile.write('%f, ' % (act_func(float(value_q7)/256))) + outfile.write('0x%02x, ' % (quan_func(act_func(float(value_q7)/self.table_entry)))) + else: + #outfile.write('%f, ' % (act_func(float(value_q7)/256))) + outfile.write('0x%04x, ' % (quan_func(act_func(float(value_q7)/self.table_entry)))) + if i % 8 == 7: + outfile.write("\n") + outfile.write("};\n\n") + + outfile.close() + + +mytable = Table(table_entry=256, table_range=16) + +mytable.table_gen() |