Skip to content

Commit 8a1db2a

Browse files
committed
small code refactor
1 parent fbe20dc commit 8a1db2a

File tree

14 files changed

+82
-89
lines changed

14 files changed

+82
-89
lines changed

packages/react-native-executorch/common/rnexecutorch/data_processing/Sequential.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,11 @@ std::vector<T> repeatInterleave(std::span<const T> data,
3838
std::to_string(repetitions.size()));
3939
}
4040

41-
IType totalReps = std::accumulate(repetitions.begin(), repetitions.end(),
42-
static_cast<IType>(0));
41+
IType totalReps = std::reduce(repetitions.begin(), repetitions.end());
4342
std::vector<T> result(totalReps);
4443

4544
IType filled = 0;
46-
for (int i = 0; i < data.size(); ++i) {
45+
for (size_t i = 0; i < data.size(); i++) {
4746
std::fill_n(result.begin() + filled, repetitions[i], data[i]);
4847
filled += repetitions[i];
4948
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#pragma once
2+
3+
#include <cstddef>
4+
#include <filesystem>
5+
#include <stdexcept>
6+
#include <string>
7+
8+
#include <rnexecutorch/Log.h>
9+
10+
/**
11+
* @brief A helper macro to check if a container has the expected size.
12+
* Prints an error message with the container's name, file, and line number if
13+
* the size does not match, and throws a std::runtime_error.
14+
*
15+
* @param container The container whose size will be checked (must have a
16+
* .size() method).
17+
* @param expected The expected size of the container.
18+
* @note The macro prints the variable name, file, and line for easier
19+
* debugging.
20+
*/
21+
#define CHECK_SIZE(container, expected) \
22+
if ((container).size() != (expected)) { \
23+
rnexecutorch::log(rnexecutorch::LOG_LEVEL::Error, \
24+
"Unexpected size for " #container " at ", \
25+
std::filesystem::path(__FILE__).filename().string(), \
26+
":", __LINE__, ": expected ", (expected), " but got ", \
27+
(container).size()); \
28+
throw std::runtime_error("Invalid input shape for " #container); \
29+
}

packages/react-native-executorch/common/rnexecutorch/models/text_to_speech/kokoro/Constants.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include <cstdint>
4+
#include <string>
45
#include <unordered_map>
56
#include <unordered_set>
67

packages/react-native-executorch/common/rnexecutorch/models/text_to_speech/kokoro/Decoder.cpp

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "Decoder.h"
22
#include <rnexecutorch/Log.h>
3+
#include <rnexecutorch/metaprogramming/ContainerHelpers.h>
34

45
namespace rnexecutorch::models::text_to_speech::kokoro {
56

@@ -15,13 +16,7 @@ Decoder::Decoder(const std::string &modelSource,
1516
auto inputTensors = getAllInputShapes(testMethod);
1617

1718
// Perform checks to validate model's compatibility with native code
18-
if (inputTensors.size() < 4) {
19-
rnexecutorch::log(rnexecutorch::LOG_LEVEL::Error,
20-
"Unexpected model input size, expected 4 tensors "
21-
"but got: ",
22-
inputTensors.size());
23-
throw std::runtime_error("[Kokoro::Decoder] Incompatible model");
24-
}
19+
CHECK_SIZE(inputTensors, 4);
2520
}
2621

2722
Result<std::vector<EValue>>
@@ -30,25 +25,9 @@ Decoder::generate(const std::string &method, const Configuration &inputConfig,
3025
std::span<float> nPred, std::span<float> ref_ls) {
3126
// Perform input shape checks
3227
// Both F0 and N vectors should be twice as long as duration
33-
if (f0Pred.size() != 2 * inputConfig.duration) {
34-
rnexecutorch::log(rnexecutorch::LOG_LEVEL::Error,
35-
"Unexpected F0 vector length: expected ",
36-
2 * inputConfig.duration, " but got ", f0Pred.size());
37-
throw std::runtime_error("[Kokoro::Decoder] Invalid input shape");
38-
}
39-
if (nPred.size() != 2 * inputConfig.duration) {
40-
rnexecutorch::log(rnexecutorch::LOG_LEVEL::Error,
41-
"Unexpected N vector length: expected ",
42-
2 * inputConfig.duration, " but got ", nPred.size());
43-
throw std::runtime_error("[Kokoro::Decoder] Invalid input shape");
44-
}
45-
// ref_hs should be a half of a voice reference vector
46-
if (ref_ls.size() != constants::kVoiceRefHalfSize) {
47-
rnexecutorch::log(rnexecutorch::LOG_LEVEL::Error,
48-
"Unexpected voice ref length: expected ",
49-
constants::kVoiceRefHalfSize, " but got ", ref_ls.size());
50-
throw std::runtime_error("[Kokoro::Decoder] Invalid input shape");
51-
}
28+
CHECK_SIZE(f0Pred, 2 * inputConfig.duration);
29+
CHECK_SIZE(nPred, 2 * inputConfig.duration);
30+
CHECK_SIZE(ref_ls, constants::kVoiceRefHalfSize);
5231

5332
// Convert input data to ExecuTorch tensors
5433
auto asrTensor = make_tensor_ptr({1, 512, inputConfig.duration}, asr.data(),

packages/react-native-executorch/common/rnexecutorch/models/text_to_speech/kokoro/Decoder.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#pragma once
22

3+
#include <memory>
34
#include <span>
5+
#include <string>
6+
#include <vector>
47

58
#include <executorch/extension/tensor/tensor.h>
69

packages/react-native-executorch/common/rnexecutorch/models/text_to_speech/kokoro/DurationPredictor.cpp

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <queue>
66
#include <rnexecutorch/Log.h>
77
#include <rnexecutorch/data_processing/Sequential.h>
8+
#include <rnexecutorch/metaprogramming/ContainerHelpers.h>
89

910
namespace rnexecutorch::models::text_to_speech::kokoro {
1011

@@ -21,13 +22,7 @@ DurationPredictor::DurationPredictor(
2122
auto inputTensors = getAllInputShapes(testMethod);
2223

2324
// Perform checks to validate model's compatibility with native code
24-
if (inputTensors.size() < 4) {
25-
rnexecutorch::log(rnexecutorch::LOG_LEVEL::Error,
26-
"Unexpected model input size, expected 4 tensors "
27-
"but got: ",
28-
inputTensors.size());
29-
throw std::runtime_error("[Kokoro::DurationPredictor] Incompatible model");
30-
}
25+
CHECK_SIZE(inputTensors, 4);
3126
}
3227

3328
std::tuple<Tensor, std::vector<int64_t>, int32_t>
@@ -38,18 +33,8 @@ DurationPredictor::generate(const std::string &method,
3833
// Perform input shape checks
3934
// Since every bit in text mask corresponds to exactly one of the tokens, both
4035
// vectors should be the same length
41-
if (tokens.size() != textMask.size()) {
42-
rnexecutorch::log(rnexecutorch::LOG_LEVEL::Error,
43-
"Unexpected text mask length: expected ", tokens.size(),
44-
" but got ", textMask.size());
45-
throw std::runtime_error("[Kokoro::DurationPredictor] Invalid input shape");
46-
}
47-
if (ref_hs.size() != constants::kVoiceRefHalfSize) {
48-
rnexecutorch::log(rnexecutorch::LOG_LEVEL::Error,
49-
"Unexpected voice ref length: expected ",
50-
constants::kVoiceRefHalfSize, " but got ", ref_hs.size());
51-
throw std::runtime_error("[Kokoro::DurationPredictor] Invalid input shape");
52-
}
36+
CHECK_SIZE(tokens, textMask.size());
37+
CHECK_SIZE(ref_hs, constants::kVoiceRefHalfSize);
5338

5439
// Convert input data to ExecuTorch tensors
5540
auto tokensTensor = make_tensor_ptr({1, static_cast<int32_t>(tokens.size())},
@@ -121,7 +106,7 @@ void DurationPredictor::scaleDurations(Tensor &durations,
121106

122107
int32_t nTokens = shape[0];
123108
int64_t *durationsPtr = durations.data_ptr<int64_t>();
124-
int64_t totalDur = std::accumulate(durationsPtr, durationsPtr + nTokens, 0LL);
109+
int64_t totalDur = std::reduce(durationsPtr, durationsPtr + nTokens);
125110

126111
float scaleFactor = static_cast<float>(targetDuration) / totalDur;
127112
bool shrinking = scaleFactor < 1.F;
@@ -142,7 +127,7 @@ void DurationPredictor::scaleDurations(Tensor &durations,
142127
scaledSum += durationsPtr[i];
143128

144129
// Keeps the entries sorted by the remainders
145-
remainders.push({remainder, i});
130+
remainders.emplace(remainder, i);
146131
}
147132

148133
// The initial processing scales durations to at least (targetDuration -

packages/react-native-executorch/common/rnexecutorch/models/text_to_speech/kokoro/DurationPredictor.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#pragma once
22

3+
#include <memory>
34
#include <span>
5+
#include <string>
46
#include <tuple>
7+
#include <vector>
58

69
#include <executorch/extension/tensor/tensor.h>
710

packages/react-native-executorch/common/rnexecutorch/models/text_to_speech/kokoro/Encoder.cpp

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "Encoder.h"
22
#include <rnexecutorch/Log.h>
3+
#include <rnexecutorch/metaprogramming/ContainerHelpers.h>
34

45
namespace rnexecutorch::models::text_to_speech::kokoro {
56

@@ -15,13 +16,7 @@ Encoder::Encoder(const std::string &modelSource,
1516
auto inputTensors = getAllInputShapes(testMethod);
1617

1718
// Perform checks to validate model's compatibility with native code
18-
if (inputTensors.size() < 3) {
19-
rnexecutorch::log(rnexecutorch::LOG_LEVEL::Error,
20-
"Unexpected model input size, expected 3 tensors "
21-
"but got: ",
22-
inputTensors.size());
23-
throw std::runtime_error("[Kokoro::Encoder] Incompatible model");
24-
}
19+
CHECK_SIZE(inputTensors, 3);
2520
}
2621

2722
Result<std::vector<EValue>> Encoder::generate(const std::string &method,
@@ -32,12 +27,7 @@ Result<std::vector<EValue>> Encoder::generate(const std::string &method,
3227
// Perform input shape checks
3328
// Since every bit in text mask corresponds to exactly one of the tokens, both
3429
// vectors should be the same length
35-
if (tokens.size() != textMask.size()) {
36-
rnexecutorch::log(rnexecutorch::LOG_LEVEL::Error,
37-
"Unexpected text mask length: expected ", tokens.size(),
38-
" but got ", textMask.size());
39-
throw std::runtime_error("[Kokoro::Encoder] Invalid input shape");
40-
}
30+
CHECK_SIZE(tokens, textMask.size());
4131

4232
// Convert input data to ExecuTorch tensors
4333
int32_t noTokens = static_cast<int32_t>(tokens.size());

packages/react-native-executorch/common/rnexecutorch/models/text_to_speech/kokoro/Encoder.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#pragma once
22

3+
#include <memory>
34
#include <span>
5+
#include <string>
6+
#include <vector>
47

58
#include <executorch/extension/tensor/tensor.h>
69

packages/react-native-executorch/common/rnexecutorch/models/text_to_speech/kokoro/F0NPredictor.cpp

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "F0NPredictor.h"
22
#include <rnexecutorch/Log.h>
3+
#include <rnexecutorch/metaprogramming/ContainerHelpers.h>
34

45
namespace rnexecutorch::models::text_to_speech::kokoro {
56

@@ -15,26 +16,15 @@ F0NPredictor::F0NPredictor(const std::string &modelSource,
1516
auto inputTensors = getAllInputShapes(testMethod);
1617

1718
// Perform checks to validate model's compatibility with native code
18-
if (inputTensors.size() < 2) {
19-
rnexecutorch::log(rnexecutorch::LOG_LEVEL::Error,
20-
"Unexpected model input size, expected 2 tensors "
21-
"but got: ",
22-
inputTensors.size());
23-
throw std::runtime_error("[Kokoro::F0NPredictor] Incompatible model");
24-
}
19+
CHECK_SIZE(inputTensors, 3);
2520
}
2621

2722
Result<std::vector<EValue>> F0NPredictor::generate(
2823
const std::string &method, const Configuration &inputConfig,
2924
std::span<int64_t> indices, std::span<float> dur, std::span<float> ref_hs) {
3025
// Perform input shape checks
3126
// s vector should be half of a voice reference vector size
32-
if (ref_hs.size() != constants::kVoiceRefHalfSize) {
33-
rnexecutorch::log(rnexecutorch::LOG_LEVEL::Error,
34-
"Unexpected voice ref length: expected ",
35-
constants::kVoiceRefHalfSize, " but got ", ref_hs.size());
36-
throw std::runtime_error("[Kokoro::F0NPredictor] Invalid input shape");
37-
}
27+
CHECK_SIZE(ref_hs, constants::kVoiceRefHalfSize);
3828

3929
// Convert input data to ExecuTorch tensors
4030
auto indicesTensor =

0 commit comments

Comments
 (0)