Skip to content

Commit

Permalink
review edits
Browse files Browse the repository at this point in the history
  • Loading branch information
CoderHam committed Aug 29, 2019
1 parent 13919c0 commit 7a41f1a
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 23 deletions.
21 changes: 8 additions & 13 deletions src/clients/python/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
# Copyright (c) 2018-2019, NVIDIA CORPORATION. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
Expand Down Expand Up @@ -732,15 +732,8 @@ def register(self, shm_handle):
Parameters
----------
name : str
The name of the shared memory region to be registered.
shm_handle : c_void_p
The handle for the shared memory region.
offset : int
The offset from the start of the shared shared memory region.
byte_size : int
The size in bytes of the data to be read / written in the shared
memory region.
Raises
------
Expand Down Expand Up @@ -933,20 +926,22 @@ def _prepare_request(self, inputs, outputs,
# batch). It is a common error when using batch-size 1 to
# specify an input directly as an array instead of as a list
# containing one array.
# Each element in the list must be a 'numpy array' if inputs are being
# passed directly and a 'tuple' if passing a reference to shared memory
# An input's data may be specified as a list of numpy arrays,
# or as a shared memory handle.
for inp_name, inp in inputs.items():
if not isinstance(inp, (list, tuple)) and type(inp) != c_void_p:
_raise_error("input '" + inp_name +
"' values must be specified as a list of numpy arrays" \
" or c_void_p representing the shared memory handle")
" or as a single c_void_p representing the shared memory handle")
if type(inp) != c_void_p:
for ip in inp:
if not isinstance(ip, (np.ndarray, tuple)):
_raise_error("input '" + inp_name +
"' values must be specified as a list of numpy arrays" \
" or c_void_p representing the shared memory handle")
" or as a single c_void_p representing the shared memory handle")
# Set run options using formats specified in 'outputs'
# An output format may be may be specified as a RAW or (CLASS, cnt)
# or as a (RAW, shared_memory_handle).
options = c_void_p()
try:
_raise_if_error(c_void_p(
Expand All @@ -957,7 +952,7 @@ def _prepare_request(self, inputs, outputs,
and output_format[0] == InferContext.ResultFormat.RAW:
if type(output_format[1]) != c_void_p:
_raise_error("shared memory requires tuple of size 2" \
" - output_format(RAW), shm_handle(c_void_p)")
" - output_format(RAW), shared_memory_handle(c_void_p)")
_raise_if_error(
c_void_p(
_crequest_infer_ctx_options_add_shared_memory(
Expand Down
12 changes: 5 additions & 7 deletions src/clients/python/shared_memory/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2018-2019, NVIDIA CORPORATION. All rights reserved.
# Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
Expand Down Expand Up @@ -45,7 +45,7 @@ def from_param(cls, value):
return value.encode('utf8')

import os
_cshm_lib = "shmwrap" if os.name == 'nt' else 'libcshm.so'
_cshm_lib = "cshm" if os.name == 'nt' else 'libcshm.so'
_cshm_path = pkg_resources.resource_filename('tensorrtserver.shared_memory', _cshm_lib)
_cshm = cdll.LoadLibrary(_cshm_path)

Expand All @@ -67,7 +67,7 @@ def _raise_if_error(errno):
if errno.value != 0:
ex = SharedMemoryException(errno)
raise ex
return 0
return

def _raise_error(msg):
ex = SharedMemoryException(msg)
Expand Down Expand Up @@ -100,16 +100,14 @@ def create_shared_memory_region(trtis_shm_name, shm_key, byte_size):

return shm_handle

def set_shared_memory_region(shm_handle, offset, input_values):
def set_shared_memory_region(shm_handle, input_values):
"""Copy the contents of the numpy array into a shared memory region with
the specified identifier, offset and size.
Parameters
----------
shm_handle : c_void_p
The handle for the shared memory region.
offset : int
The offset from the start of the shared shared memory region.
input_values : np.array
The list of numpy arrays to be copied into the shared memory region.
Expand All @@ -125,7 +123,7 @@ def set_shared_memory_region(shm_handle, offset, input_values):
if not isinstance(input_value, (np.ndarray,)):
_raise_error("input_values must be specified as a list/tuple of numpy arrays")

offset_current = offset
offset_current = 0
for input_value in input_values:
input_value = np.ascontiguousarray(input_value).flatten()
byte_size = input_value.size * input_value.itemsize
Expand Down
5 changes: 5 additions & 0 deletions src/clients/python/shared_memory/shared_memory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "src/clients/python/shared_memory/shared_memory.h"

#include <errno.h>
#include <fcntl.h>
#include <sys/mman.h>
Expand All @@ -36,6 +37,8 @@
//==============================================================================
// SharedMemoryControlContext

namespace {

void*
SharedMemoryHandleCreate(
std::string trtis_shm_name, void* shm_addr, std::string shm_key, int shm_fd,
Expand Down Expand Up @@ -63,6 +66,8 @@ SharedMemoryRegionMap(
return 0;
}

} // namespace

int
SharedMemoryRegionCreate(
const char* trtis_shm_name, const char* shm_key, size_t byte_size,
Expand Down
1 change: 0 additions & 1 deletion src/clients/python/shared_memory/shared_memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
#include <unistd.h>

#ifdef __cplusplus

extern "C" {
#endif

Expand Down
4 changes: 2 additions & 2 deletions src/clients/python/simple_shm_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@
shm_ip1_handle = shm.create_shared_memory_region("input1_data", "/input1_simple", input_byte_size)

# Put input data values into shared memory
shm.set_shared_memory_region(shm_ip0_handle, 0, [input0_data])
shm.set_shared_memory_region(shm_ip1_handle, 0, [input1_data])
shm.set_shared_memory_region(shm_ip0_handle, [input0_data])
shm.set_shared_memory_region(shm_ip1_handle, [input1_data])

# Register Input0 and Input1 shared memory with TRTIS
shared_memory_ctx.register(shm_ip0_handle)
Expand Down

0 comments on commit 7a41f1a

Please sign in to comment.