Skip to content
Open
Show file tree
Hide file tree
Changes from 60 commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
1e7035e
Implement simple lexiconfree time-sync beam search
Feb 19, 2025
bf0a8ce
Add some comments
Feb 19, 2025
d6689b4
Add `createSearchAlgorithm` to Search::Module
Feb 20, 2025
664945c
Fix compilation
Feb 26, 2025
488fb0e
Refactor traceback/lattice building and construct proper (nonlinear) …
Feb 28, 2025
1599302
Factor out time statistics into new Core::StopWatch class
Feb 28, 2025
9a60916
Don't copy sibling from predecessor
Feb 28, 2025
8e96423
Better handling of blank index
Feb 28, 2025
536ac82
Apply suggestions from code review
Feb 28, 2025
f21935e
Implement StopWatch class
Mar 4, 2025
5f82460
Use TIMER_START and TIMER_STOP macros instead
Mar 4, 2025
4779dd5
Simplify AdvancedTreeSearch PerformanceCounter by inheriting from Sto…
Mar 4, 2025
f5a3182
Small fixes in StopWatch class
Mar 4, 2025
97e5bd7
Make StopWatch a member of PerformanceCounter instead of inheriting
Mar 4, 2025
b77cf23
Implement LatticeTrace class
Mar 4, 2025
5fcfff7
Make predecessor and sibling public members
Mar 4, 2025
3152300
Look for initial trace instead of associating empty trace with initia…
Mar 4, 2025
0b676f9
Remove redundant includes
Mar 4, 2025
159fbd8
Add assertions for assumptions in lattice building
Mar 5, 2025
f2f4cf7
Merge remote-tracking branch 'origin/lattice_traces' into lexiconfree…
SimBe195 Mar 5, 2025
0577e79
Remove wrong assertion
SimBe195 Mar 5, 2025
04b6ac4
Merge remote-tracking branch 'origin/lattice_traces' into lexiconfree…
SimBe195 Mar 5, 2025
b454e39
Remove initial item in `performTraceback`
SimBe195 Mar 5, 2025
b3d5f02
Merge remote-tracking branch 'origin/lattice_traces' into lexiconfree…
SimBe195 Mar 5, 2025
d393c7e
Fix arc scores
SimBe195 Mar 5, 2025
b1ed20e
Merge remote-tracking branch 'origin/lattice_traces' into lexiconfree…
SimBe195 Mar 5, 2025
f112113
Merge remote-tracking branch 'origin/stopwatch' into lexiconfree_beam…
SimBe195 Mar 5, 2025
d67cf45
Update traceback/lattice building logic
SimBe195 Mar 5, 2025
54535e6
Make `elapsed` functions const
SimBe195 Mar 5, 2025
f0832f8
Merge branch 'stopwatch' into lexiconfree_beam_search
SimBe195 Mar 5, 2025
a125afa
Merge branch 'stopwatch' into lattice_traces
SimBe195 Mar 5, 2025
46ee1a8
Merge branch 'lattice_traces' into lexiconfree_beam_search
SimBe195 Mar 5, 2025
c86855e
Add RecognizerNodeV2
SimBe195 Mar 5, 2025
1dc47e6
Make `modelCombination_` a ref + some formatting
SimBe195 Mar 6, 2025
600999b
Better readable lattice building function
SimBe195 Mar 6, 2025
e309602
Fix error string
SimBe195 Mar 26, 2025
52dc192
Add DataView class to replace feature input/output of LabelScorer and…
SimBe195 Mar 27, 2025
8dd7251
Rewrite docstring, remove static_ptr cast, add operator[] function
SimBe195 Mar 27, 2025
1cad598
Fix indentation
SimBe195 Apr 1, 2025
540ccf5
Merge branch 'master' into shared_ptr_adapter
SimBe195 Apr 4, 2025
b6ae9d4
Remove unnecessary includes
SimBe195 Apr 4, 2025
6ecb644
Formatting
SimBe195 Apr 4, 2025
b33902d
Fix #define name
SimBe195 Apr 4, 2025
277d09d
Update EncoderDecoderLabelScorer
SimBe195 Apr 9, 2025
9c4bbba
Add copyright header to DataView.cc
SimBe195 Apr 10, 2025
c9dc854
Introduce python bindings for search algorithm
SimBe195 Apr 10, 2025
883e723
Remove unused includes
SimBe195 Apr 10, 2025
1f18ab8
Merge branch 'master' into search_python_bindings
SimBe195 Apr 28, 2025
08691d7
Apply suggestions from code review
SimBe195 Apr 29, 2025
e6c7f71
Formatting
SimBe195 Apr 29, 2025
0b10f17
Remove traceback items with no pronunciation
SimBe195 Apr 30, 2025
d9ba049
Add python label scorer implementation and pybindings
SimBe195 May 9, 2025
5e05b38
Merge branch 'master' into python_label_scorer
SimBe195 May 9, 2025
1051bcf
Merge branch 'master' into search_python_bindings
SimBe195 May 9, 2025
aeceba4
Merge branch 'search_python_bindings' into python_label_scorer
SimBe195 May 9, 2025
235893d
Reorder in Makefile
SimBe195 May 9, 2025
53f8ae0
Fix Makefile formatting
SimBe195 May 9, 2025
22b1d94
Add TODO statement about Core::Configuration pybinds
SimBe195 May 9, 2025
217230e
Formatting
SimBe195 May 9, 2025
603f0d7
Apply suggestions from code review
SimBe195 Jul 23, 2025
5743a7d
Merge branch 'master' into python_label_scorer
SimBe195 Jul 25, 2025
ad6b3f9
Merge remote-tracking branch 'origin/master' into python_label_scorer
curufinwe Oct 9, 2025
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
176 changes: 176 additions & 0 deletions src/Python/LabelScorer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
/** Copyright 2025 RWTH Aachen University. All rights reserved.
*
* Licensed under the RWTH ASR License (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.hltpr.rwth-aachen.de/rwth-asr/rwth-asr-license.html
*
* 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.
*/

