summaryrefslogtreecommitdiff
path: root/pkgs/development/python-modules/torchrl/default.nix
blob: a5db6383c25e347ec830f99a9431de049042e75f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
{
  lib,
  stdenv,
  buildPythonPackage,
  fetchFromGitHub,

  # build-system
  cmake,
  ninja,
  numpy,
  pybind11,
  setuptools,
  torch,

  # dependencies
  cloudpickle,
  packaging,
  pyvers,
  tensordict,

  # optional-dependencies
  # atari
  gymnasium,
  # brax
  brax,
  jax,
  # checkpointing
  torchsnapshot,
  # dm-control
  dm-control,
  # gym-continuous
  mujoco,
  # llm
  accelerate,
  datasets,
  einops,
  immutabledict,
  langdetect,
  nltk,
  playwright,
  protobuf,
  safetensors,
  sentencepiece,
  transformers,
  vllm,
  # marl
  pettingzoo,
  # offline-data
  h5py,
  huggingface-hub,
  minari,
  pandas,
  pillow,
  requests,
  scikit-learn,
  torchvision,
  tqdm,
  # rendering
  moviepy,
  # utils
  git,
  hydra-core,
  tensorboard,
  wandb,

  # tests
  imageio,
  pytest-rerunfailures,
  pytestCheckHook,
  pyyaml,
  scipy,
}:

buildPythonPackage rec {
  pname = "torchrl";
  version = "0.10.1";
  pyproject = true;

  src = fetchFromGitHub {
    owner = "pytorch";
    repo = "rl";
    tag = "v${version}";
    hash = "sha256-Vd/w11P4NVrx2xki+VYlXQaM8F+vpdokke8ZAHg6h0Q=";
  };

  postPatch = ''
    substituteInPlace pyproject.toml \
      --replace-fail "pybind11[global]" "pybind11"
  '';

  build-system = [
    cmake
    ninja
    numpy
    pybind11
    setuptools
    torch
  ];
  dontUseCmakeConfigure = true;

  dependencies = [
    cloudpickle
    numpy
    packaging
    tensordict
    pyvers
    torch
  ];

  optional-dependencies = {
    atari = [
      gymnasium
    ]
    ++ gymnasium.optional-dependencies.atari;
    brax = [
      brax
      jax
    ];
    checkpointing = [ torchsnapshot ];
    dm-control = [ dm-control ];
    gym-continuous = [
      gymnasium
      mujoco
    ];
    llm = [
      accelerate
      datasets
      einops
      immutabledict
      langdetect
      nltk
      playwright
      protobuf
      safetensors
      sentencepiece
      transformers
      vllm
    ];
    marl = [
      # dm-meltingpot (unpackaged)
      pettingzoo
      # vmas (unpackaged)
    ];
    offline-data = [
      h5py
      huggingface-hub
      minari
      pandas
      pillow
      requests
      scikit-learn
      torchvision
      tqdm
    ];
    open-spiel = [
      # open-spiel (unpackaged)
    ];
    rendering = [ moviepy ];
    replay-buffer = [ torch ];
    utils = [
      git
      hydra-core
      # hydra-submitit-launcher (unpackaged)
      tensorboard
      tqdm
      wandb
    ];
  };

  # torchrl needs to create a folder to store datasets
  preBuild = ''
    export D4RL_DATASET_DIR=$(mktemp -d)
  '';

  pythonImportsCheck = [ "torchrl" ];

  # We have to delete the source because otherwise it is used instead of the installed package.
  preCheck = ''
    rm -rf torchrl

    export XDG_RUNTIME_DIR=$(mktemp -d)
  '';

  nativeCheckInputs = [
    h5py
    gymnasium
    imageio
    pytest-rerunfailures
    pytestCheckHook
    pyyaml
    scipy
    torchvision
  ]
  ++ optional-dependencies.atari
  ++ optional-dependencies.gym-continuous
  ++ optional-dependencies.llm
  ++ optional-dependencies.rendering;

  disabledTests = [
    # Require network
    "test_create_or_load_dataset"
    "test_from_text_env_tokenizer"
    "test_from_text_env_tokenizer_catframes"
    "test_from_text_rb_slicesampler"
    "test_generate"
    "test_get_dataloader"
    "test_get_scores"
    "test_preproc_data"
    "test_prompt_tensordict_tokenizer"
    "test_reward_model"
    "test_tensordict_tokenizer"
    "test_transform_compose"
    "test_transform_model"
    "test_transform_no_env"
    "test_transform_rb"

    # ray.exceptions.RuntimeEnvSetupError: Failed to set up runtime environment
    "TestRayCollector"

    # torchrl is incompatible with gymnasium>=1.0
    # https://github.com/pytorch/rl/discussions/2483
    "test_resetting_strategies"
    "test_torchrl_to_gym"
    "test_vecenvs_nan"

    # gym.error.VersionNotFound: Environment version `v5` for environment `HalfCheetah` doesn't exist.
    "test_collector_run"
    "test_transform_inverse"

    # OSError: Unable to synchronously create file (unable to truncate a file which is already open)
    "test_multi_env"
    "test_simple_env"

    # ImportWarning: Ignoring non-library in plugin directory:
    # /nix/store/cy8vwf1dacp3xfwnp9v6a1sz8bic8ylx-python3.12-mujoco-3.3.2/lib/python3.12/site-packages/mujoco/plugin/libmujoco.so.3.3.2
    "test_auto_register"
    "test_info_dict_reader"

    # mujoco.FatalError: an OpenGL platform library has not been loaded into this process, this most likely means that a valid OpenGL context has not been created before mjr_makeContext was called
    "test_vecenvs_env"

    # ValueError: Can't write images with one color channel.
    "test_log_video"

    # Those tests require the ALE environments (provided by unpackaged shimmy)
    "test_collector_env_reset"
    "test_gym"
    "test_gym_fake_td"
    "test_recorder"
    "test_recorder_load"
    "test_rollout"
    "test_parallel_trans_env_check"
    "test_serial_trans_env_check"
    "test_single_trans_env_check"
    "test_td_creation_from_spec"
    "test_trans_parallel_env_check"
    "test_trans_serial_env_check"
    "test_transform_env"

    # undeterministic
    "test_distributed_collector_updatepolicy"
    "test_timeit"

    # On a 24 threads system
    # assert torch.get_num_threads() == max(1, init_threads - 3)
    # AssertionError: assert 23 == 21
    "test_auto_num_threads"

    # Flaky (hangs indefinitely on some CPUs)
    "test_gae_multidim"
    "test_gae_param_as_tensor"
  ]
  ++ lib.optionals (stdenv.hostPlatform.isLinux && stdenv.hostPlatform.isAarch64) [
    # Flaky
    # AssertionError: assert tensor([51.]) == ((5 * 11) + 2)
    "test_vecnorm_parallel_auto"
  ];

  disabledTestPaths = [
    # ERROR collecting test/smoke_test.py
    # import file mismatch:
    # imported module 'smoke_test' has this __file__ attribute:
    #   /build/source/test/llm/smoke_test.py
    # which is not the same as the test file we want to collect:
    #   /build/source/test/smoke_test.py
    "test/llm"
  ];

  meta = {
    description = "Modular, primitive-first, python-first PyTorch library for Reinforcement Learning";
    homepage = "https://github.com/pytorch/rl";
    changelog = "https://github.com/pytorch/rl/releases/tag/v${version}";
    license = lib.licenses.mit;
    maintainers = with lib.maintainers; [ GaetanLepage ];
  };
}