Move soundstream TFLM example to the model repository Change-Id: I4cd50e5fd2ff6ac603aa9a637fa740c146f8aca0
diff --git a/WORKSPACE b/WORKSPACE index 4255c99..86215ec 100644 --- a/WORKSPACE +++ b/WORKSPACE
@@ -1,6 +1,6 @@ workspace(name = "kelvin_sw") -load("//build_tools/bazel:repos.bzl", "kelvin_sw_repos", "model_repos", "tflm_repos") +load("//build_tools/bazel:repos.bzl", "kelvin_sw_repos", "tflm_repos") kelvin_sw_repos() # Register Kelvin toolchain @@ -21,5 +21,3 @@ load("@tflm_pip_deps//:requirements.bzl", "install_deps") install_deps() - -model_repos()
diff --git a/build_tools/bazel/repos.bzl b/build_tools/bazel/repos.bzl index d6e4ded..1fecab6 100644 --- a/build_tools/bazel/repos.bzl +++ b/build_tools/bazel/repos.bzl
@@ -9,7 +9,7 @@ # Kelvin toolchain native.new_local_repository( name = "kelvin-gcc", - build_file = "third_party/kelvin-gcc/BUILD", + build_file = "@kelvin_sw//third_party/kelvin-gcc:BUILD.kelvin-gcc", path = "../../cache/toolchain_kelvin", ) @@ -26,7 +26,7 @@ # risc-v isa test http_archive( name = "riscv-tests", - build_file = "//third_party/riscv:BUILD.riscv-tests", + build_file = "@kelvin_sw//third_party/riscv:BUILD.riscv-tests", sha256 = "1c7eb58edd7399b3ad2f9624a2003862cd87a6904237a737f39cd3978bab46a8", urls = ["https://github.com/riscv-software-src/riscv-tests/archive/d4eaa5bd6674b51d3b9b24913713c4638e99cdd9.tar.gz"], strip_prefix = "riscv-tests-d4eaa5bd6674b51d3b9b24913713c4638e99cdd9", @@ -55,10 +55,3 @@ urls = ["https://github.com/pybind/pybind11_bazel/archive/faf56fb3df11287f26dbc66fdedf60a2fc2c6631.zip"], sha256 = "a185aa68c93b9f62c80fcb3aadc3c83c763854750dc3f38be1dadcb7be223837", ) - -def model_repos(): - native.new_local_repository( - name = "ml-models", - path = "../../ml/ml-models", - build_file = "third_party/ml-models/BUILD", - )
diff --git a/examples/tflm/soundstream/BUILD b/examples/tflm/soundstream/BUILD deleted file mode 100644 index 56917ee..0000000 --- a/examples/tflm/soundstream/BUILD +++ /dev/null
@@ -1,235 +0,0 @@ -load("//build_tools/bazel:kelvin.bzl", "generate_cc_arrays", "kelvin_binary") - -package(default_visibility = ["//visibility:public"]) - -cc_library( - name = "soundstream", - srcs = [ - "decoder.cc", - "decoder_non_stream_q16x8_b64_io_int16_tflite.cc", - "decoder_non_stream_q16x8_b64_io_int16_tflite.h", - "decoder_streaming_q16x8_b64_io_int16_tflite.cc", - "decoder_streaming_q16x8_b64_io_int16_tflite.h", - "encoder.cc", - "encoder_non_stream_q16x8_b64_io_int16_tflite.cc", - "encoder_non_stream_q16x8_b64_io_int16_tflite.h", - "encoder_streaming_q16x8_b64_io_int16_tflite.cc", - "encoder_streaming_q16x8_b64_io_int16_tflite.h", - ], - hdrs = [ - "decoder.h", - "encoder.h", - ], - tags = ["manual"], - deps = [ - "@tflite-micro//tensorflow/lite/micro:micro_framework", - "@tflite-micro//tensorflow/lite/micro:recording_allocators", - ], -) - -kelvin_binary( - name = "soundstream_decoder_non_streaming", - srcs = [ - "soundstream_decoder.cc", - ], - tags = ["manual"], - deps = [ - ":soundstream", - "//crt:crt", - ], -) - -kelvin_binary( - name = "soundstream_decoder_streaming", - srcs = [ - "soundstream_decoder.cc", - ], - copts = ["-DSTREAMING"], - tags = ["manual"], - deps = [ - ":soundstream", - "//crt:crt", - ], -) - -kelvin_binary( - name = "soundstream_encoder_non_streaming", - srcs = [ - "soundstream_encoder.cc", - ], - tags = ["manual"], - deps = [ - ":soundstream", - "//crt:crt", - ], -) - -kelvin_binary( - name = "soundstream_encoder_streaming", - srcs = [ - "soundstream_encoder.cc", - ], - copts = ["-DSTREAMING"], - tags = ["manual"], - deps = [ - ":soundstream", - "//crt:crt", - ], -) - -kelvin_binary( - name = "soundstream_e2e_non_streaming", - srcs = [ - "best_of_times_s16_decoded.cc", - "best_of_times_s16_encoded.cc", - "best_of_times_s16_wav.cc", - "soundstream_e2e.cc", - ], - hdrs = [ - "best_of_times_s16_decoded.h", - "best_of_times_s16_encoded.h", - "best_of_times_s16_wav.h", - ], - tags = ["manual"], - deps = [ - ":soundstream", - "//crt:crt", - ], -) - -kelvin_binary( - name = "soundstream_e2e_streaming", - srcs = [ - "best_of_times_s16_decoded_streaming.cc", - "best_of_times_s16_encoded_streaming.cc", - "best_of_times_s16_wav.cc", - "soundstream_e2e.cc", - ], - hdrs = [ - "best_of_times_s16_decoded_streaming.h", - "best_of_times_s16_encoded_streaming.h", - "best_of_times_s16_wav.h", - ], - copts = ["-DSTREAMING"], - tags = ["manual"], - deps = [ - ":soundstream", - "//crt:crt", - ], -) - -generate_cc_arrays( - name = "decoder_non_stream_q16x8_b64_io_int16_tflite_cc", - src = "@ml-models//:quant_models/_decoder_non_stream_q16x8_b64_io_int16.tflite", - out = "decoder_non_stream_q16x8_b64_io_int16_tflite.cc", - tags = ["manual"], -) - -generate_cc_arrays( - name = "decoder_non_stream_q16x8_b64_io_int16_tflite_h", - src = "@ml-models//:quant_models/_decoder_non_stream_q16x8_b64_io_int16.tflite", - out = "decoder_non_stream_q16x8_b64_io_int16_tflite.h", - tags = ["manual"], -) - -generate_cc_arrays( - name = "encoder_non_stream_q16x8_b64_io_int16_tflite_cc", - src = "@ml-models//:quant_models/_encoder_non_stream_q16x8_b64_io_int16.tflite", - out = "encoder_non_stream_q16x8_b64_io_int16_tflite.cc", - tags = ["manual"], -) - -generate_cc_arrays( - name = "encoder_non_stream_q16x8_b64_io_int16_tflite_h", - src = "@ml-models//:quant_models/_encoder_non_stream_q16x8_b64_io_int16.tflite", - out = "encoder_non_stream_q16x8_b64_io_int16_tflite.h", - tags = ["manual"], -) - -generate_cc_arrays( - name = "decoder_streaming_q16x8_b64_io_int16_tflite_cc", - src = "@ml-models//:quant_models/_decoder_streaming_q16x8_b64_io_int16.tflite", - out = "decoder_streaming_q16x8_b64_io_int16_tflite.cc", - tags = ["manual"], -) - -generate_cc_arrays( - name = "decoder_streaming_q16x8_b64_io_int16_tflite_h", - src = "@ml-models//:quant_models/_decoder_streaming_q16x8_b64_io_int16.tflite", - out = "decoder_streaming_q16x8_b64_io_int16_tflite.h", - tags = ["manual"], -) - -generate_cc_arrays( - name = "encoder_streaming_q16x8_b64_io_int16_tflite_cc", - src = "@ml-models//:quant_models/_encoder_streaming_q16x8_b64_io_int16.tflite", - out = "encoder_streaming_q16x8_b64_io_int16_tflite.cc", - tags = ["manual"], -) - -generate_cc_arrays( - name = "encoder_streaming_q16x8_b64_io_int16_tflite_h", - src = "@ml-models//:quant_models/_encoder_streaming_q16x8_b64_io_int16.tflite", - out = "encoder_streaming_q16x8_b64_io_int16_tflite.h", - tags = ["manual"], -) - -generate_cc_arrays( - name = "best_of_times_s16_wav_cc", - src = "best_of_times_s16.wav", - out = "best_of_times_s16_wav.cc", -) - -generate_cc_arrays( - name = "best_of_times_s16_wav_h", - src = "best_of_times_s16.wav", - out = "best_of_times_s16_wav.h", -) - -generate_cc_arrays( - name = "best_of_times_s16_encoded_cc", - src = "best_of_times_s16_encoded.raw", - out = "best_of_times_s16_encoded.cc", -) - -generate_cc_arrays( - name = "best_of_times_s16_encoded_h", - src = "best_of_times_s16_encoded.raw", - out = "best_of_times_s16_encoded.h", -) - -generate_cc_arrays( - name = "best_of_times_s16_decoded_cc", - src = "best_of_times_s16_decoded.raw", - out = "best_of_times_s16_decoded.cc", -) - -generate_cc_arrays( - name = "best_of_times_s16_decoded_h", - src = "best_of_times_s16_decoded.raw", - out = "best_of_times_s16_decoded.h", -) - -generate_cc_arrays( - name = "best_of_times_s16_encoded_streaming_cc", - src = "best_of_times_s16_encoded_streaming.raw", - out = "best_of_times_s16_encoded_streaming.cc", -) - -generate_cc_arrays( - name = "best_of_times_s16_encoded_streaming_h", - src = "best_of_times_s16_encoded_streaming.raw", - out = "best_of_times_s16_encoded_streaming.h", -) - -generate_cc_arrays( - name = "best_of_times_s16_decoded_streaming_cc", - src = "best_of_times_s16_decoded_streaming.raw", - out = "best_of_times_s16_decoded_streaming.cc", -) - -generate_cc_arrays( - name = "best_of_times_s16_decoded_streaming_h", - src = "best_of_times_s16_decoded_streaming.raw", - out = "best_of_times_s16_decoded_streaming.h", -)
diff --git a/examples/tflm/soundstream/best_of_times_s16.wav b/examples/tflm/soundstream/best_of_times_s16.wav deleted file mode 100644 index e3a7685..0000000 --- a/examples/tflm/soundstream/best_of_times_s16.wav +++ /dev/null Binary files differ
diff --git a/examples/tflm/soundstream/best_of_times_s16_decoded.raw b/examples/tflm/soundstream/best_of_times_s16_decoded.raw deleted file mode 100644 index 9ce7cd9..0000000 --- a/examples/tflm/soundstream/best_of_times_s16_decoded.raw +++ /dev/null Binary files differ
diff --git a/examples/tflm/soundstream/best_of_times_s16_decoded_streaming.raw b/examples/tflm/soundstream/best_of_times_s16_decoded_streaming.raw deleted file mode 100644 index 91f655e..0000000 --- a/examples/tflm/soundstream/best_of_times_s16_decoded_streaming.raw +++ /dev/null Binary files differ
diff --git a/examples/tflm/soundstream/best_of_times_s16_encoded.raw b/examples/tflm/soundstream/best_of_times_s16_encoded.raw deleted file mode 100644 index 94c79da..0000000 --- a/examples/tflm/soundstream/best_of_times_s16_encoded.raw +++ /dev/null Binary files differ
diff --git a/examples/tflm/soundstream/best_of_times_s16_encoded_streaming.raw b/examples/tflm/soundstream/best_of_times_s16_encoded_streaming.raw deleted file mode 100644 index 7956e3c..0000000 --- a/examples/tflm/soundstream/best_of_times_s16_encoded_streaming.raw +++ /dev/null Binary files differ
diff --git a/examples/tflm/soundstream/decoder.cc b/examples/tflm/soundstream/decoder.cc deleted file mode 100644 index 6aa939a..0000000 --- a/examples/tflm/soundstream/decoder.cc +++ /dev/null
@@ -1,96 +0,0 @@ -// Copyright 2023 Google LLC -// Licensed under the Apache License, Version 2.0, see LICENSE for details. -// SPDX-License-Identifier: Apache-2.0 - -#include "examples/tflm/soundstream/decoder.h" - -#include "examples/tflm/soundstream/decoder_non_stream_q16x8_b64_io_int16_tflite.h" -#include "examples/tflm/soundstream/decoder_streaming_q16x8_b64_io_int16_tflite.h" - -namespace kelvin::soundstream::decoder { - -constexpr unsigned int kNonStreamingOpCount = 11; -constexpr unsigned int kStreamingOpCount = 16; -// Not sure how to get a good upper bound on this one, so arbitrarily chosen. -constexpr unsigned int kStreamingVariablesCount = 40; - -template <bool kStreaming> -class DecoderImpl : public Decoder { - public: - static Decoder* Setup(const uint8_t* model_data, uint8_t* tensor_arena, - size_t tensor_arena_size) { - auto* model = tflite::GetModel(model_data); - if (model->version() != TFLITE_SCHEMA_VERSION) { - return nullptr; - } - - DecoderImpl* d = new DecoderImpl(model, tensor_arena, tensor_arena_size); - - TfLiteStatus allocate_status = d->interpreter()->AllocateTensors(); - if (allocate_status != kTfLiteOk) { - MicroPrintf("Failed to allocate decoder's tensors"); - return nullptr; - } - return d; - } - tflite::MicroInterpreter* interpreter() { return &interpreter_; } - - private: - DecoderImpl(const tflite::Model* model, uint8_t* tensor_arena, - size_t tensor_arena_size) - : resolver_(CreateResolver()), - allocator_(tflite::RecordingMicroAllocator::Create(tensor_arena, - tensor_arena_size)), - variables_(tflite::MicroResourceVariables::Create( - allocator_.get(), kStreamingVariablesCount)), - interpreter_(model, resolver_, allocator_.get(), variables_.get()) {} - - static constexpr int kOpCount = - kStreaming ? kStreamingOpCount : kStreamingOpCount; - static inline tflite::MicroMutableOpResolver<kOpCount> CreateResolver() { - tflite::MicroMutableOpResolver<kOpCount> resolver; - resolver.AddReshape(); - resolver.AddPad(); - resolver.AddConv2D(); - resolver.AddLeakyRelu(); - resolver.AddSplit(); - resolver.AddTransposeConv(); - resolver.AddStridedSlice(); - resolver.AddConcatenation(); - resolver.AddDepthwiseConv2D(); - resolver.AddAdd(); - resolver.AddQuantize(); - if (kStreaming) { - resolver.AddCallOnce(); - resolver.AddVarHandle(); - resolver.AddReadVariable(); - resolver.AddAssignVariable(); - resolver.AddSub(); - } - return resolver; - } - const tflite::MicroMutableOpResolver<kOpCount> resolver_; - // Created in the arena - std::unique_ptr<tflite::RecordingMicroAllocator> allocator_; - // Created in the arena - std::unique_ptr<tflite::MicroResourceVariables> variables_; - tflite::MicroInterpreter interpreter_; -}; - -// Two separate methods to construct streaming vs non-streaming, so that the -// compiler can eliminate one if it's unused. Perhaps with LTO we could combine -// them together. -std::unique_ptr<Decoder> SetupStreaming(uint8_t* tensor_arena, - size_t tensor_arena_size) { - return std::unique_ptr<Decoder>(DecoderImpl<true>::Setup( - g__decoder_streaming_q16x8_b64_io_int16_model_data, tensor_arena, - tensor_arena_size)); -} -std::unique_ptr<Decoder> Setup(uint8_t* tensor_arena, - size_t tensor_arena_size) { - return std::unique_ptr<Decoder>(DecoderImpl<false>::Setup( - g__decoder_non_stream_q16x8_b64_io_int16_model_data, tensor_arena, - tensor_arena_size)); -} - -} // namespace kelvin::soundstream::decoder
diff --git a/examples/tflm/soundstream/decoder.h b/examples/tflm/soundstream/decoder.h deleted file mode 100644 index 337f402..0000000 --- a/examples/tflm/soundstream/decoder.h +++ /dev/null
@@ -1,31 +0,0 @@ -// Copyright 2023 Google LLC -// Licensed under the Apache License, Version 2.0, see LICENSE for details. -// SPDX-License-Identifier: Apache-2.0 - -#ifndef EXAMPLES_TFLM_SOUNDSTREAM_DECODER_H_ -#define EXAMPLES_TFLM_SOUNDSTREAM_DECODER_H_ - -#include <cstddef> -#include <memory> - -#include "tensorflow/lite/micro/micro_interpreter.h" -#include "tensorflow/lite/micro/micro_mutable_op_resolver.h" -#include "tensorflow/lite/micro/recording_micro_allocator.h" - -namespace kelvin::soundstream::decoder { -// RecordingMicroAllocator on desktop recorded 94512 bytes of allocation. -constexpr size_t kTensorArenaSizeBytes = 96 * 1024; -// RecordingMicroAllocator on desktop recorded 143296 bytes of allocation. -constexpr size_t kTensorArenaStreamingSizeBytes = 168 * 1024; - -class Decoder { - public: - virtual tflite::MicroInterpreter* interpreter() = 0; -}; - -std::unique_ptr<Decoder> Setup(uint8_t* tensor_arena, size_t tensor_arena_size); -std::unique_ptr<Decoder> SetupStreaming(uint8_t* tensor_arena, - size_t tensor_arena_size); -} // namespace kelvin::soundstream::decoder - -#endif // EXAMPLES_TFLM_SOUNDSTREAM_DECODER_H_
diff --git a/examples/tflm/soundstream/encoder.cc b/examples/tflm/soundstream/encoder.cc deleted file mode 100644 index 6d3c985..0000000 --- a/examples/tflm/soundstream/encoder.cc +++ /dev/null
@@ -1,95 +0,0 @@ -// Copyright 2023 Google LLC -// Licensed under the Apache License, Version 2.0, see LICENSE for details. -// SPDX-License-Identifier: Apache-2.0 - -#include "examples/tflm/soundstream/encoder.h" - -#include "examples/tflm/soundstream/encoder_non_stream_q16x8_b64_io_int16_tflite.h" -#include "examples/tflm/soundstream/encoder_streaming_q16x8_b64_io_int16_tflite.h" -#include "tensorflow/lite/micro/micro_mutable_op_resolver.h" -#include "tensorflow/lite/micro/recording_micro_allocator.h" - -namespace kelvin::soundstream::encoder { - -constexpr unsigned int kNonStreamingOpCount = 6; -constexpr unsigned int kStreamingOpCount = 13; -// Not sure how to get a good upper bound on this one, so arbitrarily chosen. -constexpr unsigned int kStreamingVariablesCount = 40; - -template <bool kStreaming> -class EncoderImpl : public Encoder { - public: - static Encoder* Setup(const uint8_t* model_data, uint8_t* tensor_arena, - size_t tensor_arena_size) { - auto* model = tflite::GetModel(model_data); - if (model->version() != TFLITE_SCHEMA_VERSION) { - return nullptr; - } - - EncoderImpl* e = new EncoderImpl(model, tensor_arena, tensor_arena_size); - - TfLiteStatus allocate_status = e->interpreter()->AllocateTensors(); - if (allocate_status != kTfLiteOk) { - MicroPrintf("Failed to allocate decoder's tensors"); - return nullptr; - } - return e; - } - tflite::MicroInterpreter* interpreter() { return &interpreter_; } - - private: - EncoderImpl(const tflite::Model* model, uint8_t* tensor_arena, - size_t tensor_arena_size) - : resolver_(CreateResolver()), - allocator_(tflite::RecordingMicroAllocator::Create(tensor_arena, - tensor_arena_size)), - variables_(tflite::MicroResourceVariables::Create( - allocator_.get(), kStreamingVariablesCount)), - interpreter_(model, resolver_, allocator_.get(), variables_.get()) {} - - static constexpr int kOpCount = - kStreaming ? kStreamingOpCount : kStreamingOpCount; - static inline tflite::MicroMutableOpResolver<kOpCount> CreateResolver() { - tflite::MicroMutableOpResolver<kOpCount> resolver; - resolver.AddReshape(); - resolver.AddPad(); - resolver.AddConv2D(); - resolver.AddLeakyRelu(); - resolver.AddDepthwiseConv2D(); - resolver.AddAdd(); - if (kStreaming) { - resolver.AddCallOnce(); - resolver.AddVarHandle(); - resolver.AddReadVariable(); - resolver.AddConcatenation(); - resolver.AddStridedSlice(); - resolver.AddAssignVariable(); - resolver.AddQuantize(); - } - return resolver; - } - const tflite::MicroMutableOpResolver<kOpCount> resolver_; - // Created in the arena - std::unique_ptr<tflite::RecordingMicroAllocator> allocator_; - // Created in the arena - std::unique_ptr<tflite::MicroResourceVariables> variables_; - tflite::MicroInterpreter interpreter_; -}; - -// Two separate methods to construct streaming vs non-streaming, so that the -// compiler can eliminate one if it's unused. Perhaps with LTO we could combine -// them together. -std::unique_ptr<Encoder> SetupStreaming(uint8_t* tensor_arena, - size_t tensor_arena_size) { - return std::unique_ptr<Encoder>(EncoderImpl<true>::Setup( - g__encoder_streaming_q16x8_b64_io_int16_model_data, tensor_arena, - tensor_arena_size)); -} -std::unique_ptr<Encoder> Setup(uint8_t* tensor_arena, - size_t tensor_arena_size) { - return std::unique_ptr<Encoder>(EncoderImpl<false>::Setup( - g__encoder_non_stream_q16x8_b64_io_int16_model_data, tensor_arena, - tensor_arena_size)); -} - -} // namespace kelvin::soundstream::encoder
diff --git a/examples/tflm/soundstream/encoder.h b/examples/tflm/soundstream/encoder.h deleted file mode 100644 index 267d553..0000000 --- a/examples/tflm/soundstream/encoder.h +++ /dev/null
@@ -1,28 +0,0 @@ -// Copyright 2023 Google LLC -// Licensed under the Apache License, Version 2.0, see LICENSE for details. -// SPDX-License-Identifier: Apache-2.0 - -#ifndef EXAMPLES_TFLM_SOUNDSTREAM_ENCODER_H_ -#define EXAMPLES_TFLM_SOUNDSTREAM_ENCODER_H_ - -#include <cstddef> -#include <memory> - -#include "tensorflow/lite/micro/micro_interpreter.h" - -namespace kelvin::soundstream::encoder { -// RecordingMicroAllocator on desktop recorded 90064 bytes of allocation. -constexpr size_t kTensorArenaSizeBytes = 96 * 1024; -// RecordingMicroAllocator on desktop recorded 147328 bytes of allocation. -constexpr size_t kTensorArenaStreamingSizeBytes = 168 * 1024; - -struct Encoder { - virtual tflite::MicroInterpreter* interpreter() = 0; -}; - -std::unique_ptr<Encoder> Setup(uint8_t* tensor_arena, size_t tensor_arena_size); -std::unique_ptr<Encoder> SetupStreaming(uint8_t* tensor_arena, - size_t tensor_arena_size); -} // namespace kelvin::soundstream::encoder - -#endif // EXAMPLES_TFLM_SOUNDSTREAM_ENCODER_H_
diff --git a/examples/tflm/soundstream/soundstream_decoder.cc b/examples/tflm/soundstream/soundstream_decoder.cc deleted file mode 100644 index 9a5a739..0000000 --- a/examples/tflm/soundstream/soundstream_decoder.cc +++ /dev/null
@@ -1,56 +0,0 @@ -// Copyright 2023 Google LLC -// Licensed under the Apache License, Version 2.0, see LICENSE for details. -// SPDX-License-Identifier: Apache-2.0 - -#include "examples/tflm/soundstream/decoder.h" - -typedef struct { - uint32_t return_code; // Populated in kelvin_start - uint32_t output_ptr; - uint32_t length; -} OutputHeader; - -__attribute__((section(".model_output_header"))) OutputHeader output_header = { - .output_ptr = 0, - .length = 0, -}; - -namespace { -#if defined(STREAMING) -constexpr size_t tensor_arena_size = - kelvin::soundstream::decoder::kTensorArenaStreamingSizeBytes; -#else -constexpr size_t tensor_arena_size = - kelvin::soundstream::decoder::kTensorArenaSizeBytes; -#endif -uint8_t decoder_tensor_arena[tensor_arena_size] __attribute__((aligned(64))); -} // namespace - -int main(int argc, char **argv) { -#if defined(STREAMING) - auto decoder = kelvin::soundstream::decoder::SetupStreaming( - decoder_tensor_arena, tensor_arena_size); -#else - auto decoder = kelvin::soundstream::decoder::Setup(decoder_tensor_arena, - tensor_arena_size); -#endif - if (!decoder) { - MicroPrintf("Unable to construct decoder"); - return -1; - } - - TfLiteTensor *decoder_input = decoder->interpreter()->input(0); - TfLiteTensor *decoder_output = decoder->interpreter()->output(0); - - memset(decoder_input->data.uint8, 0, decoder_input->bytes); - TfLiteStatus invoke_status = decoder->interpreter()->Invoke(); - if (invoke_status != kTfLiteOk) { - MicroPrintf("Failed to invoke decoder"); - return -1; - } - - output_header.length = decoder_output->bytes; - output_header.output_ptr = - reinterpret_cast<uint32_t>(decoder_output->data.uint8); - return 0; -}
diff --git a/examples/tflm/soundstream/soundstream_e2e.cc b/examples/tflm/soundstream/soundstream_e2e.cc deleted file mode 100644 index 9ba8913..0000000 --- a/examples/tflm/soundstream/soundstream_e2e.cc +++ /dev/null
@@ -1,106 +0,0 @@ -// Copyright 2023 Google LLC -// Licensed under the Apache License, Version 2.0, see LICENSE for details. -// SPDX-License-Identifier: Apache-2.0 - -#include "examples/tflm/soundstream/best_of_times_s16_wav.h" -#include "examples/tflm/soundstream/decoder.h" -#include "examples/tflm/soundstream/encoder.h" - -#if defined(STREAMING) -#include "examples/tflm/soundstream/best_of_times_s16_decoded_streaming.h" -#include "examples/tflm/soundstream/best_of_times_s16_encoded_streaming.h" -const unsigned char *reference_decoded = g_best_of_times_s16_decoded_streaming; -const unsigned char *reference_encoded = g_best_of_times_s16_encoded_streaming; -#else -#include "examples/tflm/soundstream/best_of_times_s16_decoded.h" -#include "examples/tflm/soundstream/best_of_times_s16_encoded.h" -const unsigned char *reference_decoded = g_best_of_times_s16_decoded; -const unsigned char *reference_encoded = g_best_of_times_s16_encoded; -#endif - -namespace { -#if defined(STREAMING) -constexpr size_t decoder_tensor_arena_size = - kelvin::soundstream::decoder::kTensorArenaStreamingSizeBytes; -constexpr size_t encoder_tensor_arena_size = - kelvin::soundstream::encoder::kTensorArenaStreamingSizeBytes; -#else -constexpr size_t decoder_tensor_arena_size = - kelvin::soundstream::decoder::kTensorArenaSizeBytes; -constexpr size_t encoder_tensor_arena_size = - kelvin::soundstream::encoder::kTensorArenaSizeBytes; -#endif -uint8_t encoder_tensor_arena[encoder_tensor_arena_size] - __attribute__((aligned(64))); -uint8_t decoder_tensor_arena[decoder_tensor_arena_size] - __attribute__((aligned(64))); -} // namespace - -int main(int argc, char **argv) { -#if defined(STREAMING) - auto encoder = kelvin::soundstream::encoder::SetupStreaming( - encoder_tensor_arena, encoder_tensor_arena_size); -#else - auto encoder = kelvin::soundstream::encoder::Setup(encoder_tensor_arena, - encoder_tensor_arena_size); -#endif - if (!encoder) { - MicroPrintf("Unable to construct encoder"); - return -1; - } - -#if defined(STREAMING) - auto decoder = kelvin::soundstream::decoder::SetupStreaming( - decoder_tensor_arena, decoder_tensor_arena_size); -#else - auto decoder = kelvin::soundstream::decoder::Setup(decoder_tensor_arena, - decoder_tensor_arena_size); -#endif - if (!decoder) { - MicroPrintf("Unable to construct decoder"); - return -1; - } - - TfLiteTensor *encoder_input = encoder->interpreter()->input(0); - TfLiteTensor *encoder_output = encoder->interpreter()->output(0); - TfLiteTensor *decoder_input = decoder->interpreter()->input(0); - TfLiteTensor *decoder_output = decoder->interpreter()->output(0); - - int invocation_count = - (g_best_of_times_s16_audio_data_size * sizeof(int16_t)) / - encoder_input->bytes; - for (int i = 0; i < invocation_count; ++i) { - MicroPrintf("Invocation %d of %d", i, invocation_count); - memcpy(encoder_input->data.uint8, - g_best_of_times_s16_audio_data + - ((i * encoder_input->bytes) / sizeof(int16_t)), - encoder_input->bytes); - TfLiteStatus invoke_status = encoder->interpreter()->Invoke(); - if (invoke_status != kTfLiteOk) { - MicroPrintf("Failed to invoke encoder"); - return -1; - } - if (memcmp(encoder_output->data.uint8, - reference_encoded + (i * encoder_output->bytes), - encoder_output->bytes)) { - MicroPrintf("Encoder output mismatches reference"); - return -1; - } - - memcpy(decoder_input->data.uint8, encoder_output->data.uint8, - decoder_input->bytes); - invoke_status = decoder->interpreter()->Invoke(); - if (invoke_status != kTfLiteOk) { - MicroPrintf("Failed to invoke decoder"); - return -1; - } - if (memcmp(decoder_output->data.uint8, - reference_decoded + (i * decoder_output->bytes), - decoder_output->bytes)) { - MicroPrintf("Decoder output mismatches reference"); - return -1; - } - } - - return 0; -}
diff --git a/examples/tflm/soundstream/soundstream_encoder.cc b/examples/tflm/soundstream/soundstream_encoder.cc deleted file mode 100644 index 6c60748..0000000 --- a/examples/tflm/soundstream/soundstream_encoder.cc +++ /dev/null
@@ -1,56 +0,0 @@ -// Copyright 2023 Google LLC -// Licensed under the Apache License, Version 2.0, see LICENSE for details. -// SPDX-License-Identifier: Apache-2.0 - -#include "examples/tflm/soundstream/encoder.h" - -typedef struct { - uint32_t return_code; // Populated in kelvin_start - uint32_t output_ptr; - uint32_t length; -} OutputHeader; - -__attribute__((section(".model_output_header"))) OutputHeader output_header = { - .output_ptr = 0, - .length = 0, -}; - -namespace { -#if defined(STREAMING) -constexpr size_t tensor_arena_size = - kelvin::soundstream::encoder::kTensorArenaStreamingSizeBytes; -#else -constexpr size_t tensor_arena_size = - kelvin::soundstream::encoder::kTensorArenaSizeBytes; -#endif -uint8_t tensor_arena[tensor_arena_size] __attribute__((aligned(64))); -} // namespace - -int main(int argc, char **argv) { -#if defined(STREAMING) - auto encoder = kelvin::soundstream::encoder::SetupStreaming( - tensor_arena, tensor_arena_size); -#else - auto encoder = - kelvin::soundstream::encoder::Setup(tensor_arena, tensor_arena_size); -#endif - if (!encoder) { - MicroPrintf("Unable to construct encoder"); - return -1; - } - - TfLiteTensor *encoder_input = encoder->interpreter()->input(0); - TfLiteTensor *encoder_output = encoder->interpreter()->output(0); - - memset(encoder_input->data.uint8, 0, encoder_input->bytes); - TfLiteStatus invoke_status = encoder->interpreter()->Invoke(); - if (invoke_status != kTfLiteOk) { - MicroPrintf("Failed to invoke encoder"); - return -1; - } - - output_header.length = encoder_output->bytes; - output_header.output_ptr = - reinterpret_cast<uint32_t>(encoder_output->data.uint8); - return 0; -}
diff --git a/platforms/registration.bzl b/platforms/registration.bzl index 43e75c5..a8b1c53 100644 --- a/platforms/registration.bzl +++ b/platforms/registration.bzl
@@ -1,5 +1,5 @@ """Kelvin toolchain registration.""" def kelvin_register_toolchain(name = "kelvin"): - native.register_execution_platforms("//platforms/riscv32:kelvin") - native.register_toolchains("//toolchains/kelvin:all") + native.register_execution_platforms("@kelvin_sw//platforms/riscv32:kelvin") + native.register_toolchains("@kelvin_sw//toolchains/kelvin:all")
diff --git a/third_party/kelvin-gcc/BUILD b/third_party/kelvin-gcc/BUILD index 3148231..22e7b57 100644 --- a/third_party/kelvin-gcc/BUILD +++ b/third_party/kelvin-gcc/BUILD
@@ -1,9 +1,3 @@ -# Automatically generated by @crt//:compiler.bzl. Do not edit. +# Copyright 2023 Google LLC. + package(default_visibility = ["//visibility:public"]) - -filegroup( - name = "all", - srcs = glob(["**"], exclude=["**/*.html","**/*.pdf"]), -) - -exports_files(["bin/**"])
diff --git a/third_party/kelvin-gcc/BUILD.kelvin-gcc b/third_party/kelvin-gcc/BUILD.kelvin-gcc new file mode 100644 index 0000000..3148231 --- /dev/null +++ b/third_party/kelvin-gcc/BUILD.kelvin-gcc
@@ -0,0 +1,9 @@ +# Automatically generated by @crt//:compiler.bzl. Do not edit. +package(default_visibility = ["//visibility:public"]) + +filegroup( + name = "all", + srcs = glob(["**"], exclude=["**/*.html","**/*.pdf"]), +) + +exports_files(["bin/**"])