def test_with_placeholder(): vx0 = chainer.Variable(np.random.rand(10, 11, 12).astype(np.float32)) vx1 = chainer.Variable(np.random.rand(10, 11, 12).astype(np.float32)) vy = chainer.functions.minimum(vx0, vx1) A = Placeholder(label="A") B = Placeholder(label="B") C = Placeholder(label="C") px0 = PlaceholderVariable([A, B, C]) px1 = PlaceholderVariable([A, B, C]) py = chainer.functions.minimum(px0, px1) graph = ChainerConverter().convert([px0, px1], [py]) A.value = 10 B.value = 11 C.value = 12 generate_kernel_test_case( description=f"[chainer] F.minimum with placeholder", graph=graph, backend=["webgpu", "webassembly"], inputs={ graph.inputs[0]: vx0.data, graph.inputs[1]: vx1.data }, expected={graph.outputs[0]: vy.data}, )
def test_with_placeholder(): vx1 = chainer.Variable(np.random.rand(10, 12).astype(np.float32) * 2 - 1) vx2 = chainer.Variable(np.random.rand(12, 14).astype(np.float32) * 2 - 1) vy = chainer.functions.matmul(vx1, vx2, False, False) M = Placeholder(label="M") K = Placeholder(label="K") N = Placeholder(label="N") px1 = PlaceholderVariable([M, K]) px2 = PlaceholderVariable([K, N]) py = chainer.functions.matmul(px1, px2, False, False) graph = ChainerConverter().convert([px1, px2], [py]) M.value = 10 K.value = 12 N.value = 14 generate_kernel_test_case( description=f"[chainer] F.matmul with placeholder", graph=graph, backend=["webgpu", "webassembly"], inputs={ graph.inputs[0]: vx1.data, graph.inputs[1]: vx2.data }, expected={graph.outputs[0]: vy.data})
def test_with_placeholder(): link = chainer.links.DilatedConvolution2D(None, 16, ksize=3, stride=1, pad=1, dilate=2) vx = chainer.Variable(np.random.rand(1, 3, 16, 16).astype(np.float32)) vy = link(vx) N = Placeholder(label="N") H = Placeholder(label="H") W = Placeholder(label="W") px = PlaceholderVariable([N, 3, H, W]) py = link(px) graph = ChainerConverter().convert([px], [py]) x = graph.inputs[0] y = graph.outputs[0] N.value = 1 H.value = 16 W.value = 16 generate_kernel_test_case( description=f"[chainer] L.DilatedConvolution2D with placeholder", graph=graph, backend=["webgpu", "webassembly"], inputs={x: vx.data}, expected={y: vy.data}, EPS=1e-2 )
def test_with_placeholder(): vx = chainer.Variable(np.random.rand(2, 20, 4, 5).astype(np.float32)) vy1, vy2, vy3 = chainer.functions.split_axis(vx, [5, 15], 1) N = Placeholder(label="N") H = Placeholder(label="H") W = Placeholder(label="W") px = PlaceholderVariable([N, 20, H, W]) py1, py2, py3 = chainer.functions.split_axis(px, [5, 15], 1) graph = ChainerConverter().convert([px], [py1, py2, py3]) N.value = 2 H.value = 4 W.value = 5 generate_kernel_test_case( description=f"[chainer] F.split_axis with placeholder", graph=graph, backend=["webgpu", "webassembly"], inputs={graph.inputs[0]: vx.data}, expected={ graph.outputs[0]: vy1.data, graph.outputs[1]: vy2.data, graph.outputs[2]: vy3.data }, )
def test_with_placeholder(): link = chainer.links.BatchNormalization(size=3) vx = chainer.Variable(np.random.rand(1, 3, 16, 16).astype(np.float32)) with chainer.using_config('train', False): vy = link(vx) N = Placeholder(label="N") H = Placeholder(label="H") W = Placeholder(label="W") px = PlaceholderVariable([N, 3, H, W]) with chainer.using_config('train', False): py = link(px) graph = ChainerConverter().convert([px], [py]) x = graph.inputs[0] y = graph.outputs[0] N.value = 1 H.value = 16 W.value = 16 generate_kernel_test_case( description=f"[chainer] L.FixedBatchNormalization with placeholder", graph=graph, backend=["webgpu", "webassembly"], inputs={x: vx.data}, expected={y: vy.data}, )
def test_with_placeholder(): link = chainer.links.Linear(16, 32) vx = chainer.Variable(np.random.rand(2, 16).astype(np.float32)) vy = link(vx) N = Placeholder(label="N") px = PlaceholderVariable([N, 16]) py = link(px) graph = ChainerConverter().convert([px], [py]) N.value = 2 generate_kernel_test_case( description=f"[chainer] L.Linear with placeholder", graph=graph, backend=["webgpu", "webassembly"], inputs={graph.inputs[0]: vx.data}, expected={graph.outputs[0]: vy.data}, )
def test_with_placeholder(): rhs = np.random.rand(8, 4) vx = chainer.Variable(np.random.rand(2, 8).astype(np.float32)) vy = vx @ rhs M = Placeholder(label="M") px = PlaceholderVariable([M, 8]) py = px @ rhs graph = ChainerConverter().convert([px], [py]) x = graph.inputs[0] y = graph.outputs[0] M.value = 2 generate_kernel_test_case( description=f"[chainer] F.MatMulVarConstant with placeholder", graph=graph, backend=["webgpu", "webassembly"], inputs={x: vx.data}, expected={y: vy.data}, )
def test_with_placeholder(): vx = chainer.Variable(np.random.rand(10, 11, 12).astype(np.float32)) vy = chainer.functions.max(vx, axis=1, keepdims=False) A = Placeholder(label="A") B = Placeholder(label="B") C = Placeholder(label="C") px = PlaceholderVariable([A, B, C]) py = chainer.functions.max(px, axis=1, keepdims=False) graph = ChainerConverter().convert([px], [py]) A.value = 10 B.value = 11 C.value = 12 generate_kernel_test_case( description=f"[chainer] F.max with placeholder", graph=graph, backend=["webgpu", "webassembly"], inputs={graph.inputs[0]: vx.data}, expected={graph.outputs[0]: vy.data}, )
def test_with_placeholder(): vx = chainer.Variable(np.random.rand(2, 20, 4, 8).astype(np.float32)) vy = chainer.functions.depth2space(vx, r=2) N = Placeholder(label="N") H = Placeholder(label="H") W = Placeholder(label="W") px = PlaceholderVariable([N, 20, H, W]) py = chainer.functions.depth2space(px, r=2) graph = ChainerConverter().convert([px], [py]) N.value = 2 H.value = 4 W.value = 8 generate_kernel_test_case( description=f"[chainer] F.depth2space with placeholder", graph=graph, backend=["webgpu", "webassembly"], inputs={graph.inputs[0]: vx.data}, expected={graph.outputs[0]: vy.data}, )
def test_with_placeholder(): vx = chainer.Variable(np.random.rand(1, 3, 16, 16).astype(np.float32)) vy = vx ** 2 H = Placeholder(label="H") W = Placeholder(label="W") px = PlaceholderVariable([1, 3, H, W]) py = px ** 2 graph = ChainerConverter().convert([px], [py]) H.value = 16 W.value = 16 generate_kernel_test_case( description=f"[chainer] F.PowVarConst with placeholder", graph=graph, backend=["webgpu", "webassembly"], inputs={graph.inputs[0]: vx.data}, expected={graph.outputs[0]: vy.data} )
def test_with_placeholder(): vx = chainer.Variable(np.random.rand(2, 16, 7, 7).astype(np.float32)) vy = chainer.functions.unpooling_2d(vx, ksize=3, stride=2, pad=0) N = Placeholder(label="N") C = Placeholder(label="C") px = PlaceholderVariable([N, C, 7, 7]) py = chainer.functions.unpooling_2d(px, ksize=3, stride=2, pad=0) graph = ChainerConverter().convert([px], [py]) N.value = 2 C.value = 16 generate_kernel_test_case( description=f"[chainer] F.unpooling_2d with placeholder", graph=graph, backend=["webgpu", "webassembly"], inputs={graph.inputs[0]: vx.data}, expected={graph.outputs[0]: vy.data}, )
def test_with_placeholder(): vx1 = chainer.Variable(np.random.rand(2, 8).astype(np.float32)) vx2 = chainer.Variable(np.random.rand(8, 6).astype(np.float32)) vy = vx1 @ vx2 M = Placeholder(label="M") N = Placeholder(label="N") px1 = PlaceholderVariable([M, 8]) px2 = PlaceholderVariable([8, N]) py = px1 @ px2 graph = ChainerConverter().convert([px1, px2], [py]) M.value = 2 N.value = 6 generate_kernel_test_case( description=f"[chainer] F.MatMulVarVar with placeholder", graph=graph, backend=["webgpu", "webassembly"], inputs={graph.inputs[0]: vx1.data, graph.inputs[1]: vx2.data}, expected={graph.outputs[0]: vy.data} )
def test_with_placeholder(): vx = chainer.Variable(np.random.rand(1, 3, 16, 16).astype(np.float32)) vy = chainer.functions.local_response_normalization(vx) N = Placeholder(label="N") C = Placeholder(label="C") H = Placeholder(label="H") W = Placeholder(label="W") px = PlaceholderVariable([N, C, H, W]) py = chainer.functions.local_response_normalization(px) graph = ChainerConverter().convert([px], [py]) x = graph.inputs[0] y = graph.outputs[0] N.value = 1 C.value = 3 H.value = 16 W.value = 16 generate_kernel_test_case( description= f"[chainer] F.local_response_normalization with placeholder", graph=graph, backend=["webgpu", "webassembly"], inputs={x: vx.data}, expected={y: vy.data}, )
def test_with_placeholder(): vx1 = chainer.Variable(np.random.rand(2, 10, 4, 5).astype(np.float32)) vx2 = chainer.Variable(np.random.rand(2, 15, 4, 5).astype(np.float32)) vy = chainer.functions.concat([vx1, vx2], axis=1) N = Placeholder(label="N") C1 = Placeholder(label="C1") C2 = Placeholder(label="C2") H = Placeholder(label="H") W = Placeholder(label="W") px1 = PlaceholderVariable([N, C1, H, W]) px2 = PlaceholderVariable([N, C2, H, W]) py = chainer.functions.concat([px1, px2], axis=1) graph = ChainerConverter().convert([px1, px2], [py]) N.value = 2 C1.value = 10 C2.value = 15 H.value = 4 W.value = 5 generate_kernel_test_case( description=f"[chainer] F.concat with placeholder", graph=graph, backend=["webgpu", "webassembly"], inputs={ graph.inputs[0]: vx1.data, graph.inputs[1]: vx2.data }, expected={graph.outputs[0]: vy.data}, )
def main(): sys.setrecursionlimit(10000) # workaround for deep copying large graph parser = argparse.ArgumentParser() parser.add_argument("kerasmodel") parser.add_argument("--backend", default="webgpu,webgl,webassembly,fallback", help="comma-separated list of backends") parser.add_argument( "--input_shape", required=True, action="append", help= "shape of blobs for inputs (example: '(1,3,224,224)'), can be specified multiple times" ) # parser.add_argument("--input_data_format", choices=["channels_first", "channels_last"]) parser.add_argument( "--out", help="output directory (default: <model>/webdnn_graph_descriptor)") parser.add_argument("--encoding", help="name of weight encoder") parser.add_argument("--visualize_ir", action="store_true") parser.add_argument( "--plugin", action="append", help="plugin python files which are imported before transpiling") args = parser.parse_args() console.stderr(f"[{path.basename(__file__)}] Generating feedforward graph") class_list = [] if args.plugin: for plugin_path in args.plugin: class_list += _load_plugin(plugin_path) custom_objects = {} if len(class_list) > 0: # custom_objects is a dictionary for load_model to load user-defined custom layers for k, v in class_list: custom_objects[k] = v input_shapes = [ Shape.parse(input_shape)[0] for input_shape in args.input_shape ] model = keras.models.load_model(args.kerasmodel, custom_objects=custom_objects, compile=False) model.build(input_shape=None) converter = KerasConverter(batch_size=Placeholder(label='N')) graph = converter.convert(model) traverse.dump(graph) for graph_input, input_shape in zip(graph.inputs, input_shapes): for p1, p2 in zip(graph_input.shape, input_shape): if not Placeholder.check_resolved( p1) and Placeholder.check_resolved(p2): p1.value = Placeholder.force_int(p2) elif Placeholder.check_resolved( p1) and not Placeholder.check_resolved(p2): raise ValueError( f'Shape mismatch: expected:{input_shape}, real:{graph_input.shape}, {p1} != {p2}' ) elif Placeholder.check_resolved(p1) and Placeholder.check_resolved( p2): assert p1 == p2, f'Shape mismatch: expected:{input_shape}, real:{graph_input.shape}, {p1} != {p2}' if args.out: output_dir = args.out else: output_dir = path.join(path.dirname(args.kerasmodel), "webdnn_graph_descriptor") os.makedirs(output_dir, exist_ok=True) if args.visualize_ir: ir_dot_path = path.join(output_dir, "ir.dot") with open(ir_dot_path, "w") as f: f.write(dump_dot(graph)) console.stderr( f"IR graph can be visualized with graphviz command: 'dot {ir_dot_path} -T png -o output.png'" ) console.stderr(f"[{path.basename(__file__)}] Generating graph descriptor") any_backend_failed = False backends = args.backend.split(",") for i, backend in enumerate(backends): console.stderr( f"[{path.basename(__file__)}] BackendName: {console.colorize(backend, console.Color.Cyan)}" ) try: graph_exec_data = generate_descriptor( backend, graph, constant_encoder_name=args.encoding) graph_exec_data.save(output_dir) except Exception as ex: if flags.DEBUG: raise ex any_backend_failed = True console.error( f"[{path.basename(__file__)}] Failed generating descriptor for {backend} backend" ) console.stderr(traceback.format_exc()) continue if any_backend_failed: exit(1)
def test_with_placeholder(): vx = chainer.Variable(np.random.rand(2, 1, 4, 1).astype(np.float32)) vy = chainer.functions.squeeze(vx, axis=None) N = Placeholder(label="N") C = Placeholder(label="C") H = Placeholder(label="H") W = Placeholder(label="W") px = PlaceholderVariable([N, C, H, W]) py = chainer.functions.squeeze(px, axis=None) graph = ChainerConverter().convert([px], [py]) N.value = 2 C.value = 1 H.value = 4 W.value = 1 generate_kernel_test_case( description=f"[chainer] F.squeeze with placeholder", graph=graph, backend=["webgpu", "webassembly"], inputs={graph.inputs[0]: vx.data}, expected={graph.outputs[0]: vy.data}, )
def test_with_placeholder(): vx = chainer.Variable(np.random.rand(2, 3, 4, 5).astype(np.float32)) vy = chainer.functions.expand_dims(vx, axis=1) N = Placeholder(label="N") C = Placeholder(label="C") H = Placeholder(label="H") W = Placeholder(label="W") px = PlaceholderVariable([N, C, H, W]) py = chainer.functions.expand_dims(px, axis=1) graph = ChainerConverter().convert([px], [py]) x = graph.inputs[0] y = graph.outputs[0] N.value = 2 C.value = 3 H.value = 4 W.value = 5 generate_kernel_test_case( description=f"[chainer] F.expand_dims with placeholder", graph=graph, backend=["webgpu", "webassembly"], inputs={x: vx.data}, expected={y: vy.data}, )