hashtable_lookup_quant8.mod.py revision 92f7fdf622816274b89feca200c5413ebed3249f
19018b4b3cbc3dc8d29f771ccf2aa09fe5748cf02Yang Ni#
29018b4b3cbc3dc8d29f771ccf2aa09fe5748cf02Yang Ni# Copyright (C) 2017 The Android Open Source Project
39018b4b3cbc3dc8d29f771ccf2aa09fe5748cf02Yang Ni#
49018b4b3cbc3dc8d29f771ccf2aa09fe5748cf02Yang Ni# Licensed under the Apache License, Version 2.0 (the "License");
59018b4b3cbc3dc8d29f771ccf2aa09fe5748cf02Yang Ni# you may not use this file except in compliance with the License.
69018b4b3cbc3dc8d29f771ccf2aa09fe5748cf02Yang Ni# You may obtain a copy of the License at
79018b4b3cbc3dc8d29f771ccf2aa09fe5748cf02Yang Ni#
89018b4b3cbc3dc8d29f771ccf2aa09fe5748cf02Yang Ni#      http://www.apache.org/licenses/LICENSE-2.0
99018b4b3cbc3dc8d29f771ccf2aa09fe5748cf02Yang Ni#
109018b4b3cbc3dc8d29f771ccf2aa09fe5748cf02Yang Ni# Unless required by applicable law or agreed to in writing, software
119018b4b3cbc3dc8d29f771ccf2aa09fe5748cf02Yang Ni# distributed under the License is distributed on an "AS IS" BASIS,
129018b4b3cbc3dc8d29f771ccf2aa09fe5748cf02Yang Ni# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139018b4b3cbc3dc8d29f771ccf2aa09fe5748cf02Yang Ni# See the License for the specific language governing permissions and
149018b4b3cbc3dc8d29f771ccf2aa09fe5748cf02Yang Ni# limitations under the License.
159018b4b3cbc3dc8d29f771ccf2aa09fe5748cf02Yang Ni#
169018b4b3cbc3dc8d29f771ccf2aa09fe5748cf02Yang Ni
179018b4b3cbc3dc8d29f771ccf2aa09fe5748cf02Yang Nilookups = 4
189018b4b3cbc3dc8d29f771ccf2aa09fe5748cf02Yang Nikeys = 3
199018b4b3cbc3dc8d29f771ccf2aa09fe5748cf02Yang Nirows = 3
209018b4b3cbc3dc8d29f771ccf2aa09fe5748cf02Yang Nifeatures = 2
219018b4b3cbc3dc8d29f771ccf2aa09fe5748cf02Yang Ni
229018b4b3cbc3dc8d29f771ccf2aa09fe5748cf02Yang Nitable = [x for x in range(rows * features)]
239018b4b3cbc3dc8d29f771ccf2aa09fe5748cf02Yang Nifor i in range(rows):
249018b4b3cbc3dc8d29f771ccf2aa09fe5748cf02Yang Ni  for j in range(features):
2592f7fdf622816274b89feca200c5413ebed3249fDong Chen    table[i * features + j] = i * 10 + j
269018b4b3cbc3dc8d29f771ccf2aa09fe5748cf02Yang Ni
279018b4b3cbc3dc8d29f771ccf2aa09fe5748cf02Yang Nimodel = Model()
289018b4b3cbc3dc8d29f771ccf2aa09fe5748cf02Yang Ni
2992f7fdf622816274b89feca200c5413ebed3249fDong Chen# lookup, key, and hits are all int32 stored as float32.
3092f7fdf622816274b89feca200c5413ebed3249fDong Chenlookup = Input("lookup", "TENSOR_FLOAT32", "{%d}" % (lookups))
319018b4b3cbc3dc8d29f771ccf2aa09fe5748cf02Yang Nikey = Input("key", "TENSOR_FLOAT32", "{%d}" % (keys))
3292f7fdf622816274b89feca200c5413ebed3249fDong Chenvalue = Input("value", "TENSOR_QUANT8_ASYMM", "0.0f, 127.5f, {%d, %d}" % (rows, features))
3392f7fdf622816274b89feca200c5413ebed3249fDong Chenoutput = Output("output", "TENSOR_QUANT8_ASYMM", "0.0f, 127.5f, {%d, %d}" % (lookups, features))
349018b4b3cbc3dc8d29f771ccf2aa09fe5748cf02Yang Nihits = Output("hits", "TENSOR_FLOAT32", "{%d}" % (lookups))
359018b4b3cbc3dc8d29f771ccf2aa09fe5748cf02Yang Nimodel = model.Operation("HASHTABLE_LOOKUP", lookup, key, value).To(output)
369018b4b3cbc3dc8d29f771ccf2aa09fe5748cf02Yang Ni
3792f7fdf622816274b89feca200c5413ebed3249fDong Cheninput0 = {lookup:  [123, 250, 255, 0],
3892f7fdf622816274b89feca200c5413ebed3249fDong Chen          key: [0, 123, 255],
399018b4b3cbc3dc8d29f771ccf2aa09fe5748cf02Yang Ni          value: table}
409018b4b3cbc3dc8d29f771ccf2aa09fe5748cf02Yang Ni
419018b4b3cbc3dc8d29f771ccf2aa09fe5748cf02Yang Nioutput0 = {output:
429018b4b3cbc3dc8d29f771ccf2aa09fe5748cf02Yang Ni           [
4392f7fdf622816274b89feca200c5413ebed3249fDong Chen               10, 11, # 2nd item
4492f7fdf622816274b89feca200c5413ebed3249fDong Chen               0, 0,   # Not found
4592f7fdf622816274b89feca200c5413ebed3249fDong Chen               20, 21, # 1st item
4692f7fdf622816274b89feca200c5413ebed3249fDong Chen               0, 1,   # 0th item
479018b4b3cbc3dc8d29f771ccf2aa09fe5748cf02Yang Ni           ],
489018b4b3cbc3dc8d29f771ccf2aa09fe5748cf02Yang Ni           hits:
499018b4b3cbc3dc8d29f771ccf2aa09fe5748cf02Yang Ni           [
509018b4b3cbc3dc8d29f771ccf2aa09fe5748cf02Yang Ni               1, 0, 1, 1,
519018b4b3cbc3dc8d29f771ccf2aa09fe5748cf02Yang Ni           ]
529018b4b3cbc3dc8d29f771ccf2aa09fe5748cf02Yang Ni}
539018b4b3cbc3dc8d29f771ccf2aa09fe5748cf02Yang Ni
549018b4b3cbc3dc8d29f771ccf2aa09fe5748cf02Yang Ni# Instantiate an example
559018b4b3cbc3dc8d29f771ccf2aa09fe5748cf02Yang NiExample((input0, output0))
56