Skip to content

Commit

Permalink
adding the source
Browse files Browse the repository at this point in the history
  • Loading branch information
nealvis committed Oct 11, 2017
1 parent 0c6f528 commit 4fbe5a4
Show file tree
Hide file tree
Showing 16 changed files with 2,347 additions and 0 deletions.
193 changes: 193 additions & 0 deletions api/LICENSE

Large diffs are not rendered by default.

69 changes: 69 additions & 0 deletions api/include/mvnc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#ifndef __MVNC_H_INCLUDED__
#define __MVNC_H_INCLUDED__

#ifdef __cplusplus
extern "C"
{
#endif

#define MVNC_MAX_NAME_SIZE 28

typedef enum {
MVNC_OK = 0,
MVNC_BUSY = -1, // Device is busy, retry later
MVNC_ERROR = -2, // Error communicating with the device
MVNC_OUT_OF_MEMORY = -3, // Out of memory
MVNC_DEVICE_NOT_FOUND = -4, // No device at the given index or name
MVNC_INVALID_PARAMETERS = -5, // At least one of the given parameters is wrong
MVNC_TIMEOUT = -6, // Timeout in the communication with the device
MVNC_MVCMD_NOT_FOUND = -7, // The file to boot Myriad was not found
MVNC_NO_DATA = -8, // No data to return, call LoadTensor first
MVNC_GONE = -9, // The graph or device has been closed during the operation
MVNC_UNSUPPORTED_GRAPH_FILE = -10, // The graph file version is not supported
MVNC_MYRIAD_ERROR = -11, // An error has been reported by the device, use MVNC_DEBUG_INFO
} mvncStatus;

typedef enum {
MVNC_LOG_LEVEL = 0, // Log level, int, 0 = nothing, 1 = errors, 2 = verbose
} mvncGlobalOptions;

typedef enum {
MVNC_ITERATIONS = 0, // Number of iterations per inference, int, normally 1, not for general use
MVNC_NETWORK_THROTTLE = 1, // Measure temperature once per inference instead of once per layer, int, not for general use
MVNC_DONT_BLOCK = 2, // LoadTensor will return BUSY instead of blocking, GetResult will return NO_DATA, int
MVNC_TIME_TAKEN = 1000, // Return time taken for inference (float *)
MVNC_DEBUG_INFO = 1001, // Return debug info, string
} mvncGraphOptions;

typedef enum {
MVNC_TEMP_LIM_LOWER = 1, // Temperature for short sleep, float, not for general use
MVNC_TEMP_LIM_HIGHER = 2, // Temperature for long sleep, float, not for general use
MVNC_BACKOFF_TIME_NORMAL = 3, // Normal sleep in ms, int, not for general use
MVNC_BACKOFF_TIME_HIGH = 4, // Short sleep in ms, int, not for general use
MVNC_BACKOFF_TIME_CRITICAL = 5, // Long sleep in ms, int, not for general use
MVNC_TEMPERATURE_DEBUG = 6, // Stop on critical temperature, int, not for general use
MVNC_THERMAL_STATS = 1000, // Return temperatures, float *, not for general use
MVNC_OPTIMISATION_LIST = 1001, // Return optimisations list, char *, not for general use
MVNC_THERMAL_THROTTLING_LEVEL = 1002, // 1=TEMP_LIM_LOWER reached, 2=TEMP_LIM_HIGHER reached
} mvncDeviceOptions;

mvncStatus mvncGetDeviceName(int index, char *name, unsigned int nameSize);
mvncStatus mvncOpenDevice(const char *name, void **deviceHandle);
mvncStatus mvncCloseDevice(void *deviceHandle);
mvncStatus mvncAllocateGraph(void *deviceHandle, void **graphHandle, const void *graphFile, unsigned int graphFileLength);
mvncStatus mvncDeallocateGraph(void *graphHandle);
mvncStatus mvncSetGlobalOption(int option, const void *data, unsigned int dataLength);
mvncStatus mvncGetGlobalOption(int option, void *data, unsigned int *dataLength);
mvncStatus mvncSetGraphOption(void *graphHandle, int option, const void *data, unsigned int dataLength);
mvncStatus mvncGetGraphOption(void *graphHandle, int option, void *data, unsigned int *dataLength);
mvncStatus mvncSetDeviceOption(void *deviceHandle, int option, const void *data, unsigned int dataLength);
mvncStatus mvncGetDeviceOption(void *deviceHandle, int option, void *data, unsigned int *dataLength);
mvncStatus mvncLoadTensor(void *graphHandle, const void *inputTensor, unsigned int inputTensorLength, void *userParam);
mvncStatus mvncGetResult(void *graphHandle, void **outputData, unsigned int *outputDataLength, void **userParam);

#include "mvnc_deprecated.h"
#ifdef __cplusplus
}
#endif

