-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
[jit] jit support property.proto #44337
Merged
Merged
Changes from 17 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
114869e
add property.proto, can compiled
zh794390558 3ae745c
property get and deserilize
zh794390558 5f628f4
support get float
zh794390558 b3eadde
format code
zh794390558 0f5d0e0
format code
zh794390558 496256d
add unittest
zh794390558 eed7a8c
add more set method
zh794390558 528ab77
fix grammar error
zh794390558 97f2601
Update paddle/fluid/jit/property.h
zh794390558 6bd27ea
Update paddle/fluid/jit/property.cc
zh794390558 7c860fa
Update paddle/fluid/jit/property.cc
zh794390558 3d7f2c1
Update paddle/fluid/jit/property.cc
zh794390558 2765add
fix comment
zh794390558 37ad0a8
fix error throw
zh794390558 febae4e
fix property save unit test
zh794390558 d0864f5
fix error info
zh794390558 2ed5590
fix copyright and header import
zh794390558 27c8d0b
reorder jit property tensor datatype
zh794390558 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,173 @@ | ||
/* Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. | ||
|
||
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 | ||
|
||
http://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 "paddle/fluid/jit/property.h" | ||
#include "glog/logging.h" | ||
#include "paddle/phi/core/enforce.h" | ||
#include "paddle/phi/core/errors.h" | ||
|
||
namespace paddle { | ||
namespace jit { | ||
|
||
int Property::Size() const { return property_.entrys_size(); } | ||
|
||
void Property::SetFloat(const float &f) { | ||
auto type = proto::ValueProto::FLOAT; | ||
auto entry = property_.add_entrys(); | ||
entry->set_type(type); | ||
entry->set_f(f); | ||
VLOG(3) << "Property: set_float " << f; | ||
} | ||
|
||
void Property::SetFloat(const std::string &name, const float &f) { | ||
auto type = proto::ValueProto::FLOAT; | ||
auto entry = property_.add_entrys(); | ||
entry->set_name(name); | ||
entry->set_type(type); | ||
entry->set_f(f); | ||
VLOG(3) << "Property: set_float " << f << " name: " << name; | ||
} | ||
|
||
float Property::GetFloat(const std::string &name) const { | ||
for (int i = 0; i < Size(); i++) { | ||
auto e = property_.entrys(i); | ||
if (e.has_name() && e.name() == name) { | ||
return e.f(); | ||
} | ||
} | ||
|
||
PADDLE_THROW(phi::errors::NotFound( | ||
"JIT::Property GetFloat: name: %s not found", name)); | ||
return 0; | ||
} | ||
|
||
float Property::GetFloat(const int &idx) const { | ||
PADDLE_ENFORCE_EQ( | ||
idx < Size() && idx >= 0, | ||
true, | ||
phi::errors::OutOfRange( | ||
"JIT::Property GetFloat: idx=%d out of range %d", idx, Size())); | ||
|
||
auto e = property_.entrys(idx); | ||
if (e.has_f()) { | ||
return e.f(); | ||
} | ||
|
||
PADDLE_THROW(phi::errors::InvalidArgument( | ||
"JIT::Property GetFloat: input idx (%d) element is not a float.", idx)); | ||
return 0; | ||
} | ||
|
||
void Property::SetFloats(const std::vector<float> &v) { | ||
auto type = proto::ValueProto::FLOATS; | ||
auto entry = property_.add_entrys(); | ||
entry->set_type(type); | ||
for (auto i : v) { | ||
entry->add_floats(i); | ||
} | ||
VLOG(3) << "Property: set_floats with length: " << v.size(); | ||
} | ||
|
||
void Property::SetFloats(const std::string &name, const std::vector<float> &v) { | ||
auto type = proto::ValueProto::FLOATS; | ||
auto entry = property_.add_entrys(); | ||
entry->set_name(name); | ||
entry->set_type(type); | ||
for (auto i : v) { | ||
entry->add_floats(i); | ||
} | ||
VLOG(3) << "Property: set_floats with length " << v.size() | ||
<< " for name: " << name; | ||
} | ||
|
||
void Property::SetInt64(const int64_t &i) { | ||
auto type = proto::ValueProto::INT; | ||
auto entry = property_.add_entrys(); | ||
entry->set_type(type); | ||
entry->set_i(i); | ||
VLOG(3) << "Property: set_int " << i; | ||
} | ||
|
||
void Property::SetInt64(const std::string &name, const int64_t &i) { | ||
auto type = proto::ValueProto::INT; | ||
auto entry = property_.add_entrys(); | ||
entry->set_name(name); | ||
entry->set_type(type); | ||
entry->set_i(i); | ||
VLOG(3) << "Property: set_int " << i << " name: " << name; | ||
zh794390558 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
void Property::SetInt64s(const std::vector<int64_t> &v) { | ||
auto type = proto::ValueProto::INTS; | ||
auto entry = property_.add_entrys(); | ||
entry->set_type(type); | ||
for (auto e : v) { | ||
entry->add_ints(e); | ||
} | ||
VLOG(3) << "Property: set_ints " << v.size(); | ||
zh794390558 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
void Property::SetInt64s(const std::string &name, | ||
const std::vector<int64_t> &v) { | ||
auto type = proto::ValueProto::INTS; | ||
auto entry = property_.add_entrys(); | ||
entry->set_name(name); | ||
entry->set_type(type); | ||
for (auto i : v) { | ||
entry->add_ints(i); | ||
} | ||
VLOG(3) << "Property: set_ints " << v[0] << " name: " << name; | ||
zh794390558 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
void Property::SetString(const std::string &s) { | ||
auto type = proto::ValueProto::STRING; | ||
auto entry = property_.add_entrys(); | ||
entry->set_type(type); | ||
entry->set_s(s); | ||
VLOG(3) << "Property: set_string with value : " << s; | ||
} | ||
|
||
void Property::SetString(const std::string &name, const std::string &s) { | ||
auto type = proto::ValueProto::STRING; | ||
auto entry = property_.add_entrys(); | ||
entry->set_name(name); | ||
entry->set_type(type); | ||
entry->set_s(s); | ||
VLOG(3) << "Property: set_string " << s << " name: " << name; | ||
} | ||
|
||
void Property::SetStrings(const std::vector<std::string> &v) { | ||
auto type = proto::ValueProto::STRINGS; | ||
auto entry = property_.add_entrys(); | ||
entry->set_type(type); | ||
for (auto i : v) { | ||
entry->add_strings(i); | ||
} | ||
VLOG(3) << "Property: set_strings " << v.size(); | ||
} | ||
|
||
void Property::SetStrings(const std::string &name, | ||
const std::vector<std::string> &v) { | ||
auto type = proto::ValueProto::STRINGS; | ||
auto entry = property_.add_entrys(); | ||
entry->set_name(name); | ||
entry->set_type(type); | ||
for (auto i : v) { | ||
entry->add_strings(i); | ||
} | ||
VLOG(3) << "Property: set_strings " << v[0] << " name: " << name; | ||
} | ||
|
||
} // namespace jit | ||
} // namespace paddle |
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,93 @@ | ||
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. | ||
// | ||
// 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 | ||
// | ||
// http://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. | ||
|
||
#pragma once | ||
|
||
#include <algorithm> | ||
#include <atomic> | ||
#include <string> | ||
#include <vector> | ||
|
||
#include "paddle/fluid/jit/property.pb.h" | ||
|
||
namespace paddle { | ||
namespace jit { | ||
|
||
class Property { | ||
public: | ||
Property() {} | ||
|
||
// Explicitly implement the copy constructor for auto parallel | ||
explicit Property(const Property &other) | ||
: property_(other.property_), original_id_(other.original_id_) {} | ||
|
||
Property &operator=(const Property &other) { | ||
property_ = other.property_; | ||
original_id_ = other.original_id_; | ||
return *this; | ||
} | ||
|
||
proto::PropertyVals *Proto() { return &property_; } | ||
|
||
const proto::PropertyVals *Proto() const { return &property_; } | ||
|
||
int Size() const; | ||
|
||
void SetFloat(const float &f); | ||
void SetFloat(const std::string &name, const float &f); | ||
|
||
void SetFloats(const std::vector<float> &v); | ||
void SetFloats(const std::string &name, const std::vector<float> &v); | ||
|
||
float GetFloat(const std::string &name) const; | ||
float GetFloat(const int &idx) const; | ||
|
||
void SetInt64(const int64_t &i); | ||
void SetInt64(const std::string &name, const int64_t &i); | ||
|
||
void SetInt64s(const std::vector<int64_t> &v); | ||
void SetInt64s(const std::string &name, const std::vector<int64_t> &v); | ||
|
||
void SetString(const std::string &s); | ||
void SetString(const std::string &name, const std::string &s); | ||
|
||
void SetStrings(const std::vector<std::string> &v); | ||
void SetStrings(const std::string &name, const std::vector<std::string> &v); | ||
|
||
// The Id() and OriginalId() are only used for auto parallel. | ||
uint64_t Id() const { return id_; } | ||
uint64_t OriginalId() const { return original_id_; } | ||
void SetOriginalId(uint64_t original_id) { original_id_ = original_id; } | ||
|
||
private: | ||
proto::PropertyVals property_; | ||
|
||
// This thread-safe implementation seems to be redudent since the neural | ||
// networks are usually constructed in a single thread. | ||
static uint64_t GenerateId() { | ||
static std::atomic<std::uint64_t> uid{0}; | ||
return ++uid; | ||
} | ||
|
||
// Note: the id_ is unique for all Property (only for auto parallel). | ||
uint64_t id_ = GenerateId(); | ||
// Note: the orignal_id_ is used for referring to the original Property | ||
// that the current Property is built from (only for auto parallel). | ||
// The default original_id_ is same as the id_, which means the | ||
// current Property is not built from the other one. | ||
uint64_t original_id_ = id_; | ||
}; | ||
|
||
} // namespace jit | ||
} // namespace paddle |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.