{"id":13653313,"url":"https://github.com/leopard-ai/betty","last_synced_at":"2025-12-29T11:55:56.287Z","repository":{"id":43033267,"uuid":"508703524","full_name":"leopard-ai/betty","owner":"leopard-ai","description":"Betty: an automatic differentiation library for generalized meta-learning and multilevel optimization","archived":false,"fork":false,"pushed_at":"2024-06-26T12:14:39.000Z","size":2990,"stargazers_count":338,"open_issues_count":4,"forks_count":28,"subscribers_count":8,"default_branch":"main","last_synced_at":"2025-04-15T14:44:19.979Z","etag":null,"topics":["artificial-intelligence","autodiff","automatic-differentiation","bilevel-optimization","deep-learning","hyperparameter-optimization","implicit-differentiation","machine-learning","meta-learning","multilevel-optimization","neural-architecture-search","pytorch","reinforcement-learning"],"latest_commit_sha":null,"homepage":"https://leopard-ai.github.io/betty/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/leopard-ai.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-06-29T13:30:04.000Z","updated_at":"2025-04-01T22:36:17.000Z","dependencies_parsed_at":"2024-01-12T19:47:31.125Z","dependency_job_id":"ec81f828-3fd5-4ef3-8d97-e77e9676384a","html_url":"https://github.com/leopard-ai/betty","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leopard-ai%2Fbetty","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leopard-ai%2Fbetty/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leopard-ai%2Fbetty/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leopard-ai%2Fbetty/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/leopard-ai","download_url":"https://codeload.github.com/leopard-ai/betty/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250384987,"owners_count":21421828,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["artificial-intelligence","autodiff","automatic-differentiation","bilevel-optimization","deep-learning","hyperparameter-optimization","implicit-differentiation","machine-learning","meta-learning","multilevel-optimization","neural-architecture-search","pytorch","reinforcement-learning"],"created_at":"2024-08-02T02:01:08.566Z","updated_at":"2025-12-29T11:55:56.205Z","avatar_url":"https://github.com/leopard-ai.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://leopard-ai.github.io/betty/\"\u003e\n    \u003cimg src=\"https://raw.githubusercontent.com/leopard-ai/betty/main/docs/_static/imgs/logo.svg\" alt=\"\" width=\"40%\" align=\"top\"\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\u003cp align=\"center\"\u003e\n  An automatic differentiation library for generalized meta-learning and multilevel optimization\u003cbr\u003e\n  \u003ca href=\"https://leopard-ai.github.io/betty/\"\u003eDocs\u003c/a\u003e |\n  \u003ca href=\"https://leopard-ai.github.io/betty/tutorial/basic/basic.html\"\u003eTutorials\u003c/a\u003e |\n  \u003ca href=\"https://github.com/leopard-ai/betty/tree/main/examples\"\u003eExamples\u003c/a\u003e |\n  \u003ca href=\"https://openreview.net/pdf?id=LV_MeMS38Q9\"\u003ePaper\u003c/a\u003e |\n  \u003ca href=\"https://github.com/leopard-ai/betty#citation\"\u003eCitation\u003c/a\u003e |\n  \u003ca href=\"https://www.casl-project.ai/\"\u003eCASL\u003c/a\u003e\n\u003c/p\u003e\n\n\u003cdiv align=\"center\"\u003e\n\n  \u003ca href=\"https://pypi.org/project/betty-ml/\"\u003e![Version](https://img.shields.io/pypi/v/betty-ml)\u003c/a\u003e\n  \u003ca href=\"https://github.com/leopard-ai/betty/tree/main/test\"\u003e![Testing](https://img.shields.io/github/actions/workflow/status/leopard-ai/betty/test.yaml?branch=main)\u003c/a\u003e\n  [![License](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](https://github.com/leopard-ai/betty/blob/main/LICENSE)\n  \u003ca href=\"https://arxiv.org/abs/2207.02849\"\u003e![arXiv](https://img.shields.io/badge/arXiv-2207.02489-b31b1b.svg)\u003c/a\u003e\n  \u003ca href=\"https://github.com/psf/black\"\u003e\u003cimg src=\"https://img.shields.io/badge/code%20style-black-000000.svg\" alt=\"code style: black\"\u003e\u003c/a\u003e\n  \u003ca href=\"https://join.slack.com/t/betty-n2l2441/shared_invite/zt-1ojhxizmt-NTmj2aVi3BuQQ6hjhNBTFQ\" target=\"_blank\"\u003e\n    \u003cimg alt=\"Slack\" src=\"https://img.shields.io/badge/Slack-Join%20Slack-blueviolet?logo=slack\" /\u003e\n  \u003c/a\u003e\n  \n\u003c/div\u003e\n\n```bash\npip install betty-ml\n```\n\n## Update\n\n**[Sep 22 2023]** \"SAMA: Making Scalable Meta Learning Practical\" got accepted at [NeurIPS 2023](https://openreview.net/forum?id=LV_MeMS38Q9)!\n\n**[Jan 21 2023]** Betty got accepted as a *notable-top-5% (oral)* paper at [ICLR 2023](https://openreview.net/forum?id=LV_MeMS38Q9)!\n\n**[Jan 12 2023]** We release *Betty v0.2* with new distributed training support for meta-learning! Currently\navailable features are:\n\n- Distributed Data Parallel [(DDP)](https://pytorch.org/tutorials/intermediate/ddp_tutorial.html)\n- ZeRO Redundancy Optimizer [(ZeRO)](https://pytorch.org/tutorials/recipes/zero_redundancy_optimizer.html)\n- *(experimental)* Fully Sharded Data Parallel [(FSDP)](https://pytorch.org/tutorials/intermediate/FSDP_tutorial.html)\n\nYou can now easily scale up meta-learning (or even meta-meta-learning) with one-liner change!\n\n- **Example**: [Meta-Weight-Net with RoBERTa](examples/bert_data_reweighting/)\n- **Tutorial**: [link](https://leopard-ai.github.io/betty/tutorial/intermediate/intermediate_distributed.html)\n\n## Introduction\n\nBetty is a [PyTorch](https://pytorch.org) library for generalized meta-learning (GML)\nand multilevel optimization (MLO) that allows a **simple** and **modular**\nprogramming interface for a number of **large-scale** applications including\n[meta-learning](examples/implicit_maml/),\n[hyperparameter optimization](examples/logistic_regression_hpo/),\n[neural architecture search](examples/neural_architecture_search/),\n[data reweighting](examples/learning_to_reweight/), and many more.\n\nWith Betty, users simply need to do two things to implement any GML/MLO programs:\n\n1. Define each level's optimization problem using the [Problem](#problem) class.\n2. Define the hierarchical problem structure using the [Engine](#engine) class.\n\n## Quick Start\n\n### Problem\n\n#### Basics\n\nEach level problem can be defined with seven components: (1) module, (2) optimizer, (3)\ndata loader, (4) loss function, (5) problem configuration, (6) name, and (7) other\noptional components (e.g.  learning rate scheduler). The loss function (4) can be\ndefined via the `training_step` method, while all other components can be provided\nthrough the class constructor. For example, an image classification problem can be\ndefined as follows:\n\n```python\nfrom betty.problems import ImplicitProblem\nfrom betty.configs import Config\n\n# set up module, optimizer, data loader (i.e. (1)-(3))\ncls_module, cls_optimizer, cls_data_loader = setup_classification()\n\nclass Classifier(ImplicitProblem):\n    # set up loss function\n    def training_step(self, batch):\n        inputs, labels = batch\n        outputs = self.module(inputs)\n        loss = F.cross_entropy(outputs, labels)\n\n        return loss\n\n# set up problem configuration\ncls_config = Config(type='darts', unroll_steps=1, log_step=100)\n\n# Classifier problem class instantiation\ncls_prob = Classifier(name='classifier',\n                      module=cls_module,\n                      optimizer=cls_optimizer,\n                      train_data_loader=cls_data_loader,\n                      config=cls_config)\n```\n\n#### Interactions between problems\n\nIn GML/MLO, each problem will often need to access modules from other problems to\ndefine its loss function. This can be achieved by using the `name` attribute as\nfollows:\n\n```python\nclass HPO(ImplicitProblem):\n    def training_step(self, batch):\n        # set up hyperparameter optimization loss\n        ...\n\n# HPO problem class instantiation\nhpo_prob = HPO(name='hpo', module=...)\n\nclass Classifier(ImplicitProblem):\n    def training_step(self, batch):\n        inputs, labels = batch\n        outputs = self.module(inputs)\n        loss = F.cross_entropy(outputs, labels)\n        \n        \"\"\"\n        accessing weight decay hyperparameter from another\n        problem HPO can be achieved by its name 'hpo'\n        \"\"\"\n        weight_decay = self.hpo()\n        reg_loss = weight_decay * sum(\n            [p.norm().pow(2) for p in self.module.parameters()]\n        )\n        \n        return loss + reg_loss\n\ncls_prob = Classifier(name='classifier', module=...)\n```\n\n### Engine\n\n#### Basics\n\nThe `Engine` class handles the hierarchical dependencies between problems. In GML/MLO,\nthere are two types of dependencies: upper-to-lower (`u2l`) and lower-to-upper (`l2u`).\nBoth types of dependencies can be defined with a Python dictionary, where the key is\nthe starting node and the value is the list of destination nodes.\n\n```python\nfrom betty import Engine\nfrom betty.configs import EngineConfig\n\n# set up all involved problems\nproblems = [cls_prob, hpo_prob]\n\n# set up upper-to-lower and lower-to-upper dependencies\nu2l = {hpo_prob: [cls_prob]}\nl2u = {cls_prob: [hpo_prob]}\ndependencies = {'u2l': u2l, 'l2u': l2u}\n\n# set up Engine configuration\nengine_config = EngineConfig(train_iters=10000, valid_step=100)\n\n# instantiate Engine class\nengine = Engine(problems=problems,\n                dependencies=dependencies,\n                config=engine_config)\n\n# execute multilevel optimization\nengine.run()\n```\n\nSince `Engine` manages the whole GML/MLO program, you can also perform a global validation\nstage within it. All problems that comprise the GML/MLO program can again be accessed with\ntheir names.\n\n```python\nclass HPOEngine(Engine):\n    # set up global validation\n    @torch.no_grad()\n    def validation(self):\n        loss = 0\n        for inputs, labels in test_loader:\n            outputs = self.classifer(inputs)\n            loss += F.cross_entropy(outputs, targets)\n            \n        # Returned dict will be automatically logged after each validation\n        return {'loss': loss}\n...\nengine = HPOEngine(problems=problems,\n                   dependencies=dependencies,\n                   config=engine_config)\nengine.run()\n```\n\nOnce we define all optimization problems and the hierarchical dependencies between them\nwith, respectively, the `Problem` class and the `Engine` class, all complicated internal\nmechanisms of GML/MLO such as gradient calculation and optimization execution order will\nbe handled by Betty. For more details and advanced features, users can check out our\n[Documentation](https://leopard-ai.github.io/betty/) and\n[Tutorials](https://leopard-ai.github.io/betty/tutorial/basic/basic.html).\n\nHappy multilevel optimization programming!\n\n## Applications\n\nWe provide reference implementations of several GML/MLO applications, including:\n\n- [Hyperparameter Optimization](examples/logistic_regression_hpo/)\n- [Neural Architecture Search](examples/neural_architecture_search/)\n- [Data Reweighting](examples/learning_to_reweight/)\n- [Domain Adaptation for Pretraining \u0026 Finetuning](examples/learning_by_ignoring/)\n- [(Implicit) Model-Agnostic Meta-Learning](examples/implicit_maml)\n\nWhile each of the above examples traditionally has a distinct implementation style, note\nthat our implementations share the same code structure thanks to Betty. More examples\nare on the way!\n\n\u003cp align=\"center\"\u003e\n    \u003cimg src=\"https://raw.githubusercontent.com/leopard-ai/betty/main/docs/_static/imgs/mlo.png\" alt=\"\" width=\"100%\" align=\"top\"\u003e\n\u003c/p\u003e\n\n## Features\n\n### Gradient Approximation Methods\n\n- Implicit Differentiation\n  - Finite Difference (or T1-T2) ([DARTS: Differentiable Architecture Search](https://arxiv.org/abs/1806.09055))\n  - Neumann Series ([Optimizing Millions of Hyperparameters by Implicit Differentiation](http://proceedings.mlr.press/v108/lorraine20a/lorraine20a.pdf))\n  - Conjugate Gradient ([Meta-Learning with Implicit Gradients](https://proceedings.neurips.cc/paper/2019/file/072b030ba126b2f4b2374f342be9ed44-Paper.pdf))\n- Iterative Differentiation\n  - Reverse-mode Automatic Differentiation ([Model-Agnostic Meta-Learning (MAML)](https://arxiv.org/abs/1703.03400))\n\n### Training\n\n- Gradient accumulation\n- FP16/BF16 training\n- Distributed data-parallel training\n- Gradient clipping\n\n### Logging\n\n- [(PyTorch) TensorBoard](https://pytorch.org/docs/stable/tensorboard.html)\n- [wandb](https://github.com/wandb/client)\n\n## Contributing\n\nWe welcome contributions from the community! Please see our [contributing\nguidelines](CONTRIBUTING.md) for details on how to contribute to Betty.\n\n## Citation\n\nIf you use Betty in your research, please cite [our\npaper](https://arxiv.org/abs/2207.02849) with the following Bibtex entry.\n\n```\n@inproceedings{\nchoe2023betty,\ntitle={Betty: An Automatic Differentiation Library for Multilevel Optimization},\nauthor={Sang Keun Choe and Willie Neiswanger and Pengtao Xie and Eric Xing},\nbooktitle={The Eleventh International Conference on Learning Representations },\nyear={2023},\nurl={https://openreview.net/forum?id=LV_MeMS38Q9}\n}\n```\n\n## License\n\nBetty is licensed under the [Apache 2.0 License](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleopard-ai%2Fbetty","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fleopard-ai%2Fbetty","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleopard-ai%2Fbetty/lists"}