{"about":{"site":"https://codewithpapers.app","non_affiliation":"Code with Papers and Syntology are not affiliated with, endorsed by, or sponsored by Papers with Code, Meta, or the pwc-archive mirror.","licence":"CC BY-SA 4.0","licence_url":"https://creativecommons.org/licenses/by-sa/4.0/legalcode","attribution":"https://codewithpapers.app/attribution","modified":"archive material modified by Syntology; see the attribution page"},"url":"/paper/fine-tuning-language-models-with-just-forward-1","title":"Fine-Tuning Language Models with Just Forward Passes","arxiv_id":"2305.17333","date":"2023-05-27","proceeding":"NeurIPS 2023 11","authors":["Sadhika Malladi","Tianyu Gao","Eshaan Nichani","Alex Damian","Jason D. Lee","Danqi Chen","Sanjeev Arora"],"abstract":"Fine-tuning language models (LMs) has yielded success on diverse downstream tasks, but as LMs grow in size, backpropagation requires a prohibitively large amount of memory. Zeroth-order (ZO) methods can in principle estimate gradients using only two forward passes but are theorized to be catastrophically slow for optimizing large models. In this work, we propose a memory-efficient zerothorder optimizer (MeZO), adapting the classical ZO-SGD method to operate in-place, thereby fine-tuning LMs with the same memory footprint as inference. For example, with a single A100 80GB GPU, MeZO can train a 30-billion parameter model, whereas fine-tuning with backpropagation can train only a 2.7B LM with the same budget. We conduct comprehensive experiments across model types (masked and autoregressive LMs), model scales (up to 66B), and downstream tasks (classification, multiple-choice, and generation). Our results demonstrate that (1) MeZO significantly outperforms in-context learning and linear probing; (2) MeZO achieves comparable performance to fine-tuning with backpropagation across multiple tasks, with up to 12x memory reduction and up to 2x GPU-hour reduction in our implementation; (3) MeZO is compatible with both full-parameter and parameter-efficient tuning techniques such as LoRA and prefix tuning; (4) MeZO can effectively optimize non-differentiable objectives (e.g., maximizing accuracy or F1). We support our empirical findings with theoretical insights, highlighting how adequate pre-training and task prompts enable MeZO to fine-tune huge models, despite classical ZO analyses suggesting otherwise.","url_abs":"https://arxiv.org/abs/2305.17333v3","url_pdf":"https://arxiv.org/pdf/2305.17333v3.pdf","source":{"archive":"pwc-archive (Hugging Face), CC BY-SA 4.0","snapshot":"2025-07-28","licence_url":"https://creativecommons.org/licenses/by-sa/4.0/legalcode","row_kind":"abstracts"},"code_links":[{"paper_slug":"fine-tuning-language-models-with-just-forward-1","repo_url":"https://github.com/princeton-nlp/mezo","is_official":1,"mentioned_in_paper":1,"mentioned_in_github":0,"framework":"pytorch","reach":{"status":"ok","spdx":"MIT"}},{"paper_slug":"fine-tuning-language-models-with-just-forward-1","repo_url":"https://github.com/liangyuwang/zo2","is_official":0,"mentioned_in_paper":0,"mentioned_in_github":1,"framework":"pytorch","reach":null},{"paper_slug":"fine-tuning-language-models-with-just-forward-1","repo_url":"https://github.com/mathisall/zo-adamu","is_official":0,"mentioned_in_paper":0,"mentioned_in_github":1,"framework":"pytorch","reach":{"status":"ok"}}],"tasks":[{"task_slug":null,"task_name":"GPU"},{"task_slug":"in-context-learning","task_name":"In-Context Learning"},{"task_slug":"multiple-choice","task_name":"Multiple-choice"}],"methods":[],"datasets_introduced":[],"methods_introduced":[],"results":[],"syntology":{"syntology_url":"https://syntology.ai/paper/2305.17333","atlas_url":"https://app.syntology.ai/?focus=2305.17333","mcp":{"get_harvested_code_for_paper":{"arxiv_id":"2305.17333"}},"developers":"https://syntology.ai/developers","read_at":"2026-09-25T09:33:49+00:00","read_at_is":"when the build read Syntology's graph, not when any sample ran","claim":"Per-sample execution status on synthesized fixtures; not a correctness claim about the paper. Samples come from repositories linked to the paper, official or community; repo_kind says which.","repos":[{"provenance":"external:paperswithcode_snapshot_2025-07-28","url":"https://github.com/princeton-nlp/mezo","reach":{"status":"ok","spdx":"MIT"}},{"provenance":"external:paperswithcode_snapshot_2025-07-28","url":"https://github.com/liangyuwang/zo2","reach":null},{"provenance":"external:paperswithcode_snapshot_2025-07-28","url":"https://github.com/mathisall/zo-adamu","reach":{"status":"ok"}}],"summary":{"ran":7,"ran_draft_wrong":2,"unverified":8},"by_repo_kind":{"official":{"samples":14,"ran":7,"repositories":1},"listed":{"samples":3,"ran":2,"repositories":1}},"repo_kind_vocabulary":{"official":"The archive marks this repository official for the paper","named_in_paper":"The archive records that the paper mentions this repository; it is not marked official","listed":"In the archive's code links for this paper, not marked official and not recorded as mentioned in the paper","found_in_text":"Syntology found this repository in the paper's own text; whether it is the authors' implementation is not asserted","community":"Not in the archive's code links for this paper; a community repository Syntology harvested"},"n_pointer_only_for_licence":0,"samples":[{"code_sha256_prefix":"506b0c89834f1424","entry":"BaseOptimizer","repo":"liangyuwang/zo2","repo_kind":"listed","path":"zo2/optimizer/mezo_sgd/zo.py","file_url":"https://github.com/liangyuwang/zo2/blob/HEAD/zo2/optimizer/mezo_sgd/zo.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"506b0c89834f1424"}},{"code_sha256_prefix":"f9a6c1b932010ab6","entry":"MeZOSGD","repo":"liangyuwang/zo2","repo_kind":"listed","path":"zo2/optimizer/mezo_sgd/zo.py","file_url":"https://github.com/liangyuwang/zo2/blob/HEAD/zo2/optimizer/mezo_sgd/zo.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":"deterministic","behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"f9a6c1b932010ab6"}},{"code_sha256_prefix":"7c5d9ba063f6642c","entry":"encode_prompt","repo":"princeton-nlp/mezo","repo_kind":"official","path":"large_models/utils.py","file_url":"https://github.com/princeton-nlp/mezo/blob/HEAD/large_models/utils.py","link_basis":"plan_row","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"7c5d9ba063f6642c"}},{"code_sha256_prefix":"b5adfd6ac99a1b18","entry":"find_module","repo":"princeton-nlp/mezo","repo_kind":"official","path":"large_models/lora.py","file_url":"https://github.com/princeton-nlp/mezo/blob/HEAD/large_models/lora.py","link_basis":"plan_row","language":"python","status":"ran_draft_wrong","verification_level":1,"contract_check":"OUTPUT_MISDECLARED","metamorphic_tier":"deterministic","behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"b5adfd6ac99a1b18"}},{"code_sha256_prefix":"54a6124d697bc2c2","entry":"forward_wrap_with_option_len","repo":"princeton-nlp/mezo","repo_kind":"official","path":"large_models/utils.py","file_url":"https://github.com/princeton-nlp/mezo/blob/HEAD/large_models/utils.py","link_basis":"plan_row","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"54a6124d697bc2c2"}},{"code_sha256_prefix":"e768e25ca95f70cd","entry":"input_example_to_string","repo":"princeton-nlp/mezo","repo_kind":"official","path":"medium_models/src/dataset.py","file_url":"https://github.com/princeton-nlp/mezo/blob/HEAD/medium_models/src/dataset.py","link_basis":"plan_row","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"e768e25ca95f70cd"}},{"code_sha256_prefix":"fbef70dc7c700501","entry":"input_example_to_tuple","repo":"princeton-nlp/mezo","repo_kind":"official","path":"medium_models/src/dataset.py","file_url":"https://github.com/princeton-nlp/mezo/blob/HEAD/medium_models/src/dataset.py","link_basis":"plan_row","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"fbef70dc7c700501"}},{"code_sha256_prefix":"e7e75981cb464788","entry":"normalize_answer","repo":"princeton-nlp/mezo","repo_kind":"official","path":"large_models/metrics.py","file_url":"https://github.com/princeton-nlp/mezo/blob/HEAD/large_models/metrics.py","link_basis":"harvester_set","language":"python","status":"ran_draft_wrong","verification_level":1,"contract_check":"OUTPUT_MISDECLARED","metamorphic_tier":"deterministic","behaviour_fingerprint":true,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"e7e75981cb464788"}},{"code_sha256_prefix":"9a62fa939fc563a4","entry":"tokenize_multipart_input","repo":"princeton-nlp/mezo","repo_kind":"official","path":"medium_models/src/dataset.py","file_url":"https://github.com/princeton-nlp/mezo/blob/HEAD/medium_models/src/dataset.py","link_basis":"plan_row","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"9a62fa939fc563a4"}},{"code_sha256_prefix":"00b3e776ab20cb69","entry":"MeZOSGDConfig","repo":"liangyuwang/zo2","repo_kind":"listed","path":"zo2/optimizer/mezo_sgd/zo.py","file_url":"https://github.com/liangyuwang/zo2/blob/HEAD/zo2/optimizer/mezo_sgd/zo.py","link_basis":"first_harvest_node","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"00b3e776ab20cb69"}},{"code_sha256_prefix":"bdb460cb247501dc","entry":"attn_forward_hook","repo":"princeton-nlp/mezo","repo_kind":"official","path":"large_models/prefix.py","file_url":"https://github.com/princeton-nlp/mezo/blob/HEAD/large_models/prefix.py","link_basis":"plan_row","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"bdb460cb247501dc"}},{"code_sha256_prefix":"84c658f2c9252878","entry":"calculate_metric","repo":"princeton-nlp/mezo","repo_kind":"official","path":"large_models/metrics.py","file_url":"https://github.com/princeton-nlp/mezo/blob/HEAD/large_models/metrics.py","link_basis":"plan_row","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"84c658f2c9252878"}},{"code_sha256_prefix":"b989d4bce26f77ce","entry":"f1","repo":"princeton-nlp/mezo","repo_kind":"official","path":"large_models/metrics.py","file_url":"https://github.com/princeton-nlp/mezo/blob/HEAD/large_models/metrics.py","link_basis":"plan_row","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"b989d4bce26f77ce"}},{"code_sha256_prefix":"f7c384f1e681ea7a","entry":"get_task","repo":"princeton-nlp/mezo","repo_kind":"official","path":"large_models/tasks.py","file_url":"https://github.com/princeton-nlp/mezo/blob/HEAD/large_models/tasks.py","link_basis":"plan_row","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"f7c384f1e681ea7a"}},{"code_sha256_prefix":"84920a8095dca4fa","entry":"prepare_inputs_for_generation","repo":"princeton-nlp/mezo","repo_kind":"official","path":"large_models/prefix.py","file_url":"https://github.com/princeton-nlp/mezo/blob/HEAD/large_models/prefix.py","link_basis":"plan_row","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"84920a8095dca4fa"}},{"code_sha256_prefix":"c239cb1add74b809","entry":"tensor_all_gather","repo":"princeton-nlp/mezo","repo_kind":"official","path":"medium_models/src/linearhead_trainer.py","file_url":"https://github.com/princeton-nlp/mezo/blob/HEAD/medium_models/src/linearhead_trainer.py","link_basis":"plan_row","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"c239cb1add74b809"}},{"code_sha256_prefix":"734d0491ecbc7841","entry":"varsize_tensor_all_gather","repo":"princeton-nlp/mezo","repo_kind":"official","path":"medium_models/src/linearhead_trainer.py","file_url":"https://github.com/princeton-nlp/mezo/blob/HEAD/medium_models/src/linearhead_trainer.py","link_basis":"plan_row","language":"python","status":"unverified","verification_level":0,"contract_check":null,"metamorphic_tier":null,"behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"734d0491ecbc7841"}}]},"arxiv_metadata":null,"syntology_extracted_results":null}