fusion-bench 0.2.9__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 (727) hide show
  1. fusion_bench/__init__.py +20 -0
  2. fusion_bench/__main__.py +4 -0
  3. fusion_bench/compat/__init__.py +0 -0
  4. fusion_bench/compat/method/__init__.py +109 -0
  5. fusion_bench/compat/method/base_algorithm.py +58 -0
  6. fusion_bench/compat/modelpool/AutoModelForSeq2SeqLM.py +34 -0
  7. fusion_bench/compat/modelpool/__init__.py +116 -0
  8. fusion_bench/compat/modelpool/base_pool.py +328 -0
  9. fusion_bench/compat/modelpool/huggingface_clip_vision.py +178 -0
  10. fusion_bench/compat/taskpool/__init__.py +95 -0
  11. fusion_bench/compat/taskpool/base_pool.py +111 -0
  12. fusion_bench/compat/taskpool/clip_image_classification.py +210 -0
  13. fusion_bench/compat/taskpool/flan_t5_glue_text_generation.py +175 -0
  14. fusion_bench/constants/__init__.py +2 -0
  15. fusion_bench/constants/paths.py +18 -0
  16. fusion_bench/dataset/__init__.py +29 -0
  17. fusion_bench/dataset/arc_agi/__init__.py +6 -0
  18. fusion_bench/dataset/arc_agi/arc.py +308 -0
  19. fusion_bench/dataset/arc_agi/arc_agi.py +365 -0
  20. fusion_bench/dataset/arc_agi/augmenters.py +1036 -0
  21. fusion_bench/dataset/arc_agi/messagers.py +1355 -0
  22. fusion_bench/dataset/arc_agi/np_cache.py +168 -0
  23. fusion_bench/dataset/arc_agi/preprocess.py +298 -0
  24. fusion_bench/dataset/arc_agi/representers.py +1019 -0
  25. fusion_bench/dataset/clip_dataset.py +71 -0
  26. fusion_bench/dataset/fer2013.py +12 -0
  27. fusion_bench/dataset/gpt2_glue.py +300 -0
  28. fusion_bench/dataset/gsm8k.py +60 -0
  29. fusion_bench/dataset/image_dataset.py +55 -0
  30. fusion_bench/dataset/imdb.py +11 -0
  31. fusion_bench/dataset/llama/__init__.py +1 -0
  32. fusion_bench/dataset/llama/alpaca.py +232 -0
  33. fusion_bench/dataset/llama/collate.py +120 -0
  34. fusion_bench/dataset/llama/metamathqa.py +50 -0
  35. fusion_bench/dataset/llama/openai.py +160 -0
  36. fusion_bench/dataset/llama/preference_700k.py +70 -0
  37. fusion_bench/dataset/llama/sharegpt.py +141 -0
  38. fusion_bench/dataset/llama/squad.py +125 -0
  39. fusion_bench/dataset/llama/stanford_shp.py +90 -0
  40. fusion_bench/dataset/llama/ultrachat.py +58 -0
  41. fusion_bench/dataset/llama/utils/__init__.py +0 -0
  42. fusion_bench/dataset/llama/wikitext.py +89 -0
  43. fusion_bench/dataset/nyuv2.py +119 -0
  44. fusion_bench/method/__init__.py +177 -0
  45. fusion_bench/method/ada_svd/__init__.py +2 -0
  46. fusion_bench/method/ada_svd/clip_vision.py +319 -0
  47. fusion_bench/method/adamerging/__init__.py +6 -0
  48. fusion_bench/method/adamerging/clip_layer_wise_adamerging.py +46 -0
  49. fusion_bench/method/adamerging/clip_task_wise_adamerging.py +187 -0
  50. fusion_bench/method/adamerging/entropy_loss.py +25 -0
  51. fusion_bench/method/adamerging/flan_t5_layer_wise_adamerging.py +332 -0
  52. fusion_bench/method/adamerging/gpt2_layer_wise_adamerging.py +351 -0
  53. fusion_bench/method/adamerging/layer_wise_adamerging.py +252 -0
  54. fusion_bench/method/adamerging/llama_adamerging.py +335 -0
  55. fusion_bench/method/adamerging/min_norm_solvers.py +227 -0
  56. fusion_bench/method/adamerging/task_wise_adamerging.py +174 -0
  57. fusion_bench/method/adamerging/utils.py +15 -0
  58. fusion_bench/method/analysis/__init__.py +2 -0
  59. fusion_bench/method/analysis/task_vector_cos_similarity.py +172 -0
  60. fusion_bench/method/analysis/task_vector_violin_plot.py +205 -0
  61. fusion_bench/method/base_algorithm.py +44 -0
  62. fusion_bench/method/classification/__init__.py +3 -0
  63. fusion_bench/method/classification/clip_finetune.py +444 -0
  64. fusion_bench/method/classification/continual_clip_finetune.py +297 -0
  65. fusion_bench/method/concrete_subspace/__init__.py +6 -0
  66. fusion_bench/method/concrete_subspace/clip_concrete_adamerging.py +595 -0
  67. fusion_bench/method/concrete_subspace/clip_concrete_task_arithmetic.py +263 -0
  68. fusion_bench/method/dare/__init__.py +4 -0
  69. fusion_bench/method/dare/simple_average.py +31 -0
  70. fusion_bench/method/dare/task_arithmetic.py +82 -0
  71. fusion_bench/method/dare/ties_merging.py +100 -0
  72. fusion_bench/method/dare/utils.py +87 -0
  73. fusion_bench/method/dawe/__init__.py +2 -0
  74. fusion_bench/method/dawe/dawe_for_clip.py +274 -0
  75. fusion_bench/method/dawe/warppers/__init__.py +13 -0
  76. fusion_bench/method/dawe/warppers/dawe_model.py +256 -0
  77. fusion_bench/method/depth_upscaling/__init__.py +3 -0
  78. fusion_bench/method/depth_upscaling/depth_upscaling.py +89 -0
  79. fusion_bench/method/depth_upscaling/depth_upscaling_for_llama.py +57 -0
  80. fusion_bench/method/dummy.py +35 -0
  81. fusion_bench/method/ensemble.py +98 -0
  82. fusion_bench/method/fisher_merging/__init__.py +4 -0
  83. fusion_bench/method/fisher_merging/clip_fisher_merging.py +191 -0
  84. fusion_bench/method/fisher_merging/fisher_merging.py +484 -0
  85. fusion_bench/method/fisher_merging/gpt2_fisher_merging.py +193 -0
  86. fusion_bench/method/linear/__init__.py +6 -0
  87. fusion_bench/method/linear/expo.py +118 -0
  88. fusion_bench/method/linear/linear_interpolation.py +60 -0
  89. fusion_bench/method/linear/llama_expo.py +229 -0
  90. fusion_bench/method/linear/simple_average_for_llama.py +54 -0
  91. fusion_bench/method/linear/task_arithmetic_for_llama.py +57 -0
  92. fusion_bench/method/lm_finetune/__init__.py +3 -0
  93. fusion_bench/method/lm_finetune/bradley_terry_rm.py +432 -0
  94. fusion_bench/method/lm_finetune/causal_lm_pretrain.py +7 -0
  95. fusion_bench/method/lm_finetune/fullfinetune_sft.py +375 -0
  96. fusion_bench/method/lm_finetune/peftfinetune_sft.py +370 -0
  97. fusion_bench/method/mixture_of_experts/__init__.py +7 -0
  98. fusion_bench/method/mixture_of_experts/mixtral_merging.py +112 -0
  99. fusion_bench/method/mixture_of_experts/mixtral_upcycling.py +329 -0
  100. fusion_bench/method/model_recombination.py +121 -0
  101. fusion_bench/method/opcm/__init__.py +4 -0
  102. fusion_bench/method/opcm/opcm.py +277 -0
  103. fusion_bench/method/opcm/task_arithmetic.py +115 -0
  104. fusion_bench/method/opcm/ties_merging.py +156 -0
  105. fusion_bench/method/opcm/utils.py +73 -0
  106. fusion_bench/method/opcm/weight_average.py +120 -0
  107. fusion_bench/method/pruning/__init__.py +5 -0
  108. fusion_bench/method/pruning/llama_magnitude_prune.py +202 -0
  109. fusion_bench/method/pruning/llama_random_prune.py +143 -0
  110. fusion_bench/method/pruning/llama_wanda_prune.py +359 -0
  111. fusion_bench/method/pruning/magnitude_diff_pruning.py +180 -0
  112. fusion_bench/method/pruning/prune_utils.py +165 -0
  113. fusion_bench/method/pruning/wanda_utils/__init__.py +7 -0
  114. fusion_bench/method/pruning/wanda_utils/ablate.py +188 -0
  115. fusion_bench/method/pruning/wanda_utils/data.py +135 -0
  116. fusion_bench/method/pruning/wanda_utils/eval.py +245 -0
  117. fusion_bench/method/pruning/wanda_utils/layerwrapper.py +61 -0
  118. fusion_bench/method/pruning/wanda_utils/prune.py +581 -0
  119. fusion_bench/method/pruning/wanda_utils/prune_opt.py +539 -0
  120. fusion_bench/method/pruning/wanda_utils/sparsegpt.py +165 -0
  121. fusion_bench/method/pwe_moe/__init__.py +5 -0
  122. fusion_bench/method/pwe_moe/clip_pwe_moe.py +315 -0
  123. fusion_bench/method/pwe_moe/module.py +316 -0
  124. fusion_bench/method/pwe_moe/phn/__init__.py +2 -0
  125. fusion_bench/method/pwe_moe/phn/solvers.py +195 -0
  126. fusion_bench/method/pwe_moe/utils.py +43 -0
  127. fusion_bench/method/rankone_moe/__init__.py +3 -0
  128. fusion_bench/method/rankone_moe/clip_rankone_moe.py +160 -0
  129. fusion_bench/method/rankone_moe/rankone_moe.py +249 -0
  130. fusion_bench/method/regmean/__init__.py +4 -0
  131. fusion_bench/method/regmean/clip_regmean.py +131 -0
  132. fusion_bench/method/regmean/gpt2_regmean.py +147 -0
  133. fusion_bench/method/regmean/regmean.py +375 -0
  134. fusion_bench/method/simple_average.py +112 -0
  135. fusion_bench/method/slerp/__init__.py +2 -0
  136. fusion_bench/method/slerp/slerp.py +101 -0
  137. fusion_bench/method/slerp/slerp_utils.py +107 -0
  138. fusion_bench/method/smile_upscaling/__init__.py +3 -0
  139. fusion_bench/method/smile_upscaling/singular_projection_merging.py +198 -0
  140. fusion_bench/method/smile_upscaling/smile_mistral_upscaling.py +331 -0
  141. fusion_bench/method/smile_upscaling/smile_upscaling.py +573 -0
  142. fusion_bench/method/sparse_we_moe/__init__.py +2 -0
  143. fusion_bench/method/sparse_we_moe/sparse_clip_we_moe.py +248 -0
  144. fusion_bench/method/sparse_we_moe/sparse_we_moe.py +301 -0
  145. fusion_bench/method/sparselo/__init__.py +2 -0
  146. fusion_bench/method/sparselo/sparselo.py +955 -0
  147. fusion_bench/method/surgery/__init__.py +1 -0
  148. fusion_bench/method/surgery/clip_layer_wise_adamerging_surgery.py +157 -0
  149. fusion_bench/method/tall_mask/__init__.py +0 -0
  150. fusion_bench/method/tall_mask/utils.py +234 -0
  151. fusion_bench/method/task_arithmetic/__init__.py +2 -0
  152. fusion_bench/method/task_arithmetic/task_arithmetic.py +151 -0
  153. fusion_bench/method/task_singular_vector/TSVC.py +16 -0
  154. fusion_bench/method/task_singular_vector/TSVM.py +63 -0
  155. fusion_bench/method/task_singular_vector/__init__.py +9 -0
  156. fusion_bench/method/task_singular_vector/utils/TSVC_utils.py +50 -0
  157. fusion_bench/method/task_singular_vector/utils/TSVM_utils.py +640 -0
  158. fusion_bench/method/task_singular_vector/utils/__init__.py +7 -0
  159. fusion_bench/method/ties_merging/__init__.py +2 -0
  160. fusion_bench/method/ties_merging/ties_merging.py +117 -0
  161. fusion_bench/method/ties_merging/ties_merging_utils.py +331 -0
  162. fusion_bench/method/trust_region/__init__.py +2 -0
  163. fusion_bench/method/trust_region/clip_task_arithmetic.py +205 -0
  164. fusion_bench/method/trust_region/utils.py +58 -0
  165. fusion_bench/method/we_moe/__init__.py +2 -0
  166. fusion_bench/method/we_moe/clip_we_moe.py +161 -0
  167. fusion_bench/method/we_moe/we_moe.py +247 -0
  168. fusion_bench/method/weighted_average/__init__.py +3 -0
  169. fusion_bench/method/weighted_average/llama.py +113 -0
  170. fusion_bench/method/weighted_average/weighted_average.py +102 -0
  171. fusion_bench/metrics/__init__.py +0 -0
  172. fusion_bench/metrics/continual_learning/backward_transfer.py +22 -0
  173. fusion_bench/metrics/nyuv2/__init__.py +11 -0
  174. fusion_bench/metrics/nyuv2/depth.py +45 -0
  175. fusion_bench/metrics/nyuv2/loss.py +31 -0
  176. fusion_bench/metrics/nyuv2/noise.py +16 -0
  177. fusion_bench/metrics/nyuv2/normal.py +48 -0
  178. fusion_bench/metrics/nyuv2/segmentation.py +43 -0
  179. fusion_bench/metrics/text_to_image_generation/__init__.py +9 -0
  180. fusion_bench/metrics/text_to_image_generation/aesthetic_scorer.py +123 -0
  181. fusion_bench/metrics/text_to_image_generation/compressibility.py +49 -0
  182. fusion_bench/metrics/text_to_image_generation/pickscore_scorer.py +95 -0
  183. fusion_bench/mixins/__init__.py +28 -0
  184. fusion_bench/mixins/clip_classification.py +252 -0
  185. fusion_bench/mixins/fabric_training.py +320 -0
  186. fusion_bench/mixins/lightning_fabric.py +174 -0
  187. fusion_bench/mixins/optim/__init__.py +0 -0
  188. fusion_bench/mixins/optim/adamw_with_warmup.py +42 -0
  189. fusion_bench/mixins/rich_live.py +21 -0
  190. fusion_bench/mixins/serialization.py +132 -0
  191. fusion_bench/mixins/simple_profiler.py +79 -0
  192. fusion_bench/modelpool/PeftModelForSeq2SeqLM.py +49 -0
  193. fusion_bench/modelpool/__init__.py +42 -0
  194. fusion_bench/modelpool/base_pool.py +268 -0
  195. fusion_bench/modelpool/causal_lm/__init__.py +2 -0
  196. fusion_bench/modelpool/causal_lm/causal_lm.py +139 -0
  197. fusion_bench/modelpool/clip_vision/__init__.py +1 -0
  198. fusion_bench/modelpool/clip_vision/modelpool.py +145 -0
  199. fusion_bench/modelpool/huggingface_automodel.py +20 -0
  200. fusion_bench/modelpool/huggingface_gpt2_classification.py +63 -0
  201. fusion_bench/modelpool/nyuv2_modelpool.py +40 -0
  202. fusion_bench/modelpool/seq2seq_lm/__init__.py +2 -0
  203. fusion_bench/modelpool/seq2seq_lm/modelpool.py +65 -0
  204. fusion_bench/modelpool/seq_classification_lm/__init__.py +2 -0
  205. fusion_bench/modelpool/seq_classification_lm/reward_model.py +15 -0
  206. fusion_bench/modelpool/seq_classification_lm/seq_classification_lm.py +98 -0
  207. fusion_bench/models/__init__.py +3 -0
  208. fusion_bench/models/chat_templates/__init__.py +1 -0
  209. fusion_bench/models/chat_templates/llama_3_Instruct.py +1 -0
  210. fusion_bench/models/chat_templates/load_tokenizer.py +43 -0
  211. fusion_bench/models/hf_clip.py +199 -0
  212. fusion_bench/models/linearized/__init__.py +0 -0
  213. fusion_bench/models/linearized/linearized_model_utils.py +91 -0
  214. fusion_bench/models/linearized/vision_model.py +122 -0
  215. fusion_bench/models/llama/__init__.py +16 -0
  216. fusion_bench/models/llama/model_utils/__init__.py +0 -0
  217. fusion_bench/models/llama/model_utils/embedding.py +87 -0
  218. fusion_bench/models/llama/model_utils/liger_kernel.py +86 -0
  219. fusion_bench/models/llama/model_utils/misc.py +112 -0
  220. fusion_bench/models/llama/model_utils/mod.py +52 -0
  221. fusion_bench/models/llama/model_utils/visual.py +241 -0
  222. fusion_bench/models/llama/patcher.py +78 -0
  223. fusion_bench/models/llama/tokenizer_loader.py +153 -0
  224. fusion_bench/models/masks/__init__.py +2 -0
  225. fusion_bench/models/masks/mask_model.py +160 -0
  226. fusion_bench/models/modeling_losparse_llama/__init__.py +4 -0
  227. fusion_bench/models/modeling_losparse_llama/configuration_losparse_llama.py +205 -0
  228. fusion_bench/models/modeling_losparse_llama/losparse_linear.py +67 -0
  229. fusion_bench/models/modeling_losparse_llama/modeling_losparse_llama.py +1825 -0
  230. fusion_bench/models/modeling_losparse_llama/register.py +8 -0
  231. fusion_bench/models/modeling_losparse_llama/utils.py +60 -0
  232. fusion_bench/models/modeling_smile_mistral/__init__.py +48 -0
  233. fusion_bench/models/modeling_smile_mistral/configuration_smile_mistral.py +21 -0
  234. fusion_bench/models/modeling_smile_mistral/modeling_smile_mistral.py +1034 -0
  235. fusion_bench/models/modeling_smile_mistral/register.py +8 -0
  236. fusion_bench/models/nyuv2/__init__.py +0 -0
  237. fusion_bench/models/nyuv2/aspp.py +82 -0
  238. fusion_bench/models/nyuv2/lightning_module.py +176 -0
  239. fusion_bench/models/nyuv2/resnet.py +405 -0
  240. fusion_bench/models/nyuv2/resnet_dilated.py +99 -0
  241. fusion_bench/models/parameter_dict.py +75 -0
  242. fusion_bench/models/rankone_moe.py +410 -0
  243. fusion_bench/models/separate_io.py +105 -0
  244. fusion_bench/models/smile_moe/__init__.py +0 -0
  245. fusion_bench/models/smile_moe/linear.py +256 -0
  246. fusion_bench/models/sparse_we_moe.py +459 -0
  247. fusion_bench/models/surgery/__init__.py +1 -0
  248. fusion_bench/models/surgery/surgerymodelwrapper.py +158 -0
  249. fusion_bench/models/utils.py +80 -0
  250. fusion_bench/models/we_moe.py +247 -0
  251. fusion_bench/models/wrappers/__init__.py +0 -0
  252. fusion_bench/models/wrappers/ensemble.py +183 -0
  253. fusion_bench/models/wrappers/layer_wise_fusion.py +336 -0
  254. fusion_bench/models/wrappers/task_wise_fusion.py +249 -0
  255. fusion_bench/optim/__init__.py +2 -0
  256. fusion_bench/optim/exception.py +47 -0
  257. fusion_bench/optim/lr_scheduler/__init__.py +1 -0
  258. fusion_bench/optim/lr_scheduler/linear_warmup.py +222 -0
  259. fusion_bench/optim/lr_scheduler/utils/__init__.py +1 -0
  260. fusion_bench/optim/lr_scheduler/utils/visualization.py +119 -0
  261. fusion_bench/optim/mezo.py +118 -0
  262. fusion_bench/programs/__init__.py +20 -0
  263. fusion_bench/programs/base_program.py +9 -0
  264. fusion_bench/programs/fabric_fusion_program.py +299 -0
  265. fusion_bench/scripts/__init__.py +0 -0
  266. fusion_bench/scripts/cli.py +43 -0
  267. fusion_bench/scripts/clip/__init__.py +0 -0
  268. fusion_bench/scripts/clip/convert_checkpoint.py +39 -0
  269. fusion_bench/scripts/imgui.py +218 -0
  270. fusion_bench/scripts/nyuv2_mtl_train.py +137 -0
  271. fusion_bench/scripts/webui.py +405 -0
  272. fusion_bench/taskpool/__init__.py +39 -0
  273. fusion_bench/taskpool/base_pool.py +35 -0
  274. fusion_bench/taskpool/clip_vision/__init__.py +4 -0
  275. fusion_bench/taskpool/clip_vision/clip_rankone_moe_taskpool.py +112 -0
  276. fusion_bench/taskpool/clip_vision/clip_sparse_wemoe_taskpool.py +120 -0
  277. fusion_bench/taskpool/clip_vision/taskpool.py +392 -0
  278. fusion_bench/taskpool/dummy.py +58 -0
  279. fusion_bench/taskpool/gpt2_text_classification.py +149 -0
  280. fusion_bench/taskpool/llama/__init__.py +1 -0
  281. fusion_bench/taskpool/llama/reward_model.py +157 -0
  282. fusion_bench/taskpool/llama/test_generation.py +185 -0
  283. fusion_bench/taskpool/nyuv2_taskpool.py +65 -0
  284. fusion_bench/tasks/__init__.py +2 -0
  285. fusion_bench/tasks/base_task.py +18 -0
  286. fusion_bench/tasks/classification.py +75 -0
  287. fusion_bench/tasks/clip_classification/__init__.py +183 -0
  288. fusion_bench/tasks/clip_classification/cifar10.py +33 -0
  289. fusion_bench/tasks/clip_classification/cifar100.py +146 -0
  290. fusion_bench/tasks/clip_classification/clip_dataset.py +1 -0
  291. fusion_bench/tasks/clip_classification/cub_200_2011.py +208 -0
  292. fusion_bench/tasks/clip_classification/dtd.py +60 -0
  293. fusion_bench/tasks/clip_classification/emnist_letters.py +31 -0
  294. fusion_bench/tasks/clip_classification/emnist_mnist.py +5 -0
  295. fusion_bench/tasks/clip_classification/eurosat.py +18 -0
  296. fusion_bench/tasks/clip_classification/fashion_mnist.py +18 -0
  297. fusion_bench/tasks/clip_classification/fer2013.py +18 -0
  298. fusion_bench/tasks/clip_classification/flower102.py +106 -0
  299. fusion_bench/tasks/clip_classification/food101.py +105 -0
  300. fusion_bench/tasks/clip_classification/gtsrb.py +51 -0
  301. fusion_bench/tasks/clip_classification/imagenet.py +2103 -0
  302. fusion_bench/tasks/clip_classification/kmnist.py +17 -0
  303. fusion_bench/tasks/clip_classification/mnist.py +5 -0
  304. fusion_bench/tasks/clip_classification/mongo_leaf_disease.py +19 -0
  305. fusion_bench/tasks/clip_classification/oxford_iiit_pet.py +41 -0
  306. fusion_bench/tasks/clip_classification/pcam.py +5 -0
  307. fusion_bench/tasks/clip_classification/rendered_sst2.py +3 -0
  308. fusion_bench/tasks/clip_classification/resisc45.py +68 -0
  309. fusion_bench/tasks/clip_classification/stanford_cars.py +209 -0
  310. fusion_bench/tasks/clip_classification/stl10.py +17 -0
  311. fusion_bench/tasks/clip_classification/sun397.py +404 -0
  312. fusion_bench/tasks/clip_classification/svhn.py +5 -0
  313. fusion_bench/tasks/clip_classification/tiny_imagenet.py +208 -0
  314. fusion_bench/tasks/flan_t5_text_generation/__init__.py +0 -0
  315. fusion_bench/tasks/flan_t5_text_generation/datasets_preprocess.py +71 -0
  316. fusion_bench/tasks/flan_t5_text_generation/glue_evaluation.py +132 -0
  317. fusion_bench/tasks/flan_t5_text_generation/glue_load_dataset.py +64 -0
  318. fusion_bench/tasks/flan_t5_text_generation/glue_preprocessors.py +379 -0
  319. fusion_bench/tasks/flan_t5_text_generation/glue_prompt_templates.py +52 -0
  320. fusion_bench/utils/__init__.py +14 -0
  321. fusion_bench/utils/auto.py +31 -0
  322. fusion_bench/utils/cache_utils.py +58 -0
  323. fusion_bench/utils/data.py +165 -0
  324. fusion_bench/utils/devices.py +231 -0
  325. fusion_bench/utils/dict.py +43 -0
  326. fusion_bench/utils/dtype.py +146 -0
  327. fusion_bench/utils/expr.py +90 -0
  328. fusion_bench/utils/fabric.py +17 -0
  329. fusion_bench/utils/functools.py +37 -0
  330. fusion_bench/utils/hydra_utils.py +28 -0
  331. fusion_bench/utils/instantiate.py +450 -0
  332. fusion_bench/utils/json.py +93 -0
  333. fusion_bench/utils/lazy_imports.py +74 -0
  334. fusion_bench/utils/misc.py +18 -0
  335. fusion_bench/utils/packages.py +84 -0
  336. fusion_bench/utils/parameters.py +323 -0
  337. fusion_bench/utils/path.py +22 -0
  338. fusion_bench/utils/plot/__init__.py +0 -0
  339. fusion_bench/utils/plot/color_data.py +1726 -0
  340. fusion_bench/utils/plot/token.py +52 -0
  341. fusion_bench/utils/plot/token_notebook.py +127 -0
  342. fusion_bench/utils/pylogger.py +55 -0
  343. fusion_bench/utils/rich_utils.py +201 -0
  344. fusion_bench/utils/set.py +8 -0
  345. fusion_bench/utils/state_dict_arithmetic.py +297 -0
  346. fusion_bench/utils/strenum/__init__.py +326 -0
  347. fusion_bench/utils/strenum/_name_mangler.py +127 -0
  348. fusion_bench/utils/strenum/_version.py +556 -0
  349. fusion_bench/utils/tensorboard.py +51 -0
  350. fusion_bench/utils/timer.py +49 -0
  351. fusion_bench/utils/type.py +34 -0
  352. fusion_bench-0.2.9.dist-info/LICENSE +21 -0
  353. fusion_bench-0.2.9.dist-info/METADATA +258 -0
  354. fusion_bench-0.2.9.dist-info/RECORD +727 -0
  355. fusion_bench-0.2.9.dist-info/WHEEL +5 -0
  356. fusion_bench-0.2.9.dist-info/entry_points.txt +3 -0
  357. fusion_bench-0.2.9.dist-info/top_level.txt +1 -0
  358. fusion_bench_config/README.md +12 -0
  359. fusion_bench_config/clip-vit-base-patch32_robustness_corrupted.yaml +23 -0
  360. fusion_bench_config/dataset/image_classification/README.md +6 -0
  361. fusion_bench_config/dataset/image_classification/test/TALL14.yaml +20 -0
  362. fusion_bench_config/dataset/image_classification/test/TALL20.yaml +28 -0
  363. fusion_bench_config/dataset/image_classification/test/cifar10.yaml +4 -0
  364. fusion_bench_config/dataset/image_classification/test/cifar100.yaml +4 -0
  365. fusion_bench_config/dataset/image_classification/test/cub-200-2011.yaml +4 -0
  366. fusion_bench_config/dataset/image_classification/test/dtd.yaml +4 -0
  367. fusion_bench_config/dataset/image_classification/test/emnist_letters.yaml +5 -0
  368. fusion_bench_config/dataset/image_classification/test/emnist_mnist.yaml +4 -0
  369. fusion_bench_config/dataset/image_classification/test/eurosat.yaml +4 -0
  370. fusion_bench_config/dataset/image_classification/test/fashion_mnist.yaml +4 -0
  371. fusion_bench_config/dataset/image_classification/test/fer2013.yaml +3 -0
  372. fusion_bench_config/dataset/image_classification/test/food101.yaml +4 -0
  373. fusion_bench_config/dataset/image_classification/test/gtsrb.yaml +4 -0
  374. fusion_bench_config/dataset/image_classification/test/kmnist.yaml +4 -0
  375. fusion_bench_config/dataset/image_classification/test/mango-leaf-disease.yaml +4 -0
  376. fusion_bench_config/dataset/image_classification/test/mnist.yaml +4 -0
  377. fusion_bench_config/dataset/image_classification/test/oxford-iiit-pet.yaml +4 -0
  378. fusion_bench_config/dataset/image_classification/test/oxford_flowers102.yaml +4 -0
  379. fusion_bench_config/dataset/image_classification/test/pcam.yaml +4 -0
  380. fusion_bench_config/dataset/image_classification/test/rendered-sst2.yaml +4 -0
  381. fusion_bench_config/dataset/image_classification/test/resisc45.yaml +4 -0
  382. fusion_bench_config/dataset/image_classification/test/stanford-cars.yaml +4 -0
  383. fusion_bench_config/dataset/image_classification/test/stl10.yaml +4 -0
  384. fusion_bench_config/dataset/image_classification/test/sun397.yaml +4 -0
  385. fusion_bench_config/dataset/image_classification/test/svhn.yaml +6 -0
  386. fusion_bench_config/dataset/image_classification/test/the_eight_tasks.yaml +9 -0
  387. fusion_bench_config/dataset/image_classification/test/tiny-imagenet.yaml +4 -0
  388. fusion_bench_config/dataset/image_classification/train/TALL14.yaml +20 -0
  389. fusion_bench_config/dataset/image_classification/train/TALL20.yaml +28 -0
  390. fusion_bench_config/dataset/image_classification/train/cifar10.yaml +4 -0
  391. fusion_bench_config/dataset/image_classification/train/cifar100.yaml +4 -0
  392. fusion_bench_config/dataset/image_classification/train/cub-200-2011.yaml +4 -0
  393. fusion_bench_config/dataset/image_classification/train/dtd.yaml +4 -0
  394. fusion_bench_config/dataset/image_classification/train/emnist_letters.yaml +4 -0
  395. fusion_bench_config/dataset/image_classification/train/emnist_mnist.yaml +4 -0
  396. fusion_bench_config/dataset/image_classification/train/eurosat.yaml +4 -0
  397. fusion_bench_config/dataset/image_classification/train/fashion_mnist.yaml +4 -0
  398. fusion_bench_config/dataset/image_classification/train/fer2013.yaml +3 -0
  399. fusion_bench_config/dataset/image_classification/train/food101.yaml +4 -0
  400. fusion_bench_config/dataset/image_classification/train/gtsrb.yaml +4 -0
  401. fusion_bench_config/dataset/image_classification/train/kmnist.yaml +4 -0
  402. fusion_bench_config/dataset/image_classification/train/mango-leaf-disease.yaml +4 -0
  403. fusion_bench_config/dataset/image_classification/train/mnist.yaml +4 -0
  404. fusion_bench_config/dataset/image_classification/train/oxford-iiit-pet.yaml +4 -0
  405. fusion_bench_config/dataset/image_classification/train/oxford_flowers102.yaml +4 -0
  406. fusion_bench_config/dataset/image_classification/train/pcam.yaml +4 -0
  407. fusion_bench_config/dataset/image_classification/train/rendered-sst2.yaml +4 -0
  408. fusion_bench_config/dataset/image_classification/train/resisc45.yaml +4 -0
  409. fusion_bench_config/dataset/image_classification/train/stanford-cars.yaml +4 -0
  410. fusion_bench_config/dataset/image_classification/train/stl10.yaml +4 -0
  411. fusion_bench_config/dataset/image_classification/train/sun397.yaml +4 -0
  412. fusion_bench_config/dataset/image_classification/train/svhn.yaml +6 -0
  413. fusion_bench_config/dataset/image_classification/train/the_eight_tasks.yaml +9 -0
  414. fusion_bench_config/dataset/image_classification/train/tiny-imagenet.yaml +4 -0
  415. fusion_bench_config/dataset/image_classification/val/dtd.yaml +10 -0
  416. fusion_bench_config/dataset/image_classification/val/eurosat.yaml +10 -0
  417. fusion_bench_config/dataset/image_classification/val/gtsrb.yaml +10 -0
  418. fusion_bench_config/dataset/image_classification/val/mnist.yaml +10 -0
  419. fusion_bench_config/dataset/image_classification/val/resisc45.yaml +10 -0
  420. fusion_bench_config/dataset/image_classification/val/stanford-cars.yaml +10 -0
  421. fusion_bench_config/dataset/image_classification/val/sun397.yaml +10 -0
  422. fusion_bench_config/dataset/image_classification/val/svhn.yaml +12 -0
  423. fusion_bench_config/dataset/image_classification/val/the_eight_tasks.yaml +9 -0
  424. fusion_bench_config/dataset/llm_sft/alpaca_cleaned.yaml +6 -0
  425. fusion_bench_config/dataset/llm_sft/ultrachat_200k.yaml +3 -0
  426. fusion_bench_config/dataset/question_answering/search_qa.yaml +6 -0
  427. fusion_bench_config/dataset/question_answering/test/search_qa.yaml +7 -0
  428. fusion_bench_config/dataset/question_answering/train/MetaMathQA.yaml +4 -0
  429. fusion_bench_config/dataset/question_answering/train/search_qa.yaml +7 -0
  430. fusion_bench_config/dataset/question_answering/val/search_qa.yaml +7 -0
  431. fusion_bench_config/dataset/summarization/test/xsum.yaml +4 -0
  432. fusion_bench_config/dataset/summarization/train/xsum.yaml +4 -0
  433. fusion_bench_config/dataset/summarization/val/xsum.yaml +4 -0
  434. fusion_bench_config/dataset/summarization/xsum.yaml +3 -0
  435. fusion_bench_config/dataset/text_generation/test/gsm-hard.yaml +4 -0
  436. fusion_bench_config/dataset/text_generation/test/gsm8k.yaml +5 -0
  437. fusion_bench_config/dataset/text_generation/test/gsm8k_question_label.yaml +3 -0
  438. fusion_bench_config/dataset/text_generation/train/CodeAlpaca-20k.yaml +4 -0
  439. fusion_bench_config/dataset/text_generation/train/gsm8k.yaml +5 -0
  440. fusion_bench_config/dataset/text_generation/train/gsm8k_question_label.yaml +3 -0
  441. fusion_bench_config/fabric/auto.yaml +16 -0
  442. fusion_bench_config/fabric/llama_ddp.yaml +18 -0
  443. fusion_bench_config/fabric/llama_fsdp.yaml +16 -0
  444. fusion_bench_config/fabric/llama_peft_fsdp.yaml +16 -0
  445. fusion_bench_config/fabric/loggers/csv_logger.yaml +11 -0
  446. fusion_bench_config/fabric/loggers/tensorboard_logger.yaml +11 -0
  447. fusion_bench_config/fabric/loggers/wandb_logger.yaml +2 -0
  448. fusion_bench_config/fabric/strategy/deepspeed.yaml +10 -0
  449. fusion_bench_config/fabric/strategy/llama_fsdp.yaml +8 -0
  450. fusion_bench_config/fabric/strategy/llama_peft_fsdp.yaml +9 -0
  451. fusion_bench_config/fabric_model_fusion.yaml +20 -0
  452. fusion_bench_config/hydra/default.yaml +8 -0
  453. fusion_bench_config/hydra/help/fusion_bench_help.yaml +47 -0
  454. fusion_bench_config/hydra/job_logging/rich_logging.yaml +20 -0
  455. fusion_bench_config/llama_full_finetune.yaml +19 -0
  456. fusion_bench_config/llama_magnitude_pruning.yaml +16 -0
  457. fusion_bench_config/llama_model_fusion.yaml +17 -0
  458. fusion_bench_config/method/ada_svd/clip_vision.yaml +9 -0
  459. fusion_bench_config/method/adamerging/clip.yaml +23 -0
  460. fusion_bench_config/method/adamerging/layer_wise_flan_t5.yaml +23 -0
  461. fusion_bench_config/method/adamerging/layer_wise_gpt2.yaml +23 -0
  462. fusion_bench_config/method/adamerging/llama_sft.yaml +33 -0
  463. fusion_bench_config/method/adamerging.yaml +23 -0
  464. fusion_bench_config/method/analysis/task_vector_cos_similarity.yaml +6 -0
  465. fusion_bench_config/method/analysis/task_vector_violin_plot.yaml +6 -0
  466. fusion_bench_config/method/classification/clip_continual_finetune.yaml +28 -0
  467. fusion_bench_config/method/classification/clip_finetune.yaml +26 -0
  468. fusion_bench_config/method/clip_finetune.yaml +26 -0
  469. fusion_bench_config/method/concrete_subspace/clip_concrete_layer_wise_adamerging.yaml +27 -0
  470. fusion_bench_config/method/concrete_subspace/clip_concrete_task_arithmetic.yaml +25 -0
  471. fusion_bench_config/method/concrete_subspace/clip_concrete_task_wise_adamerging.yaml +27 -0
  472. fusion_bench_config/method/dare/simple_average.yaml +5 -0
  473. fusion_bench_config/method/dare/task_arithmetic.yaml +6 -0
  474. fusion_bench_config/method/dare/ties_merging.yaml +15 -0
  475. fusion_bench_config/method/dawe/dawe_for_clip.yaml +32 -0
  476. fusion_bench_config/method/depth_upscaling.yaml +5 -0
  477. fusion_bench_config/method/dummy.yaml +1 -0
  478. fusion_bench_config/method/ensemble/max_model_predictor.yaml +1 -0
  479. fusion_bench_config/method/ensemble/simple_ensemble.yaml +2 -0
  480. fusion_bench_config/method/ensemble/weighted_ensemble.yaml +6 -0
  481. fusion_bench_config/method/fisher_merging/clip_fisher_merging.yaml +13 -0
  482. fusion_bench_config/method/fisher_merging/fisher_merging.yaml +9 -0
  483. fusion_bench_config/method/fisher_merging/gpt2_fisher_merging.yaml +12 -0
  484. fusion_bench_config/method/linear/expo.yaml +8 -0
  485. fusion_bench_config/method/linear/linear_interpolation.yaml +3 -0
  486. fusion_bench_config/method/linear/llama_expo.yaml +19 -0
  487. fusion_bench_config/method/linear/llama_expo_with_dare.yaml +19 -0
  488. fusion_bench_config/method/linear/simple_average_for_llama.yaml +5 -0
  489. fusion_bench_config/method/linear/task_arithmetic_for_llama.yaml +4 -0
  490. fusion_bench_config/method/linear/weighted_average.yaml +6 -0
  491. fusion_bench_config/method/linear/weighted_average_for_llama.yaml +12 -0
  492. fusion_bench_config/method/lm_finetune/bradley_terry_rm.yaml +47 -0
  493. fusion_bench_config/method/lm_finetune/fullfinetune_sft.yaml +47 -0
  494. fusion_bench_config/method/lm_finetune/peftfinetune_sft.yaml +63 -0
  495. fusion_bench_config/method/mixtral_moe_merging.yaml +4 -0
  496. fusion_bench_config/method/mixtral_moe_upscaling.yaml +7 -0
  497. fusion_bench_config/method/model_recombination.yaml +4 -0
  498. fusion_bench_config/method/opcm/opcm.yaml +12 -0
  499. fusion_bench_config/method/opcm/task_arithmetic.yaml +12 -0
  500. fusion_bench_config/method/opcm/ties_merging.yaml +18 -0
  501. fusion_bench_config/method/opcm/weight_average.yaml +10 -0
  502. fusion_bench_config/method/pruning/llama_magnitude_pruning.yaml +14 -0
  503. fusion_bench_config/method/pruning/llama_random_pruning.yaml +9 -0
  504. fusion_bench_config/method/pruning/llama_wanda_pruning.yaml +16 -0
  505. fusion_bench_config/method/pruning/magnitude_diff_pruning.yaml +5 -0
  506. fusion_bench_config/method/pwe_moe_ls_for_clip.yaml +22 -0
  507. fusion_bench_config/method/rankone_moe/rankone_moe.yaml +26 -0
  508. fusion_bench_config/method/regmean/clip_regmean.yaml +11 -0
  509. fusion_bench_config/method/regmean/gpt2_regmean.yaml +12 -0
  510. fusion_bench_config/method/regmean/regmean.yaml +4 -0
  511. fusion_bench_config/method/simple_average.yaml +1 -0
  512. fusion_bench_config/method/slerp/slerp.yaml +6 -0
  513. fusion_bench_config/method/smile_upscaling/singular_projection_merging.yaml +8 -0
  514. fusion_bench_config/method/smile_upscaling/smile_mistral_upscaling.yaml +10 -0
  515. fusion_bench_config/method/smile_upscaling/smile_upscaling.yaml +14 -0
  516. fusion_bench_config/method/sparselo_pruning/llama_iterative_sparselo.yaml +20 -0
  517. fusion_bench_config/method/sparselo_pruning/llama_pcp_sparselo.yaml +20 -0
  518. fusion_bench_config/method/sparselo_pruning/llama_sparselo.yaml +19 -0
  519. fusion_bench_config/method/surgery/adamerging_surgery.yaml +27 -0
  520. fusion_bench_config/method/task_arithmetic.yaml +2 -0
  521. fusion_bench_config/method/task_singular_vector/TaskSingularVectorMerging.yaml +2 -0
  522. fusion_bench_config/method/ties_merging.yaml +8 -0
  523. fusion_bench_config/method/trust_region/clip_task_arithmetic.yaml +7 -0
  524. fusion_bench_config/method/wemoe/sparse_weight_ensembling_moe.yaml +39 -0
  525. fusion_bench_config/method/wemoe/weight_ensembling_moe.yaml +20 -0
  526. fusion_bench_config/model/clip-vit/README.md +38 -0
  527. fusion_bench_config/model/clip-vit/clip-vit-base-patch16.yaml +1 -0
  528. fusion_bench_config/model/clip-vit/clip-vit-base-patch16_TALL14.yaml +22 -0
  529. fusion_bench_config/model/clip-vit/clip-vit-base-patch16_TALL20.yaml +29 -0
  530. fusion_bench_config/model/clip-vit/clip-vit-base-patch16_cifar10.yaml +1 -0
  531. fusion_bench_config/model/clip-vit/clip-vit-base-patch16_cifar100.yaml +1 -0
  532. fusion_bench_config/model/clip-vit/clip-vit-base-patch16_dtd.yaml +1 -0
  533. fusion_bench_config/model/clip-vit/clip-vit-base-patch16_eight_tasks.yaml +10 -0
  534. fusion_bench_config/model/clip-vit/clip-vit-base-patch16_emnist_letters.yaml +1 -0
  535. fusion_bench_config/model/clip-vit/clip-vit-base-patch16_eurosat.yaml +1 -0
  536. fusion_bench_config/model/clip-vit/clip-vit-base-patch16_fashion_mnist.yaml +1 -0
  537. fusion_bench_config/model/clip-vit/clip-vit-base-patch16_fer2013.yaml +1 -0
  538. fusion_bench_config/model/clip-vit/clip-vit-base-patch16_food101.yaml +1 -0
  539. fusion_bench_config/model/clip-vit/clip-vit-base-patch16_gtsrb.yaml +1 -0
  540. fusion_bench_config/model/clip-vit/clip-vit-base-patch16_kmnist.yaml +1 -0
  541. fusion_bench_config/model/clip-vit/clip-vit-base-patch16_mnist.yaml +1 -0
  542. fusion_bench_config/model/clip-vit/clip-vit-base-patch16_oxford-iiit-pet.yaml +1 -0
  543. fusion_bench_config/model/clip-vit/clip-vit-base-patch16_oxford_flowers102.yaml +1 -0
  544. fusion_bench_config/model/clip-vit/clip-vit-base-patch16_pcam.yaml +1 -0
  545. fusion_bench_config/model/clip-vit/clip-vit-base-patch16_rendered-sst2.yaml +1 -0
  546. fusion_bench_config/model/clip-vit/clip-vit-base-patch16_resisc45.yaml +1 -0
  547. fusion_bench_config/model/clip-vit/clip-vit-base-patch16_stanford-cars.yaml +1 -0
  548. fusion_bench_config/model/clip-vit/clip-vit-base-patch16_stl10.yaml +1 -0
  549. fusion_bench_config/model/clip-vit/clip-vit-base-patch16_sun397.yaml +1 -0
  550. fusion_bench_config/model/clip-vit/clip-vit-base-patch16_svhn.yaml +1 -0
  551. fusion_bench_config/model/clip-vit/clip-vit-base-patch32.yaml +1 -0
  552. fusion_bench_config/model/clip-vit/clip-vit-base-patch32_TALL14.yaml +22 -0
  553. fusion_bench_config/model/clip-vit/clip-vit-base-patch32_TALL20.yaml +29 -0
  554. fusion_bench_config/model/clip-vit/clip-vit-base-patch32_cifar10.yaml +1 -0
  555. fusion_bench_config/model/clip-vit/clip-vit-base-patch32_cifar100.yaml +1 -0
  556. fusion_bench_config/model/clip-vit/clip-vit-base-patch32_dtd.yaml +1 -0
  557. fusion_bench_config/model/clip-vit/clip-vit-base-patch32_eight_tasks.yaml +11 -0
  558. fusion_bench_config/model/clip-vit/clip-vit-base-patch32_emnist_letters.yaml +1 -0
  559. fusion_bench_config/model/clip-vit/clip-vit-base-patch32_eurosat.yaml +1 -0
  560. fusion_bench_config/model/clip-vit/clip-vit-base-patch32_fashion_mnist.yaml +1 -0
  561. fusion_bench_config/model/clip-vit/clip-vit-base-patch32_fer2013.yaml +1 -0
  562. fusion_bench_config/model/clip-vit/clip-vit-base-patch32_food101.yaml +1 -0
  563. fusion_bench_config/model/clip-vit/clip-vit-base-patch32_gtsrb.yaml +1 -0
  564. fusion_bench_config/model/clip-vit/clip-vit-base-patch32_kmnist.yaml +1 -0
  565. fusion_bench_config/model/clip-vit/clip-vit-base-patch32_mnist.yaml +1 -0
  566. fusion_bench_config/model/clip-vit/clip-vit-base-patch32_oxford-iiit-pet.yaml +1 -0
  567. fusion_bench_config/model/clip-vit/clip-vit-base-patch32_oxford_flowers102.yaml +1 -0
  568. fusion_bench_config/model/clip-vit/clip-vit-base-patch32_pcam.yaml +1 -0
  569. fusion_bench_config/model/clip-vit/clip-vit-base-patch32_rendered-sst2.yaml +1 -0
  570. fusion_bench_config/model/clip-vit/clip-vit-base-patch32_resisc45.yaml +1 -0
  571. fusion_bench_config/model/clip-vit/clip-vit-base-patch32_stanford-cars.yaml +1 -0
  572. fusion_bench_config/model/clip-vit/clip-vit-base-patch32_stl10.yaml +1 -0
  573. fusion_bench_config/model/clip-vit/clip-vit-base-patch32_sun397.yaml +1 -0
  574. fusion_bench_config/model/clip-vit/clip-vit-base-patch32_svhn.yaml +1 -0
  575. fusion_bench_config/model/clip-vit/clip-vit-large-patch14.yaml +1 -0
  576. fusion_bench_config/model/clip-vit/clip-vit-large-patch14_TALL14.yaml +22 -0
  577. fusion_bench_config/model/clip-vit/clip-vit-large-patch14_TALL20.yaml +29 -0
  578. fusion_bench_config/model/clip-vit/clip-vit-large-patch14_cifar10.yaml +1 -0
  579. fusion_bench_config/model/clip-vit/clip-vit-large-patch14_cifar100.yaml +1 -0
  580. fusion_bench_config/model/clip-vit/clip-vit-large-patch14_dtd.yaml +1 -0
  581. fusion_bench_config/model/clip-vit/clip-vit-large-patch14_eight_tasks.yaml +10 -0
  582. fusion_bench_config/model/clip-vit/clip-vit-large-patch14_emnist_letters.yaml +1 -0
  583. fusion_bench_config/model/clip-vit/clip-vit-large-patch14_eurosat.yaml +1 -0
  584. fusion_bench_config/model/clip-vit/clip-vit-large-patch14_fashion_mnist.yaml +1 -0
  585. fusion_bench_config/model/clip-vit/clip-vit-large-patch14_fer2013.yaml +1 -0
  586. fusion_bench_config/model/clip-vit/clip-vit-large-patch14_food101.yaml +1 -0
  587. fusion_bench_config/model/clip-vit/clip-vit-large-patch14_gtsrb.yaml +1 -0
  588. fusion_bench_config/model/clip-vit/clip-vit-large-patch14_kmnist.yaml +1 -0
  589. fusion_bench_config/model/clip-vit/clip-vit-large-patch14_mnist.yaml +1 -0
  590. fusion_bench_config/model/clip-vit/clip-vit-large-patch14_oxford-iiit-pet.yaml +1 -0
  591. fusion_bench_config/model/clip-vit/clip-vit-large-patch14_oxford_flowers102.yaml +1 -0
  592. fusion_bench_config/model/clip-vit/clip-vit-large-patch14_pcam.yaml +1 -0
  593. fusion_bench_config/model/clip-vit/clip-vit-large-patch14_rendered-sst2.yaml +1 -0
  594. fusion_bench_config/model/clip-vit/clip-vit-large-patch14_resisc45.yaml +1 -0
  595. fusion_bench_config/model/clip-vit/clip-vit-large-patch14_stanford-cars.yaml +1 -0
  596. fusion_bench_config/model/clip-vit/clip-vit-large-patch14_stl10.yaml +1 -0
  597. fusion_bench_config/model/clip-vit/clip-vit-large-patch14_sun397.yaml +1 -0
  598. fusion_bench_config/model/clip-vit/clip-vit-large-patch14_svhn.yaml +1 -0
  599. fusion_bench_config/model/clip-vit/download_TALL20_models.sh +6 -0
  600. fusion_bench_config/model/clip-vit/generate_vit_model_config.sh +23 -0
  601. fusion_bench_config/model/flan-t5/flan-t5-base.yaml +3 -0
  602. fusion_bench_config/model/flan-t5/flan-t5-base_glue-cola.yaml +3 -0
  603. fusion_bench_config/model/flan-t5/flan-t5-base_glue-cola_lora-16.yaml +4 -0
  604. fusion_bench_config/model/flan-t5/flan-t5-base_glue-mnli.yaml +3 -0
  605. fusion_bench_config/model/flan-t5/flan-t5-base_glue-mnli_lora-16.yaml +4 -0
  606. fusion_bench_config/model/flan-t5/flan-t5-base_glue-mrpc.yaml +3 -0
  607. fusion_bench_config/model/flan-t5/flan-t5-base_glue-mrpc_lora-16.yaml +4 -0
  608. fusion_bench_config/model/flan-t5/flan-t5-base_glue-qnli.yaml +3 -0
  609. fusion_bench_config/model/flan-t5/flan-t5-base_glue-qnli_lora-16.yaml +4 -0
  610. fusion_bench_config/model/flan-t5/flan-t5-base_glue-qqp.yaml +3 -0
  611. fusion_bench_config/model/flan-t5/flan-t5-base_glue-qqp_lora-16.yaml +4 -0
  612. fusion_bench_config/model/flan-t5/flan-t5-base_glue-rte.yaml +3 -0
  613. fusion_bench_config/model/flan-t5/flan-t5-base_glue-rte_lora-16.yaml +4 -0
  614. fusion_bench_config/model/flan-t5/flan-t5-base_glue-sst2.yaml +3 -0
  615. fusion_bench_config/model/flan-t5/flan-t5-base_glue-sst2_lora-16.yaml +4 -0
  616. fusion_bench_config/model/flan-t5/flan-t5-base_glue-stsb.yaml +3 -0
  617. fusion_bench_config/model/flan-t5/flan-t5-base_glue-stsb_lora-16.yaml +4 -0
  618. fusion_bench_config/model/flan-t5/flan-t5-large.yaml +3 -0
  619. fusion_bench_config/model/flan-t5/flan-t5-large_glue-cola_lora-16.yaml +4 -0
  620. fusion_bench_config/model/flan-t5/flan-t5-large_glue-mnli_lora-16.yaml +4 -0
  621. fusion_bench_config/model/flan-t5/flan-t5-large_glue-mrpc_lora-16.yaml +4 -0
  622. fusion_bench_config/model/flan-t5/flan-t5-large_glue-qnli_lora-16.yaml +4 -0
  623. fusion_bench_config/model/flan-t5/flan-t5-large_glue-qqp_lora-16.yaml +4 -0
  624. fusion_bench_config/model/flan-t5/flan-t5-large_glue-rte_lora-16.yaml +4 -0
  625. fusion_bench_config/model/flan-t5/flan-t5-large_glue-sst2_lora-16.yaml +4 -0
  626. fusion_bench_config/model/flan-t5/flan-t5-large_glue-stsb_lora-16.yaml +4 -0
  627. fusion_bench_config/model/flan-t5/generate_flan-t5.sh +38 -0
  628. fusion_bench_config/modelpool/CLIPVisionModelPool/_template.yaml +12 -0
  629. fusion_bench_config/modelpool/CLIPVisionModelPool/clip-vit-base-patch16_TA8.yaml +8 -0
  630. fusion_bench_config/modelpool/CLIPVisionModelPool/clip-vit-base-patch16_TA8_lora.yaml +53 -0
  631. fusion_bench_config/modelpool/CLIPVisionModelPool/clip-vit-base-patch16_TA8_model_only.yaml +6 -0
  632. fusion_bench_config/modelpool/CLIPVisionModelPool/clip-vit-base-patch16_TALL14.yaml +11 -0
  633. fusion_bench_config/modelpool/CLIPVisionModelPool/clip-vit-base-patch16_TALL14_model_only.yaml +9 -0
  634. fusion_bench_config/modelpool/CLIPVisionModelPool/clip-vit-base-patch16_TALL20.yaml +11 -0
  635. fusion_bench_config/modelpool/CLIPVisionModelPool/clip-vit-base-patch16_TALL20_model_only.yaml +9 -0
  636. fusion_bench_config/modelpool/CLIPVisionModelPool/clip-vit-base-patch16_individual.yaml +19 -0
  637. fusion_bench_config/modelpool/CLIPVisionModelPool/clip-vit-base-patch16_individual_lora.yaml +14 -0
  638. fusion_bench_config/modelpool/CLIPVisionModelPool/clip-vit-base-patch32_TA8.yaml +5 -0
  639. fusion_bench_config/modelpool/CLIPVisionModelPool/clip-vit-base-patch32_TA8_control_task.yaml +24 -0
  640. fusion_bench_config/modelpool/CLIPVisionModelPool/clip-vit-base-patch32_TA8_model_only.yaml +3 -0
  641. fusion_bench_config/modelpool/CLIPVisionModelPool/clip-vit-base-patch32_TALL14.yaml +8 -0
  642. fusion_bench_config/modelpool/CLIPVisionModelPool/clip-vit-base-patch32_TALL14_model_only.yaml +6 -0
  643. fusion_bench_config/modelpool/CLIPVisionModelPool/clip-vit-base-patch32_TALL20.yaml +8 -0
  644. fusion_bench_config/modelpool/CLIPVisionModelPool/clip-vit-base-patch32_TALL20_model_only.yaml +6 -0
  645. fusion_bench_config/modelpool/CLIPVisionModelPool/clip-vit-base-patch32_generalization_exp1.yaml +24 -0
  646. fusion_bench_config/modelpool/CLIPVisionModelPool/clip-vit-base-patch32_generalization_exp2.yaml +24 -0
  647. fusion_bench_config/modelpool/CLIPVisionModelPool/clip-vit-base-patch32_individual.yaml +13 -0
  648. fusion_bench_config/modelpool/CLIPVisionModelPool/clip-vit-base-patch32_mtl.yaml +5 -0
  649. fusion_bench_config/modelpool/CLIPVisionModelPool/clip-vit-base-patch32_robustness_clean.yaml +18 -0
  650. fusion_bench_config/modelpool/CLIPVisionModelPool/clip-vit-base-patch32_robustness_corrupted.yaml +29 -0
  651. fusion_bench_config/modelpool/CLIPVisionModelPool/clip-vit-base-patch32_single_finetuned.yaml +5 -0
  652. fusion_bench_config/modelpool/CLIPVisionModelPool/clip-vit-base-patch32_single_task_projection.yaml +15 -0
  653. fusion_bench_config/modelpool/CLIPVisionModelPool/clip-vit-base-patch32_svhn_and_mnist.yaml +6 -0
  654. fusion_bench_config/modelpool/CLIPVisionModelPool/clip-vit-base-patch32_two_tasks_control_task.yaml +18 -0
  655. fusion_bench_config/modelpool/CLIPVisionModelPool/clip-vit-large-patch14_TA8.yaml +8 -0
  656. fusion_bench_config/modelpool/CLIPVisionModelPool/clip-vit-large-patch14_TA8_model_only.yaml +6 -0
  657. fusion_bench_config/modelpool/CLIPVisionModelPool/clip-vit-large-patch14_TALL14.yaml +11 -0
  658. fusion_bench_config/modelpool/CLIPVisionModelPool/clip-vit-large-patch14_TALL14_model_only.yaml +9 -0
  659. fusion_bench_config/modelpool/CLIPVisionModelPool/clip-vit-large-patch14_TALL20.yaml +11 -0
  660. fusion_bench_config/modelpool/CLIPVisionModelPool/clip-vit-large-patch14_TALL20_model_only.yaml +9 -0
  661. fusion_bench_config/modelpool/CLIPVisionModelPool/clip-vit-large-patch14_individual.yaml +19 -0
  662. fusion_bench_config/modelpool/CausalLMPool/llama_alpaca_cleaned.yaml +21 -0
  663. fusion_bench_config/modelpool/CausalLMPool/llama_codealpaca.yaml +21 -0
  664. fusion_bench_config/modelpool/CausalLMPool/llama_for_causallm.yaml +20 -0
  665. fusion_bench_config/modelpool/CausalLMPool/llama_metamathqa.yaml +19 -0
  666. fusion_bench_config/modelpool/CausalLMPool/llama_ultrachat.yaml +18 -0
  667. fusion_bench_config/modelpool/CausalLMPool/simle_mixtral_exp_v4.yaml +21 -0
  668. fusion_bench_config/modelpool/CausalLMPool/single_llama_model.yaml +17 -0
  669. fusion_bench_config/modelpool/Seq2SeqLMPool/_template.yaml +8 -0
  670. fusion_bench_config/modelpool/Seq2SeqLMPool/flan-t5-base_glue.yaml +13 -0
  671. fusion_bench_config/modelpool/Seq2SeqLMPool/flan-t5-base_glue_lora16.yaml +41 -0
  672. fusion_bench_config/modelpool/Seq2SeqLMPool/flan-t5-base_glue_lora16_tta.yaml +68 -0
  673. fusion_bench_config/modelpool/Seq2SeqLMPool/flan-t5-base_individual.yaml +7 -0
  674. fusion_bench_config/modelpool/Seq2SeqLMPool/flan-t5-large_glue_lora16.yaml +45 -0
  675. fusion_bench_config/modelpool/SeqenceClassificationModelPool/llama_preference700k.yaml +23 -0
  676. fusion_bench_config/modelpool/SeqenceClassificationModelPool/single_reward_model.yaml +14 -0
  677. fusion_bench_config/modelpool/automodelpool.yaml +12 -0
  678. fusion_bench_config/modelpool/gpt-2_glue.yaml +64 -0
  679. fusion_bench_config/modelpool/mixtral_moe_merging.yaml +14 -0
  680. fusion_bench_config/modelpool/mixtral_moe_upscaling.yaml +6 -0
  681. fusion_bench_config/modelpool/nyuv2_modelpool.yaml +26 -0
  682. fusion_bench_config/modelpool/smile_mistral_exp_v1.yaml +9 -0
  683. fusion_bench_config/modelpool/smile_mistral_exp_v2.yaml +9 -0
  684. fusion_bench_config/modelpool/smile_mistral_exp_v3.yaml +9 -0
  685. fusion_bench_config/modelpool/smile_mistral_exp_v4.yaml +13 -0
  686. fusion_bench_config/nyuv2_config.yaml +17 -0
  687. fusion_bench_config/nyuv2_mtl_train.yaml +32 -0
  688. fusion_bench_config/taskpool/CLIPVisionModelTaskPool/_template.yaml +31 -0
  689. fusion_bench_config/taskpool/CLIPVisionModelTaskPool/clip-vit-base-patch32_robustness_corrupted.yaml +27 -0
  690. fusion_bench_config/taskpool/CLIPVisionModelTaskPool/clip-vit-classification_TA8.yaml +11 -0
  691. fusion_bench_config/taskpool/CLIPVisionModelTaskPool/clip-vit-classification_TA8_B16.yaml +31 -0
  692. fusion_bench_config/taskpool/CLIPVisionModelTaskPool/clip-vit-classification_TA8_L14.yaml +12 -0
  693. fusion_bench_config/taskpool/CLIPVisionModelTaskPool/clip-vit-classification_TA8_val.yaml +12 -0
  694. fusion_bench_config/taskpool/CLIPVisionModelTaskPool/clip-vit-classification_TA8_with_control_task.yaml +12 -0
  695. fusion_bench_config/taskpool/CLIPVisionModelTaskPool/clip-vit-classification_TALL14.yaml +19 -0
  696. fusion_bench_config/taskpool/CLIPVisionModelTaskPool/clip-vit-classification_TALL20.yaml +26 -0
  697. fusion_bench_config/taskpool/CLIPVisionModelTaskPool/clip-vit-single-task_cifar10.yaml +3 -0
  698. fusion_bench_config/taskpool/CLIPVisionModelTaskPool/clip-vit-single-task_cifar100.yaml +3 -0
  699. fusion_bench_config/taskpool/CLIPVisionModelTaskPool/clip-vit-single-task_dtd.yaml +3 -0
  700. fusion_bench_config/taskpool/CLIPVisionModelTaskPool/clip-vit-single-task_emnist_letters.yaml +3 -0
  701. fusion_bench_config/taskpool/CLIPVisionModelTaskPool/clip-vit-single-task_eurosat.yaml +3 -0
  702. fusion_bench_config/taskpool/CLIPVisionModelTaskPool/clip-vit-single-task_fashion_mnist.yaml +3 -0
  703. fusion_bench_config/taskpool/CLIPVisionModelTaskPool/clip-vit-single-task_fer2013.yaml +3 -0
  704. fusion_bench_config/taskpool/CLIPVisionModelTaskPool/clip-vit-single-task_food101.yaml +3 -0
  705. fusion_bench_config/taskpool/CLIPVisionModelTaskPool/clip-vit-single-task_gtsrb.yaml +3 -0
  706. fusion_bench_config/taskpool/CLIPVisionModelTaskPool/clip-vit-single-task_kmnist.yaml +3 -0
  707. fusion_bench_config/taskpool/CLIPVisionModelTaskPool/clip-vit-single-task_mnist.yaml +3 -0
  708. fusion_bench_config/taskpool/CLIPVisionModelTaskPool/clip-vit-single-task_oxford-iiit-pet.yaml +3 -0
  709. fusion_bench_config/taskpool/CLIPVisionModelTaskPool/clip-vit-single-task_oxford_flowers102.yaml +3 -0
  710. fusion_bench_config/taskpool/CLIPVisionModelTaskPool/clip-vit-single-task_oxford_flowers102_val.yaml +3 -0
  711. fusion_bench_config/taskpool/CLIPVisionModelTaskPool/clip-vit-single-task_pcam.yaml +3 -0
  712. fusion_bench_config/taskpool/CLIPVisionModelTaskPool/clip-vit-single-task_rendered-sst2.yaml +3 -0
  713. fusion_bench_config/taskpool/CLIPVisionModelTaskPool/clip-vit-single-task_resisc45.yaml +3 -0
  714. fusion_bench_config/taskpool/CLIPVisionModelTaskPool/clip-vit-single-task_stanford-cars.yaml +3 -0
  715. fusion_bench_config/taskpool/CLIPVisionModelTaskPool/clip-vit-single-task_stl10.yaml +3 -0
  716. fusion_bench_config/taskpool/CLIPVisionModelTaskPool/clip-vit-single-task_sun397.yaml +3 -0
  717. fusion_bench_config/taskpool/CLIPVisionModelTaskPool/clip-vit-single-task_svhn.yaml +3 -0
  718. fusion_bench_config/taskpool/CLIPVisionModelTaskPool/clip_rankone_wemoe_clip-vit-classification_TA8.yaml +18 -0
  719. fusion_bench_config/taskpool/CLIPVisionModelTaskPool/clip_sparse_wemoe_clip-vit-classification_TA8.yaml +18 -0
  720. fusion_bench_config/taskpool/clip-vit-base-patch32_robustness_clean.yaml +24 -0
  721. fusion_bench_config/taskpool/clip-vit-base-patch32_robustness_corrupted.yaml +27 -0
  722. fusion_bench_config/taskpool/clip-vit-base-patch32_svhn_and_mnist.yaml +22 -0
  723. fusion_bench_config/taskpool/dummy.yaml +2 -0
  724. fusion_bench_config/taskpool/flan-t5_glue_text_generation.yaml +44 -0
  725. fusion_bench_config/taskpool/gpt-2_glue.yaml +39 -0
  726. fusion_bench_config/taskpool/nyuv2_taskpool.yaml +9 -0
  727. fusion_bench_config/taskpool/reward_model_evaluation.yaml +18 -0
