[nncase] Update runtime

pull/107/head
sunnycase 2020-03-04 14:31:10 +08:00
parent 573a00dfc8
commit 4c600fcd0a
34 changed files with 246 additions and 46 deletions

View File

@ -1,4 +1,4 @@
/* Copyright 2019 Canaan Inc.
/* Copyright 2019-2020 Canaan Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -147,6 +147,11 @@ struct memory_range
uint32_t size;
};
inline padding operator+(const padding &lhs, const padding &rhs) noexcept
{
return { lhs.before + rhs.before, lhs.after + rhs.after };
}
inline bool operator==(const padding &lhs, const padding &rhs) noexcept
{
return lhs.before == rhs.before && lhs.after == rhs.after;
@ -169,6 +174,16 @@ bool operator!=(const value_range<T> &lhs, const value_range<T> &rhs) noexcept
return lhs.min != rhs.min || lhs.max != rhs.max;
}
inline bool operator==(const scalar &lhs, const scalar &rhs) noexcept
{
return lhs.type == rhs.type && lhs.storage == rhs.storage;
}
inline bool operator!=(const scalar &lhs, const scalar &rhs) noexcept
{
return lhs.type != rhs.type || lhs.storage != rhs.storage;
}
#ifndef DEFINE_ENUM_FLAG_OPERATORS
#define DEFINE_ENUM_FLAG_OPERATORS(ENUMTYPE) \
inline ENUMTYPE operator|(ENUMTYPE a, ENUMTYPE b) { return ENUMTYPE(((int)a) | ((int)b)); } \

View File

@ -1,4 +1,4 @@
/* Copyright 2019 Canaan Inc.
/* Copyright 2019-2020 Canaan Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@ -1,4 +1,4 @@
/* Copyright 2019 Canaan Inc.
/* Copyright 2019-2020 Canaan Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@ -1,4 +1,4 @@
/* Copyright 2019 Canaan Inc.
/* Copyright 2019-2020 Canaan Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -292,7 +292,7 @@ namespace kernels
}
template <bool IsDepthwise, int32_t FilterSize>
void fake_kpu_conv2d(const float *input, float *output, const float *weights, const float *bias, int32_t in_h, int32_t in_w, int32_t in_channels, int32_t out_channels, xtl::span<const runtime::k210::piecewise_linear_segment> fused_activation)
void fake_kpu_conv2d(const float *input, float *output, const float *weights, const float *bias, int32_t in_h, int32_t in_w, int32_t in_channels, int32_t out_channels, value_range<float> fused_activation)
{
const auto channel_size = size_t(in_h) * in_w;
@ -341,10 +341,7 @@ namespace kernels
}
}
auto &seg = *std::find_if(fused_activation.rbegin(), fused_activation.rend(), [value](const runtime::k210::piecewise_linear_segment &seg) {
return value > seg.start;
});
*output++ = value * seg.mul + seg.add;
*output++ = kernels::details::apply_activation(value, fused_activation);
}
}
}

View File

@ -1,4 +1,4 @@
/* Copyright 2019 Canaan Inc.
/* Copyright 2019-2020 Canaan Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@ -1,4 +1,4 @@
/* Copyright 2019 Canaan Inc.
/* Copyright 2019-2020 Canaan Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -15,6 +15,7 @@
#pragma once
#include "../kernel_utils.h"
#include <cmath>
#include <runtime/nnil.h>
#include <runtime/runtime_op_utility.h>
#include <xtl/xspan.hpp>
#ifdef __riscv
@ -657,6 +658,143 @@ namespace kernels
}
}
}
inline void nnil_unary_method(const float *input, float *output, size_t count, xtl::span<const uint8_t> body)
{
using namespace nncase::runtime;
for (size_t i = 0; i < count; i++)
{
nnil_evalstack stack;
span_reader sr(body);
nnil_reader reader(sr);
bool ret = false;
while (reader.avail() && !ret)
{
auto op = reader.next();
switch (op.opcode)
{
case nnil_nop:
break;
case nnil_dup:
stack.dup();
break;
case nnil_pop:
stack.pop();
break;
case nnil_lda_0:
stack.push(input[i]);
break;
case nnil_ldc_r4_0:
stack.push(0.f);
break;
case nnil_ldc_r4_1:
stack.push(1.f);
break;
case nnil_ldc_r4:
stack.push(op.ldc_r4.r4);
break;
case nnil_abs:
stack.push(fabsf(stack.pop()));
break;
case nnil_ceil:
stack.push(ceilf(stack.pop()));
break;
case nnil_cos:
stack.push(cosf(stack.pop()));
break;
case nnil_exp:
stack.push(expf(stack.pop()));
break;
case nnil_floor:
stack.push(floorf(stack.pop()));
break;
case nnil_log:
stack.push(logf(stack.pop()));
break;
case nnil_neg:
stack.push(-stack.pop());
break;
case nnil_rsqrt:
stack.push(1.f / sqrtf(stack.pop()));
break;
case nnil_sin:
stack.push(sinf(stack.pop()));
break;
case nnil_square:
{
auto v = stack.pop();
stack.push(v * v);
break;
}
case nnil_add:
{
auto b = stack.pop();
auto a = stack.pop();
stack.push(a + b);
break;
}
case nnil_sub:
{
auto b = stack.pop();
auto a = stack.pop();
stack.push(a - b);
break;
}
case nnil_mul:
{
auto b = stack.pop();
auto a = stack.pop();
stack.push(a * b);
break;
}
case nnil_div:
{
auto b = stack.pop();
auto a = stack.pop();
stack.push(a / b);
break;
}
case nnil_min:
{
auto b = stack.pop();
auto a = stack.pop();
stack.push(std::min(a, b));
break;
}
case nnil_max:
{
auto b = stack.pop();
auto a = stack.pop();
stack.push(std::max(a, b));
break;
}
case nnil_clamp:
{
auto high = stack.pop();
auto low = stack.pop();
auto v = stack.pop();
stack.push(std::clamp(v, low, high));
break;
}
case nnil_ret:
output[i] = stack.pop();
ret = true;
break;
default:
NNCASE_THROW(std::runtime_error, "Invalid nnil op");
break;
}
}
}
}
inline void table_lookup1d(const uint8_t *CXX_RESTRICT input, uint8_t *CXX_RESTRICT output, size_t size, const uint8_t *CXX_RESTRICT table)
{
for (size_t i = 0; i < size; i++)
output[i] = table[input[i]];
}
}
}
}

View File

@ -1,4 +1,4 @@
/* Copyright 2019 Canaan Inc.
/* Copyright 2019-2020 Canaan Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@ -1,4 +1,4 @@
/* Copyright 2019 Canaan Inc.
/* Copyright 2019-2020 Canaan Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@ -1,4 +1,4 @@
/* Copyright 2019 Canaan Inc.
/* Copyright 2019-2020 Canaan Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@ -1,4 +1,4 @@
/* Copyright 2019 Canaan Inc.
/* Copyright 2019-2020 Canaan Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@ -1,4 +1,4 @@
/* Copyright 2019 Canaan Inc.
/* Copyright 2019-2020 Canaan Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@ -1,4 +1,4 @@
/* Copyright 2019 Canaan Inc.
/* Copyright 2019-2020 Canaan Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@ -1,4 +1,4 @@
/* Copyright 2019 Canaan Inc.
/* Copyright 2019-2020 Canaan Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@ -1,4 +1,4 @@
/* Copyright 2019 Canaan Inc.
/* Copyright 2019-2020 Canaan Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@ -1,4 +1,4 @@
/* Copyright 2019 Canaan Inc.
/* Copyright 2019-2020 Canaan Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@ -1,4 +1,4 @@
/* Copyright 2019 Canaan Inc.
/* Copyright 2019-2020 Canaan Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -258,13 +258,6 @@ namespace runtime
};
using kpu_activation_table_t = std::array<kpu_activation_segment, 16>;
struct piecewise_linear_segment
{
float start;
float mul;
float add;
};
}
}
}

View File

@ -1,4 +1,4 @@
/* Copyright 2019 Canaan Inc.
/* Copyright 2019-2020 Canaan Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@ -1,4 +1,4 @@
/* Copyright 2019 Canaan Inc.
/* Copyright 2019-2020 Canaan Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@ -1,4 +1,4 @@
/* Copyright 2019 Canaan Inc.
/* Copyright 2019-2020 Canaan Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -465,6 +465,34 @@ namespace runtime
memory_range output;
unary_op_t unary_op;
};
struct nnil_unary_method_options
{
memory_range input;
memory_range output;
xtl::span<const uint8_t> body;
void deserialize(span_reader &reader)
{
reader.read(input);
reader.read(output);
reader.read_avail(body);
}
void serialize(binary_writer &writer) const
{
writer.write(input);
writer.write(output);
writer.write_array(body);
}
};
struct table_lookup1d_options : public simple_node_body<table_lookup1d_options>
{
memory_range input;
memory_range table;
memory_range output;
};
}
}
}

View File

@ -1,4 +1,4 @@
/* Copyright 2019 Canaan Inc.
/* Copyright 2019-2020 Canaan Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@ -1,4 +1,4 @@
/* Copyright 2019 Canaan Inc.
/* Copyright 2019-2020 Canaan Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@ -17,8 +17,9 @@ BEGINE_DEFINE_TARGET(neutral)
DEFINE_NEUTRAL_RUNTIME_OP(quantized_conv2d, QuantizedConv2D, 0x0F)
DEFINE_NEUTRAL_RUNTIME_OP(quantized_matmul, QuantizedMatMul, 0x10)
DEFINE_NEUTRAL_RUNTIME_OP(quantized_binary, QuantizedBinary, 0x11)
// DEFINE_NEUTRAL_RUNTIME_OP(table_lookup1d, TableLookup1D, 0x12)
DEFINE_NEUTRAL_RUNTIME_OP(table_lookup1d, TableLookup1D, 0x12)
DEFINE_NEUTRAL_RUNTIME_OP(conv2d_transpose, QuantizedBinary, 0x13)
DEFINE_NEUTRAL_RUNTIME_OP(nnil_unary_method, NNILUnaryMethod, 0x14)
END_DEFINE_TARGET()
// CPU

View File

@ -1,4 +1,4 @@
/* Copyright 2019 Canaan Inc.
/* Copyright 2019-2020 Canaan Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@ -1,4 +1,4 @@
/* Copyright 2019 Canaan Inc.
/* Copyright 2019-2020 Canaan Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@ -1,4 +1,4 @@
/* Copyright 2019 Canaan Inc.
/* Copyright 2019-2020 Canaan Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -58,6 +58,12 @@ namespace runtime
advance(sizeof(T) * N);
}
void read_avail(xtl::span<const uint8_t> &span)
{
span = span_;
span_ = {};
}
template <class T>
const T *peek() const noexcept
{

View File

@ -1,4 +1,4 @@
/* Copyright 2019 Canaan Inc.
/* Copyright 2019-2020 Canaan Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@ -1,4 +1,4 @@
/* Copyright 2019 Canaan Inc.
/* Copyright 2019-2020 Canaan Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@ -1,4 +1,4 @@
/* Copyright 2019 Canaan Inc.
/* Copyright 2019-2020 Canaan Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@ -1,4 +1,4 @@
/* Copyright 2019 Canaan Inc.
/* Copyright 2019-2020 Canaan Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@ -1,4 +1,4 @@
/* Copyright 2019 Canaan Inc.
/* Copyright 2019-2020 Canaan Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@ -1,4 +1,4 @@
/* Copyright 2019 Canaan Inc.
/* Copyright 2019-2020 Canaan Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@ -1,4 +1,4 @@
/* Copyright 2019 Canaan Inc.
/* Copyright 2019-2020 Canaan Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@ -1,4 +1,4 @@
/* Copyright 2019 Canaan Inc.
/* Copyright 2019-2020 Canaan Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@ -1,4 +1,4 @@
/* Copyright 2019 Canaan Inc.
/* Copyright 2019-2020 Canaan Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -387,6 +387,28 @@ namespace runtime
return kcr_error;
}
}
kernel_call_result nnil_unary_method(nnil_unary_method_options &options, interpreter_t &interpreter, interpreter_step_t step)
{
auto input = interpreter.memory_at<float>(options.input);
auto output = interpreter.memory_at<float>(options.output);
kernels::neutral::nnil_unary_method(input.data(), output.data(), input.size(), options.body);
return kcr_done;
}
kernel_call_result table_lookup1d(table_lookup1d_options &options, interpreter_t &interpreter, interpreter_step_t step)
{
if (options.input.datatype != dt_uint8)
return kcr_error;
auto input = interpreter.memory_at<uint8_t>(options.input);
auto table = interpreter.memory_at<uint8_t>(options.table);
auto output = interpreter.memory_at<uint8_t>(options.output);
kernels::neutral::table_lookup1d(input.data(), output.data(), input.size(), table.data());
return kcr_done;
}
}
}
}