From 9272bc7f4361d4d48465cb4e6da98b200393568f Mon Sep 17 00:00:00 2001 From: komaksym Date: Mon, 29 Sep 2025 14:19:30 +0200 Subject: [PATCH 1/9] init new question --- .../190_gradient-accumulation/description.md | 3 ++ .../190_gradient-accumulation/example.json | 5 ++ questions/190_gradient-accumulation/learn.md | 47 +++++++++++++++++++ questions/190_gradient-accumulation/meta.json | 12 +++++ .../pytorch/solution.py | 2 + .../pytorch/starter_code.py | 2 + .../pytorch/tests.json | 6 +++ .../190_gradient-accumulation/solution.py | 3 ++ .../190_gradient-accumulation/starter_code.py | 4 ++ .../190_gradient-accumulation/tests.json | 6 +++ .../tinygrad/solution.py | 2 + .../tinygrad/starter_code.py | 2 + .../tinygrad/tests.json | 6 +++ 13 files changed, 100 insertions(+) create mode 100644 questions/190_gradient-accumulation/description.md create mode 100644 questions/190_gradient-accumulation/example.json create mode 100644 questions/190_gradient-accumulation/learn.md create mode 100644 questions/190_gradient-accumulation/meta.json create mode 100644 questions/190_gradient-accumulation/pytorch/solution.py create mode 100644 questions/190_gradient-accumulation/pytorch/starter_code.py create mode 100644 questions/190_gradient-accumulation/pytorch/tests.json create mode 100644 questions/190_gradient-accumulation/solution.py create mode 100644 questions/190_gradient-accumulation/starter_code.py create mode 100644 questions/190_gradient-accumulation/tests.json create mode 100644 questions/190_gradient-accumulation/tinygrad/solution.py create mode 100644 questions/190_gradient-accumulation/tinygrad/starter_code.py create mode 100644 questions/190_gradient-accumulation/tinygrad/tests.json diff --git a/questions/190_gradient-accumulation/description.md b/questions/190_gradient-accumulation/description.md new file mode 100644 index 00000000..684dbcd6 --- /dev/null +++ b/questions/190_gradient-accumulation/description.md @@ -0,0 +1,3 @@ +## Problem + +Write a concise problem description here. diff --git a/questions/190_gradient-accumulation/example.json b/questions/190_gradient-accumulation/example.json new file mode 100644 index 00000000..4e7fdd99 --- /dev/null +++ b/questions/190_gradient-accumulation/example.json @@ -0,0 +1,5 @@ +{ + "input": "...", + "output": "...", + "reasoning": "Explain why the output follows from the input." +} diff --git a/questions/190_gradient-accumulation/learn.md b/questions/190_gradient-accumulation/learn.md new file mode 100644 index 00000000..31c0cec5 --- /dev/null +++ b/questions/190_gradient-accumulation/learn.md @@ -0,0 +1,47 @@ +## Solution Explanation + +Add intuition, math, and step-by-step reasoning here. + +### Writing Mathematical Expressions with LaTeX + +This editor supports LaTeX for rendering mathematical equations and expressions. Here's how you can use it: + +1. **Inline Math**: + - Wrap your expression with single `$` symbols. + - Example: `$E = mc^2$` → Renders as: ( $E = mc^2$ ) + +2. **Block Math**: + - Wrap your expression with double `$$` symbols. + - Example: + ``` + $$ + \int_a^b f(x) \, dx + $$ + ``` + Renders as: + $$ + \int_a^b f(x) \, dx + $$ + +3. **Math Functions**: + - Use standard LaTeX functions like `\frac`, `\sqrt`, `\sum`, etc. + - Examples: + - `$\frac{a}{b}$` → ( $\frac{a}{b}$ ) + - `$\sqrt{x}$` → ( $\sqrt{x}$ ) + +4. **Greek Letters and Symbols**: + - Use commands like `\alpha`, `\beta`, etc., for Greek letters. + - Example: `$\alpha + \beta = \gamma$` → ( $\alpha + \beta = \gamma$ ) + +5. **Subscripts and Superscripts**: + - Use `_{}` for subscripts and `^{}` for superscripts. + - Examples: + - `$x_i$` → ( $x_i$ ) + - `$x^2$` → ( $x^2$ ) + +6. **Combined Examples**: + - `$\sum_{i=1}^n i^2 = \frac{n(n+1)(2n+1)}{6}$` + Renders as: + $\sum_{i=1}^n i^2 = \frac{n(n+1)(2n+1)}{6}$ + +Feel free to write your own mathematical expressions, and they will be rendered beautifully in the preview! diff --git a/questions/190_gradient-accumulation/meta.json b/questions/190_gradient-accumulation/meta.json new file mode 100644 index 00000000..9db2e26a --- /dev/null +++ b/questions/190_gradient-accumulation/meta.json @@ -0,0 +1,12 @@ +{ + "id": "XXX", + "title": "TITLE GOES HERE", + "difficulty": "medium", + "category": "Machine Learning", + "video": "", + "likes": "0", + "dislikes": "0", + "contributor": [], + "tinygrad_difficulty": "", + "pytorch_difficulty": "" +} diff --git a/questions/190_gradient-accumulation/pytorch/solution.py b/questions/190_gradient-accumulation/pytorch/solution.py new file mode 100644 index 00000000..9b74bcbd --- /dev/null +++ b/questions/190_gradient-accumulation/pytorch/solution.py @@ -0,0 +1,2 @@ +def your_function(...): + ... diff --git a/questions/190_gradient-accumulation/pytorch/starter_code.py b/questions/190_gradient-accumulation/pytorch/starter_code.py new file mode 100644 index 00000000..d3e5beb5 --- /dev/null +++ b/questions/190_gradient-accumulation/pytorch/starter_code.py @@ -0,0 +1,2 @@ +def your_function(...): + pass diff --git a/questions/190_gradient-accumulation/pytorch/tests.json b/questions/190_gradient-accumulation/pytorch/tests.json new file mode 100644 index 00000000..e4e4b180 --- /dev/null +++ b/questions/190_gradient-accumulation/pytorch/tests.json @@ -0,0 +1,6 @@ +[ + { + "test": "print(your_function(...))", + "expected_output": "..." + } +] diff --git a/questions/190_gradient-accumulation/solution.py b/questions/190_gradient-accumulation/solution.py new file mode 100644 index 00000000..b1ff1c5b --- /dev/null +++ b/questions/190_gradient-accumulation/solution.py @@ -0,0 +1,3 @@ +def your_function(...): + # reference implementation + ... diff --git a/questions/190_gradient-accumulation/starter_code.py b/questions/190_gradient-accumulation/starter_code.py new file mode 100644 index 00000000..564b3118 --- /dev/null +++ b/questions/190_gradient-accumulation/starter_code.py @@ -0,0 +1,4 @@ +# Implement your function below. + +def your_function(...): + pass diff --git a/questions/190_gradient-accumulation/tests.json b/questions/190_gradient-accumulation/tests.json new file mode 100644 index 00000000..e4e4b180 --- /dev/null +++ b/questions/190_gradient-accumulation/tests.json @@ -0,0 +1,6 @@ +[ + { + "test": "print(your_function(...))", + "expected_output": "..." + } +] diff --git a/questions/190_gradient-accumulation/tinygrad/solution.py b/questions/190_gradient-accumulation/tinygrad/solution.py new file mode 100644 index 00000000..9b74bcbd --- /dev/null +++ b/questions/190_gradient-accumulation/tinygrad/solution.py @@ -0,0 +1,2 @@ +def your_function(...): + ... diff --git a/questions/190_gradient-accumulation/tinygrad/starter_code.py b/questions/190_gradient-accumulation/tinygrad/starter_code.py new file mode 100644 index 00000000..d3e5beb5 --- /dev/null +++ b/questions/190_gradient-accumulation/tinygrad/starter_code.py @@ -0,0 +1,2 @@ +def your_function(...): + pass diff --git a/questions/190_gradient-accumulation/tinygrad/tests.json b/questions/190_gradient-accumulation/tinygrad/tests.json new file mode 100644 index 00000000..e4e4b180 --- /dev/null +++ b/questions/190_gradient-accumulation/tinygrad/tests.json @@ -0,0 +1,6 @@ +[ + { + "test": "print(your_function(...))", + "expected_output": "..." + } +] From 32ca92d17949382219f3f074ca4aa2c658006435 Mon Sep 17 00:00:00 2001 From: komaksym Date: Mon, 29 Sep 2025 14:25:45 +0200 Subject: [PATCH 2/9] add description --- questions/190_gradient-accumulation/description.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/questions/190_gradient-accumulation/description.md b/questions/190_gradient-accumulation/description.md index 684dbcd6..0dd68528 100644 --- a/questions/190_gradient-accumulation/description.md +++ b/questions/190_gradient-accumulation/description.md @@ -1,3 +1,3 @@ ## Problem -Write a concise problem description here. +Write a Python function `accumulate_gradients` that takes a list of numpy arrays (each representing gradients from a mini-batch) and returns a single numpy array representing the sum of all gradients. All input arrays will have the same shape. Only use standard Python and numpy. The returned array should be of type float and have the same shape as the input arrays. From fa72d2ad3cfeb1d511ea4cdd779c314a139167d3 Mon Sep 17 00:00:00 2001 From: komaksym Date: Mon, 29 Sep 2025 14:25:58 +0200 Subject: [PATCH 3/9] add learn about section --- questions/190_gradient-accumulation/learn.md | 81 ++++++++------------ 1 file changed, 34 insertions(+), 47 deletions(-) diff --git a/questions/190_gradient-accumulation/learn.md b/questions/190_gradient-accumulation/learn.md index 31c0cec5..e137bf1c 100644 --- a/questions/190_gradient-accumulation/learn.md +++ b/questions/190_gradient-accumulation/learn.md @@ -1,47 +1,34 @@ -## Solution Explanation - -Add intuition, math, and step-by-step reasoning here. - -### Writing Mathematical Expressions with LaTeX - -This editor supports LaTeX for rendering mathematical equations and expressions. Here's how you can use it: - -1. **Inline Math**: - - Wrap your expression with single `$` symbols. - - Example: `$E = mc^2$` → Renders as: ( $E = mc^2$ ) - -2. **Block Math**: - - Wrap your expression with double `$$` symbols. - - Example: - ``` - $$ - \int_a^b f(x) \, dx - $$ - ``` - Renders as: - $$ - \int_a^b f(x) \, dx - $$ - -3. **Math Functions**: - - Use standard LaTeX functions like `\frac`, `\sqrt`, `\sum`, etc. - - Examples: - - `$\frac{a}{b}$` → ( $\frac{a}{b}$ ) - - `$\sqrt{x}$` → ( $\sqrt{x}$ ) - -4. **Greek Letters and Symbols**: - - Use commands like `\alpha`, `\beta`, etc., for Greek letters. - - Example: `$\alpha + \beta = \gamma$` → ( $\alpha + \beta = \gamma$ ) - -5. **Subscripts and Superscripts**: - - Use `_{}` for subscripts and `^{}` for superscripts. - - Examples: - - `$x_i$` → ( $x_i$ ) - - `$x^2$` → ( $x^2$ ) - -6. **Combined Examples**: - - `$\sum_{i=1}^n i^2 = \frac{n(n+1)(2n+1)}{6}$` - Renders as: - $\sum_{i=1}^n i^2 = \frac{n(n+1)(2n+1)}{6}$ - -Feel free to write your own mathematical expressions, and they will be rendered beautifully in the preview! +# **Gradient Accumulation** + +## **1. Definition** +Gradient accumulation is a technique used in machine learning to simulate larger batch sizes by accumulating gradients over multiple mini-batches before performing an optimizer step. Instead of updating the model parameters after every mini-batch, gradients are summed (accumulated) over several mini-batches, and the update is performed only after a specified number of accumulations. + +## **2. Why Use Gradient Accumulation?** +* **Simulate Large Batch Training:** Allows training with an effective batch size larger than what fits in memory by splitting it into smaller mini-batches. +* **Stabilize Training:** Larger effective batch sizes can lead to more stable gradient estimates and smoother convergence. +* **Hardware Constraints:** Useful when GPU/TPU memory is limited and cannot accommodate large batches directly. + +## **3. Gradient Accumulation Mechanism** +Given a list of gradient arrays $g_1, g_2, \ldots, g_N$ (from $N$ mini-batches), the accumulated gradient $G$ is computed as: + +$$ +G = \sum_{i=1}^N g_i +$$ + +Where: +* $g_i$: The gradient array from the $i$-th mini-batch (numpy array) +* $N$: The number of mini-batches to accumulate +* $G$: The accumulated gradient (numpy array of the same shape) + +**Example:** +If $g_1 = [1, 2]$, $g_2 = [3, 4]$, $g_3 = [5, 6]$: +* $G = [1+3+5, 2+4+6] = [9, 12]$ + +## **4. Applications of Gradient Accumulation** +Gradient accumulation is widely used in training: +* **Large Models:** When training large models that require large batch sizes for stability or convergence. +* **Distributed Training:** To synchronize gradients across multiple devices or nodes. +* **Memory-Constrained Environments:** When hardware cannot fit the desired batch size in memory. +* **Any optimization problem** where effective batch size needs to be increased without increasing memory usage. + +Gradient accumulation is a simple yet powerful tool to enable flexible and efficient training in modern machine learning workflows. From 812f5453f6884ebb43229eb4c7965169a20f8cfc Mon Sep 17 00:00:00 2001 From: komaksym Date: Mon, 29 Sep 2025 14:26:04 +0200 Subject: [PATCH 4/9] add solution --- questions/190_gradient-accumulation/solution.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/questions/190_gradient-accumulation/solution.py b/questions/190_gradient-accumulation/solution.py index b1ff1c5b..5f4bd817 100644 --- a/questions/190_gradient-accumulation/solution.py +++ b/questions/190_gradient-accumulation/solution.py @@ -1,3 +1,13 @@ -def your_function(...): - # reference implementation - ... +import numpy as np + +def accumulate_gradients(grad_list): + """ + Accumulates (sums) a list of gradient arrays into a single array. + + Args: + grad_list (list of np.ndarray): List of gradient arrays, all of the same shape. + + Returns: + np.ndarray: The accumulated (summed) gradients, same shape as input arrays. + """ + return np.sum(grad_list, axis=0).astype(float) From 836a78192a7b56755706cc6a025ac5bbff9c8056 Mon Sep 17 00:00:00 2001 From: komaksym Date: Mon, 29 Sep 2025 14:26:12 +0200 Subject: [PATCH 5/9] add starter code --- questions/190_gradient-accumulation/starter_code.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/questions/190_gradient-accumulation/starter_code.py b/questions/190_gradient-accumulation/starter_code.py index 564b3118..af0a948c 100644 --- a/questions/190_gradient-accumulation/starter_code.py +++ b/questions/190_gradient-accumulation/starter_code.py @@ -1,4 +1,14 @@ +import numpy as np + # Implement your function below. +def accumulate_gradients(grad_list): + """ + Accumulates (sums) a list of gradient arrays into a single array. + + Args: + grad_list (list of np.ndarray): List of gradient arrays, all of the same shape. -def your_function(...): + Returns: + np.ndarray: The accumulated (summed) gradients, same shape as input arrays. + """ pass From f68b184851595023f04390cf937c01b3f477c961 Mon Sep 17 00:00:00 2001 From: komaksym Date: Mon, 29 Sep 2025 14:26:26 +0200 Subject: [PATCH 6/9] add example --- questions/190_gradient-accumulation/example.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/questions/190_gradient-accumulation/example.json b/questions/190_gradient-accumulation/example.json index 4e7fdd99..e53b0514 100644 --- a/questions/190_gradient-accumulation/example.json +++ b/questions/190_gradient-accumulation/example.json @@ -1,5 +1,5 @@ { - "input": "...", - "output": "...", - "reasoning": "Explain why the output follows from the input." + "input": "import numpy as np\ngrad_list = [np.array([1.0, 2.0]), np.array([3.0, 4.0]), np.array([5.0, 6.0])]\naccum = accumulate_gradients(grad_list)\nprint(accum)", + "output": "[9. 12.]", + "reasoning": "The sum of [1.0, 2.0], [3.0, 4.0], and [5.0, 6.0] is [9.0, 12.0]. Each element is summed across all arrays." } From 40c6b83b3eac58949df421f97016de4099a75e44 Mon Sep 17 00:00:00 2001 From: komaksym Date: Mon, 29 Sep 2025 14:26:40 +0200 Subject: [PATCH 7/9] add metadata --- questions/190_gradient-accumulation/meta.json | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/questions/190_gradient-accumulation/meta.json b/questions/190_gradient-accumulation/meta.json index 9db2e26a..bed716c2 100644 --- a/questions/190_gradient-accumulation/meta.json +++ b/questions/190_gradient-accumulation/meta.json @@ -1,12 +1,10 @@ { - "id": "XXX", - "title": "TITLE GOES HERE", - "difficulty": "medium", + "id": "190", + "title": "Gradient Accumulation", + "difficulty": "easy", "category": "Machine Learning", "video": "", "likes": "0", "dislikes": "0", - "contributor": [], - "tinygrad_difficulty": "", - "pytorch_difficulty": "" + "contributor": ["https://github.com/komaksym"] } From 48ba83753deab6920aaceea4e06682dc13455210 Mon Sep 17 00:00:00 2001 From: komaksym Date: Mon, 29 Sep 2025 14:26:47 +0200 Subject: [PATCH 8/9] add tests --- .../190_gradient-accumulation/tests.json | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/questions/190_gradient-accumulation/tests.json b/questions/190_gradient-accumulation/tests.json index e4e4b180..55d2bf67 100644 --- a/questions/190_gradient-accumulation/tests.json +++ b/questions/190_gradient-accumulation/tests.json @@ -1,6 +1,22 @@ [ { - "test": "print(your_function(...))", - "expected_output": "..." + "test": "import numpy as np\ngrad_list = [np.array([1.0, 2.0]), np.array([3.0, 4.0]), np.array([5.0, 6.0])]\nprint(accumulate_gradients(grad_list))", + "expected_output": "[ 9. 12.]" + }, + { + "test": "import numpy as np\ngrad_list = [np.array([0.0, 0.0]), np.array([0.0, 0.0])]\nprint(accumulate_gradients(grad_list))", + "expected_output": "[0. 0.]" + }, + { + "test": "import numpy as np\ngrad_list = [np.array([1.5, -2.5]), np.array([-1.5, 2.5])]\nprint(accumulate_gradients(grad_list))", + "expected_output": "[0. 0.]" + }, + { + "test": "import numpy as np\ngrad_list = [np.array([10.0]), np.array([20.0]), np.array([30.0])]\nprint(accumulate_gradients(grad_list))", + "expected_output": "[60.]" + }, + { + "test": "import numpy as np\ngrad_list = [np.array([1.0, 2.0, 3.0]), np.array([4.0, 5.0, 6.0])]\nprint(accumulate_gradients(grad_list))", + "expected_output": "[5. 7. 9.]" } ] From 2bc53e64df9504adfef5f13229e4a8eb61504557 Mon Sep 17 00:00:00 2001 From: komaksym Date: Fri, 17 Oct 2025 13:31:08 +0200 Subject: [PATCH 9/9] fix contributor --- questions/190_gradient-accumulation/meta.json | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/questions/190_gradient-accumulation/meta.json b/questions/190_gradient-accumulation/meta.json index bed716c2..7d921bfe 100644 --- a/questions/190_gradient-accumulation/meta.json +++ b/questions/190_gradient-accumulation/meta.json @@ -6,5 +6,10 @@ "video": "", "likes": "0", "dislikes": "0", - "contributor": ["https://github.com/komaksym"] + "contributor": [ + { + "profile_link": "https://github.com/komaksym", + "name": "komaksym" + } + ] }