def create_relu(input_shape): import ngraph as ng input_shape = ng.impl.PartialShape(input_shape) param = ng.parameter(input_shape, dtype=np.float32, name="data") result = ng.relu(param, name="out") function = ng.Function(result, [param], "TestFunction") return function
def create_encoder(input_shape, levels = 4): import ngraph as ng # input input_node = ng.parameter(input_shape, np.float32, name="data") padding_begin = padding_end = [0, 0] strides = [1, 1] dilations = [1, 1] input_channels = [input_shape[1]] last_output = input_node # convolution layers for i in range(levels): input_c = input_channels[-1] output_c = input_c * 2 conv_w = np.random.uniform(0, 1, [output_c, input_c, 5, 5]).astype(np.float32) conv_node = ng.convolution(last_output, conv_w, strides, padding_begin, padding_end, dilations) input_channels.append(output_c) last_output = conv_node # deconvolution layers for i in range(levels): input_c = input_channels[-2] output_c = input_channels.pop(-1) deconv_w = np.random.uniform(0, 1, [output_c, input_c, 5, 5]).astype(np.float32) deconv_node = ng.convolution_backprop_data(last_output, deconv_w, strides) last_output = deconv_node # result last_output.set_friendly_name("out") result_node = ng.result(last_output) return ng.Function(result_node, [input_node], "Encoder")
def ngraph_embedding(ids, vocab_embeddings, vocab_size, embedding_dim, padding_idx, sparse): """ decomposing embedding with ngraph ops. """ import ngraph as ng from ngraph import opset8 as opset from openvino.inference_engine import IECore if vocab_embeddings is None: # vocab_embeddings = np.zeros( (vocab_size, embedding_dim)).astype("float32") node_ids = ng.parameter(shape=ids.shape, name='ids', dtype=ids.dtype) node_w = ng.parameter(shape=vocab_embeddings.shape, name='w', dtype=vocab_embeddings.dtype) if padding_idx == -1: padding_idx += vocab_size if padding_idx is not None: ''' mask W ''' masked_embeddings = np.ones(vocab_embeddings.shape, dtype='int64') masked_embeddings[padding_idx, :] = 0 # mask node_mask = ng.constant(masked_embeddings, name='mask', dtype=vocab_embeddings.dtype) node_masked_w = ng.multiply(node_w, node_mask) node_axis = ng.constant([0], name='const0', dtype=np.int64) node_gather = opset.gather(data=node_masked_w if padding_idx else node_w, indices=node_ids, axis=node_axis, batch_dims=0) graph = ng.result(node_gather, name='y') parameters = [node_ids, node_w] inputs_dict = {'ids': ids, "w": vocab_embeddings} # function = ng.Function(graph, parameters, "embedding") ie_network = ng.function_to_cnn(function) ie = IECore() executable_network = ie.load_network(ie_network, 'CPU') output = executable_network.infer(inputs_dict) return output
def __init__(self, dataflow, fusible=never_fusible): """ Performs fusion on the provided dataflow graph Implementation of: *Fast Greedy Weighted Fusion*, Ken Kennedy, Internal journal of Parallel Programming (2002): Download: https://drive.google.com/open?id=0B8aziUAQFjRTbDNjeGM5elpFeEk """ # Extracts clusters super(KernelFlowGraph, self).__init__(dataflow.transformer, dataflow.results) self.fusible = lambda x, y: fusible(self.transformer, x, y) successors = self.successors path_from, bad_path_from = self._compute_paths() edges = {(a, b) for a, _ in successors.items() for b in _} edges = sorted(edges, key=lambda x: (x[0].id, x[1].id)) clusters = dict((x, {x}) for e in edges for x in e) while edges: # Pop edges and adjusts order if necessary v, w = edges.pop() # Cannot be fused if w in bad_path_from[v]: continue # Merge vertices between v and w to_merge = self.between(v, w, path_from) for x in to_merge: clusters[v] |= clusters.pop(x) self.transfer_edges(v, x, successors) self.transfer_edges(v, x, path_from) self.transfer_edges(v, x, bad_path_from) edges = {(a, b) for a, _ in successors.items() for b in _} edges = sorted(edges, key=lambda x: (x[0].id, x[1].id)) # Creates adjacency list for each cluster extract_subgraph = lambda R: dict( (a, b & R) for a, b in list(dataflow.successors.items()) if a in R) clusters = {x: extract_subgraph(y) for x, y in list(clusters.items())} # Creates final adjacency list clusters = { x: ng.Function(y) if x.is_device_op else x for x, y in list(clusters.items()) } self.successors = { clusters[a]: {clusters[b] for b in lst} for a, lst in list(successors.items()) } # Saves dataflow for visualization self.dataflow = dataflow
# \ / # \ / # ____\____/____ # | Concat | # | concat | # |____________| # | # | # | # _______|_______ # | Result | # | result | # |_____________| import ngraph as ng import numpy as np data1 = ng.opset8.parameter([1, 3, 2, 2], np.int64) data1.friendly_name = "data1" # operation name data2 = ng.opset8.parameter([1, 2, 2, 2], np.int64) data2.friendly_name = "data2" # operation name concat = ng.opset8.concat([data1, data2], 1) concat.friendly_name = "concat" # operation name result = ng.opset8.result(concat) result.friendly_name = "result" # operation name f = ng.Function(result, [data1, data2], "function_name") #! [ngraph:graph]