Skip to content
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

Clean up exceptions #229

Merged
merged 4 commits into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ark/api/data_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include "bfloat16.h"
#include "half.h"
#include "logging.h"
#include "logging.hpp"
#include "model/model_data_type.hpp"

namespace ark {
Expand Down Expand Up @@ -50,7 +50,7 @@
}
auto it = instances.find(type_name);
if (it == instances.end()) {
ERR(InvalidUsageError, "Unknown data type: ", type_name);
ERR(UnsupportedError, "Unknown data type: ", type_name);

Check warning on line 53 in ark/api/data_type.cpp

View check run for this annotation

Codecov / codecov/patch

ark/api/data_type.cpp#L53

Added line #L53 was not covered by tests
}
return *(it->second);
}
Expand Down
3 changes: 1 addition & 2 deletions ark/api/dims.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@

#include <vector>

#include "error.hpp"
#include "logging.h"
#include "logging.hpp"

namespace ark {

Expand Down
29 changes: 29 additions & 0 deletions ark/api/error_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

#include "ark/error.hpp"

#include "unittest/unittest_utils.h"

ark::unittest::State test_error() {
UNITTEST_THROW(throw ark::ModelError("test"), ark::ModelError);

try {
throw ark::ModelError("test");
} catch (const ark::ModelError &e) {
UNITTEST_EQ(std::string(e.what()), "test");
}

try {
throw ark::ModelError("test");
} catch (const ark::BaseError &e) {
UNITTEST_EQ(std::string(e.what()), "test");
}

return ark::unittest::SUCCESS;
}

int main() {
UNITTEST(test_error);
return 0;
}
2 changes: 1 addition & 1 deletion ark/api/executor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#include "gpu/gpu_kernel.h"
#include "gpu/gpu_logging.h"
#include "gpu/gpu_manager.h"
#include "logging.h"
#include "logging.hpp"
#include "model/model_buffer.hpp"
#include "model/model_data_type.hpp"
#include "model/model_tensor.hpp"
Expand Down
4 changes: 1 addition & 3 deletions ark/api/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@

#include "env.h"
#include "file_io.h"
#include "logging.h"

#define SHM_DIR "/dev/shm/"
#include "logging.hpp"

namespace ark {

Expand Down
2 changes: 1 addition & 1 deletion ark/api/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include <limits>

#include "logging.h"
#include "logging.hpp"

namespace ark {

Expand Down
2 changes: 1 addition & 1 deletion ark/api/model_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include "ark/model_graph.hpp"

#include "logging.h"
#include "logging.hpp"
#include "model/model_graph_impl.hpp"
#include "model/model_node.hpp"

Expand Down
2 changes: 1 addition & 1 deletion ark/api/model_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include <algorithm>

#include "logging.h"
#include "logging.hpp"
#include "model/model_node.hpp"
#include "model/model_op.hpp"
#include "unittest/unittest_utils.h"
Expand Down
3 changes: 1 addition & 2 deletions ark/api/planner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@
static void check_config_field(const ModelOpRef op, const Json &config,
const std::string &field) {
if (!config.contains(field)) {
ERR(NotFoundError, "Config field not found: ", field, " in ",
op->name());
ERR(PlanError, "Config field not found: ", field, " in ", op->name());

Check warning on line 51 in ark/api/planner.cpp

View check run for this annotation

Codecov / codecov/patch

ark/api/planner.cpp#L51

Added line #L51 was not covered by tests
}
}

Expand Down
4 changes: 2 additions & 2 deletions ark/arch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include <map>

#include "logging.h"
#include "logging.hpp"

namespace ark {

Expand Down Expand Up @@ -85,7 +85,7 @@
}
auto it = instances.find(type_name);
if (it == instances.end()) {
ERR(InvalidUsageError, "Unknown architecture type: ", type_name);
ERR(UnsupportedError, "Unknown architecture type: ", type_name);

Check warning on line 88 in ark/arch.cpp

View check run for this annotation

Codecov / codecov/patch

ark/arch.cpp#L88

Added line #L88 was not covered by tests
}
return it->second;
}
Expand Down
8 changes: 4 additions & 4 deletions ark/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "ark/data_type.hpp"
#include "env.h"
#include "file_io.h"
#include "logging.h"
#include "logging.hpp"
#include "model/model_buffer.hpp"
#include "model/model_data_type.hpp"
#include "model/model_op.hpp"
Expand Down Expand Up @@ -166,7 +166,7 @@
const std::string &template_path =
ark_root + "/include/kernels/kernel_template.in";
if (!is_file(template_path)) {
ERR(SchedulerError, "kernel template file not found: ", template_path);
ERR(InternalError, "kernel template file not found: ", template_path);

Check warning on line 169 in ark/codegen.cpp

View check run for this annotation

Codecov / codecov/patch

ark/codegen.cpp#L169

Added line #L169 was not covered by tests
}
std::string template_code = read_file(template_path);
std::map<std::string, std::string> replacements = {
Expand Down Expand Up @@ -276,7 +276,7 @@
rg_json["ProcessorRange"][1]);
if (*rg_proc_range.begin() < *proc_range.begin() ||
*rg_proc_range.end() > *proc_range.end()) {
ERR(SchedulerError, "invalid processor range of resource group");
ERR(PlanError, "invalid processor range of resource group");

Check warning on line 279 in ark/codegen.cpp

View check run for this annotation

Codecov / codecov/patch

ark/codegen.cpp#L279

Added line #L279 was not covered by tests
}
Range<size_t> rg_warp_range(rg_json["WarpRange"][0],
rg_json["WarpRange"][1]);
Expand Down Expand Up @@ -305,7 +305,7 @@
n_slots = total_warps / num_warps_per_task;
}
if (n_slots == 0) {
ERR(SchedulerError, "not enough resources for task group");
ERR(PlanError, "not enough resources for task group");

Check warning on line 308 in ark/codegen.cpp

View check run for this annotation

Codecov / codecov/patch

ark/codegen.cpp#L308

Added line #L308 was not covered by tests
}

size_t task_b = *task_range.begin();
Expand Down
36 changes: 0 additions & 36 deletions ark/error.hpp

This file was deleted.

2 changes: 1 addition & 1 deletion ark/file_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <fstream>
#include <sstream>

#include "logging.h"
#include "logging.hpp"

namespace fs = std::filesystem;

Expand Down
6 changes: 3 additions & 3 deletions ark/gpu/gpu_compile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
const std::string &output_file_path) {
#if defined(ARK_CUDA)
if (!arch->belongs_to(ARCH_CUDA)) {
ERR(InvalidUsageError, "Invalid architecture: ", arch->name());
ERR(UnsupportedError, "Invalid architecture: ", arch->name());

Check warning on line 81 in ark/gpu/gpu_compile.cpp

View check run for this annotation

Codecov / codecov/patch

ark/gpu/gpu_compile.cpp#L81

Added line #L81 was not covered by tests
}
const std::string &cc = arch->category()[1];

Expand Down Expand Up @@ -107,7 +107,7 @@
#elif defined(ARK_ROCM)
// Remove prepending "rocm_" from arch to get the compute capability
if (!arch->belongs_to(ARCH_ROCM)) {
ERR(InvalidUsageError, "Invalid architecture: ", arch->name());
ERR(UnsupportedError, "Invalid architecture: ", arch->name());
}
const std::string &cc = to_lower(arch->category()[1]);

Expand Down Expand Up @@ -195,7 +195,7 @@
}
std::string exec_print_str = exec_print.str();
if (exec_print_str.size() > 0) {
ERR(ExecutorError, "\n", compile_cmd, "\n", exec_print_str,
ERR(InternalError, "\n", compile_cmd, "\n", exec_print_str,

Check warning on line 198 in ark/gpu/gpu_compile.cpp

View check run for this annotation

Codecov / codecov/patch

ark/gpu/gpu_compile.cpp#L198

Added line #L198 was not covered by tests
"\n");
}
LOG(INFO, "Compile succeed: ", code_file_path, " (",
Expand Down
2 changes: 1 addition & 1 deletion ark/gpu/gpu_logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#define ARK_GPU_LOGGING_H_

#include "gpu/gpu.h"
#include "logging.h"
#include "logging.hpp"

#define GLOG(cmd) \
do { \
Expand Down
6 changes: 4 additions & 2 deletions ark/gpu/gpu_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@ GpuManager::Impl::Impl(int gpu_id) : gpu_id_(gpu_id) {
// E.g.: "gfx90a:sramecc+:xnack-"
std::string gcn_arch_name = prop.gcnArchName;
if (gcn_arch_name.substr(0, 3) != "gfx") {
ERR(ExecutorError, "unexpected GCN architecture name: ", gcn_arch_name);
ERR(UnsupportedError,
"unexpected GCN architecture name: ", gcn_arch_name);
}
size_t pos_e = gcn_arch_name.find(":");
if (pos_e == std::string::npos) {
ERR(ExecutorError, "unexpected GCN architecture name: ", gcn_arch_name);
ERR(UnsupportedError,
"unexpected GCN architecture name: ", gcn_arch_name);
}
// E.g.: "ROCM_90A"
auto arch_name = "ROCM_" + to_upper(gcn_arch_name.substr(3, pos_e - 3));
Expand Down
3 changes: 1 addition & 2 deletions ark/gpu/gpu_memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@
if (align_ == 0) {
align_ = 1;
} else if (align_ & (align_ - 1)) {
ERR(InvalidUsageError, "align must be a power of 2. Given %zu.",
align_);
ERR(InternalError, "align must be a power of 2. Given %zu.", align_);

Check warning on line 44 in ark/gpu/gpu_memory.cpp

View check run for this annotation

Codecov / codecov/patch

ark/gpu/gpu_memory.cpp#L44

Added line #L44 was not covered by tests
}
manager_.set_current();
bytes_raw_ = bytes_ + align_ - 1;
Expand Down
1 change: 1 addition & 0 deletions ark/include/ark.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include <ark/data_type.hpp>
#include <ark/dims.hpp>
#include <ark/error.hpp>
#include <ark/executor.hpp>
#include <ark/init.hpp>
#include <ark/model_graph.hpp>
Expand Down
47 changes: 47 additions & 0 deletions ark/include/ark/error.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

#ifndef ARK_ERROR_HPP
#define ARK_ERROR_HPP

#include <stdexcept>
#include <string>

namespace ark {

/// Base class for all ARK errors.
class BaseError : public std::exception {
private:
std::string msg_;

public:
BaseError(const std::string &msg) : msg_(msg) {}
const char *what() const noexcept override { return msg_.c_str(); }
};

#define REGISTER_ERROR_TYPE(_name) \
class _name : public BaseError { \
public: \
_name(const std::string &msg) : BaseError(msg) {} \
};

/// Internal error in ARK, likely a bug.
REGISTER_ERROR_TYPE(InternalError)

Check warning on line 29 in ark/include/ark/error.hpp

View check run for this annotation

Codecov / codecov/patch

ark/include/ark/error.hpp#L29

Added line #L29 was not covered by tests
/// Invalid usage of ARK API.
REGISTER_ERROR_TYPE(InvalidUsageError)
/// Invalid ARK model definition or usage.
REGISTER_ERROR_TYPE(ModelError)
/// Invalid ARK plan definition or usage.
REGISTER_ERROR_TYPE(PlanError)

Check warning on line 35 in ark/include/ark/error.hpp

View check run for this annotation

Codecov / codecov/patch

ark/include/ark/error.hpp#L35

Added line #L35 was not covered by tests
/// Unsupported feature triggered.
REGISTER_ERROR_TYPE(UnsupportedError)

Check warning on line 37 in ark/include/ark/error.hpp

View check run for this annotation

Codecov / codecov/patch

ark/include/ark/error.hpp#L37

Added line #L37 was not covered by tests
/// Error from invalid system state such as a system call failure.
REGISTER_ERROR_TYPE(SystemError)
/// Error from a CUDA/HIP API call.
REGISTER_ERROR_TYPE(GpuError)

Check warning on line 41 in ark/include/ark/error.hpp

View check run for this annotation

Codecov / codecov/patch

ark/include/ark/error.hpp#L41

Added line #L41 was not covered by tests
/// Error from a unit test.
REGISTER_ERROR_TYPE(UnitTestError)

Check warning on line 43 in ark/include/ark/error.hpp

View check run for this annotation

Codecov / codecov/patch

ark/include/ark/error.hpp#L43

Added line #L43 was not covered by tests

} // namespace ark

#endif // ARK_ERROR_HPP
2 changes: 1 addition & 1 deletion ark/logging.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

#include "logging.h"
#include "logging.hpp"

#include <unistd.h>

Expand Down
8 changes: 4 additions & 4 deletions ark/logging.h → ark/logging.hpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

#ifndef ARK_LOGGING_H_
#define ARK_LOGGING_H_
#ifndef ARK_LOGGING_HPP_
#define ARK_LOGGING_HPP_

#include <iostream>
#include <sstream>
#include <string>

#include "error.hpp"
#include "ark/error.hpp"

namespace ark {

Expand Down Expand Up @@ -94,4 +94,4 @@ inline void _err(const std::string &file, int line, T value, Args... args) {

} // namespace ark

#endif // ARK_LOGGING_H_
#endif // ARK_LOGGING_HPP_
14 changes: 5 additions & 9 deletions ark/model/model_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include "model_buffer.hpp"

#include "logging.h"
#include "logging.hpp"

namespace ark {

Expand Down Expand Up @@ -51,17 +51,13 @@

std::shared_ptr<ModelBuffer> ModelBuffer::deserialize(const Json &serialized) {
if (!serialized.contains("Id")) {
ERR(InvalidUsageError,
"ModelBuffer deserialization failed: missing Id");
ERR(ModelError, "ModelBuffer deserialization failed: missing Id");

Check warning on line 54 in ark/model/model_buffer.cpp

View check run for this annotation

Codecov / codecov/patch

ark/model/model_buffer.cpp#L54

Added line #L54 was not covered by tests
} else if (!serialized.contains("Rank")) {
ERR(InvalidUsageError,
"ModelBuffer deserialization failed: missing Rank");
ERR(ModelError, "ModelBuffer deserialization failed: missing Rank");

Check warning on line 56 in ark/model/model_buffer.cpp

View check run for this annotation

Codecov / codecov/patch

ark/model/model_buffer.cpp#L56

Added line #L56 was not covered by tests
} else if (!serialized.contains("SendTags")) {
ERR(InvalidUsageError,
"ModelBuffer deserialization failed: missing SendTags");
ERR(ModelError, "ModelBuffer deserialization failed: missing SendTags");

Check warning on line 58 in ark/model/model_buffer.cpp

View check run for this annotation

Codecov / codecov/patch

ark/model/model_buffer.cpp#L58

Added line #L58 was not covered by tests
} else if (!serialized.contains("RecvTags")) {
ERR(InvalidUsageError,
"ModelBuffer deserialization failed: missing RecvTags");
ERR(ModelError, "ModelBuffer deserialization failed: missing RecvTags");

Check warning on line 60 in ark/model/model_buffer.cpp

View check run for this annotation

Codecov / codecov/patch

ark/model/model_buffer.cpp#L60

Added line #L60 was not covered by tests
}
return std::make_shared<ModelBuffer>(serialized["Id"], serialized["Rank"],
serialized["SendTags"],
Expand Down
Loading
Loading