#include "LabelScorer.hh"

#include <pybind11/gil.h>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>

#include "ScoringContext.hh"

namespace py = pybind11;

namespace Python {

PythonLabelScorer::PythonLabelScorer(Core::Configuration const& config)
: Core::Component(config),
Precursor(config) {
}

void PythonLabelScorer::reset() {
PYBIND11_OVERRIDE_PURE(void, LabelScorer, reset);
}

void PythonLabelScorer::signalNoMoreFeatures() {
PYBIND11_OVERRIDE_PURE_NAME(
void,
LabelScorer,
"signal_no_more_features",
signalNoMoreFeatures);
}

Nn::ScoringContextRef PythonLabelScorer::getInitialScoringContext() {
py::gil_scoped_acquire gil;
// Store `py::object` from virtual python call in a `PythonScoringContext`
return Core::ref(new PythonScoringContext(getInitialPythonScoringContext()));
}

py::object PythonLabelScorer::getInitialPythonScoringContext() {
PYBIND11_OVERRIDE_PURE_NAME(
py::object,
Nn::LabelScorer,
"get_initial_scoring_context",
getInitialPythonScoringContext);
}

Nn::ScoringContextRef PythonLabelScorer::extendedScoringContext(Request const& request) {
auto* pythonScoringContext = dynamic_cast<PythonScoringContext const*>(request.context.get());
py::gil_scoped_acquire gil;
// Store `py::object` from virtual python call in a `PythonScoringContext`
auto newScoringContext = extendedPythonScoringContext(pythonScoringContext->object, request.nextToken, request.transitionType);
return Core::ref(new PythonScoringContext(std::move(newScoringContext)));
}

py::object PythonLabelScorer::extendedPythonScoringContext(py::object const& context, Nn::LabelIndex nextToken, TransitionType transitionType) {
PYBIND11_OVERRIDE_PURE_NAME(
py::object,
Nn::LabelScorer,
"extended_scoring_context",
extendedPythonScoringContext,
context,
nextToken,
transitionType);
}

void PythonLabelScorer::addInput(Nn::DataView const& input) {
// Call batched version
addInputs(input, 1);
}

void PythonLabelScorer::addInputs(Nn::DataView const& input, size_t nTimesteps) {
py::gil_scoped_acquire gil;

// Convert `input` to a `py::array` for virtual python call
ssize_t featureDimSize = input.size() / nTimesteps;

py::array_t<f32> inputArray(
{static_cast<ssize_t>(nTimesteps), featureDimSize},
{sizeof(f32) * featureDimSize, sizeof(f32)},
input.data());

addPythonInputs(inputArray);
}

void PythonLabelScorer::addPythonInputs(py::array const& inputs) {
PYBIND11_OVERRIDE_PURE_NAME(
void,
Nn::LabelScorer,
"add_inputs",
addPythonInputs,
inputs);
}

std::optional<Nn::LabelScorer::ScoreWithTime> PythonLabelScorer::computeScoreWithTime(Request const& request) {
// Extract the underlying `py::object` from ScoringContext in `request` to supply them to the virtual python call
auto* pythonScoringContext = dynamic_cast<PythonScoringContext const*>(request.context.get());

std::vector<py::object> contexts = {pythonScoringContext->object};
std::vector<Nn::LabelIndex> nextTokens = {request.nextToken};
std::vector<TransitionType> transitionTypes = {request.transitionType};

py::gil_scoped_acquire gil;

// Call batched version
if (auto result = computePythonScoresWithTimes(contexts, nextTokens, transitionTypes)) {
verify(result->size() == 1);
ScoreWithTime scoreWithTime{result->front().first, result->front().second};
return scoreWithTime;
}

return {};
}

std::optional<Nn::LabelScorer::ScoresWithTimes> PythonLabelScorer::computeScoresWithTimes(std::vector<Request> const& requests) {
std::vector<py::object> contexts;
std::vector<Nn::LabelIndex> nextTokens;
std::vector<TransitionType> transitionTypes;

contexts.reserve(requests.size());
nextTokens.reserve(requests.size());
transitionTypes.reserve(requests.size());

// Extract the underlying `py::object`s from ScoringContexts in `requests` to supply them to the virtual python call
for (auto const& request : requests) {
auto* pythonScoringContext = dynamic_cast<PythonScoringContext const*>(request.context.get());
contexts.push_back(pythonScoringContext->object);
nextTokens.push_back(request.nextToken);
transitionTypes.push_back(request.transitionType);
}

py::gil_scoped_acquire gil;

if (auto result = computePythonScoresWithTimes(contexts, nextTokens, transitionTypes)) {
verify(result->size() == requests.size());
ScoresWithTimes scoresWithTimes;
scoresWithTimes.scores.reserve(result->size());
for (auto const& [score, timeframe] : *result) {
scoresWithTimes.scores.push_back(score);
scoresWithTimes.timeframes.push_back(timeframe);
}
return scoresWithTimes;
}

return {};
}

std::optional<std::vector<std::pair<Nn::LabelScorer::Score, Speech::TimeframeIndex>>> PythonLabelScorer::computePythonScoresWithTimes(std::vector<py::object> const& contexts, std::vector<Nn::LabelIndex> const& nextTokens, std::vector<TransitionType> const& transitionTypes) {
using returnType = std::optional<std::vector<std::pair<Nn::LabelScorer::Score, Speech::TimeframeIndex>>>; // Macro can't handle types with commas inside properly
PYBIND11_OVERRIDE_PURE_NAME(
returnType,
Nn::LabelScorer,
"compute_scores_with_times",
computePythonScoresWithTimes,
contexts,
nextTokens,
transitionTypes);
}

void PythonLabelScorer::setInstance(py::object const& instance) {
py::gil_scoped_acquire gil;
pyInstance_ = instance;
}

} // namespace Python
80 changes: 80 additions & 0 deletions src/Python/LabelScorer.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/** Copyright 2025 RWTH Aachen University. All rights reserved.
*
* Licensed under the RWTH ASR License (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.hltpr.rwth-aachen.de/rwth-asr/rwth-asr-license.html
*
* 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.
*/

