forked from apache/mxnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request apache#18 from javelinjs/scala-package-cc
Symbol creation
- Loading branch information
Showing
10 changed files
with
688 additions
and
12 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
scala-package/core/src/main/scala/ml/dmlc/mxnet/AttrScope.scala
This file contains 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,39 @@ | ||
package ml.dmlc.mxnet | ||
|
||
/** | ||
* Attribute manager for scoping. | ||
* User can also inherit this object to change naming behavior. | ||
* @author Yizhi Liu | ||
*/ | ||
class AttrScope(attr: Map[String, String] = Map.empty) { | ||
private var _attr = attr | ||
/** | ||
* Get the attribute dict given the attribute set by the symbol. | ||
* @param userDefinedAttr The attribute passed in by user during symbol creation. | ||
* @return Updated attributes to add other scope related attributes. | ||
*/ | ||
def get(userDefinedAttr: Option[Map[String, String]]): Map[String, String] = { | ||
_attr ++ userDefinedAttr.getOrElse(Map.empty[String, String]) | ||
} | ||
|
||
def withScope[T](body: => T): T = { | ||
val oldAttrScope = AttrScope.current | ||
this._attr = AttrScope.current._attr ++ this._attr | ||
AttrScope.setCurrentAttr(this) | ||
try { | ||
body | ||
} finally { | ||
AttrScope.setCurrentAttr(oldAttrScope) | ||
} | ||
} | ||
} | ||
|
||
object AttrScope { | ||
private var _current = new AttrScope() | ||
def current: AttrScope = _current | ||
private def setCurrentAttr(attr: AttrScope): Unit = { | ||
_current = attr | ||
} | ||
|
||
def apply(attr: Map[String, String] = Map.empty): AttrScope = new AttrScope(attr) | ||
} |
This file contains 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 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 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
51 changes: 51 additions & 0 deletions
51
scala-package/core/src/main/scala/ml/dmlc/mxnet/NameManager.scala
This file contains 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,51 @@ | ||
package ml.dmlc.mxnet | ||
|
||
import scala.collection.mutable | ||
|
||
/** | ||
* NameManager to do automatic naming. | ||
* User can also inherit this object to change naming behavior. | ||
* @author Yizhi Liu | ||
*/ | ||
class NameManager { | ||
val counter: mutable.Map[String, Int] = mutable.HashMap.empty[String, Int] | ||
/** | ||
* Get the canonical name for a symbol. | ||
* This is default implementation. | ||
* When user specified a name, | ||
* the user specified name will be used. | ||
* When user did not, we will automatically generate a name based on hint string. | ||
* | ||
* @param name : The name user specified. | ||
* @param hint : A hint string, which can be used to generate name. | ||
* @return A canonical name for the user. | ||
*/ | ||
def get(name: Option[String], hint: String): String = { | ||
name.getOrElse { | ||
if (!counter.contains(hint)) { | ||
counter(hint) = 0 | ||
} | ||
val generatedName = s"$hint${counter(hint)}" | ||
counter(hint) += 1 | ||
generatedName | ||
} | ||
} | ||
|
||
def withScope[T](body: => T): T = { | ||
val oldManager = NameManager.current | ||
NameManager.setCurrentManager(this) | ||
try { | ||
body | ||
} finally { | ||
NameManager.setCurrentManager(oldManager) | ||
} | ||
} | ||
} | ||
|
||
object NameManager { | ||
private var _current = new NameManager() | ||
def current: NameManager = _current | ||
private def setCurrentManager(manager: NameManager): Unit = { | ||
_current = manager | ||
} | ||
} |
Oops, something went wrong.