#endif
39 changes: 39 additions & 0 deletions api/include/mvnc_deprecated.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#ifndef __MVNC_DEPRECATED_H_INCLUDED__
#define __MVNC_DEPRECATED_H_INCLUDED__

#ifdef __cplusplus
extern "C"
{
#endif

typedef mvncGraphOptions GraphOptions __attribute__ \
((deprecated("GraphOptions is deprecated. Please use mvncGraphOptions")));
typedef mvncDeviceOptions DeviceOptions __attribute__ \
((deprecated("DeviceOptions is deprecated. Please use mvncDeviceOptions")));

// Deprecated Define
#define MVNC_MAXNAMESIZE _Pragma("GCC warning \"'MVNC_MAXNAMESIZE' is deprecated. Please use 'MVNC_MAX_NAME_SIZE'\"") MVNC_MAX_NAME_SIZE

// Deprecated Global Options
#define MVNC_LOGLEVEL _Pragma("GCC warning \"'MVNC_LOGLEVEL' is deprecated. Please use 'MVNC_LOG_LEVEL'\"") MVNC_LOG_LEVEL

// Deprecated status values
#define MVNC_MVCMDNOTFOUND _Pragma("GCC warning \"'MVNC_MVCMDNOTFOUND' is deprecated. Please use 'MVNC_MVCMD_NOT_FOUND'\"") MVNC_MVCMD_NOT_FOUND
#define MVNC_NODATA _Pragma("GCC warning \"'MVNC_NO_DATA' is deprecated. Please use 'MVNC_NO_DATA'\"") MVNC_NO_DATA
#define MVNC_UNSUPPORTEDGRAPHFILE _Pragma("GCC warning \"'MVNC_UNSUPPORTEDGRAPHFILE' is deprecated. Please use 'MVNC_UNSUPPORTED_GRAPH_FILE'\"") MVNC_UNSUPPORTED_GRAPH_FILE
#define MVNC_MYRIADERROR _Pragma("GCC warning \"'MVNC_MYRIADERROR' is deprecated. Please use 'MVNC_MYRIAD_ERROR'\"") MVNC_MYRIAD_ERROR

// Deprecated Graph Options values
#define MVNC_DONTBLOCK _Pragma("GCC warning \"'MVNC_DONTBLOCK' is deprecated. Please use 'MVNC_DONT_BLOCK'\"") MVNC_DONT_BLOCK
#define MVNC_TIMETAKEN _Pragma("GCC warning \"'MVNC_TIMETAKEN' is deprecated. Please use 'MVNC_TIME_TAKEN'\"") MVNC_TIME_TAKEN
#define MVNC_DEBUGINFO _Pragma("GCC warning \"'MVNC_DEBUGINFO' is deprecated. Please use 'MVNC_DEBUG_INFO'\"") MVNC_DEBUG_INFO

// Deprecated Device Options Values
#define MVNC_THERMALSTATS _Pragma("GCC warning \"'MVNC_THERMALSTATS' is deprecated. Please use 'MVNC_THERMAL_STATS'\"") MVNC_THERMAL_STATS
#define MVNC_OPTIMISATIONLIST _Pragma("GCC warning \"'MVNC_OPTIMISATIONLIST' is deprecated. Please use 'MVNC_OPTIMISATION_LIST'\"") MVNC_OPTIMISATION_LIST

#ifdef __cplusplus
}
#endif

