Commit 5fea4bba authored by Paul's avatar Paul
Browse files

Add custom exception

parent 10aff10b
......@@ -2,6 +2,7 @@
#define RTG_GUARD_BUILTIN_HPP
#include <rtg/operand.hpp>
#include <rtg/errors.hpp>
namespace rtg {
......@@ -10,16 +11,16 @@ namespace builtin {
struct literal
{
std::string name() const { return "@literal"; }
shape compute_shape(std::vector<shape>) const { throw "builtin"; }
argument compute(std::vector<argument>) const { throw "builtin"; }
shape compute_shape(std::vector<shape>) const { RTG_THROW("builtin"); }
argument compute(std::vector<argument>) const { RTG_THROW("builtin"); }
};
struct param
{
std::string parameter;
std::string name() const { return "@param:" + parameter; }
shape compute_shape(std::vector<shape>) const { throw "builtin"; }
argument compute(std::vector<argument>) const { throw "builtin"; }
shape compute_shape(std::vector<shape>) const { RTG_THROW("builtin"); }
argument compute(std::vector<argument>) const { RTG_THROW("builtin"); }
};
} // namespace builtin
......
#ifndef RTG_GUARD_ERRORS_HPP
#define RTG_GUARD_ERRORS_HPP
#include <exception>
#include <stdexcept>
#include <string>
namespace rtg {
struct exception : std::runtime_error
{
exception(std::string msg = "")
: std::runtime_error(msg)
{}
};
inline exception make_exception(std::string context, std::string message = "")
{
return {context + ": " + message};
}
inline std::string make_source_context(const std::string& file, int line)
{
return file + ":" + std::to_string(line);
}
#define RTG_THROW(...) \
throw rtg::make_exception(rtg::make_source_context(__FILE__, __LINE__), __VA_ARGS__)
} // namespace rtg
#endif
......@@ -9,7 +9,7 @@ namespace rtg {
struct not_computable
{
argument compute(std::vector<argument>) const { throw std::runtime_error("not computable"); }
argument compute(std::vector<argument>) const { RTG_THROW("not computable"); }
};
struct convolution
......@@ -25,15 +25,15 @@ struct convolution
shape compute_shape(std::vector<shape> inputs) const
{
if(inputs.size() != 2)
throw std::runtime_error("Wrong number of arguments");
RTG_THROW("Wrong number of arguments");
const shape& input = inputs.at(0);
const shape& weights = inputs.at(1);
if(input.type() != weights.type())
throw std::runtime_error("Type doesn't match");
RTG_THROW("Type doesn't match");
if(input.lens().size() != weights.lens().size())
throw std::runtime_error("Dimensions don't match");
RTG_THROW("Dimensions don't match");
if(input.lens().size() != 4)
throw std::runtime_error("Only 4d convolution supported");
RTG_THROW("Only 4d convolution supported");
auto t = input.type();
return {t,
......@@ -55,7 +55,7 @@ struct convolution
}};
}
argument compute(std::vector<argument>) const { throw std::runtime_error("not computable"); }
argument compute(std::vector<argument>) const { RTG_THROW("not computable"); }
};
struct pooling
......@@ -72,10 +72,10 @@ struct pooling
shape compute_shape(std::vector<shape> inputs) const
{
if(inputs.empty())
throw std::runtime_error("Wrong number of arguments");
RTG_THROW("Wrong number of arguments");
const shape& input = inputs.at(0);
if(input.lens().size() != 4)
throw std::runtime_error("Only 4d pooling supported");
RTG_THROW("Only 4d pooling supported");
auto t = input.type();
return {t,
......@@ -95,7 +95,7 @@ struct pooling
}};
}
argument compute(std::vector<argument>) const { throw std::runtime_error("not computable"); }
argument compute(std::vector<argument>) const { RTG_THROW("not computable"); }
};
struct activation
......@@ -105,11 +105,11 @@ struct activation
shape compute_shape(std::vector<shape> inputs) const
{
if(inputs.empty())
throw std::runtime_error("Wrong number of arguments");
RTG_THROW("Wrong number of arguments");
return inputs.front();
}
argument compute(std::vector<argument>) const { throw std::runtime_error("not computable"); }
argument compute(std::vector<argument>) const { RTG_THROW("not computable"); }
};
struct reshape
......@@ -119,7 +119,7 @@ struct reshape
shape compute_shape(std::vector<shape> inputs) const
{
if(inputs.empty())
throw std::runtime_error("Wrong number of arguments");
RTG_THROW("Wrong number of arguments");
auto&& idims = inputs.front().lens();
std::vector<std::size_t> rdims(dims.begin(), dims.end());
for(std::size_t i = 0; i < dims.size(); i++)
......@@ -135,7 +135,7 @@ struct reshape
return {inputs.front().type(), rdims};
}
argument compute(std::vector<argument>) const { throw std::runtime_error("not computable"); }
argument compute(std::vector<argument>) const { RTG_THROW("not computable"); }
};
} // namespace rtg
......
......@@ -5,6 +5,8 @@
#include <cassert>
#include <ostream>
#include <rtg/errors.hpp>
namespace rtg {
struct shape
......@@ -115,7 +117,7 @@ struct shape
RTG_SHAPE_VISIT_TYPES(RTG_SHAPE_VISITOR_CASE)
#undef RTG_SHAPE_VISITOR_CASE
}
throw std::runtime_error("Unknown type");
RTG_THROW("Unknown type");
}
private:
......
......@@ -96,7 +96,7 @@ std::string shape::type_string() const
RTG_SHAPE_VISIT_TYPES(RTG_SHAPE_TYPE_STRING_CASE)
#undef RTG_SHAPE_TYPE_STRING_CASE
}
throw "Invalid type";
RTG_THROW("Invalid type");
}
bool operator==(const shape& x, const shape& y)
......
......@@ -11,13 +11,13 @@ struct sum_op
{
rtg::argument result;
if(args.size() != 2)
throw "Wrong args";
RTG_THROW("Wrong args");
if(args[0].get_shape() != args[1].get_shape())
throw "Wrong args";
RTG_THROW("Wrong args");
if(args[0].get_shape().lens().size() != 1)
throw "Wrong args";
RTG_THROW("Wrong args");
if(args[0].get_shape().lens().front() != 1)
throw "Wrong args";
RTG_THROW("Wrong args");
args[0].visit_at([&](auto x) {
args[1].visit_at([&](auto y) { result = rtg::literal{x + y}.get_argument(); });
......@@ -28,7 +28,7 @@ struct sum_op
rtg::shape compute_shape(std::vector<rtg::shape> inputs) const
{
if(inputs.size() != 2)
throw "Wrong inputs";
RTG_THROW("Wrong inputs");
return inputs.front();
}
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment