|
| 1 | +package com.thoughtworks.deeplearning.plugins |
| 2 | + |
| 3 | +import com.thoughtworks.continuation._ |
| 4 | +import com.thoughtworks.deeplearning.DeepLearning |
| 5 | +import com.thoughtworks.deeplearning.DeepLearning.Tape |
| 6 | +import com.thoughtworks.raii.asynchronous._ |
| 7 | +import scalaz.Applicative |
| 8 | +import scalaz.syntax.all._ |
| 9 | +import scalaz.Tags.Parallel |
| 10 | +import shapeless.{::, HList, HNil} |
| 11 | + |
| 12 | +import java.io.{PrintStream, PrintWriter} |
| 13 | + |
| 14 | +import scalaz.Semigroup |
| 15 | + |
| 16 | +private object HLists { |
| 17 | + |
| 18 | + implicit val doParallelApplicative = |
| 19 | + asynchronousDoParallelApplicative(DeepLearning.multipleExceptionThrowableSemigroup) |
| 20 | + |
| 21 | + private val noop: Do[HNil] => UnitContinuation[Unit] = { |
| 22 | + Function.const(UnitContinuation.now(())) |
| 23 | + } |
| 24 | + |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * @author 杨博 (Yang Bo) |
| 29 | + */ |
| 30 | +trait HLists { |
| 31 | + import com.thoughtworks.deeplearning.plugins.HLists._ |
| 32 | + |
| 33 | + trait ImplicitsApi { |
| 34 | + implicit def hnilDeepLearning[L <: HNil]: DeepLearning.Aux[L, HNil, HNil] = new DeepLearning[L] { |
| 35 | + type Data = HNil |
| 36 | + type Delta = HNil |
| 37 | + |
| 38 | + def forward(differentiable: L): Do[Tape[Data, Delta]] = { |
| 39 | + Do.now(Tape(HNil, noop)) |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + implicit def hconsDeepLearning[Head, Tail <: HList, HeadData, TailData <: HList, HeadDelta, TailDelta <: HList]( |
| 44 | + implicit headDeepLearning: DeepLearning.Aux[Head, HeadData, HeadDelta], |
| 45 | + tailDeepLearning: DeepLearning.Aux[Tail, TailData, TailDelta]) |
| 46 | + : DeepLearning.Aux[Head :: Tail, HeadData :: TailData, HeadDelta :: TailDelta] = new DeepLearning[Head :: Tail] { |
| 47 | + type Data = HeadData :: TailData |
| 48 | + type Delta = HeadDelta :: TailDelta |
| 49 | + |
| 50 | + def forward(differentiable: Head :: Tail): Do[Tape[Data, Delta]] = { |
| 51 | + val head :: tail = differentiable |
| 52 | + val doHead: ParallelDo[Tape[HeadData, HeadDelta]] = Parallel(headDeepLearning.forward(head)) |
| 53 | + |
| 54 | + val doTail: ParallelDo[Tape[TailData, TailDelta]] = Parallel(tailDeepLearning.forward(tail)) |
| 55 | + |
| 56 | + Parallel.unwrap(Applicative[ParallelDo].tuple2(doHead, doTail)).map { |
| 57 | + case (Tape(headData, headBackward), Tape(tailData, tailBackward)) => |
| 58 | + def backward(doDelta: Do[HeadDelta :: TailDelta]) = { |
| 59 | + val continuationHead: ParallelContinuation[Unit] = Parallel(headBackward(doDelta.map(_.head))) |
| 60 | + val continuationTail: ParallelContinuation[Unit] = Parallel(tailBackward(doDelta.map(_.tail))) |
| 61 | + Parallel.unwrap(continuationParallelApplicative.apply2(continuationHead, continuationTail) { |
| 62 | + (_: Unit, _: Unit) => |
| 63 | + () |
| 64 | + }) |
| 65 | + } |
| 66 | + Tape(headData :: tailData, backward) |
| 67 | + } |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + type Implicits <: ImplicitsApi |
| 75 | + |
| 76 | +} |
0 commit comments