-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[microNPU] Tweak a layout transform matrix #10763
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
"""Common methods for the NPU tensor expressions""" | ||
|
||
from typing import Tuple, List | ||
|
||
|
||
def get_layout_transform_matrices(ofm_channels: int) -> Tuple[List[List[float]], List[List[float]]]: | ||
"""Get the NHWC->NHCWB16 and NHCWB16->NHWC layout transform matrices. | ||
For information about the supported layouts see https://developer.arm.com/documentation/102420/ | ||
0200/Functional-description/Control-and-data-flow/Supported-memory-formats-for-feature-maps | ||
|
||
Parameters | ||
---------- | ||
ofm_channels : int | ||
The number of output channels in a NHWC layout | ||
|
||
Returns | ||
------- | ||
nhwc_to_nhcwb16, nhcwb16_to_nhwc : Tuple[List[List[float]], List[List[float]]] | ||
The layout transformation matrices | ||
""" | ||
|
||
# The value of the last dimension (B16) is always 16. | ||
nhwc_to_nhcwb16 = [ | ||
[1, 0, 0, 0, 0], | ||
[0, 1, 0, 0, 0], | ||
[0, 0, 0, 1 / 16, 0], | ||
[0, 0, 1, 0, 0], | ||
[0, 0, 0, 0, 16], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. docs : might worth putting a comment to indicate that b16 axis is always going to be of fixed size of 16. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
[0, 0, 0, 0, 1], | ||
] | ||
|
||
# When we convert from NHWC to NHCWB16, the new C value is given by | ||
# (ofm_channels - 1) // 16 + 1, which is a lossy operation, so we need to use | ||
# the actual value of channels in the transform matrix to accurately recover | ||
# the C in NHWC when we convert from NHCWB16 to NHWC. | ||
nhcwb16_to_nhwc = [ | ||
[1, 0, 0, 0, 0, 0], | ||
[0, 1, 0, 0, 0, 0], | ||
[0, 0, 0, 1, 0, 0], | ||
[0, 0, 0, 0, 0, ofm_channels], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. docs : lets state that the conversion of nhwc_to_nhcwb16 is lossy (because b16 axis is fixed to 16). Therefore, to recover the original "c" of "nhwc", we need to use the original channels in the transform matrix. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
[0, 0, 0, 0, 0, 1], | ||
] | ||
|
||
return nhwc_to_nhcwb16, nhcwb16_to_nhwc |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
docs : Lets link to this page : https://developer.arm.com/documentation/102420/0200/Functional-description/Control-and-data-flow/Supported-memory-formats-for-feature-maps here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done