Update nncase runtime

pull/95/head
sunnycase 2019-08-16 11:17:13 +08:00
parent d452cbfb07
commit 9f54db5e4c
9 changed files with 138 additions and 7 deletions

View File

@ -156,4 +156,16 @@ inline bool operator!=(const padding &lhs, const padding &rhs) noexcept
{
return lhs.before != rhs.before || lhs.after != rhs.after;
}
template<class T>
bool operator==(const value_range<T> &lhs, const value_range<T> &rhs) noexcept
{
return lhs.min == rhs.min && lhs.max == rhs.max;
}
template <class T>
bool operator!=(const value_range<T> &lhs, const value_range<T> &rhs) noexcept
{
return lhs.min != rhs.min || lhs.max != rhs.max;
}
}

View File

@ -22,7 +22,7 @@ namespace nncase
inline std::vector<uint8_t> read_file(const std::filesystem::path &filename)
{
std::ifstream infile(filename, std::ios::binary | std::ios::in);
if (infile.bad())
if (!infile.good())
throw std::runtime_error("Cannot open file: " + filename.string());
infile.seekg(0, std::ios::end);

View File

@ -13,7 +13,7 @@
* limitations under the License.
*/
#pragma once
#include "../utils.h"
#include "../kernel_utils.h"
#include <runtime/runtime_op_utility.h>
namespace nncase

View File

@ -13,9 +13,10 @@
* limitations under the License.
*/
#pragma once
#include "../utils.h"
#include "../kernel_utils.h"
#include <runtime/k210/k210_runtime_op_utility.h>
#include <runtime/runtime_op_utility.h>
#include <xtl/xspan.hpp>
namespace nncase
{
@ -182,7 +183,7 @@ namespace kernels
auto &seg = *std::find_if(activation.rbegin(), activation.rend(), [value](const runtime::k210::kpu_activation_segment &seg) {
return value > seg.start_x;
});
value = runtime::carry_shift<int64_t, true>((value - seg.start_x) * seg.mul, seg.shift);
value = runtime::carry_shift<int64_t, true>((value - seg.start_x) * seg.mul, seg.shift) + seg.add;
*out_it++ = (uint8_t)std::clamp(value, int64_t(0), int64_t(255));
}
}
@ -291,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, const value_range<float> &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, xtl::span<const runtime::k210::piecewise_linear_segment> fused_activation)
{
const auto channel_size = size_t(in_h) * in_w;
@ -340,7 +341,10 @@ namespace kernels
}
}
*output++ = kernels::details::apply_activation(value, fused_activation);
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;
}
}
}

View File

@ -13,7 +13,7 @@
* limitations under the License.
*/
#pragma once
#include "../utils.h"
#include "../kernel_utils.h"
#include <cmath>
#include <runtime/runtime_op_utility.h>
#include <xtl/xspan.hpp>

View File

@ -0,0 +1,79 @@
/* Copyright 2019 Canaan Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <iostream>
#include <xtl/xspan.hpp>
namespace nncase
{
namespace runtime
{
class binary_reader
{
public:
binary_reader(std::istream &stream)
: stream_(stream)
{
}
template <class T>
T read()
{
T value;
read(value);
return value;
}
template <class T>
void read(T &value)
{
stream_.read(reinterpret_cast<char *>(&value), sizeof(value));
}
template <class T>
void read_array(xtl::span<T> value)
{
stream_.read(reinterpret_cast<char *>(value.data()), value.size_bytes());
}
std::streampos position() const
{
return stream_.tellg();
}
void position(std::streampos pos)
{
stream_.seekg(pos);
}
void skip(std::streamoff off)
{
stream_.seekg(off, std::ios::cur);
}
size_t avail()
{
auto pos = stream_.tellg();
stream_.seekg(0, std::ios::end);
auto end = stream_.tellg();
stream_.seekg(pos);
return size_t(end - pos);
}
private:
std::istream &stream_;
};
}
}

View File

@ -258,6 +258,13 @@ 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

@ -0,0 +1,29 @@
/* Copyright 2019 Canaan Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <array>
#include <cstdint>
#include <datatypes.h>
#include <xtensor/xstorage.hpp>
namespace nncase
{
namespace runtime
{
namespace neutral
{
}
}
}