Skip to content

Commit

Permalink
Add the flushing denormal values option on BigDL side (#2934)
Browse files Browse the repository at this point in the history
  • Loading branch information
mengceng15 committed Oct 28, 2019
1 parent 2970c09 commit 94cf8ea
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ class ThreadPool(private var poolSize: Int) {


this.invokeAndWait2((0 until 1).map(_ => () => {
if (System.getProperty("bigdl.flushDenormalState", "true").toBoolean) {
BackendMklDnn.setFlushDenormalState()
}

require(MKL.isMKLLoaded)
require(BackendMklDnn.isLoaded)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.intel.analytics.bigdl.utils

import com.intel.analytics.bigdl.mkl.hardware.Affinity
import com.intel.analytics.bigdl.tensor.Tensor
import org.scalatest.{FlatSpec, Matchers}

import scala.concurrent.ExecutionException
Expand Down Expand Up @@ -121,4 +122,102 @@ class ThreadPoolSpec extends FlatSpec with Matchers {
results.foreach(_.get())
}
}

"setFlushDenormalState" should "influence float arithmetic operations" in {
val poolSize = 1
val ompSize = 4

val threadPool = new ThreadPool(poolSize)

System.setProperty("bigdl.flushDenormalState", "false")
threadPool.setMKLThreadOfMklDnnBackend(ompSize)

threadPool.invokeAndWait2( (0 until 1).map(i => () => {
// A denormalized value is in the range
// from 1.4E-45 to 1.18E-38,
// or from -1.18E-38 to -1.4E-45
val denormal: Float = -1.234E-41F
val floatOne: Float = 1.0F

val result: Float = denormal * floatOne
// The result should not be zero without setting
(result == 0.0F) should be(false)
}))

System.setProperty("bigdl.flushDenormalState", "true")
threadPool.setMKLThreadOfMklDnnBackend(ompSize)

threadPool.invokeAndWait2( (0 until 1).map(i => () => {
// A denormalized value is in the range
// from 1.4E-45 to 1.18E-38,
// or from -1.18E-38 to -1.4E-45
val denormal: Float = -1.234E-41F
val floatOne: Float = 1.0F

val result = denormal * floatOne
// The result should be zero with setting
(result == 0.0F) should be (true)
}))

System.clearProperty("bigdl.flushDenormalState")
}

"setFlushDenormalState" should "not influence other threads float arithmetic operations" in {
val poolSize = 1
val ompSize = 4

val threadPool1 = new ThreadPool(poolSize)
System.setProperty("bigdl.flushDenormalState", "true")
threadPool1.setMKLThreadOfMklDnnBackend(ompSize)

val threadPool2 = new ThreadPool(poolSize)
System.setProperty("bigdl.flushDenormalState", "false")
threadPool2.setMKLThreadOfMklDnnBackend(ompSize)

// A denormalized value is in the range
// from 1.4E-45 to 1.18E-38,
// or from -1.18E-38 to -1.4E-45
val denormal: Float = -1.234E-41F
val floatOne: Float = 1.0F

threadPool1.invokeAndWait2( (0 until 1).map(i => () => {
val result = denormal * floatOne
// The result should be zero with setting
(result == 0.0F) should be (true)
}))

threadPool2.invokeAndWait2( (0 until 1).map(i => () => {
val result = denormal * floatOne
// The result should not be zero without setting
(result == 0.0F) should be (false)
}))

System.clearProperty("bigdl.flushDenormalState")
}

"setFlushDenormalState" should "not influence other threads MKL operations" in {
val poolSize = 1
val ompSize = 4

val threadPool1 = new ThreadPool(poolSize)
System.setProperty("bigdl.flushDenormalState", "false")
threadPool1.setMKLThreadOfMklDnnBackend(ompSize)

val threadPool2 = new ThreadPool(poolSize)
System.setProperty("bigdl.flushDenormalState", "true")
threadPool2.setMKLThreadOfMklDnnBackend(ompSize)

val tensor = Tensor[Float](1).fill(-1.234E-41F)
val floatOne = 1.0F

threadPool1.invokeAndWait2( (0 until 1).map(i => () => {
val result = tensor.pow(floatOne)

for (i <- 0 to result.storage().array().length -1) {
assert(result.storage().array()(i) != 0.0F)
}
}))

System.clearProperty("bigdl.flushDenormalState")
}
}

0 comments on commit 94cf8ea

Please sign in to comment.