solarian-diffusion 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (159) hide show
  1. diffusion_policy/__init__.py +0 -0
  2. diffusion_policy/codecs/__init__.py +0 -0
  3. diffusion_policy/codecs/imagecodecs_numcodecs.py +1408 -0
  4. diffusion_policy/common/__init__.py +0 -0
  5. diffusion_policy/common/checkpoint_util.py +64 -0
  6. diffusion_policy/common/json_logger.py +117 -0
  7. diffusion_policy/common/normalize_util.py +251 -0
  8. diffusion_policy/common/pose_repr_util.py +112 -0
  9. diffusion_policy/common/pytorch_util.py +82 -0
  10. diffusion_policy/common/q_critic_util.py +135 -0
  11. diffusion_policy/common/replay_buffer.py +672 -0
  12. diffusion_policy/common/sampler.py +204 -0
  13. diffusion_policy/config/legacy/task/blockpush_lowdim_seed.yaml +34 -0
  14. diffusion_policy/config/legacy/task/blockpush_lowdim_seed_abs.yaml +34 -0
  15. diffusion_policy/config/legacy/task/can_image.yaml +64 -0
  16. diffusion_policy/config/legacy/task/can_image_abs.yaml +64 -0
  17. diffusion_policy/config/legacy/task/can_lowdim.yaml +45 -0
  18. diffusion_policy/config/legacy/task/can_lowdim_abs.yaml +46 -0
  19. diffusion_policy/config/legacy/task/kitchen_lowdim.yaml +33 -0
  20. diffusion_policy/config/legacy/task/kitchen_lowdim_abs.yaml +38 -0
  21. diffusion_policy/config/legacy/task/lift_abs.yaml +72 -0
  22. diffusion_policy/config/legacy/task/lift_image.yaml +64 -0
  23. diffusion_policy/config/legacy/task/lift_image_abs.yaml +63 -0
  24. diffusion_policy/config/legacy/task/lift_lowdim.yaml +46 -0
  25. diffusion_policy/config/legacy/task/lift_lowdim_abs.yaml +47 -0
  26. diffusion_policy/config/legacy/task/pusht_image.yaml +40 -0
  27. diffusion_policy/config/legacy/task/pusht_lowdim.yaml +34 -0
  28. diffusion_policy/config/legacy/task/real_pusht_image.yaml +47 -0
  29. diffusion_policy/config/legacy/task/square_abs.yaml +72 -0
  30. diffusion_policy/config/legacy/task/square_image.yaml +64 -0
  31. diffusion_policy/config/legacy/task/square_image_abs.yaml +64 -0
  32. diffusion_policy/config/legacy/task/square_lowdim.yaml +46 -0
  33. diffusion_policy/config/legacy/task/square_lowdim_abs.yaml +47 -0
  34. diffusion_policy/config/legacy/task/tool_hang_abs.yaml +71 -0
  35. diffusion_policy/config/legacy/task/tool_hang_image.yaml +63 -0
  36. diffusion_policy/config/legacy/task/tool_hang_image_abs.yaml +63 -0
  37. diffusion_policy/config/legacy/task/tool_hang_lowdim.yaml +45 -0
  38. diffusion_policy/config/legacy/task/tool_hang_lowdim_abs.yaml +46 -0
  39. diffusion_policy/config/legacy/task/transport_image.yaml +75 -0
  40. diffusion_policy/config/legacy/task/transport_image_abs.yaml +75 -0
  41. diffusion_policy/config/legacy/task/transport_lowdim.yaml +49 -0
  42. diffusion_policy/config/legacy/task/transport_lowdim_abs.yaml +49 -0
  43. diffusion_policy/config/legacy/task/umi.yaml +45 -0
  44. diffusion_policy/config/legacy/task/umi_image.yaml +36 -0
  45. diffusion_policy/config/legacy/task/umi_teleop_image.yaml +41 -0
  46. diffusion_policy/config/legacy/train_bet_lowdim_workspace.yaml +132 -0
  47. diffusion_policy/config/legacy/train_diffusion_transformer_hybrid_workspace.yaml +141 -0
  48. diffusion_policy/config/legacy/train_diffusion_transformer_lowdim_kitchen_workspace.yaml +146 -0
  49. diffusion_policy/config/legacy/train_diffusion_transformer_lowdim_pusht_workspace.yaml +146 -0
  50. diffusion_policy/config/legacy/train_diffusion_transformer_lowdim_workspace.yaml +146 -0
  51. diffusion_policy/config/legacy/train_diffusion_transformer_real_hybrid_workspace.yaml +144 -0
  52. diffusion_policy/config/legacy/train_diffusion_transformer_umi_teleop_hybrid_workspace.yaml +156 -0
  53. diffusion_policy/config/legacy/train_diffusion_unet_ddim_hybrid_workspace.yaml +140 -0
  54. diffusion_policy/config/legacy/train_diffusion_unet_ddim_lowdim_workspace.yaml +146 -0
  55. diffusion_policy/config/legacy/train_diffusion_unet_hybrid_workspace.yaml +137 -0
  56. diffusion_policy/config/legacy/train_diffusion_unet_image_pretrained_workspace.yaml +149 -0
  57. diffusion_policy/config/legacy/train_diffusion_unet_image_workspace.yaml +150 -0
  58. diffusion_policy/config/legacy/train_diffusion_unet_lowdim_workspace.yaml +143 -0
  59. diffusion_policy/config/legacy/train_diffusion_unet_real_hybrid_workspace.yaml +141 -0
  60. diffusion_policy/config/legacy/train_diffusion_unet_real_image_workspace.yaml +152 -0
  61. diffusion_policy/config/legacy/train_diffusion_unet_real_pretrained_workspace.yaml +152 -0
  62. diffusion_policy/config/legacy/train_diffusion_unet_timm_workspace.yaml +145 -0
  63. diffusion_policy/config/legacy/train_diffusion_unet_umi_hybrid_workspace.yaml +141 -0
  64. diffusion_policy/config/legacy/train_diffusion_unet_umi_teleop_hybrid_workspace.yaml +153 -0
  65. diffusion_policy/config/legacy/train_diffusion_unet_umi_timm_workspace.yaml +145 -0
  66. diffusion_policy/config/legacy/train_diffusion_unet_umi_workspace.yaml +150 -0
  67. diffusion_policy/config/legacy/train_diffusion_unet_video_workspace.yaml +128 -0
  68. diffusion_policy/config/legacy/train_ibc_dfo_hybrid_workspace.yaml +111 -0
  69. diffusion_policy/config/legacy/train_ibc_dfo_lowdim_workspace.yaml +109 -0
  70. diffusion_policy/config/legacy/train_ibc_dfo_real_hybrid_workspace.yaml +111 -0
  71. diffusion_policy/config/legacy/train_robomimic_image_workspace.yaml +92 -0
  72. diffusion_policy/config/legacy/train_robomimic_lowdim_workspace.yaml +92 -0
  73. diffusion_policy/config/legacy/train_robomimic_real_image_workspace.yaml +92 -0
  74. diffusion_policy/config/rl_annotate.yaml +265 -0
  75. diffusion_policy/config/rl_annotate_per_frame.yaml +162 -0
  76. diffusion_policy/config/rl_annotate_per_frame_v4.yaml +162 -0
  77. diffusion_policy/config/rl_annotate_v2.yaml +141 -0
  78. diffusion_policy/config/rl_annotate_v3.yaml +165 -0
  79. diffusion_policy/config/task/lift.yaml +89 -0
  80. diffusion_policy/config/task/square.yaml +92 -0
  81. diffusion_policy/config/task/tool_hang.yaml +93 -0
  82. diffusion_policy/config/task/tool_hang_abs.yaml +71 -0
  83. diffusion_policy/config/task/umi.yaml +105 -0
  84. diffusion_policy/config/task/umi_bimanual.yaml +135 -0
  85. diffusion_policy/config/task/umi_cup_wild.yaml +106 -0
  86. diffusion_policy/config/task/umi_image.yaml +36 -0
  87. diffusion_policy/config/task/umi_pick_lego.yaml +106 -0
  88. diffusion_policy/config/task/umi_pick_single_lego_175.yaml +106 -0
  89. diffusion_policy/config/task/umi_pick_single_lego_bbox.yaml +135 -0
  90. diffusion_policy/config/task/umi_teleop_image.yaml +41 -0
  91. diffusion_policy/config/train_diffusion_transformer_umi_bimanual_workspace.yaml +185 -0
  92. diffusion_policy/config/train_diffusion_transformer_umi_workspace.yaml +185 -0
  93. diffusion_policy/config/train_diffusion_unet_image_workspace.yaml +152 -0
  94. diffusion_policy/config/train_diffusion_unet_timm_robomimic_workspace.yaml +152 -0
  95. diffusion_policy/config/train_diffusion_unet_timm_umi_cup_wild.yaml +260 -0
  96. diffusion_policy/config/train_diffusion_unet_timm_umi_pick_lego_pile_bbox.yaml +265 -0
  97. diffusion_policy/config/train_diffusion_unet_timm_umi_pick_lego_pile_bbox_awr.yaml +324 -0
  98. diffusion_policy/config/train_diffusion_unet_timm_umi_pick_lego_pile_bbox_parl.yaml +399 -0
  99. diffusion_policy/config/train_diffusion_unet_timm_umi_pick_single_lego.yaml +258 -0
  100. diffusion_policy/config/train_diffusion_unet_timm_umi_pick_single_lego_125.yaml +260 -0
  101. diffusion_policy/config/train_diffusion_unet_timm_umi_pick_single_lego_175.yaml +260 -0
  102. diffusion_policy/config/train_diffusion_unet_timm_umi_pick_single_lego_bbox.yaml +265 -0
  103. diffusion_policy/config/train_diffusion_unet_timm_umi_pick_two_legos_bbox.yaml +265 -0
  104. diffusion_policy/config/train_diffusion_unet_timm_umi_workspace.yaml +224 -0
  105. diffusion_policy/config/train_diffusion_unet_timm_umi_workspace_lego.yaml +213 -0
  106. diffusion_policy/config/train_diffusion_unet_timm_umi_workspace_lego_resume.yaml +213 -0
  107. diffusion_policy/config/train_diffusion_unet_timm_umi_workspace_pick_lego.yaml +254 -0
  108. diffusion_policy/config/train_diffusion_unet_timm_umi_workspace_red_cup.yaml +219 -0
  109. diffusion_policy/config/train_diffusion_unet_umi_bimanual_workspace.yaml +203 -0
  110. diffusion_policy/config/train_iql_umi.yaml +186 -0
  111. diffusion_policy/config/train_iql_umi_v2.yaml +212 -0
  112. diffusion_policy/config/train_iql_umi_v3.yaml +212 -0
  113. diffusion_policy/dataset/__init__.py +0 -0
  114. diffusion_policy/dataset/base_dataset.py +76 -0
  115. diffusion_policy/dataset/umi_dataset.py +652 -0
  116. diffusion_policy/env_runner/__init__.py +0 -0
  117. diffusion_policy/env_runner/base_image_runner.py +9 -0
  118. diffusion_policy/env_runner/real_pusht_image_runner.py +10 -0
  119. diffusion_policy/model/__init__.py +0 -0
  120. diffusion_policy/model/common/__init__.py +0 -0
  121. diffusion_policy/model/common/dict_of_tensor_mixin.py +38 -0
  122. diffusion_policy/model/common/lr_scheduler.py +46 -0
  123. diffusion_policy/model/common/module_attr_mixin.py +14 -0
  124. diffusion_policy/model/common/normalizer.py +353 -0
  125. diffusion_policy/model/common/tensor_util.py +960 -0
  126. diffusion_policy/model/diffusion/__init__.py +0 -0
  127. diffusion_policy/model/diffusion/conditional_unet1d.py +238 -0
  128. diffusion_policy/model/diffusion/conv1d_components.py +46 -0
  129. diffusion_policy/model/diffusion/ema_model.py +88 -0
  130. diffusion_policy/model/diffusion/mask_generator.py +221 -0
  131. diffusion_policy/model/diffusion/positional_embedding.py +17 -0
  132. diffusion_policy/model/vision/__init__.py +0 -0
  133. diffusion_policy/model/vision/crop_randomizer.py +288 -0
  134. diffusion_policy/model/vision/multi_image_obs_encoder.py +195 -0
  135. diffusion_policy/model/vision/timm_obs_encoder.py +525 -0
  136. diffusion_policy/policy/__init__.py +0 -0
  137. diffusion_policy/policy/base_image_policy.py +27 -0
  138. diffusion_policy/policy/diffusion_unet_image_policy.py +264 -0
  139. diffusion_policy/policy/diffusion_unet_timm_policy.py +607 -0
  140. diffusion_policy/policy/parl_action_optimizer.py +675 -0
  141. diffusion_policy/workspace/__init__.py +0 -0
  142. diffusion_policy/workspace/base_workspace.py +146 -0
  143. diffusion_policy/workspace/train_diffusion_unet_image_workspace.py +1585 -0
  144. solarian_diffusion/__init__.py +32 -0
  145. solarian_diffusion/config.py +31 -0
  146. solarian_diffusion/policy_server.py +316 -0
  147. solarian_diffusion/rtc.py +272 -0
  148. solarian_diffusion/service.py +16 -0
  149. solarian_diffusion/trainer.py +213 -0
  150. solarian_diffusion-0.1.0.dist-info/METADATA +126 -0
  151. solarian_diffusion-0.1.0.dist-info/RECORD +159 -0
  152. solarian_diffusion-0.1.0.dist-info/WHEEL +5 -0
  153. solarian_diffusion-0.1.0.dist-info/entry_points.txt +8 -0
  154. solarian_diffusion-0.1.0.dist-info/licenses/LICENSE +201 -0
  155. solarian_diffusion-0.1.0.dist-info/licenses/NOTICE +45 -0
  156. solarian_diffusion-0.1.0.dist-info/top_level.txt +3 -0
  157. umi/__init__.py +0 -0
  158. umi/common/__init__.py +0 -0
  159. umi/common/pose_util.py +115 -0
