-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Scale and Mask #2738
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
Closed
Closed
Scale and Mask #2738
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
ad378bc
added fields Scale and Mask into protobuf and abstract module
LeicongLi 2a3db7b
add scale and mask fields into protobuf and BigDL module
LeicongLi 11e14bd
add scale and mask fields into protobuf and BigDL module
LeicongLi 6b2be05
add scale and mask field into protobuf and bigdl module
LeicongLi bf5ad1d
add scale and mask field into protobuf and bigdl module
LeicongLi cba5033
create trait to hold scales and mask which enables fp32 to int8 conve…
LeicongLi 70c8c67
create trait to hold scales and mask which enables fp32 to int8 conve…
LeicongLi 324c728
create trait to hold scales and mask which enables fp32 to int8 conve…
LeicongLi f7e8655
create trait to hold scales and mask which enables fp32 to int8 conve…
LeicongLi 39153c7
change comments to adjust StyleCheck
LeicongLi 728e917
change comments to adjust StyleCheck
LeicongLi 83b23d1
change comments to adjust StyleCheck
LeicongLi 2bea2f7
added unit test for save/load scale and mask
LeicongLi a584215
added unit test for save/load scale and mask
LeicongLi 54c10db
fix syntax
LeicongLi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
1,129 changes: 1,029 additions & 100 deletions
1,129
spark/dl/src/main/java/com/intel/analytics/bigdl/serialization/Bigdl.java
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
164 changes: 164 additions & 0 deletions
164
spark/dl/src/main/scala/com/intel/analytics/bigdl/nn/mkldnn/MklInt8Convertible.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| /* | ||
| * Copyright 2016 The BigDL Authors. | ||
| * | ||
| * Licensed 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. | ||
| */ | ||
| package com.intel.analytics.bigdl.nn.mkldnn | ||
|
|
||
| import scala.collection.mutable.ArrayBuffer | ||
|
|
||
| /** | ||
| * Trait which provides MKL-DNN functionality to convert FP32 model to INT8 model | ||
| */ | ||
| trait MklInt8Convertible { | ||
| // input dimension mask | ||
| protected var inDimMask: Int = 0 | ||
| // output dimension mask | ||
| protected var outDimMask: Int = 0 | ||
| // input scales | ||
| private[mkldnn] var inScalesBuffer: ArrayBuffer[Array[Float]] = ArrayBuffer.empty[Array[Float]] | ||
| // output scales | ||
| private[mkldnn] var outScalesBuffer: ArrayBuffer[Array[Float]] = ArrayBuffer.empty[Array[Float]] | ||
|
|
||
|
|
||
| /** | ||
| * Get dimension mask of input | ||
| * @return inDimMask field which stores value of input dimension mask | ||
| */ | ||
| def getInputDimMask(): Int = { | ||
| inDimMask | ||
| } | ||
|
|
||
| /** | ||
| * Set dimension mask of input | ||
| * @param mask value of input dimension mask to be set | ||
| * @return Unit | ||
| */ | ||
| def setInputDimMask(mask: Int) : Unit = { | ||
| inDimMask = mask | ||
| } | ||
|
|
||
| /** | ||
| * Get dimension mask of output | ||
| * @return outDimMask field which stores value of output dimension mask | ||
| */ | ||
| def getOutputDimMask(): Int = { | ||
| outDimMask | ||
| } | ||
|
|
||
| /** | ||
| * Set dimension mask of output | ||
| * @param mask value of output dimension mask to be set | ||
| * @return Unit | ||
| */ | ||
| def setOutputDimMask(mask: Int): Unit = { | ||
| outDimMask = mask | ||
| } | ||
|
|
||
| /** | ||
| * Get input scales | ||
| * @return field which stores value of input scales | ||
| */ | ||
| def getInputScales(): Array[Array[Float]] = { | ||
| inScalesBuffer.toArray | ||
| } | ||
|
|
||
| /** | ||
| * Set input scales | ||
| * Clear existing buffer of input scales, and place updated scales into the cleared buffer | ||
| * @param inScales value of input scales to be set | ||
| * @return Unit | ||
| */ | ||
| def setInputScales(inScales: Array[Array[Float]]): Unit = { | ||
| inScalesBuffer.clear() | ||
| inScales.foreach(appendInputScales) | ||
| } | ||
|
|
||
| /** | ||
| * Get output scales | ||
| * @return field which stores value of output scales | ||
| */ | ||
| def getOutputScales(): Array[Array[Float]] = { | ||
| outScalesBuffer.toArray | ||
| } | ||
|
|
||
| /** | ||
| * Set output scales | ||
| * Clear existing buffer of output scales, and place updated scales into the cleared buffer | ||
| * @param outScales value of output scales to be set | ||
| * @return Unit | ||
| */ | ||
| def setOutputScales(outScales: Array[Array[Float]]): Unit = { | ||
| outScalesBuffer.clear() | ||
| outScales.foreach(appendOutputScales) | ||
| } | ||
|
|
||
| /** | ||
| * Append a scale, an array of float, into input scales buffer | ||
| * @param scale value of an input scale to be appended | ||
| * @return Unit | ||
| */ | ||
| private def appendInputScales(scale: Array[Float]): Unit = { | ||
| inScalesBuffer.append(scale) | ||
| } | ||
|
|
||
| /** | ||
| * Append a scale, an array of float, into output scales buffer | ||
| * @param scale value of an output scale to be appended | ||
| * @return Unit | ||
| */ | ||
| private def appendOutputScales(scale: Array[Float]): Unit = { | ||
| outScalesBuffer.append(scale) | ||
| } | ||
|
|
||
| /** | ||
| * Update input scales at specific index with provided new scale | ||
| * @param scale the new scale | ||
| * @param index the index of which the scale need to be updated | ||
| * @return Unit | ||
| */ | ||
| def updateInputScales(scale: Array[Float], index: Int): Unit = { | ||
| updateScalesHelper(inScalesBuffer, scale, index) | ||
| } | ||
|
|
||
| /** | ||
| * Update output scales at specific index with provided new scale | ||
| * @param scale the new scale | ||
| * @param index the index of which the scale need to be updated | ||
| * @return Unit | ||
| */ | ||
| def updateOutputSclaes(scale: Array[Float], index: Int): Unit = { | ||
| updateScalesHelper(outScalesBuffer, scale, index) | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Scales update helper. Replace scale at specific index with provided new scale | ||
| * @param scales the scales arrayBuffer to be updated | ||
| * @param scale the new scale | ||
| * @param index the index of which the scale need to be updated | ||
| * @return Unit | ||
| */ | ||
| private def updateScalesHelper(scales: ArrayBuffer[Array[Float]], | ||
| scale: Array[Float], index: Int): Unit = { | ||
| if (scales.length - 1 < index) { | ||
| scales.append(scale) | ||
| } | ||
|
|
||
| scales(index).indices.foreach(i => | ||
| if (scale(i) > scales(index)(i)) { | ||
| scales(index)(i) = scale(i) | ||
| }) | ||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
By BigDL conversion, I prefer to use
inputDimMask, similar below foroutputDimMask