parent
e09d50e4d6
commit
ba5fa502cc
File diff suppressed because it is too large
Load Diff
@ -1,96 +0,0 @@
|
||||
# Generated by the gRPC Python protocol compiler plugin. DO NOT EDIT!
|
||||
import grpc
|
||||
|
||||
import ms_service_pb2 as ms__service__pb2
|
||||
|
||||
|
||||
class MSServiceStub(object):
|
||||
"""Missing associated documentation comment in .proto file"""
|
||||
|
||||
def __init__(self, channel):
|
||||
"""Constructor.
|
||||
|
||||
Args:
|
||||
channel: A grpc.Channel.
|
||||
"""
|
||||
self.Predict = channel.unary_unary(
|
||||
'/ms_serving.MSService/Predict',
|
||||
request_serializer=ms__service__pb2.PredictRequest.SerializeToString,
|
||||
response_deserializer=ms__service__pb2.PredictReply.FromString,
|
||||
)
|
||||
self.Test = channel.unary_unary(
|
||||
'/ms_serving.MSService/Test',
|
||||
request_serializer=ms__service__pb2.PredictRequest.SerializeToString,
|
||||
response_deserializer=ms__service__pb2.PredictReply.FromString,
|
||||
)
|
||||
|
||||
|
||||
class MSServiceServicer(object):
|
||||
"""Missing associated documentation comment in .proto file"""
|
||||
|
||||
def Predict(self, request, context):
|
||||
"""Missing associated documentation comment in .proto file"""
|
||||
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
|
||||
context.set_details('Method not implemented!')
|
||||
raise NotImplementedError('Method not implemented!')
|
||||
|
||||
def Test(self, request, context):
|
||||
"""Missing associated documentation comment in .proto file"""
|
||||
context.set_code(grpc.StatusCode.UNIMPLEMENTED)
|
||||
context.set_details('Method not implemented!')
|
||||
raise NotImplementedError('Method not implemented!')
|
||||
|
||||
|
||||
def add_MSServiceServicer_to_server(servicer, server):
|
||||
rpc_method_handlers = {
|
||||
'Predict': grpc.unary_unary_rpc_method_handler(
|
||||
servicer.Predict,
|
||||
request_deserializer=ms__service__pb2.PredictRequest.FromString,
|
||||
response_serializer=ms__service__pb2.PredictReply.SerializeToString,
|
||||
),
|
||||
'Test': grpc.unary_unary_rpc_method_handler(
|
||||
servicer.Test,
|
||||
request_deserializer=ms__service__pb2.PredictRequest.FromString,
|
||||
response_serializer=ms__service__pb2.PredictReply.SerializeToString,
|
||||
),
|
||||
}
|
||||
generic_handler = grpc.method_handlers_generic_handler(
|
||||
'ms_serving.MSService', rpc_method_handlers)
|
||||
server.add_generic_rpc_handlers((generic_handler,))
|
||||
|
||||
|
||||
# This class is part of an EXPERIMENTAL API.
|
||||
class MSService(object):
|
||||
"""Missing associated documentation comment in .proto file"""
|
||||
|
||||
@staticmethod
|
||||
def Predict(request,
|
||||
target,
|
||||
options=(),
|
||||
channel_credentials=None,
|
||||
call_credentials=None,
|
||||
compression=None,
|
||||
wait_for_ready=None,
|
||||
timeout=None,
|
||||
metadata=None):
|
||||
return grpc.experimental.unary_unary(request, target, '/ms_serving.MSService/Predict',
|
||||
ms__service__pb2.PredictRequest.SerializeToString,
|
||||
ms__service__pb2.PredictReply.FromString,
|
||||
options, channel_credentials,
|
||||
call_credentials, compression, wait_for_ready, timeout, metadata)
|
||||
|
||||
@staticmethod
|
||||
def Test(request,
|
||||
target,
|
||||
options=(),
|
||||
channel_credentials=None,
|
||||
call_credentials=None,
|
||||
compression=None,
|
||||
wait_for_ready=None,
|
||||
timeout=None,
|
||||
metadata=None):
|
||||
return grpc.experimental.unary_unary(request, target, '/ms_serving.MSService/Test',
|
||||
ms__service__pb2.PredictRequest.SerializeToString,
|
||||
ms__service__pb2.PredictReply.FromString,
|
||||
options, channel_credentials,
|
||||
call_credentials, compression, wait_for_ready, timeout, metadata)
|
@ -1,67 +0,0 @@
|
||||
/**
|
||||
* Copyright 2020 Huawei Technologies Co., Ltd
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#include <grpcpp/grpcpp.h>
|
||||
#include <grpcpp/health_check_service_interface.h>
|
||||
#include <grpcpp/ext/proto_server_reflection_plugin.h>
|
||||
#include <iostream>
|
||||
|
||||
#include "./ms_service.grpc.pb.h"
|
||||
|
||||
using grpc::Server;
|
||||
using grpc::ServerBuilder;
|
||||
using grpc::ServerContext;
|
||||
using grpc::Status;
|
||||
using ms_serving::MSService;
|
||||
using ms_serving::PredictReply;
|
||||
using ms_serving::PredictRequest;
|
||||
|
||||
// Logic and data behind the server's behavior.
|
||||
class MSServiceImpl final : public MSService::Service {
|
||||
Status Predict(ServerContext *context, const PredictRequest *request, PredictReply *reply) override {
|
||||
std::cout << "server eval" << std::endl;
|
||||
return Status::OK;
|
||||
}
|
||||
};
|
||||
|
||||
void RunServer() {
|
||||
std::string server_address("0.0.0.0:50051");
|
||||
MSServiceImpl service;
|
||||
|
||||
grpc::EnableDefaultHealthCheckService(true);
|
||||
grpc::reflection::InitProtoReflectionServerBuilderPlugin();
|
||||
auto option = grpc::MakeChannelArgumentOption(GRPC_ARG_ALLOW_REUSEPORT, 0);
|
||||
|
||||
ServerBuilder builder;
|
||||
builder.SetOption(std::move(option));
|
||||
// Listen on the given address without any authentication mechanism.
|
||||
builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
|
||||
// Register "service" as the instance through which we'll communicate with
|
||||
// clients. In this case it corresponds to an *synchronous* service.
|
||||
builder.RegisterService(&service);
|
||||
// Finally assemble the server.
|
||||
std::unique_ptr<Server> server(builder.BuildAndStart());
|
||||
std::cout << "Server listening on " << server_address << std::endl;
|
||||
|
||||
// Wait for the server to shutdown. Note that some other thread must be
|
||||
// responsible for shutting down the server for this call to ever return.
|
||||
server->Wait();
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
RunServer();
|
||||
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
# Copyright 2020 Huawei Technologies Co., Ltd
|
||||
#
|
||||
# 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.
|
||||
# ============================================================================
|
||||
import numpy as np
|
||||
|
||||
import mindspore.context as context
|
||||
import mindspore.nn as nn
|
||||
from mindspore.ops import operations as P
|
||||
from mindspore import Tensor
|
||||
from mindspore.train.serialization import export
|
||||
|
||||
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
|
||||
|
||||
class Net(nn.Cell):
|
||||
def __init__(self):
|
||||
super(Net, self).__init__()
|
||||
self.add = P.TensorAdd()
|
||||
|
||||
def construct(self, x_, y_):
|
||||
return self.add(x_, y_)
|
||||
|
||||
x = np.ones(4).astype(np.float32)
|
||||
y = np.ones(4).astype(np.float32)
|
||||
|
||||
def export_net():
|
||||
add = Net()
|
||||
output = add(Tensor(x), Tensor(y))
|
||||
export(add, Tensor(x), Tensor(y), file_name='tensor_add.pb', file_format='BINARY')
|
||||
print(x)
|
||||
print(y)
|
||||
print(output.asnumpy())
|
||||
|
||||
if __name__ == "__main__":
|
||||
export_net()
|
||||
|
@ -1,57 +0,0 @@
|
||||
# Copyright 2020 Huawei Technologies Co., Ltd
|
||||
#
|
||||
# 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.
|
||||
# ============================================================================
|
||||
import grpc
|
||||
import numpy as np
|
||||
import ms_service_pb2
|
||||
import ms_service_pb2_grpc
|
||||
|
||||
|
||||
def run():
|
||||
channel = grpc.insecure_channel('localhost:50051')
|
||||
stub = ms_service_pb2_grpc.MSServiceStub(channel)
|
||||
# request = ms_service_pb2.PredictRequest()
|
||||
# request.name = 'haha'
|
||||
# response = stub.Eval(request)
|
||||
# print("ms client received: " + response.message)
|
||||
|
||||
request = ms_service_pb2.PredictRequest()
|
||||
request.data.tensor_shape.dims.extend([32, 1, 32, 32])
|
||||
request.data.tensor_type = ms_service_pb2.MS_FLOAT32
|
||||
request.data.data = (np.ones([32, 1, 32, 32]).astype(np.float32) * 0.01).tobytes()
|
||||
|
||||
request.label.tensor_shape.dims.extend([32])
|
||||
request.label.tensor_type = ms_service_pb2.MS_INT32
|
||||
request.label.data = np.ones([32]).astype(np.int32).tobytes()
|
||||
|
||||
result = stub.Predict(request)
|
||||
#result_np = np.frombuffer(result.result.data, dtype=np.float32).reshape(result.result.tensor_shape.dims)
|
||||
print("ms client received: ")
|
||||
#print(result_np)
|
||||
|
||||
# future_list = []
|
||||
# times = 1000
|
||||
# for i in range(times):
|
||||
# async_future = stub.Eval.future(request)
|
||||
# future_list.append(async_future)
|
||||
# print("async call, future list add item " + str(i));
|
||||
#
|
||||
# for i in range(len(future_list)):
|
||||
# async_result = future_list[i].result()
|
||||
# print("ms client async get result of item " + str(i))
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
run()
|
@ -1,55 +0,0 @@
|
||||
# Copyright 2020 Huawei Technologies Co., Ltd
|
||||
#
|
||||
# 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.
|
||||
# ============================================================================
|
||||
from concurrent import futures
|
||||
import time
|
||||
import grpc
|
||||
import numpy as np
|
||||
import ms_service_pb2
|
||||
import ms_service_pb2_grpc
|
||||
import test_cpu_lenet
|
||||
from mindspore import Tensor
|
||||
|
||||
class MSService(ms_service_pb2_grpc.MSServiceServicer):
|
||||
def Predict(self, request, context):
|
||||
request_data = request.data
|
||||
request_label = request.label
|
||||
|
||||
data_from_buffer = np.frombuffer(request_data.data, dtype=np.float32)
|
||||
data_from_buffer = data_from_buffer.reshape(request_data.tensor_shape.dims)
|
||||
data = Tensor(data_from_buffer)
|
||||
|
||||
label_from_buffer = np.frombuffer(request_label.data, dtype=np.int32)
|
||||
label_from_buffer = label_from_buffer.reshape(request_label.tensor_shape.dims)
|
||||
label = Tensor(label_from_buffer)
|
||||
|
||||
result = test_cpu_lenet.test_lenet(data, label)
|
||||
result_reply = ms_service_pb2.PredictReply()
|
||||
result_reply.result.tensor_shape.dims.extend(result.shape())
|
||||
result_reply.result.data = result.asnumpy().tobytes()
|
||||
return result_reply
|
||||
|
||||
def serve():
|
||||
server = grpc.server(futures.ThreadPoolExecutor(max_workers=1))
|
||||
ms_service_pb2_grpc.add_MSServiceServicer_to_server(MSService(), server)
|
||||
server.add_insecure_port('[::]:50051')
|
||||
server.start()
|
||||
try:
|
||||
while True:
|
||||
time.sleep(60*60*24) # one day in seconds
|
||||
except KeyboardInterrupt:
|
||||
server.stop(0)
|
||||
|
||||
if __name__ == '__main__':
|
||||
serve()
|
@ -1,91 +0,0 @@
|
||||
# Copyright 2019 Huawei Technologies Co., Ltd
|
||||
#
|
||||
# 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.
|
||||
# ============================================================================
|
||||
import numpy as np
|
||||
import mindspore.context as context
|
||||
import mindspore.nn as nn
|
||||
from mindspore import Tensor
|
||||
from mindspore.nn import TrainOneStepCell, WithLossCell
|
||||
from mindspore.nn.optim import Momentum
|
||||
from mindspore.ops import operations as P
|
||||
import ms_service_pb2
|
||||
|
||||
|
||||
class LeNet(nn.Cell):
|
||||
def __init__(self):
|
||||
super(LeNet, self).__init__()
|
||||
self.relu = P.ReLU()
|
||||
self.batch_size = 32
|
||||
|
||||
self.conv1 = nn.Conv2d(1, 6, kernel_size=5, stride=1, padding=0, has_bias=False, pad_mode='valid')
|
||||
self.conv2 = nn.Conv2d(6, 16, kernel_size=5, stride=1, padding=0, has_bias=False, pad_mode='valid')
|
||||
self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
|
||||
self.reshape = P.Reshape()
|
||||
self.fc1 = nn.Dense(400, 120)
|
||||
self.fc2 = nn.Dense(120, 84)
|
||||
self.fc3 = nn.Dense(84, 10)
|
||||
|
||||
def construct(self, input_x):
|
||||
output = self.conv1(input_x)
|
||||
output = self.relu(output)
|
||||
output = self.pool(output)
|
||||
output = self.conv2(output)
|
||||
output = self.relu(output)
|
||||
output = self.pool(output)
|
||||
output = self.reshape(output, (self.batch_size, -1))
|
||||
output = self.fc1(output)
|
||||
output = self.relu(output)
|
||||
output = self.fc2(output)
|
||||
output = self.relu(output)
|
||||
output = self.fc3(output)
|
||||
return output
|
||||
|
||||
|
||||
def train(net, data, label):
|
||||
learning_rate = 0.01
|
||||
momentum = 0.9
|
||||
|
||||
optimizer = Momentum(filter(lambda x: x.requires_grad, net.get_parameters()), learning_rate, momentum)
|
||||
criterion = nn.SoftmaxCrossEntropyWithLogits(is_grad=False, sparse=True)
|
||||
net_with_criterion = WithLossCell(net, criterion)
|
||||
train_network = TrainOneStepCell(net_with_criterion, optimizer) # optimizer
|
||||
train_network.set_train()
|
||||
res = train_network(data, label)
|
||||
print("+++++++++Loss+++++++++++++")
|
||||
print(res)
|
||||
print("+++++++++++++++++++++++++++")
|
||||
assert res
|
||||
return res
|
||||
|
||||
def test_lenet(data, label):
|
||||
context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
|
||||
net = LeNet()
|
||||
return train(net, data, label)
|
||||
|
||||
if __name__ == '__main__':
|
||||
tensor = ms_service_pb2.Tensor()
|
||||
tensor.tensor_shape.dim.extend([32, 1, 32, 32])
|
||||
# tensor.tensor_shape.dim.add() = 1
|
||||
# tensor.tensor_shape.dim.add() = 32
|
||||
# tensor.tensor_shape.dim.add() = 32
|
||||
tensor.tensor_type = ms_service_pb2.MS_FLOAT32
|
||||
tensor.data = np.ones([32, 1, 32, 32]).astype(np.float32).tobytes()
|
||||
|
||||
data_from_buffer = np.frombuffer(tensor.data, dtype=np.float32)
|
||||
print(tensor.tensor_shape.dim)
|
||||
data_from_buffer = data_from_buffer.reshape(tensor.tensor_shape.dim)
|
||||
print(data_from_buffer.shape)
|
||||
input_data = Tensor(data_from_buffer * 0.01)
|
||||
input_label = Tensor(np.ones([32]).astype(np.int32))
|
||||
test_lenet(input_data, input_label)
|
Loading…
Reference in new issue