Skip to content

Commit 30b5c6f

Browse files
authored
0.2.0 release (#21)
* 0.2.0 release * Run clang-format.
1 parent 31abb37 commit 30b5c6f

42 files changed

Lines changed: 749 additions & 795 deletions

Some content is hidden

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

docs/installation.rst

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@ TorchFort can be installed in multiple ways but we highly recommend building and
77
Docker Installation
88
-------------------
99

10-
We provide a ``Dockerfile`` which contains all relevant dependencies and builds using the `NVIDIA HPC SDK <https://developer.nvidia.com/hpc-sdk>`_ software libraries and compilers, which is our recommended way to build TorchFort. In order to build TorchFort using Docker, simply clone the repo and call:
10+
We provide a `Dockerfile <https://github.com/NVIDIA/TorchFort/blob/master/docker/Dockerfile>`_ which contains all relevant dependencies and builds using the `NVIDIA HPC SDK <https://developer.nvidia.com/hpc-sdk>`_ software libraries and compilers, which is our recommended way to build TorchFort. In order to build TorchFort using Docker, simply clone the repo and call:
1111

1212
.. code-block:: bash
1313
1414
docker build -t torchfort:latest -f docker/Dockerfile .
1515
1616
from the top level directory of the repo. Inside the container, TorchFort will be installed in ``/opt/torchfort``.
1717

18-
We provide an alternative docker file ``Dockerfile_gnu`` which can be used to build TorchFort using GNU compilers.
18+
We provide an alternative docker file `Dockerfile_gnu <https://github.com/NVIDIA/TorchFort/blob/master/docker/Dockerfile_gnu>`_ which can be used to build TorchFort using GNU compilers. Additionally, we provide a docker file `Dockerfile_gnu_cpuonly <https://github.com/NVIDIA/TorchFort/blob/master/docker/Dockerfile_gnu_cpuonly>`_ which can be used to build TorchFort using GNU compilers without GPU support enabled.
1919

2020
CMake Installation
2121
------------------
2222

23-
For a native installation TorchFort provides a ``CMakeList.txt`` file. Please make sure that the following required packages are installed on your system before installing TorchFort:
23+
For a native installation TorchFort provides a `CMakeList.txt <https://github.com/NVIDIA/TorchFort/blob/master/CMakeLists.txt>`_ file. Please make sure that the following required packages are installed on your system before installing TorchFort:
2424

2525
* Requirements for core functionality and examples:
2626

@@ -31,14 +31,16 @@ For a native installation TorchFort provides a ``CMakeList.txt`` file. Please ma
3131
- MPI
3232
- NVIDIA Collective Communication Library (``NCCL``)
3333
- ``HDF5``
34-
- the Python modules specified in ``requirements.txt``
34+
- the Python modules specified in `requirements.txt <https://github.com/NVIDIA/TorchFort/blob/master/requirements.txt>`_
3535
- GNU or `NVHPC <https://developer.nvidia.com/hpc-sdk>`_ compilers. NVHPC compilers are **required** if CUDA Fortran device array support is desired.
3636

37-
3837
* Additional requirements for building this documentation:
3938

4039
- Doxygen
41-
- the Python modules specified in ``docs/requirements.txt``
40+
- the Python modules specified in `docs/requirements.txt <https://github.com/NVIDIA/TorchFort/blob/master/docs/requirements.txt>`_
41+
42+
For CPU-only builds, CUDA and NCCL are not required.
43+
4244

4345
To build TorchFort, clone the repo then call the following from the root directory:
4446

@@ -52,7 +54,7 @@ To build TorchFort, clone the repo then call the following from the root directo
5254
..
5355
make -j install
5456
55-
See the top level ``CMakeLists.txt`` file for additional CMake configuration options.
57+
See the top level `CMakeList.txt <https://github.com/NVIDIA/TorchFort/blob/master/CMakeLists.txt>`_ file for additional CMake configuration options.
5658

5759
Build Documentation
5860
-------------------

examples/cpp/cart_pole/train.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
} \
5151
} while (false)
5252