#endif
Empty file added api/python/mvnc/__init__.py
Empty file.
268 changes: 268 additions & 0 deletions api/python/mvnc/mvncapi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@
# Copyright 2017 Intel Corporation.
# The source code, information and material ("Material") contained herein is
# owned by Intel Corporation or its suppliers or licensors, and title to such
# Material remains with Intel Corporation or its suppliers or licensors.
# The Material contains proprietary information of Intel or its suppliers and
# licensors. The Material is protected by worldwide copyright laws and treaty
# provisions.
# No part of the Material may be used, copied, reproduced, modified, published,
# uploaded, posted, transmitted, distributed or disclosed in any way without
# Intel's prior express written permission. No license under any patent,
# copyright or other intellectual property rights in the Material is granted to
# or conferred upon you, either expressly, by implication, inducement, estoppel
# or otherwise.
# Any license under such intellectual property rights must be express and
# approved by Intel in writing.

import sys
import numpy
import warnings
from enum import Enum
from ctypes import *

# The toolkit wants its local version
try:
f = CDLL("./libmvnc.so")
except:
f = CDLL("libmvnc.so")

warnings.simplefilter('default', DeprecationWarning)


class EnumDeprecationHelper(object):
def __init__(self, new_target, deprecated_values):
self.new_target = new_target
self.deprecated_values = deprecated_values

def __call__(self, *args, **kwargs):
return self.new_target(*args, **kwargs)

def __getattr__(self, attr):
if (attr in self.deprecated_values):
warnings.warn('\033[93m' + "\"" + attr + "\" is deprecated. Please use \"" +
self.deprecated_values[attr] + "\"!" + '\033[0m',
DeprecationWarning, stacklevel=2)
return getattr(self.new_target, self.deprecated_values[attr])
return getattr(self.new_target, attr)


class mvncStatus(Enum):
OK = 0
BUSY = -1
ERROR = -2
OUT_OF_MEMORY = -3
DEVICE_NOT_FOUND = -4
INVALID_PARAMETERS = -5
TIMEOUT = -6
MVCMD_NOT_FOUND = -7
NO_DATA = -8
GONE = -9
UNSUPPORTED_GRAPH_FILE = -10
MYRIAD_ERROR = -11

Status = EnumDeprecationHelper(mvncStatus, {"MVCMDNOTFOUND": "MVCMD_NOT_FOUND",
"NODATA": "NO_DATA",
"UNSUPPORTEDGRAPHFILE": "UNSUPPORTED_GRAPH_FILE",
"MYRIADERROR": "MYRIAD_ERROR"})


class mvncGlobalOption(Enum):
LOG_LEVEL = 0

GlobalOption = EnumDeprecationHelper(mvncGlobalOption, {"LOGLEVEL": "LOG_LEVEL"})


class mvncDeviceOption(Enum):
TEMP_LIM_LOWER = 1
TEMP_LIM_HIGHER = 2
BACKOFF_TIME_NORMAL = 3
BACKOFF_TIME_HIGH = 4
BACKOFF_TIME_CRITICAL = 5
TEMPERATURE_DEBUG = 6
THERMAL_STATS = 1000
OPTIMISATION_LIST = 1001
THERMAL_THROTTLING_LEVEL = 1002

DeviceOption = EnumDeprecationHelper(mvncDeviceOption, {"THERMALSTATS": "THERMAL_STATS",
"OPTIMISATIONLIST": "OPTIMISATION_LIST"})


