-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ModuleList, LayerNorm. Also misc improvements
- Loading branch information
Showing
5 changed files
with
126 additions
and
1 deletion.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
core/src/main/scala/torch/nn/modules/container/ModuleList.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,40 @@ | ||
/* | ||
* Copyright 2022 storch.dev | ||
* | ||
* 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 torch | ||
package nn | ||
package modules | ||
package container | ||
|
||
import sourcecode.Name | ||
import scala.util.Random | ||
|
||
final class ModuleList[D <: DType](override val modules: TensorModule[D]*) | ||
extends Module | ||
with Seq[TensorModule[D]]: | ||
// with TensorModule[D]: | ||
modules.zipWithIndex.foreach((module, index) => | ||
this.register(module)(using Name(index.toString())) | ||
) | ||
|
||
override def iterator: Iterator[TensorModule[D]] = modules.iterator | ||
|
||
// def apply(v1: Int | torch.Tensor[D]): torch.nn.modules.TensorModule[D] & torch.Tensor[D] = ??? | ||
def apply(i: Int): torch.nn.modules.TensorModule[D] = modules(i) | ||
|
||
override def length: Int = modules.length | ||
|
||
override def toString = getClass().getSimpleName() |
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
55 changes: 55 additions & 0 deletions
55
core/src/main/scala/torch/nn/modules/normalization/LayerNorm.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,55 @@ | ||
/* | ||
* Copyright 2022 storch.dev | ||
* | ||
* 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 torch | ||
package nn | ||
package modules | ||
package normalization | ||
|
||
import org.bytedeco.pytorch | ||
import org.bytedeco.pytorch.{LayerNormImpl, LayerNormOptions, LongVector} | ||
import torch.nn.modules.TensorModule | ||
import torch.{DType, Tensor} | ||
|
||
/** Applies Layer Normalization over a mini-batch of inputs as described in the paper Layer | ||
* Normalization // TODO Add docs | ||
*/ | ||
final class LayerNorm[ParamType <: DType: Default]( | ||
normalizedShape: Seq[Int] | Int, | ||
eps: Double = 1e-05, | ||
elementwiseAffine: Boolean = true | ||
) extends TensorModule[ParamType]: | ||
private val options: LayerNormOptions = normalizedShape match { | ||
case normalizedShape: Seq[Int] => | ||
LayerNormOptions(LongVector(normalizedShape.map(_.toLong)*)) | ||
case normalizedShape: Int => | ||
LayerNormOptions(LongVector(normalizedShape.toLong)) | ||
} | ||
options.eps().put(eps) | ||
options.elementwise_affine().put(elementwiseAffine) | ||
|
||
override private[torch] val nativeModule: LayerNormImpl = LayerNormImpl(options) | ||
|
||
override def registerWithParent[M <: pytorch.Module](parent: M)(using | ||
name: sourcecode.Name | ||
): Unit = | ||
parent.register_module(name.value, nativeModule) | ||
|
||
val weight: Tensor[ParamType] = Tensor[ParamType](nativeModule.weight) | ||
val bias: Tensor[ParamType] = Tensor[ParamType](nativeModule.bias) | ||
|
||
def apply(t: Tensor[ParamType]): Tensor[ParamType] = | ||
Tensor[ParamType](nativeModule.forward(t.native)) |
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