Skip to content

[Not for merge]: Support mmap#3484

Draft
csukuangfj wants to merge 1 commit intok2-fsa:masterfrom
csukuangfj:mmap
Draft

[Not for merge]: Support mmap#3484
csukuangfj wants to merge 1 commit intok2-fsa:masterfrom
csukuangfj:mmap

Conversation

@csukuangfj
Copy link
Copy Markdown
Collaborator

Fixes #3226

I evaluated the Qwen3 ASR and SenseVoice int8 models. My tests show that enabling mmap does not yield any noticeable improvement in loading latency. Additionally, converting the ONNX models to .ort format also failed to reduce the loading time.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request implements memory-mapping for model loading in OfflineQwen3ASRModel and OfflineSenseVoiceModel on non-Windows systems, enhancing performance. It also updates model configuration descriptions to include support for the .ort format. Review feedback suggests centralizing the mmap utility functions to a shared file to eliminate code duplication, adopting a consistent MappedModel structure across different model implementations, using the standard int type for errno, and refactoring repetitive initialization methods to reduce boilerplate.

Comment on lines +244 to +288
void MapModel(const std::string &filename, const char *name,
MappedModel *mapped_model) {
int fd = open(filename.c_str(), O_RDONLY);
if (fd == -1) {
SHERPA_ONNX_LOGE("Failed to open %s '%s': %s", name, filename.c_str(),
std::strerror(errno));
SHERPA_ONNX_EXIT(-1);
}

struct stat st;
if (fstat(fd, &st) == -1) {
SHERPA_ONNX_LOGE("Failed to stat %s '%s': %s", name, filename.c_str(),
std::strerror(errno));
close(fd);
SHERPA_ONNX_EXIT(-1);
}

if (st.st_size <= 0) {
SHERPA_ONNX_LOGE("%s '%s' has invalid size: %lld", name, filename.c_str(),
static_cast<long long>(st.st_size));
close(fd);
SHERPA_ONNX_EXIT(-1);
}

void *data = mmap(nullptr, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
int32_t saved_errno = errno;
close(fd);

if (data == MAP_FAILED) {
SHERPA_ONNX_LOGE("Failed to mmap %s '%s': %s", name, filename.c_str(),
std::strerror(saved_errno));
SHERPA_ONNX_EXIT(-1);
}

mapped_model->data = data;
mapped_model->size = static_cast<size_t>(st.st_size);
}

void UnmapModel(MappedModel *mapped_model) {
if (mapped_model->data) {
munmap(mapped_model->data, mapped_model->size);
mapped_model->data = nullptr;
mapped_model->size = 0;
}
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The logic for memory-mapping models (MapModel and UnmapModel) is duplicated here and in offline-sense-voice-model.cc. Following the general rules for this repository, these utility functions should be moved to a common utility file (e.g., sherpa-onnx/csrc/mmap-utils.h) to improve maintainability and reduce code duplication.

References
  1. Move duplicated utility functions, such as Trim, to a common utility file (e.g., text-utils.h and text-utils.cc) for reuse across the codebase.

Comment on lines +230 to +231
void *mapped_model_data_ = nullptr;
size_t mapped_model_size_ = 0;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For consistency with the implementation in offline-qwen3-asr-model.cc, consider using a MappedModel struct to group the mapped data pointer and its size. This would also facilitate refactoring the mmap logic into a shared utility.

References
  1. Move duplicated utility functions, such as Trim, to a common utility file (e.g., text-utils.h and text-utils.cc) for reuse across the codebase.

}

void *data = mmap(nullptr, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
int32_t saved_errno = errno;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

errno is defined as an int. While int32_t is often the same size on modern platforms, it is more idiomatic to use int when saving errno.

    int saved_errno = errno;

Comment on lines +209 to +241
void InitConvFrontendFromFile(const std::string &filename) {
#if defined(_WIN32)
conv_sess_ = std::make_unique<Ort::Session>(env_,
SHERPA_ONNX_TO_ORT_PATH(filename),
sess_opts_conv_);
InitConvFrontend(nullptr, 0);
#else
MapModel(filename, "qwen3_asr.conv_frontend", &conv_model_);
InitConvFrontend(conv_model_.data, conv_model_.size);
#endif
}

void InitEncoderFromFile(const std::string &filename) {
#if defined(_WIN32)
encoder_sess_ = std::make_unique<Ort::Session>(
env_, SHERPA_ONNX_TO_ORT_PATH(filename), sess_opts_encoder_);
InitEncoder(nullptr, 0);
#else
MapModel(filename, "qwen3_asr.encoder", &encoder_model_);
InitEncoder(encoder_model_.data, encoder_model_.size);
#endif
}

void InitDecoderFromFile(const std::string &filename) {
#if defined(_WIN32)
decoder_sess_ = std::make_unique<Ort::Session>(
env_, SHERPA_ONNX_TO_ORT_PATH(filename), sess_opts_decoder_);
InitDecoder(nullptr, 0);
#else
MapModel(filename, "qwen3_asr.decoder", &decoder_model_);
InitDecoder(decoder_model_.data, decoder_model_.size);
#endif
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The methods InitConvFrontendFromFile, InitEncoderFromFile, and InitDecoderFromFile follow an identical pattern. Consider refactoring this into a single helper method that takes the filename, model name, session pointer, session options, and the specific initialization function as arguments to reduce boilerplate code.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 8, 2026

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 69a5693d-3dfb-461c-ae73-557192b6a62e

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Help wanted] Support loading ort format models using mmap on non-Windows platforms

1 participant