class mvncGraphOption(Enum):
ITERATIONS = 0
NETWORK_THROTTLE = 1
DONT_BLOCK = 2
TIME_TAKEN = 1000
DEBUG_INFO = 1001

GraphOption = EnumDeprecationHelper(mvncGraphOption, {"DONTBLOCK": "DONT_BLOCK",
"TIMETAKEN": "TIME_TAKEN",
"DEBUGINFO": "DEBUG_INFO"})


def EnumerateDevices():
name = create_string_buffer(28)
i = 0
devices = []
while True:
if f.mvncGetDeviceName(i, name, 28) != 0:
break
devices.append(name.value.decode("utf-8"))
i = i + 1
return devices


def SetGlobalOption(opt, data):
data = c_int(data)
status = f.mvncSetGlobalOption(opt.value, pointer(data), sizeof(data))
if status != Status.OK.value:
raise Exception(Status(status))


def GetGlobalOption(opt):
if opt == GlobalOption.LOG_LEVEL:
optsize = c_uint()
optvalue = c_uint()
status = f.mvncGetGlobalOption(opt.value, byref(optvalue), byref(optsize))
if status != Status.OK.value:
raise Exception(Status(status))
return optvalue.value
optsize = c_uint()
optdata = POINTER(c_byte)()
status = f.mvncGetDeviceOption(0, opt.value, byref(optdata), byref(optsize))
if status != Status.OK.value:
raise Exception(Status(status))
v = create_string_buffer(optsize.value)
memmove(v, optdata, optsize.value)
return v.raw


class Device:
def __init__(self, name):
self.handle = c_void_p()
self.name = name

def OpenDevice(self):
status = f.mvncOpenDevice(bytes(bytearray(self.name, "utf-8")), byref(self.handle))
if status != Status.OK.value:
raise Exception(Status(status))

def CloseDevice(self):
status = f.mvncCloseDevice(self.handle)
self.handle = c_void_p()
if status != Status.OK.value:
raise Exception(Status(status))

def SetDeviceOption(self, opt, data):
if opt == DeviceOption.TEMP_LIM_HIGHER or opt == DeviceOption.TEMP_LIM_LOWER:
data = c_float(data)
else:
data = c_int(data)
status = f.mvncSetDeviceOption(self.handle, opt.value, pointer(data), sizeof(data))
if status != Status.OK.value:
raise Exception(Status(status))

def GetDeviceOption(self, opt):
if opt == DeviceOption.TEMP_LIM_HIGHER or opt == DeviceOption.TEMP_LIM_LOWER:
optdata = c_float()
elif (opt == DeviceOption.BACKOFF_TIME_NORMAL or opt == DeviceOption.BACKOFF_TIME_HIGH or
opt == DeviceOption.BACKOFF_TIME_CRITICAL or opt == DeviceOption.TEMPERATURE_DEBUG or
opt == DeviceOption.THERMAL_THROTTLING_LEVEL):
optdata = c_int()
else:
optdata = POINTER(c_byte)()
optsize = c_uint()
status = f.mvncGetDeviceOption(self.handle, opt.value, byref(optdata), byref(optsize))
if status != Status.OK.value:
raise Exception(Status(status))
if opt == DeviceOption.TEMP_LIM_HIGHER or opt == DeviceOption.TEMP_LIM_LOWER:
return optdata.value
elif (opt == DeviceOption.BACKOFF_TIME_NORMAL or opt == DeviceOption.BACKOFF_TIME_HIGH or
opt == DeviceOption.BACKOFF_TIME_CRITICAL or opt == DeviceOption.TEMPERATURE_DEBUG or
opt == DeviceOption.THERMAL_THROTTLING_LEVEL):
return optdata.value
v = create_string_buffer(optsize.value)
memmove(v, optdata, optsize.value)
if opt == DeviceOption.OPTIMISATION_LIST:
l = []
for i in range(40):
if v.raw[i * 50] != 0:
ss = v.raw[i * 50:]
end = ss.find(0)
l.append(ss[0:end].decode())
return l
if opt == DeviceOption.THERMAL_STATS:
return numpy.frombuffer(v.raw, dtype=numpy.float32)
return int.from_bytes(v.raw, byteorder='little')

