-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
15 changed files
with
557 additions
and
7 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
load("//yggdrasil_decision_forests/utils:compile.bzl", "cc_library_ydf") | ||
|
||
package( | ||
default_visibility = ["//visibility:public"], | ||
licenses = ["notice"], | ||
) | ||
|
||
cc_library_ydf( | ||
name = "serving", | ||
hdrs = ["serving.h"], | ||
deps = [ | ||
"//yggdrasil_decision_forests/model:abstract_model", | ||
"//yggdrasil_decision_forests/model:all_models", | ||
"//yggdrasil_decision_forests/model:model_library", | ||
"//yggdrasil_decision_forests/serving:example_set", | ||
"//yggdrasil_decision_forests/serving:fast_engine", | ||
"@com_google_absl//absl/status:statusor", | ||
"@com_google_absl//absl/strings", | ||
], | ||
) |
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,57 @@ | ||
/* | ||
* Copyright 2022 Google LLC. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#ifndef YGGDRASIL_DECISION_FORESTS_API_SERVING_H_ | ||
#define YGGDRASIL_DECISION_FORESTS_API_SERVING_H_ | ||
|
||
#include <memory> | ||
|
||
#include "absl/status/statusor.h" | ||
#include "absl/strings/string_view.h" | ||
#include "yggdrasil_decision_forests/model/abstract_model.h" | ||
#include "yggdrasil_decision_forests/model/model_library.h" | ||
#include "yggdrasil_decision_forests/serving/example_set.h" | ||
#include "yggdrasil_decision_forests/serving/fast_engine.h" | ||
|
||
namespace yggdrasil_decision_forests::serving_api { | ||
|
||
using AbstractModel = ::yggdrasil_decision_forests::model::AbstractModel; | ||
using FastEngine = ::yggdrasil_decision_forests::serving::FastEngine; | ||
using FeaturesDefinition = | ||
::yggdrasil_decision_forests::serving::FeaturesDefinition; | ||
|
||
using NumericalFeatureId = ::yggdrasil_decision_forests::serving:: | ||
FeaturesDefinitionNumericalOrCategoricalFlat::NumericalFeatureId; | ||
using CategoricalFeatureId = ::yggdrasil_decision_forests::serving:: | ||
FeaturesDefinitionNumericalOrCategoricalFlat::CategoricalFeatureId; | ||
using CategoricalSetFeatureId = ::yggdrasil_decision_forests::serving:: | ||
FeaturesDefinitionNumericalOrCategoricalFlat::CategoricalSetFeatureId; | ||
using BooleanFeatureId = ::yggdrasil_decision_forests::serving:: | ||
FeaturesDefinitionNumericalOrCategoricalFlat::BooleanFeatureId; | ||
using MultiDimNumericalFeatureId = ::yggdrasil_decision_forests::serving:: | ||
FeaturesDefinitionNumericalOrCategoricalFlat::MultiDimNumericalFeatureId; | ||
|
||
// Loads a model in memory. This model can then be compiled to be run | ||
// efficiently. | ||
inline absl::StatusOr<std::unique_ptr<AbstractModel>> LoadModel( | ||
absl::string_view directory) { | ||
std::unique_ptr<AbstractModel> model; | ||
RETURN_IF_ERROR(model::LoadModel(directory, &model)); | ||
return model; | ||
} | ||
|
||
} // namespace yggdrasil_decision_forests::serving_api | ||
|
||
#endif // YGGDRASIL_DECISION_FORESTS_API_SERVING_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
50 changes: 50 additions & 0 deletions
50
yggdrasil_decision_forests/port/python/ydf/model/export_cc_generator.py
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,50 @@ | ||
# Copyright 2022 Google LLC. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Generates the c++ code to run a model for a unit-test.""" | ||
|
||
|
||
from collections.abc import Sequence | ||
|
||
from absl import app | ||
from absl import flags | ||
from absl import logging | ||
|
||
import ydf | ||
|
||
_INPUT_MODEL = flags.DEFINE_string("input_model", None, "Path to input model") | ||
|
||
_OUTPUT_CODE = flags.DEFINE_string( | ||
"output_code", None, "Path to generated c++ file." | ||
) | ||
|
||
|
||
def process(input_model: str, output_code: str) -> None: | ||
logging.info( | ||
"Loading model %s and generating cc code in %s", input_model, output_code | ||
) | ||
|
||
model = ydf.load_model(input_model) | ||
with open(output_code, "w") as f: | ||
f.write(model.to_cpp("123")) | ||
|
||
|
||
def main(argv: Sequence[str]) -> None: | ||
if len(argv) > 1: | ||
raise app.UsageError("Too many command-line arguments.") | ||
process(_INPUT_MODEL.value, _OUTPUT_CODE.value) | ||
|
||
|
||
if __name__ == "__main__": | ||
app.run(main) |
47 changes: 47 additions & 0 deletions
47
yggdrasil_decision_forests/port/python/ydf/model/export_cc_run_test.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,47 @@ | ||
/* | ||
* Copyright 2022 Google LLC. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include <string> | ||
|
||
#include "gmock/gmock.h" | ||
#include "gtest/gtest.h" | ||
#include "ydf/model/export_cc_generated_lib.h" | ||
#include "yggdrasil_decision_forests/utils/filesystem.h" | ||
#include "yggdrasil_decision_forests/utils/logging.h" | ||
#include "yggdrasil_decision_forests/utils/test.h" | ||
|
||
namespace yggdrasil_decision_forests { | ||
namespace { | ||
|
||
std::string TestDataDir() { | ||
return file::JoinPath(test::DataRootDirectory(), | ||
"yggdrasil_decision_forests/test_data"); | ||
} | ||
|
||
TEST(RunModel, Base) { | ||
const auto model = exported_model_123::Load( | ||
file::JoinPath(TestDataDir(), "model", "adult_binary_class_gbdt")); | ||
ASSERT_OK(model.status()); | ||
|
||
const auto predictions = model->Predict(); | ||
|
||
YDF_LOG(INFO) << "Predictions:"; | ||
for (const float p : predictions) { | ||
YDF_LOG(INFO) << p; | ||
} | ||
} | ||
|
||
} // namespace | ||
} // namespace yggdrasil_decision_forests |
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
Oops, something went wrong.