#ifndef PYTHON_LABEL_SCORER_HH
#define PYTHON_LABEL_SCORER_HH

#include <pybind11/pybind11.h>

#include <Nn/LabelScorer/LabelScorer.hh>

namespace py = pybind11;

namespace Python {

/*
* Trampoline class that is used in order to expose the LabelScorer class via pybind.
* It mainly specifies the signatures of abstract methods that need to be implemented in python
* and performs conversion between "C++ types" such as `DataView` and `ScoringContext`
* and "Python types" such as `py::array` and `py::object`.
*
* See https://pybind11.readthedocs.io/en/stable/advanced/classes.html for official documentation
* on the "trampoline" pattern.
*/
class PythonLabelScorer : public Nn::LabelScorer {
public:
using Precursor = Nn::LabelScorer;

PythonLabelScorer(Core::Configuration const& config);
virtual ~PythonLabelScorer() = default;

// Must be overridden in python by name "reset"
virtual void reset() override;

// Can be overridden in python. No-op per default.
virtual void signalNoMoreFeatures() override;

// Must be overridden in python by name "get_initial_scoring_context"
virtual Nn::ScoringContextRef getInitialScoringContext() override;
virtual py::object getInitialPythonScoringContext();

// Must be overridden in python by name "extended_scoring_context"
virtual Nn::ScoringContextRef extendedScoringContext(Request const& request) override;
virtual py::object extendedPythonScoringContext(py::object const& context, Nn::LabelIndex nextToken, TransitionType transitionType);

// Calls batched version with `nTimesteps = 1`
virtual void addInput(Nn::DataView const& input) override;

// Must be overridden in python by name "add_inputs"
virtual void addInputs(Nn::DataView const& input, size_t nTimesteps) override;
virtual void addPythonInputs(py::array const& inputs);

// Calls batched version
virtual std::optional<ScoreWithTime> computeScoreWithTime(Request const& request) override;

// Must be overridden in python by name "compute_scores_with_times"
virtual std::optional<ScoresWithTimes> computeScoresWithTimes(std::vector<Request> const& requests) override;
virtual std::optional<std::vector<std::pair<Score, Speech::TimeframeIndex>>> computePythonScoresWithTimes(std::vector<py::object> const& contexts, std::vector<Nn::LabelIndex> const& nextTokens, std::vector<TransitionType> const& transitionTypes);

// Keep track of python object as a member to make sure it doesn't get garbage collected
void setInstance(py::object const& instance);

protected:
py::object pyInstance_; // Hold the Python wrapper
};

} // namespace Python

