Skip to content

Commit

Permalink
Merge pull request intel-analytics#4 from JerryYanWan/deepspeech
Browse files Browse the repository at this point in the history
deepspeech2 inference
  • Loading branch information
YY-OnCall authored May 19, 2017
2 parents d26dc50 + 69dbe5e commit 7d199e7
Show file tree
Hide file tree
Showing 28 changed files with 3,019 additions and 22 deletions.
70 changes: 48 additions & 22 deletions pipeline/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<groupId>com.intel.analytics</groupId>
<artifactId>pipeline</artifactId>
<version>0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<repositories>
<repository>
Expand Down Expand Up @@ -56,17 +57,36 @@
</repositories>

<properties>
<java.version>1.7</java.version>
<javac.version>1.7</javac.version>
<scala.major.version>2.10</scala.major.version>
<scala.version>2.10.5</scala.version>
<scala.macros.version>2.0.1</scala.macros.version>

<java.version>1.8</java.version>
<!--<javac.version>1.8</javac.version>-->
<scala.major.version>2.11</scala.major.version>
<scala.version>2.11.8</scala.version>
<scala.macros.version>2.11</scala.macros.version>
<scalatest.version>2.2.4</scalatest.version>
<spark.version>1.5.1</spark.version>
<spark-scope>provided</spark-scope>
<spark.version>2.0.1</spark.version>
<spark-scope>compile</spark-scope>

</properties>

<dependencies>

<dependency>
<groupId>com.intel.analytics.bigdl</groupId>
<artifactId>bigdl-SPARK_2.0</artifactId>
<version>0.1.0</version>
</dependency>
<dependency>
<groupId>jflac</groupId>
<artifactId>jflac</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>com.github.fommil</groupId>
<artifactId>jniloader</artifactId>
<version>1.1</version>
</dependency>

<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-compiler</artifactId>
Expand Down Expand Up @@ -103,32 +123,38 @@
<version>${scalatest.version}</version>
<scope>${spark-scope}</scope>
</dependency>
<dependency>
<groupId>com.intel.analytics.bigdl</groupId>
<artifactId>bigdl</artifactId>
<version>0.2.0-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-core_${scala.major.version}</artifactId>
<version>${spark.version}</version>
<scope>${spark-scope}</scope>
<!--<scope>${spark-scope}</scope>-->
</dependency>
<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-sql_${scala.major.version}</artifactId>
<version>${spark.version}</version>
</dependency>

<dependency>
<groupId>org.apache.spark</groupId>
<artifactId>spark-mllib_${scala.major.version}</artifactId>
<version>${spark.version}</version>
<scope>${spark-scope}</scope>
<!--<scope>${spark-scope}</scope>-->
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
</plugin>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.0</version>
<!--<version>3.2.0</version>-->
<executions>
<execution>
<id>eclipse-add-source</id>
Expand Down Expand Up @@ -169,13 +195,13 @@
</args>
<!-- The following plugin is required to use quasiquotes in Scala 2.10 and is used
by Spark SQL for code generation. -->
<compilerPlugins>
<compilerPlugin>
<groupId>org.scalamacros</groupId>
<artifactId>paradise_${scala.version}</artifactId>
<version>${scala.macros.version}</version>
</compilerPlugin>
</compilerPlugins>
<!--<compilerPlugins>-->
<!--<compilerPlugin>-->
<!--<groupId>org.scalamacros</groupId>-->
<!--<artifactId>paradise_${scala.version}</artifactId>-->
<!--<version>${scala.macros.version}</version>-->
<!--</compilerPlugin>-->
<!--</compilerPlugins>-->
</configuration>
</plugin>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

### ds2 model (~387MB):
https://drive.google.com/open?id=0B_s7AwBOnuD-ckRqQWM3WFctZmM

### sample audio files:

Please upload your samples audio to HDFS. For instance, hdfs://127.0.0.1:9001/deepspeech/data

Please save your ds2.model to HDFS. For instance, hdfs://127.0.0.1:9001/deepspeech/data/ds2.model

### run on clusters

