Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

F/no more cstrings #79

Open
wants to merge 18 commits into
base: ubit
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ tests/*/cpp
*/*/models
tests/deep_mlp/data
.vscode
*.pyc
.*.pyc
*.swp
.*.swp
300 changes: 183 additions & 117 deletions utensor_cgen/backend/operators.py

Large diffs are not rendered by default.

53 changes: 47 additions & 6 deletions utensor_cgen/backend/snippets/_snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,23 @@
"CommentSnippet", "ContextHeaderSnippet",
"ContextSnippetsContainer", "QuantizedAddOpSnippet",
"QuantizedMulOpSnippet",
"CreateTensorBinarySnippet", "WeightSnippet",
"CreateTensorBinarySnippet", "WeightSnippet", "TensorStringReferenceSnippet",
"ContextGlobalArrayContainer", "QuantRangeForMultiplicationSnippet",
"FusedConv2DOpMaxpoolSnippet", "QuantizedFusedConv2DMaxpoolOpSnippet",
"GatherOpSnippet",
"CreateTensorRamSnippet", "Uint8Q7OriginSnippet"]

#TODO Put this in the correct location
def mhash(mstr):
"""
Simple java string hash
"""
v = int(7)
for c in mstr:
v = (v*31 + ord(c)) & 0xffffffff
return v


