Skip to content

Commit c399b94

Browse files
committed
Add a Naming plugin
1 parent 53ef8c1 commit c399b94

File tree

6 files changed

+63
-6
lines changed

6 files changed

+63
-6
lines changed

build.sbt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ lazy val `plugins-Layers` = project.dependsOn(DeepLearning)
1010

1111
lazy val `plugins-Weights` = project.dependsOn(DeepLearning)
1212

13+
lazy val `plugins-Naming` = project.dependsOn(`plugins-Layers`, `plugins-Weights`)
14+
1315
lazy val `plugins-Logging` = project.dependsOn(`plugins-Layers`, `plugins-Weights`)
1416

1517
lazy val `plugins-Operators` = project
@@ -116,6 +118,7 @@ lazy val `plugins-Builtins` =
116118
`plugins-Layers`,
117119
`plugins-Weights`,
118120
`plugins-Logging`,
121+
`plugins-Naming`,
119122
`plugins-Operators`,
120123
`plugins-FloatTraining`,
121124
`plugins-FloatLiterals`,

plugins-Builtins/src/main/scala-2.11/com/thoughtworks/deeplearning/plugins/Builtins.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ trait Builtins
8787
with Layers
8888
with Weights
8989
with Logging
90+
with Naming
9091
with Operators
9192
with FloatTraining
9293
with FloatLiterals
@@ -119,4 +120,14 @@ trait Builtins
119120
with super[INDArrayLayers].ImplicitsApi
120121

121122
type Implicits <: ImplicitsApi
123+
124+
trait LayerApi extends super[Logging].LayerApi with super[Naming].LayerApi { this: Layer =>
125+
}
126+
127+
type Layer <: LayerApi
128+
129+
trait WeightApi extends super[Logging].WeightApi with super[Naming].WeightApi { this: Weight =>
130+
}
131+
132+
type Weight <: WeightApi
122133
}

plugins-Builtins/src/main/scala-2.12/com/thoughtworks/deeplearning/plugins/Builtins.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ trait Builtins
99
with Layers
1010
with Weights
1111
with Logging
12+
with Naming
1213
with Operators
1314
with FloatTraining
1415
with FloatLiterals
@@ -27,4 +28,14 @@ trait Builtins
2728
with super[DoubleLiterals].ImplicitsApi
2829

2930
type Implicits <: ImplicitsApi
31+
32+
trait LayerApi extends super[Logging].LayerApi with super[Naming].LayerApi { this: Layer =>
33+
}
34+
35+
type Layer <: LayerApi
36+
37+
trait WeightApi extends super[Logging].WeightApi with super[Naming].WeightApi { this: Weight =>
38+
}
39+
40+
type Weight <: WeightApi
3041
}

plugins-Logging/src/main/scala/com/thoughtworks/deeplearning/plugins/Logging.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ object Logging {
1313
level: Level,
1414
message: String = null,
1515
parameters: Array[AnyRef] = null,
16-
thrown: Throwable = null)(implicit fullName: sourcecode.FullName, methodName: sourcecode.Name, caller: Caller[_])
16+
thrown: Throwable = null)(implicit fullName: sourcecode.FullName, name: sourcecode.Name, caller: Caller[_])
1717
extends LogRecord(level, message) {
1818

1919
setParameters(parameters)
2020
setThrown(thrown)
2121
setLoggerName(fullName.value)
2222
setSourceClassName(caller.value.getClass.getName)
23-
setSourceMethodName(methodName.value)
23+
setSourceMethodName(name.value)
2424
}
2525

2626
trait LazyMessage extends LogRecord {
@@ -35,15 +35,15 @@ object Logging {
3535
}
3636

3737
final class ThrownInLayer(val layer: Layers#Layer, getThrown: Throwable)(implicit fullName: sourcecode.FullName,
38-
methodName: sourcecode.Name,
38+
name: sourcecode.Name,
3939
caller: Caller[_])
4040
extends ContextualLogRecord(Level.SEVERE, thrown = getThrown)
4141
with LazyMessage {
4242
override protected def makeDefaultMessage: Fastring = fast"An exception is thrown in layer $layer"
4343
}
4444

4545
final class ThrownInWeight(val weight: Weights#Weight, getThrown: Throwable)(implicit fullName: sourcecode.FullName,
46-
methodName: sourcecode.Name,
46+
name: sourcecode.Name,
4747
caller: Caller[_])
4848
extends ContextualLogRecord(Level.SEVERE, thrown = getThrown)
4949
with LazyMessage {
@@ -63,7 +63,7 @@ trait Logging extends Layers with Weights {
6363

6464
trait LayerApi extends super.LayerApi { this: Layer =>
6565
implicit protected def fullName: sourcecode.FullName
66-
implicit protected def methodName: sourcecode.Name
66+
implicit protected def name: sourcecode.Name
6767
implicit protected def caller: Caller[_]
6868
override protected def handleException(thrown: Throwable): Unit = {
6969
super.handleException(thrown)
@@ -74,7 +74,7 @@ trait Logging extends Layers with Weights {
7474

7575
trait WeightApi extends super.WeightApi { this: Weight =>
7676
implicit protected def fullName: sourcecode.FullName
77-
implicit protected def methodName: sourcecode.Name
77+
implicit protected def name: sourcecode.Name
7878
implicit protected def caller: Caller[_]
7979
override protected def handleException(thrown: Throwable): Unit = {
8080
super.handleException(thrown)

plugins-Naming/build.sbt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
libraryDependencies += "com.lihaoyi" %% "sourcecode" % "0.1.3"
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.thoughtworks.deeplearning.plugins
2+
3+
/** A plugin that automatically names [[Layer]]s and [[Weight]]s.
4+
*
5+
* @author 杨博 (Yang Bo)
6+
*/
7+
trait Naming extends Layers with Weights {
8+
9+
trait LayerApi extends super.LayerApi { this: Layer =>
10+
def fullName: sourcecode.FullName
11+
def name: sourcecode.Name
12+
13+
override def toString: String = {
14+
raw"""Layer[fullName=${fullName.value}]"""
15+
}
16+
}
17+
override type Layer <: LayerApi
18+
19+
trait WeightApi extends super.WeightApi { this: Weight =>
20+
def fullName: sourcecode.FullName
21+
def name: sourcecode.Name
22+
23+
override def toString: String = {
24+
raw"""Weight[fullName=${fullName.value}]"""
25+
}
26+
27+
}
28+
override type Weight <: WeightApi
29+
override type Implicits <: super[Layers].ImplicitsApi with super[Weights].ImplicitsApi
30+
31+
}

0 commit comments

Comments
 (0)