```shell
./bigdl.sh -- spark-submit --master local[1] \
--conf spark.driver.memory=20g \
--conf "spark.serializer=org.apache.spark.serializer.JavaSerializer" \
--class com.intel.analytics.zoo.pipeline.deepspeech2.example.InferenceEvaluate \
pipeline-0.1-SNAPSHOT-jar-with-dependencies.jar \
--host hdfs://127.0.0.1:9001 --path /deepspeech/data
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/bin/sh

#
# 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.
#

#
# This script is to set right environment variables for BigDL library. User can use this script to
# invoke command(e.g. spark-submit), like this
# bigdl.sh [options] -- command
# Note that the command should be placed behind the -- symbol. And the options of bigdl should be
# placed before the -- symbol.
#
# User can also source this script, then can run command without the script. Like this
# source bigdl.sh [options]
# command1
# command2
#
# Options:
# -c : core number to run program. Note that it will be override by Spark configuration, like
# n of local[n] in Spark local mode, or --executor-cores in Spark cluster mode. So it only
# takes affect when program don't run on Spark.
#

CMD=""
EXIT_CODE=0

set_mkl_blas() {
# TODO: Generate export statements in the build from spark-bigdl.conf
export OMP_NUM_THREADS=1
export KMP_BLOCKTIME=0
export OMP_WAIT_POLICY=passive
export DL_ENGINE_TYPE=mklblas
export MKL_DISABLE_FAST_MM=1
}

parse() {
while [ $# -gt 0 ]; do
key="$1"
case $key in
-c|--core)
export DL_CORE_NUMBER="$2"
shift 2
;;
-l|--local)
export BIGDL_LOCAL_MODE="1"
shift 1
;;
--)
shift
for token in "$@"; do
case "$token" in
*\ * )
# add quto when there's space in the token
CMD="$CMD '$token'"
;;
*)
CMD="$CMD $token"
;;
esac
done
shift $# # escape from the while loop
;;
*)
echo "Invalid option $key"
shift $# # escape from the while loop
EXIT_CODE=1
;;
esac
done
}

parse "$@"

if [ $EXIT_CODE -ne 0 ]; then
return $EXIT_CODE
fi

# set the envs based on the mode
set_mkl_blas

if [ "$CMD" != "" ]; then
eval $CMD
fi
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* 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

import com.intel.analytics.bigdl.nn._
import com.intel.analytics.bigdl.nn.abstractnn.TensorModule
import com.intel.analytics.bigdl.tensor.{DoubleType, FloatType, Tensor}
import com.intel.analytics.bigdl.tensor.TensorNumericMath.TensorNumeric

import scala.reflect.ClassTag

@SerialVersionUID( - 467695939363389565L)
class BatchNormalizationDS[@specialized(Float, Double) T: ClassTag](
nOutput: Int,
eps: Double = 1e-3,
momentum: Double = 0.1,
affine: Boolean = false)
(implicit ev: TensorNumeric[T])
extends BatchNormalization[T](nOutput, eps, momentum, affine) {

val batchDim = 0
val timeDim = 1
val featDim = 2

override def updateOutput(input: Tensor[T]): Tensor[T] = {
require(input.dim == 3,
"In BatchNormalizationBRNN, input should be a 3D tensor, [B, T, D]" +
s"input.dim = ${input.dim}")
val size = input.size()
input.resize(size(batchDim) * size(timeDim), size(featDim))
output = super.updateOutput(input)
output.resize(size)
input.resize(size)
output
}

override def backward(input: Tensor[T], gradOutput: Tensor[T]): Tensor[T] = {
val size = input.size()
input.resize(size(batchDim) * size(timeDim), size(featDim))
gradOutput.resize(size(batchDim) * size(timeDim), size(featDim))
val result = super.backward(input, gradOutput)
input.resize(size)
gradOutput.resize(size)
result.resize(size)
result
}

override def updateGradInput(input: Tensor[T], gradOutput: Tensor[T]): Tensor[T] = {
val size = input.size()
input.resize(size(batchDim) * size(timeDim), size(featDim))
gradOutput.resize(size(batchDim) * size(timeDim), size(featDim))
gradInput = super.updateGradInput(input, gradOutput)
gradInput.resize(size)
input.resize(size)
gradOutput.resize(size)
gradInput
}

override def accGradParameters(input: Tensor[T], gradOutput: Tensor[T], scale: Double): Unit = {
val size = input.size()
input.resize(size(batchDim) * size(timeDim), size(featDim))
gradOutput.resize(size(batchDim) * size(timeDim), size(featDim))
super.accGradParameters(input, gradOutput)
input.resize(size)
gradOutput.resize(size)
}

override def toString(): String = s"BatchNormalizationBRNN"

override def canEqual(other: Any): Boolean = other.isInstanceOf[BatchNormalizationDS[T]]

override def equals(other: Any): Boolean = other match {
case that: BatchNormalizationDS[T] =>
super.equals(that) &&
(that canEqual this)
case _ => false
}

override def hashCode(): Int = {
val state = Seq(super.hashCode(), batchDim, timeDim, featDim)
state.map(_.hashCode()).foldLeft(0)((a, b) => 31 * a + b)
}
}

object BatchNormalizationDS {
def apply[@specialized(Float, Double) T: ClassTag](
nOutput: Int,
eps: Double = 1e-5) (implicit ev: TensorNumeric[T]): BatchNormalizationDS[T] = {
new BatchNormalizationDS[T](nOutput, eps)
}
}
Loading

0 comments on commit 7d199e7

Please sign in to comment.