{"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/sophia-a-scalable-stochastic-second-order","title":"Sophia: A Scalable Stochastic Second-order Optimizer for Language Model Pre-training","arxiv_id":"2305.14342","date":"2023-05-23","proceeding":null,"authors":["Hong Liu","Zhiyuan Li","David Hall","Percy Liang","Tengyu Ma"],"abstract":"Given the massive cost of language model pre-training, a non-trivial improvement of the optimization algorithm would lead to a material reduction on the time and cost of training. Adam and its variants have been state-of-the-art for years, and more sophisticated second-order (Hessian-based) optimizers often incur too much per-step overhead. In this paper, we propose Sophia, Second-order Clipped Stochastic Optimization, a simple scalable second-order optimizer that uses a light-weight estimate of the diagonal Hessian as the pre-conditioner. The update is the moving average of the gradients divided by the moving average of the estimated Hessian, followed by element-wise clipping. The clipping controls the worst-case update size and tames the negative impact of non-convexity and rapid change of Hessian along the trajectory. Sophia only estimates the diagonal Hessian every handful of iterations, which has negligible average per-step time and memory overhead. On language modeling with GPT models of sizes ranging from 125M to 1.5B, Sophia achieves a 2x speed-up compared to Adam in the number of steps, total compute, and wall-clock time, achieving the same perplexity with 50% fewer steps, less total compute, and reduced wall-clock time. Theoretically, we show that Sophia, in a much simplified setting, adapts to the heterogeneous curvatures in different parameter dimensions, and thus has a run-time bound that does not depend on the condition number of the loss.","url_abs":"https://arxiv.org/abs/2305.14342v4","url_pdf":"https://arxiv.org/pdf/2305.14342v4.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":"sophia-a-scalable-stochastic-second-order","repo_url":"https://github.com/stanford-crfm/levanter","is_official":0,"mentioned_in_paper":1,"mentioned_in_github":1,"framework":"jax","reach":{"status":"ok","spdx":"Apache-2.0"}},{"paper_slug":"sophia-a-scalable-stochastic-second-order","repo_url":"https://github.com/Liuhong99/Sophia","is_official":0,"mentioned_in_paper":0,"mentioned_in_github":1,"framework":"pytorch","reach":null},{"paper_slug":"sophia-a-scalable-stochastic-second-order","repo_url":"https://github.com/cor3bit/somax","is_official":0,"mentioned_in_paper":0,"mentioned_in_github":1,"framework":"jax","reach":null},{"paper_slug":"sophia-a-scalable-stochastic-second-order","repo_url":"https://github.com/helw150/levanter","is_official":0,"mentioned_in_paper":0,"mentioned_in_github":1,"framework":"jax","reach":{"status":"ok","spdx":"Apache-2.0"}},{"paper_slug":"sophia-a-scalable-stochastic-second-order","repo_url":"https://github.com/kyegomez/Sophia","is_official":0,"mentioned_in_paper":0,"mentioned_in_github":1,"framework":"pytorch","reach":null},{"paper_slug":"sophia-a-scalable-stochastic-second-order","repo_url":"https://github.com/MindSpore-scientific/code-2/tree/main/stochastic-attention-head-removal-a-simple","is_official":0,"mentioned_in_paper":0,"mentioned_in_github":0,"framework":"mindspore","reach":null},{"paper_slug":"sophia-a-scalable-stochastic-second-order","repo_url":"https://github.com/labmlai/annotated_deep_learning_paper_implementations","is_official":0,"mentioned_in_paper":0,"mentioned_in_github":0,"framework":"pytorch","reach":null}],"tasks":[{"task_slug":"language-modeling","task_name":"Language Modeling"},{"task_slug":"language-modelling","task_name":"Language Modelling"},{"task_slug":"stochastic-optimization","task_name":"Stochastic Optimization"}],"methods":[{"method_slug":"adam","method_name":"Adam"},{"method_slug":"attention","method_name":"Attention"},{"method_slug":"attention-dropout","method_name":"Attention Dropout"},{"method_slug":"bpe","method_name":"BPE"},{"method_slug":"cosine-annealing","method_name":"Cosine Annealing"},{"method_slug":"dense-connections","method_name":"Dense Connections"},{"method_slug":"discriminative-fine-tuning","method_name":"Discriminative Fine-Tuning"},{"method_slug":"dropout","method_name":"Dropout"},{"method_slug":"gpt","method_name":"GPT"},{"method_slug":"gpt-2","method_name":"GPT-2"},{"method_slug":"layer-normalization","method_name":"Layer Normalization"},{"method_slug":"linear-layer","method_name":"Linear Layer"},{"method_slug":"linear-warmup-with-cosine-annealing","method_name":"Linear Warmup With Cosine Annealing"},{"method_slug":"multi-head-attention","method_name":"Multi-Head Attention"},{"method_slug":"residual-connection","method_name":"Residual Connection"},{"method_slug":"softmax","method_name":"Softmax"},{"method_slug":"sophia","method_name":"Sophia"},{"method_slug":"weight-decay","method_name":"Weight Decay"}],"datasets_introduced":[],"methods_introduced":[{"slug":"sophia","name":"Sophia","full_name":"Second-order Clipped Stochastic Optimization"}],"results":[],"syntology":{"atlas_url":"https://app.syntology.ai/?focus=2305.14342","mcp":{"get_harvested_code_for_paper":{"arxiv_id":"2305.14342"}},"developers":"https://syntology.ai/developers","read_at":"2026-09-24T18:15:14+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/cor3bit/somax","reach":null},{"provenance":"external:paperswithcode_snapshot_2025-07-28","url":"https://github.com/kyegomez/Sophia","reach":null},{"provenance":"external:paperswithcode_snapshot_2025-07-28","url":"https://github.com/Liuhong99/Sophia","reach":null},{"provenance":"external:paperswithcode_snapshot_2025-07-28","url":"https://github.com/stanford-crfm/levanter","reach":{"status":"ok","spdx":"Apache-2.0"}},{"provenance":"external:paperswithcode_snapshot_2025-07-28","url":"https://github.com/MindSpore-scientific/code-2/tree/main/stochastic-attention-head-removal-a-simple","reach":null},{"provenance":"external:paperswithcode_snapshot_2025-07-28","url":"https://github.com/labmlai/annotated_deep_learning_paper_implementations","reach":null},{"provenance":"external:paperswithcode_snapshot_2025-07-28","url":"https://github.com/helw150/levanter","reach":{"status":"ok","spdx":"Apache-2.0"}}],"summary":{"ran":4,"ran_honours":1,"ran_draft_wrong":1,"unverified":13},"by_repo_kind":{"named_in_paper":{"samples":5,"ran":0,"repositories":1},"listed":{"samples":14,"ran":6,"repositories":4}},"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":"15bc730db57a3642","entry":"GenericAdaptiveOptimizer","repo":"labmlai/annotated_deep_learning_paper_implementations","repo_kind":"listed","path":"labml_nn/optimizers/sophia.py","file_url":"https://github.com/labmlai/annotated_deep_learning_paper_implementations/blob/HEAD/labml_nn/optimizers/sophia.py","link_basis":"first_harvest_node","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":"15bc730db57a3642"}},{"code_sha256_prefix":"a6e9300c48fec63f","entry":"Sophia","repo":"labmlai/annotated_deep_learning_paper_implementations","repo_kind":"listed","path":"labml_nn/optimizers/sophia.py","file_url":"https://github.com/labmlai/annotated_deep_learning_paper_implementations/blob/HEAD/labml_nn/optimizers/sophia.py","link_basis":"first_harvest_node","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":"a6e9300c48fec63f"}},{"code_sha256_prefix":"1436d0069dad845e","entry":"SophiaG","repo":"Liuhong99/Sophia","repo_kind":"listed","path":"sophia.py","file_url":"https://github.com/Liuhong99/Sophia/blob/HEAD/sophia.py","link_basis":"first_harvest_node","language":"python","status":"ran","verification_level":1,"contract_check":null,"metamorphic_tier":"deterministic","behaviour_fingerprint":false,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"1436d0069dad845e"}},{"code_sha256_prefix":"ba6a2ba94fbeb5e9","entry":"WeightDecay","repo":"labmlai/annotated_deep_learning_paper_implementations","repo_kind":"listed","path":"labml_nn/optimizers/sophia.py","file_url":"https://github.com/labmlai/annotated_deep_learning_paper_implementations/blob/HEAD/labml_nn/optimizers/sophia.py","link_basis":"first_harvest_node","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":"ba6a2ba94fbeb5e9"}},{"code_sha256_prefix":"a273291a5ece194d","entry":"get_lr","repo":"Liuhong99/Sophia","repo_kind":"listed","path":"train_sophiag.py","file_url":"https://github.com/Liuhong99/Sophia/blob/HEAD/train_sophiag.py","link_basis":"first_harvest_node","language":"python","status":"ran_honours","verification_level":1,"contract_check":"HONOURS","metamorphic_tier":"well_formed","behaviour_fingerprint":true,"licence":"MIT","inline_ok":true,"mcp_get_code":{"code_sha256":"a273291a5ece194d"}},{"code_sha256_prefix":"2382cb6829841947","entry":"group_texts","repo":"kyegomez/Sophia","repo_kind":"listed","path":"experiments/training.py","file_url":"https://github.com/kyegomez/Sophia/blob/HEAD/experiments/training.py","link_basis":"first_harvest_node","language":"python","status":"ran_draft_wrong","verification_level":1,"contract_check":"OUTPUT_MISDECLARED","metamorphic_tier":"deterministic","behaviour_fingerprint":false,"licence":"Apache-2.0","inline_ok":true,"mcp_get_code":{"code_sha256":"2382cb6829841947"}},{"code_sha256_prefix":"34b6a986acbf4f68","entry":"_single_tensor_sophiag","repo":"Liuhong99/Sophia","repo_kind":"listed","path":"sophia.py","file_url":"https://github.com/Liuhong99/Sophia/blob/HEAD/sophia.py","link_basis":"first_harvest_node","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":"34b6a986acbf4f68"}},{"code_sha256_prefix":"de22a502eda2c3b9","entry":"auto_metric_from_name","repo":"stanford-crfm/levanter","repo_kind":"named_in_paper","path":"src/levanter/metrics.py","file_url":"https://github.com/stanford-crfm/levanter/blob/HEAD/src/levanter/metrics.py","link_basis":"harvester_set","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":"de22a502eda2c3b9"}},{"code_sha256_prefix":"c501ae8133a11ec3","entry":"distinct_values","repo":"stanford-crfm/levanter","repo_kind":"named_in_paper","path":"src/levanter/schedule.py","file_url":"https://github.com/stanford-crfm/levanter/blob/HEAD/src/levanter/schedule.py","link_basis":"harvester_set","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":"c501ae8133a11ec3"}},{"code_sha256_prefix":"1421ef5f024f6e23","entry":"eval_loss_loop","repo":"helw150/levanter","repo_kind":"listed","path":"src/levanter/callbacks.py","file_url":"https://github.com/helw150/levanter/blob/HEAD/src/levanter/callbacks.py","link_basis":"harvester_set","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":"1421ef5f024f6e23"}},{"code_sha256_prefix":"d40382c2fb844d41","entry":"fold","repo":"stanford-crfm/levanter","repo_kind":"named_in_paper","path":"src/levanter/metrics.py","file_url":"https://github.com/stanford-crfm/levanter/blob/HEAD/src/levanter/metrics.py","link_basis":"harvester_set","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":"d40382c2fb844d41"}},{"code_sha256_prefix":"003d0a17d2ac09ca","entry":"load_array_from_tensorstore","repo":"helw150/levanter","repo_kind":"listed","path":"src/levanter/tensorstore_serialization.py","file_url":"https://github.com/helw150/levanter/blob/HEAD/src/levanter/tensorstore_serialization.py","link_basis":"harvester_set","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":"003d0a17d2ac09ca"}},{"code_sha256_prefix":"f3fb5a3a9edbf5ef","entry":"local_device_grid_positions","repo":"helw150/levanter","repo_kind":"listed","path":"src/levanter/mesh.py","file_url":"https://github.com/helw150/levanter/blob/HEAD/src/levanter/mesh.py","link_basis":"harvester_set","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":"f3fb5a3a9edbf5ef"}},{"code_sha256_prefix":"efa87b8fc2935200","entry":"process_mesh_position","repo":"helw150/levanter","repo_kind":"listed","path":"src/levanter/mesh.py","file_url":"https://github.com/helw150/levanter/blob/HEAD/src/levanter/mesh.py","link_basis":"harvester_set","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":"efa87b8fc2935200"}},{"code_sha256_prefix":"030ef454b59ac14a","entry":"process_mesh_size","repo":"helw150/levanter","repo_kind":"listed","path":"src/levanter/mesh.py","file_url":"https://github.com/helw150/levanter/blob/HEAD/src/levanter/mesh.py","link_basis":"harvester_set","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":"030ef454b59ac14a"}},{"code_sha256_prefix":"8b3f53737bb49ef4","entry":"save_xla_dumps_to_wandb","repo":"helw150/levanter","repo_kind":"listed","path":"src/levanter/logging.py","file_url":"https://github.com/helw150/levanter/blob/HEAD/src/levanter/logging.py","link_basis":"harvester_set","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":"8b3f53737bb49ef4"}},{"code_sha256_prefix":"24947d588d9b62aa","entry":"sophiag","repo":"Liuhong99/Sophia","repo_kind":"listed","path":"sophia.py","file_url":"https://github.com/Liuhong99/Sophia/blob/HEAD/sophia.py","link_basis":"first_harvest_node","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":"24947d588d9b62aa"}},{"code_sha256_prefix":"3f1967528c23afbd","entry":"unwrap_metrics","repo":"stanford-crfm/levanter","repo_kind":"named_in_paper","path":"src/levanter/metrics.py","file_url":"https://github.com/stanford-crfm/levanter/blob/HEAD/src/levanter/metrics.py","link_basis":"harvester_set","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":"3f1967528c23afbd"}},{"code_sha256_prefix":"9b3a9ebcd8b2b32b","entry":"value_at_step","repo":"stanford-crfm/levanter","repo_kind":"named_in_paper","path":"src/levanter/schedule.py","file_url":"https://github.com/stanford-crfm/levanter/blob/HEAD/src/levanter/schedule.py","link_basis":"harvester_set","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":"9b3a9ebcd8b2b32b"}}]},"arxiv_metadata":null,"syntology_extracted_results":null}