@@ -0,0 +1,960 @@
1
+ """
2
+ A collection of utilities for working with nested tensor structures consisting
3
+ of numpy arrays and torch tensors.
4
+ """
5
+ import collections
6
+ import numpy as np
7
+ import torch
8
+
9
+
10
+ def recursive_dict_list_tuple_apply(x, type_func_dict):
11
+ """
12
+ Recursively apply functions to a nested dictionary or list or tuple, given a dictionary of
13
+ {data_type: function_to_apply}.
14
+
15
+ Args:
16
+ x (dict or list or tuple): a possibly nested dictionary or list or tuple
17
+ type_func_dict (dict): a mapping from data types to the functions to be
18
+ applied for each data type.
19
+
20
+ Returns:
21
+ y (dict or list or tuple): new nested dict-list-tuple
22
+ """
23
+ assert(list not in type_func_dict)
24
+ assert(tuple not in type_func_dict)
25
+ assert(dict not in type_func_dict)
26
+
27
+ if isinstance(x, (dict, collections.OrderedDict)):
28
+ new_x = collections.OrderedDict() if isinstance(x, collections.OrderedDict) else dict()
29
+ for k, v in x.items():
30
+ new_x[k] = recursive_dict_list_tuple_apply(v, type_func_dict)
31
+ return new_x
32
+ elif isinstance(x, (list, tuple)):
33
+ ret = [recursive_dict_list_tuple_apply(v, type_func_dict) for v in x]
34
+ if isinstance(x, tuple):
35
+ ret = tuple(ret)
36
+ return ret
37
+ else:
38
+ for t, f in type_func_dict.items():
39
+ if isinstance(x, t):
40
+ return f(x)
41
+ else:
42
+ raise NotImplementedError(
43
+ 'Cannot handle data type %s' % str(type(x)))
44
+
45
+
46
+ def map_tensor(x, func):
47
+ """
48
+ Apply function @func to torch.Tensor objects in a nested dictionary or
49
+ list or tuple.
50
+
51
+ Args:
52
+ x (dict or list or tuple): a possibly nested dictionary or list or tuple
53
+ func (function): function to apply to each tensor
54
+
55
+ Returns:
56
+ y (dict or list or tuple): new nested dict-list-tuple
57
+ """
58
+ return recursive_dict_list_tuple_apply(
59
+ x,
60
+ {
61
+ torch.Tensor: func,
62
+ type(None): lambda x: x,
63
+ }
64
+ )
65
+
66
+
67
+ def map_ndarray(x, func):
68
+ """
69
+ Apply function @func to np.ndarray objects in a nested dictionary or
70
+ list or tuple.
71
+
72
+ Args:
73
+ x (dict or list or tuple): a possibly nested dictionary or list or tuple
74
+ func (function): function to apply to each array
75
+
76
+ Returns:
77
+ y (dict or list or tuple): new nested dict-list-tuple
78
+ """
79
+ return recursive_dict_list_tuple_apply(
80
+ x,
81
+ {
82
+ np.ndarray: func,
83
+ type(None): lambda x: x,
84
+ }
85
+ )
86
+
87
+
88
+ def map_tensor_ndarray(x, tensor_func, ndarray_func):
89
+ """
90
+ Apply function @tensor_func to torch.Tensor objects and @ndarray_func to
91
+ np.ndarray objects in a nested dictionary or list or tuple.
92
+
93
+ Args:
94
+ x (dict or list or tuple): a possibly nested dictionary or list or tuple
95
+ tensor_func (function): function to apply to each tensor
96
+ ndarray_Func (function): function to apply to each array
97
+
98
+ Returns:
99
+ y (dict or list or tuple): new nested dict-list-tuple
100
+ """
101
+ return recursive_dict_list_tuple_apply(
102
+ x,
103
+ {
104
+ torch.Tensor: tensor_func,
105
+ np.ndarray: ndarray_func,
106
+ type(None): lambda x: x,
107
+ }
108
+ )
109
+
110
+
111
+ def clone(x):
112
+ """
113
+ Clones all torch tensors and numpy arrays in nested dictionary or list
114
+ or tuple and returns a new nested structure.
115
+
116
+ Args:
117
+ x (dict or list or tuple): a possibly nested dictionary or list or tuple
118
+
119
+ Returns:
120
+ y (dict or list or tuple): new nested dict-list-tuple
121
+ """
122
+ return recursive_dict_list_tuple_apply(
123
+ x,
124
+ {
125
+ torch.Tensor: lambda x: x.clone(),
126
+ np.ndarray: lambda x: x.copy(),
127
+ type(None): lambda x: x,
128
+ }
129
+ )
130
+
131
+
132
+ def detach(x):
133
+ """
134
+ Detaches all torch tensors in nested dictionary or list
135
+ or tuple and returns a new nested structure.
136
+
137
+ Args:
138
+ x (dict or list or tuple): a possibly nested dictionary or list or tuple
139
+
140
+ Returns:
141
+ y (dict or list or tuple): new nested dict-list-tuple
142
+ """
143
+ return recursive_dict_list_tuple_apply(
144
+ x,
145
+ {
146
+ torch.Tensor: lambda x: x.detach(),
147
+ }
148
+ )
149
+
150
+
151
+ def to_batch(x):
152
+ """
153
+ Introduces a leading batch dimension of 1 for all torch tensors and numpy
154
+ arrays in nested dictionary or list or tuple and returns a new nested structure.
155
+
156
+ Args:
157
+ x (dict or list or tuple): a possibly nested dictionary or list or tuple
158
+
159
+ Returns:
160
+ y (dict or list or tuple): new nested dict-list-tuple
161
+ """
162
+ return recursive_dict_list_tuple_apply(
163
+ x,
164
+ {
165
+ torch.Tensor: lambda x: x[None, ...],
166
+ np.ndarray: lambda x: x[None, ...],
167
+ type(None): lambda x: x,
168
+ }
169
+ )
170
+
171
+
172
+ def to_sequence(x):
173
+ """
174
+ Introduces a time dimension of 1 at dimension 1 for all torch tensors and numpy
175
+ arrays in nested dictionary or list or tuple and returns a new nested structure.
176
+
177
+ Args:
178
+ x (dict or list or tuple): a possibly nested dictionary or list or tuple
179
+
180
+ Returns:
181
+ y (dict or list or tuple): new nested dict-list-tuple
182
+ """
183
+ return recursive_dict_list_tuple_apply(
184
+ x,
185
+ {
186
+ torch.Tensor: lambda x: x[:, None, ...],
187
+ np.ndarray: lambda x: x[:, None, ...],
188
+ type(None): lambda x: x,
189
+ }
190
+ )
191
+
192
+
193
+ def index_at_time(x, ind):
194
+ """
195
+ Indexes all torch tensors and numpy arrays in dimension 1 with index @ind in
196
+ nested dictionary or list or tuple and returns a new nested structure.
197
+
198
+ Args:
199
+ x (dict or list or tuple): a possibly nested dictionary or list or tuple
200
+ ind (int): index
201
+
202
+ Returns:
203
+ y (dict or list or tuple): new nested dict-list-tuple
204
+ """
205
+ return recursive_dict_list_tuple_apply(
206
+ x,
207
+ {
208
+ torch.Tensor: lambda x: x[:, ind, ...],
209
+ np.ndarray: lambda x: x[:, ind, ...],
210
+ type(None): lambda x: x,
211
+ }
212
+ )
213
+
214
+
215
+ def unsqueeze(x, dim):
216
+ """
217
+ Adds dimension of size 1 at dimension @dim in all torch tensors and numpy arrays
218
+ in nested dictionary or list or tuple and returns a new nested structure.
219
+
220
+ Args:
221
+ x (dict or list or tuple): a possibly nested dictionary or list or tuple
222
+ dim (int): dimension
223
+
224
+ Returns:
225
+ y (dict or list or tuple): new nested dict-list-tuple
226
+ """
227
+ return recursive_dict_list_tuple_apply(
228
+ x,
229
+ {
230
+ torch.Tensor: lambda x: x.unsqueeze(dim=dim),
231
+ np.ndarray: lambda x: np.expand_dims(x, axis=dim),
232
+ type(None): lambda x: x,
233
+ }
234
+ )
235
+
236
+
237
+ def contiguous(x):
238
+ """
239
+ Makes all torch tensors and numpy arrays contiguous in nested dictionary or
240
+ list or tuple and returns a new nested structure.
241
+
242
+ Args:
243
+ x (dict or list or tuple): a possibly nested dictionary or list or tuple
244
+
245
+ Returns:
246
+ y (dict or list or tuple): new nested dict-list-tuple
247
+ """
248
+ return recursive_dict_list_tuple_apply(
249
+ x,
250
+ {
251
+ torch.Tensor: lambda x: x.contiguous(),
252
+ np.ndarray: lambda x: np.ascontiguousarray(x),
253
+ type(None): lambda x: x,
254
+ }
255
+ )
256
+
257
+
258
+ def to_device(x, device):
259
+ """
260
+ Sends all torch tensors in nested dictionary or list or tuple to device
261
+ @device, and returns a new nested structure.
262
+
263
+ Args:
264
+ x (dict or list or tuple): a possibly nested dictionary or list or tuple
265
+ device (torch.Device): device to send tensors to
266
+
267
+ Returns:
268
+ y (dict or list or tuple): new nested dict-list-tuple
269
+ """
270
+ return recursive_dict_list_tuple_apply(
271
+ x,
272
+ {
273
+ torch.Tensor: lambda x, d=device: x.to(d),
274
+ type(None): lambda x: x,
275
+ }
276
+ )
277
+
278
+
279
+ def to_tensor(x):
280
+ """
281
+ Converts all numpy arrays in nested dictionary or list or tuple to
282
+ torch tensors (and leaves existing torch Tensors as-is), and returns
283
+ a new nested structure.
284
+
285
+ Args:
286
+ x (dict or list or tuple): a possibly nested dictionary or list or tuple
287
+
288
+ Returns:
289
+ y (dict or list or tuple): new nested dict-list-tuple
290
+ """
291
+ return recursive_dict_list_tuple_apply(
292
+ x,
293
+ {
294
+ torch.Tensor: lambda x: x,
295
+ np.ndarray: lambda x: torch.from_numpy(x),
296
+ type(None): lambda x: x,
297
+ }
298
+ )
299
+
300
+
301
+ def to_numpy(x):
302
+ """
303
+ Converts all torch tensors in nested dictionary or list or tuple to
304
+ numpy (and leaves existing numpy arrays as-is), and returns
305
+ a new nested structure.
306
+
307
+ Args:
308
+ x (dict or list or tuple): a possibly nested dictionary or list or tuple
309
+
310
+ Returns:
311
+ y (dict or list or tuple): new nested dict-list-tuple
312
+ """
313
+ def f(tensor):
314
+ if tensor.is_cuda:
315
+ return tensor.detach().cpu().numpy()
316
+ else:
317
+ return tensor.detach().numpy()
318
+ return recursive_dict_list_tuple_apply(
319
+ x,
320
+ {
321
+ torch.Tensor: f,
322
+ np.ndarray: lambda x: x,
323
+ type(None): lambda x: x,
324
+ }
325
+ )
326
+
327
+
328
+ def to_list(x):
329
+ """
330
+ Converts all torch tensors and numpy arrays in nested dictionary or list
331
+ or tuple to a list, and returns a new nested structure. Useful for
332
+ json encoding.
333
+
334
+ Args:
335
+ x (dict or list or tuple): a possibly nested dictionary or list or tuple
336
+
337
+ Returns:
338
+ y (dict or list or tuple): new nested dict-list-tuple
339
+ """
340
+ def f(tensor):
341
+ if tensor.is_cuda:
342
+ return tensor.detach().cpu().numpy().tolist()
343
+ else:
344
+ return tensor.detach().numpy().tolist()
345
+ return recursive_dict_list_tuple_apply(
346
+ x,
347
+ {
348
+ torch.Tensor: f,
349
+ np.ndarray: lambda x: x.tolist(),
350
+ type(None): lambda x: x,
351
+ }
352
+ )
353
+
354
+
355
+ def to_float(x):
356
+ """
357
+ Converts all torch tensors and numpy arrays in nested dictionary or list
358
+ or tuple to float type entries, and returns a new nested structure.
359
+
360
+ Args:
361
+ x (dict or list or tuple): a possibly nested dictionary or list or tuple
362
+
363
+ Returns:
364
+ y (dict or list or tuple): new nested dict-list-tuple
365
+ """
366
+ return recursive_dict_list_tuple_apply(
367
+ x,
368
+ {
369
+ torch.Tensor: lambda x: x.float(),
370
+ np.ndarray: lambda x: x.astype(np.float32),
371
+ type(None): lambda x: x,
372
+ }
373
+ )
374
+
375
+
376
+ def to_uint8(x):
377
+ """
378
+ Converts all torch tensors and numpy arrays in nested dictionary or list
379
+ or tuple to uint8 type entries, and returns a new nested structure.
380
+
381
+ Args:
382
+ x (dict or list or tuple): a possibly nested dictionary or list or tuple
383
+
384
+ Returns:
385
+ y (dict or list or tuple): new nested dict-list-tuple
386
+ """
387
+ return recursive_dict_list_tuple_apply(
388
+ x,
389
+ {
390
+ torch.Tensor: lambda x: x.byte(),
391
+ np.ndarray: lambda x: x.astype(np.uint8),
392
+ type(None): lambda x: x,
393
+ }
394
+ )
395
+
396
+
397
+ def to_torch(x, device):
398
+ """
399
+ Converts all numpy arrays and torch tensors in nested dictionary or list or tuple to
400
+ torch tensors on device @device and returns a new nested structure.
401
+
402
+ Args:
403
+ x (dict or list or tuple): a possibly nested dictionary or list or tuple
404
+ device (torch.Device): device to send tensors to
405
+
406
+ Returns:
407
+ y (dict or list or tuple): new nested dict-list-tuple
408
+ """
409
+ return to_device(to_float(to_tensor(x)), device)
410
+
411
+
412
+ def to_one_hot_single(tensor, num_class):
413
+ """
414
+ Convert tensor to one-hot representation, assuming a certain number of total class labels.
415
+
416
+ Args:
417
+ tensor (torch.Tensor): tensor containing integer labels
418
+ num_class (int): number of classes
419
+
420
+ Returns:
421
+ x (torch.Tensor): tensor containing one-hot representation of labels
422
+ """
423
+ x = torch.zeros(tensor.size() + (num_class,)).to(tensor.device)
424
+ x.scatter_(-1, tensor.unsqueeze(-1), 1)
425
+ return x
426
+
427
+
428
+ def to_one_hot(tensor, num_class):
429
+ """
430
+ Convert all tensors in nested dictionary or list or tuple to one-hot representation,
431
+ assuming a certain number of total class labels.
432
+
433
+ Args:
434
+ tensor (dict or list or tuple): a possibly nested dictionary or list or tuple
435
+ num_class (int): number of classes
436
+
437
+ Returns:
438
+ y (dict or list or tuple): new nested dict-list-tuple
439
+ """
440
+ return map_tensor(tensor, func=lambda x, nc=num_class: to_one_hot_single(x, nc))
441
+
442
+
443
+ def flatten_single(x, begin_axis=1):
444
+ """
445
+ Flatten a tensor in all dimensions from @begin_axis onwards.
446
+
447
+ Args:
448
+ x (torch.Tensor): tensor to flatten
449
+ begin_axis (int): which axis to flatten from
450
+
451
+ Returns:
452
+ y (torch.Tensor): flattened tensor
453
+ """
454
+ fixed_size = x.size()[:begin_axis]
455
+ _s = list(fixed_size) + [-1]
456
+ return x.reshape(*_s)
457
+
458
+
459
+ def flatten(x, begin_axis=1):
460
+ """
461
+ Flatten all tensors in nested dictionary or list or tuple, from @begin_axis onwards.
462
+
463
+ Args:
464
+ x (dict or list or tuple): a possibly nested dictionary or list or tuple
465
+ begin_axis (int): which axis to flatten from
466
+
467
+ Returns:
468
+ y (dict or list or tuple): new nested dict-list-tuple
469
+ """
470
+ return recursive_dict_list_tuple_apply(
471
+ x,
472
+ {
473
+ torch.Tensor: lambda x, b=begin_axis: flatten_single(x, begin_axis=b),
474
+ }
475
+ )
476
+
477
+
478
+ def reshape_dimensions_single(x, begin_axis, end_axis, target_dims):
479
+ """
480
+ Reshape selected dimensions in a tensor to a target dimension.
481
+
482
+ Args:
483
+ x (torch.Tensor): tensor to reshape
484
+ begin_axis (int): begin dimension
485
+ end_axis (int): end dimension
486
+ target_dims (tuple or list): target shape for the range of dimensions
487
+ (@begin_axis, @end_axis)
488
+
489
+ Returns:
490
+ y (torch.Tensor): reshaped tensor
491
+ """
492
+ assert(begin_axis <= end_axis)
493
+ assert(begin_axis >= 0)
494
+ assert(end_axis < len(x.shape))
495
+ assert(isinstance(target_dims, (tuple, list)))
496
+ s = x.shape
497
+ final_s = []
498
+ for i in range(len(s)):
499
+ if i == begin_axis:
500
+ final_s.extend(target_dims)
501
+ elif i < begin_axis or i > end_axis:
502
+ final_s.append(s[i])
503
+ return x.reshape(*final_s)
504
+
505
+
506
+ def reshape_dimensions(x, begin_axis, end_axis, target_dims):
507
+ """
508
+ Reshape selected dimensions for all tensors in nested dictionary or list or tuple
509
+ to a target dimension.
510
+
511
+ Args:
512
+ x (dict or list or tuple): a possibly nested dictionary or list or tuple
513
+ begin_axis (int): begin dimension
514
+ end_axis (int): end dimension
515
+ target_dims (tuple or list): target shape for the range of dimensions
516
+ (@begin_axis, @end_axis)
517
+
518
+ Returns:
519
+ y (dict or list or tuple): new nested dict-list-tuple
520
+ """
521
+ return recursive_dict_list_tuple_apply(
522
+ x,
523
+ {
524
+ torch.Tensor: lambda x, b=begin_axis, e=end_axis, t=target_dims: reshape_dimensions_single(
525
+ x, begin_axis=b, end_axis=e, target_dims=t),
526
+ np.ndarray: lambda x, b=begin_axis, e=end_axis, t=target_dims: reshape_dimensions_single(
527
+ x, begin_axis=b, end_axis=e, target_dims=t),
528
+ type(None): lambda x: x,
529
+ }
530
+ )
531
+
532
+
533
+ def join_dimensions(x, begin_axis, end_axis):
534
+ """
535
+ Joins all dimensions between dimensions (@begin_axis, @end_axis) into a flat dimension, for
536
+ all tensors in nested dictionary or list or tuple.
537
+
538
+ Args:
539
+ x (dict or list or tuple): a possibly nested dictionary or list or tuple
540
+ begin_axis (int): begin dimension
541
+ end_axis (int): end dimension
542
+
543
+ Returns:
544
+ y (dict or list or tuple): new nested dict-list-tuple
545
+ """
546
+ return recursive_dict_list_tuple_apply(
547
+ x,
548
+ {
549
+ torch.Tensor: lambda x, b=begin_axis, e=end_axis: reshape_dimensions_single(
550
+ x, begin_axis=b, end_axis=e, target_dims=[-1]),
551
+ np.ndarray: lambda x, b=begin_axis, e=end_axis: reshape_dimensions_single(
552
+ x, begin_axis=b, end_axis=e, target_dims=[-1]),
553
+ type(None): lambda x: x,
554
+ }
555
+ )
556
+
557
+
558
+ def expand_at_single(x, size, dim):
559
+ """
560
+ Expand a tensor at a single dimension @dim by @size
561
+
562
+ Args:
563
+ x (torch.Tensor): input tensor
564
+ size (int): size to expand
565
+ dim (int): dimension to expand
566
+
567
+ Returns:
568
+ y (torch.Tensor): expanded tensor
569
+ """
570
+ assert dim < x.ndimension()
571
+ assert x.shape[dim] == 1
572
+ expand_dims = [-1] * x.ndimension()
573
+ expand_dims[dim] = size
574
+ return x.expand(*expand_dims)
575
+
576
+
577
+ def expand_at(x, size, dim):
578
+ """
579
+ Expand all tensors in nested dictionary or list or tuple at a single
580
+ dimension @dim by @size.
581
+
582
+ Args:
583
+ x (dict or list or tuple): a possibly nested dictionary or list or tuple
584
+ size (int): size to expand
585
+ dim (int): dimension to expand
586
+
587
+ Returns:
588
+ y (dict or list or tuple): new nested dict-list-tuple
589
+ """
590
+ return map_tensor(x, lambda t, s=size, d=dim: expand_at_single(t, s, d))
591
+
592
+
593
+ def unsqueeze_expand_at(x, size, dim):
594
+ """
595
+ Unsqueeze and expand a tensor at a dimension @dim by @size.
596
+
597
+ Args:
598
+ x (dict or list or tuple): a possibly nested dictionary or list or tuple
599
+ size (int): size to expand
600
+ dim (int): dimension to unsqueeze and expand
601
+
602
+ Returns:
603
+ y (dict or list or tuple): new nested dict-list-tuple
604
+ """
605
+ x = unsqueeze(x, dim)
606
+ return expand_at(x, size, dim)
607
+
608
+
609
+ def repeat_by_expand_at(x, repeats, dim):
610
+ """
611
+ Repeat a dimension by combining expand and reshape operations.
612
+
613
+ Args:
614
+ x (dict or list or tuple): a possibly nested dictionary or list or tuple
615
+ repeats (int): number of times to repeat the target dimension
616
+ dim (int): dimension to repeat on
617
+
618
+ Returns:
619
+ y (dict or list or tuple): new nested dict-list-tuple
620
+ """
621
+ x = unsqueeze_expand_at(x, repeats, dim + 1)
622
+ return join_dimensions(x, dim, dim + 1)
623
+
624
+
625
+ def named_reduce_single(x, reduction, dim):
626
+ """
627
+ Reduce tensor at a dimension by named reduction functions.
628
+
629
+ Args:
630
+ x (torch.Tensor): tensor to be reduced
631
+ reduction (str): one of ["sum", "max", "mean", "flatten"]
632
+ dim (int): dimension to be reduced (or begin axis for flatten)
633
+
634
+ Returns:
635
+ y (torch.Tensor): reduced tensor
636
+ """
637
+ assert x.ndimension() > dim
638
+ assert reduction in ["sum", "max", "mean", "flatten"]
639
+ if reduction == "flatten":
640
+ x = flatten(x, begin_axis=dim)
641
+ elif reduction == "max":
642
+ x = torch.max(x, dim=dim)[0] # [B, D]
643
+ elif reduction == "sum":
644
+ x = torch.sum(x, dim=dim)
645
+ else:
646
+ x = torch.mean(x, dim=dim)
647
+ return x
648
+
649
+
650
+ def named_reduce(x, reduction, dim):
651
+ """
652
+ Reduces all tensors in nested dictionary or list or tuple at a dimension
653
+ using a named reduction function.
654
+
655
+ Args:
656
+ x (dict or list or tuple): a possibly nested dictionary or list or tuple
657
+ reduction (str): one of ["sum", "max", "mean", "flatten"]
658
+ dim (int): dimension to be reduced (or begin axis for flatten)
659
+
660
+ Returns:
661
+ y (dict or list or tuple): new nested dict-list-tuple
662
+ """
663
+ return map_tensor(x, func=lambda t, r=reduction, d=dim: named_reduce_single(t, r, d))
664
+
665
+
666
+ def gather_along_dim_with_dim_single(x, target_dim, source_dim, indices):
667
+ """
668
+ This function indexes out a target dimension of a tensor in a structured way,
669
+ by allowing a different value to be selected for each member of a flat index
670
+ tensor (@indices) corresponding to a source dimension. This can be interpreted
671
+ as moving along the source dimension, using the corresponding index value
672
+ in @indices to select values for all other dimensions outside of the
673
+ source and target dimensions. A common use case is to gather values
674
+ in target dimension 1 for each batch member (target dimension 0).
675
+
676
+ Args:
677
+ x (torch.Tensor): tensor to gather values for
678
+ target_dim (int): dimension to gather values along
679
+ source_dim (int): dimension to hold constant and use for gathering values
680
+ from the other dimensions
681
+ indices (torch.Tensor): flat index tensor with same shape as tensor @x along
682
+ @source_dim
683
+
684
+ Returns:
685
+ y (torch.Tensor): gathered tensor, with dimension @target_dim indexed out
686
+ """
687
+ assert len(indices.shape) == 1
688
+ assert x.shape[source_dim] == indices.shape[0]
689
+
690
+ # unsqueeze in all dimensions except the source dimension
691
+ new_shape = [1] * x.ndimension()
692
+ new_shape[source_dim] = -1
693
+ indices = indices.reshape(*new_shape)
694
+
695
+ # repeat in all dimensions - but preserve shape of source dimension,
696
+ # and make sure target_dimension has singleton dimension
697
+ expand_shape = list(x.shape)
698
+ expand_shape[source_dim] = -1
699
+ expand_shape[target_dim] = 1
700
+ indices = indices.expand(*expand_shape)
701
+
702
+ out = x.gather(dim=target_dim, index=indices)
703
+ return out.squeeze(target_dim)
704
+
705
+
706
+ def gather_along_dim_with_dim(x, target_dim, source_dim, indices):
707
+ """
708
+ Apply @gather_along_dim_with_dim_single to all tensors in a nested
709
+ dictionary or list or tuple.
710
+
711
+ Args:
712
+ x (dict or list or tuple): a possibly nested dictionary or list or tuple
713
+ target_dim (int): dimension to gather values along
714
+ source_dim (int): dimension to hold constant and use for gathering values
715
+ from the other dimensions
716
+ indices (torch.Tensor): flat index tensor with same shape as tensor @x along
717
+ @source_dim
718
+
719
+ Returns:
720
+ y (dict or list or tuple): new nested dict-list-tuple
721
+ """
722
+ return map_tensor(x,
723
+ lambda y, t=target_dim, s=source_dim, i=indices: gather_along_dim_with_dim_single(y, t, s, i))
724
+
725
+
726
+ def gather_sequence_single(seq, indices):
727
+ """
728
+ Given a tensor with leading dimensions [B, T, ...], gather an element from each sequence in
729
+ the batch given an index for each sequence.
730
+
731
+ Args:
732
+ seq (torch.Tensor): tensor with leading dimensions [B, T, ...]
733
+ indices (torch.Tensor): tensor indices of shape [B]
734
+
735
+ Return:
736
+ y (torch.Tensor): indexed tensor of shape [B, ....]
737
+ """
738
+ return gather_along_dim_with_dim_single(seq, target_dim=1, source_dim=0, indices=indices)
739
+
740
+
741
+ def gather_sequence(seq, indices):
742
+ """
743
+ Given a nested dictionary or list or tuple, gathers an element from each sequence of the batch
744
+ for tensors with leading dimensions [B, T, ...].
745
+
746
+ Args:
747
+ seq (dict or list or tuple): a possibly nested dictionary or list or tuple with tensors
748
+ of leading dimensions [B, T, ...]
749
+ indices (torch.Tensor): tensor indices of shape [B]
750
+
751
+ Returns:
752
+ y (dict or list or tuple): new nested dict-list-tuple with tensors of shape [B, ...]
753
+ """
754
+ return gather_along_dim_with_dim(seq, target_dim=1, source_dim=0, indices=indices)
755
+
756
+
757
+ def pad_sequence_single(seq, padding, batched=False, pad_same=True, pad_values=None):
758
+ """
759
+ Pad input tensor or array @seq in the time dimension (dimension 1).
760
+
761
+ Args:
762
+ seq (np.ndarray or torch.Tensor): sequence to be padded
763
+ padding (tuple): begin and end padding, e.g. [1, 1] pads both begin and end of the sequence by 1
764
+ batched (bool): if sequence has the batch dimension
765
+ pad_same (bool): if pad by duplicating
766
+ pad_values (scalar or (ndarray, Tensor)): values to be padded if not pad_same
767
+
768
+ Returns:
769
+ padded sequence (np.ndarray or torch.Tensor)
770
+ """
771
+ assert isinstance(seq, (np.ndarray, torch.Tensor))
772
+ assert pad_same or pad_values is not None
773
+ if pad_values is not None:
774
+ assert isinstance(pad_values, float)
775
+ repeat_func = np.repeat if isinstance(seq, np.ndarray) else torch.repeat_interleave
776
+ concat_func = np.concatenate if isinstance(seq, np.ndarray) else torch.cat
777
+ ones_like_func = np.ones_like if isinstance(seq, np.ndarray) else torch.ones_like
778
+ seq_dim = 1 if batched else 0
779
+
780
+ begin_pad = []
781
+ end_pad = []
782
+
783
+ if padding[0] > 0:
784
+ pad = seq[[0]] if pad_same else ones_like_func(seq[[0]]) * pad_values
785
+ begin_pad.append(repeat_func(pad, padding[0], seq_dim))
786
+ if padding[1] > 0:
787
+ pad = seq[[-1]] if pad_same else ones_like_func(seq[[-1]]) * pad_values
788
+ end_pad.append(repeat_func(pad, padding[1], seq_dim))
789
+
790
+ return concat_func(begin_pad + [seq] + end_pad, seq_dim)
791
+
792
+
793
+ def pad_sequence(seq, padding, batched=False, pad_same=True, pad_values=None):
794
+ """
795
+ Pad a nested dictionary or list or tuple of sequence tensors in the time dimension (dimension 1).
796
+
797
+ Args:
798
+ seq (dict or list or tuple): a possibly nested dictionary or list or tuple with tensors
799
+ of leading dimensions [B, T, ...]
800
+ padding (tuple): begin and end padding, e.g. [1, 1] pads both begin and end of the sequence by 1
801
+ batched (bool): if sequence has the batch dimension
802
+ pad_same (bool): if pad by duplicating
803
+ pad_values (scalar or (ndarray, Tensor)): values to be padded if not pad_same
804
+
805
+ Returns:
806
+ padded sequence (dict or list or tuple)
807
+ """
808
+ return recursive_dict_list_tuple_apply(
809
+ seq,
810
+ {
811
+ torch.Tensor: lambda x, p=padding, b=batched, ps=pad_same, pv=pad_values:
812
+ pad_sequence_single(x, p, b, ps, pv),
813
+ np.ndarray: lambda x, p=padding, b=batched, ps=pad_same, pv=pad_values:
814
+ pad_sequence_single(x, p, b, ps, pv),
815
+ type(None): lambda x: x,
816
+ }
817
+ )
818
+
819
+
820
+ def assert_size_at_dim_single(x, size, dim, msg):
821
+ """
822
+ Ensure that array or tensor @x has size @size in dim @dim.
823
+
824
+ Args:
825
+ x (np.ndarray or torch.Tensor): input array or tensor
826
+ size (int): size that tensors should have at @dim
827
+ dim (int): dimension to check
828
+ msg (str): text to display if assertion fails
829
+ """
830
+ assert x.shape[dim] == size, msg
831
+
832
+
833
+ def assert_size_at_dim(x, size, dim, msg):
834
+ """
835
+ Ensure that arrays and tensors in nested dictionary or list or tuple have
836
+ size @size in dim @dim.
837
+
838
+ Args:
839
+ x (dict or list or tuple): a possibly nested dictionary or list or tuple
840
+ size (int): size that tensors should have at @dim
841
+ dim (int): dimension to check
842
+ """
843
+ map_tensor(x, lambda t, s=size, d=dim, m=msg: assert_size_at_dim_single(t, s, d, m))
844
+
845
+
846
+ def get_shape(x):
847
+ """
848
+ Get all shapes of arrays and tensors in nested dictionary or list or tuple.
849
+
850
+ Args:
851
+ x (dict or list or tuple): a possibly nested dictionary or list or tuple
852
+
853
+ Returns:
854
+ y (dict or list or tuple): new nested dict-list-tuple that contains each array or
855
+ tensor's shape
856
+ """
857
+ return recursive_dict_list_tuple_apply(
858
+ x,
859
+ {
860
+ torch.Tensor: lambda x: x.shape,
861
+ np.ndarray: lambda x: x.shape,
862
+ type(None): lambda x: x,
863
+ }
864
+ )
865
+
866
+
867
+ def list_of_flat_dict_to_dict_of_list(list_of_dict):
868
+ """
869
+ Helper function to go from a list of flat dictionaries to a dictionary of lists.
870
+ By "flat" we mean that none of the values are dictionaries, but are numpy arrays,
871
+ floats, etc.
872
+
873
+ Args:
874
+ list_of_dict (list): list of flat dictionaries
875
+
876
+ Returns:
877
+ dict_of_list (dict): dictionary of lists
878
+ """
879
+ assert isinstance(list_of_dict, list)
880
+ dic = collections.OrderedDict()
881
+ for i in range(len(list_of_dict)):
882
+ for k in list_of_dict[i]:
883
+ if k not in dic:
884
+ dic[k] = []
885
+ dic[k].append(list_of_dict[i][k])
886
+ return dic
887
+
888
+
889
+ def flatten_nested_dict_list(d, parent_key='', sep='_', item_key=''):
890
+ """
891
+ Flatten a nested dict or list to a list.
892
+
893
+ For example, given a dict
894
+ {
895
+ a: 1
896
+ b: {
897
+ c: 2
898
+ }
899
+ c: 3
900
+ }
901
+
902
+ the function would return [(a, 1), (b_c, 2), (c, 3)]
903
+
904
+ Args:
905
+ d (dict, list): a nested dict or list to be flattened
906
+ parent_key (str): recursion helper
907
+ sep (str): separator for nesting keys
908
+ item_key (str): recursion helper
909
+ Returns:
910
+ list: a list of (key, value) tuples
911
+ """
912
+ items = []
913
+ if isinstance(d, (tuple, list)):
914
+ new_key = parent_key + sep + item_key if len(parent_key) > 0 else item_key
915
+ for i, v in enumerate(d):
916
+ items.extend(flatten_nested_dict_list(v, new_key, sep=sep, item_key=str(i)))
917
+ return items
918
+ elif isinstance(d, dict):
919
+ new_key = parent_key + sep + item_key if len(parent_key) > 0 else item_key
920
+ for k, v in d.items():
921
+ assert isinstance(k, str)
922
+ items.extend(flatten_nested_dict_list(v, new_key, sep=sep, item_key=k))
923
+ return items
924
+ else:
925
+ new_key = parent_key + sep + item_key if len(parent_key) > 0 else item_key
926
+ return [(new_key, d)]
927
+
928
+
929
+ def time_distributed(inputs, op, activation=None, inputs_as_kwargs=False, inputs_as_args=False, **kwargs):
930
+ """
931
+ Apply function @op to all tensors in nested dictionary or list or tuple @inputs in both the
932
+ batch (B) and time (T) dimension, where the tensors are expected to have shape [B, T, ...].
933
+ Will do this by reshaping tensors to [B * T, ...], passing through the op, and then reshaping
934
+ outputs to [B, T, ...].
935
+
936
+ Args:
937
+ inputs (list or tuple or dict): a possibly nested dictionary or list or tuple with tensors
938
+ of leading dimensions [B, T, ...]
939
+ op: a layer op that accepts inputs
940
+ activation: activation to apply at the output
941
+ inputs_as_kwargs (bool): whether to feed input as a kwargs dict to the op
942
+ inputs_as_args (bool) whether to feed input as a args list to the op
943
+ kwargs (dict): other kwargs to supply to the op
944
+
945
+ Returns:
946
+ outputs (dict or list or tuple): new nested dict-list-tuple with tensors of leading dimension [B, T].
947
+ """
948
+ batch_size, seq_len = flatten_nested_dict_list(inputs)[0][1].shape[:2]
949
+ inputs = join_dimensions(inputs, 0, 1)
950
+ if inputs_as_kwargs:
951
+ outputs = op(**inputs, **kwargs)
952
+ elif inputs_as_args:
953
+ outputs = op(*inputs, **kwargs)
954
+ else:
955
+ outputs = op(inputs, **kwargs)
956
+
957
+ if activation is not None:
958
+ outputs = map_tensor(outputs, activation)
959
+ outputs = reshape_dimensions(outputs, begin_axis=0, end_axis=0, target_dims=(batch_size, seq_len))
960
+ return outputs