Skip to content

Commit

Permalink
Internal change
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 416556647
  • Loading branch information
achoum authored and copybara-github committed Dec 15, 2021
1 parent 511d113 commit 9e9f777
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 17 deletions.
22 changes: 10 additions & 12 deletions yggdrasil_decision_forests/learner/abstract_learner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -619,32 +619,30 @@ void InitializeModelWithAbstractTrainingConfig(

void InitializeModelMetadataWithAbstractTrainingConfig(
const proto::TrainingConfig& training_config, AbstractModel* model) {
const auto& src = training_config.metadata();
auto& dst = *model->mutable_metadata();

dst = src;
auto* dst = model->mutable_metadata();
dst->Import(training_config.metadata());

// Owner
if (!dst.has_owner()) {
if (dst->owner().empty()) {
auto opt_username = utils::UserName();
if (opt_username.has_value()) {
dst.set_owner(std::move(opt_username).value());
dst->set_owner(std::move(opt_username).value());
}
}

// Date
if (!dst.has_created_date()) {
dst.set_created_date(absl::ToUnixSeconds(absl::Now()));
if (dst->created_date() == 0) {
dst->set_created_date(absl::ToUnixSeconds(absl::Now()));
}

// UID
if (!dst.has_uid()) {
dst.set_uid(utils::GenUniqueIdUint64());
if (dst->uid() == 0) {
dst->set_uid(utils::GenUniqueIdUint64());
}

// Framework
if (!dst.has_framework()) {
dst.set_framework("Yggdrasil c++");
if (dst->framework().empty()) {
dst->set_framework("Yggdrasil c++");
}
}

Expand Down
2 changes: 2 additions & 0 deletions yggdrasil_decision_forests/model/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ cc_library_ydf(
srcs = [
"abstract_model.cc",
"fast_engine_factory.cc",
"metadata.cc",
],
hdrs = [
"abstract_model.h",
"fast_engine_factory.h",
"metadata.h",
],
deps = [
":abstract_model_cc_proto",
Expand Down
4 changes: 2 additions & 2 deletions yggdrasil_decision_forests/model/abstract_model.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void AbstractModel::ExportProto(const AbstractModel& model,
proto->set_classification_outputs_probabilities(
model.classification_outputs_probabilities_);

*proto->mutable_metadata() = model.metadata();
model.metadata().Export(proto->mutable_metadata());
}

void AbstractModel::ImportProto(const proto::AbstractModel& proto,
Expand All @@ -100,7 +100,7 @@ void AbstractModel::ImportProto(const proto::AbstractModel& proto,
model->classification_outputs_probabilities_ =
proto.classification_outputs_probabilities();

*model->mutable_metadata() = proto.metadata();
model->mutable_metadata()->Import(proto.metadata());
}

metric::proto::EvaluationResults AbstractModel::Evaluate(
Expand Down
12 changes: 9 additions & 3 deletions yggdrasil_decision_forests/model/abstract_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "yggdrasil_decision_forests/metric/metric.pb.h"
#include "yggdrasil_decision_forests/model/abstract_model.pb.h"
#include "yggdrasil_decision_forests/model/fast_engine_factory.h"
#include "yggdrasil_decision_forests/model/metadata.h"
#include "yggdrasil_decision_forests/model/prediction.pb.h"
#include "yggdrasil_decision_forests/serving/fast_engine.h"
#include "yggdrasil_decision_forests/utils/compatibility.h"
Expand Down Expand Up @@ -359,8 +360,11 @@ class AbstractModel {
const std::vector<std::string>& variable_importances);

// Metadata accessors.
const proto::Metadata& metadata() const { return metadata_; }
proto::Metadata* mutable_metadata() { return &metadata_; }
//
// Note: The use of "MetaData" (instead of "proto::MetaData") is a temporary
// change. Do not depend on it.
const MetaData& metadata() const { return metadata_; }
MetaData* mutable_metadata() { return &metadata_; }

protected:
explicit AbstractModel(const absl::string_view name) : name_(name) {}
Expand Down Expand Up @@ -411,7 +415,9 @@ class AbstractModel {
// of the task=CLASSIFICATION model might not be a probability.
bool classification_outputs_probabilities_ = true;

proto::Metadata metadata_;
// TODO(gbm): Use proto::Metadata.
// Note: Cannot use proto::Metadata with the version of protobuf linked by TF.
MetaData metadata_;

// Note: New fields should be registered in:
// - The proto serialization functions.
Expand Down
36 changes: 36 additions & 0 deletions yggdrasil_decision_forests/model/metadata.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2021 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 "yggdrasil_decision_forests/model/metadata.h"

namespace yggdrasil_decision_forests {
namespace model {

void MetaData::Export(model::proto::Metadata* dst) const {
dst->set_owner(owner_);
dst->set_created_date(created_date_);
dst->set_uid(uid_);
dst->set_framework(framework_);
}

void MetaData::Import(const model::proto::Metadata& src) {
owner_ = src.owner();
created_date_ = src.created_date();
uid_ = src.uid();
framework_ = src.framework();
}

} // namespace model
} // namespace yggdrasil_decision_forests
54 changes: 54 additions & 0 deletions yggdrasil_decision_forests/model/metadata.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2021 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_MODEL_METADATA_H_
#define YGGDRASIL_DECISION_FORESTS_MODEL_METADATA_H_

#include <string>

#include "yggdrasil_decision_forests/model/abstract_model.pb.h"

namespace yggdrasil_decision_forests {
namespace model {

// Meta-data about the model.
class MetaData {
public:
const std::string& owner() const { return owner_; }
void set_owner(const std::string& value) { owner_ = value; }

const uint64_t created_date() const { return created_date_; }
void set_created_date(const uint64_t value) { created_date_ = value; }

const uint64_t uid() const { return uid_; }
void set_uid(const uint64_t value) { uid_ = value; }

const std::string& framework() const { return framework_; }
void set_framework(const std::string& value) { framework_ = value; }

void Export(model::proto::Metadata* dst) const;
void Import(const model::proto::Metadata& src);

private:
std::string owner_;
int64_t created_date_ = 0;
uint64_t uid_ = 0;
std::string framework_;
};

} // namespace model
} // namespace yggdrasil_decision_forests

#endif // YGGDRASIL_DECISION_FORESTS_MODEL_METADATA_H_

0 comments on commit 9e9f777

Please sign in to comment.