Skip to content

Commit 8865404

Browse files
committed
Merged PR 2055: Remove using namespace from headers under lotus/core
1. Remove using namespace from headers under lotus/core 2. Fix a tiny bug in ExecutionFrame::ReleaseMLValue function Related work items: #137
1 parent 5ccd400 commit 8865404

File tree

115 files changed

+561
-600
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

115 files changed

+561
-600
lines changed

lotus/core/common/common.h

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
#include <algorithm>
2424
#include <functional>
25-
#include <map>
2625
#include <memory>
2726
#include <numeric>
2827
#include <set>
@@ -39,21 +38,11 @@
3938

4039
namespace Lotus {
4140

42-
template <typename Key, typename Value>
43-
using LotusMap = std::unordered_map<Key, Value>;
44-
4541
using TimePoint = std::chrono::high_resolution_clock::time_point;
4642

4743
// Using statements for common classes that we refer to in lotus very often.
4844
// TODO(Task:137) Remove 'using' statements from header files
4945
using Common::Status;
50-
using Common::StatusCategory;
51-
using Common::StatusCode;
52-
using std::make_unique;
53-
using std::set;
54-
using std::string;
55-
using std::unique_ptr;
56-
using std::vector;
5746

5847
#ifdef _WIN32
5948
#define UNUSED_PARAMETER(x) (x)
@@ -91,7 +80,7 @@ std::vector<std::string> GetStackTrace();
9180
if (!(condition)) throw Lotus::LotusException(WHERE_WITH_STACK, #condition, Lotus::MakeString(__VA_ARGS__))
9281

9382
#define LOTUS_MAKE_STATUS(category, code, ...) \
94-
Status(category, code, Lotus::MakeString(__VA_ARGS__))
83+
Lotus::Common::Status(Lotus::Common::category, Lotus::Common::code, Lotus::MakeString(__VA_ARGS__))
9584

9685
// Macros to disable the copy and/or move ctor and assignment methods
9786
// These are usually placed in the private: declarations for a class.
@@ -143,33 +132,33 @@ std::vector<std::string> GetStackTrace();
143132
#define LOTUS_EXPORT
144133
#endif
145134

146-
inline void MakeStringInternal(std::stringstream& /*ss*/) noexcept {}
135+
inline void MakeStringInternal(std::ostringstream& /*ss*/) noexcept {}
147136

148137
template <typename T>
149-
inline void MakeStringInternal(std::stringstream& ss, const T& t) noexcept {
138+
inline void MakeStringInternal(std::ostringstream& ss, const T& t) noexcept {
150139
ss << t;
151140
}
152141

153142
template <typename T, typename... Args>
154-
inline void MakeStringInternal(std::stringstream& ss, const T& t, const Args&... args) noexcept {
143+
inline void MakeStringInternal(std::ostringstream& ss, const T& t, const Args&... args) noexcept {
155144
::Lotus::MakeStringInternal(ss, t);
156145
::Lotus::MakeStringInternal(ss, args...);
157146
}
158147

159148
template <typename... Args>
160-
string MakeString(const Args&... args) {
161-
std::stringstream ss;
149+
std::string MakeString(const Args&... args) {
150+
std::ostringstream ss;
162151
::Lotus::MakeStringInternal(ss, args...);
163-
return string(ss.str());
152+
return std::string(ss.str());
164153
}
165154

166155
// Specializations for already-a-string types.
167156
template <>
168-
inline string MakeString(const string& str) {
157+
inline std::string MakeString(const std::string& str) {
169158
return str;
170159
}
171-
inline string MakeString(const char* p_str) {
172-
return string(p_str);
160+
inline std::string MakeString(const char* p_str) {
161+
return p_str;
173162
}
174163

175164
inline long long TimeDiffMicroSeconds(TimePoint start_time) {

lotus/core/common/logging/logging.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ LoggingManager::~LoggingManager() {
120120
void LoggingManager::CreateDefaultLogger(const std::string &logger_id) {
121121
// this method is only called from ctor in scope where DefaultLoggerMutex() is already locked
122122

123-
unique_ptr<Logger> &default_logger{GetDefaultLogger()};
123+
std::unique_ptr<Logger> &default_logger{GetDefaultLogger()};
124124

125125
if (default_logger != nullptr) {
126126
throw std::logic_error("Default logger already set. ");

lotus/core/common/logging/logging.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ using Timestamp = std::chrono::time_point<std::chrono::system_clock>;
5454
#ifdef _DEBUG
5555
static bool vlog_enabled = true; // Set directly based on your needs.
5656
#else
57-
static const bool vlog_enabled = false; // no VLOG output
57+
constexpr bool vlog_enabled = false; // no VLOG output
5858
#endif
5959

6060
enum class DataType {
@@ -233,7 +233,7 @@ class Logger {
233233

234234
inline const Logger &LoggingManager::DefaultLogger() {
235235
// fetch the container for the default logger once to void function calls in the future
236-
static unique_ptr<Logger> &default_logger = GetDefaultLogger();
236+
static std::unique_ptr<Logger> &default_logger = GetDefaultLogger();
237237

238238
if (default_logger == nullptr) {
239239
// fail early for attempted misuse. don't use logging macros as we have no logger.

lotus/core/common/status.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ bool Status::IsOK() const noexcept {
1919
}
2020

2121
StatusCategory Status::Category() const noexcept {
22-
return IsOK() ? StatusCategory::NONE : state_->category;
22+
return IsOK() ? Common::NONE : state_->category;
2323
}
2424

2525
int Status::Code() const noexcept {
26-
return IsOK() ? static_cast<int>(StatusCode::OK) : state_->code;
26+
return IsOK() ? static_cast<int>(Common::OK) : state_->code;
2727
}
2828

2929
const std::string& Status::ErrorMessage() const noexcept {
@@ -37,11 +37,11 @@ std::string Status::ToString() const {
3737

3838
std::string result;
3939

40-
if (StatusCategory::SYSTEM == state_->category) {
40+
if (Common::SYSTEM == state_->category) {
4141
result += "SystemError";
4242
result += " : ";
4343
result += std::to_string(errno);
44-
} else if (StatusCategory::LOTUS == state_->category) {
44+
} else if (Common::LOTUS == state_->category) {
4545
result += "[LotusError]";
4646
result += " : ";
4747
result += std::to_string(Code());

lotus/core/common/status.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class Status {
8383
State(StatusCategory cat0, int code0, const std::string& msg0)
8484
: category(cat0), code(code0), msg(msg0) {}
8585

86-
StatusCategory category = StatusCategory::NONE;
86+
StatusCategory category = Common::NONE;
8787
int code = 0;
8888
std::string msg;
8989
};

lotus/core/framework/allocation_planner.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "core/graph/utils.h"
1010
#include "core/framework/data_types.h"
1111
#include "core/framework/mldata_type_utils.h"
12-
12+
using namespace Lotus::Common;
1313
using namespace onnx;
1414
namespace Lotus {
1515

@@ -39,7 +39,7 @@ std::ostream& operator<<(std::ostream& out, std::pair<const SequentialExecutionP
3939
const SequentialExecutionPlan& plan = *planinfo.first;
4040
const SessionState& session_state = *planinfo.second;
4141
const LotusIR::Graph& graph = *session_state.GetGraph();
42-
std::unordered_map<int, string> index_to_name;
42+
std::unordered_map<int, std::string> index_to_name;
4343

4444
out << "Allocation Plan:\n";
4545
for (auto& name_index : session_state.GetMLValueIdxMap()) {

lotus/core/framework/bfc_arena.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ struct AllocatorStats {
5252
this->total_allocated_bytes_ = 0;
5353
}
5454

55-
string DebugString() const {
55+
std::string DebugString() const {
5656
std::stringstream ss;
5757
ss << "Limit: " << this->bytes_limit << "\n"
5858
<< "InUse: " << this->bytes_in_use << "\n"
@@ -160,8 +160,8 @@ class BFCArena : public IArenaAllocator {
160160

161161
bool in_use() const { return allocation_id != -1; }
162162

163-
string DebugString(BFCArena* a,
164-
bool recurse) {
163+
std::string DebugString(BFCArena* a,
164+
bool recurse) {
165165
std::stringstream ss;
166166
ss << " Size: " << size << " | Requested Size: " << requested_size << " | in_use: " << in_use();
167167
if (recurse && prev != BFCArena::kInvalidChunkHandle) {

lotus/core/framework/environment.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ Status Environment::Initialize() {
2525
// TODO: Should register microsoft domain kernels here.
2626

2727
} catch (std::exception& ex) {
28-
status = Status{LOTUS, StatusCode::RUNTIME_EXCEPTION, std::string{"Exception caught: "} + ex.what()};
28+
status = Status{LOTUS, Common::RUNTIME_EXCEPTION, std::string{"Exception caught: "} + ex.what()};
2929
} catch (...) {
30-
status = Status{LOTUS, StatusCode::RUNTIME_EXCEPTION};
30+
status = Status{LOTUS, Common::RUNTIME_EXCEPTION};
3131
}
3232

3333
return status;

lotus/core/framework/execution_frame.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#include "core/framework/op_kernel.h"
44
#include "core/framework/session_state.h"
55
#include "core/framework/mem_pattern_planner.h"
6-
6+
using namespace Lotus::Common;
77
namespace Lotus {
88

99
ExecutionFrame::ExecutionFrame(const std::unordered_map<std::string, MLValue>& feeds,
@@ -74,7 +74,7 @@ Status ExecutionFrame::AllocateMLValueTensorSelfOwnBufferHelper(int mlvalue_inde
7474

7575
int64_t len = shape.Size();
7676
if (len < 0) {
77-
return Status(StatusCategory::LOTUS, StatusCode::INVALID_ARGUMENT, "Tensor shape cannot contain any negative value");
77+
return Status(Common::LOTUS, Common::INVALID_ARGUMENT, "Tensor shape cannot contain any negative value");
7878
}
7979
len *= element_type->Size();
8080
//safety check for 32 bits systems
@@ -288,8 +288,8 @@ Common::Status ExecutionFrame::AllocateAsPerAllocationPlan(int mlvalue_index,
288288
}
289289

290290
void ExecutionFrame::Init(const LotusIR::Graph* graph,
291-
const std::unordered_map<string, MLValue>& feeds,
292-
const std::vector<string>& output_names,
291+
const std::unordered_map<std::string, MLValue>& feeds,
292+
const std::vector<std::string>& output_names,
293293
const std::vector<MLValue>& fetches) {
294294
LOTUS_ENFORCE(graph);
295295

lotus/core/framework/execution_frame.h

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include "core/framework/allocation_planner.h"
1212
#include "core/framework/ml_value_patterns_planner.h"
1313
#include "core/common/logging/logging.h"
14-
// #include "core/framework/session_state.h"
1514

1615
namespace Lotus {
1716

@@ -115,34 +114,37 @@ class ExecutionFrame {
115114
// Return nullptr if index map to an value that is an unused optional input/output
116115
template <typename T>
117116
const T* GetValue(int index) const {
118-
LOTUS_ENFORCE(index >= 0 && index < node_values_.size());
117+
LOTUS_ENFORCE(index >= 0 && static_cast<size_t>(index) < node_values_.size());
119118
return node_values_[index] >= 0 ? &all_values_[node_values_[index]].Get<T>() : nullptr;
120119
}
121120

122121
Fence_t GetFence(int index) const {
123-
LOTUS_ENFORCE(index >= 0 && index < node_values_.size());
122+
LOTUS_ENFORCE(index >= 0 && static_cast<size_t>(index) < node_values_.size());
124123
return node_values_[index] >= 0 ? all_values_[node_values_[index]].Fence() : nullptr;
125124
}
126125

127126
// Return nullptr if index map to an value that is an unused optional input/output
128127
MLDataType GetType(int index) const {
129-
LOTUS_ENFORCE(index >= 0 && index < node_values_.size());
128+
LOTUS_ENFORCE(index >= 0 && static_cast<size_t>(index) < node_values_.size());
130129
return node_values_[index] >= 0 ? all_values_[node_values_[index]].Type() : nullptr;
131130
}
132131

133132
// Return nullptr if index map to an value that is an unused optional input/output
134133
template <typename T>
135134
T* GetMutableValue(int index) {
136-
LOTUS_ENFORCE(index >= 0 && index < node_values_.size());
135+
LOTUS_ENFORCE(index >= 0 && static_cast<size_t>(index) < node_values_.size());
137136
return node_values_[index] >= 0 ? all_values_[node_values_[index]].GetMutable<T>() : nullptr;
138137
}
139138

140139
AllocatorPtr GetAllocator(const AllocatorInfo& info);
141140

142-
void ReleaseMLValue(int mlvalue_idx) {
143-
LOTUS_ENFORCE(mlvalue_idx >= 0 || mlvalue_idx < all_values_.size());
141+
Status ReleaseMLValue(int mlvalue_idx) {
142+
if (mlvalue_idx < 0 || mlvalue_idx >= all_values_.size()) {
143+
return LOTUS_MAKE_STATUS(LOTUS, INVALID_ARGUMENT, "invalid index ", mlvalue_idx);
144+
}
144145
all_values_[mlvalue_idx] = MLValue();
145146
TraceFree(mlvalue_idx);
147+
return Status::OK();
146148
}
147149

148150
const Lotus::SessionState& SessionState() const {
@@ -170,8 +172,8 @@ class ExecutionFrame {
170172
bool create_fence);
171173

172174
void Init(const LotusIR::Graph* graph,
173-
const std::unordered_map<string, MLValue>& feeds,
174-
const std::vector<string>& output_names,
175+
const std::unordered_map<std::string, MLValue>& feeds,
176+
const std::vector<std::string>& output_names,
175177
const std::vector<MLValue>& fetches);
176178

177179
void SetupNodeArg(const LotusIR::NodeArg* arg);
@@ -187,7 +189,7 @@ class ExecutionFrame {
187189
template <typename T>
188190
Status GetOrCreateMLValue(int index, const MLValueAllocationParameters& parameters, T*& value) {
189191
if (index < 0 || index >= node_values_.size()) {
190-
return Status(LOTUS, INVALID_ARGUMENT,
192+
return Status(Common::LOTUS, Common::INVALID_ARGUMENT,
191193
"Try to access with invalid node value index: " + std::to_string(index));
192194
}
193195

@@ -236,7 +238,7 @@ class ExecutionFrame {
236238
// i-th kernel is still waiting for pending_counts_[i] inputs.
237239
std::vector<int> pending_counts_; // not used currently
238240

239-
std::unordered_map<string, int> value_name_to_index_;
241+
std::unordered_map<std::string, int> value_name_to_index_;
240242

241243
const Lotus::SessionState& session_state_;
242244

0 commit comments

Comments
 (0)