Skip to content

Fix no global destructor for strings in training modules #12521

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 15, 2025
Merged
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
26 changes: 18 additions & 8 deletions extension/training/module/training_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,19 @@ namespace extension {
namespace training {

namespace {
std::string gradients_method_prefix = "__et_training_gradients_index_";
std::string parameters_method_prefix = "__et_training_parameters_index_";
std::string fqn_method_prefix = "__et_training_fqn_";

std::string make_parameters_method_name(const std::string& method_name) {
return "__et_training_parameters_index_" + method_name;
}

std::string make_gradients_method_name(const std::string& method_name) {
return "__et_training_gradients_index_" + method_name;
}

std::string make_fqn_method_name(const std::string& method_name) {
return "__et_training_fqn_" + method_name;
}

} // namespace

runtime::Result<std::vector<runtime::EValue>>
Expand All @@ -24,15 +34,15 @@ TrainingModule::execute_forward_backward(
const std::vector<runtime::EValue>& input) {
// Find where the user outputs end.
const std::string gradients_method_name =
gradients_method_prefix + method_name;
make_gradients_method_name(method_name);
auto res = executorch::extension::Module::execute(gradients_method_name);
if (!res.ok()) {
return res.error();
}
uint64_t grad_start = res.get()[0].toInt();

const std::string parameters_method_name =
parameters_method_prefix + method_name;
make_parameters_method_name(method_name);
// get params start.
auto param_res =
executorch::extension::Module::execute(parameters_method_name);
Expand Down Expand Up @@ -66,7 +76,7 @@ TrainingModule::execute_forward_backward(
auto& gradients_map = method_named_gradients_.at(method_name);

// Get names if we havent seen this method before.
const std::string fqn_method_name = fqn_method_prefix + method_name;
const std::string fqn_method_name = make_fqn_method_name(method_name);
auto fqn_res = executorch::extension::Module::execute(fqn_method_name);
if (!fqn_res.ok()) {
return fqn_res.error();
Expand All @@ -92,9 +102,9 @@ TrainingModule::named_parameters(const std::string& method_name) {
// If we haven't seen this method before, populate the dict.
if (method_named_parameters_.find(method_name) ==
method_named_parameters_.end()) {
const std::string fqn_method_name = fqn_method_prefix + method_name;
const std::string fqn_method_name = make_fqn_method_name(method_name);
const std::string parameters_method_name =
parameters_method_prefix + method_name;
make_parameters_method_name(method_name);

method_named_parameters_.insert({method_name, {}});

Expand Down
Loading