Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 6 additions & 0 deletions ASSET_LICENSES.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ content in all three model families:
`probe_02_clean_16k_mono_no_resample.wav` and is distributed under the same
terms.

Byte-identical copies of `probe_02_clean_16k_mono_no_resample.wav` are also
stored at:

- `families/nemotron3_diarization/tests/data/maintainer-recording-16k-mono.wav`

The PersonaPlex fixture contains the same maintainer-owned spoken recording,
converted to mono 24 kHz float32 for official-reference regression tests:

Expand Down Expand Up @@ -212,6 +217,7 @@ International license:
- `families/higgs_audio_v2_tokenizer/tests/data/librispeech-test-clean-6930-75918-0003.wav`
- `families/hubert/tests/data/librispeech-test-clean-6930-75918-0003.wav`
- `families/mimi/tests/data/librispeech-test-clean-6930-75918-0003.wav`
- `families/nemotron3_diarization/tests/data/librispeech-test-clean-6930-75918-0003.wav`
- `families/qwen3_asr/tests/data/librispeech-test-clean-6930-75918-0003.wav`
- `families/wav2vec2_bert/tests/data/librispeech-test-clean-6930-75918-0003.wav`
- `families/wavlm/tests/data/librispeech-test-clean-6930-75918-0003.wav`
Expand Down
48 changes: 48 additions & 0 deletions core/api/include/trtmc/audio.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#ifndef TRTMC_AUDIO_H
#define TRTMC_AUDIO_H

#include "trtmc/matrix.h"
#include "trtmc/scores.h"
#include "trtmc/types.h"