# TODO: Better abstraction, i.e a better backend for code generation
class CreateTensorIdxSnippet(Snippet):
__template_name__ = "snippets/create_tensor_idx.cpp"
Expand Down Expand Up @@ -666,21 +677,23 @@ def __init__(self, inputs, output, strides, ksize, padding,
self.template_vars["to_eval"] = to_eval

class QuantizedFusedConv2DMaxpoolOpSnippet(Snippet):
__template_name__ = "snippets/fused_conv2d_maxpool_op.cpp"
__template_name__ = "snippets/quantized_fused_conv2d_maxpool_op.cpp"
__headers__ = set(['"uTensor/ops/MatrixOps.hpp"'])

def __init__(self, inputs, output, strides, ksize, padding,
in_dtype, filter_dtype, out_dtype,
def __init__(self, inputs, outputs, strides, ksize, padding,
in_dtype, filter_dtype, out_dtypes,
ref_count=0,
to_eval=False):
Snippet.__init__(self)
if ref_count:
self.template_vars["ref_count"] = ref_count
print(outputs)
print(out_dtypes)
self.template_vars["inputs"] = inputs
self.template_vars["output"] = output
self.template_vars["outputs"] = outputs
self.template_vars["in_dtype"] = NP_TYPES_MAP[in_dtype].tensor_type_str
self.template_vars["filter_dtype"] = NP_TYPES_MAP[filter_dtype].tensor_type_str
self.template_vars["out_dtype"] = NP_TYPES_MAP[out_dtype].tensor_type_str
self.template_vars["out_dtypes"] = [NP_TYPES_MAP[out_dtype].tensor_type_str for out_dtype in out_dtypes]
self.template_vars["strides"] = strides
self.template_vars["ksize"] = ksize
self.template_vars["padding"] = padding
Expand Down Expand Up @@ -766,10 +779,38 @@ def __init__(self, guard_name, graph_name, placeholders=None):
self.template_vars["graph_name"] = graph_name
self.template_vars["placeholders"] = placeholders

class TensorStringReferenceSnippet(Snippet):
__template_name__ = "snippets/tensor_string_reference.hpp"
__headers__ = set([])
__references__ = set([])

@classmethod
def add_reference(cls, sref_name):
cls.__references__.add(sref_name)

@classmethod
def have_reference(cls, sref_name):
return sref_name not in cls.__references__

def __init__(self, sref_name):
Snippet.__init__(self)
self.template_vars['sref_name'] = sref_name
self.template_vars['string_id'] = mhash(sref_name)
# Dont render duplicates
self.renderable = self.have_reference(sref_name)
self.add_reference(sref_name)

def render(self):
if self.renderable:
return Snippet.render(self)
else:
return ''

class WeightSnippet(Snippet):
__template_name__ = "snippets/weight_snippet.hpp"
__headers__ = set([])


def __init__(self, inline_name, type, shape, value):
Snippet.__init__(self)
length = np.prod(shape)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <stdint.h>
{% for snippet in snippets%}
{{snippet.render()}}
{% endfor %}
Expand Down
10 changes: 5 additions & 5 deletions utensor_cgen/backend/snippets/templates/snippets/add_op.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ S_TENSOR {{sptr_name}};
{% endif %}
{
{% if ref_count %}
ctx.add(new RamTensor<{{out_dtype}}>(), "{{output}}", {{ref_count}});
ctx.add(new RamTensor<{{out_dtype}}>(), {{output}}, {{ref_count}});
{% else %}
ctx.add(new RamTensor<{{out_dtype}}>(), "{{output}}");
ctx.add(new RamTensor<{{out_dtype}}>(), {{output}});
{% endif %}
ctx.push(new AddOp<{{in_dtype}}, {{out_dtype}}>(),
{ {% for tname in inputs[:-1]%}"{{tname}}", {%endfor%}"{{inputs[-1]}}" },
{ "{{output}}" });
{ {% for tname in inputs[:-1]%}{{tname}}, {%endfor%}{{inputs[-1]}} },
{ {{output}} });
{% if to_eval %}
ctx.eval();
{% endif %}
}
}
12 changes: 6 additions & 6 deletions utensor_cgen/backend/snippets/templates/snippets/argmax_op.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ S_TENSOR {{sptr_name}};
{% endif %}
{
{% if ref_count %}
ctx.add(new RamTensor<{{out_dtype}}>(), "{{output}}", {{ref_count}});
ctx.add(new RamTensor<{{out_dtype}}>(), {{output}}, {{ref_count}});
{% else %}
ctx.add(new RamTensor<{{out_dtype}}>(), "{{output}}");
ctx.add(new RamTensor<{{out_dtype}}>(), {{output}});
{% endif %}
ctx.push(new ArgMaxOp<{{in_dtype}}, {{out_dtype}}>(),
{ {% for tname in inputs[:-1]%}"{{tname}}", {%endfor%}"{{inputs[-1]}}" },
{ "{{output}}" });
{ {% for tname in inputs[:-1]%}{{tname}}, {%endfor%}{{inputs[-1]}} },
{ {{output}} });
{% if create_sptr %}
{{sptr_name}} = ctx.get("{{output}}");
{{sptr_name}} = ctx.get({{output}});
{% endif %}
{% if to_eval %}
ctx.eval();
{% endif %}
}
}
20 changes: 10 additions & 10 deletions utensor_cgen/backend/snippets/templates/snippets/cmsis_nn_fc_op.cpp
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
{
{#
// {%if ref_counts%}
// ctx.add(new RamTensor<{{out_dtypes[0]}}>(), "{{outputs[0]}}", {{ref_counts[0]}});
// ctx.add(new RamTensor<{{out_dtypes[0]}}>({1}), "{{outputs[1]}}", {{ref_counts[1]}});
// ctx.add(new RamTensor<{{out_dtypes[0]}}>({1}), "{{outputs[2]}}", {{ref_counts[2]}});
// ctx.add(new RamTensor<{{out_dtypes[0]}}>(), {{outputs[0]}}, {{ref_counts[0]}});
// ctx.add(new RamTensor<{{out_dtypes[0]}}>({1}), {{outputs[1]}}, {{ref_counts[1]}});
// ctx.add(new RamTensor<{{out_dtypes[0]}}>({1}), {{outputs[2]}}, {{ref_counts[2]}});
// {%else%}
// ctx.add(new RamTensor<{{out_dtypes[0]}}>(), "{{outputs[0]}}");
// ctx.add(new RamTensor<{{out_dtypes[0]}}>({1}), "{{outputs[1]}}");
// ctx.add(new RamTensor<{{out_dtypes[0]}}>({1}), "{{outputs[2]}}");
// ctx.add(new RamTensor<{{out_dtypes[0]}}>(), {{outputs[0]}});
// ctx.add(new RamTensor<{{out_dtypes[0]}}>({1}), {{outputs[1]}});
// ctx.add(new RamTensor<{{out_dtypes[0]}}>({1}), {{outputs[2]}});
// {%endif%}
#}

{%if ref_counts%}
ctx.add(new RamTensor<{{out_dtype}}>(), "{{output}}", {{ref_counts[0]}});
ctx.add(new RamTensor<{{out_dtype}}>(), {{output}}, {{ref_counts[0]}});
{%else%}
ctx.add(new RamTensor<{{out_dtype}}>(), "{{output}}");
ctx.add(new RamTensor<{{out_dtype}}>(), {{output}});
{%endif%}

ctx.push(new FullyConnectedLayerCmsisOp<{{out_dtype}}>(),
{ {%for tname in inputs[:-1] %}"{{tname}}", {%endfor%}"{{inputs[-1]}}" },
{ "{{output}}" });
{ {%for tname in inputs[:-1] %}{{tname}}, {%endfor%}{{inputs[-1]}} },
{ {{output}} });
{%if to_eval%}
ctx.eval();
{%endif%}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ S_TENSOR {{sptr_name}};
{% endif %}
{
{% if ref_count %}
ctx.add(new RamTensor<q7_t>(), "{{output}}", {{ref_count}});
ctx.add(new RamTensor<q7_t>(), {{output}}, {{ref_count}});
{% else %}
ctx.add(new RamTensor<q7_t>(), "{{output}}");
ctx.add(new RamTensor<q7_t>(), {{output}});
{% endif %}
ctx.push(new Uint8Q7OriginOp(),
{ {% for tname in inputs[:-1]%}"{{tname}}", {%endfor%}"{{inputs[-1]}}" },
{ "{{output}}" });
{ {% for tname in inputs[:-1]%}{{tname}}, {%endfor%}{{inputs[-1]}} },
{ {{output}} });
{% if to_eval %}
ctx.eval();
{% endif %}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
{% if ref_count %}
ctx.add(new RamTensor<{{out_dtype}}>(), "{{output}}", {{ref_count}});
ctx.add(new RamTensor<{{out_dtype}}>(), {{output}}, {{ref_count}});
{% else %}
ctx.add(new RamTensor<{{out_dtype}}>(), "{{output}}");
ctx.add(new RamTensor<{{out_dtype}}>(), {{output}});
{% endif %}
ctx.push(new ConvOp<{{in_dtype}}, {{filter_dtype}}, {{out_dtype}}>({ {% for s in strides[:-1]%}{{s}}, {%endfor%}{{strides[-1]}} }, {{padding}}),
{ {% for tname in inputs[:-1]%}"{{tname}}", {%endfor%}"{{inputs[-1]}}" },
{ "{{output}}"});
{ {% for tname in inputs[:-1]%}{{tname}}, {%endfor%}{{inputs[-1]}} },
{ {{output}}});
{% if to_eval %}
ctx.eval();
{% endif %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ S_TENSOR {{sptr_name}};
{
{%if ref_count%}
ctx.add(new {{tensor_type}}<{{dtype}}>({{tensor_shape}}, {{inline_name}}),
"{{tensor_name}}",
{{ tensor_name }},
{{ref_count}});
{% else %}
ctx.add(new {{tensor_type}}<{{dtype}}>({{tensor_shape}}, {{inline_name}}),
"{{tensor_name}}");
{{ tensor_name }});
{%endif%}
{% if create_sptr %}
{{sptr_name}} = ctx.get("{{tensor_name}}");
{{sptr_name}} = ctx.get({{ tensor_name }});
{% endif %}
{%if to_eval%}
ctx.eval();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ S_TENSOR {{sptr_name}};
{
TensorIdxImporter t_import;
{% if ref_count %}
ctx.add(t_import.{{importer_dtype}}_import("{{idx_path}}"),
"{{tensor_name}}",
ctx.add(t_import.{{importer_dtype}}_import({{idx_path}}),
{{tensor_name}},
{{ref_count}});
{% else %}
ctx.add(t_import.{{importer_dtype}}_import("{{idx_path}}"),
"{{tensor_name}}");
ctx.add(t_import.{{importer_dtype}}_import({{idx_path}}),
{{tensor_name}});
{% endif %}
{% if create_sptr %}
{{sptr_name}} = ctx.get("{{tensor_name}}");
{{sptr_name}} = ctx.get({{tensor_name}});
{% endif %}
{% if to_eval %}
ctx.eval();
{% endif %}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
S_TENSOR {{sptr_name}};
{% endif %}
{
ctx.add(new {{tensor_type}}<{{dtype}}>({% if tensor_shape %}{{tensor_shape}}{%endif%}), "{{tensor_name}}"{%if ref_count%}, {{ref_count}}{%endif%});
ctx.add(new {{tensor_type}}<{{dtype}}>({% if tensor_shape %}{{tensor_shape}}{%endif%}), {{tensor_name}}{%if ref_count%}, {{ref_count}}{%endif%});
{% if create_sptr %}
{{sptr_name}} = ctx.get("{{tensor_name}}");
{{sptr_name}} = ctx.get({{tensor_name}});
{% endif %}
{%if to_eval%}
ctx.eval();
{%endif%}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ S_TENSOR {{sptr_name}};
{% endif %}
{
{% if ref_count %}
ctx.add(new RamTensor<{{out_dtype}}>(), "{{output}}", {{ref_count}});
ctx.add(new RamTensor<{{out_dtype}}>(), {{output}}, {{ref_count}});
{% else %}
ctx.add(new RamTensor<{{out_dtype}}>(), "{{output}}");
ctx.add(new RamTensor<{{out_dtype}}>(), {{output}});
{% endif %}
ctx.push(new DequantizeOp(),
{ {% for tname in inputs[:-1]%}"{{tname}}", {%endfor%}"{{inputs[-1]}}" },
{ "{{output}}" });
{ {% for tname in inputs[:-1]%}{{tname}}, {%endfor%}{{inputs[-1]}} },
{ {{output}} });
{% if create_sptr %}
{{sptr_name}} = ctx.get("{{output}}");
{{sptr_name}} = ctx.get({{output}});
{% endif %}
{%if to_eval%}
ctx.eval();
{%endif%}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
{% if ref_count %}
ctx.add(new RamTensor<{{out_dtype}}>(), "{{output}}", {{ref_count}});
ctx.add(new RamTensor<{{out_dtype}}>(), {{output}}, {{ref_count}});
{% else %}
ctx.add(new RamTensor<{{out_dtype}}>(), "{{output}}");
ctx.add(new RamTensor<{{out_dtype}}>(), {{output}});
{% endif %}
ctx.push(new FusedConvMaxpoolOp<{{in_dtype}}, {{filter_dtype}}, {{out_dtype}}>({ {% for s in strides[:-1]%}{{s}}, {%endfor%}{{strides[-1]}} }, { {% for s in ksize[:-1]%}{{s}}, {%endfor%}{{ksize[-1]}} },{{padding}}),
{ {% for tname in inputs[:-1]%}"{{tname}}", {%endfor%}"{{inputs[-1]}}" },
{ "{{output}}"});
{ {% for tname in inputs[:-1]%}{{tname}}, {%endfor%}{{inputs[-1]}} },
{ {{output}}});
{% if to_eval %}
ctx.eval();
{% endif %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ S_TENSOR {{sptr_name}};
{% endif %}
{
{% if ref_count %}
ctx.add(new RamTensor<{{out_dtype}}>(), "{{output}}", {{ref_count}});
ctx.add(new RamTensor<{{out_dtype}}>(), {{output}}, {{ref_count}});
{% else %}
ctx.add(new RamTensor<{{out_dtype}}>(), "{{output}}");
ctx.add(new RamTensor<{{out_dtype}}>(), {{output}});
{% endif %}
ctx.push(new GatherOp<{{in_dtype}}>(),
{ {% for tname in inputs[:-1]%}"{{tname}}", {%endfor%}"{{inputs[-1]}}" },
{ "{{output}}" });
{ {% for tname in inputs[:-1]%}{{tname}}, {%endfor%}{{inputs[-1]}} },
{ {{output}} });
{% if to_eval %}
ctx.eval();
{% endif %}
Expand Down
12 changes: 6 additions & 6 deletions utensor_cgen/backend/snippets/templates/snippets/matmul_op.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ S_TENSOR {%for sptr_name in sptr_names[:-1]%}{{sptr_name}}, {%endfor%} {{sptr_na
{% endif %}
{
{% if ref_count %}
ctx.add(new RamTensor<{{out_dtype}}>(), "{{output}}", {{ref_count}});
ctx.add(new RamTensor<{{out_dtype}}>(), {{output}}, {{ref_count}});
{% else %}
ctx.add(new RamTensor<{{out_dtype}}>(), "{{output}}");
ctx.add(new RamTensor<{{out_dtype}}>(), {{output}});
{% endif %}
ctx.push(new MatMulOp<{{x_dtype}}, {{w_dtype}}, {{out_dtype}}>(),
{ {%for tname in inputs[:-1] %}"{{tname}}", {% endfor %} "{{inputs[-1]}}" },
{ "{{output}}" });
{ {%for tname in inputs[:-1] %}{{tname}}, {% endfor %} {{inputs[-1]}} },
{ {{output}} });
{% for sptr_name, output in zip(sptr_names, outputs) %}
{{sptr_name}} = ctx.get("{{output}}");
{{sptr_name}} = ctx.get({{output}});
{% endfor %}
{% if to_eval %}
ctx.eval();
{% endif %}
}
}
12 changes: 6 additions & 6 deletions utensor_cgen/backend/snippets/templates/snippets/max_op.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ S_TENSOR {{sptr_name}};
out_tensor = new RamTensor<{{out_dtype}}>();
{%endif%}
{%if ref_count %}
ctx.add(out_tensor, "{{output}}", {{ref_count}});
ctx.add(out_tensor, {{output}}, {{ref_count}});
{%else%}
ctx.add(out_tensor, "{{output}}");
ctx.add(out_tensor, {{output}});
{%endif%}
ctx.push(new MaxOp(),
{ {% for tname in inputs[:-1]%}"{{tname}}", {%endfor%}"{{inputs[-1]}}" },
{ "{{output}}" });
{ {% for tname in inputs[:-1]%}{{tname}}, {%endfor%}{{inputs[-1]}} },
{ {{output}} });
{% if create_sptr %}
{{sptr_name}} = ctx.get("{{output}}");
{{sptr_name}} = ctx.get({{output}});
{% endif %}
{%if to_eval%}
ctx.eval();
{%endif%}
}
}
Loading