@@ -0,0 +1,1036 @@
1
+ """
2
+ Augmenters for ARC tasks
3
+
4
+ The augmenters are used to apply transformations to the input and output of the tasks.
5
+
6
+ Augmenter: Base class for all augmenters
7
+ Rotate: Rotate the grid by 90, 180, 270 degrees
8
+ PermuteColors: Permute the colors in the grid
9
+ PermuteColorswithMap: Permute the colors in the grid using a given color map
10
+ PermuteColorsRespectKeyColors: Permute the colors in the grid while keeping the key colors fixed
11
+ Flip: Flip the grid along the given axis
12
+ Reflect: Reflect the grid along the given axis
13
+ Repeat: Repeat the grid along the given
14
+ Transpose: Transpose the grid
15
+ IncreaseResolution: Increase the resolution of the grid
16
+ IncreaseHeight: Increase the height of the grid
17
+ IncreaseWidth: Increase the width of the grid
18
+ DropoutInput: Delete a random rectangular patch from the input
19
+ DropoutOutput: Delete a random rectangular patch from the output
20
+ RandomTranslateXY: Randomly shift the grid along the x and y axis
21
+ RandomTranslateX: Randomly shift the grid along the x axis
22
+ RandomTranslateY: Randomly shift the grid along the y axis
23
+ Chain: Chain multiple augmenters together
24
+ Concat: Concatenate the output of multiple augmenters
25
+ IdentityAugmenter: Identity augmenter
26
+ PermuteExamples: Permute the training examples in the task
27
+ """
28
+
29
+ import copy
30
+ from typing import List, Optional, Tuple
31
+
32
+ import numpy as np
33
+ from numpy.random import RandomState
34
+
35
+ from .arc import Example, Grid, Task
36
+
37
+
38
+ class Augmenter:
39
+ share_rng: bool = False
40
+
41
+ def __repr__(self):
42
+ return str(self)
43
+
44
+ def __call__(self, grid: Grid, rng: RandomState = None) -> Grid:
45
+ return self.apply_to_grid(grid, rng=rng)
46
+
47
+ def apply_to_grid(self, grid: Grid, rng: RandomState = None) -> Grid:
48
+ raise NotImplementedError()
49
+
50
+ def apply_to_example(
51
+ self,
52
+ example: Example,
53
+ rng: RandomState = None,
54
+ to_input: bool = True,
55
+ to_output: bool = True,
56
+ ) -> Example:
57
+ input = (
58
+ self.apply_to_grid(example.input, rng=rng) if to_input else example.input
59
+ )
60
+ output = (
61
+ self.apply_to_grid(example.output, rng=rng) if to_output else example.output
62
+ )
63
+ if example.cot is not None:
64
+ cot = copy.deepcopy(example.cot)
65
+ if not np.array_equal(cot[-1], output):
66
+ cot.append(output)
67
+ else:
68
+ cot = None
69
+ return Example(input, output, cot)
70
+
71
+ def apply_to_task(
72
+ self, task: Task, rng: RandomState = None, share_rng=False, **kwargs
73
+ ) -> Task:
74
+
75
+ if self.share_rng:
76
+ # make sure all examples get seperate copies of same rng
77
+ # this is to make sure that the same random number(s) is used for all examples
78
+ # seed = rng.randint(0, 2**32)
79
+ train_rngs = [copy.deepcopy(rng) for i in range(len(task.train_examples))]
80
+ test_rng = copy.deepcopy(rng) # RandomState(seed)
81
+ else:
82
+ train_rngs = [rng for _ in range(len(task.train_examples))]
83
+ test_rng = rng
84
+
85
+ return Task(
86
+ train_examples=[
87
+ self.apply_to_example(example, rng=rng_i, **kwargs)
88
+ for example, rng_i in zip(task.train_examples, train_rngs)
89
+ ],
90
+ test_example=self.apply_to_example(
91
+ task.test_example, rng=test_rng, **kwargs
92
+ ),
93
+ name=task.name,
94
+ )
95
+
96
+
97
+ class Rotate(Augmenter):
98
+ def __init__(self, angle: int):
99
+ assert angle in [0, 90, 180, 270]
100
+ self.angle = angle
101
+
102
+ def __str__(self):
103
+ return f"Rotate({self.angle})"
104
+
105
+ def apply_to_grid(self, grid: Grid, rng: RandomState = None) -> Grid:
106
+ # roate the input and output by the given angle
107
+ del rng
108
+ if self.angle == 90:
109
+ return np.rot90(grid, k=1)
110
+ elif self.angle == 180:
111
+ return np.rot90(grid, k=2)
112
+ elif self.angle == 270:
113
+ return np.rot90(grid, k=3)
114
+ else:
115
+ raise ValueError("Invalid angle")
116
+
117
+
118
+ class PermuteColors(Augmenter):
119
+ share_rng = True
120
+ color_mapper = None
121
+
122
+ def __str__(self):
123
+ return "PermuteColors()"
124
+
125
+ def apply_to_task(
126
+ self,
127
+ task: Task,
128
+ use_test_output: bool = True,
129
+ rng: RandomState = None,
130
+ share_rng=False,
131
+ **kwargs,
132
+ ) -> Task:
133
+ # get all used colors in the inputs and outputs
134
+ colors = []
135
+ for example in task.train_examples:
136
+ colors += example.input.flatten().tolist()
137
+ colors += example.output.flatten().tolist()
138
+ colors += task.test_example.input.flatten().tolist()
139
+
140
+ if use_test_output:
141
+ colors += task.test_example.output.flatten().tolist()
142
+
143
+ # get unique colors
144
+ colors = set(colors)
145
+ # remove 0
146
+ colors = colors - {0}
147
+ remaining_colors = list(set(list(range(1, 10))) - colors)
148
+ colors = list(colors)
149
+
150
+ rng.shuffle(remaining_colors) # inplac
151
+
152
+ permuted_colors = rng.permutation(colors).tolist()
153
+ # sample a mapping from colors to new ids
154
+ color_map = {0: 0}
155
+
156
+ for color in colors:
157
+
158
+ if color in color_map:
159
+ continue
160
+
161
+ if len(remaining_colors) > 0:
162
+ new_color = remaining_colors.pop()
163
+ else:
164
+ new_color = permuted_colors.pop()
165
+ # in this case we want to directly swap colors
166
+ # so new_color shoud be the color that is being replaced
167
+ color_map[new_color] = color
168
+ # remove color from permuted colors
169
+ if color in permuted_colors:
170
+ permuted_colors.remove(color)
171
+
172
+ color_map[color] = new_color
173
+
174
+ self._color_map = color_map
175
+
176
+ def color_mapper(color: int) -> int:
177
+ return color_map.get(color, color)
178
+
179
+ self.color_mapper = np.vectorize(color_mapper)
180
+
181
+ return Task(
182
+ train_examples=[
183
+ self.apply_to_example(example, rng=rng, **kwargs)
184
+ for example in task.train_examples
185
+ ],
186
+ test_example=self.apply_to_example(task.test_example, rng=rng, **kwargs),
187
+ name=task.name,
188
+ )
189
+
190
+ def apply_to_grid(self, grid: Grid, rng: RandomState = None) -> Grid:
191
+ # map colors to new colors
192
+ return self.color_mapper(grid)
193
+
194
+
195
+ class PermuteColorswithMap(Augmenter):
196
+
197
+ def __init__(self, color_map):
198
+ self.color_map = color_map
199
+
200
+ def color_mapper(color: int) -> int:
201
+ return color_map.get(color, color)
202
+
203
+ self.color_mapper = np.vectorize(color_mapper)
204
+
205
+ def apply_to_grid(self, grid: Grid, rng: RandomState = None) -> Grid:
206
+ # map colors to new colors
207
+ return self.color_mapper(grid)
208
+
209
+ def __repr__(self):
210
+ return f"PermuteColorswithMap({self.color_map})"
211
+
212
+ def __str__(self):
213
+ return self.__repr__()
214
+
215
+
216
+ class PermuteColorsRespectKeyColors(Augmenter):
217
+ share_rng = True
218
+ color_mapper = None
219
+
220
+ def __init__(
221
+ self,
222
+ key_colors: List[int] = None,
223
+ use_remaining_colors: bool = True,
224
+ use_test_output: bool = False,
225
+ ):
226
+ self.key_colors = key_colors
227
+ self.use_remaining_colors = use_remaining_colors
228
+ self.use_test_output = use_test_output
229
+
230
+ def __str__(self):
231
+ return "PermuteColorsRespectKeyColors(key_colors={self.key_colors}, use_remaining_colors={self.use_remaining_colors}, use_test_output={self.use_test_output})"
232
+
233
+ @staticmethod
234
+ def get_key_colors(task: Task, use_test_output: bool = False):
235
+ key_colors_input = []
236
+ key_colors_output = []
237
+ for example in task.train_examples:
238
+ key_colors_input.append(set(example.input.flatten().tolist()))
239
+ key_colors_output.append(set(example.output.flatten().tolist()))
240
+
241
+ key_colors_input.append(set(task.test_example.input.flatten().tolist()))
242
+ if use_test_output:
243
+ key_colors_output.append(set(task.test_example.output.flatten().tolist()))
244
+
245
+ all_colors = set.union(*key_colors_input, *key_colors_output)
246
+
247
+ key_colors_input = set.intersection(*key_colors_input)
248
+ key_colors_output = set.intersection(*key_colors_output)
249
+ key_colors = key_colors_input.union(key_colors_output)
250
+
251
+ return key_colors, all_colors
252
+
253
+ def apply_to_task(
254
+ self, task: Task, rng: RandomState = None, share_rng=False, **kwargs
255
+ ) -> Task:
256
+ key_colors, colors = PermuteColorsRespectKeyColors.get_key_colors(
257
+ task, use_test_output=self.use_test_output
258
+ )
259
+ if self.key_colors is None:
260
+ self.key_colors = key_colors
261
+ else:
262
+ key_colors = self.key_colors
263
+
264
+ remaining_colors = list(set(list(range(10))) - colors)
265
+ colors = list(colors)
266
+
267
+ rng.shuffle(remaining_colors) # inplace
268
+ # keep key colors_static
269
+ color_map = {}
270
+
271
+ for key_color in self.key_colors:
272
+ color_map[key_color] = key_color
273
+ if key_color in colors:
274
+ colors.remove(key_color)
275
+
276
+ permuted_colors = rng.permutation(colors).tolist()
277
+
278
+ for color in colors:
279
+
280
+ if color in color_map:
281
+ continue
282
+
283
+ if self.use_remaining_colors and len(remaining_colors) > 0:
284
+ new_color = remaining_colors.pop()
285
+ else:
286
+ new_color = permuted_colors.pop()
287
+ # in this case we want to directly swap colors
288
+ # so new_color shoud be the color that is being replaced
289
+ color_map[new_color] = color
290
+ # remove color from permuted colors
291
+ if color in permuted_colors:
292
+ permuted_colors.remove(color)
293
+
294
+ color_map[color] = new_color
295
+
296
+ def color_mapper(color: int) -> int:
297
+ return color_map.get(color, color)
298
+
299
+ self.color_mapper = np.vectorize(color_mapper)
300
+
301
+ return Task(
302
+ train_examples=[
303
+ self.apply_to_example(example, rng=rng, **kwargs)
304
+ for example in task.train_examples
305
+ ],
306
+ test_example=self.apply_to_example(task.test_example, rng=rng, **kwargs),
307
+ name=task.name,
308
+ )
309
+
310
+ def apply_to_grid(self, grid: Grid, rng: RandomState = None) -> Grid:
311
+ # map colors to new colors
312
+ return self.color_mapper(grid)
313
+
314
+
315
+ class PermuteColorsStatic(Augmenter):
316
+ share_rng = True
317
+ color_mapper = None
318
+
319
+ def __str__(self):
320
+ return f"PermuteColorsStatic({self.color_map})"
321
+
322
+ def __init__(self, color_map):
323
+ self.color_map = color_map
324
+
325
+ self.color_mapper = np.vectorize(lambda x: self.color_map.get(x, x))
326
+
327
+ def apply_to_grid(self, grid: Grid, rng: RandomState = None) -> Grid:
328
+ return self.color_mapper(grid)
329
+
330
+
331
+ class Flip(Augmenter):
332
+ def __init__(self, axis: int):
333
+ assert axis in [0, 1]
334
+ self.axis = axis
335
+
336
+ def __str__(self):
337
+ return f"Flip({self.axis})"
338
+
339
+ def apply_to_grid(self, grid: Grid, rng: RandomState = None) -> Grid:
340
+ del rng
341
+ if self.axis == 0:
342
+ return np.flipud(grid)
343
+ elif self.axis == 1:
344
+ return np.fliplr(grid)
345
+ else:
346
+ raise ValueError("Invalid axis")
347
+
348
+
349
+ class Reflect(Augmenter):
350
+ def __init__(self, axis: int, reverse=False):
351
+ assert axis in [0, 1]
352
+ self.axis = axis
353
+ self.reverse = reverse
354
+
355
+ def __str__(self):
356
+ return f"Reflect({self.axis}, reverse={self.reverse})"
357
+
358
+ def apply_to_grid(self, grid: Grid, rng: RandomState = None) -> Example:
359
+ del rng
360
+ # reflect the input and output by the given axis
361
+ original = grid
362
+ if self.axis == 0:
363
+ reflected = np.flipud(original)
364
+ elif self.axis == 1:
365
+ reflected = np.fliplr(original)
366
+ else:
367
+ raise ValueError("Invalid axis")
368
+
369
+ if self.reverse:
370
+ if self.axis == 0:
371
+ return np.concatenate((reflected, original), axis=0)
372
+ elif self.axis == 1:
373
+ return np.concatenate((reflected, original), axis=1)
374
+ else:
375
+ if self.axis == 0:
376
+ return np.concatenate((original, reflected), axis=0)
377
+ elif self.axis == 1:
378
+ return np.concatenate((original, reflected), axis=1)
379
+
380
+
381
+ class Repeat(Augmenter):
382
+ def __init__(self, axis: int, n: int):
383
+ assert axis in [0, 1, 2]
384
+ self.axis = axis
385
+ self.n = n
386
+
387
+ def __str__(self):
388
+ return f"Repeat({self.axis}, {self.n})"
389
+
390
+ def apply_to_grid(self, grid: Grid, rng: RandomState = None) -> Grid:
391
+ del rng
392
+ if self.axis == 0:
393
+ return np.concatenate([grid] * self.n, axis=0)
394
+ elif self.axis == 1:
395
+ return np.concatenate([grid] * self.n, axis=1)
396
+ elif self.axis == 2:
397
+ return np.concatenate(
398
+ [np.concatenate([grid] * self.n, axis=0)] * self.n, axis=1
399
+ )
400
+ else:
401
+ raise ValueError("Invalid axis")
402
+
403
+
404
+ class Transpose(Augmenter):
405
+ def __str__(self):
406
+ return "Transpose()"
407
+
408
+ def apply_to_grid(self, grid: Grid, rng: RandomState = None) -> Grid:
409
+ del rng
410
+ return np.transpose(grid)
411
+
412
+
413
+ class IncreaseResolution(Augmenter):
414
+ def __init__(self, factor: int):
415
+ assert factor > 1
416
+ self.factor = factor
417
+
418
+ def __str__(self):
419
+ return f"IncreaseResolution({self.factor})"
420
+
421
+ def apply_to_grid(self, grid: Grid, rng: RandomState = None) -> Grid:
422
+ del rng
423
+ grid = np.repeat(grid, self.factor, axis=0)
424
+ grid = np.repeat(grid, self.factor, axis=1)
425
+ return grid
426
+
427
+
428
+ class IncreaseHeight(Augmenter):
429
+ def __init__(self, factor: int):
430
+ assert factor > 1
431
+ self.factor = factor
432
+
433
+ def __str__(self):
434
+ return f"IncreaseHeight({self.factor})"
435
+
436
+ def apply_to_grid(self, grid: Grid, rng: RandomState = None) -> Grid:
437
+ del rng
438
+ grid = np.repeat(grid, self.factor, axis=0)
439
+ return grid
440
+
441
+
442
+ class IncreaseWidth(Augmenter):
443
+ def __init__(self, factor: int):
444
+ assert factor > 1
445
+ self.factor = factor
446
+
447
+ def __str__(self):
448
+ return f"IncreaseWidth({self.factor})"
449
+
450
+ def apply_to_grid(self, grid: Grid, rng: RandomState = None) -> Grid:
451
+ del rng
452
+ grid = np.repeat(grid, self.factor, axis=1)
453
+ return grid
454
+
455
+
456
+ class DropoutInput(Augmenter):
457
+ """
458
+ Delete a random rectangular patch
459
+ """
460
+
461
+ def __init__(self):
462
+ self.dropout_color = 0
463
+
464
+ def __str__(self):
465
+ return "DropoutInput()"
466
+
467
+ def apply_to_grid(self, grid: Grid, rng: RandomState = None) -> Grid:
468
+ # get rng with seed
469
+ if rng is None:
470
+ rng = RandomState()
471
+ grid = grid.copy()
472
+ num_dropouts = rng.randint(1, 3)
473
+ for _ in range(num_dropouts):
474
+ x_len_ratio = rng.uniform(0.1, 0.3)
475
+ y_len_ratio = rng.uniform(0.1, 0.3)
476
+ x_start_ratio = rng.uniform(0.1, 0.7)
477
+ y_start_ratio = rng.uniform(0.1, 0.7)
478
+
479
+ x_len = int(np.ceil(grid.shape[0] * x_len_ratio))
480
+ y_len = int(np.ceil(grid.shape[1] * y_len_ratio))
481
+
482
+ x_start = int(grid.shape[0] * x_start_ratio)
483
+ y_start = int(grid.shape[1] * y_start_ratio)
484
+
485
+ grid[x_start : x_start + x_len, y_start : y_start + y_len] = (
486
+ self.dropout_color
487
+ )
488
+ return grid
489
+
490
+ def apply_to_task(
491
+ self, task: Task, rng: RandomState = None, share_rng=False, **kwargs
492
+ ) -> Task:
493
+ # find unused colors in the task
494
+ all_colors = set()
495
+ for example in task.train_examples:
496
+ all_colors.update(set(example.input.flatten().tolist()))
497
+ all_colors.update(set(example.output.flatten().tolist()))
498
+
499
+ all_colors = list(set(range(10)) - all_colors)
500
+ if len(all_colors) == 0:
501
+ self.dropout_color = 0
502
+ else:
503
+ self.dropout_color = rng.choice(all_colors)
504
+
505
+ return super().apply_to_task(task, rng=rng, share_rng=share_rng, **kwargs)
506
+
507
+
508
+ class DropoutOutput(Augmenter):
509
+ share_rng = True
510
+
511
+ def __str__(self):
512
+ return "DropoutOutput()"
513
+ self.dropout_color = 0
514
+
515
+ def apply_to_grid(self, grid: Grid, rng: RandomState = None) -> Grid:
516
+ assert rng is not None
517
+ grid = grid.copy()
518
+ num_dropouts = rng.randint(1, 3)
519
+ for _ in range(num_dropouts):
520
+ x_len_ratio = rng.uniform(0.1, 0.3)
521
+ y_len_ratio = rng.uniform(0.1, 0.3)
522
+ x_start_ratio = rng.uniform(0.1, 0.7)
523
+ y_start_ratio = rng.uniform(0.1, 0.7)
524
+
525
+ x_len = int(np.ceil(grid.shape[0] * x_len_ratio))
526
+ y_len = int(np.ceil(grid.shape[1] * y_len_ratio))
527
+
528
+ x_start = int(grid.shape[0] * x_start_ratio)
529
+ y_start = int(grid.shape[1] * y_start_ratio)
530
+
531
+ grid[x_start : x_start + x_len, y_start : y_start + y_len] = (
532
+ self.dropout_color
533
+ )
534
+ return grid
535
+
536
+ def apply_to_task(
537
+ self, task: Task, rng: RandomState = None, share_rng=False, **kwargs
538
+ ) -> Task:
539
+ # find unused colors in the task
540
+ all_colors = set()
541
+ for example in task.train_examples:
542
+ all_colors.update(set(example.input.flatten().tolist()))
543
+ all_colors.update(set(example.output.flatten().tolist()))
544
+
545
+ all_colors = list(set(range(10)) - all_colors)
546
+
547
+ if len(all_colors) == 0:
548
+ self.dropout_color = 0
549
+ else:
550
+ self.dropout_color = rng.choice(all_colors)
551
+
552
+ return super().apply_to_task(task, rng=rng, share_rng=share_rng, **kwargs)
553
+
554
+
555
+ class RandomTranslateXY(Augmenter):
556
+ share_rng = True
557
+
558
+ def __str__(self):
559
+ return "RandomTranslateXY()"
560
+
561
+ def apply_to_grid(self, grid: Grid, rng: RandomState = None) -> Grid:
562
+ assert rng is not None
563
+ # if rng.rand() < 0.5:
564
+ shift_x = rng.randint(0, min(4, grid.shape[0]))
565
+ shift_y = rng.randint(0, min(4, grid.shape[1]))
566
+ # else:
567
+ # shift_x_ratio = rng.uniform(-0.5, 0.5)
568
+ # shift_x = int(np.round(grid.shape[0] * shift_x_ratio))
569
+ # shift_y_ratio = rng.uniform(-0.5, 0.5)
570
+ # shift_y = int(np.round(grid.shape[1] * shift_y_ratio))
571
+ return np.roll(grid, (shift_x, shift_y), axis=(0, 1))
572
+
573
+
574
+ class RandomTranslateX(Augmenter):
575
+ share_rng = True
576
+
577
+ def __str__(self):
578
+ return "RandomTranslateX()"
579
+
580
+ def apply_to_grid(self, grid: Grid, rng: RandomState = None) -> Grid:
581
+ assert rng is not None
582
+ # if rng.rand() < 0.5:
583
+ shift_x = rng.randint(1, min(4, grid.shape[0]))
584
+ # else:
585
+ # shift_x_ratio = rng.uniform(-0.5, 0.5)
586
+ # shift_x = int(np.round(grid.shape[0] * shift_x_ratio))
587
+ return np.roll(grid, shift_x, axis=0)
588
+
589
+
590
+ class RandomTranslateY(Augmenter):
591
+ share_rng = True
592
+
593
+ def __str__(self):
594
+ return "RandomTranslateY()"
595
+
596
+ def apply_to_grid(self, grid: Grid, rng: RandomState = None) -> Grid:
597
+ assert rng is not None
598
+ # if rng.rand() < 0.5:
599
+ shift_y = rng.randint(1, min(4, grid.shape[1]))
600
+ # else:
601
+ # shift_y_ratio = rng.uniform(-0.5, 0.5)
602
+ # shift_y = int(np.round(grid.shape[1] * shift_y_ratio))
603
+ return np.roll(grid, shift_y, axis=1)
604
+
605
+
606
+ def find_connected_components(
607
+ grid: Grid, background_color: Optional[int] = None
608
+ ) -> List[List[tuple[int, int]]]:
609
+ # get background color as the most frequenct color
610
+ if background_color is None:
611
+ background_color = np.bincount(grid.flatten()).argmax()
612
+
613
+ # get connected components
614
+ visited = np.zeros_like(grid)
615
+ components = []
616
+ for i in range(grid.shape[0]):
617
+ for j in range(grid.shape[1]):
618
+ if visited[i, j] == 0 and grid[i, j] != background_color:
619
+ component = []
620
+ stack = [(i, j)]
621
+ while stack:
622
+ x, y = stack.pop()
623
+ if x < 0 or x >= grid.shape[0] or y < 0 or y >= grid.shape[1]:
624
+ continue
625
+ if visited[x, y] == 1 or grid[x, y] == background_color:
626
+ continue
627
+ component.append((x, y))
628
+ visited[x, y] = 1
629
+ stack.append((x + 1, y))
630
+ stack.append((x - 1, y))
631
+ stack.append((x, y + 1))
632
+ stack.append((x, y - 1))
633
+ components.append(component)
634
+
635
+ return components, background_color
636
+
637
+
638
+ class RandomObjectRotate(Augmenter):
639
+ share_rng = True
640
+
641
+ def __init__(self, angle: int):
642
+ self.angle = angle
643
+
644
+ def __str__(self):
645
+ return f"RandomObjectRotate({self.angle})"
646
+
647
+ def apply_to_grid(
648
+ self, grid: Grid, rng: RandomState = None, background_color: int = 0
649
+ ) -> Grid:
650
+ assert rng is not None
651
+
652
+ components, background_color = find_connected_components(
653
+ grid, background_color=background_color
654
+ )
655
+
656
+ if not components:
657
+ return grid
658
+
659
+ idx = rng.choice(len(components), size=1)[0]
660
+ component = components[idx]
661
+
662
+ # copy grid
663
+ grid = grid.copy()
664
+
665
+ # rotate part of the grid by the given angle assume left bottom corner is the origin
666
+ rotated_component = []
667
+ origin = np.array([min(x for x, y in component), min(y for x, y in component)])
668
+ for x, y in component:
669
+ color = grid[x, y]
670
+ x -= origin[0]
671
+ y -= origin[1]
672
+ if self.angle == 90:
673
+ x, y = y, -x
674
+ elif self.angle == 180:
675
+ x, y = -x, -y
676
+ elif self.angle == 270:
677
+ x, y = -y, x
678
+ rotated_component.append((x + origin[0], y + origin[1], color))
679
+
680
+ for x, y in component:
681
+ grid[x, y] = background_color
682
+ for x, y, color in rotated_component:
683
+ grid[x, y] = color
684
+
685
+ return grid
686
+
687
+
688
+ class PermuteExamples(Augmenter):
689
+ def apply_to_task(
690
+ self, task: Task, rng: RandomState = None, share_rng=False, **kwargs
691
+ ) -> Task:
692
+ if rng is None:
693
+ rng = RandomState()
694
+
695
+ perm = rng.permutation(len(task.train_examples))
696
+ train_examples = [task.train_examples[i] for i in perm]
697
+ return Task(
698
+ train_examples=train_examples,
699
+ test_example=task.test_example,
700
+ name=task.name,
701
+ )
702
+
703
+
704
+ class RandomObjectTranslateXY(Augmenter):
705
+ share_rng = True
706
+
707
+ def __str__(self):
708
+ return "RandomObjectTranslateXY()"
709
+
710
+ def apply_to_grid(
711
+ self,
712
+ grid: Grid,
713
+ rng: RandomState = None,
714
+ background_color: Optional[int] = None,
715
+ ) -> Grid:
716
+ assert rng is not None
717
+
718
+ components, background_color = find_connected_components(
719
+ grid, background_color=background_color
720
+ )
721
+
722
+ if not components:
723
+ return grid
724
+
725
+ idx = rng.choice(len(components), size=1)[0]
726
+ component = components[idx]
727
+
728
+ # copy grid
729
+ grid = grid.copy()
730
+
731
+ # translate part of the grid by the given angle assume left bottom corner is the origin
732
+ translated_component = []
733
+ shift_x = rng.randint(-grid.shape[0] // 2, grid.shape[0] // 2)
734
+ shift_y = rng.randint(-grid.shape[1] // 2, grid.shape[1] // 2)
735
+ for x, y in component:
736
+ color = grid[x, y]
737
+ x += shift_x
738
+ y += shift_y
739
+ translated_component.append((x, y, color))
740
+
741
+ for x, y in component:
742
+ grid[x, y] = background_color
743
+ for x, y, color in translated_component:
744
+ grid[x, y] = color
745
+
746
+ return grid
747
+
748
+
749
+ class Chain(Augmenter):
750
+ def __init__(self, augmenters: Tuple[Augmenter]):
751
+ self.augmenters = augmenters
752
+
753
+ def __str__(self):
754
+ return f"Chain({self.augmenters})"
755
+
756
+ def apply_to_task(
757
+ self, task: Task, rng: RandomState = None, share_rng=False, **kwargs
758
+ ) -> Task:
759
+ for augmenter in self.augmenters:
760
+ task = augmenter.apply_to_task(
761
+ task, rng=rng, share_rng=augmenter.share_rng, **kwargs
762
+ )
763
+ return task
764
+
765
+ def apply_to_example(
766
+ self,
767
+ example: Example,
768
+ rng: RandomState = None,
769
+ to_input: bool = True,
770
+ to_output: bool = True,
771
+ ) -> Example:
772
+ for augmenter in self.augmenters:
773
+ example = augmenter.apply_to_example(
774
+ example, rng=rng, to_input=to_input, to_output=to_output
775
+ )
776
+ return example
777
+
778
+ def apply_to_grid(self, grid: Grid, rng: RandomState = None) -> Grid:
779
+ for augmenter in self.augmenters:
780
+ grid = augmenter.apply_to_grid(grid, rng=rng)
781
+ return grid
782
+
783
+
784
+ class Concat(Augmenter):
785
+ def __init__(self, augmenters: Tuple[Augmenter], axis: int = 0):
786
+ self.augmenters = augmenters
787
+ self.axis = axis
788
+
789
+ def __str__(self):
790
+ return f"Concat({self.augmenters}, axis={self.axis})"
791
+
792
+ def apply_to_grid(self, grid: Grid, rng: RandomState = None) -> Grid:
793
+ grids = []
794
+ for augmenter in self.augmenters:
795
+ grids.append(augmenter.apply_to_grid(grid, rng=rng))
796
+
797
+ return np.concatenate(grids, axis=self.axis)
798
+
799
+
800
+ class IdentityAugmenter(Augmenter):
801
+ def __str__(self):
802
+ return "IdentityAugmenter()"
803
+
804
+ def apply_to_grid(self, grid: Grid, rng: RandomState = None) -> Grid:
805
+ return grid.copy()
806
+
807
+
808
+ def inverse(augmenter):
809
+ if isinstance(augmenter, Rotate):
810
+ return Rotate(360 - augmenter.angle)
811
+ elif isinstance(augmenter, Flip):
812
+ return augmenter
813
+ elif isinstance(augmenter, Transpose):
814
+ return augmenter
815
+ elif isinstance(augmenter, PermuteColors):
816
+ color_map = augmenter._color_map
817
+ # reverse
818
+ inverse_map = {v: k for k, v in color_map.items()}
819
+ return PermuteColorswithMap(inverse_map)
820
+
821
+
822
+ augmenters_to_apply_to_input = [
823
+ DropoutInput(),
824
+ IncreaseResolution(2),
825
+ IncreaseResolution(3),
826
+ IncreaseHeight(2),
827
+ IncreaseWidth(2),
828
+ IncreaseHeight(3),
829
+ IncreaseWidth(3),
830
+ ]
831
+
832
+ input_augmenters_probs = [
833
+ 1,
834
+ 1 / 2,
835
+ 1 / 2,
836
+ 1 / 4,
837
+ 1 / 4,
838
+ 1 / 4,
839
+ 1 / 4,
840
+ ]
841
+
842
+ augmenters_to_apply_to_output = [
843
+ Rotate(90),
844
+ Rotate(270),
845
+ Rotate(180),
846
+ Flip(0),
847
+ Flip(1),
848
+ Reflect(0, reverse=True),
849
+ Reflect(1, reverse=True),
850
+ Reflect(0, reverse=False),
851
+ Reflect(1, reverse=False),
852
+ IncreaseResolution(2),
853
+ IncreaseResolution(3),
854
+ IncreaseHeight(2),
855
+ IncreaseWidth(2),
856
+ Transpose(),
857
+ RandomTranslateXY(),
858
+ DropoutOutput(),
859
+ Repeat(0, 2),
860
+ Repeat(1, 2),
861
+ Repeat(0, 3),
862
+ Repeat(1, 3),
863
+ Repeat(2, 2),
864
+ Repeat(2, 3),
865
+ ]
866
+
867
+ output_augmenters_probs = [
868
+ 1 / 3,
869
+ 1 / 3,
870
+ 1 / 3,
871
+ 1 / 2,
872
+ 1 / 2,
873
+ 1 / 4,
874
+ 1 / 4,
875
+ 1 / 4,
876
+ 1 / 4,
877
+ 1 / 4,
878
+ 1 / 4,
879
+ 1 / 4,
880
+ 1 / 4,
881
+ 1 / 2,
882
+ 1 / 2,
883
+ 1 / 2,
884
+ 1 / 5,
885
+ 1 / 5,
886
+ 1 / 5,
887
+ 1 / 5,
888
+ 1 / 5,
889
+ 1 / 5,
890
+ ]
891
+
892
+ augmenters_to_apply_to_both = [
893
+ IncreaseResolution(2),
894
+ IncreaseResolution(3),
895
+ IncreaseHeight(2),
896
+ IncreaseWidth(2),
897
+ IncreaseHeight(3),
898
+ IncreaseWidth(3),
899
+ PermuteColors(),
900
+ # Transpose(),
901
+ # Rotate(90),
902
+ # Rotate(270),
903
+ # Rotate(180),
904
+ # Flip(0),
905
+ # Flip(1),
906
+ # Reflect(0, reverse=False),
907
+ # Reflect(1, reverse=False),
908
+ ]
909
+
910
+ both_augmenters_probs = [
911
+ 1 / 2,
912
+ 1 / 2,
913
+ 1 / 4,
914
+ 1 / 4,
915
+ 1 / 4,
916
+ 1 / 4,
917
+ 1 / 2,
918
+ # 1/8,
919
+ # 1/8,
920
+ # 1/8,
921
+ # 1/8,
922
+ # 1/8,
923
+ # 1/8,
924
+ # 1/8,
925
+ # 1/8,
926
+ ]
927
+
928
+ # normalize
929
+ input_augmenters_probs = [
930
+ p / sum(input_augmenters_probs) for p in input_augmenters_probs
931
+ ]
932
+ output_augmenters_probs = [
933
+ p / sum(output_augmenters_probs) for p in output_augmenters_probs
934
+ ]
935
+ both_augmenters_probs = [p / sum(both_augmenters_probs) for p in both_augmenters_probs]
936
+
937
+
938
+ def apply_a_random_augmentation(task: Task, rng=None) -> Tuple[Task, Augmenter, str]:
939
+ if rng is None:
940
+ rng = RandomState()
941
+
942
+ category = rng.choice(["input", "output", "both"], p=[0.3, 0.6, 0.1])
943
+
944
+ if category == "input":
945
+ augmenter = rng.choice(augmenters_to_apply_to_input, p=input_augmenters_probs)
946
+ task = augmenter.apply_to_task(task, to_input=True, to_output=False, rng=rng)
947
+ elif category == "output":
948
+ augmenter = rng.choice(augmenters_to_apply_to_output, p=output_augmenters_probs)
949
+ task = augmenter.apply_to_task(task, to_input=False, to_output=True, rng=rng)
950
+ else:
951
+ augmenter = rng.choice(augmenters_to_apply_to_both, p=both_augmenters_probs)
952
+ task = augmenter.apply_to_task(task, to_input=True, to_output=True, rng=rng)
953
+
954
+ return task, augmenter, category
955
+
956
+
957
+ if __name__ == "__main__":
958
+
959
+ grid = np.array(
960
+ [
961
+ [1, 1, 1, 1, 1, 1, 1],
962
+ [2, 2, 2, 2, 2, 2, 2],
963
+ [3, 3, 3, 3, 3, 3, 3],
964
+ [4, 4, 4, 4, 4, 4, 4],
965
+ [5, 5, 5, 5, 5, 5, 5],
966
+ ]
967
+ )
968
+
969
+ rng = RandomState(45)
970
+ task = Task(train_examples=[Example(grid, grid)], test_example=Example(grid, grid))
971
+
972
+ drop_task = DropoutOutput().apply_to_task(
973
+ task, rng=rng, to_input=False, to_output=True
974
+ )
975
+ drop_task_input = DropoutInput().apply_to_task(
976
+ task, rng=rng, to_input=True, to_output=False
977
+ )
978
+
979
+ grid1 = np.array(
980
+ [
981
+ [0, 0, 0, 0, 0],
982
+ [0, 1, 1, 1, 0],
983
+ [0, 1, 1, 1, 0],
984
+ [0, 0, 0, 0, 0],
985
+ [0, 2, 2, 0, 0],
986
+ [0, 2, 2, 0, 0],
987
+ [0, 2, 2, 0, 0],
988
+ ]
989
+ )
990
+
991
+ # another grid
992
+ grid2 = np.array(
993
+ [
994
+ [0, 0, 0, 0, 0],
995
+ [0, 5, 5, 5, 0],
996
+ [0, 5, 5, 5, 0],
997
+ [0, 0, 0, 0, 0],
998
+ [0, 2, 4, 0, 0],
999
+ [0, 9, 4, 0, 0],
1000
+ [0, 9, 4, 0, 0],
1001
+ ]
1002
+ )
1003
+
1004
+ train_examples = [Example(grid1, grid1) for _ in range(3)]
1005
+ train_examples.append(Example(grid1, grid2))
1006
+ test_example = Example(grid1, grid1)
1007
+
1008
+ task = Task(train_examples=train_examples, test_example=test_example)
1009
+
1010
+ for _ in range(1000):
1011
+ ttask, augmenterr, category = apply_a_random_augmentation(task, rng=rng)
1012
+ assert np.array_equal(
1013
+ ttask.train_examples[0].output, ttask.train_examples[1].output
1014
+ )
1015
+ assert np.array_equal(
1016
+ ttask.train_examples[0].output, ttask.train_examples[2].output
1017
+ )
1018
+ assert not np.array_equal(
1019
+ ttask.train_examples[0].output, ttask.train_examples[3].output
1020
+ )
1021
+
1022
+ # apply permute colors
1023
+ permute_colors = PermuteColors()
1024
+ ttask = permute_colors.apply_to_task(task, rng=rng)
1025
+
1026
+ # random permutation
1027
+ permuted = rng.permutation(np.arange(10))
1028
+
1029
+ print(permuted)
1030
+
1031
+ def color_mapper(color: int) -> int:
1032
+ return permuted[color]
1033
+
1034
+ color_mapper_v = np.vectorize(color_mapper)
1035
+
1036
+ color_mapper_v(task.test_example.input)