53-
5453
int main(int argc, char* argv[]) {
5554

5655
// load config file
@@ -94,11 +93,10 @@ int main(int argc, char* argv[]) {
9493

9594
// instantiate torchfort
9695
#if ENABLE_GPU
97-
CHECK_TORCHFORT(torchfort_rl_off_policy_create_system("td3_system", "config.yaml",
98-
0, TORCHFORT_DEVICE_CPU));
96+
CHECK_TORCHFORT(torchfort_rl_off_policy_create_system("td3_system", "config.yaml", 0, TORCHFORT_DEVICE_CPU));
9997
#else
100-
CHECK_TORCHFORT(torchfort_rl_off_policy_create_system("td3_system", "config.yaml",
101-
TORCHFORT_DEVICE_CPU, TORCHFORT_DEVICE_CPU));
98+
CHECK_TORCHFORT(
99+
torchfort_rl_off_policy_create_system("td3_system", "config.yaml", TORCHFORT_DEVICE_CPU, TORCHFORT_DEVICE_CPU));
102100
#endif
103101

104102
// define variables
@@ -167,7 +165,7 @@ int main(int argc, char* argv[]) {
167165
daction, 2, action_shape_batch.data(), TORCHFORT_FLOAT,
168166
0));
169167

170-
// copy data to host
168+
// copy data to host
171169
#ifdef ENABLE_GPU
172170
cudaMemcpy(action.data(), daction, action.size() * sizeof(float), cudaMemcpyDeviceToHost);
173171
#else

src/csrc/distributed.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,12 @@ void Comm::initialize(bool initialize_nccl) {
103103

104104
void Comm::finalize() {
105105
#ifdef ENABLE_GPU
106-
if (nccl_comm) CHECK_NCCL(ncclCommDestroy(nccl_comm));
107-
if (stream) CHECK_CUDA(cudaStreamDestroy(stream));
108-
if (event) CHECK_CUDA(cudaEventDestroy(event));
106+
if (nccl_comm)
107+
CHECK_NCCL(ncclCommDestroy(nccl_comm));
108+
if (stream)
109+
CHECK_CUDA(cudaStreamDestroy(stream));
110+
if (event)
111+
CHECK_CUDA(cudaEventDestroy(event));
109112
#endif
110113
}
111114

@@ -124,8 +127,8 @@ void Comm::allreduce(torch::Tensor& tensor, bool average) const {
124127
} else {
125128
nccl_dtype = get_nccl_dtype(tensor);
126129
}
127-
CHECK_NCCL(ncclAllReduce(tensor.data_ptr(), tensor.data_ptr(), count, nccl_dtype,
128-
(average) ? ncclAvg : ncclSum, nccl_comm, stream));
130+
CHECK_NCCL(ncclAllReduce(tensor.data_ptr(), tensor.data_ptr(), count, nccl_dtype, (average) ? ncclAvg : ncclSum,
131+
nccl_comm, stream));
129132

130133
CHECK_CUDA(cudaEventRecord(event, stream));
131134
CHECK_CUDA(cudaStreamWaitEvent(torch_stream, event));
@@ -139,8 +142,7 @@ void Comm::allreduce(torch::Tensor& tensor, bool average) const {
139142
} else {
140143
mpi_dtype = get_mpi_dtype(tensor);
141144
}
142-
CHECK_MPI(MPI_Allreduce(MPI_IN_PLACE, tensor.data_ptr(), count, mpi_dtype,
143-
MPI_SUM, mpi_comm));
145+
CHECK_MPI(MPI_Allreduce(MPI_IN_PLACE, tensor.data_ptr(), count, mpi_dtype, MPI_SUM, mpi_comm));
144146

145147
if (average) {
146148
tensor /= size;
@@ -205,8 +207,7 @@ void Comm::broadcast(torch::Tensor& tensor, int root) const {
205207
CHECK_CUDA(cudaEventRecord(event, torch_stream));
206208
CHECK_CUDA(cudaStreamWaitEvent(stream, event));
207209

208-
CHECK_NCCL(
209-
ncclBroadcast(tensor.data_ptr(), tensor.data_ptr(), count, nccl_dtype, root, nccl_comm, stream));
210+
CHECK_NCCL(ncclBroadcast(tensor.data_ptr(), tensor.data_ptr(), count, nccl_dtype, root, nccl_comm, stream));
210211

211212
CHECK_CUDA(cudaEventRecord(event, stream));
212213
CHECK_CUDA(cudaStreamWaitEvent(torch_stream, event));

src/csrc/include/internal/distributed.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ struct Comm {
5959
#endif
6060
bool initialized = false;
6161

62-
Comm(MPI_Comm mpi_comm) : mpi_comm(mpi_comm) {};
62+
Comm(MPI_Comm mpi_comm) : mpi_comm(mpi_comm){};
6363
};
6464

6565
} // namespace torchfort

src/csrc/include/internal/lr_schedulers.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ class StepLR : public BaseLRScheduler {
8585

8686
class LinearLR : public BaseLRScheduler {
8787
public:
88-
LinearLR(torch::optim::Optimizer& optimizer, const unsigned total_iters, const double start_factor=0.333, const double end_factor = 1.0);
88+
LinearLR(torch::optim::Optimizer& optimizer, const unsigned total_iters, const double start_factor = 0.333,
89+
const double end_factor = 1.0);
8990

9091
private:
9192
std::vector<double> get_lrs() override;

src/csrc/include/internal/models.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ struct MLPModel : BaseModel, public std::enable_shared_from_this<BaseModel> {
5353
std::vector<torch::Tensor> biases;
5454
};
5555

56-
5756
struct SACMLPModel : BaseModel, public std::enable_shared_from_this<BaseModel> {
5857
void setup(const ParamMap& params) override;
5958
std::vector<torch::Tensor> forward(const std::vector<torch::Tensor>& inputs) override;
@@ -67,10 +66,8 @@ struct SACMLPModel : BaseModel, public std::enable_shared_from_this<BaseModel> {
6766
std::vector<torch::nn::Linear> out_layers;
6867
std::vector<torch::Tensor> biases;
6968
std::vector<torch::Tensor> out_biases;
70-
7169
};
7270

73-
7471
struct ActorCriticMLPModel : BaseModel, public std::enable_shared_from_this<BaseModel> {
7572
void setup(const ParamMap& params) override;
7673
std::vector<torch::Tensor> forward(const std::vector<torch::Tensor>& inputs) override;

src/csrc/include/internal/rl/off_policy.h

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,14 @@ static void update_replay_buffer(const char* name, T* state_old, T* state_new, s
114114
auto action_old_tensor = get_tensor<L>(action_old, action_dim, action_shape)
115115
.to(torch::kFloat32, /* non_blocking = */ false, /* copy = */ true);
116116

117-
registry[name]->updateReplayBuffer(state_old_tensor, action_old_tensor, state_new_tensor,
118-
static_cast<float>(reward), final_state);
117+
registry[name]->updateReplayBuffer(state_old_tensor, action_old_tensor, state_new_tensor, static_cast<float>(reward),
118+
final_state);
119119
return;
120120
}
121121

122122
template <MemoryLayout L, typename T>
123123
static void predict_explore(const char* name, T* state, size_t state_dim, int64_t* state_shape, T* action,
124-
size_t action_dim, int64_t* action_shape, cudaStream_t ext_stream) {
124+
size_t action_dim, int64_t* action_shape, cudaStream_t ext_stream) {
125125

126126
#ifdef ENABLE_GPU
127127
// device and stream handling
@@ -134,8 +134,8 @@ static void predict_explore(const char* name, T* state, size_t state_dim, int64_
134134
#endif
135135

136136
// create tensors
137-
auto state_tensor = get_tensor<L>(state, state_dim, state_shape)
138-
.to(torch::kFloat32, /* non_blocking = */ false, /* copy = */ true);
137+
auto state_tensor =
138+
get_tensor<L>(state, state_dim, state_shape).to(torch::kFloat32, /* non_blocking = */ false, /* copy = */ true);
139139
auto action_tensor = get_tensor<L>(action, action_dim, action_shape);
140140

141141
// fwd pass
@@ -147,7 +147,7 @@ static void predict_explore(const char* name, T* state, size_t state_dim, int64_
147147

148148
template <MemoryLayout L, typename T>
149149
static void predict(const char* name, T* state, size_t state_dim, int64_t* state_shape, T* action, size_t action_dim,
150-
int64_t* action_shape, cudaStream_t ext_stream) {
150+
int64_t* action_shape, cudaStream_t ext_stream) {
151151

152152
#ifdef ENABLE_GPU
153153
// device and stream handling
@@ -158,10 +158,10 @@ static void predict(const char* name, T* state, size_t state_dim, int64_t* state
158158
guard.reset_stream(stream);
159159
}
160160
#endif
161-
161+
162162
// create tensors
163-
auto state_tensor = get_tensor<L>(state, state_dim, state_shape)
164-
.to(torch::kFloat32, /* non_blocking = */ false, /* copy = */ true);
163+
auto state_tensor =
164+
get_tensor<L>(state, state_dim, state_shape).to(torch::kFloat32, /* non_blocking = */ false, /* copy = */ true);
165165
auto action_tensor = get_tensor<L>(action, action_dim, action_shape);
166166

167167
// fwd pass
@@ -172,9 +172,9 @@ static void predict(const char* name, T* state, size_t state_dim, int64_t* state
172172
}
173173

174174
template <MemoryLayout L, typename T>
175-
static void policy_evaluate(const char* name, T* state, size_t state_dim, int64_t* state_shape, T* action, size_t action_dim,
176-
int64_t* action_shape, T* reward, size_t reward_dim, int64_t* reward_shape,
177-
cudaStream_t ext_stream) {
175+
static void policy_evaluate(const char* name, T* state, size_t state_dim, int64_t* state_shape, T* action,
176+
size_t action_dim, int64_t* action_shape, T* reward, size_t reward_dim,
177+
int64_t* reward_shape, cudaStream_t ext_stream) {
178178

179179
#ifdef ENABLE_GPU
180180
// device and stream handling
@@ -187,8 +187,8 @@ static void policy_evaluate(const char* name, T* state, size_t state_dim, int64_
187187
#endif
188188

189189
// create tensors
190-
auto state_tensor = get_tensor<L>(state, state_dim, state_shape)
191-
.to(torch::kFloat32, /* non_blocking = */ false, /* copy = */ true);
190+
auto state_tensor =
191+
get_tensor<L>(state, state_dim, state_shape).to(torch::kFloat32, /* non_blocking = */ false, /* copy = */ true);
192192
auto action_tensor = get_tensor<L>(action, action_dim, action_shape)
193193
.to(torch::kFloat32, /* non_blocking = */ false, /* copy = */ true);
194194
auto reward_tensor = get_tensor<L>(reward, reward_dim, reward_shape);
@@ -208,6 +208,6 @@ template <typename T> void wandb_log_system(const char* name, const char* metric
208208
}
209209

210210
} // namespace off_policy
211-
211+
212212
} // namespace rl
213213
} // namespace torchfort

src/csrc/include/internal/rl/off_policy/ddpg.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,16 @@
4343

4444
// rl stuff
4545
#include "internal/rl/noise_actor.h"
46-
#include "internal/rl/replay_buffer.h"
4746
#include "internal/rl/off_policy.h"
47+
#include "internal/rl/replay_buffer.h"
4848
#include "internal/rl/utils.h"
4949

5050
namespace torchfort {
5151

5252
namespace rl {
5353

5454
namespace off_policy {
55-
55+
5656
// implementing https://spinningup.openai.com/en/latest/algorithms/ddpg.html#pseudocode
5757
// we implement the update on a single batch with (s, a, r, s', d):
5858
// gamma is a tensor here to support multi-step delayed learning. Here, gamma^n
@@ -100,7 +100,8 @@ void train_ddpg(const ModelPack& p_model, const ModelPack& p_model_target, const
100100

101101
// backward and update step
102102
// compute loss
103-
torch::Tensor q_old_tensor = q_model.model->forward(std::vector<torch::Tensor>{state_old_tensor, action_old_tensor})[0];
103+
torch::Tensor q_old_tensor =
104+
q_model.model->forward(std::vector<torch::Tensor>{state_old_tensor, action_old_tensor})[0];
104105
torch::Tensor q_loss_tensor = q_loss_func->forward(q_old_tensor, y_tensor);
105106
q_model.optimizer->zero_grad();
106107
q_loss_tensor.backward();
@@ -277,7 +278,7 @@ class DDPGSystem : public RLOffPolicySystem, public std::enable_shared_from_this
277278
};
278279

279280
} // namespace off_policy
280-
281+
281282
} // namespace rl
282283

283284
} // namespace torchfort

0 commit comments

Comments
 (0)