#endif // PYTHON_LABEL_SCORER_HH
3 changes: 3 additions & 0 deletions src/Python/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ LIBPYTHON_O = \
$(OBJDIR)/AllophoneStateFsaBuilder.o \
$(OBJDIR)/Configuration.o \
$(OBJDIR)/Init.o \
$(OBJDIR)/LabelScorer.o \
$(OBJDIR)/Numpy.o \
$(OBJDIR)/ScoringContext.o \
$(OBJDIR)/Search.o \
$(OBJDIR)/Utilities.o

CHECK_O = $(OBJDIR)/check.o \
Expand Down
31 changes: 31 additions & 0 deletions src/Python/ScoringContext.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/** Copyright 2025 RWTH Aachen University. All rights reserved.
*
* Licensed under the RWTH ASR License (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.hltpr.rwth-aachen.de/rwth-asr/rwth-asr-license.html
*
* 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.
*/

#include "ScoringContext.hh"

namespace Python {

size_t PythonScoringContext::hash() const {
return py::hash(py::cast<py::handle>(object));
}

bool PythonScoringContext::isEqual(Nn::ScoringContextRef const& other) const {
auto* otherPtr = dynamic_cast<const PythonScoringContext*>(other.get());

py::gil_scoped_acquire gil;
return object.equal(py::cast<py::handle>(otherPtr->object));
}

} // namespace Python
47 changes: 47 additions & 0 deletions src/Python/ScoringContext.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/** Copyright 2025 RWTH Aachen University. All rights reserved.
*
* Licensed under the RWTH ASR License (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.hltpr.rwth-aachen.de/rwth-asr/rwth-asr-license.html
*
* 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.
*/

#ifndef PYTHON_SCORING_CONTEXT_HH
#define PYTHON_SCORING_CONTEXT_HH

#include <pybind11/pybind11.h>

#include <Nn/LabelScorer/ScoringContext.hh>

namespace py = pybind11;

namespace Python {

/*
* Scoring context containing some arbitrary (hashable) python object
*/
struct PythonScoringContext : public Nn::ScoringContext {
py::object object;

PythonScoringContext()
: object(py::none()) {}

PythonScoringContext(py::object&& object)
: object(object) {}

bool isEqual(Nn::ScoringContextRef const& other) const;
size_t hash() const;
};

typedef Core::Ref<const PythonScoringContext> PythonScoringContextRef;

} // namespace Python

#endif // PYTHON_SCORING_CONTEXT_HH
Loading