Skip to content
This repository was archived by the owner on Dec 28, 2023. It is now read-only.

Commit 485e5b9

Browse files
author
Isaac Poulton
authored
Replace spdlog output with exception messages (#11)
* Replace spdlog output with exception messages * Move spdlog into example directory * Allow disabling of example and test executables
1 parent 6460f0a commit 485e5b9

File tree

12 files changed

+40
-55
lines changed

12 files changed

+40
-55
lines changed

.gitmodules

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[submodule "lib/spdlog"]
2-
path = lib/spdlog
2+
path = example/lib/spdlog
33
url = git@github.com:gabime/spdlog.git
44
[submodule "lib/msgpack-c"]
55
path = example/lib/msgpack-c

CMakeLists.txt

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ list(APPEND CPPCHECK_ARGS
2424
--suppressions-list=${CMAKE_CURRENT_LIST_DIR}/CppCheckSuppressions.txt
2525
-I ${CMAKE_CURRENT_LIST_DIR}/src
2626
-I ${CMAKE_CURRENT_LIST_DIR}/include
27-
-I ${CMAKE_CURRENT_LIST_DIR}/lib/spdlog/include
2827
-I ${CMAKE_CURRENT_LIST_DIR}/example
2928
${CMAKE_CURRENT_LIST_DIR}/src
3029
${CMAKE_CURRENT_LIST_DIR}/example
@@ -42,8 +41,6 @@ find_package(Torch REQUIRED)
4241
if (TORCH_CXX_FLAGS)
4342
set(CMAKE_CXX_FLAGS ${TORCH_CXX_FLAGS})
4443
endif()
45-
## Spdlog
46-
add_subdirectory(lib/spdlog)
4744

4845
# Define targets
4946
add_library(cpprl STATIC "")
@@ -64,18 +61,25 @@ endif(MSVC)
6461
set(CPPRL_INCLUDE_DIRS
6562
include
6663
src
67-
lib/spdlog/include
6864
${TORCH_INCLUDE_DIRS}
6965
)
7066
target_include_directories(cpprl PRIVATE ${CPPRL_INCLUDE_DIRS})
71-
target_include_directories(cpprl_tests PRIVATE ${CPPRL_INCLUDE_DIRS})
67+
if (CPPRL_BUILD_TESTS)
68+
target_include_directories(cpprl_tests PRIVATE ${CPPRL_INCLUDE_DIRS})
69+
endif(CPPRL_BUILD_TESTS)
7270

7371
# Linking
7472
target_link_libraries(cpprl torch ${TORCH_LIBRARIES})
75-
target_link_libraries(cpprl_tests torch ${TORCH_LIBRARIES})
73+
target_link_libraries(cpprl torch ${TORCH_LIBRARIES})
74+
if (CPPRL_BUILD_TESTS)
75+
target_link_libraries(cpprl_tests torch ${TORCH_LIBRARIES})
76+
endif(CPPRL_BUILD_TESTS)
7677

7778
# Example
78-
add_subdirectory(example)
79+
option(CPPRL_BUILD_EXAMPLE "Whether or not to build the CppRl Gym example" ON)
80+
if (CPPRL_BUILD_EXAMPLE)
81+
add_subdirectory(example)
82+
endif(CPPRL_BUILD_EXAMPLE)
7983

8084
# Recurse into source tree
8185
add_subdirectory(src)

example/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ add_executable(gym_client gym_client.cpp communicator.cpp)
33
set(LIB_DIR ${CMAKE_CURRENT_LIST_DIR}/lib)
44
set(CPPZMQ_DIR ${LIB_DIR}/cppzmq)
55
set(MSGPACK_DIR ${LIB_DIR}/msgpack-c)
6+
set(SPDLOG_DIR ${LIB_DIR}/spdlog)
67
set(ZMQ_DIR ${LIB_DIR}/libzmq)
78

9+
# Spdlog
10+
add_subdirectory(${SPDLOG_DIR})
811
# ZMQ
912
option(ZMQ_BUILD_TESTS "" OFF)
1013
add_subdirectory(${ZMQ_DIR})
@@ -16,6 +19,7 @@ target_include_directories(gym_client
1619
../lib/spdlog/include
1720
${CPPZMQ_DIR}
1821
${MSGPACK_DIR}/include
22+
${SPDLOG_DIR}/include
1923
${ZMQ_DIR}/include
2024
)
2125

src/algorithms/ppo.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include <chrono>
22
#include <memory>
33

4-
#include <spdlog/spdlog.h>
54
#include <torch/torch.h>
65

76
#include "cpprl/algorithms/ppo.h"

src/distributions/bernoulli.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include <ATen/core/Reduction.h>
22
#include <c10/util/ArrayRef.h>
3-
#include <spdlog/spdlog.h>
43
#include <torch/torch.h>
54

65
#include "cpprl/distributions/bernoulli.h"
@@ -13,15 +12,14 @@ Bernoulli::Bernoulli(const torch::Tensor *probs,
1312
{
1413
if ((probs == nullptr) == (logits == nullptr))
1514
{
16-
spdlog::error("Either probs or logits is required, but not both");
17-
throw std::exception();
15+
throw std::runtime_error("Either probs or logits is required, but not both");
1816
}
1917

2018
if (probs != nullptr)
2119
{
2220
if (probs->dim() < 1)
2321
{
24-
throw std::exception();
22+
throw std::runtime_error("Probabilities tensor must have at least one dimension");
2523
}
2624
this->probs = *probs;
2725
// 1.21e-7 is used as the epsilon to match PyTorch's Python results as closely
@@ -33,7 +31,7 @@ Bernoulli::Bernoulli(const torch::Tensor *probs,
3331
{
3432
if (logits->dim() < 1)
3533
{
36-
throw std::exception();
34+
throw std::runtime_error("Logits tensor must have at least one dimension");
3735
}
3836
this->logits = *logits;
3937
this->probs = torch::sigmoid(*logits);

src/distributions/categorical.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include <c10/util/ArrayRef.h>
2-
#include <spdlog/spdlog.h>
32
#include <torch/torch.h>
43

54
#include "cpprl/distributions/categorical.h"
@@ -12,15 +11,14 @@ Categorical::Categorical(const torch::Tensor *probs,
1211
{
1312
if ((probs == nullptr) == (logits == nullptr))
1413
{
15-
spdlog::error("Either probs or logits is required, but not both");
16-
throw std::exception();
14+
throw std::runtime_error("Either probs or logits is required, but not both");
1715
}
1816

1917
if (probs != nullptr)
2018
{
2119
if (probs->dim() < 1)
2220
{
23-
throw std::exception();
21+
throw std::runtime_error("Probabilities tensor must have at least one dimension");
2422
}
2523
this->probs = *probs / probs->sum(-1, true);
2624
// 1.21e-7 is used as the epsilon to match PyTorch's Python results as closely
@@ -32,7 +30,7 @@ Categorical::Categorical(const torch::Tensor *probs,
3230
{
3331
if (logits->dim() < 1)
3432
{
35-
throw std::exception();
33+
throw std::runtime_error("Logits tensor must have at least one dimension");
3634
}
3735
this->logits = *logits - logits->logsumexp(-1, true);
3836
this->probs = torch::softmax(this->logits, -1);

src/generators/feed_forward_generator.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include <algorithm>
22
#include <vector>
33

4-
#include <spdlog/spdlog.h>
54
#include <torch/torch.h>
65

76
#include "cpprl/generators/feed_forward_generator.h"
@@ -44,10 +43,7 @@ MiniBatch FeedForwardGenerator::next()
4443
{
4544
if (index >= indices.size(0))
4645
{
47-
spdlog::error("No minibatches left in generator. Index {}, minibatch "
48-
"count: {}.",
49-
index, indices.size(0));
50-
throw std::exception();
46+
throw std::runtime_error("No minibatches left in generator.");
5147
}
5248

5349
MiniBatch mini_batch;

src/generators/recurrent_generator.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#include <algorithm>
22
#include <vector>
33

4-
#include <spdlog/spdlog.h>
54
#include <torch/torch.h>
65

76
#include "cpprl/generators/recurrent_generator.h"
@@ -49,10 +48,7 @@ MiniBatch RecurrentGenerator::next()
4948
{
5049
if (index >= indices.size(0))
5150
{
52-
spdlog::error("No minibatches left in generator. Index {}, minibatch "
53-
"count: {}.",
54-
index, indices.size(0));
55-
throw std::exception();
51+
throw std::runtime_error("No minibatches left in generator.");
5652
}
5753

5854
MiniBatch mini_batch;

src/model/policy.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#include <spdlog/spdlog.h>
21
#include <torch/torch.h>
32

43
#include "cpprl/model/policy.h"
@@ -35,8 +34,7 @@ PolicyImpl::PolicyImpl(ActionSpace action_space, std::shared_ptr<NNBase> base)
3534
}
3635
else
3736
{
38-
spdlog::error("Action space {} not supported", action_space.type);
39-
throw std::exception();
37+
throw std::runtime_error("Action space " + action_space.type + " not supported");
4038
}
4139
register_module("output", output_layer);
4240
}

0 commit comments

Comments
 (0)