-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use on-device model service in opt guide
Add a controller to use the on-device service. Also adds commandline switch for overriding model path. Bug: b/302402576 Change-Id: Ife417eea6f3090bcf514d0a99f4e7f5e2341c1b5 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4950733 Reviewed-by: Sophie Chang <sophiechang@chromium.org> Reviewed-by: Clark DuVall <cduvall@chromium.org> Commit-Queue: Raj T <rajendrant@chromium.org> Cr-Commit-Position: refs/heads/main@{#1215846}
- Loading branch information
rajendrant
authored and
Chromium LUCI CQ
committed
Oct 27, 2023
1 parent
6878592
commit 82e89b6
Showing
15 changed files
with
395 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
...e/browser/optimization_guide/model_execution/chrome_on_device_model_service_controller.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright 2023 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "chrome/browser/optimization_guide/model_execution/chrome_on_device_model_service_controller.h" | ||
|
||
#include "content/public/browser/service_process_host.h" | ||
|
||
namespace optimization_guide { | ||
|
||
ChromeOnDeviceModelServiceController::ChromeOnDeviceModelServiceController() = | ||
default; | ||
ChromeOnDeviceModelServiceController::~ChromeOnDeviceModelServiceController() = | ||
default; | ||
|
||
void ChromeOnDeviceModelServiceController::LaunchService() { | ||
content::ServiceProcessHost::Launch< | ||
on_device_model::mojom::OnDeviceModelService>( | ||
service_remote_.BindNewPipeAndPassReceiver(), | ||
content::ServiceProcessHost::Options() | ||
.WithDisplayName("On-Device Model Service") | ||
.Pass()); | ||
} | ||
|
||
} // namespace optimization_guide |
25 changes: 25 additions & 0 deletions
25
...me/browser/optimization_guide/model_execution/chrome_on_device_model_service_controller.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright 2023 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef CHROME_BROWSER_OPTIMIZATION_GUIDE_MODEL_EXECUTION_CHROME_ON_DEVICE_MODEL_SERVICE_CONTROLLER_H_ | ||
#define CHROME_BROWSER_OPTIMIZATION_GUIDE_MODEL_EXECUTION_CHROME_ON_DEVICE_MODEL_SERVICE_CONTROLLER_H_ | ||
|
||
#include "components/optimization_guide/core/model_execution/on_device_model_service_controller.h" | ||
|
||
namespace optimization_guide { | ||
|
||
class ChromeOnDeviceModelServiceController | ||
: public OnDeviceModelServiceController { | ||
public: | ||
ChromeOnDeviceModelServiceController(); | ||
~ChromeOnDeviceModelServiceController() override; | ||
|
||
private: | ||
// OnDeviceModelServiceController implementation: | ||
void LaunchService() override; | ||
}; | ||
|
||
} // namespace optimization_guide | ||
|
||
#endif // CHROME_BROWSER_OPTIMIZATION_GUIDE_MODEL_EXECUTION_CHROME_ON_DEVICE_MODEL_SERVICE_CONTROLLER_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
components/optimization_guide/core/model_execution/on_device_model_service_controller.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright 2023 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
#include "components/optimization_guide/core/model_execution/on_device_model_service_controller.h" | ||
#include "services/on_device_model/public/cpp/model_assets.h" | ||
#include "services/on_device_model/public/mojom/on_device_model.mojom.h" | ||
|
||
namespace optimization_guide { | ||
|
||
OnDeviceModelServiceController::OnDeviceModelServiceController() = default; | ||
OnDeviceModelServiceController::~OnDeviceModelServiceController() = default; | ||
|
||
void OnDeviceModelServiceController::Init(const base::FilePath& model_path) { | ||
CHECK(model_path_.empty()); | ||
model_path_ = model_path; | ||
} | ||
|
||
void OnDeviceModelServiceController::Execute( | ||
std::string_view input, | ||
mojo::PendingRemote<on_device_model::mojom::StreamingResponder> | ||
streaming_responder) { | ||
if (model_remote_) { | ||
model_remote_->Execute(std::string(input), std::move(streaming_responder)); | ||
return; | ||
} | ||
LaunchService(); | ||
service_remote_->LoadModel( | ||
on_device_model::LoadModelAssets(model_path_), | ||
base::BindOnce(&OnDeviceModelServiceController::OnLoadModelResult, | ||
weak_ptr_factory_.GetWeakPtr(), input, | ||
std::move(streaming_responder))); | ||
} | ||
|
||
void OnDeviceModelServiceController::OnLoadModelResult( | ||
std::string_view input, | ||
mojo::PendingRemote<on_device_model::mojom::StreamingResponder> | ||
streaming_responder, | ||
on_device_model::mojom::LoadModelResultPtr result) { | ||
if (result->is_model()) { | ||
model_remote_ = mojo::Remote<on_device_model::mojom::OnDeviceModel>( | ||
std::move(result->get_model())); | ||
Execute(input, std::move(streaming_responder)); | ||
} | ||
} | ||
|
||
} // namespace optimization_guide |
71 changes: 71 additions & 0 deletions
71
components/optimization_guide/core/model_execution/on_device_model_service_controller.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright 2023 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
#ifndef COMPONENTS_OPTIMIZATION_GUIDE_CORE_MODEL_EXECUTION_ON_DEVICE_MODEL_SERVICE_CONTROLLER_H_ | ||
#define COMPONENTS_OPTIMIZATION_GUIDE_CORE_MODEL_EXECUTION_ON_DEVICE_MODEL_SERVICE_CONTROLLER_H_ | ||
|
||
#include <string_view> | ||
|
||
#include "base/memory/weak_ptr.h" | ||
#include "base/sequence_checker.h" | ||
#include "mojo/public/cpp/bindings/remote.h" | ||
#include "services/on_device_model/public/mojom/on_device_model.mojom.h" | ||
#include "third_party/abseil-cpp/absl/types/optional.h" | ||
|
||
namespace base { | ||
class FilePath; | ||
} // namespace base | ||
|
||
namespace optimization_guide { | ||
|
||
// Controls the lifetime of the on-device model service, loading and unloading | ||
// of the models, and executing them via the service. | ||
// | ||
// TODO(b/302402576): Handle unloading the model, and stopping the service. The | ||
// StreamingResponder should notify the controller upon completion to accomplish | ||
// this. Also handle multiple requests gracefully and fail the subsequent | ||
// requests, while handling the first one. | ||
class OnDeviceModelServiceController { | ||
public: | ||
OnDeviceModelServiceController(); | ||
virtual ~OnDeviceModelServiceController(); | ||
|
||
// Initializes the on-device model controller with the parameters, to be ready | ||
// to load models and execute. | ||
void Init(const base::FilePath& model_path); | ||
|
||
// Executes the model for `input` and the response will be sent to | ||
// `streaming_responder`. This will load the model if needed, before | ||
// execution. | ||
void Execute(std::string_view input, | ||
mojo::PendingRemote<on_device_model::mojom::StreamingResponder> | ||
streaming_responder); | ||
|
||
// Launches the on-device model-service. | ||
virtual void LaunchService() = 0; | ||
|
||
private: | ||
friend class ChromeOnDeviceModelServiceController; | ||
friend class OnDeviceModelServiceControllerTest; | ||
friend class FakeOnDeviceModelServiceController; | ||
|
||
// Invoked at the end of model load, to continue with model execution. | ||
void OnLoadModelResult( | ||
std::string_view input, | ||
mojo::PendingRemote<on_device_model::mojom::StreamingResponder> | ||
streaming_responder, | ||
on_device_model::mojom::LoadModelResultPtr result); | ||
|
||
base::FilePath model_path_; | ||
mojo::Remote<on_device_model::mojom::OnDeviceModelService> service_remote_; | ||
mojo::Remote<on_device_model::mojom::OnDeviceModel> model_remote_; | ||
|
||
SEQUENCE_CHECKER(sequence_checker_); | ||
|
||
// Used to get `weak_ptr_` to self. | ||
base::WeakPtrFactory<OnDeviceModelServiceController> weak_ptr_factory_{this}; | ||
}; | ||
|
||
} // namespace optimization_guide | ||
|
||
#endif // COMPONENTS_OPTIMIZATION_GUIDE_CORE_MODEL_EXECUTION_ON_DEVICE_MODEL_SERVICE_CONTROLLER_H_ |
Oops, something went wrong.