def AllocateGraph(self, graphfile):
hgraph = c_void_p()
status = f.mvncAllocateGraph(self.handle, byref(hgraph), graphfile, len(graphfile))
if status != Status.OK.value:
raise Exception(Status(status))
return Graph(hgraph)


class Graph:
def __init__(self, handle):
self.handle = handle
self.userobjs = {}

def SetGraphOption(self, opt, data):
data = c_int(data)
status = f.mvncSetGraphOption(self.handle, opt.value, pointer(data), sizeof(data))
if status != Status.OK.value:
raise Exception(Status(status))

def GetGraphOption(self, opt):
if opt == GraphOption.ITERATIONS or opt == GraphOption.NETWORK_THROTTLE or opt == GraphOption.DONT_BLOCK:
optdata = c_int()
else:
optdata = POINTER(c_byte)()
optsize = c_uint()
status = f.mvncGetGraphOption(self.handle, opt.value, byref(optdata), byref(optsize))
if status != Status.OK.value:
raise Exception(Status(status))
if opt == GraphOption.ITERATIONS or opt == GraphOption.NETWORK_THROTTLE or opt == GraphOption.DONT_BLOCK:
return optdata.value
v = create_string_buffer(optsize.value)
memmove(v, optdata, optsize.value)
if opt == GraphOption.TIME_TAKEN:
return numpy.frombuffer(v.raw, dtype=numpy.float32)
if opt == GraphOption.DEBUG_INFO:
return v.raw[0:v.raw.find(0)].decode()
return int.from_bytes(v.raw, byteorder='little')

def DeallocateGraph(self):
status = f.mvncDeallocateGraph(self.handle)
self.handle = 0
if status != Status.OK.value:
raise Exception(Status(status))

def LoadTensor(self, tensor, userobj):
tensor = tensor.tostring()
userobj = py_object(userobj)
key = c_long(addressof(userobj))
self.userobjs[key.value] = userobj
status = f.mvncLoadTensor(self.handle, tensor, len(tensor), key)
if status == Status.BUSY.value:
return False
if status != Status.OK.value:
del self.userobjs[key.value]
raise Exception(Status(status))
return True

def GetResult(self):
tensor = c_void_p()
tensorlen = c_uint()
userobj = c_long()
status = f.mvncGetResult(self.handle, byref(tensor), byref(tensorlen), byref(userobj))
if status == Status.NO_DATA.value:
return None, None
if status != Status.OK.value:
raise Exception(Status(status))
v = create_string_buffer(tensorlen.value)
memmove(v, tensor, tensorlen.value)
tensor = numpy.frombuffer(v.raw, dtype=numpy.float16)
retuserobj = self.userobjs[userobj.value]
del self.userobjs[userobj.value]
return tensor, retuserobj.value
3 changes: 3 additions & 0 deletions api/src/97-usbboot.rules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SUBSYSTEM=="usb", ATTRS{idProduct}=="2150", ATTRS{idVendor}=="03e7", GROUP="users", MODE="0666", ENV{ID_MM_DEVICE_IGNORE}="1"
SUBSYSTEM=="usb", ATTRS{idProduct}=="f63b", ATTRS{idVendor}=="040e", GROUP="users", MODE="0666", ENV{ID_MM_DEVICE_IGNORE}="1"
SUBSYSTEM=="tty", ATTRS{idProduct}=="2150", ATTRS{idVendor}=="03e7", GROUP="users", MODE="0666", ENV{ID_MM_DEVICE_IGNORE}="1"
Loading

0 comments on commit 4fbe5a4

Please sign in to comment.