Skip to content

Env amends#436

Open
Ero-Sennin9 wants to merge 35 commits into
triton-inference-server:mainfrom
Ero-Sennin9:env_amends
Open

Env amends#436
Ero-Sennin9 wants to merge 35 commits into
triton-inference-server:mainfrom
Ero-Sennin9:env_amends

Conversation

@Ero-Sennin9
Copy link
Copy Markdown

No description provided.

Comment thread src/pb_env.cc Outdated
std::string
EnvironmentManager::ExtractIfNotExtracted(std::string env_path)
std::shared_ptr<Environment> // TODO: write logic with shared and weak ptrs in this method
EnvironmentManager::GetEnvironment(ModelState* model_state)
Copy link
Copy Markdown

@aleksn7 aleksn7 May 12, 2026

Choose a reason for hiding this comment

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

Let’s preserve the old name

Copy link
Copy Markdown

@aleksn7 aleksn7 May 12, 2026

Choose a reason for hiding this comment

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

I think EnvironmentManager doesn't need to know about model_state

Comment thread src/pb_env.cc Outdated
const auto env_itr = env_map_.find(canonical_env_path);

std::string model_id = model_state.GetModelId();
std::string env_key = model_state->Name() + "-" + model_id;
Copy link
Copy Markdown

@aleksn7 aleksn7 May 12, 2026

Choose a reason for hiding this comment

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

Why do we need to add model name here? Multiple models could refer to the same environment

Comment thread src/pb_env.cc Outdated
// need to be extracted again.
if (env_itr->second.second == last_modified_time) {
// need to be extracted again (or the current environment has no owners anymore).
if (env_itr->second.first.expired()) {
Copy link
Copy Markdown

@aleksn7 aleksn7 May 12, 2026

Choose a reason for hiding this comment

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

This condition is executed only if env has already been destroyed and model has to recreate it

Also, when deleting the environment, we want to remove its key from the map

In this case, we can’t derive the environment directory name from the map size, because that could cause environments to overwrite each other.

Comment thread src/pb_env.cc
if (env_itr->second.first.expired()) {
env_map_.erase(env_itr);
} else if (env_itr->second.second == last_modified_time) {
env_extracted = true;
Copy link
Copy Markdown

@aleksn7 aleksn7 May 12, 2026

Choose a reason for hiding this comment

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

We could store last_modified_time in Environment structure

Comment thread src/pb_env.cc Outdated
canonical_env_path)
.c_str());
return canonical_env_path;
return std::make_shared<Environment>(canonical_env_path, canonical_env_path);
Copy link
Copy Markdown

@aleksn7 aleksn7 May 12, 2026

Choose a reason for hiding this comment

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

We should return nullptr here instead

Comment thread src/pb_env.cc Outdated
std::string dst_env_path;
if (re_extraction) {
dst_env_path = env_map_[canonical_env_path].first;
dst_env_path = env_map_[env_key].first;
Copy link
Copy Markdown

@aleksn7 aleksn7 May 12, 2026

Choose a reason for hiding this comment

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

I don't think this will compile
It's better to call the ->Path() method here

Comment thread src/pb_env.cc Outdated
}

Environment::Environment(const std::string& source, const std::string& path) : source_(source), path_(path) {
if (source == path) {
Copy link
Copy Markdown

@aleksn7 aleksn7 May 12, 2026

Choose a reason for hiding this comment

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

Let's check this condition before creating the object.
Also, in this case, destructor will be called with an invalid Path value

Comment thread src/pb_env.h Outdated
#include <mutex>
#include <string>

#include "python_be.h"
Copy link
Copy Markdown

@aleksn7 aleksn7 May 12, 2026

Choose a reason for hiding this comment

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

We don't need this include if we remove model_state from the GetEnvironment signature

Comment thread src/stub_launcher.cc Outdated
model_state->StateForBackend()->env_manager->ExtractIfNotExtracted(
python_execution_env_);
python_execution_env_ =
std::move(model_state->StateForBackend()->env_manager->GetEnvironment(model_state));
Copy link
Copy Markdown

@aleksn7 aleksn7 May 12, 2026

Choose a reason for hiding this comment

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

We don't need this std::move here because copy elision will work here

Comment thread src/pb_env.cc Outdated
if (re_extraction) {
// Just update the last modified timestamp
env_map_[canonical_env_path].second = last_modified_time;
if (re_extraction ) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Let's merge this condition with the condition on line 308

Comment thread src/pb_env.cc Outdated
// Add the environment to the list of environments
env = std::make_shared<Environment>(
canonical_env_path_str, dst_env_path, last_modified_time);
env->SetManager(this);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Why do we need a separate method for this? Let's pass it into the Environment constructor

Comment thread src/pb_env.cc Outdated
Environment::~Environment()
{
if (manager_ != nullptr) {
manager_->env_map_.erase(source_);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This map is protected by a mutex inside manager_
Let's add a method inside EnvironmentManager struct that locks the mutex and erases environment from the map.

Comment thread src/pb_env.h Outdated
const std::string& Source() const { return source_; }
const std::string& Path() const { return path_; }
const time_t& LastModifiedTime() const { return last_modified_time_; }
explicit operator std::string() const { return Path(); }
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Useless operator

Comment thread src/pb_env.cc Outdated
const auto env_itr = env_map_.find(canonical_env_path);

std::string canonical_env_path_str(canonical_env_path);
std::string env_key = canonical_env_path_str;
Copy link
Copy Markdown

@aleksn7 aleksn7 May 13, 2026

Choose a reason for hiding this comment

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

Why do we need three identical variables canonical_env_path_str, env_key and canonical_env_path?

I would write something like this:

std::string canonical_env_path = [&]{
    char canonical_env_path[PATH_MAX + 1];
    char* err = realpath(env_path.c_str(), canonical_env_path);
    if (err == nullptr) {
        throw PythonBackendException(
            std::string("Failed to get the canonical path for ") + env_path + ".");
    }
    return std::string(canonical_env_path);
}();

And drop the other variables: canonical_env_path_str, env_key

Comment thread src/pb_env.cc Outdated
// anymore).

if (env == nullptr) {
// refer to case when env was not loaded
Copy link
Copy Markdown

@aleksn7 aleksn7 May 13, 2026

Choose a reason for hiding this comment

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

We will enter this condition if an environment with such a key existed and was dropped earlier
Not because the environment hasn't been loaded yet

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

it simulates when env was not loaded, because we are deleting notice about it in map

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I said: "refers to case", but not "this is case when"

Comment thread src/pb_env.cc Outdated
EnvironmentManager::ExtractIfNotExtracted(std::string env_path)

std::shared_ptr<
Environment> // TODO: write logic with shared and weak ptrs in this method
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Let's drop the useless comment here

Comment thread src/pb_env.cc Outdated
}
const auto env_itr = env_map_.find(canonical_env_path);

std::string& env_key = canonical_env_path;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Do we really need this variable?

Comment thread src/pb_env.cc Outdated

std::string canonical_env_path_str(canonical_env_path);
if (!re_extraction) {
env->AddOwner();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

We should increment reference count on each ExtractIfNotExtracted call

Comment thread src/pb_env.cc Outdated
}
const auto env_itr = env_map_.find(canonical_env_path);

return EnvironmentGuard(*this, canonical_env_path);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Let's create environment here and pass it explicitly to the constructor

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

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants