remotecontrol.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2019 Alexey Edelev <semlanik@gmail.com>
  5. *
  6. * This file is part of NeuralNetwork project https://git.semlanik.org/semlanik/NeuralNetwork
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of this
  9. * software and associated documentation files (the "Software"), to deal in the Software
  10. * without restriction, including without limitation the rights to use, copy, modify,
  11. * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  12. * to permit persons to whom the Software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all copies
  16. * or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  19. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  20. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  21. * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. package remotecontrol
  26. import (
  27. context "context"
  28. fmt "fmt"
  29. "log"
  30. "net"
  31. "google.golang.org/grpc/codes"
  32. status "google.golang.org/grpc/status"
  33. neuralnetworkbase "../neuralnetworkbase"
  34. "gonum.org/v1/gonum/mat"
  35. grpc "google.golang.org/grpc"
  36. )
  37. type RemoteControl struct {
  38. nn *neuralnetworkbase.NeuralNetwork
  39. activationsQueue chan *LayerMatrix
  40. biasesQueue chan *LayerMatrix
  41. weightsQueue chan *LayerMatrix
  42. }
  43. func (rw *RemoteControl) Init(nn *neuralnetworkbase.NeuralNetwork) {
  44. rw.nn = nn
  45. rw.activationsQueue = make(chan *LayerMatrix, 10)
  46. rw.biasesQueue = make(chan *LayerMatrix, 10)
  47. rw.weightsQueue = make(chan *LayerMatrix, 10)
  48. }
  49. func (rw *RemoteControl) UpdateActivations(l int, a *mat.Dense) {
  50. matrix := NewLayerMatrix(l, a, LayerMatrix_Activations)
  51. select {
  52. case rw.activationsQueue <- matrix:
  53. default:
  54. }
  55. }
  56. func (rw *RemoteControl) UpdateBiases(l int, biases *mat.Dense) {
  57. matrix := NewLayerMatrix(l, biases, LayerMatrix_Biases)
  58. select {
  59. case rw.biasesQueue <- matrix:
  60. default:
  61. }
  62. }
  63. func (rw *RemoteControl) UpdateWeights(l int, weights *mat.Dense) {
  64. matrix := NewLayerMatrix(l, weights, LayerMatrix_Weights)
  65. select {
  66. case rw.weightsQueue <- matrix:
  67. default:
  68. }
  69. }
  70. func NewLayerMatrix(l int, dense *mat.Dense, contentType LayerMatrix_ContentType) (matrix *LayerMatrix) {
  71. buffer, err := dense.MarshalBinary()
  72. if err != nil {
  73. log.Fatalln("Invalid dense is provided for remote control")
  74. }
  75. matrix = &LayerMatrix{
  76. Matrix: &Matrix{
  77. Matrix: buffer,
  78. },
  79. Layer: int32(l),
  80. ContentType: contentType,
  81. }
  82. return
  83. }
  84. func (rw *RemoteControl) GetConfiguration(context.Context, *None) (*Configuration, error) {
  85. config := &Configuration{}
  86. for _, size := range rw.nn.Sizes {
  87. config.Sizes = append(config.Sizes, int32(size))
  88. }
  89. return config, nil
  90. }
  91. func (rw *RemoteControl) Activations(_ *None, srv RemoteControl_ActivationsServer) error {
  92. ctx := srv.Context()
  93. for {
  94. select {
  95. case <-ctx.Done():
  96. return ctx.Err()
  97. default:
  98. }
  99. msg := <-rw.activationsQueue
  100. srv.Send(msg)
  101. }
  102. }
  103. func (rw *RemoteControl) Biases(_ *None, srv RemoteControl_BiasesServer) error {
  104. ctx := srv.Context()
  105. for {
  106. select {
  107. case <-ctx.Done():
  108. return ctx.Err()
  109. default:
  110. }
  111. msg := <-rw.biasesQueue
  112. srv.Send(msg)
  113. }
  114. }
  115. func (rw *RemoteControl) Weights(_ *None, srv RemoteControl_WeightsServer) error {
  116. ctx := srv.Context()
  117. for {
  118. select {
  119. case <-ctx.Done():
  120. return ctx.Err()
  121. default:
  122. }
  123. msg := <-rw.weightsQueue
  124. srv.Send(msg)
  125. }
  126. }
  127. func (rw *RemoteControl) Predict(context.Context, *Matrix) (*Matrix, error) {
  128. return nil, status.Error(codes.Unimplemented, "Not implemented")
  129. }
  130. func (rw *RemoteControl) Run() {
  131. grpcServer := grpc.NewServer()
  132. RegisterRemoteControlServer(grpcServer, rw)
  133. lis, err := net.Listen("tcp", "localhost:65001")
  134. if err != nil {
  135. fmt.Printf("Failed to listen: %v\n", err)
  136. }
  137. fmt.Printf("Listen localhost:65001\n")
  138. if err := grpcServer.Serve(lis); err != nil {
  139. fmt.Printf("Failed to serve: %v\n", err)
  140. }
  141. }