Expand Down Expand Up @@ -42,6 +43,7 @@ typedef struct {
#define TRTMC_TASK_SPEECH_TRANSLATION "speech_translation"
#define TRTMC_TASK_AUDIO_LANGUAGE_IDENTIFICATION "audio_language_identification"
#define TRTMC_TASK_AUDIO_TO_CLASS_SCORES "audio_to_class_scores"
#define TRTMC_TASK_AUDIO_TO_TIMED_LABELS "audio_to_timed_labels"
#define TRTMC_TASK_SPEECH_TO_SPEECH_RESPONSE "speech_to_speech_response"
#define TRTMC_TASK_BATCH_SPEECH_TRANSCRIPTION "batch_speech_transcription"
#define TRTMC_TASK_BATCH_SPEECH_TRANSLATION "batch_speech_translation"
Expand Down Expand Up @@ -103,10 +105,45 @@ typedef struct {
typedef struct {
trtmc_audio_view_v1 audio;
} trtmc_audio_to_class_scores_request_v1;
typedef struct {
trtmc_audio_view_v1 audio;
} trtmc_audio_to_timed_labels_request_v1;
typedef struct {
trtmc_audio_view_v1 audio;
} trtmc_speech_to_speech_response_request_v1;

/* One active interval of one label, [start_seconds, end_seconds) on the input
* clock: seconds from the first audio frame, 0 <= start < end. label indexes
* the result's labels. Intervals of one label never overlap; intervals of
* different labels may (overlapping speakers or sound events). */
typedef struct {
double start_seconds;
double end_seconds;
uint32_t label;
} trtmc_timed_label_v1;

/* Time-localized labels of one audio input. labels names every label ordinal
* (at least one, each nonempty). An empty vocabulary_id means the names carry
* no identity beyond this result: a diarization family numbers anonymous
* speakers by first appearance in each input. intervals are ordered by start,
* then label. frame_scores is optional: either empty (rows 0, frame_seconds
* 0, score_kind 0) or [frame, label] with one column per label, frame f
* covering [f * frame_seconds, (f + 1) * frame_seconds) of the input, and the
* scores raw in the declared TRTMC_SCORE_* kind: every score finite, and a
* probability within [0, 1]. Multi-label families score every label
* independently; nothing normalizes across labels. */
typedef struct {
const trtmc_timed_label_v1* intervals;
uint64_t interval_count;
trtmc_strings_view labels;
trtmc_string_view vocabulary_id;
trtmc_f32_matrix_view_v1 frame_scores;
double frame_seconds;
uint32_t score_kind;
double setup_ms;
double inference_ms;
} trtmc_timed_labels_view_v1;

/* Language presence fields are exactly zero or one. Present identifiers must
* be nonempty. Absence uses only a default declared by the loaded family;
* a family without an applicable default rejects absence. */
Expand Down Expand Up @@ -174,6 +211,17 @@ typedef struct {
trtmc_error**);
} trtmc_audio_to_class_scores_api_v1;

/* Who or what is active when: speaker diarization, voice activity or sound-event
* detection over the whole input, returned as labelled intervals plus the
* family's frame scores. Not transcription and not clip-level tagging. */
typedef struct {
trtmc_api_header header;
trtmc_status(TRTMC_CALL* run)(trtmc_model*, const trtmc_audio_to_timed_labels_request_v1*,
const trtmc_config_view_v1*, trtmc_result**, trtmc_error**);
trtmc_status(TRTMC_CALL* result_view)(const trtmc_result*, trtmc_timed_labels_view_v1*,
trtmc_error**);
} trtmc_audio_to_timed_labels_api_v1;

/* Response speech to the complete input utterance, not generic audio editing. */
typedef struct {
trtmc_api_header header;
Expand Down
87 changes: 87 additions & 0 deletions core/api/include/trtmc/audio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#pragma once

#include "trtmc/audio.h"
#include "trtmc/matrix.hpp"
#include "trtmc/scores.hpp"
#include "trtmc/text.hpp"

Expand Down Expand Up @@ -62,6 +63,9 @@ struct AudioLanguageIdentificationRequest {
struct AudioToClassScoresRequest {
AudioView audio;
};
struct AudioToTimedLabelsRequest {
AudioView audio;
};
struct SpeechToSpeechResponseRequest {
AudioView audio;
};
Expand Down Expand Up @@ -99,13 +103,60 @@ class AudioGenerationResult {
trtmc_audio_result_view_v1 view_{};
};

using TimedLabel = trtmc_timed_label_v1;

class TimedLabelsResult {
public:
TimedLabelsResult(std::shared_ptr<detail::ModelState> state, trtmc_result* result) noexcept
: owner_(std::move(state), result) {}
TimedLabelsResult(const TimedLabelsResult&) = delete;
TimedLabelsResult& operator=(const TimedLabelsResult&) = delete;
TimedLabelsResult(TimedLabelsResult&& other) noexcept
: owner_(std::move(other.owner_)), view_(std::exchange(other.view_, {})) {}
TimedLabelsResult& operator=(TimedLabelsResult&& other) noexcept {
if (this != &other) {
owner_ = std::move(other.owner_);
view_ = std::exchange(other.view_, {});
}
return *this;
}
// Ordered by start, then label; label indexes labels().
Span<const TimedLabel> intervals() const noexcept {
return {view_.intervals, static_cast<std::size_t>(view_.interval_count)};
}
std::vector<std::string_view> labels() const {
std::vector<std::string_view> labels;
for (std::uint64_t i = 0; i < view_.labels.size; ++i)
labels.push_back(detail::string_view(view_.labels.data[i]));
return labels;
}
std::string_view vocabulary_id() const { return detail::string_view(view_.vocabulary_id); }
// Raw [frame, label] scores; empty when the family has none.
FloatMatrixView frame_scores() const noexcept {
return {{view_.frame_scores.data, static_cast<std::size_t>(view_.frame_scores.count)},
view_.frame_scores.rows,
view_.frame_scores.columns};
}
double frame_seconds() const noexcept { return view_.frame_seconds; }
std::uint32_t score_kind() const noexcept { return view_.score_kind; }
double setup_ms() const noexcept { return view_.setup_ms; }
double inference_ms() const noexcept { return view_.inference_ms; }
// C views and the convenience views above borrow this result owner.
trtmc_timed_labels_view_v1& wire_view() noexcept { return view_; }

private:
detail::ResultOwner owner_;
trtmc_timed_labels_view_v1 view_{};
};

using TextToAudioResult = AudioGenerationResult;
using TextToSpeechResult = AudioGenerationResult;
using SpeechToSpeechResponseResult = AudioGenerationResult;
using SpeechTranscriptionResult = TextContinuationResult;
using SpeechTranslationResult = TextContinuationResult;
using AudioLanguageIdentificationResult = LabelScoresResult;
using AudioToClassScoresResult = LabelScoresResult;
using AudioToTimedLabelsResult = TimedLabelsResult;
using BatchSpeechTranscriptionResult = BatchTextContinuationResult;
using BatchSpeechTranslationResult = BatchTextContinuationResult;
using MixedBatchSpeechToTextResult = BatchTextContinuationResult;
Expand Down Expand Up @@ -379,6 +430,42 @@ class AudioToClassScores {
const trtmc_audio_to_class_scores_api_v1* api_;
};

class AudioToTimedLabels {
public:
static constexpr std::string_view kTask = TRTMC_TASK_AUDIO_TO_TIMED_LABELS;
static constexpr std::uint32_t kMajor = 1, kMinor = 0;
AudioToTimedLabelsResult run(const AudioToTimedLabelsRequest& input,
const Config& config = {}) const {
const trtmc_audio_to_timed_labels_request_v1 request{detail::c_audio(input.audio)};
auto entries = config.c_entries();
auto options = entries.view();
trtmc_result* raw = nullptr;
trtmc_error* error = nullptr;
auto status = api_->run(state_->handle, &request, &options, &raw, &error);
TimedLabelsResult result(state_, raw);
detail::check(state_->api, status, error);
error = nullptr;
status = api_->result_view(raw, &result.wire_view(), &error);
detail::check(state_->api, status, error);
return result;
}
std::vector<ConfigField> config_fields() const {
return detail::config_fields(state_, kTask, kMajor, kMinor);
}
static void validate_table(const trtmc_api_header* table) {
detail::validate_audio_table<trtmc_audio_to_timed_labels_api_v1>(table);
}

private:
friend class Model;
AudioToTimedLabels(std::shared_ptr<detail::ModelState> state,
const trtmc_api_header* table) noexcept
: state_(std::move(state)),
api_(reinterpret_cast<const trtmc_audio_to_timed_labels_api_v1*>(table)) {}
std::shared_ptr<detail::ModelState> state_;
const trtmc_audio_to_timed_labels_api_v1* api_;
};

class SpeechToSpeechResponse {
public:
static constexpr std::string_view kTask = TRTMC_TASK_SPEECH_TO_SPEECH_RESPONSE;
Expand Down
138 changes: 138 additions & 0 deletions core/api/runtime/audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

#include "api_internal.h"

#include <cmath>
#include <cstdint>

namespace trtmc::api {

internal::AudioView audio_view(const trtmc_audio_view_v1& input) {
Expand Down Expand Up @@ -266,6 +269,138 @@ extern "C" trtmc_status TRTMC_CALL audio_to_class_scores_run(
});
}

void require_named_labels(const std::vector<std::string>& labels) {
if (labels.empty() || labels.size() > UINT32_MAX)
throw ApiFailure{TRTMC_INTERNAL_ERROR, "timed labels need between one and 2^32-1 labels"};
for (const auto& label : labels)
if (label.empty())
throw ApiFailure{TRTMC_INTERNAL_ERROR, "timed labels must name every label"};
}

void require_valid_interval(const internal::TimedLabel& interval, std::size_t label_count) {
if (!std::isfinite(interval.start_seconds) || !std::isfinite(interval.end_seconds) ||
interval.start_seconds < 0 || interval.end_seconds <= interval.start_seconds)
throw ApiFailure{TRTMC_INTERNAL_ERROR, "timed label interval is empty or not finite"};
if (interval.label >= label_count)
throw ApiFailure{TRTMC_INTERNAL_ERROR, "timed label interval names an unknown label"};
}

bool ordered_after(const internal::TimedLabel& interval, const internal::TimedLabel& previous) {
return interval.start_seconds > previous.start_seconds ||
(interval.start_seconds == previous.start_seconds && interval.label > previous.label);
}

void require_ordered_intervals(const std::vector<internal::TimedLabel>& intervals,
std::size_t label_count) {
std::vector<double> label_end(label_count, 0.0);
for (std::size_t index = 0; index < intervals.size(); ++index) {
const auto& interval = intervals[index];
require_valid_interval(interval, label_count);
if (index > 0 && !ordered_after(interval, intervals[index - 1]))
throw ApiFailure{TRTMC_INTERNAL_ERROR,
"timed label intervals are not ordered by start, then label"};
if (interval.start_seconds < label_end[interval.label])
throw ApiFailure{TRTMC_INTERNAL_ERROR, "intervals of one timed label overlap"};
label_end[interval.label] = interval.end_seconds;
}
}

void require_frame_score_values(const std::vector<float>& scores, bool probability) {
for (const float score : scores) {
if (!std::isfinite(score))
throw ApiFailure{TRTMC_INTERNAL_ERROR, "timed label frame scores must be finite"};
if (probability && (score < 0.0F || score > 1.0F))
throw ApiFailure{TRTMC_INTERNAL_ERROR,
"timed label probability frame scores must lie in [0, 1]"};
}
}

void require_frame_scores(const internal::TimedLabelsResult& result) {
if (result.frame_scores.empty()) {
if (result.frame_count != 0 || result.frame_seconds != 0)
throw ApiFailure{TRTMC_INTERNAL_ERROR,
"timed labels without frame scores declare frames"};
return;
}
const auto labels = static_cast<std::uint64_t>(result.labels.size());
if (result.frame_count == 0 || result.frame_count > UINT64_MAX / labels ||
result.frame_count * labels != result.frame_scores.size())
throw ApiFailure{TRTMC_INTERNAL_ERROR,
"timed label frame scores are not one row per frame and label"};
if (!std::isfinite(result.frame_seconds) || result.frame_seconds <= 0)
throw ApiFailure{TRTMC_INTERNAL_ERROR, "timed label frames need a positive duration"};
switch (result.score_kind) {
case internal::ScoreKind::Logit:
case internal::ScoreKind::Unbounded:
require_frame_score_values(result.frame_scores, false);
return;
case internal::ScoreKind::Probability:
require_frame_score_values(result.frame_scores, true);
return;
}
throw ApiFailure{TRTMC_INTERNAL_ERROR, "timed label frame scores have an unknown kind"};
}

struct TimedLabelsStorage final : ResultStorage {
explicit TimedLabelsStorage(internal::TimedLabelsResult value) : result(std::move(value)) {
require_named_labels(result.labels);
require_ordered_intervals(result.intervals, result.labels.size());
require_frame_scores(result);
intervals.reserve(result.intervals.size());
for (const auto& interval : result.intervals)
intervals.push_back({interval.start_seconds, interval.end_seconds, interval.label});
labels.reserve(result.labels.size());
for (const auto& label : result.labels)
labels.push_back(borrowed_string(label));
}
internal::TimedLabelsResult result;
std::vector<trtmc_timed_label_v1> intervals;
std::vector<trtmc_string_view> labels;
};

extern "C" trtmc_status TRTMC_CALL audio_to_timed_labels_run(
trtmc_model* model, const trtmc_audio_to_timed_labels_request_v1* input,
const trtmc_config_view_v1* config, trtmc_result** output, trtmc_error** error) noexcept {
if (output)
*output = nullptr;
return guarded(error, [&] {
require(input && output, "audio timed-labels request and result output are required");
const internal::AudioToTimedLabelsRequest request{audio_view(input->audio)};
const ConvertedConfig options(config);

std::lock_guard<std::mutex> lock(model_mutex(model));
auto& task = require_interface<internal::IAudioToTimedLabels>(
model, internal::IAudioToTimedLabels::kTask);
validate_task_config(model_owner(model),
internal::contract_key<internal::IAudioToTimedLabels>(),
options.view());
*output = make_result<TimedLabelsStorage>(task.run(request, options.view()));
});
}

trtmc_status TRTMC_CALL timed_labels_result_view(const trtmc_result* result,
trtmc_timed_labels_view_v1* output,
trtmc_error** error) noexcept {
if (output)
*output = {};
return guarded(error, [&] {
require(output != nullptr, "timed labels output is required");
const auto& storage = require_result<TimedLabelsStorage>(result);
const auto& value = storage.result;
const bool framed = !value.frame_scores.empty();
*output = {storage.intervals.data(),
storage.intervals.size(),
{storage.labels.data(), storage.labels.size()},
borrowed_string(value.vocabulary_id),
{value.frame_scores.data(), value.frame_scores.size(), value.frame_count,
framed ? value.labels.size() : 0},
value.frame_seconds,
framed ? static_cast<std::uint32_t>(value.score_kind) : 0U,
value.setup_ms,
value.inference_ms};
});
}

extern "C" trtmc_status TRTMC_CALL speech_response_run(
trtmc_model* model, const trtmc_speech_to_speech_response_request_v1* input,
const trtmc_config_view_v1* config, trtmc_result** output, trtmc_error** error) noexcept {
Expand Down Expand Up @@ -542,6 +677,8 @@ const trtmc_audio_language_identification_api_v1 language_api{
{1, 0, sizeof(language_api)}, language_identification_run, label_scores_result_view};
const trtmc_audio_to_class_scores_api_v1 audio_class_scores_api{
{1, 0, sizeof(audio_class_scores_api)}, audio_to_class_scores_run, label_scores_result_view};
const trtmc_audio_to_timed_labels_api_v1 audio_timed_labels_api{
{1, 0, sizeof(audio_timed_labels_api)}, audio_to_timed_labels_run, timed_labels_result_view};
const trtmc_speech_to_speech_response_api_v1 response_api{
{1, 0, sizeof(response_api)}, speech_response_run, audio_result_view};
const trtmc_audio_codebook_tokens_to_audio_api_v1 codec_audio_api{
Expand Down Expand Up @@ -586,6 +723,7 @@ const TaskBinding bindings[] = {
{internal::ISpeechTranslation::kTask, 1, 0, &translation_api.header},
{internal::IAudioLanguageIdentification::kTask, 1, 0, &language_api.header},
{internal::IAudioToClassScores::kTask, 1, 0, &audio_class_scores_api.header},
{internal::IAudioToTimedLabels::kTask, 1, 0, &audio_timed_labels_api.header},
{internal::ISpeechToSpeechResponse::kTask, 1, 0, &response_api.header},
{internal::IBatchSpeechTranscription::kTask, 1, 0, &batch_transcription_api.header},
{internal::IBatchSpeechTranslation::kTask, 1, 0, &batch_translation_api.header},
Expand Down
Loading