remotecontrol.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. return nil, status.Error(codes.Unimplemented, "Not implemented")
  86. }
  87. func (rw *RemoteControl) Activations(_ *None, srv RemoteControl_ActivationsServer) error {
  88. ctx := srv.Context()
  89. for {
  90. select {
  91. case <-ctx.Done():
  92. return ctx.Err()
  93. default:
  94. }
  95. msg := <-rw.activationsQueue
  96. srv.Send(msg)
  97. }
  98. }
  99. func (rw *RemoteControl) Biases(_ *None, srv RemoteControl_BiasesServer) error {
  100. ctx := srv.Context()
  101. for {
  102. select {
  103. case <-ctx.Done():
  104. return ctx.Err()
  105. default:
  106. }
  107. msg := <-rw.biasesQueue
  108. srv.Send(msg)
  109. }
  110. }
  111. func (rw *RemoteControl) Weights(_ *None, srv RemoteControl_WeightsServer) error {
  112. ctx := srv.Context()
  113. for {
  114. select {
  115. case <-ctx.Done():
  116. return ctx.Err()
  117. default:
  118. }
  119. msg := <-rw.weightsQueue
  120. srv.Send(msg)
  121. }
  122. }
  123. func (rw *RemoteControl) Predict(context.Context, *Matrix) (*Matrix, error) {
  124. return nil, status.Error(codes.Unimplemented, "Not implemented")
  125. }
  126. func (rw *RemoteControl) Run() {
  127. grpcServer := grpc.NewServer()
  128. RegisterRemoteControlServer(grpcServer, rw)
  129. lis, err := net.Listen("tcp", "localhost:65001")
  130. if err != nil {
  131. fmt.Printf("Failed to listen: %v\n", err)
  132. }
  133. fmt.Printf("Listen localhost:65001\n")
  134. if err := grpcServer.Serve(lis); err != nil {
  135. fmt.Printf("Failed to serve: %v\n", err)
  136. }
  137. }