Skip to content

Conversation

@trivedivivek
Copy link
Contributor

Summary:
This diff simplifies the the matrix multiplication scale equation in q_linear op.

The existing equation in q_linear op is:

for i in K / 4
  sums[c] = mat1_tex . (qmat2(c) scales[c])
  out += sums

where c = [0, 4), out, sums, mat1_tex and qmat2 are vectors and scales is a scalar.

The dot product is associative with respect to scalar multiplication as mentioned in https://en.wikipedia.org/wiki/Dot_product ie. (ac1).(bc2) = c1c2(a.b)
Thus, the multiplication can be rearranged as:

for i in K / 4
  sums[c] = (mat1_tex . qmat2(c)) scales[c]
  out += sums

Using distributive property of multiplication ie. ab + ac + ad ... = a(b + c+ d...) the code can be further simplified to:

for i in K / 4
  sums[c] = mat1_tex . qmat2(c)
  out += sums
out *= scale

This rearrangement significantly reduces redundant multiplications.

Reviewed By: SS-JIA

Differential Revision: D64479405

@pytorch-bot
Copy link

pytorch-bot bot commented Oct 22, 2024

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/6437

Note: Links to docs will display an error until the docs builds have been completed.

❗ 1 Active SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

✅ No Failures

As of commit 9443a2b with merge base 4f12131 (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 22, 2024
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D64479405

trivedivivek added a commit to trivedivivek/executorch that referenced this pull request Oct 22, 2024
… and improve power and performance. (pytorch#6437)

Summary:

This diff simplifies the the matrix multiplication scale equation in q_linear op.

The existing equation in q_linear op is:
```
for i in K / 4
  sums[c] = mat1_tex . (qmat2(c) scales[c])
  out += sums
```
where c = [0, 4), out, sums, mat1_tex and qmat2 are vectors and scales is a scalar.

The dot product is associative with respect to scalar multiplication as mentioned in https://en.wikipedia.org/wiki/Dot_product ie. (ac1).(bc2) = c1c2(a.b)
Thus, the multiplication can be rearranged as:
```
for i in K / 4
  sums[c] = (mat1_tex . qmat2(c)) scales[c]
  out += sums
```
Using distributive property of multiplication ie. ab + ac + ad ... = a(b + c+ d...) the code can be further simplified to:

```
for i in K / 4
  sums[c] = mat1_tex . qmat2(c)
  out += sums
out *= scale
```

This rearrangement significantly reduces redundant multiplications.

Reviewed By: SS-JIA

Differential Revision: D64479405
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D64479405

trivedivivek added a commit to trivedivivek/executorch that referenced this pull request Oct 23, 2024
… and improve power and performance. (pytorch#6437)

Summary:

This diff simplifies the the matrix multiplication scale equation in q_linear op.

The existing equation in q_linear op is:
```
for i in K / 4
  sums[c] = mat1_tex . (qmat2(c) scales[c])
  out += sums
```
where c = [0, 4), out, sums, mat1_tex and qmat2 are vectors and scales is a scalar.

The dot product is associative with respect to scalar multiplication as mentioned in https://en.wikipedia.org/wiki/Dot_product ie. (ac1).(bc2) = c1c2(a.b)
Thus, the multiplication can be rearranged as:
```
for i in K / 4
  sums[c] = (mat1_tex . qmat2(c)) scales[c]
  out += sums
```
Using distributive property of multiplication ie. ab + ac + ad ... = a(b + c+ d...) the code can be further simplified to:

```
for i in K / 4
  sums[c] = mat1_tex . qmat2(c)
  out += sums
out *= scale
```

This rearrangement significantly reduces redundant multiplications.

Reviewed By: SS-JIA

Differential Revision: D64479405
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D64479405

… and improve power and performance. (pytorch#6437)

Summary:

This diff simplifies the the matrix multiplication scale equation in q_linear op.

The existing equation in q_linear op is:
```
for i in K / 4
  sums[c] = mat1_tex . (qmat2(c) scales[c])
  out += sums
```
where c = [0, 4), out, sums, mat1_tex and qmat2 are vectors and scales is a scalar.

The dot product is associative with respect to scalar multiplication as mentioned in https://en.wikipedia.org/wiki/Dot_product ie. (ac1).(bc2) = c1c2(a.b)
Thus, the multiplication can be rearranged as:
```
for i in K / 4
  sums[c] = (mat1_tex . qmat2(c)) scales[c]
  out += sums
```
Using distributive property of multiplication ie. ab + ac + ad ... = a(b + c+ d...) the code can be further simplified to:

```
for i in K / 4
  sums[c] = mat1_tex . qmat2(c)
  out += sums
out *= scale
```

This rearrangement significantly reduces redundant multiplications.

Reviewed By: SS-JIA, jorgep31415

Differential Revision: D64479405
@facebook-github-bot
Copy link
Contributor

This pull request was exported from Phabricator. Differential Revision: D64479405

@facebook-github-bot facebook-github-bot merged commit fa30e80 into pytorch:main Oct 23, 2024
46 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. fb-exported

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants