Skip to content
Snippets Groups Projects

[add] Element wise backward

Merged Jerome Hue requested to merge jeromeh/aidge_backend_cpu:element-wise-backward into dev
All threads resolved!

Context

Fix #37 (closed)

Modified Files

  • MulImpl.hpp and MulImpl_kernels.hpp : Refactor backward pass of Mul, see explantions below.
  • MulImpl.cpp
  • Test_MulImpl.cpp : Test the new backward pass. The tests consists of 4 tests with fixed values, and one with random values (but fixed dimensions).

Description of the changes

Overview

The backward kernel computes gradients for the multiplication operation while handling broadcasting. For each element in the output gradient tensor, it:

  1. Determines which input elements contributed to this output (considering broadcasting)
  2. Applies the chain rule for multiplication to compute gradients
    grad_input_0[i] += grad_output[k] * input1[j]
    grad_input_1[j] += grad_output[k] * input0[i]
    where i,j are the contributing indices from input0 and input1 for output position k

Example

`input0` [1,3]: [[1,2,3]]
`input1` [4,3]: [[4,5,6],
                 [7,8,9],
                 [1,2,3],
                 [4,5,6]]
Output shape: [4,3], thus `outputDims` = [4,3], `dims0` = [1,3], and `dims1` = [4,3].
Broadcasted shapes:
- getBroadcastedDims(outputDims, dims0): [1,3]
- getBroadcastedDims(outputDims, dims1): [4,3]

For instance, assume we multiply two tensors A and B, with A of shape [1,3] and B of shape [4,3].

input0 = [[1,2,3]]

input1 = [[4, 5, 6], [7, 8, 9], [1, 2, 3], [4, 5, 6]]

Let's also assumes that the output gradient is full of 1s.

Then we iterate for i = 0 to output.size():

  • At iteration 0 / index [0,0]

    → The element at index [0,0] of input0 contributed to the output value

    → Thelement at index [0,0] of input1 contributed to the output value.

grad_input_0[0] += grad_output[0,0] * input0[0,0]
grad_input_1[0] += grad_output[0,0] * input1[0,0]
  • At iteration 1 / index [0,1]

    → The element at index [0,1] of input0 contributed to the output value.

    → The element at index [0,1] of input1 contributed to the output value.

grad_input_0[1] += grad_output[0,1] * input1[0,1]
grad_input_1[1] += grad_output[0,1] * input0[0,1]
  • At Iteration 2 / index [0,2]

    → The element at index [0,2] of input0 contributed to the output value.

    → The element at index [0,2] of input1 contributed to the output value.

grad_input_0[2] += grad_output[0,2] * input1[0,2] 
grad_input_1[2] += grad_output[0,2] * input0[0,2]
  • At Iteration 3 / index [1,0]

    → The element at index [0,0] of input0 contributed to the output value (due to broadcasting!). This can be determined by looking at the dimensions of getBroadcastedDims(outputDims, dims0), which is 1 for index 0. It indicates that the dimension has been broacasted for the output, or that both dimensions are equal to 1.

    → The element at index [1,0] of input1 contributed to the output value.

// Updates:
grad_input_0[0] += grad_output[1,0] * input1[1,0]
grad_input_1[3] += grad_output[1,0] * input0[0,0]

etc.

Edited by Jerome Hue

Merge request reports

Loading
Loading

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
  • Jerome Hue added 1 commit

    added 1 commit

    • d0b0ef0a - chore: Rename test section to be more explicit

    Compare with previous version

  • Jerome Hue added 1 commit

    added 1 commit

    • 104de16d - chore: Rename a test section

    Compare with previous version

  • Jerome Hue changed the description

    changed the description

  • Jerome Hue added 1 commit

    added 1 commit

    • 7361d007 - chore: Rename some variables to be more explicit

    Compare with previous version

  • Jerome Hue added 26 commits

    added 26 commits

    • 7361d007...07996a12 - 15 commits from branch eclipse/aidge:dev
    • ab18b9db - 1 earlier commit
    • 36a15c19 - WIP/TODO Test Mul bacward function
    • 76888f9a - WIP Improve mul backward kernel
    • adc30d44 - chore: Rename variables and add a new test section
    • 4d681856 - chore: Refactor Mul operator test
    • 49b0a2c1 - chore: Comply with clang-tidy rules
    • 191881f5 - chore: Rename test sections
    • 26635709 - chore: Remove commented code
    • 8f237f4c - chore: Rename test section to be more explicit
    • 6acc82fe - chore: Rename a test section
    • 1c5ca468 - chore: Rename some variables to be more explicit

    Compare with previous version

  • Maxence Naud resolved all threads

    resolved all threads

  • Maxence Naud approved this merge request

    approved this merge request

  • Jerome Hue changed the description

    changed the description

  • Jerome Hue added 14 commits

    added 14 commits

    • 1c5ca468...7d527920 - 8 commits from branch eclipse/aidge:dev
    • 11103c91 - WIP: Improved Mul Backward pass
    • 8a23e6fb - WIP/TODO Test Mul bacward function
    • a4b0e02a - WIP Improve mul backward kernel
    • b6f651b9 - chore: Rename variables and add a new test section
    • 1101341c - chore: Remove commented code
    • 63ccf533 - chore: Rename some variables to be more explicit

    Compare with previous version

  • Jerome Hue reset approvals from @pineapple by pushing to the branch

    reset approvals from @pineapple by pushing to the branch

  • Jerome Hue changed the description

    changed the description

  • Jerome Hue added 3 commits

    added 3 commits

    • dc6c7efd - Change 'weightInterLeaving' for 'weightInterLeaved'
    • fc8967c7 - UPD: change log::info of some tests to the new format
    • 5a1b833d - chore: Improve and test Mul Backward kernel

    Compare with previous version

  • Jerome Hue added 10 commits

    added 10 commits

    • 5a1b833d...7d527920 - 4 commits from branch eclipse/aidge:dev
    • 11103c91 - WIP: Improved Mul Backward pass
    • 8a23e6fb - WIP/TODO Test Mul bacward function
    • a4b0e02a - WIP Improve mul backward kernel
    • b6f651b9 - chore: Rename variables and add a new test section
    • 1101341c - chore: Remove commented code
    • 63ccf533 - chore: Rename some variables to be more explicit

    Compare with previous version

  • Jerome Hue added 1 commit

    added 1 commit

    • 09a81344 - chore: Improve and test Mul Backward kernel

    Compare with previous version

  • Jerome Hue added 1 commit

    added 1 commit

    • 4fbee26f - chore: Improve and test Mul Backward kernel

    Compare with previous version

  • Maxence Naud resolved all threads

    resolved all threads

  • Maxence Naud added 20 commits

    added 20 commits

    Compare with previous version

  • Maxence Naud mentioned in commit 9d427c11

    mentioned in commit 9d427c11

  • merged

  • mentioned in issue #30 (closed)

  • Jerome Hue mentioned in merge request !134 (merged)

    mentioned in merge request !134 (merged)

  • Please register or sign in to reply
    Loading